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"
27 /* --- functions --- */
28 static inline gboolean
29 g_pattern_ph_match (const gchar *match_pattern,
30 const gchar *match_string)
32 register const gchar *pattern, *string;
35 pattern = match_pattern;
36 string = match_string;
62 while (ch == '*' || ch == '?');
74 if (g_pattern_ph_match (pattern, string))
96 g_pattern_match (GPatternSpec *pspec,
99 const gchar *string_reversed)
101 g_return_val_if_fail (pspec != NULL, FALSE);
102 g_return_val_if_fail (string != NULL, FALSE);
103 g_return_val_if_fail (string_reversed != NULL, FALSE);
105 switch (pspec->match_type)
108 return g_pattern_ph_match (pspec->pattern, string);
110 case G_MATCH_ALL_TAIL:
111 return g_pattern_ph_match (pspec->pattern_reversed, string_reversed);
114 if (pspec->pattern_length > string_length)
116 else if (pspec->pattern_length == string_length)
117 return strcmp (pspec->pattern, string) == 0;
118 else if (pspec->pattern_length)
119 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
124 if (pspec->pattern_length > string_length)
126 else if (pspec->pattern_length == string_length)
127 return strcmp (pspec->pattern_reversed, string_reversed) == 0;
128 else if (pspec->pattern_length)
129 return strncmp (pspec->pattern_reversed,
131 pspec->pattern_length) == 0;
136 if (pspec->pattern_length != string_length)
139 return strcmp (pspec->pattern_reversed, string_reversed) == 0;
142 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
148 g_pattern_spec_new (const gchar *pattern)
153 guint hw = 0, tw = 0, hj = 0, tj = 0;
155 g_return_val_if_fail (pattern != NULL, NULL);
157 pspec = g_new (GPatternSpec, 1);
158 pspec->pattern_length = strlen (pattern);
159 pspec->pattern = strcpy (g_new (gchar, pspec->pattern_length + 1), pattern);
160 pspec->pattern_reversed = g_new (gchar, pspec->pattern_length + 1);
161 t = pspec->pattern_reversed + pspec->pattern_length;
164 while (t >= pspec->pattern_reversed)
166 register gchar c = *(h++);
185 pspec->match_type = hw > tw || (hw == tw && hj > tj) ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
190 if (hw == 0 && tw == 0)
192 pspec->match_type = G_MATCH_EXACT;
201 if (p > pspec->pattern && !strchr (p, '*'))
205 pspec->match_type = G_MATCH_TAIL;
206 pspec->pattern_length = strlen (p);
207 tmp = pspec->pattern;
208 pspec->pattern = strcpy (g_new (gchar, pspec->pattern_length + 1), p);
210 g_free (pspec->pattern_reversed);
211 pspec->pattern_reversed = g_new (gchar, pspec->pattern_length + 1);
212 t = pspec->pattern_reversed + pspec->pattern_length;
215 while (t >= pspec->pattern_reversed)
223 p = pspec->pattern_reversed;
226 if (p > pspec->pattern_reversed && !strchr (p, '*'))
230 pspec->match_type = G_MATCH_HEAD;
231 pspec->pattern_length = strlen (p);
232 tmp = pspec->pattern_reversed;
233 pspec->pattern_reversed = strcpy (g_new (gchar, pspec->pattern_length + 1), p);
235 g_free (pspec->pattern);
236 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
237 t = pspec->pattern + pspec->pattern_length;
239 h = pspec->pattern_reversed;
240 while (t >= pspec->pattern)
249 g_pattern_match_string (GPatternSpec *pspec,
252 gchar *string_reversed, *t;
257 g_return_val_if_fail (pspec != NULL, FALSE);
258 g_return_val_if_fail (string != NULL, FALSE);
260 length = strlen (string);
261 string_reversed = g_new (gchar, length + 1);
262 t = string_reversed + length;
265 while (t >= string_reversed)
268 ergo = g_pattern_match (pspec, length, string, string_reversed);
269 g_free (string_reversed);
275 g_pattern_match_simple (const gchar *pattern,
281 g_return_val_if_fail (pattern != NULL, FALSE);
282 g_return_val_if_fail (string != NULL, FALSE);
284 pspec = g_pattern_spec_new (pattern);
285 ergo = g_pattern_match_string (pspec, string);
286 g_pattern_spec_free (pspec);
292 g_pattern_spec_free (GPatternSpec *pspec)
294 g_return_if_fail (pspec != NULL);
296 g_free (pspec->pattern);
297 g_free (pspec->pattern_reversed);