Lines Matching defs:regex

566     // regfree'ing an invalid regex might crash because the content
567 // of the regex is undefined. Since the regex's are essentially
594 void RE::Init(const char* regex) {
595 pattern_ = posix::StrDup(regex);
599 const size_t full_regex_len = strlen(regex) + 10;
602 snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
609 // Some implementation of POSIX regex (e.g. on at least some
611 // regex. We change it to an equivalent form "()" to be safe.
613 const char* const partial_regex = (*regex == '\0') ? "()" : regex;
617 << "Regular expression \"" << regex
674 std::string FormatRegexSyntaxError(const char* regex, int index) {
676 << " in simple regular expression \"" << regex << "\": ").GetString();
679 // Generates non-fatal failures and returns false if regex is invalid;
681 bool ValidateRegex(const char* regex) {
682 if (regex == NULL) {
684 // assertion failures to match where the regex is used in user
694 for (int i = 0; regex[i]; i++) {
695 if (regex[i] == '\\') { // An escape sequence
697 if (regex[i] == '\0') {
698 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
703 if (!IsValidEscape(regex[i])) {
704 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
705 << "invalid escape sequence \"\\" << regex[i] << "\".";
710 const char ch = regex[i];
713 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
716 } else if (ch == '$' && regex[i + 1] != '\0') {
717 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
721 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
725 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
737 // Matches a repeated regex atom followed by a valid simple regular
738 // expression. The regex atom is defined as c if escaped is false,
745 bool escaped, char c, char repeat, const char* regex,
755 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
768 // Returns true iff regex matches a prefix of str. regex must be a
771 bool MatchRegexAtHead(const char* regex, const char* str) {
772 if (*regex == '\0') // An empty regex matches a prefix of anything.
775 // "$" only matches the end of a string. Note that regex being
777 if (*regex == '$')
780 // Is the first thing in regex an escape sequence?
781 const bool escaped = *regex == '\\';
783 ++regex;
784 if (IsRepeat(regex[1])) {
786 // here's an indirect recursion. It terminates as the regex gets
789 escaped, regex[0], regex[1], regex + 2, str);
791 // regex isn't empty, isn't "$", and doesn't start with a
792 // repetition. We match the first atom of regex with the first
794 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
795 MatchRegexAtHead(regex + 1, str + 1);
799 // Returns true iff regex matches any substring of str. regex must be
803 // the regex length, so we won't need to worry about running out of
805 // exponential with respect to the regex length + the string length,
807 bool MatchRegexAnywhere(const char* regex, const char* str) {
808 if (regex == NULL || str == NULL)
811 if (*regex == '^')
812 return MatchRegexAtHead(regex + 1, str);
816 if (MatchRegexAtHead(regex, str))
841 void RE::Init(const char* regex) {
843 if (regex != NULL) {
844 pattern_ = posix::StrDup(regex);
847 is_valid_ = ValidateRegex(regex);
849 // No need to calculate the full pattern when the regex is invalid.
853 const size_t len = strlen(regex);
860 if (*regex != '^')
865 memcpy(buffer, regex, len);
868 if (len == 0 || regex[len - 1] != '$')