Returns true iff regex matches a prefix of str. regex must be a valid simple regular expression and not start with "^", or the result is undefined.
| 8121 | // valid simple regular expression and not start with "^", or the |
| 8122 | // result is undefined. |
| 8123 | bool MatchRegexAtHead(const char* regex, const char* str) { |
| 8124 | if (*regex == '\0') // An empty regex matches a prefix of anything. |
| 8125 | return true; |
| 8126 | |
| 8127 | // "$" only matches the end of a string. Note that regex being |
| 8128 | // valid guarantees that there's nothing after "$" in it. |
| 8129 | if (*regex == '$') |
| 8130 | return *str == '\0'; |
| 8131 | |
| 8132 | // Is the first thing in regex an escape sequence? |
| 8133 | const bool escaped = *regex == '\\'; |
| 8134 | if (escaped) |
| 8135 | ++regex; |
| 8136 | if (IsRepeat(regex[1])) { |
| 8137 | // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so |
| 8138 | // here's an indirect recursion. It terminates as the regex gets |
| 8139 | // shorter in each recursion. |
| 8140 | return MatchRepetitionAndRegexAtHead( |
| 8141 | escaped, regex[0], regex[1], regex + 2, str); |
| 8142 | } else { |
| 8143 | // regex isn't empty, isn't "$", and doesn't start with a |
| 8144 | // repetition. We match the first atom of regex with the first |
| 8145 | // character of str and recurse. |
| 8146 | return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) && |
| 8147 | MatchRegexAtHead(regex + 1, str + 1); |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | // Returns true iff regex matches any substring of str. regex must be |
| 8152 | // a valid simple regular expression, or the result is undefined. |
no test coverage detected