diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2012-05-11 22:08:50 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2012-05-11 22:08:50 +0000 |
commit | 93303819acd1d66ad477a2bb2b7d583471361b07 (patch) | |
tree | 82b43d857d1139e7a54d6f75d3bd87b672572acb /llvm/lib/Support/StringRef.cpp | |
parent | 0021bd2fe31cb93e3c4f1468eb5bbbbaa39a32e0 (diff) | |
download | bcm5719-llvm-93303819acd1d66ad477a2bb2b7d583471361b07.tar.gz bcm5719-llvm-93303819acd1d66ad477a2bb2b7d583471361b07.zip |
[Support/StringRef] Add find_last_not_of and {r,l,}trim.
llvm-svn: 156652
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index 97af0fff5ee..8aab4b2760e 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/edit_distance.h" + #include <bitset> using namespace llvm; @@ -230,6 +231,31 @@ StringRef::size_type StringRef::find_last_of(StringRef Chars, return npos; } +/// find_last_not_of - Find the last character in the string that is not +/// \arg C, or npos if not found. +StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const { + for (size_type i = min(From, Length) - 1, e = -1; i != e; --i) + if (Data[i] != C) + return i; + return npos; +} + +/// find_last_not_of - Find the last character in the string that is not in +/// \arg Chars, or npos if not found. +/// +/// Note: O(size() + Chars.size()) +StringRef::size_type StringRef::find_last_not_of(StringRef Chars, + size_t From) const { + std::bitset<1 << CHAR_BIT> CharBits; + for (size_type i = 0, e = Chars.size(); i != e; ++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; +} + void StringRef::split(SmallVectorImpl<StringRef> &A, StringRef Separators, int MaxSplit, bool KeepEmpty) const { |