/** * g_pattern_match: * @pspec: a #GPatternSpec * @string_length: the length of @string (in bytes, i.e. strlen(), * <emphasis>not</emphasis> g_utf8_strlen()) * @string: the UTF-8 encoded string to match * @string_reversed: (allow-none): the reverse of @string or %NULL * @Returns: %TRUE if @string matches @pspec * * Matches a string against a compiled pattern. Passing the correct * length of the string given is mandatory. The reversed string can be * omitted by passing %NULL, this is more efficient if the reversed * version of the string to be matched is not at hand, as * g_pattern_match() will only construct it if the compiled pattern * requires reverse matches. * * Note that, if the user code will (possibly) match a string against a * multitude of patterns containing wildcards, chances are high that * some patterns will require a reversed string. In this case, it's * more efficient to provide the reversed string to avoid multiple * constructions thereof in the various calls to g_pattern_match(). * * Note also that the reverse of a UTF-8 encoded string can in general * <emphasis>not</emphasis> be obtained by g_strreverse(). This works * only if the string doesn't contain any multibyte characters. GLib * offers the g_utf8_strreverse() function to reverse UTF-8 encoded * strings. **/ gboolean g_pattern_match (GPatternSpec *pspec, guint string_length, const gchar *string, const gchar *string_reversed) { g_return_val_if_fail (pspec != NULL, FALSE); g_return_val_if_fail (string != NULL, FALSE); if (string_length < pspec->min_length || string_length > pspec->max_length) return FALSE; switch (pspec->match_type) { gboolean dummy; case G_MATCH_ALL: return g_pattern_ph_match (pspec->pattern, string, &dummy); case G_MATCH_ALL_TAIL: if (string_reversed) return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy); else { gboolean result; gchar *tmp; tmp = g_utf8_strreverse (string, string_length); result = g_pattern_ph_match (pspec->pattern, tmp, &dummy); g_free (tmp); return result; } case G_MATCH_HEAD: if (pspec->pattern_length == string_length) return strcmp (pspec->pattern, string) == 0; else if (pspec->pattern_length) return strncmp (pspec->pattern, string, pspec->pattern_length) == 0; else return TRUE; case G_MATCH_TAIL: if (pspec->pattern_length) return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0; else return TRUE; case G_MATCH_EXACT: if (pspec->pattern_length != string_length) return FALSE; else return strcmp (pspec->pattern, string) == 0; default: g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE); return FALSE; } }
gboolean g_pattern_match (GPatternSpec *pspec, guint string_length, const gchar *string, const gchar *string_reversed) { g_return_val_if_fail (pspec != NULL, FALSE); g_return_val_if_fail (string != NULL, FALSE); g_return_val_if_fail (string_reversed != NULL, FALSE); switch (pspec->match_type) { case G_MATCH_ALL: return g_pattern_ph_match (pspec->pattern, string); case G_MATCH_ALL_TAIL: return g_pattern_ph_match (pspec->pattern_reversed, string_reversed); case G_MATCH_HEAD: if (pspec->pattern_length > string_length) return FALSE; else if (pspec->pattern_length == string_length) return strcmp (pspec->pattern, string) == 0; else if (pspec->pattern_length) return strncmp (pspec->pattern, string, pspec->pattern_length) == 0; else return TRUE; case G_MATCH_TAIL: if (pspec->pattern_length > string_length) return FALSE; else if (pspec->pattern_length == string_length) return strcmp (pspec->pattern_reversed, string_reversed) == 0; else if (pspec->pattern_length) return strncmp (pspec->pattern_reversed, string_reversed, pspec->pattern_length) == 0; else return TRUE; case G_MATCH_EXACT: if (pspec->pattern_length != string_length) return FALSE; else return strcmp (pspec->pattern_reversed, string_reversed) == 0; default: g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE); return FALSE; } }
/* --- functions --- */ static inline gboolean g_pattern_ph_match (const gchar *match_pattern, const gchar *match_string, gboolean *wildcard_reached_p) { register const gchar *pattern, *string; register gchar ch; pattern = match_pattern; string = match_string; ch = *pattern; pattern++; while (ch) { switch (ch) { case '?': if (!*string) return FALSE; string = g_utf8_next_char (string); break; case '*': *wildcard_reached_p = TRUE; do { ch = *pattern; pattern++; if (ch == '?') { if (!*string) return FALSE; string = g_utf8_next_char (string); } } while (ch == '*' || ch == '?'); if (!ch) return TRUE; do { gboolean next_wildcard_reached = FALSE; while (ch != *string) { if (!*string) return FALSE; string = g_utf8_next_char (string); } string++; if (g_pattern_ph_match (pattern, string, &next_wildcard_reached)) return TRUE; if (next_wildcard_reached) /* the forthcoming pattern substring up to the next wildcard has * been matched, but a mismatch occoured for the rest of the * pattern, following the next wildcard. * there's no need to advance the current match position any * further if the rest pattern will not match. */ return FALSE; } while (*string); break; default: if (ch == *string) string++; else return FALSE; break; } ch = *pattern; pattern++; } return *string == 0; }
static inline gboolean g_pattern_ph_match (const gchar *match_pattern, const gchar *match_string) { register const gchar *pattern, *string; register gchar ch; pattern = match_pattern; string = match_string; ch = *pattern; pattern++; while (ch) { switch (ch) { case '?': if (!*string) return FALSE; string = g_utf8_next_char (string); break; case '*': do { ch = *pattern; pattern++; if (ch == '?') { if (!*string) return FALSE; string = g_utf8_next_char (string); } } while (ch == '*' || ch == '?'); if (!ch) return TRUE; do { while (ch != *string) { if (!*string) return FALSE; string = g_utf8_next_char (string); } string++; if (g_pattern_ph_match (pattern, string)) return TRUE; } while (*string); break; default: if (ch == *string) string++; else return FALSE; break; } ch = *pattern; pattern++; } return *string == 0; }