wildmatch: fix case-insensitive matching
[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
21#define FALSE 0
22#define TRUE 1
3ae5396c
NTND
23
24#define NOMATCH 1
25#define MATCH 0
5230f605
NTND
26#define ABORT_ALL -1
27#define ABORT_TO_STARSTAR -2
28
29#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
30 && *(class) == *(litmatch) \
31 && strncmp((char*)class, litmatch, len) == 0)
32
33#if defined STDC_HEADERS || !defined isascii
34# define ISASCII(c) 1
35#else
36# define ISASCII(c) isascii(c)
37#endif
38
39#ifdef isblank
40# define ISBLANK(c) (ISASCII(c) && isblank(c))
41#else
42# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
43#endif
44
45#ifdef isgraph
46# define ISGRAPH(c) (ISASCII(c) && isgraph(c))
47#else
48# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
49#endif
50
51#define ISPRINT(c) (ISASCII(c) && isprint(c))
52#define ISDIGIT(c) (ISASCII(c) && isdigit(c))
53#define ISALNUM(c) (ISASCII(c) && isalnum(c))
54#define ISALPHA(c) (ISASCII(c) && isalpha(c))
55#define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
56#define ISLOWER(c) (ISASCII(c) && islower(c))
57#define ISPUNCT(c) (ISASCII(c) && ispunct(c))
58#define ISSPACE(c) (ISASCII(c) && isspace(c))
59#define ISUPPER(c) (ISASCII(c) && isupper(c))
60#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
61
b0e02876 62/* Match pattern "p" against "text" */
9b4edc0a 63static int dowild(const uchar *p, const uchar *text, int force_lower_case)
5230f605 64{
327f2f3e 65 uchar p_ch;
5230f605 66
327f2f3e
NTND
67 for ( ; (p_ch = *p) != '\0'; text++, p++) {
68 int matched, special;
69 uchar t_ch, prev_ch;
70 if ((t_ch = *text) == '\0' && p_ch != '*')
5230f605 71 return ABORT_ALL;
327f2f3e
NTND
72 if (force_lower_case && ISUPPER(t_ch))
73 t_ch = tolower(t_ch);
164bf83a
NTND
74 if (force_lower_case && ISUPPER(p_ch))
75 p_ch = tolower(p_ch);
327f2f3e
NTND
76 switch (p_ch) {
77 case '\\':
78 /* Literal match with following character. Note that the test
79 * in "default" handles the p[1] == '\0' failure case. */
5230f605 80 p_ch = *++p;
327f2f3e
NTND
81 /* FALLTHROUGH */
82 default:
83 if (t_ch != p_ch)
3ae5396c 84 return NOMATCH;
327f2f3e
NTND
85 continue;
86 case '?':
87 /* Match anything but '/'. */
88 if (t_ch == '/')
3ae5396c 89 return NOMATCH;
5230f605 90 continue;
327f2f3e
NTND
91 case '*':
92 if (*++p == '*') {
93 while (*++p == '*') {}
94 special = TRUE;
95 } else
96 special = FALSE;
97 if (*p == '\0') {
98 /* Trailing "**" matches everything. Trailing "*" matches
99 * only if there are no more slash characters. */
100 if (!special) {
101 if (strchr((char*)text, '/') != NULL)
3ae5396c 102 return NOMATCH;
327f2f3e 103 }
3ae5396c 104 return MATCH;
327f2f3e
NTND
105 }
106 while (1) {
107 if (t_ch == '\0')
108 break;
9b4edc0a 109 if ((matched = dowild(p, text, force_lower_case)) != NOMATCH) {
327f2f3e
NTND
110 if (!special || matched != ABORT_TO_STARSTAR)
111 return matched;
112 } else if (!special && t_ch == '/')
113 return ABORT_TO_STARSTAR;
114 t_ch = *++text;
115 }
5230f605 116 return ABORT_ALL;
327f2f3e
NTND
117 case '[':
118 p_ch = *++p;
119#ifdef NEGATE_CLASS2
120 if (p_ch == NEGATE_CLASS2)
121 p_ch = NEGATE_CLASS;
122#endif
123 /* Assign literal TRUE/FALSE because of "matched" comparison. */
124 special = p_ch == NEGATE_CLASS? TRUE : FALSE;
125 if (special) {
126 /* Inverted character class. */
127 p_ch = *++p;
128 }
129 prev_ch = 0;
130 matched = FALSE;
131 do {
132 if (!p_ch)
133 return ABORT_ALL;
134 if (p_ch == '\\') {
135 p_ch = *++p;
136 if (!p_ch)
137 return ABORT_ALL;
138 if (t_ch == p_ch)
139 matched = TRUE;
140 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
141 p_ch = *++p;
142 if (p_ch == '\\') {
143 p_ch = *++p;
144 if (!p_ch)
145 return ABORT_ALL;
146 }
147 if (t_ch <= p_ch && t_ch >= prev_ch)
148 matched = TRUE;
149 p_ch = 0; /* This makes "prev_ch" get set to 0. */
150 } else if (p_ch == '[' && p[1] == ':') {
151 const uchar *s;
152 int i;
153 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
154 if (!p_ch)
155 return ABORT_ALL;
156 i = p - s - 1;
157 if (i < 0 || p[-1] != ':') {
158 /* Didn't find ":]", so treat like a normal set. */
159 p = s - 2;
160 p_ch = '[';
161 if (t_ch == p_ch)
162 matched = TRUE;
163 continue;
164 }
165 if (CC_EQ(s,i, "alnum")) {
166 if (ISALNUM(t_ch))
167 matched = TRUE;
168 } else if (CC_EQ(s,i, "alpha")) {
169 if (ISALPHA(t_ch))
170 matched = TRUE;
171 } else if (CC_EQ(s,i, "blank")) {
172 if (ISBLANK(t_ch))
173 matched = TRUE;
174 } else if (CC_EQ(s,i, "cntrl")) {
175 if (ISCNTRL(t_ch))
176 matched = TRUE;
177 } else if (CC_EQ(s,i, "digit")) {
178 if (ISDIGIT(t_ch))
179 matched = TRUE;
180 } else if (CC_EQ(s,i, "graph")) {
181 if (ISGRAPH(t_ch))
182 matched = TRUE;
183 } else if (CC_EQ(s,i, "lower")) {
184 if (ISLOWER(t_ch))
185 matched = TRUE;
186 } else if (CC_EQ(s,i, "print")) {
187 if (ISPRINT(t_ch))
188 matched = TRUE;
189 } else if (CC_EQ(s,i, "punct")) {
190 if (ISPUNCT(t_ch))
191 matched = TRUE;
192 } else if (CC_EQ(s,i, "space")) {
193 if (ISSPACE(t_ch))
194 matched = TRUE;
195 } else if (CC_EQ(s,i, "upper")) {
196 if (ISUPPER(t_ch))
197 matched = TRUE;
198 } else if (CC_EQ(s,i, "xdigit")) {
199 if (ISXDIGIT(t_ch))
200 matched = TRUE;
201 } else /* malformed [:class:] string */
202 return ABORT_ALL;
203 p_ch = 0; /* This makes "prev_ch" get set to 0. */
204 } else if (t_ch == p_ch)
205 matched = TRUE;
206 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
207 if (matched == special || t_ch == '/')
3ae5396c 208 return NOMATCH;
327f2f3e
NTND
209 continue;
210 }
5230f605 211 }
5230f605 212
3ae5396c 213 return *text ? NOMATCH : MATCH;
5230f605
NTND
214}
215
216/* Match the "pattern" against the "text" string. */
9b4edc0a 217int wildmatch(const char *pattern, const char *text, int flags)
5230f605 218{
9b4edc0a
NTND
219 return dowild((const uchar*)pattern, (const uchar*)text,
220 flags & FNM_CASEFOLD ? 1 :0);
5230f605 221}