[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 12:27:45 PDT 2022


sivachandra created this revision.
sivachandra added a reviewer: jeffbailey.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
sivachandra requested review of this revision.

Repository:
  rG LLVM Github Monorepo

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,43 @@
   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);
+}
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.445908.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220719/0ec35c09/attachment.bin>


More information about the libc-commits mailing list