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 */
28 /* --- functions --- */
29 static inline gboolean
30 g_pattern_ph_match (const gchar *match_pattern,
31 const gchar *match_string)
33 register const gchar *pattern, *string;
36 pattern = match_pattern;
37 string = match_string;
63 while (ch == '*' || ch == '?');
75 if (g_pattern_ph_match (pattern, string))
97 g_pattern_match (GPatternSpec *pspec,
100 const gchar *string_reversed)
102 g_return_val_if_fail (pspec != NULL, FALSE);
103 g_return_val_if_fail (string != NULL, FALSE);
104 g_return_val_if_fail (string_reversed != NULL, FALSE);
106 switch (pspec->match_type)
109 return g_pattern_ph_match (pspec->pattern, string);
111 case G_MATCH_ALL_TAIL:
112 return g_pattern_ph_match (pspec->pattern_reversed, string_reversed);
115 if (pspec->pattern_length > string_length)
117 else if (pspec->pattern_length == string_length)
118 return strcmp (pspec->pattern, string) == 0;
119 else if (pspec->pattern_length)
120 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
125 if (pspec->pattern_length > string_length)
127 else if (pspec->pattern_length == string_length)
128 return strcmp (pspec->pattern_reversed, string_reversed) == 0;
129 else if (pspec->pattern_length)
130 return strncmp (pspec->pattern_reversed,
132 pspec->pattern_length) == 0;
137 if (pspec->pattern_length != string_length)
140 return strcmp (pspec->pattern_reversed, string_reversed) == 0;
143 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
149 g_pattern_spec_new (const gchar *pattern)
154 guint hw = 0, tw = 0, hj = 0, tj = 0;
156 g_return_val_if_fail (pattern != NULL, NULL);
158 pspec = g_new (GPatternSpec, 1);
159 pspec->pattern_length = strlen (pattern);
160 pspec->pattern = strcpy (g_new (gchar, pspec->pattern_length + 1), pattern);
161 pspec->pattern_reversed = g_new (gchar, pspec->pattern_length + 1);
162 t = pspec->pattern_reversed + pspec->pattern_length;
165 while (t >= pspec->pattern_reversed)
167 register gchar c = *(h++);
186 pspec->match_type = hw > tw || (hw == tw && hj > tj) ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
191 if (hw == 0 && tw == 0)
193 pspec->match_type = G_MATCH_EXACT;
202 if (p > pspec->pattern && !strchr (p, '*'))
206 pspec->match_type = G_MATCH_TAIL;
207 pspec->pattern_length = strlen (p);
208 tmp = pspec->pattern;
209 pspec->pattern = strcpy (g_new (gchar, pspec->pattern_length + 1), p);
211 g_free (pspec->pattern_reversed);
212 pspec->pattern_reversed = g_new (gchar, pspec->pattern_length + 1);
213 t = pspec->pattern_reversed + pspec->pattern_length;
216 while (t >= pspec->pattern_reversed)
224 p = pspec->pattern_reversed;
227 if (p > pspec->pattern_reversed && !strchr (p, '*'))
231 pspec->match_type = G_MATCH_HEAD;
232 pspec->pattern_length = strlen (p);
233 tmp = pspec->pattern_reversed;
234 pspec->pattern_reversed = strcpy (g_new (gchar, pspec->pattern_length + 1), p);
236 g_free (pspec->pattern);
237 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
238 t = pspec->pattern + pspec->pattern_length;
240 h = pspec->pattern_reversed;
241 while (t >= pspec->pattern)
250 g_pattern_match_string (GPatternSpec *pspec,
253 gchar *string_reversed, *t;
258 g_return_val_if_fail (pspec != NULL, FALSE);
259 g_return_val_if_fail (string != NULL, FALSE);
261 length = strlen (string);
262 string_reversed = g_new (gchar, length + 1);
263 t = string_reversed + length;
266 while (t >= string_reversed)
269 ergo = g_pattern_match (pspec, length, string, string_reversed);
270 g_free (string_reversed);
276 g_pattern_match_simple (const gchar *pattern,
282 g_return_val_if_fail (pattern != NULL, FALSE);
283 g_return_val_if_fail (string != NULL, FALSE);
285 pspec = g_pattern_spec_new (pattern);
286 ergo = g_pattern_match_string (pspec, string);
287 g_pattern_spec_free (pspec);
293 g_pattern_spec_free (GPatternSpec *pspec)
295 g_return_if_fail (pspec != NULL);
297 g_free (pspec->pattern);
298 g_free (pspec->pattern_reversed);