[libc-commits] [PATCH] D130112: [libc] Add a method `find_last_of` to StringView.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Jul 19 15:43:29 PDT 2022


sivachandra updated this revision to Diff 445963.
sivachandra added a comment.

Add tests for empty string views.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130112/new/

https://reviews.llvm.org/D130112

Files:
  libc/src/__support/CPP/StringView.h
  libc/test/src/__support/CPP/stringview_test.cpp


Index: libc/test/src/__support/CPP/stringview_test.cpp
===================================================================
--- libc/test/src/__support/CPP/stringview_test.cpp
+++ libc/test/src/__support/CPP/stringview_test.cpp
@@ -199,3 +199,53 @@
   ASSERT_FALSE(Tmp.find_first_of('c', 0) == 1);
   ASSERT_FALSE(Tmp.find_first_of('c', 1) == 1);
 }
+
+TEST(LlvmLibcStringViewTest, FindLastOf) {
+  StringView Tmp("abada");
+
+  ASSERT_EQ(Tmp.find_last_of('a'), size_t(4));
+  ASSERT_EQ(Tmp.find_last_of('a', 123), size_t(4));
+  ASSERT_EQ(Tmp.find_last_of('a', 5), size_t(4));
+  ASSERT_EQ(Tmp.find_last_of('a', 4), size_t(4));
+  ASSERT_EQ(Tmp.find_last_of('a', 3), size_t(2));
+  ASSERT_EQ(Tmp.find_last_of('a', 2), size_t(2));
+  ASSERT_EQ(Tmp.find_last_of('a', 1), size_t(0));
+  ASSERT_EQ(Tmp.find_last_of('a', 0), size_t(0));
+
+  ASSERT_EQ(Tmp.find_last_of('b'), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 123), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 5), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 4), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 3), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 2), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 1), size_t(1));
+  ASSERT_EQ(Tmp.find_last_of('b', 0), StringView::npos);
+
+  ASSERT_EQ(Tmp.find_last_of('d'), size_t(3));
+  ASSERT_EQ(Tmp.find_last_of('d', 123), size_t(3));
+  ASSERT_EQ(Tmp.find_last_of('d', 5), size_t(3));
+  ASSERT_EQ(Tmp.find_last_of('d', 4), size_t(3));
+  ASSERT_EQ(Tmp.find_last_of('d', 3), size_t(3));
+  ASSERT_EQ(Tmp.find_last_of('d', 2), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('d', 1), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('d', 0), StringView::npos);
+
+  ASSERT_EQ(Tmp.find_last_of('e'), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 123), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 5), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 4), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 3), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 2), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 1), StringView::npos);
+  ASSERT_EQ(Tmp.find_last_of('e', 0), StringView::npos);
+
+  StringView Empty;
+  ASSERT_EQ(Empty.find_last_of('a'), StringView::npos);
+  ASSERT_EQ(Empty.find_last_of('a', 0), StringView::npos);
+  ASSERT_EQ(Empty.find_last_of('a', 123), StringView::npos);
+
+  StringView Empty1("");
+  ASSERT_EQ(Empty1.find_last_of('a'), StringView::npos);
+  ASSERT_EQ(Empty1.find_last_of('a', 0), StringView::npos);
+  ASSERT_EQ(Empty1.find_last_of('a', 123), StringView::npos);
+}
Index: libc/src/__support/CPP/StringView.h
===================================================================
--- libc/src/__support/CPP/StringView.h
+++ libc/src/__support/CPP/StringView.h
@@ -176,6 +176,20 @@
     return npos;
   }
 
+  // Search for the last character matching the character
+  //
+  // Return the index of the last character equal to the |c| before End.
+  size_t find_last_of(const char c, size_t End = npos) const {
+    End = End > size() ? size() : End + 1;
+    StringView S = drop_back(size() - End);
+    while (!S.empty()) {
+      if (S.back() == c)
+        return S.size() - 1;
+      S = S.drop_back();
+    }
+    return npos;
+  }
+
   // Search for the first character satisfying the predicate Function
   //
   // Returns The index of the first character satisfying Function starting from


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130112.445963.patch
Type: text/x-patch
Size: 3385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220719/dc74d1a2/attachment-0001.bin>


More information about the libc-commits mailing list