Support/ADT/StringRef: Add find_last_of.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 30 Nov 2010 23:27:35 +0000 (23:27 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 30 Nov 2010 23:27:35 +0000 (23:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120495 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringRef.h
lib/Support/StringRef.cpp

index 364ace7e1d0397b70a60ec5d6768b96636347dd1..1766d2b9f2d017f78346292a1773f13c7f6992d7 100644 (file)
@@ -256,6 +256,18 @@ namespace llvm {
     /// Note: O(size() + Chars.size())
     size_type find_first_not_of(StringRef Chars, size_t From = 0) const;
 
+    /// find_last_of - Find the last character in the string that is \arg C, or
+    /// npos if not found.
+    size_type find_last_of(char C, size_t From = npos) const {
+      return rfind(C, From);
+    }
+
+    /// find_last_of - Find the last character in the string that is in \arg C,
+    /// or npos if not found.
+    ///
+    /// Note: O(size() + Chars.size())
+    size_type find_last_of(StringRef Chars, size_t From = npos) const;
+
     /// @}
     /// @name Helpful Algorithms
     /// @{
index 0e09122e766395d8df60150618c2b73ab00d16b1..8e636c3317f3f225e378df4791e2b19923b2bb60 100644 (file)
@@ -200,6 +200,21 @@ StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
   return npos;
 }
 
+/// find_last_of - Find the last character in the string that is in \arg C,
+/// or npos if not found.
+///
+/// Note: O(size() + Chars.size())
+StringRef::size_type StringRef::find_last_of(StringRef Chars,
+                                             size_t From) const {
+  std::bitset<1 << CHAR_BIT> CharBits;
+  for (size_type i = 0; i != Chars.size(); ++i)
+    CharBits.set((unsigned char)Chars[i]);
+
+  for (size_type i = min(From, Length) - 1, e = -1; i != e; --i)
+    if (CharBits.test((unsigned char)Data[i]))
+      return i;
+  return npos;
+}
 
 //===----------------------------------------------------------------------===//
 // Helpful Algorithms