[gtksourceview] implregex: make next() closer to gregex



commit ae02138af843b942c2b11f1d2fbdf869b8f4d4c8
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 2 14:32:20 2021 -0700

    implregex: make next() closer to gregex

 gtksourceview/implregex.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
---
diff --git a/gtksourceview/implregex.c b/gtksourceview/implregex.c
index 92d55226..978b3f88 100644
--- a/gtksourceview/implregex.c
+++ b/gtksourceview/implregex.c
@@ -1032,18 +1032,19 @@ impl_match_info_next (ImplMatchInfo  *match_info,
        g_assert (match_info->regex != NULL);
        g_assert (match_info->regex->code != NULL);
        g_assert (match_info->offsets == pcre2_get_ovector_pointer (match_info->match_data));
+       g_assert (match_info->start_pos >= 0);
 
-again:
-       match_info->matches = -1;
+       prev_begin = match_info->offsets[0];
+       prev_end = match_info->offsets[1];
 
        if (match_info->start_pos > match_info->string_len)
        {
+               /* we have reached the end of the string */
+               match_info->start_pos = -1;
+               match_info->matches = PCRE2_ERROR_NOMATCH;
                return FALSE;
        }
 
-       prev_begin = match_info->offsets[0];
-       prev_end = match_info->offsets[1];
-
        if (match_info->regex->has_jit)
        {
                match_info->matches = pcre2_jit_match (match_info->regex->code,
@@ -1072,25 +1073,23 @@ again:
 
        if (set_regex_error (error, match_info->matches))
        {
-               match_info->start_pos = match_info->string_len + 1;
                return FALSE;
        }
 
        /* Avoid infinite loops if the pattern is an empty string or
         * something equivalent.
         */
-       if (prev_end == match_info->offsets[1])
+       if (match_info->start_pos == match_info->offsets[1])
        {
-               const char *next = g_utf8_next_char (match_info->string + prev_end);
-
                if (match_info->start_pos > match_info->string_len)
                {
+                       /* we have reached the end of the string */
                        match_info->start_pos = -1;
                        match_info->matches = PCRE2_ERROR_NOMATCH;
                        return FALSE;
                }
 
-               match_info->start_pos = next - match_info->string;
+               match_info->start_pos = g_utf8_next_char (&match_info->string[match_info->start_pos]) - 
match_info->string;
        }
        else
        {
@@ -1112,14 +1111,15 @@ again:
            prev_begin == match_info->offsets[0] &&
            prev_end == match_info->offsets[1])
        {
-               goto again;
+               /* ignore this match and search the next one */
+               return impl_match_info_next (match_info, error);
        }
 
        g_assert (match_info->offsets == pcre2_get_ovector_pointer (match_info->match_data));
 
        GTK_SOURCE_PROFILER_END_MARK (G_STRFUNC, NULL);
 
-       return impl_match_info_matches (match_info);
+       return match_info->matches >= 0;
 }
 
 int


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]