MCPcopy Create free account
hub / github.com/apache/singa / MatchRepetitionAndRegexAtHead

Function MatchRepetitionAndRegexAtHead

test/gtest/gtest-all.cc:8530–8552  ·  view source on GitHub ↗

Matches a repeated regex atom followed by a valid simple regular expression. The regex atom is defined as c if escaped is false, or \c otherwise. repeat is the repetition meta character (?, *, or +). The behavior is undefined if str contains too many characters to be indexable by size_t, in which case the test will probably time out anyway. We are fine with this limitation as std::string has i

Source from the content-addressed store, hash-verified

8528// probably time out anyway. We are fine with this limitation as
8529// std::string has it too.
8530bool MatchRepetitionAndRegexAtHead(
8531 bool escaped, char c, char repeat, const char* regex,
8532 const char* str) {
8533 const size_t min_count = (repeat == '+') ? 1 : 0;
8534 const size_t max_count = (repeat == '?') ? 1 :
8535 static_cast<size_t>(-1) - 1;
8536 // We cannot call numeric_limits::max() as it conflicts with the
8537 // max() macro on Windows.
8538
8539 for (size_t i = 0; i <= max_count; ++i) {
8540 // We know that the atom matches each of the first i characters in str.
8541 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
8542 // We have enough matches at the head, and the tail matches too.
8543 // Since we only care about *whether* the pattern matches str
8544 // (as opposed to *how* it matches), there is no need to find a
8545 // greedy match.
8546 return true;
8547 }
8548 if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
8549 return false;
8550 }
8551 return false;
8552}
8553
8554// Returns true iff regex matches a prefix of str. regex must be a
8555// valid simple regular expression and not start with "^", or the

Callers 1

MatchRegexAtHeadFunction · 0.85

Calls 2

MatchRegexAtHeadFunction · 0.85
AtomMatchesCharFunction · 0.85

Tested by

no test coverage detected