[PATCH] Introduce Regex::isLiteralERE function.

Peter Collingbourne peter at pcc.me.uk
Fri Aug 2 18:58:31 PDT 2013


This will be used to implement an optimisation for literal entries in
special case lists (D1150).

http://llvm-reviews.chandlerc.com/D1278

Files:
  include/llvm/Support/Regex.h
  lib/Support/Regex.cpp
  unittests/Support/RegexTest.cpp

Index: include/llvm/Support/Regex.h
===================================================================
--- include/llvm/Support/Regex.h
+++ include/llvm/Support/Regex.h
@@ -77,6 +77,10 @@
     /// string.
     std::string sub(StringRef Repl, StringRef String, std::string *Error = 0);
 
+    /// \brief If this function returns true, ^Str$ is an extended regular
+    /// expression that matches Str and only Str.
+    static bool isLiteralERE(StringRef Str);
+
   private:
     struct llvm_regex *preg;
     int error;
Index: lib/Support/Regex.cpp
===================================================================
--- lib/Support/Regex.cpp
+++ lib/Support/Regex.cpp
@@ -168,3 +168,10 @@
 
   return Res;
 }
+
+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;
+}
Index: unittests/Support/RegexTest.cpp
===================================================================
--- unittests/Support/RegexTest.cpp
+++ unittests/Support/RegexTest.cpp
@@ -112,4 +112,19 @@
   EXPECT_EQ(Error, "invalid backreference string '100'");
 }
 
+TEST_F(RegexTest, IsLiteralERE) {
+  EXPECT_TRUE(Regex::isLiteralERE("abc"));
+  EXPECT_FALSE(Regex::isLiteralERE("a(bc)"));
+  EXPECT_FALSE(Regex::isLiteralERE("^abc"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc$"));
+  EXPECT_FALSE(Regex::isLiteralERE("a|bc"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc*"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc+"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc?"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc."));
+  EXPECT_FALSE(Regex::isLiteralERE("a[bc]"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc\\1"));
+  EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
+}
+
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1278.1.patch
Type: text/x-patch
Size: 1887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130802/a464c010/attachment.bin>


More information about the llvm-commits mailing list