diff options
| author | Vedant Kumar <vsk@apple.com> | 2016-02-16 01:48:39 +0000 |
|---|---|---|
| committer | Vedant Kumar <vsk@apple.com> | 2016-02-16 01:48:39 +0000 |
| commit | f114b40c7310d04b984dd0b0800718f74382d991 (patch) | |
| tree | 773ea7e1e1a8b95c9bfc5204eebe8d5e10a5a89b | |
| parent | 975ce8fab505b29c9a49f6d1f0d84193f81d3661 (diff) | |
| download | bcm5719-llvm-f114b40c7310d04b984dd0b0800718f74382d991.tar.gz bcm5719-llvm-f114b40c7310d04b984dd0b0800718f74382d991.zip | |
[ADT] Add StringRef::{l,r}trim(char) overloads (NFC)
Add support for trimming a single kind of character from a StringRef.
This makes the common case of trimming null bytes much neater. It's also
probably a bit speedier too, since it avoids creating a std::bitset in
find_{first,last}_not_of.
llvm-svn: 260925
| -rw-r--r-- | llvm/include/llvm/ADT/StringRef.h | 18 | ||||
| -rw-r--r-- | llvm/unittests/ADT/StringRefTest.cpp | 2 |
2 files changed, 19 insertions, 1 deletions
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 350032b8c4e..90d0a159dcd 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -539,18 +539,36 @@ namespace llvm { return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); } + /// Return string with consecutive \p Char characters starting from the + /// the left removed. + StringRef ltrim(char Char) const { + return drop_front(std::min(Length, find_first_not_of(Char))); + } + /// Return string with consecutive characters in \p Chars starting from /// the left removed. StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const { return drop_front(std::min(Length, find_first_not_of(Chars))); } + /// Return string with consecutive \p Char characters starting from the + /// right removed. + StringRef rtrim(char Char) const { + return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1)); + } + /// Return string with consecutive characters in \p Chars starting from /// the right removed. StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const { return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1)); } + /// Return string with consecutive \p Char characters starting from the + /// left and right removed. + StringRef trim(char Char) const { + return ltrim(Char).rtrim(Char); + } + /// Return string with consecutive characters in \p Chars starting from /// the left and right removed. StringRef trim(StringRef Chars = " \t\n\v\f\r") const { diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 6cf2e6a0454..6354026d7ae 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -301,7 +301,7 @@ TEST(StringRefTest, Trim) { EXPECT_EQ(StringRef(""), StringRef(" ").trim()); EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim()); EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim()); - EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1))); + EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0')); } TEST(StringRefTest, StartsWith) { |

