llvm.org GIT mirror llvm / aa80e61
Introduce Regex::isLiteralERE function. This will be used to implement an optimisation for literal entries in special case lists. Differential Revision: http://llvm-reviews.chandlerc.com/D1278 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187731 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 7 years ago
3 changed file(s) with 26 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
7676 /// string.
7777 std::string sub(StringRef Repl, StringRef String, std::string *Error = 0);
7878
79 /// \brief If this function returns true, ^Str$ is an extended regular
80 /// expression that matches Str and only Str.
81 static bool isLiteralERE(StringRef Str);
82
7983 private:
8084 struct llvm_regex *preg;
8185 int error;
167167
168168 return Res;
169169 }
170
171 bool Regex::isLiteralERE(StringRef Str) {
172 // Check for regex metacharacters. This list was derived from our regex
173 // implementation in regcomp.c and double checked against the POSIX extended
174 // regular expression specification.
175 return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
176 }
111111 EXPECT_EQ(Error, "invalid backreference string '100'");
112112 }
113113
114 TEST_F(RegexTest, IsLiteralERE) {
115 EXPECT_TRUE(Regex::isLiteralERE("abc"));
116 EXPECT_FALSE(Regex::isLiteralERE("a(bc)"));
117 EXPECT_FALSE(Regex::isLiteralERE("^abc"));
118 EXPECT_FALSE(Regex::isLiteralERE("abc$"));
119 EXPECT_FALSE(Regex::isLiteralERE("a|bc"));
120 EXPECT_FALSE(Regex::isLiteralERE("abc*"));
121 EXPECT_FALSE(Regex::isLiteralERE("abc+"));
122 EXPECT_FALSE(Regex::isLiteralERE("abc?"));
123 EXPECT_FALSE(Regex::isLiteralERE("abc."));
124 EXPECT_FALSE(Regex::isLiteralERE("a[bc]"));
125 EXPECT_FALSE(Regex::isLiteralERE("abc\\1"));
126 EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
114127 }
128
129 }