diff options
author | Zachary Turner <zturner@google.com> | 2016-09-25 03:27:29 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2016-09-25 03:27:29 +0000 |
commit | 84505f9a9c958e5728c4b500ef429c29f72a7ff5 (patch) | |
tree | 20014e9941fb64c7c01db440c8adc08ae5726866 /llvm/unittests/ADT/StringRefTest.cpp | |
parent | 9e1b0b5b788ac4ede3f239acddfeed10e84aaeac (diff) | |
download | llvm-84505f9a9c958e5728c4b500ef429c29f72a7ff5.zip llvm-84505f9a9c958e5728c4b500ef429c29f72a7ff5.tar.gz llvm-84505f9a9c958e5728c4b500ef429c29f72a7ff5.tar.bz2 |
Add some predicated searching functions to StringRef.
This adds 4 new functions to StringRef, which can be used to
take or drop characters while a certain condition is met, or
until a certain condition is met. They are:
take_while - Return characters until a condition is not met.
take_until - Return characters until a condition is met.
drop_while - Remove characters until a condition is not met.
drop_until - Remove characters until a condition is met.
Internally, all of these functions delegate to two additional
helper functions which can be used to search for the position
of a character meeting or not meeting a condition, which are:
find_if - Find the first character matching a predicate.
find_if_not - Find the first character not matching a predicate.
Differential Revision: https://reviews.llvm.org/D24842
llvm-svn: 282346
Diffstat (limited to 'llvm/unittests/ADT/StringRefTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/StringRefTest.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 39ed71b..e1f5496 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -864,4 +864,60 @@ TEST(StringRefTest, Take) { EXPECT_TRUE(Taken.empty()); } +TEST(StringRefTest, FindIf) { + StringRef Punct("Test.String"); + StringRef NoPunct("ABCDEFG"); + StringRef Empty; + + auto IsPunct = [](char c) { return ::ispunct(c); }; + auto IsAlpha = [](char c) { return ::isalpha(c); }; + EXPECT_EQ(4, Punct.find_if(IsPunct)); + EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct)); + EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct)); + + EXPECT_EQ(4, Punct.find_if_not(IsAlpha)); + EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha)); + EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha)); +} + +TEST(StringRefTest, TakeWhileUntil) { + StringRef Test("String With 1 Number"); + + StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); }); + EXPECT_EQ("", Taken); + + Taken = Test.take_until([](char c) { return ::isdigit(c); }); + EXPECT_EQ("String With ", Taken); + + Taken = Test.take_while([](char c) { return true; }); + EXPECT_EQ(Test, Taken); + + Taken = Test.take_until([](char c) { return true; }); + EXPECT_EQ("", Taken); + + Test = ""; + Taken = Test.take_while([](char c) { return true; }); + EXPECT_EQ("", Taken); +} + +TEST(StringRefTest, DropWhileUntil) { + StringRef Test("String With 1 Number"); + + StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); }); + EXPECT_EQ(Test, Taken); + + Taken = Test.drop_until([](char c) { return ::isdigit(c); }); + EXPECT_EQ("1 Number", Taken); + + Taken = Test.drop_while([](char c) { return true; }); + EXPECT_EQ("", Taken); + + Taken = Test.drop_until([](char c) { return true; }); + EXPECT_EQ(Test, Taken); + + StringRef EmptyString = ""; + Taken = EmptyString.drop_while([](char c) { return true; }); + EXPECT_EQ("", Taken); +} + } // end anonymous namespace |