2 ** Do shell-style pattern matching for ?, \, [], and * characters.
5 ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6 ** Rich $alz is now <rsalz@bbn.com>.
8 ** Modified by Wayne Davison to special-case '/' matching, to make '**'
9 ** work differently than '*', and to fix the character-class code.
12 #include "git-compat-util.h"
13 #include "wildmatch.h"
15 typedef unsigned char uchar
;
17 /* Internal return values */
18 #define WM_ABORT_ALL -1
19 #define WM_ABORT_TO_STARSTAR -2
21 /* What character marks an inverted character class? */
22 #define NEGATE_CLASS '!'
23 #define NEGATE_CLASS2 '^'
25 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
26 && *(class) == *(litmatch) \
27 && strncmp((char*)class, litmatch, len) == 0)
29 #if defined STDC_HEADERS || !defined isascii
32 # define ISASCII(c) isascii(c)
36 # define ISBLANK(c) (ISASCII(c) && isblank(c))
38 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
42 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
44 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
47 #define ISPRINT(c) (ISASCII(c) && isprint(c))
48 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
49 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
50 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
51 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
52 #define ISLOWER(c) (ISASCII(c) && islower(c))
53 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
54 #define ISSPACE(c) (ISASCII(c) && isspace(c))
55 #define ISUPPER(c) (ISASCII(c) && isupper(c))
56 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
58 /* Match pattern "p" against "text" */
59 static int dowild(const uchar
*p
, const uchar
*text
, unsigned int flags
)
62 const uchar
*pattern
= p
;
64 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
65 int matched
, match_slash
, negated
;
67 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
69 if ((flags
& WM_CASEFOLD
) && ISUPPER(t_ch
))
71 if ((flags
& WM_CASEFOLD
) && ISUPPER(p_ch
))
75 /* Literal match with following character. Note that the test
76 * in "default" handles the p[1] == '\0' failure case. */
84 /* Match anything but '/'. */
85 if ((flags
& WM_PATHNAME
) && t_ch
== '/')
90 const uchar
*prev_p
= p
;
91 while (*++p
== '*') {}
92 if (!(flags
& WM_PATHNAME
))
93 /* without WM_PATHNAME, '*' == '**' */
95 else if ((prev_p
- pattern
< 2 || *(prev_p
- 2) == '/') &&
96 (*p
== '\0' || *p
== '/' ||
97 (p
[0] == '\\' && p
[1] == '/'))) {
99 * Assuming we already match 'foo/' and are at
100 * <star star slash>, just assume it matches
101 * nothing and go ahead match the rest of the
102 * pattern with the remaining string. This
103 * helps make foo/<*><*>/bar (<> because
104 * otherwise it breaks C comment syntax) match
105 * both foo/bar and foo/a/bar.
108 dowild(p
+ 1, text
, flags
) == WM_MATCH
)
111 } else /* WM_PATHNAME is set */
114 /* without WM_PATHNAME, '*' == '**' */
115 match_slash
= flags
& WM_PATHNAME
? 0 : 1;
117 /* Trailing "**" matches everything. Trailing "*" matches
118 * only if there are no more slash characters. */
120 if (strchr((char *)text
, '/'))
121 return WM_ABORT_TO_STARSTAR
;
124 } else if (!match_slash
&& *p
== '/') {
126 * _one_ asterisk followed by a slash
127 * with WM_PATHNAME matches the next
130 const char *slash
= strchr((char*)text
, '/');
133 text
= (const uchar
*)slash
;
134 /* the slash is consumed by the top-level for loop */
141 * Try to advance faster when an asterisk is
142 * followed by a literal. We know in this case
143 * that the string before the literal
144 * must belong to "*".
145 * If match_slash is false, do not look past
146 * the first slash as it cannot belong to '*'.
148 if (!is_glob_special(*p
)) {
150 if ((flags
& WM_CASEFOLD
) && ISUPPER(p_ch
))
151 p_ch
= tolower(p_ch
);
152 while ((t_ch
= *text
) != '\0' &&
153 (match_slash
|| t_ch
!= '/')) {
154 if ((flags
& WM_CASEFOLD
) && ISUPPER(t_ch
))
155 t_ch
= tolower(t_ch
);
164 return WM_ABORT_TO_STARSTAR
;
167 if ((matched
= dowild(p
, text
, flags
)) != WM_NOMATCH
) {
168 if (!match_slash
|| matched
!= WM_ABORT_TO_STARSTAR
)
170 } else if (!match_slash
&& t_ch
== '/')
171 return WM_ABORT_TO_STARSTAR
;
178 if (p_ch
== NEGATE_CLASS2
)
181 /* Assign literal 1/0 because of "matched" comparison. */
182 negated
= p_ch
== NEGATE_CLASS
? 1 : 0;
184 /* Inverted character class. */
198 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
205 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
207 else if ((flags
& WM_CASEFOLD
) && ISLOWER(t_ch
)) {
208 uchar t_ch_upper
= toupper(t_ch
);
209 if (t_ch_upper
<= p_ch
&& t_ch_upper
>= prev_ch
)
212 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
213 } else if (p_ch
== '[' && p
[1] == ':') {
216 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
220 if (i
< 0 || p
[-1] != ':') {
221 /* Didn't find ":]", so treat like a normal set. */
228 if (CC_EQ(s
,i
, "alnum")) {
231 } else if (CC_EQ(s
,i
, "alpha")) {
234 } else if (CC_EQ(s
,i
, "blank")) {
237 } else if (CC_EQ(s
,i
, "cntrl")) {
240 } else if (CC_EQ(s
,i
, "digit")) {
243 } else if (CC_EQ(s
,i
, "graph")) {
246 } else if (CC_EQ(s
,i
, "lower")) {
249 } else if (CC_EQ(s
,i
, "print")) {
252 } else if (CC_EQ(s
,i
, "punct")) {
255 } else if (CC_EQ(s
,i
, "space")) {
258 } else if (CC_EQ(s
,i
, "upper")) {
261 else if ((flags
& WM_CASEFOLD
) && ISLOWER(t_ch
))
263 } else if (CC_EQ(s
,i
, "xdigit")) {
266 } else /* malformed [:class:] string */
268 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
269 } else if (t_ch
== p_ch
)
271 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
272 if (matched
== negated
||
273 ((flags
& WM_PATHNAME
) && t_ch
== '/'))
279 return *text
? WM_NOMATCH
: WM_MATCH
;
282 /* Match the "pattern" against the "text" string. */
283 int wildmatch(const char *pattern
, const char *text
, unsigned int flags
)
285 int res
= dowild((const uchar
*)pattern
, (const uchar
*)text
, flags
);
286 return res
== WM_MATCH
? WM_MATCH
: WM_NOMATCH
;