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.
28 #include "gmessages.h"
33 /* keep enum and structure of gpattern.c and patterntest.c in sync */
36 G_MATCH_ALL, /* "*A?A*" */
37 G_MATCH_ALL_TAIL, /* "*A?AA" */
38 G_MATCH_HEAD, /* "AAAA*" */
39 G_MATCH_TAIL, /* "*AAAA" */
40 G_MATCH_EXACT, /* "AAAAA" */
46 GMatchType match_type;
53 /* --- functions --- */
55 static inline gboolean
56 g_pattern_ph_match (const gchar *match_pattern,
57 const gchar *match_string)
59 register const gchar *pattern, *string;
62 pattern = match_pattern;
63 string = match_string;
74 string = g_utf8_next_char (string);
86 string = g_utf8_next_char (string);
89 while (ch == '*' || ch == '?');
98 string = g_utf8_next_char (string);
101 if (g_pattern_ph_match (pattern, string))
123 g_pattern_match (GPatternSpec *pspec,
126 const gchar *string_reversed)
128 g_return_val_if_fail (pspec != NULL, FALSE);
129 g_return_val_if_fail (string != NULL, FALSE);
131 if (pspec->min_length > string_length)
134 switch (pspec->match_type)
137 return g_pattern_ph_match (pspec->pattern, string);
138 case G_MATCH_ALL_TAIL:
140 return g_pattern_ph_match (pspec->pattern, string_reversed);
145 tmp = g_utf8_strreverse (string, string_length);
146 result = g_pattern_ph_match (pspec->pattern, tmp);
151 if (pspec->pattern_length == string_length)
152 return strcmp (pspec->pattern, string) == 0;
153 else if (pspec->pattern_length)
154 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
158 if (pspec->pattern_length)
159 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
163 if (pspec->pattern_length != string_length)
166 return strcmp (pspec->pattern, string) == 0;
168 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
174 g_pattern_spec_new (const gchar *pattern)
177 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
178 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
179 gboolean follows_wildcard = FALSE;
180 guint pending_jokers = 0;
185 g_return_val_if_fail (pattern != NULL, NULL);
187 /* canonicalize pattern and collect necessary stats */
188 pspec = g_new (GPatternSpec, 1);
189 pspec->pattern_length = strlen (pattern);
190 pspec->min_length = 0;
191 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
193 for (i = 0, s = pattern; *s != 0; s++)
198 if (follows_wildcard) /* compress multiple wildcards */
200 pspec->pattern_length--;
203 follows_wildcard = TRUE;
213 for (; pending_jokers; pending_jokers--, i++) {
219 follows_wildcard = FALSE;
226 for (; pending_jokers; pending_jokers--) {
233 seen_joker = hj_pos >= 0;
234 seen_wildcard = hw_pos >= 0;
235 more_wildcards = seen_wildcard && hw_pos != tw_pos;
237 /* special case sole head/tail wildcard or exact matches */
238 if (!seen_joker && !more_wildcards)
240 if (pspec->pattern[0] == '*')
242 pspec->match_type = G_MATCH_TAIL;
243 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
244 pspec->pattern[pspec->pattern_length] = 0;
247 if (pspec->pattern_length > 0 &&
248 pspec->pattern[pspec->pattern_length - 1] == '*')
250 pspec->match_type = G_MATCH_HEAD;
251 pspec->pattern[--pspec->pattern_length] = 0;
256 pspec->match_type = G_MATCH_EXACT;
261 /* now just need to distinguish between head or tail match start */
262 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
263 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
265 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
266 else /* seen_joker */
267 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
268 if (pspec->match_type == G_MATCH_ALL_TAIL) {
269 gchar *tmp = pspec->pattern;
270 pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
277 g_pattern_spec_free (GPatternSpec *pspec)
279 g_return_if_fail (pspec != NULL);
281 g_free (pspec->pattern);
286 g_pattern_spec_equal (GPatternSpec *pspec1,
287 GPatternSpec *pspec2)
289 g_return_val_if_fail (pspec1 != NULL, FALSE);
290 g_return_val_if_fail (pspec2 != NULL, FALSE);
292 return (pspec1->pattern_length == pspec2->pattern_length &&
293 pspec1->match_type == pspec2->match_type &&
294 strcmp (pspec1->pattern, pspec2->pattern) == 0);
298 g_pattern_match_string (GPatternSpec *pspec,
301 g_return_val_if_fail (pspec != NULL, FALSE);
302 g_return_val_if_fail (string != NULL, FALSE);
304 return g_pattern_match (pspec, strlen (string), string, NULL);
308 g_pattern_match_simple (const gchar *pattern,
314 g_return_val_if_fail (pattern != NULL, FALSE);
315 g_return_val_if_fail (string != NULL, FALSE);
317 pspec = g_pattern_spec_new (pattern);
318 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
319 g_pattern_spec_free (pspec);