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"
24 #include "gutils.h" /* inline hassle */
27 /* keep enum and structure of gpattern.c and patterntest.c in sync */
30 G_MATCH_ALL, /* "*A?A*" */
31 G_MATCH_ALL_TAIL, /* "*A?AA" */
32 G_MATCH_HEAD, /* "AAAA*" */
33 G_MATCH_TAIL, /* "*AAAA" */
34 G_MATCH_EXACT, /* "AAAAA" */
39 GMatchType match_type;
45 /* --- functions --- */
47 instring_reverse (guint length,
65 strdup_reverse (guint length,
68 gchar *t, *dest = g_new (gchar, length + 1);
77 static inline gboolean
78 g_pattern_ph_match (const gchar *match_pattern,
79 const gchar *match_string)
81 register const gchar *pattern, *string;
84 pattern = match_pattern;
85 string = match_string;
111 while (ch == '*' || ch == '?');
116 while (ch != *string)
123 if (g_pattern_ph_match (pattern, string))
145 g_pattern_match (GPatternSpec *pspec,
148 const gchar *string_reversed)
150 g_return_val_if_fail (pspec != NULL, FALSE);
151 g_return_val_if_fail (string != NULL, FALSE);
153 switch (pspec->match_type)
158 return g_pattern_ph_match (pspec->pattern, string);
159 case G_MATCH_ALL_TAIL:
161 return g_pattern_ph_match (pspec->pattern, string_reversed);
164 tmp = strdup_reverse (string_length, string);
165 result = g_pattern_ph_match (pspec->pattern, tmp);
170 if (pspec->pattern_length > string_length)
172 else if (pspec->pattern_length == string_length)
173 return strcmp (pspec->pattern, string) == 0;
174 else if (pspec->pattern_length)
175 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
179 if (pspec->pattern_length > string_length)
181 else if (pspec->pattern_length == string_length)
184 return strcmp (pspec->pattern, string_reversed) == 0;
187 tmp = strdup_reverse (string_length, string);
188 result = strcmp (pspec->pattern, tmp) == 0;
193 else if (pspec->pattern_length)
196 return strncmp (pspec->pattern, string_reversed, pspec->pattern_length) == 0;
199 tmp = strdup_reverse (string_length, string);
200 result = strncmp (pspec->pattern, tmp, pspec->pattern_length) == 0;
208 if (pspec->pattern_length != string_length)
211 return strcmp (pspec->pattern, string) == 0;
213 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
219 g_pattern_spec_new (const gchar *pattern)
222 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
223 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
224 gboolean follows_wildcard = FALSE;
229 g_return_val_if_fail (pattern != NULL, NULL);
231 /* canonicalize pattern and collect necessary stats */
232 pspec = g_new (GPatternSpec, 1);
233 pspec->pattern_length = strlen (pattern);
234 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
236 for (i = 0, s = pattern; *s != 0; s++)
241 if (follows_wildcard) /* compress multiple wildcards */
243 pspec->pattern_length--;
246 follows_wildcard = TRUE;
257 follows_wildcard = FALSE;
264 seen_joker = hj_pos >= 0;
265 seen_wildcard = hw_pos >= 0;
266 more_wildcards = seen_wildcard && hw_pos != tw_pos;
268 /* special case sole head/tail wildcard or exact matches */
269 if (!seen_joker && !more_wildcards)
271 if (pspec->pattern[0] == '*')
273 pspec->match_type = G_MATCH_TAIL;
274 instring_reverse (pspec->pattern_length, pspec->pattern);
275 pspec->pattern[--pspec->pattern_length] = 0;
278 if (pspec->pattern[pspec->pattern_length - 1] == '*')
280 pspec->match_type = G_MATCH_HEAD;
281 pspec->pattern[--pspec->pattern_length] = 0;
286 pspec->match_type = G_MATCH_EXACT;
291 /* now just need to distinguish between head or tail match start */
292 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
293 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
295 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
296 else /* seen_joker */
297 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
298 if (pspec->match_type == G_MATCH_ALL_TAIL)
299 instring_reverse (pspec->pattern_length, pspec->pattern);
304 g_pattern_spec_free (GPatternSpec *pspec)
306 g_return_if_fail (pspec != NULL);
308 g_free (pspec->pattern);
313 g_pattern_spec_equal (GPatternSpec *pspec1,
314 GPatternSpec *pspec2)
316 g_return_val_if_fail (pspec1 != NULL, FALSE);
317 g_return_val_if_fail (pspec2 != NULL, FALSE);
319 return (pspec1->pattern_length == pspec2->pattern_length &&
320 pspec1->match_type == pspec2->match_type &&
321 strcmp (pspec1->pattern, pspec2->pattern) == 0);
325 g_pattern_match_string (GPatternSpec *pspec,
328 g_return_val_if_fail (pspec != NULL, FALSE);
329 g_return_val_if_fail (string != NULL, FALSE);
331 return g_pattern_match (pspec, strlen (string), string, NULL);
335 g_pattern_match_simple (const gchar *pattern,
341 g_return_val_if_fail (pattern != NULL, FALSE);
342 g_return_val_if_fail (string != NULL, FALSE);
344 pspec = g_pattern_spec_new (pattern);
345 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
346 g_pattern_spec_free (pspec);