Skip to content

Commit 6c1c442

Browse files
committed
Merge branch 'jc/maint-simpler-common-prefix' into maint
* jc/maint-simpler-common-prefix: common_prefix: simplify and fix scanning for prefixes
2 parents 54dc783 + 42f9852 commit 6c1c442

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

dir.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ static int common_prefix(const char **pathspec)
3131
if (!slash)
3232
return 0;
3333

34+
/*
35+
* The first 'prefix' characters of 'path' are common leading
36+
* path components among the pathspecs we have seen so far,
37+
* including the trailing slash.
38+
*/
3439
prefix = slash - path + 1;
3540
while ((next = *++pathspec) != NULL) {
36-
int len = strlen(next);
37-
if (len >= prefix && !memcmp(path, next, prefix))
41+
int len, last_matching_slash = -1;
42+
for (len = 0; len < prefix && next[len] == path[len]; len++)
43+
if (next[len] == '/')
44+
last_matching_slash = len;
45+
if (len == prefix)
3846
continue;
39-
len = prefix - 1;
40-
for (;;) {
41-
if (!len)
42-
return 0;
43-
if (next[--len] != '/')
44-
continue;
45-
if (memcmp(path, next, len+1))
46-
continue;
47-
prefix = len + 1;
48-
break;
49-
}
47+
if (last_matching_slash < 0)
48+
return 0;
49+
prefix = last_matching_slash + 1;
5050
}
5151
return prefix;
5252
}

0 commit comments

Comments
 (0)