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
47 get_char (const char **str)
49 gunichar c = g_utf8_get_char (*str);
50 *str = g_utf8_next_char (*str);
52 #ifdef G_PLATFORM_WIN32
53 c = g_unichar_tolower (c);
59 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
66 get_unescaped_char (const char **str,
67 gboolean *was_escaped)
69 gunichar c = get_char (str);
71 *was_escaped = DO_ESCAPE && c == '\\';
78 /* Match STRING against the filename pattern PATTERN, returning zero if
79 it matches, nonzero if not. */
82 gtk_fnmatch_intern (const char *pattern,
84 gboolean component_start,
85 gboolean no_leading_period)
87 const char *p = pattern, *n = string;
91 const char *last_n = n;
93 gunichar c = get_char (&p);
94 gunichar nc = get_char (&n);
101 else if (nc == G_DIR_SEPARATOR)
103 else if (nc == '.' && component_start && no_leading_period)
113 if (nc == '.' && component_start && no_leading_period)
117 const char *last_p = p;
119 for (last_p = p, c = get_char (&p);
120 c == '?' || c == '*';
121 last_p = p, c = get_char (&p))
127 else if (nc == G_DIR_SEPARATOR)
131 last_n = n; nc = get_char (&n);
136 /* If the pattern ends with wildcards, we have a
137 * guaranteed match unless there is a dir separator
138 * in the remainder of the string.
142 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
148 if (DO_ESCAPE && c == '\\')
151 for (p = last_p; nc != '\0';)
153 if ((c == '[' || nc == c) &&
154 gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
157 component_start = (nc == G_DIR_SEPARATOR);
167 /* Nonzero if the sense of the character class is inverted. */
169 gboolean was_escaped;
171 if (nc == '\0' || nc == G_DIR_SEPARATOR)
174 if (nc == '.' && component_start && no_leading_period)
177 not = (*p == '!' || *p == '^');
181 c = get_unescaped_char (&p, &was_escaped);
184 register gunichar cstart = c, cend = c;
186 /* [ (unterminated) loses. */
189 c = get_unescaped_char (&p, &was_escaped);
191 if (!was_escaped && c == '-' && *p != ']')
193 cend = get_unescaped_char (&p, &was_escaped);
200 if (nc >= cstart && nc <= cend)
203 if (!was_escaped && c == ']')
211 /* Skip the rest of the [...] that already matched. */
212 /* XXX 1003.2d11 is unclear if was_escaped is right. */
213 while (was_escaped || c != ']')
216 /* [... (unterminated) loses. */
219 c = get_unescaped_char (&p, &was_escaped);
231 component_start = (nc == G_DIR_SEPARATOR);
240 /* Match STRING against the filename pattern PATTERN, returning zero if
241 * it matches, nonzero if not.
243 * GTK+ used to use a old version of GNU fnmatch() that was buggy
244 * in various ways and didn't handle UTF-8. The following is
245 * converted to UTF-8. To simplify the process of making it
246 * correct, this is special-cased to the combinations of flags
247 * that gtkfilesel.c uses.
249 * FNM_FILE_NAME - always set
250 * FNM_LEADING_DIR - never set
251 * FNM_NOESCAPE - set only on windows
252 * FNM_CASEFOLD - set only on windows
255 _gtk_fnmatch (const char *pattern,
257 gboolean no_leading_period)
259 return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
262 #undef FNMATCH_TEST_CASES
263 #ifdef FNMATCH_TEST_CASES
265 #define TEST(pat, str, no_leading_period, result) \
266 g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
268 int main (int argc, char **argv)
270 TEST ("[a-]", "-", TRUE, TRUE);
272 TEST ("a", "a", TRUE, TRUE);
273 TEST ("a", "b", TRUE, FALSE);
275 /* Test what ? matches */
276 TEST ("?", "a", TRUE, TRUE);
277 TEST ("?", ".", TRUE, FALSE);
278 TEST ("a?", "a.", TRUE, TRUE);
279 TEST ("a/?", "a/b", TRUE, TRUE);
280 TEST ("a/?", "a/.", TRUE, FALSE);
281 TEST ("?", "/", TRUE, FALSE);
283 /* Test what * matches */
284 TEST ("*", "a", TRUE, TRUE);
285 TEST ("*", ".", TRUE, FALSE);
286 TEST ("a*", "a.", TRUE, TRUE);
287 TEST ("a/*", "a/b", TRUE, TRUE);
288 TEST ("a/*", "a/.", TRUE, FALSE);
289 TEST ("*", "/", TRUE, FALSE);
292 TEST ("[ab]", "a", TRUE, TRUE);
293 TEST ("[ab]", "c", TRUE, FALSE);
294 TEST ("[^ab]", "a", TRUE, FALSE);
295 TEST ("[!ab]", "a", TRUE, FALSE);
296 TEST ("[^ab]", "c", TRUE, TRUE);
297 TEST ("[!ab]", "c", TRUE, TRUE);
298 TEST ("[a-c]", "b", TRUE, TRUE);
299 TEST ("[a-c]", "d", TRUE, FALSE);
300 TEST ("[a-]", "-", TRUE, TRUE);
301 TEST ("[]]", "]", TRUE, TRUE);
302 TEST ("[^]]", "a", TRUE, TRUE);
303 TEST ("[!]]", "a", TRUE, TRUE);
305 /* Various unclosed ranges */
306 TEST ("[ab", "a", TRUE, FALSE);
307 TEST ("[a-", "a", TRUE, FALSE);
308 TEST ("[ab", "c", TRUE, FALSE);
309 TEST ("[a-", "c", TRUE, FALSE);
310 TEST ("[^]", "a", TRUE, FALSE);
312 /* Ranges and special no-wildcard matches */
313 TEST ("[.]", ".", TRUE, FALSE);
314 TEST ("a[.]", "a.", TRUE, TRUE);
315 TEST ("a/[.]", "a/.", TRUE, FALSE);
316 TEST ("[/]", "/", TRUE, FALSE);
317 TEST ("[^/]", "a", TRUE, TRUE);
319 /* Basic tests of * (and combinations of * and ?) */
320 TEST ("a*b", "ab", TRUE, TRUE);
321 TEST ("a*b", "axb", TRUE, TRUE);
322 TEST ("a*b", "axxb", TRUE, TRUE);
323 TEST ("a**b", "ab", TRUE, TRUE);
324 TEST ("a**b", "axb", TRUE, TRUE);
325 TEST ("a**b", "axxb", TRUE, TRUE);
326 TEST ("a*?*b", "ab", TRUE, FALSE);
327 TEST ("a*?*b", "axb", TRUE, TRUE);
328 TEST ("a*?*b", "axxb", TRUE, TRUE);
330 /* Test of *[range] */
331 TEST ("a*[cd]", "ac", TRUE, TRUE);
332 TEST ("a*[cd]", "axc", TRUE, TRUE);
333 TEST ("a*[cd]", "axx", TRUE, FALSE);
335 TEST ("a/[.]", "a/.", TRUE, FALSE);
336 TEST ("a*[.]", "a/.", TRUE, FALSE);
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 ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
346 TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
349 /* Tests of escaping */
350 TEST ("\\\\", "\\", TRUE, TRUE);
351 TEST ("\\?", "?", TRUE, TRUE);
352 TEST ("\\?", "a", TRUE, FALSE);
353 TEST ("\\*", "*", TRUE, TRUE);
354 TEST ("\\*", "a", TRUE, FALSE);
355 TEST ("\\[a-b]", "[a-b]", TRUE, TRUE);
356 TEST ("[\\\\]", "\\", TRUE, TRUE);
357 TEST ("[\\^a]", "a", TRUE, TRUE);
358 TEST ("[a\\-c]", "b", TRUE, FALSE);
359 TEST ("[a\\-c]", "-", TRUE, TRUE);
360 TEST ("[a\\]", "a", TRUE, FALSE);
361 #endif /* DO_ESCAPE */
366 #endif /* FNMATCH_TEST_CASES */