MCPcopy Create free account
hub / github.com/EmbeddedRPC/erpc / MatchRepetitionAndRegexAtHead

Function MatchRepetitionAndRegexAtHead

test/common/gtest/gtest.cpp:9361–9383  ·  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

9359// probably time out anyway. We are fine with this limitation as
9360// std::string has it too.
9361bool MatchRepetitionAndRegexAtHead(
9362 bool escaped, char c, char repeat, const char* regex,
9363 const char* str) {
9364 const size_t min_count = (repeat == '+') ? 1 : 0;
9365 const size_t max_count = (repeat == '?') ? 1 :
9366 static_cast<size_t>(-1) - 1;
9367 // We cannot call numeric_limits::max() as it conflicts with the
9368 // max() macro on Windows.
9369
9370 for (size_t i = 0; i <= max_count; ++i) {
9371 // We know that the atom matches each of the first i characters in str.
9372 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
9373 // We have enough matches at the head, and the tail matches too.
9374 // Since we only care about *whether* the pattern matches str
9375 // (as opposed to *how* it matches), there is no need to find a
9376 // greedy match.
9377 return true;
9378 }
9379 if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
9380 return false;
9381 }
9382 return false;
9383}
9384
9385// Returns true iff regex matches a prefix of str. regex must be a
9386// 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