MCPcopy Create free account
hub / github.com/awslabs/aws-lambda-cpp / MatchRepetitionAndRegexAtHead

Function MatchRepetitionAndRegexAtHead

tests/gtest/gtest-all.cc:10587–10609  ·  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

10585// probably time out anyway. We are fine with this limitation as
10586// std::string has it too.
10587bool MatchRepetitionAndRegexAtHead(
10588 bool escaped, char c, char repeat, const char* regex,
10589 const char* str) {
10590 const size_t min_count = (repeat == '+') ? 1 : 0;
10591 const size_t max_count = (repeat == '?') ? 1 :
10592 static_cast<size_t>(-1) - 1;
10593 // We cannot call numeric_limits::max() as it conflicts with the
10594 // max() macro on Windows.
10595
10596 for (size_t i = 0; i <= max_count; ++i) {
10597 // We know that the atom matches each of the first i characters in str.
10598 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
10599 // We have enough matches at the head, and the tail matches too.
10600 // Since we only care about *whether* the pattern matches str
10601 // (as opposed to *how* it matches), there is no need to find a
10602 // greedy match.
10603 return true;
10604 }
10605 if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
10606 return false;
10607 }
10608 return false;
10609}
10610
10611// Returns true iff regex matches a prefix of str. regex must be a
10612// 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