[PATCH] D154975: [Support] extend llvm:Regex::sub() for global substitution

Ding Fei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 11 08:02:04 PDT 2023


danix800 created this revision.
danix800 added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
danix800 requested review of this revision.
Herald added a subscriber: llvm-commits.

Extend llvm::Regex::sub() to do global substitution, for reducing client code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154975

Files:
  llvm/include/llvm/Support/Regex.h
  llvm/lib/Support/Regex.cpp
  llvm/unittests/Support/RegexTest.cpp


Index: llvm/unittests/Support/RegexTest.cpp
===================================================================
--- llvm/unittests/Support/RegexTest.cpp
+++ llvm/unittests/Support/RegexTest.cpp
@@ -127,6 +127,9 @@
 
   EXPECT_EQ("aber", Regex("a[0-9]+b").sub("a\\100b", "a1234ber", &Error));
   EXPECT_EQ(Error, "invalid backreference string '100'");
+
+  // Global substitution
+  EXPECT_EQ("a%%b%%c%%d", Regex("%").sub("%%", "a%b%c%d", nullptr, true));
 }
 
 TEST_F(RegexTest, IsLiteralERE) {
Index: llvm/lib/Support/Regex.cpp
===================================================================
--- llvm/lib/Support/Regex.cpp
+++ llvm/lib/Support/Regex.cpp
@@ -131,7 +131,7 @@
 }
 
 std::string Regex::sub(StringRef Repl, StringRef String,
-                       std::string *Error) const {
+                       std::string *Error, bool Global) const {
   SmallVector<StringRef, 8> Matches;
 
   // Return the input if there was no match.
@@ -141,6 +141,7 @@
   // Otherwise splice in the replacement string, starting with the prefix before
   // the match.
   std::string Res(String.begin(), Matches[0].begin());
+  StringRef OrigRepl = Repl;
 
   // Then the replacement string, honoring possible substitutions.
   while (!Repl.empty()) {
@@ -197,8 +198,13 @@
     }
   }
 
+  StringRef Suffix = StringRef(Matches[0].end(), String.end() - Matches[0].end());
   // And finally the suffix.
-  Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
+  if (Global) {
+    Res += sub(OrigRepl, Suffix, Error, Global);
+  } else {
+    Res += Suffix;
+  }
 
   return Res;
 }
Index: llvm/include/llvm/Support/Regex.h
===================================================================
--- llvm/include/llvm/Support/Regex.h
+++ llvm/include/llvm/Support/Regex.h
@@ -95,7 +95,7 @@
     /// backreferences, trailing backslashes) will be recorded as a non-empty
     /// string. If there is no error, it will be an empty string.
     std::string sub(StringRef Repl, StringRef String,
-                    std::string *Error = nullptr) const;
+                    std::string *Error = nullptr, bool Global = false) const;
 
     /// If this function returns true, ^Str$ is an extended regular
     /// expression that matches Str and only Str.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154975.539108.patch
Type: text/x-patch
Size: 2255 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230711/43401080/attachment.bin>


More information about the llvm-commits mailing list