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
,
83 gboolean no_leading_period
)
85 const char *p
= pattern
, *n
= string
;
89 const char *last_n
= n
;
91 gunichar c
= get_char (&p
);
92 gunichar nc
= get_char (&n
);
99 else if (nc
== G_DIR_SEPARATOR
)
101 else if (nc
== '.' && component_start
&& no_leading_period
)
111 if (nc
== '.' && component_start
&& no_leading_period
)
115 const char *last_p
= p
;
117 for (last_p
= p
, c
= get_char (&p
);
118 c
== '?' || c
== '*';
119 last_p
= p
, c
= get_char (&p
))
125 else if (nc
== G_DIR_SEPARATOR
)
129 last_n
= n
; nc
= get_char (&n
);
134 /* If the pattern ends with wildcards, we have a
135 * guaranteed match unless there is a dir separator
136 * in the remainder of the string.
140 if (strchr (last_n
, G_DIR_SEPARATOR
) != NULL
)
146 if (DO_ESCAPE
&& c
== '\\')
149 for (p
= last_p
; nc
!= '\0';)
151 if ((c
== '[' || nc
== c
) &&
152 gtk_fnmatch_intern (p
, last_n
, component_start
, no_leading_period
))
155 component_start
= (nc
== G_DIR_SEPARATOR
);
165 /* Nonzero if the sense of the character class is inverted. */
167 gboolean was_escaped
;
169 if (nc
== '\0' || nc
== G_DIR_SEPARATOR
)
172 if (nc
== '.' && component_start
&& no_leading_period
)
175 not = (*p
== '!' || *p
== '^');
179 c
= get_unescaped_char (&p
, &was_escaped
);
182 register gunichar cstart
= c
, cend
= c
;
184 /* [ (unterminated) loses. */
187 c
= get_unescaped_char (&p
, &was_escaped
);
189 if (!was_escaped
&& c
== '-' && *p
!= ']')
191 cend
= get_unescaped_char (&p
, &was_escaped
);
198 if (nc
>= cstart
&& nc
<= cend
)
201 if (!was_escaped
&& c
== ']')
209 /* Skip the rest of the [...] that already matched. */
210 /* XXX 1003.2d11 is unclear if was_escaped is right. */
211 while (was_escaped
|| c
!= ']')
214 /* [... (unterminated) loses. */
217 c
= get_unescaped_char (&p
, &was_escaped
);
229 component_start
= (nc
== G_DIR_SEPARATOR
);
238 /* Match STRING against the filename pattern PATTERN, returning zero if
239 * it matches, nonzero if not.
241 * GTK+ used to use a old version of GNU fnmatch() that was buggy
242 * in various ways and didn't handle UTF-8. The following is
243 * converted to UTF-8. To simplify the process of making it
244 * correct, this is special-cased to the combinations of flags
245 * that gtkfilesel.c uses.
247 * FNM_FILE_NAME - always set
248 * FNM_LEADING_DIR - never set
249 * FNM_NOESCAPE - set only on windows
250 * FNM_CASEFOLD - set only on windows
253 _gtk_fnmatch (const char *pattern
,
255 gboolean no_leading_period
)
257 return gtk_fnmatch_intern (pattern
, string
, TRUE
, no_leading_period
);
260 #undef FNMATCH_TEST_CASES
261 #ifdef FNMATCH_TEST_CASES
263 #define TEST(pat, str, no_leading_period, result) \
264 g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
266 int main (int argc
, char **argv
)
268 TEST ("[a-]", "-", TRUE
, TRUE
);
270 TEST ("a", "a", TRUE
, TRUE
);
271 TEST ("a", "b", TRUE
, FALSE
);
273 /* Test what ? matches */
274 TEST ("?", "a", TRUE
, TRUE
);
275 TEST ("?", ".", TRUE
, FALSE
);
276 TEST ("a?", "a.", TRUE
, TRUE
);
277 TEST ("a/?", "a/b", TRUE
, TRUE
);
278 TEST ("a/?", "a/.", TRUE
, FALSE
);
279 TEST ("?", "/", TRUE
, FALSE
);
281 /* Test what * matches */
282 TEST ("*", "a", TRUE
, TRUE
);
283 TEST ("*", ".", TRUE
, FALSE
);
284 TEST ("a*", "a.", TRUE
, TRUE
);
285 TEST ("a/*", "a/b", TRUE
, TRUE
);
286 TEST ("a/*", "a/.", TRUE
, FALSE
);
287 TEST ("*", "/", TRUE
, FALSE
);
290 TEST ("[ab]", "a", TRUE
, TRUE
);
291 TEST ("[ab]", "c", TRUE
, FALSE
);
292 TEST ("[^ab]", "a", TRUE
, FALSE
);
293 TEST ("[!ab]", "a", TRUE
, FALSE
);
294 TEST ("[^ab]", "c", TRUE
, TRUE
);
295 TEST ("[!ab]", "c", TRUE
, TRUE
);
296 TEST ("[a-c]", "b", TRUE
, TRUE
);
297 TEST ("[a-c]", "d", TRUE
, FALSE
);
298 TEST ("[a-]", "-", TRUE
, TRUE
);
299 TEST ("[]]", "]", TRUE
, TRUE
);
300 TEST ("[^]]", "a", TRUE
, TRUE
);
301 TEST ("[!]]", "a", TRUE
, TRUE
);
303 /* Various unclosed ranges */
304 TEST ("[ab", "a", TRUE
, FALSE
);
305 TEST ("[a-", "a", TRUE
, FALSE
);
306 TEST ("[ab", "c", TRUE
, FALSE
);
307 TEST ("[a-", "c", TRUE
, FALSE
);
308 TEST ("[^]", "a", TRUE
, FALSE
);
310 /* Ranges and special no-wildcard matches */
311 TEST ("[.]", ".", TRUE
, FALSE
);
312 TEST ("a[.]", "a.", TRUE
, TRUE
);
313 TEST ("a/[.]", "a/.", TRUE
, FALSE
);
314 TEST ("[/]", "/", TRUE
, FALSE
);
315 TEST ("[^/]", "a", TRUE
, TRUE
);
317 /* Basic tests of * (and combinations of * and ?) */
318 TEST ("a*b", "ab", TRUE
, TRUE
);
319 TEST ("a*b", "axb", TRUE
, TRUE
);
320 TEST ("a*b", "axxb", TRUE
, TRUE
);
321 TEST ("a**b", "ab", TRUE
, TRUE
);
322 TEST ("a**b", "axb", TRUE
, TRUE
);
323 TEST ("a**b", "axxb", TRUE
, TRUE
);
324 TEST ("a*?*b", "ab", TRUE
, FALSE
);
325 TEST ("a*?*b", "axb", TRUE
, TRUE
);
326 TEST ("a*?*b", "axxb", TRUE
, TRUE
);
328 /* Test of *[range] */
329 TEST ("a*[cd]", "ac", TRUE
, TRUE
);
330 TEST ("a*[cd]", "axc", TRUE
, TRUE
);
331 TEST ("a*[cd]", "axx", TRUE
, FALSE
);
333 TEST ("a/[.]", "a/.", TRUE
, FALSE
);
334 TEST ("a*[.]", "a/.", TRUE
, FALSE
);
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 ("[ä]", "ä", TRUE
, TRUE
); /* TEST ("[ä]", "ä", TRUE); */
343 TEST ("[ä-ö]", "é", TRUE
, TRUE
); /* TEST ("[ä-ö]", "é", TRUE); */
344 TEST ("[ä-ö]", "a", TRUE
, FALSE
); /* TEST ("[ä-ö]", "a", FALSE); */
347 /* Tests of escaping */
348 TEST ("\\\\", "\\", TRUE
, TRUE
);
349 TEST ("\\?", "?", TRUE
, TRUE
);
350 TEST ("\\?", "a", TRUE
, FALSE
);
351 TEST ("\\*", "*", TRUE
, TRUE
);
352 TEST ("\\*", "a", TRUE
, FALSE
);
353 TEST ("\\[a-b]", "[a-b]", TRUE
, TRUE
);
354 TEST ("[\\\\]", "\\", TRUE
, TRUE
);
355 TEST ("[\\^a]", "a", TRUE
, TRUE
);
356 TEST ("[a\\-c]", "b", TRUE
, FALSE
);
357 TEST ("[a\\-c]", "-", TRUE
, TRUE
);
358 TEST ("[a\\]", "a", TRUE
, FALSE
);
359 #endif /* DO_ESCAPE */
364 #endif /* FNMATCH_TEST_CASES */