Skip to content

Commit

Permalink
Fix glob matching algo
Browse files Browse the repository at this point in the history
There was one issue when you were using only `?`.
I've added a circuit break that fixes that issue and also improve the perf of the algo in the case of patterns with no `*` and shorter than the length of the input string.
  • Loading branch information
pierotibou committed Dec 7, 2023
1 parent 7d78f74 commit 4ff93ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ext/ddshared.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ bool dd_glob_rule_matches(zval *pattern, zend_string* value) {
char *s = ZSTR_VAL(value);

int wildcards = 0;
int patternLength = 0;
int stringLength = ZSTR_LEN(value);
while (*p) {
if (*(p++) == '*') {
++wildcards;
}
patternLength++;
}

// If there are no wildcards, no need to go through the whole string if pattern is shorter than the input string
// Indeed wildcards (ie '*') can replace multiple characters while '?' canonly replace one
if (wildcards == 0 && patternLength < stringLength) {
return false;
}

p = Z_STRVAL_P(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ $tests = [
["**fooname", "fooname", true],
["*", "fooname", true],
["???????", "fooname", true],
["??????", "fooname", false],
["??", "fooname", false],
["*?", "fooname", true],
["?*", "fooname", true],
["f*o*e", "fooname", true],
Expand Down Expand Up @@ -61,6 +63,10 @@ As expected, * matches fooname (name)
As expected, * matches fooname (service)
As expected, ??????? matches fooname (name)
As expected, ??????? matches fooname (service)
As expected, ?????? doesn't match fooname (name)
As expected, ?????? doesn't match fooname (service)
As expected, ?? doesn't match fooname (name)
As expected, ?? doesn't match fooname (service)
As expected, *? matches fooname (name)
As expected, *? matches fooname (service)
As expected, ?* matches fooname (name)
Expand Down

0 comments on commit 4ff93ff

Please sign in to comment.