diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -685,6 +685,16 @@ return true; } + /// Returns true if this StringRef has the given prefix, ignoring case, + /// and removes that prefix. + bool consume_front_lower(StringRef Prefix) { + if (!startswith_lower(Prefix)) + return false; + + *this = drop_front(Prefix.size()); + return true; + } + /// Returns true if this StringRef has the given suffix and removes that /// suffix. bool consume_back(StringRef Suffix) { @@ -695,6 +705,16 @@ return true; } + /// Returns true if this StringRef has the given suffix, ignoring case, + /// and removes that suffix. + bool consume_back_lower(StringRef Suffix) { + if (!endswith_lower(Suffix)) + return false; + + *this = drop_back(Suffix.size()); + return true; + } + /// Return a reference to the substring from [Start, End). /// /// \param Start The index of the starting character in the substring; if diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -392,6 +392,24 @@ EXPECT_TRUE(Str.consume_front("")); } +TEST(StringRefTest, ConsumeFrontLower) { + StringRef Str("heLLo"); + EXPECT_TRUE(Str.consume_front_lower("")); + EXPECT_EQ("heLLo", Str); + EXPECT_FALSE(Str.consume_front("HEl")); + EXPECT_EQ("heLLo", Str); + EXPECT_TRUE(Str.consume_front_lower("HEl")); + EXPECT_EQ("Lo", Str); + EXPECT_FALSE(Str.consume_front_lower("loworld")); + EXPECT_EQ("Lo", Str); + EXPECT_FALSE(Str.consume_front_lower("ol")); + EXPECT_EQ("Lo", Str); + EXPECT_TRUE(Str.consume_front_lower("lo")); + EXPECT_EQ("", Str); + EXPECT_FALSE(Str.consume_front_lower("o")); + EXPECT_TRUE(Str.consume_front_lower("")); +} + TEST(StringRefTest, EndsWith) { StringRef Str("hello"); EXPECT_TRUE(Str.endswith("")); @@ -427,6 +445,24 @@ EXPECT_TRUE(Str.consume_back("")); } +TEST(StringRefTest, ConsumeBackLower) { + StringRef Str("heLLo"); + EXPECT_TRUE(Str.consume_back_lower("")); + EXPECT_EQ("heLLo", Str); + EXPECT_FALSE(Str.consume_back("lO")); + EXPECT_EQ("heLLo", Str); + EXPECT_TRUE(Str.consume_back_lower("lO")); + EXPECT_EQ("heL", Str); + EXPECT_FALSE(Str.consume_back_lower("helhel")); + EXPECT_EQ("heL", Str); + EXPECT_FALSE(Str.consume_back_lower("hle")); + EXPECT_EQ("heL", Str); + EXPECT_TRUE(Str.consume_back_lower("hEl")); + EXPECT_EQ("", Str); + EXPECT_FALSE(Str.consume_back_lower("h")); + EXPECT_TRUE(Str.consume_back_lower("")); +} + TEST(StringRefTest, Find) { StringRef Str("helloHELLO"); StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");