Add missing escape characters to the new Regex::escape() function
authorAlp Toker <alp@nuanti.com>
Thu, 12 Dec 2013 02:51:58 +0000 (02:51 +0000)
committerAlp Toker <alp@nuanti.com>
Thu, 12 Dec 2013 02:51:58 +0000 (02:51 +0000)
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

lib/Support/Regex.cpp
unittests/Support/RegexTest.cpp

index 3e112df6c77be6f082467f865e44d948344b247c..eb94745d9e36644279398c8a208d755fb85ca63c 100644 (file)
@@ -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;
index 7b977f7446681d122d1c8cf0912429086bde2913..abc8d0f38e04c6eeda771b8c95549fda009df5d2 100644 (file)
@@ -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));