[PATCH] D23965: Add StringRef::keep_front() and StringRef::keep_back()

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 27 19:42:42 PDT 2016


zturner created this revision.
zturner added a subscriber: llvm-commits.

I've needed these methods time and time again, but always resort to using some incantation of slice(), substr(), or drop_front() / drop_back() with a length calculation.

While these methods are extremely simple, they can make certain operations very elegant, especially when you want to slice up a string in various ways and you want to chain the operations together.

https://reviews.llvm.org/D23965

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

Index: unittests/ADT/StringRefTest.cpp
===================================================================
--- unittests/ADT/StringRefTest.cpp
+++ unittests/ADT/StringRefTest.cpp
@@ -640,5 +640,48 @@
   EXPECT_NE(Str2.data(), Str2c.data());
 }
 
+TEST(StringRefTest, Drop) {
+  StringRef Test("StringRefTest::Drop");
+
+  StringRef Dropped = Test.drop_front(5);
+  EXPECT_EQ(Dropped, "gRefTest::Drop");
+
+  Dropped = Test.drop_back(5);
+  EXPECT_EQ(Dropped, "StringRefTest:");
+
+  Dropped = Test.drop_front(0);
+  EXPECT_EQ(Dropped, Test);
+
+  Dropped = Test.drop_back(0);
+  EXPECT_EQ(Dropped, Test);
+
+  Dropped = Test.drop_front(Test.size());
+  EXPECT_TRUE(Dropped.empty());
+
+  Dropped = Test.drop_back(Test.size());
+  EXPECT_TRUE(Dropped.empty());
+}
+
+TEST(StringRefTest, Keep) {
+  StringRef Test("StringRefTest::Keep");
+
+  StringRef Kept = Test.keep_front(5);
+  EXPECT_EQ(Kept, "Strin");
+
+  Kept = Test.keep_back(5);
+  EXPECT_EQ(Kept, ":Keep");
+
+  Kept = Test.keep_front(Test.size());
+  EXPECT_EQ(Kept, Test);
+
+  Kept = Test.keep_back(Test.size());
+  EXPECT_EQ(Kept, Test);
+
+  Kept = Test.keep_front(0);
+  EXPECT_TRUE(Kept.empty());
+
+  Kept = Test.keep_back(0);
+  EXPECT_TRUE(Kept.empty());
+}
 
 } // end anonymous namespace
Index: include/llvm/ADT/StringRef.h
===================================================================
--- include/llvm/ADT/StringRef.h
+++ include/llvm/ADT/StringRef.h
@@ -428,6 +428,28 @@
       return StringRef(Data + Start, std::min(N, Length - Start));
     }
 
+    /// Return a StringRef equal to 'this' but with only the first \p N
+    /// elements remaining.  If \p N is greater than the length of the
+    /// string, the entire string is returned.
+    LLVM_ATTRIBUTE_ALWAYS_INLINE
+    LLVM_ATTRIBUTE_UNUSED_RESULT
+    StringRef keep_front(size_t N = 1) const {
+      if (N >= size())
+        return *this;
+      return drop_back(size() - N);
+    }
+
+    /// Return a StringRef equal to 'this' but with only the first \p N
+    /// elements remaining.  If \p N is greater than the length of the
+    /// string, the entire string is returned.
+    LLVM_ATTRIBUTE_ALWAYS_INLINE
+    LLVM_ATTRIBUTE_UNUSED_RESULT
+    StringRef keep_back(size_t N = 1) const {
+      if (N >= size())
+        return *this;
+      return drop_front(size() - N);
+    }
+
     /// Return a StringRef equal to 'this' but with the first \p N elements
     /// dropped.
     LLVM_ATTRIBUTE_ALWAYS_INLINE


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23965.69499.patch
Type: text/x-patch
Size: 2461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160828/d0a3b289/attachment.bin>


More information about the llvm-commits mailing list