Skip to content

Commit

Permalink
Merge branch 'lt/dirmatch-optim'
Browse files Browse the repository at this point in the history
* lt/dirmatch-optim:
  Optimize match_pathspec() to avoid fnmatch()
  • Loading branch information
Junio C Hamano committed May 9, 2008
2 parents c5445fe + 88ea811 commit 4c4d3ac
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ int common_prefix(const char **pathspec)
return prefix;
}

static inline int special_char(unsigned char c1)
{
return !c1 || c1 == '*' || c1 == '[' || c1 == '?';
}

/*
* Does 'match' matches the given name?
* A match is found if
Expand All @@ -69,14 +74,27 @@ static int match_one(const char *match, const char *name, int namelen)
int matchlen;

/* If the match was just the prefix, we matched */
matchlen = strlen(match);
if (!matchlen)
if (!*match)
return MATCHED_RECURSIVELY;

for (;;) {
unsigned char c1 = *match;
unsigned char c2 = *name;
if (special_char(c1))
break;
if (c1 != c2)
return 0;
match++;
name++;
namelen--;
}


/*
* If we don't match the matchstring exactly,
* we need to match by fnmatch
*/
matchlen = strlen(match);
if (strncmp(match, name, matchlen))
return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;

Expand Down

0 comments on commit 4c4d3ac

Please sign in to comment.