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