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.
27 #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;
54 /* --- functions --- */
55 static inline gboolean
56 g_pattern_ph_match (const gchar *match_pattern,
57 const gchar *match_string,
58 gboolean *wildcard_reached_p)
60 register const gchar *pattern, *string;
63 pattern = match_pattern;
64 string = match_string;
75 string = g_utf8_next_char (string);
79 *wildcard_reached_p = TRUE;
88 string = g_utf8_next_char (string);
91 while (ch == '*' || ch == '?');
96 gboolean next_wildcard_reached = FALSE;
101 string = g_utf8_next_char (string);
104 if (g_pattern_ph_match (pattern, string, &next_wildcard_reached))
106 if (next_wildcard_reached)
107 /* the forthcoming pattern substring up to the next wildcard has
108 * been matched, but a mismatch occoured for the rest of the
109 * pattern, following the next wildcard.
110 * there's no need to advance the current match position any
111 * further if the rest pattern will not match.
134 g_pattern_match (GPatternSpec *pspec,
137 const gchar *string_reversed)
139 g_return_val_if_fail (pspec != NULL, FALSE);
140 g_return_val_if_fail (string != NULL, FALSE);
142 if (string_length < pspec->min_length ||
143 string_length > pspec->max_length)
146 switch (pspec->match_type)
150 return g_pattern_ph_match (pspec->pattern, string, &dummy);
151 case G_MATCH_ALL_TAIL:
153 return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy);
158 tmp = g_utf8_strreverse (string, string_length);
159 result = g_pattern_ph_match (pspec->pattern, tmp, &dummy);
164 if (pspec->pattern_length == string_length)
165 return strcmp (pspec->pattern, string) == 0;
166 else if (pspec->pattern_length)
167 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
171 if (pspec->pattern_length)
172 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
176 if (pspec->pattern_length != string_length)
179 return strcmp (pspec->pattern, string) == 0;
181 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
187 g_pattern_spec_new (const gchar *pattern)
190 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
191 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
192 gboolean follows_wildcard = FALSE;
193 guint pending_jokers = 0;
198 g_return_val_if_fail (pattern != NULL, NULL);
200 /* canonicalize pattern and collect necessary stats */
201 pspec = g_new (GPatternSpec, 1);
202 pspec->pattern_length = strlen (pattern);
203 pspec->min_length = 0;
204 pspec->max_length = 0;
205 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
207 for (i = 0, s = pattern; *s != 0; s++)
212 if (follows_wildcard) /* compress multiple wildcards */
214 pspec->pattern_length--;
217 follows_wildcard = TRUE;
225 pspec->max_length += 4; /* maximum UTF-8 character length */
228 for (; pending_jokers; pending_jokers--, i++) {
234 follows_wildcard = FALSE;
242 for (; pending_jokers; pending_jokers--) {
249 seen_joker = hj_pos >= 0;
250 seen_wildcard = hw_pos >= 0;
251 more_wildcards = seen_wildcard && hw_pos != tw_pos;
253 pspec->max_length = G_MAXUINT;
255 /* special case sole head/tail wildcard or exact matches */
256 if (!seen_joker && !more_wildcards)
258 if (pspec->pattern[0] == '*')
260 pspec->match_type = G_MATCH_TAIL;
261 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
262 pspec->pattern[pspec->pattern_length] = 0;
265 if (pspec->pattern_length > 0 &&
266 pspec->pattern[pspec->pattern_length - 1] == '*')
268 pspec->match_type = G_MATCH_HEAD;
269 pspec->pattern[--pspec->pattern_length] = 0;
274 pspec->match_type = G_MATCH_EXACT;
279 /* now just need to distinguish between head or tail match start */
280 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
281 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
283 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
284 else /* seen_joker */
285 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
286 if (pspec->match_type == G_MATCH_ALL_TAIL) {
287 gchar *tmp = pspec->pattern;
288 pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
295 g_pattern_spec_free (GPatternSpec *pspec)
297 g_return_if_fail (pspec != NULL);
299 g_free (pspec->pattern);
304 g_pattern_spec_equal (GPatternSpec *pspec1,
305 GPatternSpec *pspec2)
307 g_return_val_if_fail (pspec1 != NULL, FALSE);
308 g_return_val_if_fail (pspec2 != NULL, FALSE);
310 return (pspec1->pattern_length == pspec2->pattern_length &&
311 pspec1->match_type == pspec2->match_type &&
312 strcmp (pspec1->pattern, pspec2->pattern) == 0);
316 g_pattern_match_string (GPatternSpec *pspec,
319 g_return_val_if_fail (pspec != NULL, FALSE);
320 g_return_val_if_fail (string != NULL, FALSE);
322 return g_pattern_match (pspec, strlen (string), string, NULL);
326 g_pattern_match_simple (const gchar *pattern,
332 g_return_val_if_fail (pattern != NULL, FALSE);
333 g_return_val_if_fail (string != NULL, FALSE);
335 pspec = g_pattern_spec_new (pattern);
336 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
337 g_pattern_spec_free (pspec);
342 #define __G_PATTERN_C__
343 #include "galiasdef.c"