[PATCH] D104218: [ADT] Add StringRef consume_front_lower and consume_back_lower

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 14 04:47:47 PDT 2021


mstorsjo created this revision.
mstorsjo added reviewers: chandlerc, MaskRay, lattner.
Herald added a subscriber: dexonsmith.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

These serve as a convenient combination of consume_front/back and
startswith_lower/endswith_lower.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104218

Files:
  llvm/include/llvm/ADT/StringRef.h
  llvm/unittests/ADT/StringRefTest.cpp


Index: llvm/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/unittests/ADT/StringRefTest.cpp
+++ 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");
Index: llvm/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/include/llvm/ADT/StringRef.h
+++ 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104218.351830.patch
Type: text/x-patch
Size: 2913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210614/5e32b319/attachment.bin>


More information about the llvm-commits mailing list