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



commit a72a70a964e4ec241a0d26c5573ef99ec5c0a89d
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 2 14:30:57 2021 -0700

    implregex: make fetch_pos() closer to gregex

 gtksourceview/implregex-private.h |  2 +-
 gtksourceview/implregex.c         | 25 +++++++++++++++----------
 2 files changed, 16 insertions(+), 11 deletions(-)
---
diff --git a/gtksourceview/implregex-private.h b/gtksourceview/implregex-private.h
index 9792192c..b0809414 100644
--- a/gtksourceview/implregex-private.h
+++ b/gtksourceview/implregex-private.h
@@ -71,7 +71,7 @@ gboolean    impl_regex_match_full            (const ImplRegex        *regex,
                                               ImplMatchInfo         **match_info,
                                               GError                **error);
 gboolean    impl_match_info_fetch_pos        (const ImplMatchInfo    *match_info,
-                                              guint                   match_num,
+                                              int                     match_num,
                                               int                    *start_pos,
                                               int                    *end_pos);
 gboolean    impl_match_info_fetch_named_pos  (const ImplMatchInfo    *match_info,
diff --git a/gtksourceview/implregex.c b/gtksourceview/implregex.c
index 178be00d..6330a1e7 100644
--- a/gtksourceview/implregex.c
+++ b/gtksourceview/implregex.c
@@ -960,26 +960,31 @@ impl_regex_replace (const ImplRegex   *regex,
 
 gboolean
 impl_match_info_fetch_pos (const ImplMatchInfo *match_info,
-                           guint                match_num,
+                           int                  match_num,
                            int                 *start_pos,
                            int                 *end_pos)
 {
        g_return_val_if_fail (match_info != NULL, FALSE);
        g_return_val_if_fail (match_info->match_data != NULL, FALSE);
        g_return_val_if_fail (match_info->offsets != NULL, FALSE);
+       g_return_val_if_fail (match_num >= 0, FALSE);
 
-       if (match_info->matches > 0 && match_num < match_info->matches)
-       {
-               if (start_pos)
-                       *start_pos = match_info->offsets[2*match_num];
+       if (match_info->matches < 0)
+               return FALSE;
 
-               if (end_pos)
-                       *end_pos = match_info->offsets[2*match_num+1];
+       /* make sure the sub expression number they're requesting is less than
+        * the total number of sub expressions in the regex. When matching all
+        * (g_regex_match_all()), also compare against the number of matches */
+       if (match_num >= MAX (match_info->matches, match_info->n_subpatterns + 1))
+               return FALSE;
 
-               return TRUE;
-       }
+       if (start_pos)
+               *start_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num] : -1;
 
-       return FALSE;
+       if (end_pos)
+               *end_pos = (match_num < match_info->matches) ? match_info->offsets[2 * match_num + 1] : -1;
+
+       return TRUE;
 }
 
 gboolean


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