1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997, 1999 Peter Mattis, Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gmessages.h"
28 /* keep enum and structure of gpattern.c and patterntest.c in sync */
31 G_MATCH_ALL, /* "*A?A*" */
32 G_MATCH_ALL_TAIL, /* "*A?AA" */
33 G_MATCH_HEAD, /* "AAAA*" */
34 G_MATCH_TAIL, /* "*AAAA" */
35 G_MATCH_EXACT, /* "AAAAA" */
41 GMatchType match_type;
48 /* --- functions --- */
51 * string: a UTF-8 string.
53 * Reverses a UTF-8 string. The @string must be valid UTF-8 encoded text.
54 * (Use g_utf8_validate() on all text before trying to use UTF-8
55 * utility functions with it.)
57 * Note that unlike g_strreverse(), this function returns
58 * newly-allocated memory, which should be freed with g_free() when
61 * Returns: a newly-allocated string which is the reverse of @string.
64 g_utf8_reverse (guint len, const gchar *string)
70 result = g_new (gchar, len + 1);
75 skip = g_utf8_skip[*(guchar*)p];
77 for (m = r; skip; skip--)
85 static inline gboolean
86 g_pattern_ph_match (const gchar *match_pattern,
87 const gchar *match_string)
89 register const gchar *pattern, *string;
92 pattern = match_pattern;
93 string = match_string;
104 string = g_utf8_next_char (string);
116 string = g_utf8_next_char (string);
119 while (ch == '*' || ch == '?');
124 while (ch != *string)
128 string = g_utf8_next_char (string);
131 if (g_pattern_ph_match (pattern, string))
153 g_pattern_match (GPatternSpec *pspec,
156 const gchar *string_reversed)
158 g_return_val_if_fail (pspec != NULL, FALSE);
159 g_return_val_if_fail (string != NULL, FALSE);
161 if (pspec->min_length > string_length)
164 switch (pspec->match_type)
167 return g_pattern_ph_match (pspec->pattern, string);
168 case G_MATCH_ALL_TAIL:
170 return g_pattern_ph_match (pspec->pattern, string_reversed);
175 tmp = g_utf8_reverse (string_length, string);
176 result = g_pattern_ph_match (pspec->pattern, tmp);
181 if (pspec->pattern_length == string_length)
182 return strcmp (pspec->pattern, string) == 0;
183 else if (pspec->pattern_length)
184 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
188 if (pspec->pattern_length)
189 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
193 if (pspec->pattern_length != string_length)
196 return strcmp (pspec->pattern, string) == 0;
198 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
204 g_pattern_spec_new (const gchar *pattern)
207 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
208 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
209 gboolean follows_wildcard = FALSE;
210 guint pending_jokers = 0;
215 g_return_val_if_fail (pattern != NULL, NULL);
217 /* canonicalize pattern and collect necessary stats */
218 pspec = g_new (GPatternSpec, 1);
219 pspec->pattern_length = strlen (pattern);
220 pspec->min_length = 0;
221 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
223 for (i = 0, s = pattern; *s != 0; s++)
228 if (follows_wildcard) /* compress multiple wildcards */
230 pspec->pattern_length--;
233 follows_wildcard = TRUE;
243 for (; pending_jokers; pending_jokers--, i++) {
249 follows_wildcard = FALSE;
256 for (; pending_jokers; pending_jokers--) {
263 seen_joker = hj_pos >= 0;
264 seen_wildcard = hw_pos >= 0;
265 more_wildcards = seen_wildcard && hw_pos != tw_pos;
267 /* special case sole head/tail wildcard or exact matches */
268 if (!seen_joker && !more_wildcards)
270 if (pspec->pattern[0] == '*')
272 pspec->match_type = G_MATCH_TAIL;
273 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
274 pspec->pattern[pspec->pattern_length] = 0;
277 if (pspec->pattern[pspec->pattern_length - 1] == '*')
279 pspec->match_type = G_MATCH_HEAD;
280 pspec->pattern[--pspec->pattern_length] = 0;
285 pspec->match_type = G_MATCH_EXACT;
290 /* now just need to distinguish between head or tail match start */
291 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
292 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
294 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
295 else /* seen_joker */
296 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
297 if (pspec->match_type == G_MATCH_ALL_TAIL) {
298 gchar *tmp = pspec->pattern;
299 pspec->pattern = g_utf8_reverse (pspec->pattern_length, pspec->pattern);
306 g_pattern_spec_free (GPatternSpec *pspec)
308 g_return_if_fail (pspec != NULL);
310 g_free (pspec->pattern);
315 g_pattern_spec_equal (GPatternSpec *pspec1,
316 GPatternSpec *pspec2)
318 g_return_val_if_fail (pspec1 != NULL, FALSE);
319 g_return_val_if_fail (pspec2 != NULL, FALSE);
321 return (pspec1->pattern_length == pspec2->pattern_length &&
322 pspec1->match_type == pspec2->match_type &&
323 strcmp (pspec1->pattern, pspec2->pattern) == 0);
327 g_pattern_match_string (GPatternSpec *pspec,
330 g_return_val_if_fail (pspec != NULL, FALSE);
331 g_return_val_if_fail (string != NULL, FALSE);
333 return g_pattern_match (pspec, strlen (string), string, NULL);
337 g_pattern_match_simple (const gchar *pattern,
343 g_return_val_if_fail (pattern != NULL, FALSE);
344 g_return_val_if_fail (string != NULL, FALSE);
346 pspec = g_pattern_spec_new (pattern);
347 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
348 g_pattern_spec_free (pspec);