[llvm] r277288 - [ADT] Add 'consume_front' and 'consume_back' methods to StringRef which

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 30 19:19:15 PDT 2016


Author: chandlerc
Date: Sat Jul 30 21:19:13 2016
New Revision: 277288

URL: http://llvm.org/viewvc/llvm-project?rev=277288&view=rev
Log:
[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

Modified:
    llvm/trunk/include/llvm/ADT/StringRef.h
    llvm/trunk/unittests/ADT/StringRefTest.cpp

Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=277288&r1=277287&r2=277288&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Sat Jul 30 21:19:13 2016
@@ -446,6 +446,30 @@ namespace llvm {
       return substr(0, size()-N);
     }
 
+    /// Returns true if this StringRef has the given prefix and removes that
+    /// prefix.
+    LLVM_ATTRIBUTE_ALWAYS_INLINE
+    LLVM_ATTRIBUTE_UNUSED_RESULT
+    bool consume_front(StringRef Prefix) {
+      if (!startswith(Prefix))
+        return false;
+
+      *this = drop_front(Prefix.size());
+      return true;
+    }
+
+    /// Returns true if this StringRef has the given suffix and removes that
+    /// suffix.
+    LLVM_ATTRIBUTE_ALWAYS_INLINE
+    LLVM_ATTRIBUTE_UNUSED_RESULT
+    bool consume_back(StringRef Suffix) {
+      if (!endswith(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

Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=277288&r1=277287&r2=277288&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Sat Jul 30 21:19:13 2016
@@ -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'));




More information about the llvm-commits mailing list