Fixed egg_regex_fetch_named(); added _egg_regex_get_backrefmax()

master
Yevgen Muntyan 2007-02-15 07:32:06 -06:00
parent 7be3b2d717
commit 0f31729b4c
2 changed files with 62 additions and 7 deletions

View File

@ -1112,19 +1112,47 @@ egg_regex_fetch_named (const EggRegex *regex,
const gchar *name, const gchar *name,
const gchar *string) const gchar *string)
{ {
/* we cannot use pcre_get_named_substring() because it allocates the gint ret;
* string using pcre_malloc(). */ gchar buf[1024];
gint num; gchar *substring;
gchar *substring_copy;
g_return_val_if_fail (regex != NULL, NULL); g_return_val_if_fail (regex != NULL, NULL);
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (name != NULL, NULL); g_return_val_if_fail (name != NULL, NULL);
num = egg_regex_get_string_number (regex, name); if (regex->match == NULL)
if (num == -1)
return NULL; return NULL;
else
return egg_regex_fetch (regex, num, string); if (regex->match->string_len < 0)
return NULL;
ret = pcre_copy_named_substring (regex->pattern->pcre_re,
string,
regex->match->offsets,
regex->match->matches,
name,
buf,
sizeof buf);
if (ret == PCRE_ERROR_NOSUBSTRING)
return NULL;
/* buf is too small, ask pcre for malloc'ed substring */
ret = pcre_get_named_substring (regex->pattern->pcre_re,
string,
regex->match->offsets,
regex->match->matches,
name,
&substring);
if (ret < 0)
/* not enough memory or something */
return NULL;
substring_copy = g_strndup (substring, ret);
pcre_free (substring);
return substring_copy;
} }
/** /**
@ -2600,3 +2628,28 @@ egg_regex_eval_replacement (EggRegex *regex,
return g_string_free (result, FALSE); return g_string_free (result, FALSE);
} }
/**
* egg_regex_get_backrefmax:
* @regex: #EggRegex.
*
* A wrapper around pcre_fullinfo(..., PCRE_INFO_BACKREFMAX).
*
* Not stock eggregex from Marco.
*
* Returns: The number of the highest back reference in the @regex's
* pattern. Zero is returned if there are no back references.
*/
gint
egg_regex_get_backrefmax (EggRegex *regex)
{
gint ret;
g_return_val_if_fail (regex != NULL, 0);
pcre_fullinfo (regex->pattern->pcre_re,
REGEX_GET_EXTRA (regex),
PCRE_INFO_BACKREFMAX,
&ret);
return ret;
}

View File

@ -65,6 +65,7 @@ G_BEGIN_DECLS
#define egg_regex_check_replacement _moo_egg_regex_check_replacement #define egg_regex_check_replacement _moo_egg_regex_check_replacement
#define egg_regex_eval_replacement _moo_egg_regex_eval_replacement #define egg_regex_eval_replacement _moo_egg_regex_eval_replacement
#define egg_regex_try_eval_replacement _moo_egg_regex_try_eval_replacement #define egg_regex_try_eval_replacement _moo_egg_regex_try_eval_replacement
#define egg_regex_get_backrefmax _moo_egg_regex_get_backrefmax
#define _egg_regex_get_memory _moo_egg_regex_get_memory #define _egg_regex_get_memory _moo_egg_regex_get_memory
@ -247,6 +248,7 @@ char *egg_regex_try_eval_replacement (EggRegex *regex,
const char *replacement, const char *replacement,
GError **error); GError **error);
gint egg_regex_get_backrefmax (EggRegex *regex);
gsize _egg_regex_get_memory (EggRegex *regex); gsize _egg_regex_get_memory (EggRegex *regex);