Update contrib.
     1 /* GLIB - Library of useful routines for C programming
 
     2  * Copyright (C) 1995-1997, 1999  Peter Mattis, Red Hat, Inc.
 
     3  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
 
     5  * This library is free software; you can redistribute it and/or
 
     6  * modify it under the terms of the GNU Lesser General Public
 
     7  * License as published by the Free Software Foundation; either
 
     8  * version 2 of the License, or (at your option) any later version.
 
    10  * This library is distributed in the hope that it will be useful,
 
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
    13  * Lesser General Public License for more details.
 
    15  * You should have received a copy of the GNU Lesser General Public
 
    16  * License along with this library; if not, write to the
 
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
    18  * Boston, MA 02111-1307, USA.
 
    28 #include "gmessages.h"
 
    34 /* keep enum and structure of gpattern.c and patterntest.c in sync */
 
    37   G_MATCH_ALL,       /* "*A?A*" */
 
    38   G_MATCH_ALL_TAIL,  /* "*A?AA" */
 
    39   G_MATCH_HEAD,      /* "AAAA*" */
 
    40   G_MATCH_TAIL,      /* "*AAAA" */
 
    41   G_MATCH_EXACT,     /* "AAAAA" */
 
    47   GMatchType match_type;
 
    55 /* --- functions --- */
 
    56 static inline gboolean
 
    57 g_pattern_ph_match (const gchar *match_pattern,
 
    58 		    const gchar *match_string,
 
    59 		    gboolean    *wildcard_reached_p)
 
    61   register const gchar *pattern, *string;
 
    64   pattern = match_pattern;
 
    65   string = match_string;
 
    76 	  string = g_utf8_next_char (string);
 
    80 	  *wildcard_reached_p = TRUE;
 
    89 		  string = g_utf8_next_char (string);
 
    92 	  while (ch == '*' || ch == '?');
 
    97               gboolean next_wildcard_reached = FALSE;
 
   102 		  string = g_utf8_next_char (string);
 
   105 	      if (g_pattern_ph_match (pattern, string, &next_wildcard_reached))
 
   107               if (next_wildcard_reached)
 
   108                 /* the forthcoming pattern substring up to the next wildcard has
 
   109                  * been matched, but a mismatch occoured for the rest of the
 
   110                  * pattern, following the next wildcard.
 
   111                  * there's no need to advance the current match position any
 
   112                  * further if the rest pattern will not match.
 
   135 g_pattern_match (GPatternSpec *pspec,
 
   138 		 const gchar  *string_reversed)
 
   140   g_return_val_if_fail (pspec != NULL, FALSE);
 
   141   g_return_val_if_fail (string != NULL, FALSE);
 
   143   if (string_length < pspec->min_length ||
 
   144       string_length > pspec->max_length)
 
   147   switch (pspec->match_type)
 
   151       return g_pattern_ph_match (pspec->pattern, string, &dummy);
 
   152     case G_MATCH_ALL_TAIL:
 
   154 	return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy);
 
   159 	  tmp = g_utf8_strreverse (string, string_length);
 
   160 	  result = g_pattern_ph_match (pspec->pattern, tmp, &dummy);
 
   165       if (pspec->pattern_length == string_length)
 
   166 	return strcmp (pspec->pattern, string) == 0;
 
   167       else if (pspec->pattern_length)
 
   168 	return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
 
   172       if (pspec->pattern_length)
 
   173         return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
 
   177       if (pspec->pattern_length != string_length)
 
   180         return strcmp (pspec->pattern, string) == 0;
 
   182       g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
 
   187 EXPORT_C GPatternSpec*
 
   188 g_pattern_spec_new (const gchar *pattern)
 
   191   gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
 
   192   gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
 
   193   gboolean follows_wildcard = FALSE;
 
   194   guint pending_jokers = 0;
 
   199   g_return_val_if_fail (pattern != NULL, NULL);
 
   201   /* canonicalize pattern and collect necessary stats */
 
   202   pspec = g_new (GPatternSpec, 1);
 
   203   pspec->pattern_length = strlen (pattern);
 
   204   pspec->min_length = 0;
 
   205   pspec->max_length = 0;
 
   206   pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
 
   208   for (i = 0, s = pattern; *s != 0; s++)
 
   213 	  if (follows_wildcard)	/* compress multiple wildcards */
 
   215 	      pspec->pattern_length--;
 
   218 	  follows_wildcard = TRUE;
 
   226 	  pspec->max_length += 4; /* maximum UTF-8 character length */
 
   229 	  for (; pending_jokers; pending_jokers--, i++) {
 
   235 	  follows_wildcard = FALSE;
 
   243   for (; pending_jokers; pending_jokers--) {
 
   250   seen_joker = hj_pos >= 0;
 
   251   seen_wildcard = hw_pos >= 0;
 
   252   more_wildcards = seen_wildcard && hw_pos != tw_pos;
 
   254     pspec->max_length = G_MAXUINT;
 
   256   /* special case sole head/tail wildcard or exact matches */
 
   257   if (!seen_joker && !more_wildcards)
 
   259       if (pspec->pattern[0] == '*')
 
   261 	  pspec->match_type = G_MATCH_TAIL;
 
   262           memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
 
   263 	  pspec->pattern[pspec->pattern_length] = 0;
 
   266       if (pspec->pattern_length > 0 &&
 
   267 	  pspec->pattern[pspec->pattern_length - 1] == '*')
 
   269 	  pspec->match_type = G_MATCH_HEAD;
 
   270 	  pspec->pattern[--pspec->pattern_length] = 0;
 
   275 	  pspec->match_type = G_MATCH_EXACT;
 
   280   /* now just need to distinguish between head or tail match start */
 
   281   tw_pos = pspec->pattern_length - 1 - tw_pos;	/* last pos to tail distance */
 
   282   tj_pos = pspec->pattern_length - 1 - tj_pos;	/* last pos to tail distance */
 
   284     pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
 
   285   else /* seen_joker */
 
   286     pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
 
   287   if (pspec->match_type == G_MATCH_ALL_TAIL) {
 
   288     gchar *tmp = pspec->pattern;
 
   289     pspec->pattern = g_utf8_strreverse (pspec->pattern, pspec->pattern_length);
 
   296 g_pattern_spec_free (GPatternSpec *pspec)
 
   298   g_return_if_fail (pspec != NULL);
 
   300   g_free (pspec->pattern);
 
   305 g_pattern_spec_equal (GPatternSpec *pspec1,
 
   306 		      GPatternSpec *pspec2)
 
   308   g_return_val_if_fail (pspec1 != NULL, FALSE);
 
   309   g_return_val_if_fail (pspec2 != NULL, FALSE);
 
   311   return (pspec1->pattern_length == pspec2->pattern_length &&
 
   312 	  pspec1->match_type == pspec2->match_type &&
 
   313 	  strcmp (pspec1->pattern, pspec2->pattern) == 0);
 
   317 g_pattern_match_string (GPatternSpec *pspec,
 
   320   g_return_val_if_fail (pspec != NULL, FALSE);
 
   321   g_return_val_if_fail (string != NULL, FALSE);
 
   323   return g_pattern_match (pspec, strlen (string), string, NULL);
 
   327 g_pattern_match_simple (const gchar *pattern,
 
   333   g_return_val_if_fail (pattern != NULL, FALSE);
 
   334   g_return_val_if_fail (string != NULL, FALSE);
 
   336   pspec = g_pattern_spec_new (pattern);
 
   337   ergo = g_pattern_match (pspec, strlen (string), string, NULL);
 
   338   g_pattern_spec_free (pspec);
 
   343 #define __G_PATTERN_C__
 
   344 #include "galiasdef.c"