[llvm] r197121 - Add missing escape characters to the new Regex::escape() function

Alp Toker alp at nuanti.com
Wed Dec 11 18:51:58 PST 2013


Author: alp
Date: Wed Dec 11 20:51:58 2013
New Revision: 197121

URL: http://llvm.org/viewvc/llvm-project?rev=197121&view=rev
Log:
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.

Modified:
    llvm/trunk/lib/Support/Regex.cpp
    llvm/trunk/unittests/Support/RegexTest.cpp

Modified: llvm/trunk/lib/Support/Regex.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Regex.cpp?rev=197121&r1=197120&r2=197121&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Regex.cpp (original)
+++ llvm/trunk/lib/Support/Regex.cpp Wed Dec 11 20:51:58 2013
@@ -169,37 +169,22 @@ std::string Regex::sub(StringRef Repl, S
   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;

Modified: llvm/trunk/unittests/Support/RegexTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/RegexTest.cpp?rev=197121&r1=197120&r2=197121&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/RegexTest.cpp (original)
+++ llvm/trunk/unittests/Support/RegexTest.cpp Wed Dec 11 20:51:58 2013
@@ -127,6 +127,11 @@ TEST_F(RegexTest, IsLiteralERE) {
   EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
 }
 
+TEST_F(RegexTest, Escape) {
+  EXPECT_EQ(Regex::escape("a[bc]"), "a\\[bc\\]");
+  EXPECT_EQ(Regex::escape("abc{1\\,2}"), "abc\\{1\\\\,2\\}");
+}
+
 TEST_F(RegexTest, IsValid) {
   std::string Error;
   EXPECT_FALSE(Regex("(foo").isValid(Error));





More information about the llvm-commits mailing list