diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-12-18 20:30:12 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-04-06 20:11:55 +0000 |
commit | 5f757f3ff9144b609b3c433dfd370cc6bdc191ad (patch) | |
tree | 1b4e980b866cd26a00af34c0a653eb640bd09caf /contrib/llvm-project/llvm/lib/Support/Regex.cpp | |
parent | 3e1c8a35f741a5d114d0ba670b15191355711fe9 (diff) | |
parent | 312c0ed19cc5276a17bacf2120097bec4515b0f1 (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/Regex.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/Support/Regex.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/Regex.cpp b/contrib/llvm-project/llvm/lib/Support/Regex.cpp index dfbd373e4a98..5eedf95c48e3 100644 --- a/contrib/llvm-project/llvm/lib/Support/Regex.cpp +++ b/contrib/llvm-project/llvm/lib/Support/Regex.cpp @@ -92,6 +92,10 @@ bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches, unsigned nmatch = Matches ? preg->re_nsub+1 : 0; + // Update null string to empty string. + if (String.data() == nullptr) + String = ""; + // pmatch needs to have at least one element. SmallVector<llvm_regmatch_t, 8> pm; pm.resize(nmatch > 0 ? nmatch : 1); @@ -163,6 +167,25 @@ std::string Regex::sub(StringRef Repl, StringRef String, // FIXME: We should have a StringExtras function for mapping C99 escapes. switch (Repl[0]) { + + // Backreference with the "\g<ref>" syntax + case 'g': + if (Repl.size() >= 4 && Repl[1] == '<') { + size_t End = Repl.find('>'); + StringRef Ref = Repl.slice(2, End); + unsigned RefValue; + if (End != StringRef::npos && !Ref.getAsInteger(10, RefValue)) { + Repl = Repl.substr(End + 1); + if (RefValue < Matches.size()) + Res += Matches[RefValue]; + else if (Error && Error->empty()) + *Error = + ("invalid backreference string 'g<" + Twine(Ref) + ">'").str(); + break; + } + } + [[fallthrough]]; + // Treat all unrecognized characters as self-quoting. default: Res += Repl[0]; |