diff options
author | Alp Toker <alp@nuanti.com> | 2013-12-12 02:51:58 +0000 |
---|---|---|
committer | Alp Toker <alp@nuanti.com> | 2013-12-12 02:51:58 +0000 |
commit | d0d1a74ac9588ab13cc88af5ec2db31235ead154 (patch) | |
tree | 3c3a811850a14fb7ead2fa03e20bd43ef8ef9cb8 /llvm/lib/Support/Regex.cpp | |
parent | 48d1b65541e9eeb3d48f6b86948ac6ea03bf8921 (diff) | |
download | bcm5719-llvm-d0d1a74ac9588ab13cc88af5ec2db31235ead154.tar.gz bcm5719-llvm-d0d1a74ac9588ab13cc88af5ec2db31235ead154.zip |
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.
llvm-svn: 197121
Diffstat (limited to 'llvm/lib/Support/Regex.cpp')
-rw-r--r-- | llvm/lib/Support/Regex.cpp | 27 |
1 files changed, 6 insertions, 21 deletions
diff --git a/llvm/lib/Support/Regex.cpp b/llvm/lib/Support/Regex.cpp index 3e112df6c77..eb94745d9e3 100644 --- a/llvm/lib/Support/Regex.cpp +++ b/llvm/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; |