-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[llvm][ADT] Add llvm::StringRef::consume_front(char) overload #172832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[llvm][ADT] Add llvm::StringRef::consume_front(char) overload #172832
Conversation
This patch adds support for consuming a `char` from the front of a `StringRef`. Most of the time a user wanting to consume a single character off the front can just wrap the character in a string literal (i.e., `consume_front("a")`). But this doesn't work if we don't have a `char` literal, but instead a variable of type `char`. I.e., `char c = 'a'; str.consume_front(c)`. This patch adds the `char` overload. We already have a `starts_with(char)` overload, so there's at least some related precedent.
|
@llvm/pr-subscribers-llvm-adt Author: Michael Buch (Michael137) ChangesThis patch adds support for consuming a Full diff: https://github.com/llvm/llvm-project/pull/172832.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 7aee2aa67ddec..2f85bb1aefdb0 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -632,6 +632,17 @@ namespace llvm {
return substr(find_if(F));
}
+ /// Returns true if this StringRef has the given prefix and removes that
+ /// prefix.
+ bool consume_front(char Prefix) {
+ if (!starts_with(Prefix))
+ return false;
+
+ *this = drop_front();
+
+ return true;
+ }
+
/// Returns true if this StringRef has the given prefix and removes that
/// prefix.
bool consume_front(StringRef Prefix) {
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index 1ace29e96dbb8..888cef9937e73 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -391,6 +391,26 @@ TEST(StringRefTest, ConsumeFront) {
EXPECT_TRUE(Str.consume_front(""));
}
+TEST(StringRefTest, ConsumeFrontChar) {
+ {
+ StringRef Str("hello");
+ EXPECT_EQ("hello", Str);
+ EXPECT_TRUE(Str.consume_front('h'));
+ EXPECT_EQ("ello", Str);
+ EXPECT_FALSE(Str.consume_front('h'));
+ EXPECT_EQ("ello", Str);
+ }
+
+ {
+ char Prefix = 'h';
+ StringRef Str("h");
+ EXPECT_TRUE(Str.consume_front(Prefix));
+ EXPECT_EQ("", Str);
+ EXPECT_FALSE(Str.consume_front('\0'));
+ EXPECT_EQ("", Str);
+ }
+}
+
TEST(StringRefTest, ConsumeFrontInsensitive) {
StringRef Str("heLLo");
EXPECT_TRUE(Str.consume_front_insensitive(""));
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add char versions of consume_front_insensitive, consume_back, consume_back_insensitive for the sake of completeness?
Do people prefer landing them together or is it ok to do this incrementally? |
|
Either should be fine. |
Sounds good! I'd prefer implementing them separately then to aid reviewability |
Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/20566 Here is the relevant piece of the build log for the reference |
…72832) This patch adds support for consuming a `char` from the front of a `StringRef`. Most of the time a user wanting to consume a single character off the front can just wrap the character in a string literal (i.e., `consume_front("a")`). But this doesn't work if we don't have a `char` literal, but instead a variable of type `char`. I.e., `char c = 'a'; str.consume_front(c)`. There's at least one helper in LLDB that does this. Also there's plenty of example of `consume_front` being passed a single character via string literal. This patch adds the `char` overload. We already have a `starts_with(char)` overload, so there's at least some related precedent.
This patch adds support for consuming a
charfrom the front of aStringRef. Most of the time a user wanting to consume a single character off the front can just wrap the character in a string literal (i.e.,consume_front("a")). But this doesn't work if we don't have acharliteral, but instead a variable of typechar. I.e.,char c = 'a'; str.consume_front(c). There's at least one helper in LLDB that does this. Also there's plenty of example ofconsume_frontbeing passed a single character via string literal. This patch adds thecharoverload. We already have astarts_with(char)overload, so there's at least some related precedent.