[libc-commits] [libc] f8322d1 - [libc] Add a method `find_last_of` to StringView.

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


Author: Siva Chandra Reddy
Date: 2022-07-19T22:43:43Z
New Revision: f8322d135176b2aebcd13d31ba51e182135db8d0

URL: https://github.com/llvm/llvm-project/commit/f8322d135176b2aebcd13d31ba51e182135db8d0
DIFF: https://github.com/llvm/llvm-project/commit/f8322d135176b2aebcd13d31ba51e182135db8d0.diff

LOG: [libc] Add a method `find_last_of` to StringView.

Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D130112

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/StringView.h b/libc/src/__support/CPP/StringView.h
index e8387aeedd6a6..d0bca3603450b 100644
--- a/libc/src/__support/CPP/StringView.h
+++ b/libc/src/__support/CPP/StringView.h
@@ -176,6 +176,20 @@ class StringView {
     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

diff  --git a/libc/test/src/__support/CPP/stringview_test.cpp b/libc/test/src/__support/CPP/stringview_test.cpp
index a20116b29edac..f0e38f1523654 100644
--- a/libc/test/src/__support/CPP/stringview_test.cpp
+++ b/libc/test/src/__support/CPP/stringview_test.cpp
@@ -199,3 +199,53 @@ TEST(LlvmLibcStringViewTest, FindFirstOf) {
   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);
+}


        


More information about the libc-commits mailing list