Lines Matching refs:regex
670 // regfree'ing an invalid regex might crash because the content
671 // of the regex is undefined. Since the regex's are essentially
697 void RE::Init(const char* regex) {
698 pattern_ = regex;
700 // NetBSD (and Android, which takes its regex implemntation from NetBSD) does
701 // not include the GNU regex extensions (such as Perl style character classes
713 const size_t full_regex_len = strlen(regex) + 10;
716 snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
723 // Some implementation of POSIX regex (e.g. on at least some
725 // regex. We change it to an equivalent form "()" to be safe.
727 const char* const partial_regex = (*regex == '\0') ? "()" : regex;
731 << "Regular expression \"" << regex
799 static std::string FormatRegexSyntaxError(const char* regex, int index) {
801 << " in simple regular expression \"" << regex << "\": ")
805 // Generates non-fatal failures and returns false if regex is invalid;
807 bool ValidateRegex(const char* regex) {
808 if (regex == nullptr) {
817 for (int i = 0; regex[i]; i++) {
818 if (regex[i] == '\\') { // An escape sequence
820 if (regex[i] == '\0') {
821 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
826 if (!IsValidEscape(regex[i])) {
827 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
828 << "invalid escape sequence \"\\" << regex[i] << "\".";
833 const char ch = regex[i];
836 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
839 } else if (ch == '$' && regex[i + 1] != '\0') {
840 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
844 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch
848 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch
860 // Matches a repeated regex atom followed by a valid simple regular
861 // expression. The regex atom is defined as c if escaped is false,
868 const char* regex, const char* str) {
876 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
888 // Returns true if and only if regex matches a prefix of str. regex must
891 bool MatchRegexAtHead(const char* regex, const char* str) {
892 if (*regex == '\0') // An empty regex matches a prefix of anything.
895 // "$" only matches the end of a string. Note that regex being
897 if (*regex == '$') return *str == '\0';
899 // Is the first thing in regex an escape sequence?
900 const bool escaped = *regex == '\\';
901 if (escaped) ++regex;
902 if (IsRepeat(regex[1])) {
904 // here's an indirect recursion. It terminates as the regex gets
906 return MatchRepetitionAndRegexAtHead(escaped, regex[0], regex[1], regex + 2,
909 // regex isn't empty, isn't "$", and doesn't start with a
910 // repetition. We match the first atom of regex with the first
912 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
913 MatchRegexAtHead(regex + 1, str + 1);
917 // Returns true if and only if regex matches any substring of str. regex must
921 // the regex length, so we won't need to worry about running out of
923 // exponential with respect to the regex length + the string length,
925 bool MatchRegexAnywhere(const char* regex, const char* str) {
926 if (regex == nullptr || str == nullptr) return false;
928 if (*regex == '^') return MatchRegexAtHead(regex + 1, str);
932 if (MatchRegexAtHead(regex, str)) return true;
953 void RE::Init(const char* regex) {
957 if (regex != nullptr) {
958 pattern_ = regex;
961 is_valid_ = ValidateRegex(regex);
963 // No need to calculate the full pattern when the regex is invalid.