[llvm] [llvm][ADT] Add llvm::StringRef::consume_front(char) overload (PR #172832)
Michael Buch via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 18 02:31:53 PST 2025
https://github.com/Michael137 created https://github.com/llvm/llvm-project/pull/172832
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 uses a helper to do 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.
>From 7028a626d2f4a06c348bac04a847b1349f2a0420 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Thu, 18 Dec 2025 10:27:53 +0000
Subject: [PATCH] [llvm][ADT] Add llvm::StringRef::consume_front(char) overload
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/include/llvm/ADT/StringRef.h | 11 +++++++++++
llvm/unittests/ADT/StringRefTest.cpp | 20 ++++++++++++++++++++
2 files changed, 31 insertions(+)
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(""));
More information about the llvm-commits
mailing list