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;
32 #include "gtkprivate.h"
38 /* We need to make sure that all constants are defined
39 * to properly compile this file
46 get_char (const char **str)
48 gunichar c = g_utf8_get_char (*str);
49 *str = g_utf8_next_char (*str);
51 #ifdef G_PLATFORM_WIN32
52 c = g_unichar_tolower (c);
58 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
65 get_unescaped_char (const char **str,
66 gboolean *was_escaped)
68 gunichar c = get_char (str);
70 *was_escaped = DO_ESCAPE && c == '\\';
77 /* Match STRING against the filename pattern PATTERN, returning zero if
78 it matches, nonzero if not. */
81 gtk_fnmatch_intern (const char *pattern,
83 gboolean component_start,
84 gboolean no_leading_period)
86 const char *p = pattern, *n = string;
90 const char *last_n = n;
92 gunichar c = get_char (&p);
93 gunichar nc = get_char (&n);
100 else if (nc == G_DIR_SEPARATOR)
102 else if (nc == '.' && component_start && no_leading_period)
112 if (nc == '.' && component_start && no_leading_period)
116 const char *last_p = p;
118 for (last_p = p, c = get_char (&p);
119 c == '?' || c == '*';
120 last_p = p, c = get_char (&p))
126 else if (nc == G_DIR_SEPARATOR)
130 last_n = n; nc = get_char (&n);
135 /* If the pattern ends with wildcards, we have a
136 * guaranteed match unless there is a dir separator
137 * in the remainder of the string.
141 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
147 if (DO_ESCAPE && c == '\\')
150 for (p = last_p; nc != '\0';)
152 if ((c == '[' || nc == c) &&
153 gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
156 component_start = (nc == G_DIR_SEPARATOR);
166 /* Nonzero if the sense of the character class is inverted. */
168 gboolean was_escaped;
170 if (nc == '\0' || nc == G_DIR_SEPARATOR)
173 if (nc == '.' && component_start && no_leading_period)
176 not = (*p == '!' || *p == '^');
180 c = get_unescaped_char (&p, &was_escaped);
183 register gunichar cstart = c, cend = c;
185 /* [ (unterminated) loses. */
188 c = get_unescaped_char (&p, &was_escaped);
190 if (!was_escaped && c == '-' && *p != ']')
192 cend = get_unescaped_char (&p, &was_escaped);
199 if (nc >= cstart && nc <= cend)
202 if (!was_escaped && c == ']')
210 /* Skip the rest of the [...] that already matched. */
211 /* XXX 1003.2d11 is unclear if was_escaped is right. */
212 while (was_escaped || c != ']')
215 /* [... (unterminated) loses. */
218 c = get_unescaped_char (&p, &was_escaped);
230 component_start = (nc == G_DIR_SEPARATOR);
239 /* Match STRING against the filename pattern PATTERN, returning zero if
240 * it matches, nonzero if not.
242 * GTK+ used to use a old version of GNU fnmatch() that was buggy
243 * in various ways and didn't handle UTF-8. The following is
244 * converted to UTF-8. To simplify the process of making it
245 * correct, this is special-cased to the combinations of flags
246 * that gtkfilesel.c uses.
248 * FNM_FILE_NAME - always set
249 * FNM_LEADING_DIR - never set
250 * FNM_NOESCAPE - set only on windows
251 * FNM_CASEFOLD - set only on windows
254 _gtk_fnmatch (const char *pattern,
256 gboolean no_leading_period)
258 return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
261 #undef FNMATCH_TEST_CASES
262 #ifdef FNMATCH_TEST_CASES
264 #define TEST(pat, str, no_leading_period, result) \
265 g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
267 int main (int argc, char **argv)
269 TEST ("[a-]", "-", TRUE, TRUE);
271 TEST ("a", "a", TRUE, TRUE);
272 TEST ("a", "b", TRUE, FALSE);
274 /* Test what ? matches */
275 TEST ("?", "a", TRUE, TRUE);
276 TEST ("?", ".", TRUE, FALSE);
277 TEST ("a?", "a.", TRUE, TRUE);
278 TEST ("a/?", "a/b", TRUE, TRUE);
279 TEST ("a/?", "a/.", TRUE, FALSE);
280 TEST ("?", "/", TRUE, FALSE);
282 /* Test what * matches */
283 TEST ("*", "a", TRUE, TRUE);
284 TEST ("*", ".", TRUE, FALSE);
285 TEST ("a*", "a.", TRUE, TRUE);
286 TEST ("a/*", "a/b", TRUE, TRUE);
287 TEST ("a/*", "a/.", TRUE, FALSE);
288 TEST ("*", "/", TRUE, FALSE);
291 TEST ("[ab]", "a", TRUE, TRUE);
292 TEST ("[ab]", "c", TRUE, FALSE);
293 TEST ("[^ab]", "a", TRUE, FALSE);
294 TEST ("[!ab]", "a", TRUE, FALSE);
295 TEST ("[^ab]", "c", TRUE, TRUE);
296 TEST ("[!ab]", "c", TRUE, TRUE);
297 TEST ("[a-c]", "b", TRUE, TRUE);
298 TEST ("[a-c]", "d", TRUE, FALSE);
299 TEST ("[a-]", "-", TRUE, TRUE);
300 TEST ("[]]", "]", TRUE, TRUE);
301 TEST ("[^]]", "a", TRUE, TRUE);
302 TEST ("[!]]", "a", TRUE, TRUE);
304 /* Various unclosed ranges */
305 TEST ("[ab", "a", TRUE, FALSE);
306 TEST ("[a-", "a", TRUE, FALSE);
307 TEST ("[ab", "c", TRUE, FALSE);
308 TEST ("[a-", "c", TRUE, FALSE);
309 TEST ("[^]", "a", TRUE, FALSE);
311 /* Ranges and special no-wildcard matches */
312 TEST ("[.]", ".", TRUE, FALSE);
313 TEST ("a[.]", "a.", TRUE, TRUE);
314 TEST ("a/[.]", "a/.", TRUE, FALSE);
315 TEST ("[/]", "/", TRUE, FALSE);
316 TEST ("[^/]", "a", TRUE, TRUE);
318 /* Basic tests of * (and combinations of * and ?) */
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, TRUE);
323 TEST ("a**b", "axb", TRUE, TRUE);
324 TEST ("a**b", "axxb", TRUE, TRUE);
325 TEST ("a*?*b", "ab", TRUE, FALSE);
326 TEST ("a*?*b", "axb", TRUE, TRUE);
327 TEST ("a*?*b", "axxb", TRUE, TRUE);
329 /* Test of *[range] */
330 TEST ("a*[cd]", "ac", TRUE, TRUE);
331 TEST ("a*[cd]", "axc", TRUE, TRUE);
332 TEST ("a*[cd]", "axx", TRUE, FALSE);
334 TEST ("a/[.]", "a/.", TRUE, FALSE);
335 TEST ("a*[.]", "a/.", TRUE, FALSE);
339 TEST ("ä", "ä", TRUE, TRUE); /* TEST ("ä", "ä", TRUE); */
340 TEST ("?", "ä", TRUE, TRUE); /* TEST ("?", "ä", TRUE); */
341 TEST ("*ö", "äö", TRUE, TRUE); /* TEST ("*ö", "äö", TRUE); */
342 TEST ("*ö", "ääö", TRUE, TRUE); /* TEST ("*ö", "ääö", TRUE); */
343 TEST ("[ä]", "ä", TRUE, TRUE); /* TEST ("[ä]", "ä", TRUE); */
344 TEST ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
345 TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
348 /* Tests of escaping */
349 TEST ("\\\\", "\\", TRUE, TRUE);
350 TEST ("\\?", "?", TRUE, TRUE);
351 TEST ("\\?", "a", TRUE, FALSE);
352 TEST ("\\*", "*", TRUE, TRUE);
353 TEST ("\\*", "a", TRUE, FALSE);
354 TEST ("\\[a-b]", "[a-b]", TRUE, TRUE);
355 TEST ("[\\\\]", "\\", TRUE, TRUE);
356 TEST ("[\\^a]", "a", TRUE, TRUE);
357 TEST ("[a\\-c]", "b", TRUE, FALSE);
358 TEST ("[a\\-c]", "-", TRUE, TRUE);
359 TEST ("[a\\]", "a", TRUE, FALSE);
360 #endif /* DO_ESCAPE */
365 #endif /* FNMATCH_TEST_CASES */