From 6f4f77b7e932f911f2c9783f48fa68f854887432 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Thu, 12 Dec 2013 00:06:41 +0000 Subject: 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 --- llvm/lib/Support/Regex.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'llvm/lib/Support') 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; +} -- cgit v1.2.3