[llvm-commits] [llvm] r79354 - in /llvm/trunk: include/llvm/ADT/StringRef.h unittests/ADT/StringRefTest.cpp
Daniel Dunbar
daniel at zuster.org
Tue Aug 18 11:26:35 PDT 2009
Author: ddunbar
Date: Tue Aug 18 13:26:35 2009
New Revision: 79354
URL: http://llvm.org/viewvc/llvm-project?rev=79354&view=rev
Log:
Add StringRef::count({char,StringRef})
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/unittests/ADT/StringRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=79354&r1=79353&r2=79354&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Aug 18 13:26:35 2009
@@ -172,6 +172,28 @@
return npos;
}
+ /// count - Return the number of occurrences of \arg C in the string.
+ size_t count(char C) const {
+ size_t Count = 0;
+ for (size_t i = 0, e = Length; i != e; ++i)
+ if (Data[i] == C)
+ return i;
+ return Count;
+ }
+
+ /// count - Return the number of non-overlapped occurrences of \arg Str in
+ /// the string.
+ size_t count(const StringRef &Str) const {
+ size_t Count = 0;
+ size_t N = Str.size();
+ if (N > Length)
+ return 0;
+ for (size_t i = 0, e = Length - N + 1; i != e; ++i)
+ if (substr(i, N).equals(Str))
+ ++Count;
+ return Count;
+ }
+
/// @}
/// @name Substring Operations
/// @{
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=79354&r1=79353&r2=79354&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Tue Aug 18 13:26:35 2009
@@ -94,6 +94,13 @@
EXPECT_TRUE(Str.find("hello") == 0);
EXPECT_TRUE(Str.find("ello") == 1);
EXPECT_TRUE(Str.find("zz") == StringRef::npos);
+
+ EXPECT_TRUE(Str.count('l') == 2);
+ EXPECT_TRUE(Str.count('z') == 0);
+ EXPECT_TRUE(Str.count("helloworld") == 0);
+ EXPECT_TRUE(Str.count("hello") == 1);
+ EXPECT_TRUE(Str.count("ello") == 1);
+ EXPECT_TRUE(Str.count("zz") == 0);
}
} // end anonymous namespace
More information about the llvm-commits
mailing list