Lines Matching refs:regex

708     // regfree'ing an invalid regex might crash because the content
709 // of the regex is undefined. Since the regex's are essentially
736 void RE::Init(const char* regex) {
737 pattern_ = posix::StrDup(regex);
741 const size_t full_regex_len = strlen(regex) + 10;
744 snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
751 // Some implementation of POSIX regex (e.g. on at least some
753 // regex. We change it to an equivalent form "()" to be safe.
755 const char* const partial_regex = (*regex == '\0') ? "()" : regex;
759 << "Regular expression \"" << regex
816 static std::string FormatRegexSyntaxError(const char* regex, int index) {
818 << " in simple regular expression \"" << regex << "\": ").GetString();
821 // Generates non-fatal failures and returns false if regex is invalid;
823 bool ValidateRegex(const char* regex) {
824 if (regex == nullptr) {
833 for (int i = 0; regex[i]; i++) {
834 if (regex[i] == '\\') { // An escape sequence
836 if (regex[i] == '\0') {
837 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
842 if (!IsValidEscape(regex[i])) {
843 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
844 << "invalid escape sequence \"\\" << regex[i] << "\".";
849 const char ch = regex[i];
852 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
855 } else if (ch == '$' && regex[i + 1] != '\0') {
856 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
860 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
864 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
876 // Matches a repeated regex atom followed by a valid simple regular
877 // expression. The regex atom is defined as c if escaped is false,
884 bool escaped, char c, char repeat, const char* regex,
894 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
907 // Returns true if and only if regex matches a prefix of str. regex must
910 bool MatchRegexAtHead(const char* regex, const char* str) {
911 if (*regex == '\0') // An empty regex matches a prefix of anything.
914 // "$" only matches the end of a string. Note that regex being
916 if (*regex == '$')
919 // Is the first thing in regex an escape sequence?
920 const bool escaped = *regex == '\\';
922 ++regex;
923 if (IsRepeat(regex[1])) {
925 // here's an indirect recursion. It terminates as the regex gets
928 escaped, regex[0], regex[1], regex + 2, str);
930 // regex isn't empty, isn't "$", and doesn't start with a
931 // repetition. We match the first atom of regex with the first
933 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
934 MatchRegexAtHead(regex + 1, str + 1);
938 // Returns true if and only if regex matches any substring of str. regex must
942 // the regex length, so we won't need to worry about running out of
944 // exponential with respect to the regex length + the string length,
946 bool MatchRegexAnywhere(const char* regex, const char* str) {
947 if (regex == nullptr || str == nullptr) return false;
949 if (*regex == '^')
950 return MatchRegexAtHead(regex + 1, str);
954 if (MatchRegexAtHead(regex, str))
979 void RE::Init(const char* regex) {
981 if (regex != nullptr) {
982 pattern_ = posix::StrDup(regex);
985 is_valid_ = ValidateRegex(regex);
987 // No need to calculate the full pattern when the regex is invalid.
991 const size_t len = strlen(regex);
998 if (*regex != '^')
1003 memcpy(buffer, regex, len);
1006 if (len == 0 || regex[len - 1] != '$')