diff options
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]; |