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, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * Stripped down, converted to UTF-8 and test cases added
29 * Owen Taylor, 13 December 2002;
37 /* We need to make sure that all constants are defined
38 * to properly compile this file
45 get_char (const char **str)
47 gunichar c = g_utf8_get_char (*str);
48 *str = g_utf8_next_char (*str);
50 #ifdef G_PLATFORM_WIN32
51 c = g_unichar_tolower (c);
57 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
64 get_unescaped_char (const char **str,
65 gboolean *was_escaped)
67 gunichar c = get_char (str);
69 *was_escaped = DO_ESCAPE && c == '\\';
76 /* Match STRING against the filename pattern PATTERN, returning zero if
77 it matches, nonzero if not. */
80 gtk_fnmatch_intern (const char *pattern,
82 gboolean component_start)
84 const char *p = pattern, *n = string;
88 const char *last_n = n;
90 gunichar c = get_char (&p);
91 gunichar nc = get_char (&n);
98 else if (nc == G_DIR_SEPARATOR)
100 else if (nc == '.' && component_start)
110 if (nc == '.' && component_start)
114 const char *last_p = p;
116 for (last_p = p, c = get_char (&p);
117 c == '?' || c == '*';
118 last_p = p, c = get_char (&p))
124 else if (nc == G_DIR_SEPARATOR)
128 last_n = n; nc = get_char (&n);
133 /* If the pattern ends with wildcards, we have a
134 * guaranteed match unless there is a dir separator
135 * in the remainder of the string.
139 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
145 if (DO_ESCAPE && c == '\\')
148 for (p = last_p; nc != '\0';)
150 if ((c == '[' || nc == c) &&
151 gtk_fnmatch_intern (p, last_n, component_start))
154 component_start = (nc == G_DIR_SEPARATOR);
164 /* Nonzero if the sense of the character class is inverted. */
166 gboolean was_escaped;
168 if (nc == '\0' || nc == G_DIR_SEPARATOR)
171 if (nc == '.' && component_start)
174 not = (*p == '!' || *p == '^');
178 c = get_unescaped_char (&p, &was_escaped);
181 register gunichar cstart = c, cend = c;
183 /* [ (unterminated) loses. */
186 c = get_unescaped_char (&p, &was_escaped);
188 if (!was_escaped && c == '-' && *p != ']')
190 cend = get_unescaped_char (&p, &was_escaped);
197 if (nc >= cstart && nc <= cend)
200 if (!was_escaped && c == ']')
208 /* Skip the rest of the [...] that already matched. */
209 /* XXX 1003.2d11 is unclear if was_escaped is right. */
210 while (was_escaped || c != ']')
213 /* [... (unterminated) loses. */
216 c = get_unescaped_char (&p, &was_escaped);
228 component_start = (nc == G_DIR_SEPARATOR);
237 /* Match STRING against the filename pattern PATTERN, returning zero if
238 * it matches, nonzero if not.
240 * GTK+ used to use a old version of GNU fnmatch() that was buggy
241 * in various ways and didn't handle UTF-8. The following is
242 * converted to UTF-8. To simplify the process of making it
243 * correct, this is special-cased to the combinations of flags
244 * that gtkfilesel.c uses.
246 * FNM_FILE_NAME - always set
247 * FNM_LEADING_DIR - never set
248 * FNM_PERIOD - always set
249 * FNM_NOESCAPE - set only on windows
250 * FNM_CASEFOLD - set only on windows
253 _gtk_fnmatch (const char *pattern,
256 return gtk_fnmatch_intern (pattern, string, TRUE);
259 #undef FNMATCH_TEST_CASES
260 #ifdef FNMATCH_TEST_CASES
262 #define TEST(pat, str, result) \
263 g_assert (_gtk_fnmatch ((pat), (str)) == result)
265 int main (int argc, char **argv)
267 TEST ("[a-]", "-", TRUE);
269 TEST ("a", "a", TRUE);
270 TEST ("a", "b", FALSE);
272 /* Test what ? matches */
273 TEST ("?", "a", TRUE);
274 TEST ("?", ".", FALSE);
275 TEST ("a?", "a.", TRUE);
276 TEST ("a/?", "a/b", TRUE);
277 TEST ("a/?", "a/.", FALSE);
278 TEST ("?", "/", FALSE);
280 /* Test what * matches */
281 TEST ("*", "a", TRUE);
282 TEST ("*", ".", FALSE);
283 TEST ("a*", "a.", TRUE);
284 TEST ("a/*", "a/b", TRUE);
285 TEST ("a/*", "a/.", FALSE);
286 TEST ("*", "/", FALSE);
289 TEST ("[ab]", "a", TRUE);
290 TEST ("[ab]", "c", FALSE);
291 TEST ("[^ab]", "a", FALSE);
292 TEST ("[!ab]", "a", FALSE);
293 TEST ("[^ab]", "c", TRUE);
294 TEST ("[!ab]", "c", TRUE);
295 TEST ("[a-c]", "b", TRUE);
296 TEST ("[a-c]", "d", FALSE);
297 TEST ("[a-]", "-", TRUE);
298 TEST ("[]]", "]", TRUE);
299 TEST ("[^]]", "a", TRUE);
300 TEST ("[!]]", "a", TRUE);
302 /* Various unclosed ranges */
303 TEST ("[ab", "a", FALSE);
304 TEST ("[a-", "a", FALSE);
305 TEST ("[ab", "c", FALSE);
306 TEST ("[a-", "c", FALSE);
307 TEST ("[^]", "a", FALSE);
309 /* Ranges and special no-wildcard matches */
310 TEST ("[.]", ".", FALSE);
311 TEST ("a[.]", "a.", TRUE);
312 TEST ("a/[.]", "a/.", FALSE);
313 TEST ("[/]", "/", FALSE);
314 TEST ("[^/]", "a", TRUE);
316 /* Basic tests of * (and combinations of * and ?) */
317 TEST ("a*b", "ab", TRUE);
318 TEST ("a*b", "axb", TRUE);
319 TEST ("a*b", "axxb", TRUE);
320 TEST ("a**b", "ab", TRUE);
321 TEST ("a**b", "axb", TRUE);
322 TEST ("a**b", "axxb", TRUE);
323 TEST ("a*?*b", "ab", FALSE);
324 TEST ("a*?*b", "axb", TRUE);
325 TEST ("a*?*b", "axxb", TRUE);
327 /* Test of *[range] */
328 TEST ("a*[cd]", "ac", TRUE);
329 TEST ("a*[cd]", "axc", TRUE);
330 TEST ("a*[cd]", "axx", FALSE);
332 TEST ("a/[.]", "a/.", FALSE);
333 TEST ("a*[.]", "a/.", FALSE);
337 TEST ("ä", "ä", TRUE); /* TEST ("ä", "ä", TRUE); */
338 TEST ("?", "ä", TRUE); /* TEST ("?", "ä", TRUE); */
339 TEST ("*ö", "äö", TRUE); /* TEST ("*ö", "äö", TRUE); */
340 TEST ("*ö", "ääö", TRUE); /* TEST ("*ö", "ääö", TRUE); */
341 TEST ("[ä]", "ä", TRUE); /* TEST ("[ä]", "ä", TRUE); */
342 TEST ("[ä-ö]", "é", TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
343 TEST ("[ä-ö]", "a", FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
346 /* Tests of escaping */
347 TEST ("\\\\", "\\", TRUE);
348 TEST ("\\?", "?", TRUE);
349 TEST ("\\?", "a", FALSE);
350 TEST ("\\*", "*", TRUE);
351 TEST ("\\*", "a", FALSE);
352 TEST ("\\[a-b]", "[a-b]", TRUE);
353 TEST ("[\\\\]", "\\", TRUE);
354 TEST ("[\\^a]", "a", TRUE);
355 TEST ("[a\\-c]", "b", FALSE);
356 TEST ("[a\\-c]", "-", TRUE);
357 TEST ("[a\\]", "a", FALSE);
358 #endif /* DO_ESCAPE */
363 #endif /* FNMATCH_TEST_CASES */