[PATCH] D22723: [ADT] Add 'consume_front' and 'consume_back' methods to StringRef which are very handy when parsing text.
Pete Cooper via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 23 10:44:32 PDT 2016
Very useful! I know a few places I would have liked to have had these.
LGTM.
Pete
Sent from my iPhone
> On Jul 23, 2016, at 1:40 AM, Chandler Carruth via llvm-commits <llvm-commits at lists.llvm.org> wrote:
>
> chandlerc created this revision.
> chandlerc added a subscriber: llvm-commits.
> Herald added a subscriber: mcrosier.
>
> They are essentially a combination of startswith and a self-modifying
> drop_front, or endswith and drop_back respectively.
>
> https://reviews.llvm.org/D22723
>
> Files:
> include/llvm/ADT/StringRef.h
> unittests/ADT/StringRefTest.cpp
>
> Index: unittests/ADT/StringRefTest.cpp
> ===================================================================
> --- unittests/ADT/StringRefTest.cpp
> +++ unittests/ADT/StringRefTest.cpp
> @@ -322,6 +322,21 @@
> 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"));
> +}
> +
> TEST(StringRefTest, EndsWith) {
> StringRef Str("hello");
> EXPECT_TRUE(Str.endswith(""));
> @@ -341,6 +356,21 @@
> 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"));
> +}
> +
> TEST(StringRefTest, Find) {
> StringRef Str("hello");
> EXPECT_EQ(2U, Str.find('l'));
> Index: include/llvm/ADT/StringRef.h
> ===================================================================
> --- include/llvm/ADT/StringRef.h
> +++ include/llvm/ADT/StringRef.h
> @@ -446,6 +446,28 @@
> return substr(0, size()-N);
> }
>
> + /// Returns true if this StringRef has the given prefix and removes that
> + /// prefix.
> + LLVM_ATTRIBUTE_ALWAYS_INLINE
> + 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
> + 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
>
>
> <D22723.65218.patch>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list