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, see <http://www.gnu.org/licenses/>.
25 #include "gmessages.h"
32 * @title: Glob-style pattern matching
33 * @short_description: matches strings against patterns containing '*'
34 * (wildcard) and '?' (joker)
36 * The g_pattern_match* functions match a string
37 * against a pattern containing '*' and '?' wildcards with similar
38 * semantics as the standard glob() function: '*' matches an arbitrary,
39 * possibly empty, string, '?' matches an arbitrary character.
41 * Note that in contrast to glob(), the '/' character can be matched by
42 * the wildcards, there are no '[...]' character ranges and '*' and '?'
43 * can not be escaped to include them literally in a pattern.
45 * When multiple strings must be matched against the same pattern, it
46 * is better to compile the pattern to a #GPatternSpec using
47 * g_pattern_spec_new() and use g_pattern_match_string() instead of
48 * g_pattern_match_simple(). This avoids the overhead of repeated
49 * pattern compilation.
55 * A GPatternSpec struct is the 'compiled' form of a pattern. This
56 * structure is opaque and its fields cannot be accessed directly.
59 /* keep enum and structure of gpattern.c and patterntest.c in sync */
62 G_MATCH_ALL, /* "*A?A*" */
63 G_MATCH_ALL_TAIL, /* "*A?AA" */
64 G_MATCH_HEAD, /* "AAAA*" */
65 G_MATCH_TAIL, /* "*AAAA" */
66 G_MATCH_EXACT, /* "AAAAA" */
72 GMatchType match_type;
80 /* --- functions --- */
81 static inline gboolean
82 g_pattern_ph_match (const gchar *match_pattern,
83 const gchar *match_string,
84 gboolean *wildcard_reached_p)
86 register const gchar *pattern, *string;
89 pattern = match_pattern;
90 string = match_string;
101 string = g_utf8_next_char (string);
105 *wildcard_reached_p = TRUE;
114 string = g_utf8_next_char (string);
117 while (ch == '*' || ch == '?');
122 gboolean next_wildcard_reached = FALSE;
123 while (ch != *string)
127 string = g_utf8_next_char (string);
130 if (g_pattern_ph_match (pattern, string, &next_wildcard_reached))
132 if (next_wildcard_reached)
133 /* the forthcoming pattern substring up to the next wildcard has
134 * been matched, but a mismatch occoured for the rest of the
135 * pattern, following the next wildcard.
136 * there's no need to advance the current match position any
137 * further if the rest pattern will not match.
161 * @pspec: a #GPatternSpec
162 * @string_length: the length of @string (in bytes, i.e. strlen(),
163 * not g_utf8_strlen())
164 * @string: the UTF-8 encoded string to match
165 * @string_reversed: (allow-none): the reverse of @string or %NULL
167 * Matches a string against a compiled pattern. Passing the correct
168 * length of the string given is mandatory. The reversed string can be
169 * omitted by passing %NULL, this is more efficient if the reversed
170 * version of the string to be matched is not at hand, as
171 * g_pattern_match() will only construct it if the compiled pattern
172 * requires reverse matches.
174 * Note that, if the user code will (possibly) match a string against a
175 * multitude of patterns containing wildcards, chances are high that
176 * some patterns will require a reversed string. In this case, it's
177 * more efficient to provide the reversed string to avoid multiple
178 * constructions thereof in the various calls to g_pattern_match().
180 * Note also that the reverse of a UTF-8 encoded string can in general
181 * not be obtained by g_strreverse(). This works only if the string
182 * does not contain any multibyte characters. GLib offers the
183 * g_utf8_strreverse() function to reverse UTF-8 encoded strings.
185 * Returns: %TRUE if @string matches @pspec
188 g_pattern_match (GPatternSpec *pspec,
191 const gchar *string_reversed)
193 g_return_val_if_fail (pspec != NULL, FALSE);
194 g_return_val_if_fail (string != NULL, FALSE);
196 if (string_length < pspec->min_length ||
197 string_length > pspec->max_length)
200 switch (pspec->match_type)
204 return g_pattern_ph_match (pspec->pattern, string, &dummy);
205 case G_MATCH_ALL_TAIL:
207 return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy);
212 tmp = g_utf8_strreverse (string, string_length);
213 result = g_pattern_ph_match (pspec->pattern, tmp, &dummy);
218 if (pspec->pattern_length == string_length)
219 return strcmp (pspec->pattern, string) == 0;
220 else if (pspec->pattern_length)
221 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
225 if (pspec->pattern_length)
226 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
230 if (pspec->pattern_length != string_length)
233 return strcmp (pspec->pattern, string) == 0;
235 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
241 * g_pattern_spec_new:
242 * @pattern: a zero-terminated UTF-8 encoded string
244 * Compiles a pattern to a #GPatternSpec.
246 * Returns: a newly-allocated #GPatternSpec
249 g_pattern_spec_new (const gchar *pattern)
252 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
253 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
254 gboolean follows_wildcard = FALSE;
255 guint pending_jokers = 0;
260 g_return_val_if_fail (pattern != NULL, NULL);
262 /* canonicalize pattern and collect necessary stats */
263 pspec = g_new (GPatternSpec, 1);
264 pspec->pattern_length = strlen (pattern);
265 pspec->min_length = 0;
266 pspec->max_length = 0;
267 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
269 for (i = 0, s = pattern; *s != 0; s++)
274 if (follows_wildcard) /* compress multiple wildcards */
276 pspec->pattern_length--;
279 follows_wildcard = TRUE;
287 pspec->max_length += 4; /* maximum UTF-8 character length */
290 for (; pending_jokers; pending_jokers--, i++) {
296 follows_wildcard = FALSE;
304 for (; pending_jokers; pending_jokers--) {
311 seen_joker = hj_pos >= 0;
312 seen_wildcard = hw_pos >= 0;
313 more_wildcards = seen_wildcard && hw_pos != tw_pos;
315 pspec->max_length = G_MAXUINT;
317 /* special case sole head/tail wildcard or exact matches */
318 if (!seen_joker && !more_wildcards)
320 if (pspec->pattern[0] == '*')
322 pspec->match_type = G_MATCH_TAIL;
323 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
324 pspec->pattern[pspec->pattern_length] = 0;
327 if (pspec->pattern_length > 0 &&
328 pspec->pattern[pspec->pattern_length - 1] == '*')
330 pspec->match_type = G_MATCH_HEAD;
331 pspec->pattern[--pspec->pattern_length] = 0;
336 pspec->match_type = G_MATCH_EXACT;
341 /* now just need to distinguish between head or tail match start */
342 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
343 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
345 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
346 else /* seen_joker */
347 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
348 if (pspec->match_type == G_MATCH_ALL_TAIL) {
349 gchar *tmp = pspec->pattern;
350 pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
357 * g_pattern_spec_free:
358 * @pspec: a #GPatternSpec
360 * Frees the memory allocated for the #GPatternSpec.
363 g_pattern_spec_free (GPatternSpec *pspec)
365 g_return_if_fail (pspec != NULL);
367 g_free (pspec->pattern);
372 * g_pattern_spec_equal:
373 * @pspec1: a #GPatternSpec
374 * @pspec2: another #GPatternSpec
376 * Compares two compiled pattern specs and returns whether they will
377 * match the same set of strings.
379 * Returns: Whether the compiled patterns are equal
382 g_pattern_spec_equal (GPatternSpec *pspec1,
383 GPatternSpec *pspec2)
385 g_return_val_if_fail (pspec1 != NULL, FALSE);
386 g_return_val_if_fail (pspec2 != NULL, FALSE);
388 return (pspec1->pattern_length == pspec2->pattern_length &&
389 pspec1->match_type == pspec2->match_type &&
390 strcmp (pspec1->pattern, pspec2->pattern) == 0);
394 * g_pattern_match_string:
395 * @pspec: a #GPatternSpec
396 * @string: the UTF-8 encoded string to match
398 * Matches a string against a compiled pattern. If the string is to be
399 * matched against more than one pattern, consider using
400 * g_pattern_match() instead while supplying the reversed string.
402 * Returns: %TRUE if @string matches @pspec
405 g_pattern_match_string (GPatternSpec *pspec,
408 g_return_val_if_fail (pspec != NULL, FALSE);
409 g_return_val_if_fail (string != NULL, FALSE);
411 return g_pattern_match (pspec, strlen (string), string, NULL);
415 * g_pattern_match_simple:
416 * @pattern: the UTF-8 encoded pattern
417 * @string: the UTF-8 encoded string to match
419 * Matches a string against a pattern given as a string. If this
420 * function is to be called in a loop, it's more efficient to compile
421 * the pattern once with g_pattern_spec_new() and call
422 * g_pattern_match_string() repeatedly.
424 * Returns: %TRUE if @string matches @pspec
427 g_pattern_match_simple (const gchar *pattern,
433 g_return_val_if_fail (pattern != NULL, FALSE);
434 g_return_val_if_fail (string != NULL, FALSE);
436 pspec = g_pattern_spec_new (pattern);
437 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
438 g_pattern_spec_free (pspec);