[gtksourceview/wip/chergert/pcre2: 3/3] pcre2: start on pcre2 implementation




commit f221201fc123a2049fd23618ee6408dd7244fe47
Author: Christian Hergert <chergert redhat com>
Date:   Fri Sep 25 10:23:18 2020 -0700

    pcre2: start on pcre2 implementation

 gtksourceview/implregex-private.h |   4 +
 gtksourceview/implregex.c         | 421 +++++++++++++++++++++++++++++++-------
 2 files changed, 346 insertions(+), 79 deletions(-)
---
diff --git a/gtksourceview/implregex-private.h b/gtksourceview/implregex-private.h
index da52474e..785d060a 100644
--- a/gtksourceview/implregex-private.h
+++ b/gtksourceview/implregex-private.h
@@ -41,6 +41,7 @@ gboolean    impl_regex_match                (const ImplRegex        *regex,
                                              const char             *string,
                                              GRegexMatchFlags        match_options,
                                              ImplMatchInfo         **match_info);
+ImplRegex  *impl_regex_ref                  (ImplRegex              *regex);
 void        impl_regex_unref                (ImplRegex              *regex);
 void        impl_match_info_free            (ImplMatchInfo          *match_info);
 char       *impl_match_info_fetch           (const ImplMatchInfo    *match_info,
@@ -70,6 +71,9 @@ gboolean    impl_match_info_fetch_named_pos (const ImplMatchInfo    *match_info,
                                              const char             *name,
                                              int                    *start_pos,
                                              int                    *end_pos);
+gboolean    impl_match_info_matches         (const ImplMatchInfo    *match_info);
+gboolean    impl_match_info_next            (ImplMatchInfo          *match_info,
+                                             GError                **error);
 const char *impl_regex_get_pattern          (const ImplRegex        *regex);
 
 G_END_DECLS
diff --git a/gtksourceview/implregex.c b/gtksourceview/implregex.c
index 56a12799..d1acb732 100644
--- a/gtksourceview/implregex.c
+++ b/gtksourceview/implregex.c
@@ -21,27 +21,47 @@
 
 #include "config.h"
 
+#define PCRE2_CODE_UNIT_WIDTH 8
+
+#include <pcre2.h>
+#include <string.h>
+
 #include "implregex-private.h"
 
 struct _ImplRegex
 {
-       int         ref_count;
-       char       *pattern;
-       GRegex     *re;
+       int                ref_count;
+       char              *pattern;
+       pcre2_code        *code;
+       GRegexMatchFlags   match_options;
+       PCRE2_SPTR         name_table;
+       int                name_count;
+       int                name_entry_size;
 };
 
 struct _ImplMatchInfo
 {
-       GMatchInfo *match_info;
+       ImplRegex        *regex;
+       const char       *string;
+       gsize             string_len;
+       pcre2_match_data *match_data;
+       guint             flags;
+       int               n_matches;
+       gssize            begin;
+       gssize            end;
 };
 
-#if 0
 static void
 set_regex_error (GError **error,
                  int      errnum)
 {
        guchar errstr[128];
 
+       if (error == NULL)
+       {
+               return;
+       }
+
        pcre2_get_error_message (errnum, errstr, sizeof errstr - 1);
        errstr[sizeof errstr - 1] = 0;
 
@@ -50,15 +70,18 @@ set_regex_error (GError **error,
                             G_REGEX_ERROR_COMPILE,
                             (const gchar *)errstr);
 }
-#endif
 
 static ImplMatchInfo *
-impl_match_info_new (const ImplRegex *regex)
+impl_match_info_new (ImplRegex *regex)
 {
        ImplMatchInfo *match_info;
 
        match_info = g_slice_new0 (ImplMatchInfo);
-       match_info->match_info = NULL;
+       match_info->regex = impl_regex_ref (regex);
+       match_info->match_data = pcre2_match_data_create_from_pattern (regex->code, NULL);
+       match_info->begin = -1;
+       match_info->end = -1;
+       match_info->n_matches = -1;
 
        return match_info;
 }
@@ -69,22 +92,50 @@ impl_regex_new (const char          *pattern,
                 GRegexMatchFlags     match_options,
                 GError             **error)
 {
-       GRegex *re;
+       pcre2_code *code;
        ImplRegex *regex;
+       PCRE2_SIZE erroffset;
+       guint flags = 0;
+       int errnumber;
 
        g_return_val_if_fail (pattern != NULL, NULL);
 
-       re = g_regex_new (pattern, compile_options, match_options, error);
+       if (compile_options & G_REGEX_CASELESS)
+               flags |= PCRE2_CASELESS;
+
+       if (compile_options & G_REGEX_NEWLINE_LF)
+               flags |= PCRE2_NEWLINE_LF;
 
-       if (re == NULL)
+       code = pcre2_compile ((PCRE2_SPTR)pattern,
+                             PCRE2_ZERO_TERMINATED | flags,
+                             0,
+                             &errnumber,
+                             &erroffset,
+                             NULL);
+
+       if (code == NULL)
        {
+               set_regex_error (error, errnumber);
                return NULL;
        }
 
        regex = g_slice_new0 (ImplRegex);
        regex->ref_count = 1;
        regex->pattern = g_strdup (pattern);
-       regex->re = re;
+       regex->match_options = match_options;
+       regex->code = code;
+
+       (void)pcre2_pattern_info (code, PCRE2_INFO_NAMECOUNT, &regex->name_count);
+
+       if (regex->name_count > 0)
+       {
+               (void)pcre2_pattern_info (code,
+                                         PCRE2_INFO_NAMEENTRYSIZE,
+                                         &regex->name_entry_size);
+               (void)pcre2_pattern_info (code,
+                                         PCRE2_INFO_NAMETABLE,
+                                         &regex->name_table);
+       }
 
        return regex;
 }
@@ -97,6 +148,17 @@ impl_regex_get_pattern (const ImplRegex *regex)
        return regex->pattern;
 }
 
+ImplRegex *
+impl_regex_ref (ImplRegex *regex)
+{
+       g_return_val_if_fail (regex != NULL, NULL);
+       g_return_val_if_fail (regex->ref_count > 0, NULL);
+
+       regex->ref_count++;
+
+       return regex;
+}
+
 void
 impl_regex_unref (ImplRegex *regex)
 {
@@ -108,7 +170,7 @@ impl_regex_unref (ImplRegex *regex)
        if (regex->ref_count == 0)
        {
                g_clear_pointer (&regex->pattern, g_free);
-               g_clear_pointer (&regex->re, g_regex_unref);
+               g_clear_pointer (&regex->code, pcre2_code_free);
                g_slice_free (ImplRegex, regex);
        }
 }
@@ -116,8 +178,12 @@ impl_regex_unref (ImplRegex *regex)
 void
 impl_match_info_free (ImplMatchInfo *match_info)
 {
-       g_clear_pointer (&match_info->match_info, g_match_info_free);
-       g_slice_free (ImplMatchInfo, match_info);
+       if (match_info != NULL)
+       {
+               g_clear_pointer (&match_info->match_data, pcre2_match_data_free);
+               g_clear_pointer (&match_info->regex, impl_regex_unref);
+               g_slice_free (ImplMatchInfo, match_info);
+       }
 }
 
 gboolean
@@ -127,51 +193,60 @@ impl_regex_match (const ImplRegex   *regex,
                   ImplMatchInfo    **match_info)
 {
        g_return_val_if_fail (regex != NULL, FALSE);
-       g_return_val_if_fail (regex->re != NULL, FALSE);
-
-       if (match_info != NULL)
-       {
-               *match_info = impl_match_info_new (regex);
-       }
-
-       return g_regex_match (regex->re,
-                             string,
-                             match_options,
-                             match_info ? &(*match_info)->match_info : NULL);
+       g_return_val_if_fail (regex->code != NULL, FALSE);
+
+       return impl_regex_match_full (regex,
+                                     string,
+                                     strlen (string),
+                                     0,
+                                     match_options,
+                                     match_info,
+                                     NULL);
 }
 
 char *
 impl_match_info_fetch (const ImplMatchInfo *match_info,
                        int                  match_num)
 {
+       int begin = 0;
+       int end = 0;
+
        g_return_val_if_fail (match_info != NULL, NULL);
 
-       return g_match_info_fetch (match_info->match_info, match_num);
+       if (match_info->string == NULL ||
+           !impl_match_info_fetch_pos (match_info, match_num, &begin, &end) ||
+           begin < 0 ||
+           end < 0)
+       {
+               return g_strdup ("");
+       }
+
+       return g_strndup (match_info->string + begin, end - begin);
 }
 
 char *
 impl_match_info_fetch_named (const ImplMatchInfo *match_info,
                              const char          *name)
 {
+       int begin;
+       int end;
+
        g_return_val_if_fail (match_info != NULL, NULL);
 
-       return g_match_info_fetch_named (match_info->match_info, name);
-}
+       if (!impl_match_info_fetch_named_pos (match_info, name, &begin, &end))
+       {
+               return NULL;
+       }
 
-static gboolean
-wrapper_eval (const GMatchInfo *match_info,
-              GString          *result,
-              gpointer          user_data)
-{
-       struct {
-               ImplRegexEvalCallback callback;
-               gpointer user_data;
-       } *wrapper = user_data;
-       ImplMatchInfo wrapped = {
-               .match_info = (GMatchInfo *)match_info,
-       };
-
-       return wrapper->callback (&wrapped, result, wrapper->user_data);
+       if (match_info->string == NULL)
+       {
+               return NULL;
+       }
+
+       g_assert (begin >= 0);
+       g_assert (end >= 0);
+
+       return g_strndup (match_info->string + begin, end - begin);
 }
 
 char *
@@ -184,25 +259,70 @@ impl_regex_replace_eval (const ImplRegex        *regex,
                          gpointer                user_data,
                          GError                **error)
 {
-       struct {
-               ImplRegexEvalCallback callback;
-               gpointer user_data;
-       } wrapper;
+       ImplMatchInfo *match_info;
+       GString *out_string;
+       gboolean done;
+       int str_pos;
 
        g_return_val_if_fail (regex != NULL, NULL);
-       g_return_val_if_fail (regex->re != NULL, NULL);
-
-       wrapper.callback = eval;
-       wrapper.user_data = user_data;
-
-       return g_regex_replace_eval (regex->re,
-                                    string,
-                                    string_len,
-                                    start_position,
-                                    match_options,
-                                    wrapper_eval,
-                                    &wrapper,
-                                    error);
+       g_return_val_if_fail (regex->code != NULL, NULL);
+
+       if (string_len < 0)
+       {
+               string_len = strlen (string);
+       }
+
+       if (start_position < 0)
+       {
+               start_position = 0;
+       }
+
+       match_info = NULL;
+
+       if (!impl_regex_match_full (regex,
+                                   string,
+                                   string_len,
+                                   start_position,
+                                   match_options,
+                                   &match_info,
+                                   error))
+       {
+               impl_match_info_free (match_info);
+               return g_strndup (string, string_len);
+       }
+
+       g_assert (match_info != NULL);
+       g_assert (match_info->n_matches > 0);
+
+       str_pos = 0;
+       out_string = g_string_sized_new (string_len);
+       done = FALSE;
+
+       while (!done && impl_match_info_matches (match_info))
+       {
+               g_assert (match_info->begin >= 0);
+               g_assert (match_info->end >= 0);
+
+               g_string_append_len (out_string,
+                                    string + str_pos,
+                                    match_info->begin - str_pos);
+               str_pos = match_info->end;
+
+               done = eval (match_info, out_string, user_data);
+
+               if (!impl_match_info_next (match_info, NULL))
+               {
+                       break;
+               }
+       }
+
+       g_string_append_len (out_string,
+                            string + str_pos,
+                            string_len - str_pos);
+
+       impl_match_info_free (match_info);
+
+       return g_string_free (out_string, FALSE);
 }
 
 gboolean
@@ -214,28 +334,64 @@ impl_regex_match_full (const ImplRegex   *regex,
                        ImplMatchInfo    **match_info,
                        GError           **error)
 {
-       GMatchInfo *wrapped = NULL;
-       gboolean ret;
+       ImplMatchInfo *fallback = NULL;
+       guint flags = PCRE2_NO_UTF_CHECK;
+       gboolean ret = FALSE;
+       PCRE2_SIZE *ovector;
+       int rc;
 
        g_return_val_if_fail (regex != NULL, FALSE);
-       g_return_val_if_fail (regex->re != NULL, FALSE);
+       g_return_val_if_fail (regex->code != NULL, FALSE);
+       g_return_val_if_fail (match_options == 0, FALSE);
 
-       ret = g_regex_match_full (regex->re,
-                                 string,
-                                 string_len,
-                                 start_position,
-                                 match_options,
-                                 &wrapped,
-                                 error);
+       if (match_info == NULL)
+       {
+               match_info = &fallback;
+       }
 
-       if (match_info != NULL)
+       if (string_len < 0)
+       {
+               string_len = strlen (string);
+       }
+
+       *match_info = impl_match_info_new ((ImplRegex *)regex);
+
+       if ((match_options | regex->match_options) & G_REGEX_ANCHORED)
+       {
+               flags |= PCRE2_ANCHORED;
+       }
+
+       (*match_info)->flags = flags;
+       (*match_info)->string = string;
+       (*match_info)->string_len = string_len;
+
+       rc = pcre2_match (regex->code,
+                         (PCRE2_SPTR)string,
+                         (PCRE2_SIZE)string_len,
+                         start_position,
+                         flags,
+                         (*match_info)->match_data,
+                         NULL);
+
+       if (rc > 0)
        {
-               *match_info = g_slice_new0 (ImplMatchInfo);
-               (*match_info)->match_info = wrapped;
+               (*match_info)->n_matches = rc;
+               ret = TRUE;
        }
-       else
+       else if (rc < 0)
        {
-               g_match_info_free (wrapped);
+               set_regex_error (error, rc);
+       }
+
+       ovector = pcre2_get_ovector_pointer ((*match_info)->match_data);
+
+       (*match_info)->n_matches = rc;
+       (*match_info)->begin = ovector[0];
+       (*match_info)->end = ovector[1];
+
+       if (fallback != NULL)
+       {
+               impl_match_info_free (fallback);
        }
 
        return ret;
@@ -247,10 +403,31 @@ impl_match_info_fetch_pos (const ImplMatchInfo *match_info,
                            int                 *start_pos,
                            int                 *end_pos)
 {
+       PCRE2_SIZE *ovector;
+       int real_start_pos = -1;
+       int real_end_pos = -1;
+
        g_return_val_if_fail (match_info != NULL, FALSE);
-       g_return_val_if_fail (match_info->match_info != NULL, FALSE);
+       g_return_val_if_fail (match_info->match_data != NULL, FALSE);
+       g_return_val_if_fail (match_num >= 0, FALSE);
 
-       return g_match_info_fetch_pos (match_info->match_info, match_num, start_pos, end_pos);
+       ovector = pcre2_get_ovector_pointer (match_info->match_data);
+
+       if (match_num >= match_info->n_matches)
+       {
+               return FALSE;
+       }
+
+       real_start_pos = ovector[2*match_num];
+       real_end_pos = ovector[2*match_num+1];
+
+       if (start_pos != NULL)
+               *start_pos = real_start_pos;
+
+       if (end_pos != NULL)
+               *end_pos = real_end_pos;
+
+       return TRUE;
 }
 
 gboolean
@@ -259,8 +436,94 @@ impl_match_info_fetch_named_pos (const ImplMatchInfo *match_info,
                                  int                 *start_pos,
                                  int                 *end_pos)
 {
+       PCRE2_SPTR tabptr;
+
+       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->regex != NULL, FALSE);
+       g_return_val_if_fail (start_pos != NULL, FALSE);
+       g_return_val_if_fail (end_pos != NULL, FALSE);
+
+       tabptr = match_info->regex->name_table;
+
+       for (int i = 0; i < match_info->regex->name_count; i++)
+       {
+               int n = (tabptr[0] << 8) | tabptr[1];
+
+               if (g_strcmp0 (name, (const char *)(tabptr+2)) == 0)
+               {
+                       return impl_match_info_fetch_pos (match_info, n, start_pos, end_pos);
+               }
+
+               tabptr += match_info->regex->name_entry_size;
+       }
+
+       *start_pos = -1;
+       *end_pos = -1;
+
+       return FALSE;
+}
+
+gboolean
+impl_match_info_matches (const ImplMatchInfo *match_info)
+{
+       g_return_val_if_fail (match_info != NULL, FALSE);
+
+       return match_info->n_matches > 0;
+}
+
+gboolean
+impl_match_info_next (ImplMatchInfo  *match_info,
+                      GError        **error)
+{
+       PCRE2_SIZE *ovector;
+       int rc;
+
        g_return_val_if_fail (match_info != NULL, FALSE);
-       g_return_val_if_fail (match_info->match_info != NULL, FALSE);
+       g_return_val_if_fail (match_info->regex != NULL, FALSE);
+
+       if (match_info->begin < 0 || match_info->end < 0)
+       {
+               g_set_error (error,
+                            G_REGEX_ERROR,
+                            G_REGEX_ERROR_MATCH,
+                            "No match");
+               return FALSE;
+       }
+
+       rc = pcre2_match (match_info->regex->code,
+                         (PCRE2_SPTR)match_info->string,
+                         (PCRE2_SIZE)match_info->string_len,
+                         MAX (match_info->begin, match_info->end),
+                         match_info->flags,
+                         match_info->match_data,
+                         NULL);
+
+       if (rc < 0)
+       {
+               match_info->n_matches = -1;
+               match_info->begin = -1;
+               match_info->end = -1;
+               set_regex_error (error, rc);
+               return FALSE;
+       }
+       else if (rc == 0)
+       {
+               match_info->n_matches = -1;
+               match_info->begin = -1;
+               match_info->end = -1;
+               g_set_error (error,
+                            G_REGEX_ERROR,
+                            G_REGEX_ERROR_MATCH,
+                            "No match");
+               return FALSE;
+       }
+
+       ovector = pcre2_get_ovector_pointer (match_info->match_data);
+
+       match_info->n_matches = rc;
+       match_info->begin = ovector[0];
+       match_info->end = ovector[1];
 
-       return g_match_info_fetch_named_pos (match_info->match_info, name, start_pos, end_pos);
+       return TRUE;
 }


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