]> Pileus Git - ~andy/gtk/blob - gtk/fnmatch.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / fnmatch.c
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
2  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 /*
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/. 
22  */
23
24 /*
25  * Stripped down, converted to UTF-8 and test cases added
26  *
27  *                    Owen Taylor, 13 December 2002;
28  */
29
30 #include "config.h"
31
32 #include "gtkprivate.h"
33
34 #include <string.h>
35
36 #include <glib.h>
37
38 /* We need to make sure that all constants are defined
39  * to properly compile this file
40  */
41 #ifndef _GNU_SOURCE
42 #define _GNU_SOURCE
43 #endif
44
45 static gunichar
46 get_char (const char **str)
47 {
48   gunichar c = g_utf8_get_char (*str);
49   *str = g_utf8_next_char (*str);
50
51 #ifdef G_PLATFORM_WIN32
52   c = g_unichar_tolower (c);
53 #endif
54
55   return c;
56 }
57
58 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
59 #define DO_ESCAPE 0
60 #else  
61 #define DO_ESCAPE 1
62 #endif  
63
64 static gunichar
65 get_unescaped_char (const char **str,
66                     gboolean    *was_escaped)
67 {
68   gunichar c = get_char (str);
69
70   *was_escaped = DO_ESCAPE && c == '\\';
71   if (*was_escaped)
72     c = get_char (str);
73   
74   return c;
75 }
76
77 /* Match STRING against the filename pattern PATTERN, returning zero if
78    it matches, nonzero if not.  */
79
80 static gboolean
81 gtk_fnmatch_intern (const char *pattern,
82                     const char *string,
83                     gboolean    component_start,
84                     gboolean    no_leading_period)
85 {
86   const char *p = pattern, *n = string;
87   
88   while (*p)
89     {
90       const char *last_n = n;
91       
92       gunichar c = get_char (&p);
93       gunichar nc = get_char (&n);
94       
95       switch (c)
96         {
97         case '?':
98           if (nc == '\0')
99             return FALSE;
100           else if (nc == G_DIR_SEPARATOR)
101             return FALSE;
102           else if (nc == '.' && component_start && no_leading_period)
103             return FALSE;
104           break;
105         case '\\':
106           if (DO_ESCAPE)
107             c = get_char (&p);
108           if (nc != c)
109             return FALSE;
110           break;
111         case '*':
112           if (nc == '.' && component_start && no_leading_period)
113             return FALSE;
114
115           {
116             const char *last_p = p;
117
118             for (last_p = p, c = get_char (&p);
119                  c == '?' || c == '*';
120                  last_p = p, c = get_char (&p))
121               {
122                 if (c == '?')
123                   {
124                     if (nc == '\0')
125                       return FALSE;
126                     else if (nc == G_DIR_SEPARATOR)
127                       return FALSE;
128                     else
129                       {
130                         last_n = n; nc = get_char (&n);
131                       }
132                   }
133               }
134
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.
138              */
139             if (c == '\0')
140               {
141                 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
142                   return FALSE;
143                 else
144                   return TRUE;
145               }
146
147             if (DO_ESCAPE && c == '\\')
148               c = get_char (&p);
149
150             for (p = last_p; nc != '\0';)
151               {
152                 if ((c == '[' || nc == c) &&
153                     gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
154                   return TRUE;
155                 
156                 component_start = (nc == G_DIR_SEPARATOR);
157                 last_n = n;
158                 nc = get_char (&n);
159               }
160                   
161             return FALSE;
162           }
163
164         case '[':
165           {
166             /* Nonzero if the sense of the character class is inverted.  */
167             gboolean not;
168             gboolean was_escaped;
169
170             if (nc == '\0' || nc == G_DIR_SEPARATOR)
171               return FALSE;
172
173             if (nc == '.' && component_start && no_leading_period)
174               return FALSE;
175
176             not = (*p == '!' || *p == '^');
177             if (not)
178               ++p;
179
180             c = get_unescaped_char (&p, &was_escaped);
181             for (;;)
182               {
183                 register gunichar cstart = c, cend = c;
184                 if (c == '\0')
185                   /* [ (unterminated) loses.  */
186                   return FALSE;
187
188                 c = get_unescaped_char (&p, &was_escaped);
189                 
190                 if (!was_escaped && c == '-' && *p != ']')
191                   {
192                     cend = get_unescaped_char (&p, &was_escaped);
193                     if (cend == '\0')
194                       return FALSE;
195
196                     c = get_char (&p);
197                   }
198
199                 if (nc >= cstart && nc <= cend)
200                   goto matched;
201
202                 if (!was_escaped && c == ']')
203                   break;
204               }
205             if (!not)
206               return FALSE;
207             break;
208
209           matched:;
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 != ']')
213               {
214                 if (c == '\0')
215                   /* [... (unterminated) loses.  */
216                   return FALSE;
217
218                 c = get_unescaped_char (&p, &was_escaped);
219               }
220             if (not)
221               return FALSE;
222           }
223           break;
224
225         default:
226           if (c != nc)
227             return FALSE;
228         }
229
230       component_start = (nc == G_DIR_SEPARATOR);
231     }
232
233   if (*n == '\0')
234     return TRUE;
235
236   return FALSE;
237 }
238
239 /* Match STRING against the filename pattern PATTERN, returning zero if
240  *  it matches, nonzero if not.
241  *
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.
247  *
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
252  */
253 gboolean
254 _gtk_fnmatch (const char *pattern,
255               const char *string,
256               gboolean no_leading_period)
257 {
258   return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
259 }
260
261 #undef FNMATCH_TEST_CASES
262 #ifdef FNMATCH_TEST_CASES
263
264 #define TEST(pat, str, no_leading_period, result) \
265   g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
266
267 int main (int argc, char **argv)
268 {
269   TEST ("[a-]", "-", TRUE, TRUE);
270   
271   TEST ("a", "a", TRUE, TRUE);
272   TEST ("a", "b", TRUE, FALSE);
273
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);
281
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);
289
290   /* Range tests */
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);
303
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);
310
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);
317   
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);
328
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);
333
334   TEST ("a/[.]", "a/.", TRUE, FALSE);
335   TEST ("a*[.]", "a/.", TRUE, FALSE);
336
337   /* Test of UTF-8 */
338
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); */
346
347 #ifdef DO_ESCAPE
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 */
361   
362   return 0;
363 }
364
365 #endif /* FNMATCH_TEST_CASES */