MCPcopy Create free account
hub / github.com/apache/brpc / MatchPatternT

Function MatchPatternT

src/butil/strings/string_util.cc:785–847  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

783
784template <typename CHAR, typename NEXT>
785static bool MatchPatternT(const CHAR* eval, const CHAR* eval_end,
786 const CHAR* pattern, const CHAR* pattern_end,
787 int depth,
788 NEXT next) {
789 const int kMaxDepth = 16;
790 if (depth > kMaxDepth)
791 return false;
792
793 // Eat all the matching chars.
794 EatSameChars(&pattern, pattern_end, &eval, eval_end, next);
795
796 // If the string is empty, then the pattern must be empty too, or contains
797 // only wildcards.
798 if (eval == eval_end) {
799 EatWildcard(&pattern, pattern_end, next);
800 return pattern == pattern_end;
801 }
802
803 // Pattern is empty but not string, this is not a match.
804 if (pattern == pattern_end)
805 return false;
806
807 // If this is a question mark, then we need to compare the rest with
808 // the current string or the string with one character eaten.
809 const CHAR* next_pattern = pattern;
810 next(&next_pattern, pattern_end);
811 if (pattern[0] == '?') {
812 if (MatchPatternT(eval, eval_end, next_pattern, pattern_end,
813 depth + 1, next))
814 return true;
815 const CHAR* next_eval = eval;
816 next(&next_eval, eval_end);
817 if (MatchPatternT(next_eval, eval_end, next_pattern, pattern_end,
818 depth + 1, next))
819 return true;
820 }
821
822 // This is a *, try to match all the possible substrings with the remainder
823 // of the pattern.
824 if (pattern[0] == '*') {
825 // Collapse duplicate wild cards (********** into *) so that the
826 // method does not recurse unnecessarily. http://crbug.com/52839
827 EatWildcard(&next_pattern, pattern_end, next);
828
829 while (eval != eval_end) {
830 if (MatchPatternT(eval, eval_end, next_pattern, pattern_end,
831 depth + 1, next))
832 return true;
833 eval++;
834 }
835
836 // We reached the end of the string, let see if the pattern contains only
837 // wildcards.
838 if (eval == eval_end) {
839 EatWildcard(&pattern, pattern_end, next);
840 if (pattern != pattern_end)
841 return false;
842 return true;

Callers 1

MatchPatternFunction · 0.85

Calls 2

EatSameCharsFunction · 0.85
EatWildcardFunction · 0.85

Tested by

no test coverage detected