1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
19 * file for a list of people on the GTK+ Team. See the ChangeLog
20 * files for a list of changes. These files are distributed with
21 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 * Stripped down, converted to UTF-8 and test cases added
27 * Owen Taylor, 13 December 2002;
35 /* We need to make sure that all constants are defined
36 * to properly compile this file
43 get_char (const char **str)
45 gunichar c = g_utf8_get_char (*str);
46 *str = g_utf8_next_char (*str);
48 #ifdef G_PLATFORM_WIN32
49 c = g_unichar_tolower (c);
55 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
62 get_unescaped_char (const char **str,
63 gboolean *was_escaped)
65 gunichar c = get_char (str);
67 *was_escaped = DO_ESCAPE && c == '\\';
74 /* Match STRING against the filename pattern PATTERN, returning zero if
75 it matches, nonzero if not. */
78 gtk_fnmatch_intern (const char *pattern,
80 gboolean component_start,
81 gboolean no_leading_period)
83 const char *p = pattern, *n = string;
87 const char *last_n = n;
89 gunichar c = get_char (&p);
90 gunichar nc = get_char (&n);
97 else if (nc == G_DIR_SEPARATOR)
99 else if (nc == '.' && component_start && no_leading_period)
109 if (nc == '.' && component_start && no_leading_period)
113 const char *last_p = p;
115 for (last_p = p, c = get_char (&p);
116 c == '?' || c == '*';
117 last_p = p, c = get_char (&p))
123 else if (nc == G_DIR_SEPARATOR)
127 last_n = n; nc = get_char (&n);
132 /* If the pattern ends with wildcards, we have a
133 * guaranteed match unless there is a dir separator
134 * in the remainder of the string.
138 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
144 if (DO_ESCAPE && c == '\\')
147 for (p = last_p; nc != '\0';)
149 if ((c == '[' || nc == c) &&
150 gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
153 component_start = (nc == G_DIR_SEPARATOR);
163 /* Nonzero if the sense of the character class is inverted. */
165 gboolean was_escaped;
167 if (nc == '\0' || nc == G_DIR_SEPARATOR)
170 if (nc == '.' && component_start && no_leading_period)
173 not = (*p == '!' || *p == '^');
177 c = get_unescaped_char (&p, &was_escaped);
180 register gunichar cstart = c, cend = c;
182 /* [ (unterminated) loses. */
185 c = get_unescaped_char (&p, &was_escaped);
187 if (!was_escaped && c == '-' && *p != ']')
189 cend = get_unescaped_char (&p, &was_escaped);
196 if (nc >= cstart && nc <= cend)
199 if (!was_escaped && c == ']')
207 /* Skip the rest of the [...] that already matched. */
208 /* XXX 1003.2d11 is unclear if was_escaped is right. */
209 while (was_escaped || c != ']')
212 /* [... (unterminated) loses. */
215 c = get_unescaped_char (&p, &was_escaped);
227 component_start = (nc == G_DIR_SEPARATOR);
236 /* Match STRING against the filename pattern PATTERN, returning zero if
237 * it matches, nonzero if not.
239 * GTK+ used to use a old version of GNU fnmatch() that was buggy
240 * in various ways and didn't handle UTF-8. The following is
241 * converted to UTF-8. To simplify the process of making it
242 * correct, this is special-cased to the combinations of flags
243 * that gtkfilesel.c uses.
245 * FNM_FILE_NAME - always set
246 * FNM_LEADING_DIR - never set
247 * FNM_NOESCAPE - set only on windows
248 * FNM_CASEFOLD - set only on windows
251 _gtk_fnmatch (const char *pattern,
253 gboolean no_leading_period)
255 return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
258 #undef FNMATCH_TEST_CASES
259 #ifdef FNMATCH_TEST_CASES
261 #define TEST(pat, str, no_leading_period, result) \
262 g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
264 int main (int argc, char **argv)
266 TEST ("[a-]", "-", TRUE, TRUE);
268 TEST ("a", "a", TRUE, TRUE);
269 TEST ("a", "b", TRUE, FALSE);
271 /* Test what ? matches */
272 TEST ("?", "a", TRUE, TRUE);
273 TEST ("?", ".", TRUE, FALSE);
274 TEST ("a?", "a.", TRUE, TRUE);
275 TEST ("a/?", "a/b", TRUE, TRUE);
276 TEST ("a/?", "a/.", TRUE, FALSE);
277 TEST ("?", "/", TRUE, FALSE);
279 /* Test what * matches */
280 TEST ("*", "a", TRUE, TRUE);
281 TEST ("*", ".", TRUE, FALSE);
282 TEST ("a*", "a.", TRUE, TRUE);
283 TEST ("a/*", "a/b", TRUE, TRUE);
284 TEST ("a/*", "a/.", TRUE, FALSE);
285 TEST ("*", "/", TRUE, FALSE);
288 TEST ("[ab]", "a", TRUE, TRUE);
289 TEST ("[ab]", "c", TRUE, FALSE);
290 TEST ("[^ab]", "a", TRUE, FALSE);
291 TEST ("[!ab]", "a", TRUE, FALSE);
292 TEST ("[^ab]", "c", TRUE, TRUE);
293 TEST ("[!ab]", "c", TRUE, TRUE);
294 TEST ("[a-c]", "b", TRUE, TRUE);
295 TEST ("[a-c]", "d", TRUE, FALSE);
296 TEST ("[a-]", "-", TRUE, TRUE);
297 TEST ("[]]", "]", TRUE, TRUE);
298 TEST ("[^]]", "a", TRUE, TRUE);
299 TEST ("[!]]", "a", TRUE, TRUE);
301 /* Various unclosed ranges */
302 TEST ("[ab", "a", TRUE, FALSE);
303 TEST ("[a-", "a", TRUE, FALSE);
304 TEST ("[ab", "c", TRUE, FALSE);
305 TEST ("[a-", "c", TRUE, FALSE);
306 TEST ("[^]", "a", TRUE, FALSE);
308 /* Ranges and special no-wildcard matches */
309 TEST ("[.]", ".", TRUE, FALSE);
310 TEST ("a[.]", "a.", TRUE, TRUE);
311 TEST ("a/[.]", "a/.", TRUE, FALSE);
312 TEST ("[/]", "/", TRUE, FALSE);
313 TEST ("[^/]", "a", TRUE, TRUE);
315 /* Basic tests of * (and combinations of * and ?) */
316 TEST ("a*b", "ab", TRUE, TRUE);
317 TEST ("a*b", "axb", TRUE, TRUE);
318 TEST ("a*b", "axxb", TRUE, TRUE);
319 TEST ("a**b", "ab", TRUE, TRUE);
320 TEST ("a**b", "axb", TRUE, TRUE);
321 TEST ("a**b", "axxb", TRUE, TRUE);
322 TEST ("a*?*b", "ab", TRUE, FALSE);
323 TEST ("a*?*b", "axb", TRUE, TRUE);
324 TEST ("a*?*b", "axxb", TRUE, TRUE);
326 /* Test of *[range] */
327 TEST ("a*[cd]", "ac", TRUE, TRUE);
328 TEST ("a*[cd]", "axc", TRUE, TRUE);
329 TEST ("a*[cd]", "axx", TRUE, FALSE);
331 TEST ("a/[.]", "a/.", TRUE, FALSE);
332 TEST ("a*[.]", "a/.", TRUE, FALSE);
336 TEST ("ä", "ä", TRUE, TRUE); /* TEST ("ä", "ä", TRUE); */
337 TEST ("?", "ä", TRUE, TRUE); /* TEST ("?", "ä", TRUE); */
338 TEST ("*ö", "äö", TRUE, TRUE); /* TEST ("*ö", "äö", TRUE); */
339 TEST ("*ö", "ääö", TRUE, TRUE); /* TEST ("*ö", "ääö", TRUE); */
340 TEST ("[ä]", "ä", TRUE, TRUE); /* TEST ("[ä]", "ä", TRUE); */
341 TEST ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
342 TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
345 /* Tests of escaping */
346 TEST ("\\\\", "\\", TRUE, TRUE);
347 TEST ("\\?", "?", TRUE, TRUE);
348 TEST ("\\?", "a", TRUE, FALSE);
349 TEST ("\\*", "*", TRUE, TRUE);
350 TEST ("\\*", "a", TRUE, FALSE);
351 TEST ("\\[a-b]", "[a-b]", TRUE, TRUE);
352 TEST ("[\\\\]", "\\", TRUE, TRUE);
353 TEST ("[\\^a]", "a", TRUE, TRUE);
354 TEST ("[a\\-c]", "b", TRUE, FALSE);
355 TEST ("[a\\-c]", "-", TRUE, TRUE);
356 TEST ("[a\\]", "a", TRUE, FALSE);
357 #endif /* DO_ESCAPE */
362 #endif /* FNMATCH_TEST_CASES */