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 --- */
50 static inline gboolean
51 g_pattern_ph_match (const gchar *match_pattern,
52 const gchar *match_string)
54 register const gchar *pattern, *string;
57 pattern = match_pattern;
58 string = match_string;
69 string = g_utf8_next_char (string);
81 string = g_utf8_next_char (string);
84 while (ch == '*' || ch == '?');
93 string = g_utf8_next_char (string);
96 if (g_pattern_ph_match (pattern, string))
118 g_pattern_match (GPatternSpec *pspec,
121 const gchar *string_reversed)
123 g_return_val_if_fail (pspec != NULL, FALSE);
124 g_return_val_if_fail (string != NULL, FALSE);
126 if (pspec->min_length > string_length)
129 switch (pspec->match_type)
132 return g_pattern_ph_match (pspec->pattern, string);
133 case G_MATCH_ALL_TAIL:
135 return g_pattern_ph_match (pspec->pattern, string_reversed);
140 tmp = g_utf8_strreverse (string, string_length);
141 result = g_pattern_ph_match (pspec->pattern, tmp);
146 if (pspec->pattern_length == string_length)
147 return strcmp (pspec->pattern, string) == 0;
148 else if (pspec->pattern_length)
149 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
153 if (pspec->pattern_length)
154 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
158 if (pspec->pattern_length != string_length)
161 return strcmp (pspec->pattern, string) == 0;
163 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
169 g_pattern_spec_new (const gchar *pattern)
172 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
173 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
174 gboolean follows_wildcard = FALSE;
175 guint pending_jokers = 0;
180 g_return_val_if_fail (pattern != NULL, NULL);
182 /* canonicalize pattern and collect necessary stats */
183 pspec = g_new (GPatternSpec, 1);
184 pspec->pattern_length = strlen (pattern);
185 pspec->min_length = 0;
186 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
188 for (i = 0, s = pattern; *s != 0; s++)
193 if (follows_wildcard) /* compress multiple wildcards */
195 pspec->pattern_length--;
198 follows_wildcard = TRUE;
208 for (; pending_jokers; pending_jokers--, i++) {
214 follows_wildcard = FALSE;
221 for (; pending_jokers; pending_jokers--) {
228 seen_joker = hj_pos >= 0;
229 seen_wildcard = hw_pos >= 0;
230 more_wildcards = seen_wildcard && hw_pos != tw_pos;
232 /* special case sole head/tail wildcard or exact matches */
233 if (!seen_joker && !more_wildcards)
235 if (pspec->pattern[0] == '*')
237 pspec->match_type = G_MATCH_TAIL;
238 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
239 pspec->pattern[pspec->pattern_length] = 0;
242 if (pspec->pattern[pspec->pattern_length - 1] == '*')
244 pspec->match_type = G_MATCH_HEAD;
245 pspec->pattern[--pspec->pattern_length] = 0;
250 pspec->match_type = G_MATCH_EXACT;
255 /* now just need to distinguish between head or tail match start */
256 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
257 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
259 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
260 else /* seen_joker */
261 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
262 if (pspec->match_type == G_MATCH_ALL_TAIL) {
263 gchar *tmp = pspec->pattern;
264 pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
271 g_pattern_spec_free (GPatternSpec *pspec)
273 g_return_if_fail (pspec != NULL);
275 g_free (pspec->pattern);
280 g_pattern_spec_equal (GPatternSpec *pspec1,
281 GPatternSpec *pspec2)
283 g_return_val_if_fail (pspec1 != NULL, FALSE);
284 g_return_val_if_fail (pspec2 != NULL, FALSE);
286 return (pspec1->pattern_length == pspec2->pattern_length &&
287 pspec1->match_type == pspec2->match_type &&
288 strcmp (pspec1->pattern, pspec2->pattern) == 0);
292 g_pattern_match_string (GPatternSpec *pspec,
295 g_return_val_if_fail (pspec != NULL, FALSE);
296 g_return_val_if_fail (string != NULL, FALSE);
298 return g_pattern_match (pspec, strlen (string), string, NULL);
302 g_pattern_match_simple (const gchar *pattern,
308 g_return_val_if_fail (pattern != NULL, FALSE);
309 g_return_val_if_fail (string != NULL, FALSE);
311 pspec = g_pattern_spec_new (pattern);
312 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
313 g_pattern_spec_free (pspec);