[balsa/port-to-pcre2: 6/10] Various: Port to LibBalsaRegex




commit 1b40b0a62864ed9727f45bcf89fcaa993b13075f
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Sep 6 14:31:46 2020 -0400

    Various: Port to LibBalsaRegex

 libbalsa/gmime-gpgme-signature.c |  6 +--
 libbalsa/html.c                  |  8 ++--
 libbalsa/mime.c                  | 84 ++++++++++++++++++++--------------------
 libbalsa/misc.h                  |  5 ++-
 src/balsa-app.c                  | 10 ++---
 src/balsa-app.h                  |  2 +-
 src/balsa-mime-widget-text.c     |  5 +--
 src/balsa-print-object-text.c    |  4 +-
 src/quote-color.c                | 12 +++---
 src/quote-color.h                |  3 +-
 src/sendmsg-window.c             |  4 +-
 src/spell-check.c                |  5 +--
 12 files changed, 70 insertions(+), 78 deletions(-)
---
diff --git a/libbalsa/gmime-gpgme-signature.c b/libbalsa/gmime-gpgme-signature.c
index 8945ababa..659d2c2e6 100644
--- a/libbalsa/gmime-gpgme-signature.c
+++ b/libbalsa/gmime-gpgme-signature.c
@@ -358,14 +358,14 @@ static gchar **
 tokenize_subject(const gchar *subject,
                                 gboolean     unescape)
 {
-       static GRegex *split_re = NULL;
+       static LibBalsaRegex *split_re = NULL;
        static volatile guint initialized = 0U;
        gchar **result;
 
        /* create the reqular expression when called for teh first time */
        if (g_atomic_int_or(&initialized, 1U) == 0U) {
                /* split a DN string at unescaped ',' and '=' chars */
-               split_re = g_regex_new("(?<!\\\\)[,=]", 0, 0, NULL);
+               split_re = libbalsa_regex_new("(?<!\\\\)[,=]", 0, NULL);
                g_assert(split_re != NULL);
        }
 
@@ -380,7 +380,7 @@ tokenize_subject(const gchar *subject,
                result[0] = g_strdup(subject);
        } else {
                /* split into (oid, value) pairs */
-               result = g_regex_split(split_re, subject, 0);
+               result = libbalsa_regex_split(split_re, subject, 0);
                if (result != NULL) {
                        gint n;
 
diff --git a/libbalsa/html.c b/libbalsa/html.c
index 6a0f14fbc..07037d8e2 100644
--- a/libbalsa/html.c
+++ b/libbalsa/html.c
@@ -679,8 +679,8 @@ libbalsa_html_print_bitmap(LibBalsaMessageBody *body,
         return NULL;
     }
 
-    have_src_cid = g_regex_match_simple(CID_REGEX, text, G_REGEX_CASELESS, 0);
-    have_src_oth = g_regex_match_simple(SRC_REGEX, text, G_REGEX_CASELESS, 0);
+    have_src_cid = libbalsa_regex_match_simple(CID_REGEX, text, PCRE2_CASELESS, 0);
+    have_src_oth = libbalsa_regex_match_simple(SRC_REGEX, text, PCRE2_CASELESS, 0);
 
     info = g_new0(LibBalsaWebKitInfo, 1);
     info->body = body;
@@ -754,8 +754,8 @@ libbalsa_html_new(LibBalsaMessageBody * body,
     info->hover_cb        = hover_cb;
     info->clicked_cb      = clicked_cb;
 
-    have_src_cid = g_regex_match_simple(CID_REGEX, text, G_REGEX_CASELESS, 0);
-    have_src_oth = g_regex_match_simple(SRC_REGEX, text, G_REGEX_CASELESS, 0);
+    have_src_cid = libbalsa_regex_match_simple(CID_REGEX, text, PCRE2_CASELESS, 0);
+    have_src_oth = libbalsa_regex_match_simple(SRC_REGEX, text, PCRE2_CASELESS, 0);
 
     info->web_view = lbh_web_view_new(info, LBH_NATURAL_SIZE, have_src_cid && !have_src_oth);
 
diff --git a/libbalsa/mime.c b/libbalsa/mime.c
index c742bbada..f9d15330f 100644
--- a/libbalsa/mime.c
+++ b/libbalsa/mime.c
@@ -671,7 +671,7 @@ is_in_url(GtkTextIter * iter, gint offset, GtkTextTag * url_tag)
 static gboolean prescanner(const gchar * p, guint len);
 static void mark_urls(GtkTextBuffer * buffer, GtkTextIter * iter,
                       GtkTextTag * tag, const gchar * p);
-static GRegex *get_url_reg(void);
+static LibBalsaRegex *get_url_reg(void);
 
 void
 libbalsa_unwrap_buffer(GtkTextBuffer * buffer, GtkTextIter * iter,
@@ -745,15 +745,15 @@ mark_urls(GtkTextBuffer * buffer, GtkTextIter * iter, GtkTextTag * tag,
 {
     const gchar *p = line;
     const gchar * const line_end = line + strlen(line);
-    GRegex *url_reg = get_url_reg();
-    GMatchInfo *url_match;
+    LibBalsaRegex *url_reg = get_url_reg();
+    LibBalsaMatchData *url_match;
     GtkTextIter start = *iter;
     GtkTextIter end = *iter;
 
-    while (g_regex_match(url_reg, p, 0, &url_match)) {
+    while (libbalsa_regex_match(url_reg, p, 0, &url_match)) {
         gint start_pos, end_pos;
 
-        if (g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos)) {
+        if (libbalsa_match_data_fetch_pos(url_match, 0, &start_pos, &end_pos)) {
             glong offset = g_utf8_pointer_to_offset(line, p + start_pos);
             gtk_text_iter_set_line_offset(&start, offset);
             offset = g_utf8_pointer_to_offset(line, p + end_pos);
@@ -764,9 +764,9 @@ mark_urls(GtkTextBuffer * buffer, GtkTextIter * iter, GtkTextTag * tag,
         }
         if (!prescanner(p, line_end - p))
             break;
-        g_match_info_free(url_match);
+        libbalsa_match_data_free(url_match);
     }
-    g_match_info_free(url_match);
+    libbalsa_match_data_free(url_match);
 }
 
 /*
@@ -825,19 +825,19 @@ prescanner(const gchar * s, guint len)
 }
 
 struct url_regex_info {
-    GRegex *url_reg;
+    LibBalsaRegex *url_reg;
     const gchar *str;
     const gchar *func;
     const gchar *msg;
 };
 
-static GRegex *
+static LibBalsaRegex *
 get_url_helper(struct url_regex_info *info)
 {
     if (!info->url_reg) {
         GError *err = NULL;
 
-        info->url_reg = g_regex_new(info->str, G_REGEX_CASELESS, 0, &err);
+        info->url_reg = libbalsa_regex_new(info->str, PCRE2_CASELESS, &err);
         if (err) {
             g_warning("%s %s: %s", info->func, info->msg, err->message);
             g_error_free(err);
@@ -847,7 +847,7 @@ get_url_helper(struct url_regex_info *info)
     return info->url_reg;
 }
 
-static GRegex *
+static LibBalsaRegex *
 get_url_reg(void)
 {
     static struct url_regex_info info = {
@@ -862,7 +862,7 @@ get_url_reg(void)
     return get_url_helper(&info);
 }
 
-static GRegex *
+static LibBalsaRegex *
 get_ml_url_reg(void)
 {
     static struct url_regex_info info = {
@@ -878,7 +878,7 @@ get_ml_url_reg(void)
     return get_url_helper(&info);
 }
 
-static GRegex *
+static LibBalsaRegex *
 get_ml_flowed_url_reg(void)
 {
     static struct url_regex_info info = {
@@ -903,8 +903,8 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
     GtkTextTag *url_tag = gtk_text_tag_table_lookup(table, "url");
     gboolean match;
     gint start_pos, end_pos;
-    GRegex *url_reg;
-    GMatchInfo *url_match;
+    LibBalsaRegex *url_reg;
+    LibBalsaMatchData *url_match;
     const gchar * const line_end = chars + len;
 
     gtk_text_buffer_get_iter_at_mark(buffer, &iter,
@@ -946,8 +946,8 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
     }
 
     url_reg = get_url_reg();
-    match = g_regex_match(url_reg, chars, 0, &url_match)
-        && g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos)
+    match = libbalsa_regex_match(url_reg, chars, 0, &url_match)
+        && libbalsa_match_data_fetch_pos(url_match, 0, &start_pos, &end_pos)
         && chars + start_pos < line_end;
 
     while (match) {
@@ -960,24 +960,24 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
         if ((start_pos > 0 && (chars[start_pos - 1] == '<')) ||
             (start_pos > 4 &&
              !g_ascii_strncasecmp(chars + start_pos - 5, "<URL:", 5))) {
-            GMatchInfo *ml_url_match;
+            LibBalsaMatchData *ml_url_match;
             gint ml_start_pos, ml_end_pos;
 
             /* if the input is flowed, we may see a space at
              * url_match.rm_eo - in this case the complete remainder
              * of the ml uri should be in the passed buffer... */
             if (url_info->buffer_is_flowed && chars[end_pos] == ' ') {
-                if (g_regex_match(get_ml_flowed_url_reg(), chars + end_pos,
+                if (libbalsa_regex_match(get_ml_flowed_url_reg(), chars + end_pos,
                                   0, &ml_url_match)
-                    && g_match_info_fetch_pos(ml_url_match, 0,
+                    && libbalsa_match_data_fetch_pos(ml_url_match, 0,
                                               &ml_start_pos, &ml_end_pos)
                     && ml_start_pos == 0)
                     end_pos += ml_end_pos - 1;
-                g_match_info_free(ml_url_match);
+                libbalsa_match_data_free(ml_url_match);
             } else if (chars[end_pos] != '>') {
-                if (g_regex_match(get_ml_url_reg(), chars + end_pos,
+                if (libbalsa_regex_match(get_ml_url_reg(), chars + end_pos,
                                   0, &ml_url_match)
-                    && g_match_info_fetch_pos(ml_url_match, 0,
+                    && libbalsa_match_data_fetch_pos(ml_url_match, 0,
                                               &ml_start_pos, NULL)
                     && ml_start_pos == 0) {
                     chars += start_pos;
@@ -985,7 +985,7 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
                         g_string_new_len(chars, line_end - chars);
                     g_string_append_c(url_info->ml_url_buffer, '\n');
                 }
-                g_match_info_free(ml_url_match);
+                libbalsa_match_data_free(ml_url_match);
                 if (url_info->ml_url_buffer)
                     return TRUE;
             }
@@ -1025,15 +1025,15 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
 
         chars += end_pos;
         if (prescanner(chars, line_end - chars)) {
-            g_match_info_free(url_match);
-            match = g_regex_match(url_reg, chars, 0, &url_match)
-                && g_match_info_fetch_pos(url_match, 0, &start_pos,
+            libbalsa_match_data_free(url_match);
+            match = libbalsa_regex_match(url_reg, chars, 0, &url_match)
+                && libbalsa_match_data_fetch_pos(url_match, 0, &start_pos,
                                           &end_pos)
                 && chars + start_pos < line_end;
         } else
             match = FALSE;
     }
-    g_match_info_free(url_match);
+    libbalsa_match_data_free(url_match);
 
     gtk_text_buffer_insert_with_tags(buffer, &iter, chars,
                                      line_end - chars, tag, NULL);
@@ -1042,7 +1042,7 @@ libbalsa_insert_with_url(GtkTextBuffer * buffer,
 }
 
 void
-libbalsa_unwrap_selection(GtkTextBuffer * buffer, GRegex * rex)
+libbalsa_unwrap_selection(GtkTextBuffer * buffer, LibBalsaRegex * rex)
 {
     GtkTextIter start, end;
     gchar *line;
@@ -1114,24 +1114,24 @@ libbalsa_unwrap_selection(GtkTextBuffer * buffer, GRegex * rex)
 }
 
 gboolean
-libbalsa_match_regex(const gchar * line, GRegex * rex, guint * count,
+libbalsa_match_regex(const gchar * line, LibBalsaRegex * rex, guint * count,
                      guint * index)
 {
-    GMatchInfo *rm;
+    LibBalsaMatchData *rm;
     gint c;
     const gchar *p;
     gint end_pos;
 
     c = 0;
     for (p = line;
-         g_regex_match(rex, p, 0, &rm)
-         && g_match_info_fetch_pos(rm, 0, NULL, &end_pos)
+         libbalsa_regex_match(rex, p, 0, &rm)
+         && libbalsa_match_data_fetch_pos(rm, 0, NULL, &end_pos)
          && end_pos > 0;
          p += end_pos) {
         c++;
-        g_match_info_free(rm);
+        libbalsa_match_data_free(rm);
     }
-    g_match_info_free(rm);
+    libbalsa_match_data_free(rm);
 
     if (count)
         *count = c;
@@ -1146,8 +1146,8 @@ libbalsa_html_encode_hyperlinks(GString * paragraph)
 {
     GString * retval;
     gchar * p;
-    GRegex *url_reg = get_url_reg();
-    GMatchInfo *url_match;
+    LibBalsaRegex *url_reg = get_url_reg();
+    LibBalsaMatchData *url_match;
     gboolean match;
     gchar * markup;
 
@@ -1163,12 +1163,12 @@ libbalsa_html_encode_hyperlinks(GString * paragraph)
     retval = g_string_new("");
     p = paragraph->str;
 
-    match = g_regex_match(url_reg, p, 0, &url_match);
+    match = libbalsa_regex_match(url_reg, p, 0, &url_match);
 
     while (match) {
         gint start_pos, end_pos;
 
-        if (!g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos))
+        if (!libbalsa_match_data_fetch_pos(url_match, 0, &start_pos, &end_pos))
             break;
 
         /* add the url to the result */
@@ -1186,12 +1186,12 @@ libbalsa_html_encode_hyperlinks(GString * paragraph)
         /* find next (if any) */
         p += end_pos;
         if (prescanner(p, paragraph->len - (p - paragraph->str))) {
-            g_match_info_free(url_match);
-            match = g_regex_match(url_reg, p, 0, &url_match);
+            libbalsa_match_data_free(url_match);
+            match = libbalsa_regex_match(url_reg, p, 0, &url_match);
         } else
             match = FALSE;
     }
-    g_match_info_free(url_match);
+    libbalsa_match_data_free(url_match);
 
     /* copy remainder */
     if (*p != '\0') {
diff --git a/libbalsa/misc.h b/libbalsa/misc.h
index bbc7f9dbe..ee949e68b 100644
--- a/libbalsa/misc.h
+++ b/libbalsa/misc.h
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <gtk/gtk.h>
 #include <gmime/gmime.h>
+#include "regex.h"
 
 typedef enum _LibBalsaCodeset LibBalsaCodeset;
 
@@ -120,8 +121,8 @@ gboolean libbalsa_insert_with_url(GtkTextBuffer * buffer,
                                  guint len,
                                  GtkTextTag * tag,
                                  LibBalsaUrlInsertInfo *url_info);
-void libbalsa_unwrap_selection(GtkTextBuffer * buffer, GRegex * rex);
-gboolean libbalsa_match_regex(const gchar * line, GRegex * rex,
+void libbalsa_unwrap_selection(GtkTextBuffer * buffer, LibBalsaRegex * rex);
+gboolean libbalsa_match_regex(const gchar * line, LibBalsaRegex * rex,
                              guint * count, guint * index);
 
 int libbalsa_safe_open (const char *path, int flags, mode_t mode, GError **err);
diff --git a/src/balsa-app.c b/src/balsa-app.c
index f50e747e3..adcb5b649 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -915,21 +915,21 @@ balsa_find_index_by_mailbox(LibBalsaMailbox * mailbox)
     return NULL;
 }
 
-GRegex *
+LibBalsaRegex *
 balsa_quote_regex_new(void)
 {
-    static GRegex *regex  = NULL;
+    static LibBalsaRegex *regex  = NULL;
     static gchar  *string = NULL;
 
     if (g_strcmp0(string, balsa_app.quote_regex) != 0) {
         g_clear_pointer(&string, g_free);
-        g_clear_pointer(&regex, g_regex_unref);
+        g_clear_pointer(&regex, libbalsa_regex_free);
     }
 
     if (regex == NULL) {
         GError *err = NULL;
 
-        regex = g_regex_new(balsa_app.quote_regex, 0, 0, &err);
+        regex = libbalsa_regex_new(balsa_app.quote_regex, 0, &err);
         if (err) {
             g_warning("quote regex compilation failed: %s", err->message);
             g_error_free(err);
@@ -938,5 +938,5 @@ balsa_quote_regex_new(void)
         string = g_strdup(balsa_app.quote_regex);
     }
 
-    return g_regex_ref(regex);
+    return regex;
 }
diff --git a/src/balsa-app.h b/src/balsa-app.h
index 49a16bed4..82ce4c145 100644
--- a/src/balsa-app.h
+++ b/src/balsa-app.h
@@ -401,6 +401,6 @@ BalsaIndex* balsa_find_index_by_mailbox(LibBalsaMailbox* mailbox);
 
 void  balsa_remove_children_mailbox_nodes(BalsaMailboxNode * mbnode);
 
-GRegex *balsa_quote_regex_new(void);
+LibBalsaRegex *balsa_quote_regex_new(void);
 
 #endif                         /* __BALSA_APP_H__ */
diff --git a/src/balsa-mime-widget-text.c b/src/balsa-mime-widget-text.c
index 1fdd35273..f185254a5 100644
--- a/src/balsa-mime-widget-text.c
+++ b/src/balsa-mime-widget-text.c
@@ -1365,7 +1365,7 @@ fill_text_buf_cited(BalsaMimeWidgetText *mwt,
                     gboolean             is_flowed,
                     gboolean             is_plain)
 {
-    GRegex *rex = NULL;
+    LibBalsaRegex *rex = NULL;
     PangoContext *context = gtk_widget_get_pango_context(widget);
     PangoFontDescription *desc = pango_context_get_font_description(context);
     gdouble char_width;
@@ -1496,9 +1496,6 @@ fill_text_buf_cited(BalsaMimeWidgetText *mwt,
         g_signal_connect_after(widget, "draw",
                                G_CALLBACK(draw_cite_bars), mwt);
     }
-
-    if (rex != NULL)
-        g_regex_unref(rex);
 }
 
 GtkWidget *
diff --git a/src/balsa-print-object-text.c b/src/balsa-print-object-text.c
index d0d61d951..a68c14b00 100644
--- a/src/balsa-print-object-text.c
+++ b/src/balsa-print-object-text.c
@@ -109,7 +109,7 @@ balsa_print_object_text_plain(GList *list, GtkPrintContext * context,
                              LibBalsaMessageBody * body,
                              BalsaPrintSetup * psetup)
 {
-    GRegex *rex;
+    LibBalsaRegex *rex;
     gchar *textbuf;
     PangoFontDescription *font;
     BalsaPrintRect rect;
@@ -277,7 +277,7 @@ balsa_print_object_text_plain(GList *list, GtkPrintContext * context,
     /* clean up */
     pango_font_description_free(font);
     g_free(textbuf);
-    g_regex_unref(rex);
+
     return list;
 }
 
diff --git a/src/quote-color.c b/src/quote-color.c
index f6dc3bf25..4cb04ae12 100644
--- a/src/quote-color.c
+++ b/src/quote-color.c
@@ -39,16 +39,14 @@
  *   an integer saying how many levels deep.  
  * */
 guint
-is_a_quote(const gchar * str, GRegex * rex)
+is_a_quote(const gchar * str, LibBalsaRegex * rex)
 {
-    guint cnt;
+    guint count = 0;
 
     g_return_val_if_fail(rex != NULL, 0);
 
-    if (str == NULL)
-       return 0;
+    if (str != NULL)
+        libbalsa_match_regex(str, rex, &count, NULL);
 
-    libbalsa_match_regex(str, rex, &cnt, NULL);
-
-    return cnt;
+    return count;
 }
diff --git a/src/quote-color.h b/src/quote-color.h
index 1ea2a9393..677916b73 100644
--- a/src/quote-color.h
+++ b/src/quote-color.h
@@ -25,10 +25,11 @@
 #endif
 
 #include <gtk/gtk.h>
+#include "regex.h"
 
 G_BEGIN_DECLS
 
-    extern guint is_a_quote(const gchar *, GRegex * rex);
+    extern guint is_a_quote(const gchar *, LibBalsaRegex * rex);
 
 G_END_DECLS
 
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index efe6f2dd7..67770fcf3 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -6025,7 +6025,7 @@ sw_reflow_activated(GSimpleAction * action, GVariant * parameter, gpointer data)
     BalsaSendmsg *bsmsg = data;
     GtkTextView *text_view;
     GtkTextBuffer *buffer;
-    GRegex *rex;
+    LibBalsaRegex *rex;
 
     if (!bsmsg->flow)
        return;
@@ -6047,8 +6047,6 @@ sw_reflow_activated(GSimpleAction * action, GVariant * parameter, gpointer data)
     gtk_text_view_scroll_to_mark(text_view,
                                 gtk_text_buffer_get_insert(buffer),
                                 0, FALSE, 0, 0);
-
-    g_regex_unref(rex);
 }
 
 /* To field "changed" signal callback. */
diff --git a/src/spell-check.c b/src/spell-check.c
index 844495e07..9942f6cc3 100644
--- a/src/spell-check.c
+++ b/src/spell-check.c
@@ -563,7 +563,7 @@ done_cb(GtkButton *button,
  * dictionary to do the checking.
  * */
 
-static GRegex *quoted_rex = NULL;
+static LibBalsaRegex *quoted_rex = NULL;
 
 void
 balsa_spell_check_start(BalsaSpellCheck *spell_check)
@@ -636,8 +636,6 @@ balsa_spell_check_start(BalsaSpellCheck *spell_check)
      * balsa_app.quote_regex may change, so compile it new every
      * time!)
      */
-    if (quoted_rex != NULL)
-        g_regex_unref(quoted_rex);
     quoted_rex = balsa_quote_regex_new();
 
     spell_check->end_iter = start;
@@ -845,7 +843,6 @@ balsa_spell_check_destroy(GObject *object)
     }
 
     g_clear_pointer(&spell_check->language_tag, g_free);
-    g_clear_pointer(&quoted_rex, g_regex_unref);
 
     if (G_OBJECT_CLASS(balsa_spell_check_parent_class)->dispose)
         (*G_OBJECT_CLASS(balsa_spell_check_parent_class)->


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