Skip to content

Commit 9b3497c

Browse files
pcloudsgitster
authored andcommitted
wildmatch: rename constants and update prototype
- All exported constants now have a prefix WM_ - Do not rely on FNM_* constants, use the WM_ counterparts - Remove TRUE and FALSE to follow Git's coding style - While at it, turn flags type from int to unsigned int - Add an (unused yet) argument to carry extra information so that we don't have to change the prototype again later when we need to pass other stuff to wildmatch Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b6a3d33 commit 9b3497c

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

dir.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ int match_pathname(const char *pathname, int pathlen,
595595
}
596596

597597
return wildmatch(pattern, name,
598-
ignore_case ? FNM_CASEFOLD : 0) == 0;
598+
ignore_case ? WM_CASEFOLD : 0,
599+
NULL) == 0;
599600
}
600601

601602
/* Scan the list and let the last match determine the fate.

test-wildmatch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ int main(int argc, char **argv)
1212
argv[i] += 3;
1313
}
1414
if (!strcmp(argv[1], "wildmatch"))
15-
return !!wildmatch(argv[3], argv[2], 0);
15+
return !!wildmatch(argv[3], argv[2], 0, NULL);
1616
else if (!strcmp(argv[1], "iwildmatch"))
17-
return !!wildmatch(argv[3], argv[2], FNM_CASEFOLD);
17+
return !!wildmatch(argv[3], argv[2], WM_CASEFOLD, NULL);
1818
else if (!strcmp(argv[1], "fnmatch"))
1919
return !!fnmatch(argv[3], argv[2], FNM_PATHNAME);
2020
else

wildmatch.c

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ typedef unsigned char uchar;
1818
#define NEGATE_CLASS '!'
1919
#define NEGATE_CLASS2 '^'
2020

21-
#define FALSE 0
22-
#define TRUE 1
23-
2421
#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
2522
&& *(class) == *(litmatch) \
2623
&& strncmp((char*)class, litmatch, len) == 0)
@@ -64,7 +61,7 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
6461
int matched, match_slash, negated;
6562
uchar t_ch, prev_ch;
6663
if ((t_ch = *text) == '\0' && p_ch != '*')
67-
return ABORT_ALL;
64+
return WM_ABORT_ALL;
6865
if (force_lower_case && ISUPPER(t_ch))
6966
t_ch = tolower(t_ch);
7067
if (force_lower_case && ISUPPER(p_ch))
@@ -77,12 +74,12 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
7774
/* FALLTHROUGH */
7875
default:
7976
if (t_ch != p_ch)
80-
return NOMATCH;
77+
return WM_NOMATCH;
8178
continue;
8279
case '?':
8380
/* Match anything but '/'. */
8481
if (t_ch == '/')
85-
return NOMATCH;
82+
return WM_NOMATCH;
8683
continue;
8784
case '*':
8885
if (*++p == '*') {
@@ -101,135 +98,136 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
10198
* both foo/bar and foo/a/bar.
10299
*/
103100
if (p[0] == '/' &&
104-
dowild(p + 1, text, force_lower_case) == MATCH)
105-
return MATCH;
106-
match_slash = TRUE;
101+
dowild(p + 1, text, force_lower_case) == WM_MATCH)
102+
return WM_MATCH;
103+
match_slash = 1;
107104
} else
108-
return ABORT_MALFORMED;
105+
return WM_ABORT_MALFORMED;
109106
} else
110-
match_slash = FALSE;
107+
match_slash = 0;
111108
if (*p == '\0') {
112109
/* Trailing "**" matches everything. Trailing "*" matches
113110
* only if there are no more slash characters. */
114111
if (!match_slash) {
115112
if (strchr((char*)text, '/') != NULL)
116-
return NOMATCH;
113+
return WM_NOMATCH;
117114
}
118-
return MATCH;
115+
return WM_MATCH;
119116
}
120117
while (1) {
121118
if (t_ch == '\0')
122119
break;
123-
if ((matched = dowild(p, text, force_lower_case)) != NOMATCH) {
124-
if (!match_slash || matched != ABORT_TO_STARSTAR)
120+
if ((matched = dowild(p, text, force_lower_case)) != WM_NOMATCH) {
121+
if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
125122
return matched;
126123
} else if (!match_slash && t_ch == '/')
127-
return ABORT_TO_STARSTAR;
124+
return WM_ABORT_TO_STARSTAR;
128125
t_ch = *++text;
129126
}
130-
return ABORT_ALL;
127+
return WM_ABORT_ALL;
131128
case '[':
132129
p_ch = *++p;
133130
#ifdef NEGATE_CLASS2
134131
if (p_ch == NEGATE_CLASS2)
135132
p_ch = NEGATE_CLASS;
136133
#endif
137-
/* Assign literal TRUE/FALSE because of "matched" comparison. */
138-
negated = p_ch == NEGATE_CLASS? TRUE : FALSE;
134+
/* Assign literal 1/0 because of "matched" comparison. */
135+
negated = p_ch == NEGATE_CLASS ? 1 : 0;
139136
if (negated) {
140137
/* Inverted character class. */
141138
p_ch = *++p;
142139
}
143140
prev_ch = 0;
144-
matched = FALSE;
141+
matched = 0;
145142
do {
146143
if (!p_ch)
147-
return ABORT_ALL;
144+
return WM_ABORT_ALL;
148145
if (p_ch == '\\') {
149146
p_ch = *++p;
150147
if (!p_ch)
151-
return ABORT_ALL;
148+
return WM_ABORT_ALL;
152149
if (t_ch == p_ch)
153-
matched = TRUE;
150+
matched = 1;
154151
} else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
155152
p_ch = *++p;
156153
if (p_ch == '\\') {
157154
p_ch = *++p;
158155
if (!p_ch)
159-
return ABORT_ALL;
156+
return WM_ABORT_ALL;
160157
}
161158
if (t_ch <= p_ch && t_ch >= prev_ch)
162-
matched = TRUE;
159+
matched = 1;
163160
p_ch = 0; /* This makes "prev_ch" get set to 0. */
164161
} else if (p_ch == '[' && p[1] == ':') {
165162
const uchar *s;
166163
int i;
167164
for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
168165
if (!p_ch)
169-
return ABORT_ALL;
166+
return WM_ABORT_ALL;
170167
i = p - s - 1;
171168
if (i < 0 || p[-1] != ':') {
172169
/* Didn't find ":]", so treat like a normal set. */
173170
p = s - 2;
174171
p_ch = '[';
175172
if (t_ch == p_ch)
176-
matched = TRUE;
173+
matched = 1;
177174
continue;
178175
}
179176
if (CC_EQ(s,i, "alnum")) {
180177
if (ISALNUM(t_ch))
181-
matched = TRUE;
178+
matched = 1;
182179
} else if (CC_EQ(s,i, "alpha")) {
183180
if (ISALPHA(t_ch))
184-
matched = TRUE;
181+
matched = 1;
185182
} else if (CC_EQ(s,i, "blank")) {
186183
if (ISBLANK(t_ch))
187-
matched = TRUE;
184+
matched = 1;
188185
} else if (CC_EQ(s,i, "cntrl")) {
189186
if (ISCNTRL(t_ch))
190-
matched = TRUE;
187+
matched = 1;
191188
} else if (CC_EQ(s,i, "digit")) {
192189
if (ISDIGIT(t_ch))
193-
matched = TRUE;
190+
matched = 1;
194191
} else if (CC_EQ(s,i, "graph")) {
195192
if (ISGRAPH(t_ch))
196-
matched = TRUE;
193+
matched = 1;
197194
} else if (CC_EQ(s,i, "lower")) {
198195
if (ISLOWER(t_ch))
199-
matched = TRUE;
196+
matched = 1;
200197
} else if (CC_EQ(s,i, "print")) {
201198
if (ISPRINT(t_ch))
202-
matched = TRUE;
199+
matched = 1;
203200
} else if (CC_EQ(s,i, "punct")) {
204201
if (ISPUNCT(t_ch))
205-
matched = TRUE;
202+
matched = 1;
206203
} else if (CC_EQ(s,i, "space")) {
207204
if (ISSPACE(t_ch))
208-
matched = TRUE;
205+
matched = 1;
209206
} else if (CC_EQ(s,i, "upper")) {
210207
if (ISUPPER(t_ch))
211-
matched = TRUE;
208+
matched = 1;
212209
} else if (CC_EQ(s,i, "xdigit")) {
213210
if (ISXDIGIT(t_ch))
214-
matched = TRUE;
211+
matched = 1;
215212
} else /* malformed [:class:] string */
216-
return ABORT_ALL;
213+
return WM_ABORT_ALL;
217214
p_ch = 0; /* This makes "prev_ch" get set to 0. */
218215
} else if (t_ch == p_ch)
219-
matched = TRUE;
216+
matched = 1;
220217
} while (prev_ch = p_ch, (p_ch = *++p) != ']');
221218
if (matched == negated || t_ch == '/')
222-
return NOMATCH;
219+
return WM_NOMATCH;
223220
continue;
224221
}
225222
}
226223

227-
return *text ? NOMATCH : MATCH;
224+
return *text ? WM_NOMATCH : WM_MATCH;
228225
}
229226

230227
/* Match the "pattern" against the "text" string. */
231-
int wildmatch(const char *pattern, const char *text, int flags)
228+
int wildmatch(const char *pattern, const char *text,
229+
unsigned int flags, struct wildopts *wo)
232230
{
233231
return dowild((const uchar*)pattern, (const uchar*)text,
234-
flags & FNM_CASEFOLD ? 1 :0);
232+
flags & WM_CASEFOLD ? 1 :0);
235233
}

wildmatch.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
/* wildmatch.h */
1+
#ifndef WILDMATCH_H
2+
#define WILDMATCH_H
23

3-
#define ABORT_MALFORMED 2
4-
#define NOMATCH 1
5-
#define MATCH 0
6-
#define ABORT_ALL -1
7-
#define ABORT_TO_STARSTAR -2
4+
#define WM_CASEFOLD 1
85

9-
int wildmatch(const char *pattern, const char *text, int flags);
6+
#define WM_ABORT_MALFORMED 2
7+
#define WM_NOMATCH 1
8+
#define WM_MATCH 0
9+
#define WM_ABORT_ALL -1
10+
#define WM_ABORT_TO_STARSTAR -2
11+
12+
struct wildopts;
13+
14+
int wildmatch(const char *pattern, const char *text,
15+
unsigned int flags,
16+
struct wildopts *wo);
17+
#endif

0 commit comments

Comments
 (0)