summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ADT/StringRefTest.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2016-07-31 02:19:13 +0000
committerChandler Carruth <chandlerc@gmail.com>2016-07-31 02:19:13 +0000
commit974c67e7c6101328c3791e560fa187227a525065 (patch)
tree408707ad1302650258a6b172e72909e99ea17c96 /llvm/unittests/ADT/StringRefTest.cpp
parent5e5501861a268e09bb5307ca41112f91e49bc08d (diff)
downloadbcm5719-llvm-974c67e7c6101328c3791e560fa187227a525065.tar.gz
bcm5719-llvm-974c67e7c6101328c3791e560fa187227a525065.zip
[ADT] Add 'consume_front' and 'consume_back' methods to StringRef which
are very handy when parsing text. They are essentially a combination of startswith and a self-modifying drop_front, or endswith and drop_back respectively. Differential Revision: https://reviews.llvm.org/D22723 llvm-svn: 277288
Diffstat (limited to 'llvm/unittests/ADT/StringRefTest.cpp')
-rw-r--r--llvm/unittests/ADT/StringRefTest.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index 66e5944b56e..72018869742 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -322,6 +322,22 @@ TEST(StringRefTest, StartsWithLower) {
EXPECT_FALSE(Str.startswith_lower("hi"));
}
+TEST(StringRefTest, ConsumeFront) {
+ StringRef Str("hello");
+ EXPECT_TRUE(Str.consume_front(""));
+ EXPECT_EQ("hello", Str);
+ EXPECT_TRUE(Str.consume_front("he"));
+ EXPECT_EQ("llo", Str);
+ EXPECT_FALSE(Str.consume_front("lloworld"));
+ EXPECT_EQ("llo", Str);
+ EXPECT_FALSE(Str.consume_front("lol"));
+ EXPECT_EQ("llo", Str);
+ EXPECT_TRUE(Str.consume_front("llo"));
+ EXPECT_EQ("", Str);
+ EXPECT_FALSE(Str.consume_front("o"));
+ EXPECT_TRUE(Str.consume_front(""));
+}
+
TEST(StringRefTest, EndsWith) {
StringRef Str("hello");
EXPECT_TRUE(Str.endswith(""));
@@ -341,6 +357,22 @@ TEST(StringRefTest, EndsWithLower) {
EXPECT_FALSE(Str.endswith_lower("hi"));
}
+TEST(StringRefTest, ConsumeBack) {
+ StringRef Str("hello");
+ EXPECT_TRUE(Str.consume_back(""));
+ EXPECT_EQ("hello", Str);
+ EXPECT_TRUE(Str.consume_back("lo"));
+ EXPECT_EQ("hel", Str);
+ EXPECT_FALSE(Str.consume_back("helhel"));
+ EXPECT_EQ("hel", Str);
+ EXPECT_FALSE(Str.consume_back("hle"));
+ EXPECT_EQ("hel", Str);
+ EXPECT_TRUE(Str.consume_back("hel"));
+ EXPECT_EQ("", Str);
+ EXPECT_FALSE(Str.consume_back("h"));
+ EXPECT_TRUE(Str.consume_back(""));
+}
+
TEST(StringRefTest, Find) {
StringRef Str("hello");
EXPECT_EQ(2U, Str.find('l'));
OpenPOWER on IntegriCloud