[llvm-commits] [llvm] r78712 - in /llvm/trunk: include/llvm/ADT/StringRef.h unittests/ADT/StringRefTest.cpp

Daniel Dunbar daniel at zuster.org
Tue Aug 11 13:47:15 PDT 2009


Author: ddunbar
Date: Tue Aug 11 15:47:15 2009
New Revision: 78712

URL: http://llvm.org/viewvc/llvm-project?rev=78712&view=rev
Log:
StringRef: Add find(char) and find(StringRef).

Also, regroup functions.

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=78712&r1=78711&r2=78712&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Aug 11 15:47:15 2009
@@ -123,7 +123,50 @@
     }
 
     /// @}
-    /// @name Utility Functions
+    /// @name String Predicates
+    /// @{
+
+    /// startswith - Check if this string starts with the given \arg Prefix.
+    bool startswith(const StringRef &Prefix) const { 
+      return substr(0, Prefix.Length).equals(Prefix);
+    }
+
+    /// endswith - Check if this string ends with the given \arg Suffix.
+    bool endswith(const StringRef &Suffix) const {
+      return slice(size() - Suffix.Length, size()).equals(Suffix);
+    }
+
+    /// @}
+    /// @name String Searching
+    /// @{
+
+    /// find - Search for the character \arg C in the string.
+    ///
+    /// \return - The index of the first occurence of \arg C, or npos if not
+    /// found.
+    size_t find(char C) const {
+      for (size_t i = 0, e = Length; i != e; ++i)
+        if (Data[i] == C)
+          return i;
+      return npos;
+    }
+
+    /// find - Search for the string \arg Str in the string.
+    ///
+    /// \return - The index of the first occurence of \arg Str, or npos if not
+    /// found.
+    size_t find(const StringRef &Str) const {
+      size_t N = Str.size();
+      if (N > Length)
+        return npos;
+      for (size_t i = 0, e = Length - N + 1; i != e; ++i)
+        if (substr(i, N).equals(Str))
+          return i;
+      return npos;
+    }
+
+    /// @}
+    /// @name Substring Operations
     /// @{
 
     /// substr - Return a reference to the substring from [Start, Start + N).
@@ -167,21 +210,10 @@
     /// \param Separator - The character to split on.
     /// \return - The split substrings.
     std::pair<StringRef, StringRef> split(char Separator) const {
-      iterator it = std::find(begin(), end(), Separator);
-      if (it == end())
+      size_t Idx = find(Separator);
+      if (Idx == npos)
         return std::make_pair(*this, StringRef());
-      return std::make_pair(StringRef(begin(), it - begin()),
-                            StringRef(it + 1, end() - (it + 1)));
-    }
-
-    /// startswith - Check if this string starts with the given \arg Prefix.
-    bool startswith(const StringRef &Prefix) const { 
-      return substr(0, Prefix.Length).equals(Prefix);
-    }
-
-    /// endswith - Check if this string ends with the given \arg Suffix.
-    bool endswith(const StringRef &Suffix) const {
-      return slice(size() - Suffix.Length, size()).equals(Suffix);
+      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
     }
 
     /// @}

Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=78712&r1=78711&r2=78712&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Tue Aug 11 15:47:15 2009
@@ -87,6 +87,13 @@
   raw_string_ostream OS(Storage);
   OS << StringRef("hello");
   EXPECT_EQ("hello", OS.str());
+
+  EXPECT_TRUE(Str.find('l') == 2);
+  EXPECT_TRUE(Str.find('z') == StringRef::npos);
+  EXPECT_TRUE(Str.find("helloworld") == StringRef::npos);
+  EXPECT_TRUE(Str.find("hello") == 0);
+  EXPECT_TRUE(Str.find("ello") == 1);
+  EXPECT_TRUE(Str.find("zz") == StringRef::npos);
 }
 
 } // end anonymous namespace





More information about the llvm-commits mailing list