Home | History | Annotate | Download | only in src

Lines Matching full:regex

104 void RE::Init(const char* regex) {
105 pattern_ = strdup(regex);
109 const size_t full_regex_len = strlen(regex) + 10;
112 snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
119 // Some implementation of POSIX regex (e.g. on at least some
121 // regex. We change it to an equivalent form "()" to be safe.
122 const char* const partial_regex = (*regex == '\0') ? "()" : regex;
126 << "Regular expression \"" << regex
183 String FormatRegexSyntaxError(const char* regex, int index) {
185 << " in simple regular expression \"" << regex << "\": ").GetString();
188 // Generates non-fatal failures and returns false if regex is invalid;
190 bool ValidateRegex(const char* regex) {
191 if (regex == NULL) {
193 // assertion failures to match where the regex is used in user
203 for (int i = 0; regex[i]; i++) {
204 if (regex[i] == '\\') { // An escape sequence
206 if (regex[i] == '\0') {
207 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
212 if (!IsValidEscape(regex[i])) {
213 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
214 << "invalid escape sequence \"\\" << regex[i] << "\".";
219 const char ch = regex[i];
222 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
225 } else if (ch == '$' && regex[i + 1] != '\0') {
226 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
230 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
234 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
246 // Matches a repeated regex atom followed by a valid simple regular
247 // expression. The regex atom is defined as c if escaped is false,
254 bool escaped, char c, char repeat, const char* regex,
264 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
277 // Returns true iff regex matches a prefix of str. regex must be a
280 bool MatchRegexAtHead(const char* regex, const char* str) {
281 if (*regex == '\0') // An empty regex matches a prefix of anything.
284 // "$" only matches the end of a string. Note that regex being
286 if (*regex == '$')
289 // Is the first thing in regex an escape sequence?
290 const bool escaped = *regex == '\\';
292 ++regex;
293 if (IsRepeat(regex[1])) {
295 // here's an indirect recursion. It terminates as the regex gets
298 escaped, regex[0], regex[1], regex + 2, str);
300 // regex isn't empty, isn't "$", and doesn't start with a
301 // repetition. We match the first atom of regex with the first
303 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
304 MatchRegexAtHead(regex + 1, str + 1);
308 // Returns true iff regex matches any substring of str. regex must be
312 // the regex length, so we won't need to worry about running out of
314 // exponential with respect to the regex length + the string length,
316 bool MatchRegexAnywhere(const char* regex, const char* str) {
317 if (regex == NULL || str == NULL)
320 if (*regex == '^')
321 return MatchRegexAtHead(regex + 1, str);
325 if (MatchRegexAtHead(regex, str))
350 void RE::Init(const char* regex) {
352 if (regex != NULL) {
354 pattern_ = _strdup(regex);
356 pattern_ = strdup(regex);
360 is_valid_ = ValidateRegex(regex);
362 // No need to calculate the full pattern when the regex is invalid.
366 const size_t len = strlen(regex);
373 if (*regex != '^')
378 memcpy(buffer, regex, len);
381 if (len == 0 || regex[len - 1] != '$')