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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jan 1, 2013 at 09:44 UTC
9b3497cab9986883ff2499722f8758795afa34b7
4 files changed
+62
-55
dir.c
+2
-1
@@ -595,7 +595,8 @@ int match_pathname(const char *pathname, int pathlen,
595
}
596
597
return wildmatch(pattern, name,
598
- ignore_case ? FNM_CASEFOLD : 0) == 0;
598
+ ignore_case ? WM_CASEFOLD : 0,
599
+ NULL) == 0;
600
}
601
602
/* Scan the list and let the last match determine the fate.
test-wildmatch.c
+2
-2
@@ -12,9 +12,9 @@ int main(int argc, char **argv)
12
argv[i] += 3;
13
}
14
if (!strcmp(argv[1], "wildmatch"))
15
- return !!wildmatch(argv[3], argv[2], 0);
15
+ return !!wildmatch(argv[3], argv[2], 0, NULL);
16
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);
18
else if (!strcmp(argv[1], "fnmatch"))
19
return !!fnmatch(argv[3], argv[2], FNM_PATHNAME);
20
else
wildmatch.c
+43
-45
@@ -18,9 +18,6 @@ typedef unsigned char uchar;
18
#define NEGATE_CLASS '!'
19
#define NEGATE_CLASS2 '^'
20
21
-#define FALSE 0
22
-#define TRUE 1
23
-
21
#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
22
&& *(class) == *(litmatch) \
23
&& strncmp((char*)class, litmatch, len) == 0)
@@ -64,7 +61,7 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
61
int matched, match_slash, negated;
62
uchar t_ch, prev_ch;
63
if ((t_ch = *text) == '\0' && p_ch != '*')
67
- return ABORT_ALL;
64
+ return WM_ABORT_ALL;
65
if (force_lower_case && ISUPPER(t_ch))
66
t_ch = tolower(t_ch);
67
if (force_lower_case && ISUPPER(p_ch))
@@ -77,12 +74,12 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
74
/* FALLTHROUGH */
75
default:
76
if (t_ch != p_ch)
80
- return NOMATCH;
77
+ return WM_NOMATCH;
78
continue;
79
case '?':
80
/* Match anything but '/'. */
81
if (t_ch == '/')
85
- return NOMATCH;
82
+ return WM_NOMATCH;
83
continue;
84
case '*':
85
if (*++p == '*') {
@@ -101,135 +98,136 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
98
* both foo/bar and foo/a/bar.
99
*/
100
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;
104
} else
108
- return ABORT_MALFORMED;
105
+ return WM_ABORT_MALFORMED;
106
} else
110
- match_slash = FALSE;
107
+ match_slash = 0;
108
if (*p == '\0') {
109
/* Trailing "**" matches everything. Trailing "*" matches
110
* only if there are no more slash characters. */
111
if (!match_slash) {
112
if (strchr((char*)text, '/') != NULL)
116
- return NOMATCH;
113
+ return WM_NOMATCH;
114
}
118
- return MATCH;
115
+ return WM_MATCH;
116
}
117
while (1) {
118
if (t_ch == '\0')
119
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)
122
return matched;
123
} else if (!match_slash && t_ch == '/')
127
- return ABORT_TO_STARSTAR;
124
+ return WM_ABORT_TO_STARSTAR;
125
t_ch = *++text;
126
}
130
- return ABORT_ALL;
127
+ return WM_ABORT_ALL;
128
case '[':
129
p_ch = *++p;
130
#ifdef NEGATE_CLASS2
131
if (p_ch == NEGATE_CLASS2)
132
p_ch = NEGATE_CLASS;
133
#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;
136
if (negated) {
137
/* Inverted character class. */
138
p_ch = *++p;
139
}
140
prev_ch = 0;
144
- matched = FALSE;
141
+ matched = 0;
142
do {
143
if (!p_ch)
147
- return ABORT_ALL;
144
+ return WM_ABORT_ALL;
145
if (p_ch == '\\') {
146
p_ch = *++p;
147
if (!p_ch)
151
- return ABORT_ALL;
148
+ return WM_ABORT_ALL;
149
if (t_ch == p_ch)
153
- matched = TRUE;
150
+ matched = 1;
151
} else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
152
p_ch = *++p;
153
if (p_ch == '\\') {
154
p_ch = *++p;
155
if (!p_ch)
159
- return ABORT_ALL;
156
+ return WM_ABORT_ALL;
157
}
158
if (t_ch <= p_ch && t_ch >= prev_ch)
162
- matched = TRUE;
159
+ matched = 1;
160
p_ch = 0; /* This makes "prev_ch" get set to 0. */
161
} else if (p_ch == '[' && p[1] == ':') {
162
const uchar *s;
163
int i;
164
for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
165
if (!p_ch)
169
- return ABORT_ALL;
166
+ return WM_ABORT_ALL;
167
i = p - s - 1;
168
if (i < 0 || p[-1] != ':') {
169
/* Didn't find ":]", so treat like a normal set. */
170
p = s - 2;
171
p_ch = '[';
172
if (t_ch == p_ch)
176
- matched = TRUE;
173
+ matched = 1;
174
continue;
175
}
176
if (CC_EQ(s,i, "alnum")) {
177
if (ISALNUM(t_ch))
181
- matched = TRUE;
178
+ matched = 1;
179
} else if (CC_EQ(s,i, "alpha")) {
180
if (ISALPHA(t_ch))
184
- matched = TRUE;
181
+ matched = 1;
182
} else if (CC_EQ(s,i, "blank")) {
183
if (ISBLANK(t_ch))
187
- matched = TRUE;
184
+ matched = 1;
185
} else if (CC_EQ(s,i, "cntrl")) {
186
if (ISCNTRL(t_ch))
190
- matched = TRUE;
187
+ matched = 1;
188
} else if (CC_EQ(s,i, "digit")) {
189
if (ISDIGIT(t_ch))
193
- matched = TRUE;
190
+ matched = 1;
191
} else if (CC_EQ(s,i, "graph")) {
192
if (ISGRAPH(t_ch))
196
- matched = TRUE;
193
+ matched = 1;
194
} else if (CC_EQ(s,i, "lower")) {
195
if (ISLOWER(t_ch))
199
- matched = TRUE;
196
+ matched = 1;
197
} else if (CC_EQ(s,i, "print")) {
198
if (ISPRINT(t_ch))
202
- matched = TRUE;
199
+ matched = 1;
200
} else if (CC_EQ(s,i, "punct")) {
201
if (ISPUNCT(t_ch))
205
- matched = TRUE;
202
+ matched = 1;
203
} else if (CC_EQ(s,i, "space")) {
204
if (ISSPACE(t_ch))
208
- matched = TRUE;
205
+ matched = 1;
206
} else if (CC_EQ(s,i, "upper")) {
207
if (ISUPPER(t_ch))
211
- matched = TRUE;
208
+ matched = 1;
209
} else if (CC_EQ(s,i, "xdigit")) {
210
if (ISXDIGIT(t_ch))
214
- matched = TRUE;
211
+ matched = 1;
212
} else /* malformed [:class:] string */
216
- return ABORT_ALL;
213
+ return WM_ABORT_ALL;
214
p_ch = 0; /* This makes "prev_ch" get set to 0. */
215
} else if (t_ch == p_ch)
219
- matched = TRUE;
216
+ matched = 1;
217
} while (prev_ch = p_ch, (p_ch = *++p) != ']');
218
if (matched == negated || t_ch == '/')
222
- return NOMATCH;
219
+ return WM_NOMATCH;
220
continue;
221
}
222
}
223
227
- return *text ? NOMATCH : MATCH;
224
+ return *text ? WM_NOMATCH : WM_MATCH;
225
}
226
227
/* 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)
230
{
231
return dowild((const uchar*)pattern, (const uchar*)text,
234
- flags & FNM_CASEFOLD ? 1 :0);
232
+ flags & WM_CASEFOLD ? 1 :0);
233
}
wildmatch.h
+15
-7
@@ -1,9 +1,17 @@
1
-/* wildmatch.h */
1
+#ifndef WILDMATCH_H
2
+#define WILDMATCH_H
3
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
5
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