Skip to content

Commit 6f1a31f

Browse files
pcloudsgitster
authored andcommitted
wildmatch: advance faster in <asterisk> + <literal> patterns
Normally when we match "*X" on "abcX", we call dowild("X", "abcX"), dowild("X", "bcX"), dowild("X", "cX") and dowild("X", "X"). Only the last call may have a chance of matching. By skipping the text before "X", we can eliminate the first three useless calls. compat, '*/*/*' on linux-2.6.git file list 2000 times, before: wildmatch 7s 985049us fnmatch 2s 735541us or 34.26% faster and after: wildmatch 4s 492549us fnmatch 0s 888263us or 19.77% slower Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4698344 commit 6f1a31f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

t/t3070-wildmatch.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ match 0 x foo '*/*/*'
207207
match 0 x foo/bar '*/*/*'
208208
match 1 x foo/bba/arr '*/*/*'
209209
match 0 x foo/bb/aa/rr '*/*/*'
210+
match 1 x foo/bb/aa/rr '**/**/**'
211+
match 1 x abcXdefXghi '*X*i'
212+
match 0 x ab/cXd/efXg/hi '*X*i'
213+
match 1 x ab/cXd/efXg/hi '*/*X*/*/*i'
214+
match 1 x ab/cXd/efXg/hi '**/*X*/**/*i'
210215

211216
pathmatch 1 foo foo
212217
pathmatch 0 foo fo
@@ -226,5 +231,8 @@ pathmatch 0 foo '*/*/*'
226231
pathmatch 0 foo/bar '*/*/*'
227232
pathmatch 1 foo/bba/arr '*/*/*'
228233
pathmatch 1 foo/bb/aa/rr '*/*/*'
234+
pathmatch 1 abcXdefXghi '*X*i'
235+
pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
236+
pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
229237

230238
test_done

wildmatch.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
133133
while (1) {
134134
if (t_ch == '\0')
135135
break;
136+
/*
137+
* Try to advance faster when an asterisk is
138+
* followed by a literal. We know in this case
139+
* that the the string before the literal
140+
* must belong to "*".
141+
* If match_slash is false, do not look past
142+
* the first slash as it cannot belong to '*'.
143+
*/
144+
if (!is_glob_special(*p)) {
145+
p_ch = *p;
146+
if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
147+
p_ch = tolower(p_ch);
148+
while ((t_ch = *text) != '\0' &&
149+
(match_slash || t_ch != '/')) {
150+
if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
151+
t_ch = tolower(t_ch);
152+
if (t_ch == p_ch)
153+
break;
154+
text++;
155+
}
156+
if (t_ch != p_ch)
157+
return WM_NOMATCH;
158+
}
136159
if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
137160
if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
138161
return matched;

0 commit comments

Comments
 (0)