diff options
author | Hans Wennborg <hans@hanshq.net> | 2013-12-12 00:06:41 +0000 |
---|---|---|
committer | Hans Wennborg <hans@hanshq.net> | 2013-12-12 00:06:41 +0000 |
commit | 6f4f77b7e932f911f2c9783f48fa68f854887432 (patch) | |
tree | d7a31c688a3bbbfb19df93f0b8760ae7e28b57ce /llvm/lib/Support/Regex.cpp | |
parent | 2c3f14055173f49827e6b03cbf5e2eb7cffa8aec (diff) | |
download | bcm5719-llvm-6f4f77b7e932f911f2c9783f48fa68f854887432.tar.gz bcm5719-llvm-6f4f77b7e932f911f2c9783f48fa68f854887432.zip |
Expose FileCheck's AddFixedStringToRegEx as Regex::escape
Both FileCheck and clang's -verify need to escape strings for regexes,
so let's expose this as a utility in the Regex class.
llvm-svn: 197096
Diffstat (limited to 'llvm/lib/Support/Regex.cpp')
-rw-r--r-- | llvm/lib/Support/Regex.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/llvm/lib/Support/Regex.cpp b/llvm/lib/Support/Regex.cpp index 54136418407..3e112df6c77 100644 --- a/llvm/lib/Support/Regex.cpp +++ b/llvm/lib/Support/Regex.cpp @@ -175,3 +175,32 @@ bool Regex::isLiteralERE(StringRef Str) { // regular expression specification. return Str.find_first_of("()^$|*+?.[]\\{}") == 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 '{': + RegexStr += '\\'; + // FALL THROUGH. + default: + RegexStr += String[i]; + break; + } + } + + return RegexStr; +} |