]> git.ipfire.org Git - thirdparty/git.git/blame - wildmatch.c
wildmatch: fix exponential behavior
[thirdparty/git.git] / wildmatch.c
CommitLineData
5230f605
NTND
1/*
2** Do shell-style pattern matching for ?, \, [], and * characters.
3** It is 8bit clean.
4**
5** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6** Rich $alz is now <rsalz@bbn.com>.
7**
8** Modified by Wayne Davison to special-case '/' matching, to make '**'
9** work differently than '*', and to fix the character-class code.
10*/
11
feabcc17
NTND
12#include "cache.h"
13#include "wildmatch.h"
14
15typedef unsigned char uchar;
5230f605
NTND
16
17/* What character marks an inverted character class? */
18#define NEGATE_CLASS '!'
19#define NEGATE_CLASS2 '^'
20
5230f605
NTND
21#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
22 && *(class) == *(litmatch) \
23 && strncmp((char*)class, litmatch, len) == 0)
24
25#if defined STDC_HEADERS || !defined isascii
26# define ISASCII(c) 1
27#else
28# define ISASCII(c) isascii(c)
29#endif
30
31#ifdef isblank
32# define ISBLANK(c) (ISASCII(c) && isblank(c))
33#else
34# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
35#endif
36
37#ifdef isgraph
38# define ISGRAPH(c) (ISASCII(c) && isgraph(c))
39#else
40# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
41#endif
42
43#define ISPRINT(c) (ISASCII(c) && isprint(c))
44#define ISDIGIT(c) (ISASCII(c) && isdigit(c))
45#define ISALNUM(c) (ISASCII(c) && isalnum(c))
46#define ISALPHA(c) (ISASCII(c) && isalpha(c))
47#define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
48#define ISLOWER(c) (ISASCII(c) && islower(c))
49#define ISPUNCT(c) (ISASCII(c) && ispunct(c))
50#define ISSPACE(c) (ISASCII(c) && isspace(c))
51#define ISUPPER(c) (ISASCII(c) && isupper(c))
52#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
53
b0e02876 54/* Match pattern "p" against "text" */
0c528168 55static int dowild(const uchar *p, const uchar *text, unsigned int flags)
5230f605 56{
327f2f3e 57 uchar p_ch;
3a078dec 58 const uchar *pattern = p;
5230f605 59
327f2f3e 60 for ( ; (p_ch = *p) != '\0'; text++, p++) {
b6a3d335 61 int matched, match_slash, negated;
327f2f3e
NTND
62 uchar t_ch, prev_ch;
63 if ((t_ch = *text) == '\0' && p_ch != '*')
9b3497ca 64 return WM_ABORT_ALL;
0c528168 65 if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
327f2f3e 66 t_ch = tolower(t_ch);
0c528168 67 if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
164bf83a 68 p_ch = tolower(p_ch);
327f2f3e
NTND
69 switch (p_ch) {
70 case '\\':
71 /* Literal match with following character. Note that the test
72 * in "default" handles the p[1] == '\0' failure case. */
5230f605 73 p_ch = *++p;
327f2f3e
NTND
74 /* FALLTHROUGH */
75 default:
76 if (t_ch != p_ch)
9b3497ca 77 return WM_NOMATCH;
327f2f3e
NTND
78 continue;
79 case '?':
80 /* Match anything but '/'. */
c41244e7 81 if ((flags & WM_PATHNAME) && t_ch == '/')
9b3497ca 82 return WM_NOMATCH;
5230f605 83 continue;
327f2f3e
NTND
84 case '*':
85 if (*++p == '*') {
40bbee0a 86 const uchar *prev_p = p - 2;
327f2f3e 87 while (*++p == '*') {}
c41244e7
NTND
88 if (!(flags & WM_PATHNAME))
89 /* without WM_PATHNAME, '*' == '**' */
90 match_slash = 1;
91 else if ((prev_p < pattern || *prev_p == '/') &&
40bbee0a
NTND
92 (*p == '\0' || *p == '/' ||
93 (p[0] == '\\' && p[1] == '/'))) {
4c251e5c
NTND
94 /*
95 * Assuming we already match 'foo/' and are at
96 * <star star slash>, just assume it matches
97 * nothing and go ahead match the rest of the
98 * pattern with the remaining string. This
99 * helps make foo/<*><*>/bar (<> because
100 * otherwise it breaks C comment syntax) match
101 * both foo/bar and foo/a/bar.
102 */
103 if (p[0] == '/' &&
0c528168 104 dowild(p + 1, text, flags) == WM_MATCH)
9b3497ca
NTND
105 return WM_MATCH;
106 match_slash = 1;
e5bbe09e
NTND
107 } else /* WM_PATHNAME is set */
108 match_slash = 0;
327f2f3e 109 } else
c41244e7
NTND
110 /* without WM_PATHNAME, '*' == '**' */
111 match_slash = flags & WM_PATHNAME ? 0 : 1;
327f2f3e
NTND
112 if (*p == '\0') {
113 /* Trailing "**" matches everything. Trailing "*" matches
114 * only if there are no more slash characters. */
b6a3d335 115 if (!match_slash) {
afe8a907 116 if (strchr((char *)text, '/'))
1f2e05f0 117 return WM_ABORT_TO_STARSTAR;
327f2f3e 118 }
9b3497ca 119 return WM_MATCH;
46983441
NTND
120 } else if (!match_slash && *p == '/') {
121 /*
122 * _one_ asterisk followed by a slash
123 * with WM_PATHNAME matches the next
124 * directory
125 */
126 const char *slash = strchr((char*)text, '/');
127 if (!slash)
1f2e05f0 128 return WM_ABORT_ALL;
46983441
NTND
129 text = (const uchar*)slash;
130 /* the slash is consumed by the top-level for loop */
131 break;
327f2f3e
NTND
132 }
133 while (1) {
134 if (t_ch == '\0')
135 break;
6f1a31f0
NTND
136 /*
137 * Try to advance faster when an asterisk is
138 * followed by a literal. We know in this case
832c0e5e 139 * that the string before the literal
6f1a31f0
NTND
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 }
1f2e05f0
PW
156 if (t_ch != p_ch) {
157 if (match_slash)
158 return WM_ABORT_ALL;
159 else
160 return WM_ABORT_TO_STARSTAR;
161 }
6f1a31f0 162 }
0c528168 163 if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
9b3497ca 164 if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
327f2f3e 165 return matched;
b6a3d335 166 } else if (!match_slash && t_ch == '/')
9b3497ca 167 return WM_ABORT_TO_STARSTAR;
327f2f3e
NTND
168 t_ch = *++text;
169 }
9b3497ca 170 return WM_ABORT_ALL;
327f2f3e
NTND
171 case '[':
172 p_ch = *++p;
173#ifdef NEGATE_CLASS2
174 if (p_ch == NEGATE_CLASS2)
175 p_ch = NEGATE_CLASS;
176#endif
9b3497ca
NTND
177 /* Assign literal 1/0 because of "matched" comparison. */
178 negated = p_ch == NEGATE_CLASS ? 1 : 0;
b6a3d335 179 if (negated) {
327f2f3e
NTND
180 /* Inverted character class. */
181 p_ch = *++p;
182 }
183 prev_ch = 0;
9b3497ca 184 matched = 0;
327f2f3e
NTND
185 do {
186 if (!p_ch)
9b3497ca 187 return WM_ABORT_ALL;
327f2f3e
NTND
188 if (p_ch == '\\') {
189 p_ch = *++p;
190 if (!p_ch)
9b3497ca 191 return WM_ABORT_ALL;
327f2f3e 192 if (t_ch == p_ch)
9b3497ca 193 matched = 1;
327f2f3e
NTND
194 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
195 p_ch = *++p;
196 if (p_ch == '\\') {
197 p_ch = *++p;
198 if (!p_ch)
9b3497ca 199 return WM_ABORT_ALL;
327f2f3e
NTND
200 }
201 if (t_ch <= p_ch && t_ch >= prev_ch)
9b3497ca 202 matched = 1;
b79c0c37
AR
203 else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) {
204 uchar t_ch_upper = toupper(t_ch);
205 if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
206 matched = 1;
207 }
327f2f3e
NTND
208 p_ch = 0; /* This makes "prev_ch" get set to 0. */
209 } else if (p_ch == '[' && p[1] == ':') {
210 const uchar *s;
211 int i;
212 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
213 if (!p_ch)
9b3497ca 214 return WM_ABORT_ALL;
327f2f3e
NTND
215 i = p - s - 1;
216 if (i < 0 || p[-1] != ':') {
217 /* Didn't find ":]", so treat like a normal set. */
218 p = s - 2;
219 p_ch = '[';
220 if (t_ch == p_ch)
9b3497ca 221 matched = 1;
327f2f3e
NTND
222 continue;
223 }
224 if (CC_EQ(s,i, "alnum")) {
225 if (ISALNUM(t_ch))
9b3497ca 226 matched = 1;
327f2f3e
NTND
227 } else if (CC_EQ(s,i, "alpha")) {
228 if (ISALPHA(t_ch))
9b3497ca 229 matched = 1;
327f2f3e
NTND
230 } else if (CC_EQ(s,i, "blank")) {
231 if (ISBLANK(t_ch))
9b3497ca 232 matched = 1;
327f2f3e
NTND
233 } else if (CC_EQ(s,i, "cntrl")) {
234 if (ISCNTRL(t_ch))
9b3497ca 235 matched = 1;
327f2f3e
NTND
236 } else if (CC_EQ(s,i, "digit")) {
237 if (ISDIGIT(t_ch))
9b3497ca 238 matched = 1;
327f2f3e
NTND
239 } else if (CC_EQ(s,i, "graph")) {
240 if (ISGRAPH(t_ch))
9b3497ca 241 matched = 1;
327f2f3e
NTND
242 } else if (CC_EQ(s,i, "lower")) {
243 if (ISLOWER(t_ch))
9b3497ca 244 matched = 1;
327f2f3e
NTND
245 } else if (CC_EQ(s,i, "print")) {
246 if (ISPRINT(t_ch))
9b3497ca 247 matched = 1;
327f2f3e
NTND
248 } else if (CC_EQ(s,i, "punct")) {
249 if (ISPUNCT(t_ch))
9b3497ca 250 matched = 1;
327f2f3e
NTND
251 } else if (CC_EQ(s,i, "space")) {
252 if (ISSPACE(t_ch))
9b3497ca 253 matched = 1;
327f2f3e
NTND
254 } else if (CC_EQ(s,i, "upper")) {
255 if (ISUPPER(t_ch))
9b3497ca 256 matched = 1;
b79c0c37
AR
257 else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
258 matched = 1;
327f2f3e
NTND
259 } else if (CC_EQ(s,i, "xdigit")) {
260 if (ISXDIGIT(t_ch))
9b3497ca 261 matched = 1;
327f2f3e 262 } else /* malformed [:class:] string */
9b3497ca 263 return WM_ABORT_ALL;
327f2f3e
NTND
264 p_ch = 0; /* This makes "prev_ch" get set to 0. */
265 } else if (t_ch == p_ch)
9b3497ca 266 matched = 1;
327f2f3e 267 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
c41244e7
NTND
268 if (matched == negated ||
269 ((flags & WM_PATHNAME) && t_ch == '/'))
9b3497ca 270 return WM_NOMATCH;
327f2f3e
NTND
271 continue;
272 }
5230f605 273 }
5230f605 274
9b3497ca 275 return *text ? WM_NOMATCH : WM_MATCH;
5230f605
NTND
276}
277
278/* Match the "pattern" against the "text" string. */
55d34269 279int wildmatch(const char *pattern, const char *text, unsigned int flags)
5230f605 280{
0c528168 281 return dowild((const uchar*)pattern, (const uchar*)text, flags);
5230f605 282}