GRegex: add g_regex_get_max_lookbehind()

It is useful for multi-segment regex matching.

A unit test is included.

https://bugzilla.gnome.org/show_bug.cgi?id=689794
This commit is contained in:
Sébastien Wilmet 2013-07-15 13:52:14 +02:00
parent d9e01e0c37
commit 6fbb146342
4 changed files with 45 additions and 0 deletions

View File

@ -1018,6 +1018,7 @@ g_regex_get_pattern
g_regex_get_max_backref g_regex_get_max_backref
g_regex_get_capture_count g_regex_get_capture_count
g_regex_get_has_cr_or_lf g_regex_get_has_cr_or_lf
g_regex_get_max_lookbehind
g_regex_get_string_number g_regex_get_string_number
g_regex_get_compile_flags g_regex_get_compile_flags
g_regex_get_match_flags g_regex_get_match_flags

View File

@ -1519,6 +1519,29 @@ g_regex_get_has_cr_or_lf (const GRegex *regex)
return !!value; return !!value;
} }
/**
* g_regex_get_max_lookbehind:
* @regex: a #GRegex structure
*
* Gets the number of characters in the longest lookbehind assertion in the
* pattern. This information is useful when doing multi-segment matching using
* the partial matching facilities.
*
* Returns: the number of characters in the longest lookbehind assertion.
*
* Since: 2.38
*/
gint
g_regex_get_max_lookbehind (const GRegex *regex)
{
gint max_lookbehind;
pcre_fullinfo (regex->pcre_re, regex->extra,
PCRE_INFO_MAXLOOKBEHIND, &max_lookbehind);
return max_lookbehind;
}
/** /**
* g_regex_get_compile_flags: * g_regex_get_compile_flags:
* @regex: a #GRegex * @regex: a #GRegex

View File

@ -454,6 +454,8 @@ GLIB_AVAILABLE_IN_ALL
gint g_regex_get_capture_count (const GRegex *regex); gint g_regex_get_capture_count (const GRegex *regex);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gboolean g_regex_get_has_cr_or_lf (const GRegex *regex); gboolean g_regex_get_has_cr_or_lf (const GRegex *regex);
GLIB_AVAILABLE_IN_2_38
gint g_regex_get_max_lookbehind (const GRegex *regex);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gint g_regex_get_string_number (const GRegex *regex, gint g_regex_get_string_number (const GRegex *regex,
const gchar *name); const gchar *name);

View File

@ -2084,6 +2084,24 @@ test_explicit_crlf (void)
g_regex_unref (regex); g_regex_unref (regex);
} }
static void
test_max_lookbehind (void)
{
GRegex *regex;
regex = g_regex_new ("abc", 0, 0, NULL);
g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 0);
g_regex_unref (regex);
regex = g_regex_new ("\\babc", 0, 0, NULL);
g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 1);
g_regex_unref (regex);
regex = g_regex_new ("(?<=123)abc", 0, 0, NULL);
g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 3);
g_regex_unref (regex);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -2102,6 +2120,7 @@ main (int argc, char *argv[])
g_test_add_func ("/regex/recursion", test_recursion); g_test_add_func ("/regex/recursion", test_recursion);
g_test_add_func ("/regex/multiline", test_multiline); g_test_add_func ("/regex/multiline", test_multiline);
g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf); g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
/* TEST_NEW(pattern, compile_opts, match_opts) */ /* TEST_NEW(pattern, compile_opts, match_opts) */
TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL); TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL);