summaryrefslogtreecommitdiff
path: root/lib/Support
diff options
context:
space:
mode:
authorAlp Toker <alp@nuanti.com>2013-12-12 02:51:58 +0000
committerAlp Toker <alp@nuanti.com>2013-12-12 02:51:58 +0000
commit22e0e0c1de72187f9f20e9d7375422b51ec10fb9 (patch)
treebf62d97e503d5507cfd501f1538b6a02e276a44a /lib/Support
parentdcdc5736ad62ad2ba62419e8d311d883cee1a329 (diff)
downloadllvm-22e0e0c1de72187f9f20e9d7375422b51ec10fb9.tar.gz
llvm-22e0e0c1de72187f9f20e9d7375422b51ec10fb9.tar.bz2
llvm-22e0e0c1de72187f9f20e9d7375422b51ec10fb9.tar.xz
Add missing escape characters to the new Regex::escape() function
The old AddFixedStringToRegEx() it was based on got away with this for the longest time, but the problem became easy to spot after the cleanup in r197096. Also add a quick unit test to cover regex escaping. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197121 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/Regex.cpp27
1 files changed, 6 insertions, 21 deletions
diff --git a/lib/Support/Regex.cpp b/lib/Support/Regex.cpp
index 3e112df6c7..eb94745d9e 100644
--- a/lib/Support/Regex.cpp
+++ b/lib/Support/Regex.cpp
@@ -169,37 +169,22 @@ std::string Regex::sub(StringRef Repl, StringRef String,
return Res;
}
+// These are the special characters matched in functions like "p_ere_exp".
+static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
+
bool Regex::isLiteralERE(StringRef Str) {
// Check for regex metacharacters. This list was derived from our regex
// implementation in regcomp.c and double checked against the POSIX extended
// regular expression specification.
- return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
+ return Str.find_first_of(RegexMetachars) == StringRef::npos;
}
std::string Regex::escape(StringRef String) {
std::string RegexStr;
-
for (unsigned i = 0, e = String.size(); i != e; ++i) {
- switch (String[i]) {
- // These are the special characters matched in "p_ere_exp".
- case '(':
- case ')':
- case '^':
- case '$':
- case '|':
- case '*':
- case '+':
- case '?':
- case '.':
- case '[':
- case '\\':
- case '{':
+ if (strchr(RegexMetachars, String[i]))
RegexStr += '\\';
- // FALL THROUGH.
- default:
- RegexStr += String[i];
- break;
- }
+ RegexStr += String[i];
}
return RegexStr;