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 <function>g_pattern_match*</function> 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
42 * <emphasis>can</emphasis> be matched by the wildcards, there are no
43 * '[...]' character ranges and '*' and '?' can
44 * <emphasis>not</emphasis> be escaped to include them literally in a
47 * When multiple strings must be matched against the same pattern, it
48 * is better to compile the pattern to a #GPatternSpec using
49 * g_pattern_spec_new() and use g_pattern_match_string() instead of
50 * g_pattern_match_simple(). This avoids the overhead of repeated
51 * pattern compilation.
57 * A #GPatternSpec-struct is the 'compiled' form of a pattern. This
58 * structure is opaque and its fields cannot be accessed directly.
61 /* keep enum and structure of gpattern.c and patterntest.c in sync */
64 G_MATCH_ALL, /* "*A?A*" */
65 G_MATCH_ALL_TAIL, /* "*A?AA" */
66 G_MATCH_HEAD, /* "AAAA*" */
67 G_MATCH_TAIL, /* "*AAAA" */
68 G_MATCH_EXACT, /* "AAAAA" */
74 GMatchType match_type;
82 /* --- functions --- */
83 static inline gboolean
84 g_pattern_ph_match (const gchar *match_pattern,
85 const gchar *match_string,
86 gboolean *wildcard_reached_p)
88 register const gchar *pattern, *string;
91 pattern = match_pattern;
92 string = match_string;
103 string = g_utf8_next_char (string);
107 *wildcard_reached_p = TRUE;
116 string = g_utf8_next_char (string);
119 while (ch == '*' || ch == '?');
124 gboolean next_wildcard_reached = FALSE;
125 while (ch != *string)
129 string = g_utf8_next_char (string);
132 if (g_pattern_ph_match (pattern, string, &next_wildcard_reached))
134 if (next_wildcard_reached)
135 /* the forthcoming pattern substring up to the next wildcard has
136 * been matched, but a mismatch occoured for the rest of the
137 * pattern, following the next wildcard.
138 * there's no need to advance the current match position any
139 * further if the rest pattern will not match.
163 * @pspec: a #GPatternSpec
164 * @string_length: the length of @string (in bytes, i.e. strlen(),
165 * <emphasis>not</emphasis> g_utf8_strlen())
166 * @string: the UTF-8 encoded string to match
167 * @string_reversed: (allow-none): the reverse of @string or %NULL
169 * Matches a string against a compiled pattern. Passing the correct
170 * length of the string given is mandatory. The reversed string can be
171 * omitted by passing %NULL, this is more efficient if the reversed
172 * version of the string to be matched is not at hand, as
173 * g_pattern_match() will only construct it if the compiled pattern
174 * requires reverse matches.
176 * Note that, if the user code will (possibly) match a string against a
177 * multitude of patterns containing wildcards, chances are high that
178 * some patterns will require a reversed string. In this case, it's
179 * more efficient to provide the reversed string to avoid multiple
180 * constructions thereof in the various calls to g_pattern_match().
182 * Note also that the reverse of a UTF-8 encoded string can in general
183 * <emphasis>not</emphasis> be obtained by g_strreverse(). This works
184 * only if the string doesn't contain any multibyte characters. GLib
185 * offers the g_utf8_strreverse() function to reverse UTF-8 encoded
188 * Returns: %TRUE if @string matches @pspec
191 g_pattern_match (GPatternSpec *pspec,
194 const gchar *string_reversed)
196 g_return_val_if_fail (pspec != NULL, FALSE);
197 g_return_val_if_fail (string != NULL, FALSE);
199 if (string_length < pspec->min_length ||
200 string_length > pspec->max_length)
203 switch (pspec->match_type)
207 return g_pattern_ph_match (pspec->pattern, string, &dummy);
208 case G_MATCH_ALL_TAIL:
210 return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy);
215 tmp = g_utf8_strreverse (string, string_length);
216 result = g_pattern_ph_match (pspec->pattern, tmp, &dummy);
221 if (pspec->pattern_length == string_length)
222 return strcmp (pspec->pattern, string) == 0;
223 else if (pspec->pattern_length)
224 return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
228 if (pspec->pattern_length)
229 return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
233 if (pspec->pattern_length != string_length)
236 return strcmp (pspec->pattern, string) == 0;
238 g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
244 * g_pattern_spec_new:
245 * @pattern: a zero-terminated UTF-8 encoded string
247 * Compiles a pattern to a #GPatternSpec.
249 * Returns: a newly-allocated #GPatternSpec
252 g_pattern_spec_new (const gchar *pattern)
255 gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
256 gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
257 gboolean follows_wildcard = FALSE;
258 guint pending_jokers = 0;
263 g_return_val_if_fail (pattern != NULL, NULL);
265 /* canonicalize pattern and collect necessary stats */
266 pspec = g_new (GPatternSpec, 1);
267 pspec->pattern_length = strlen (pattern);
268 pspec->min_length = 0;
269 pspec->max_length = 0;
270 pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
272 for (i = 0, s = pattern; *s != 0; s++)
277 if (follows_wildcard) /* compress multiple wildcards */
279 pspec->pattern_length--;
282 follows_wildcard = TRUE;
290 pspec->max_length += 4; /* maximum UTF-8 character length */
293 for (; pending_jokers; pending_jokers--, i++) {
299 follows_wildcard = FALSE;
307 for (; pending_jokers; pending_jokers--) {
314 seen_joker = hj_pos >= 0;
315 seen_wildcard = hw_pos >= 0;
316 more_wildcards = seen_wildcard && hw_pos != tw_pos;
318 pspec->max_length = G_MAXUINT;
320 /* special case sole head/tail wildcard or exact matches */
321 if (!seen_joker && !more_wildcards)
323 if (pspec->pattern[0] == '*')
325 pspec->match_type = G_MATCH_TAIL;
326 memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
327 pspec->pattern[pspec->pattern_length] = 0;
330 if (pspec->pattern_length > 0 &&
331 pspec->pattern[pspec->pattern_length - 1] == '*')
333 pspec->match_type = G_MATCH_HEAD;
334 pspec->pattern[--pspec->pattern_length] = 0;
339 pspec->match_type = G_MATCH_EXACT;
344 /* now just need to distinguish between head or tail match start */
345 tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
346 tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
348 pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
349 else /* seen_joker */
350 pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
351 if (pspec->match_type == G_MATCH_ALL_TAIL) {
352 gchar *tmp = pspec->pattern;
353 pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
360 * g_pattern_spec_free:
361 * @pspec: a #GPatternSpec
363 * Frees the memory allocated for the #GPatternSpec.
366 g_pattern_spec_free (GPatternSpec *pspec)
368 g_return_if_fail (pspec != NULL);
370 g_free (pspec->pattern);
375 * g_pattern_spec_equal:
376 * @pspec1: a #GPatternSpec
377 * @pspec2: another #GPatternSpec
379 * Compares two compiled pattern specs and returns whether they will
380 * match the same set of strings.
382 * Returns: Whether the compiled patterns are equal
385 g_pattern_spec_equal (GPatternSpec *pspec1,
386 GPatternSpec *pspec2)
388 g_return_val_if_fail (pspec1 != NULL, FALSE);
389 g_return_val_if_fail (pspec2 != NULL, FALSE);
391 return (pspec1->pattern_length == pspec2->pattern_length &&
392 pspec1->match_type == pspec2->match_type &&
393 strcmp (pspec1->pattern, pspec2->pattern) == 0);
397 * g_pattern_match_string:
398 * @pspec: a #GPatternSpec
399 * @string: the UTF-8 encoded string to match
401 * Matches a string against a compiled pattern. If the string is to be
402 * matched against more than one pattern, consider using
403 * g_pattern_match() instead while supplying the reversed string.
405 * Returns: %TRUE if @string matches @pspec
408 g_pattern_match_string (GPatternSpec *pspec,
411 g_return_val_if_fail (pspec != NULL, FALSE);
412 g_return_val_if_fail (string != NULL, FALSE);
414 return g_pattern_match (pspec, strlen (string), string, NULL);
418 * g_pattern_match_simple:
419 * @pattern: the UTF-8 encoded pattern
420 * @string: the UTF-8 encoded string to match
422 * Matches a string against a pattern given as a string. If this
423 * function is to be called in a loop, it's more efficient to compile
424 * the pattern once with g_pattern_spec_new() and call
425 * g_pattern_match_string() repeatedly.
427 * Returns: %TRUE if @string matches @pspec
430 g_pattern_match_simple (const gchar *pattern,
436 g_return_val_if_fail (pattern != NULL, FALSE);
437 g_return_val_if_fail (string != NULL, FALSE);
439 pspec = g_pattern_spec_new (pattern);
440 ergo = g_pattern_match (pspec, strlen (string), string, NULL);
441 g_pattern_spec_free (pspec);