[vte] regex: Distinguish regex purposes



commit 4a05c4413419c2d4a8d9edc2da22ca1ff48680bb
Author: Christian Persch <chpe gnome org>
Date:   Sat Mar 19 15:47:00 2016 +0100

    regex: Distinguish regex purposes

 doc/reference/vte-sections.txt |    3 +-
 src/app.vala                   |    6 +-
 src/vte/vteregex.h             |   14 ++++--
 src/vteapp.c                   |    8 ++--
 src/vtegtk.cc                  |    6 +++
 src/vteregex.cc                |   98 +++++++++++++++++++++++++++++++--------
 src/vteregexinternal.hh        |    8 +++
 7 files changed, 111 insertions(+), 32 deletions(-)
---
diff --git a/doc/reference/vte-sections.txt b/doc/reference/vte-sections.txt
index 8fa4107..56ae775 100644
--- a/doc/reference/vte-sections.txt
+++ b/doc/reference/vte-sections.txt
@@ -136,7 +136,8 @@ VteTerminalClassPrivate
 VteRegex
 vte_regex_ref
 vte_regex_unref
-vte_regex_new
+vte_regex_new_for_match
+vte_regex_new_for_search
 vte_regex_jit
 
 <SUBSECTION Standard>
diff --git a/src/app.vala b/src/app.vala
index 8104964..275ae3a 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -132,7 +132,7 @@ class SearchPopover : Gtk.Popover
             flags |= 0x00000008u; /* PCRE2_CASELESS */
           if (multiline)
             flags |= 0x00000400u; /* PCRE2_MULTILINE */
-          regex = new Vte.Regex(pattern, pattern.length, flags);
+          regex = new Vte.Regex.for_search(pattern, pattern.length, flags);
 
           try {
             regex.jit(0x00000001u /* PCRE2_JIT_COMPLETE */);
@@ -400,8 +400,8 @@ class Window : Gtk.ApplicationWindow
         if (!App.Options.no_pcre) {
           Vte.Regex regex;
 
-          regex = new Vte.Regex(dingus[i], dingus[i].length,
-                                0x40080408u /* PCRE2_UTF | PCRE2_NO_UTF_CHECK | PCRE2_CASELESS | 
PCRE2_MULTILINE */);
+          regex = new Vte.Regex.for_match(dingus[i], dingus[i].length,
+                                          0x40080408u /* PCRE2_UTF | PCRE2_NO_UTF_CHECK | PCRE2_CASELESS | 
PCRE2_MULTILINE */);
           try {
             regex.jit(0x00000001u /* PCRE2_JIT_COMPLETE */);
             regex.jit(0x00000002u /* PCRE2_JIT_PARTIAL_SOFT */);
diff --git a/src/vte/vteregex.h b/src/vte/vteregex.h
index 7169668..ced5ca6 100644
--- a/src/vte/vteregex.h
+++ b/src/vte/vteregex.h
@@ -51,10 +51,16 @@ _VTE_PUBLIC
 VteRegex *vte_regex_unref    (VteRegex *regex) _VTE_GNUC_NONNULL(1);
 
 _VTE_PUBLIC
-VteRegex *vte_regex_new      (const char *pattern,
-                              gssize      pattern_length,
-                              guint32     flags,
-                              GError    **error) _VTE_GNUC_NONNULL(1);
+VteRegex *vte_regex_new_for_match (const char *pattern,
+                                   gssize      pattern_length,
+                                   guint32     flags,
+                                   GError    **error) _VTE_GNUC_NONNULL(1);
+
+_VTE_PUBLIC
+VteRegex *vte_regex_new_for_search (const char *pattern,
+                                    gssize      pattern_length,
+                                    guint32     flags,
+                                    GError    **error) _VTE_GNUC_NONNULL(1);
 
 _VTE_PUBLIC
 gboolean  vte_regex_jit     (VteRegex *regex,
diff --git a/src/vteapp.c b/src/vteapp.c
index 580cbe9..27b1f0e 100644
--- a/src/vteapp.c
+++ b/src/vteapp.c
@@ -171,7 +171,7 @@ button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer data)
                }
 #ifdef WITH_PCRE2
                 if (!use_gregex) {
-                        VteRegex *regex = vte_regex_new("\\d+", -1, PCRE2_UTF, NULL);
+                        VteRegex *regex = vte_regex_new_for_match("\\d+", -1, PCRE2_UTF, NULL);
                         has_extra_match = vte_terminal_event_check_regex_simple(terminal,
                                                                                 (GdkEvent*)event,
                                                                                 &regex, 1,
@@ -569,9 +569,9 @@ add_dingus (VteTerminal *terminal,
                 VteRegex *regex = NULL;
 
                 if (!use_gregex)
-                        regex = vte_regex_new(dingus[i], -1,
-                                              PCRE2_UTF | PCRE2_NO_UTF_CHECK,
-                                              &error);
+                        regex = vte_regex_new_for_match(dingus[i], -1,
+                                                        PCRE2_UTF | PCRE2_NO_UTF_CHECK,
+                                                        &error);
                 else
 #endif
                         gregex = g_regex_new(dingus[i], G_REGEX_OPTIMIZE | G_REGEX_MULTILINE, 0, &error);
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index a8b61e5..eb955e5 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -1734,6 +1734,7 @@ vte_terminal_match_add_regex(VteTerminal *terminal,
 
        g_return_val_if_fail(VTE_IS_TERMINAL(terminal), -1);
        g_return_val_if_fail(regex != NULL, -1);
+        g_return_val_if_fail(_vte_regex_has_purpose(regex, VteRegexPurpose::match), -1);
 
         auto impl = IMPL(terminal);
         /* Can't mix GRegex and PCRE2 */
@@ -1836,6 +1837,8 @@ vte_terminal_event_check_regex_simple(VteTerminal *terminal,
         g_return_val_if_fail(VTE_IS_TERMINAL(terminal), FALSE);
         g_return_val_if_fail(event != NULL, FALSE);
         g_return_val_if_fail(regexes != NULL || n_regexes == 0, FALSE);
+        for (gsize i = 0; i < n_regexes; i++)
+                g_return_val_if_fail(_vte_regex_has_purpose(regexes[i], VteRegexPurpose::match), -1);
         g_return_val_if_fail(matches != NULL, FALSE);
 
         return IMPL(terminal)->regex_match_check_extra(event, regexes, n_regexes, match_flags, matches);
@@ -1947,6 +1950,7 @@ void
 vte_terminal_match_remove(VteTerminal *terminal, int tag)
 {
        g_return_if_fail(VTE_IS_TERMINAL(terminal));
+        g_return_if_fail(tag != -1);
         IMPL(terminal)->regex_match_remove(tag);
 }
 
@@ -2013,6 +2017,8 @@ vte_terminal_search_set_regex (VteTerminal *terminal,
 {
 #ifdef WITH_PCRE2
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
+        g_return_if_fail(regex == NULL || _vte_regex_has_purpose(regex, VteRegexPurpose::search));
+
         IMPL(terminal)->search_set_regex(regex, flags);
 #endif
 }
diff --git a/src/vteregex.cc b/src/vteregex.cc
index 53a5dd3..672bf02 100644
--- a/src/vteregex.cc
+++ b/src/vteregex.cc
@@ -36,6 +36,7 @@
 
 struct _VteRegex {
         volatile int ref_count;
+        VteRegexPurpose purpose;
 #ifdef WITH_PCRE2
         pcre2_code_8 *code;
 #endif /* WITH_PCRE2 */
@@ -54,12 +55,14 @@ struct _VteRegex {
 #ifdef WITH_PCRE2
 
 static VteRegex *
-regex_new(pcre2_code_8 *code)
+regex_new(pcre2_code_8 *code,
+          VteRegexPurpose purpose)
 {
         VteRegex *regex;
 
         regex = g_slice_new(VteRegex);
         regex->ref_count = 1;
+        regex->purpose = purpose;
         regex->code = code;
 
         return regex;
@@ -142,24 +145,9 @@ vte_regex_unref(VteRegex *regex)
         return NULL;
 }
 
-/**
- * vte_regex_new:
- * @pattern: a regex pattern string
- * @pattern_length: the length of @pattern in bytes, or -1 if the
- *  string is NUL-terminated and the length is unknown
- * @flags: PCRE2 compile flags
- * @error: (allow-none): return location for a #GError, or %NULL
- *
- * Compiles @pattern into a regex. See man:pcre2pattern(3) for information
- * about the supported regex language.
- *
- * The regex will be compiled using %PCRE2_UTF and possibly other flags, in
- * addition to the flags supplied in @flags.
- *
- * Returns: (transfer full): a newly created #VteRegex, or %NULL with @error filled in
- */
-VteRegex *
-vte_regex_new(const char *pattern,
+static VteRegex *
+vte_regex_new(VteRegexPurpose purpose,
+              const char *pattern,
               gssize      pattern_length,
               guint32     flags,
               GError    **error)
@@ -199,13 +187,76 @@ vte_regex_new(const char *pattern,
                 return NULL;
         }
 
-        return regex_new(code);
+        return regex_new(code, purpose);
 #else
         set_unsupported_error(error);
         return NULL;
 #endif /* WITH_PCRE2 */
 }
 
+/**
+ * vte_regex_new_for_match:
+ * @pattern: a regex pattern string
+ * @pattern_length: the length of @pattern in bytes, or -1 if the
+ *  string is NUL-terminated and the length is unknown
+ * @flags: PCRE2 compile flags
+ * @error: (allow-none): return location for a #GError, or %NULL
+ *
+ * Compiles @pattern into a regex for use as a match regex
+ * with vte_terminal_match_add_regex() or
+ * vte_terminal_event_check_regex_simple().
+ *
+ * See man:pcre2pattern(3) for information
+ * about the supported regex language.
+ *
+ * The regex will be compiled using %PCRE2_UTF and possibly other flags, in
+ * addition to the flags supplied in @flags.
+ *
+ * Returns: (transfer full): a newly created #VteRegex, or %NULL with @error filled in
+ */
+VteRegex *
+vte_regex_new_for_match(const char *pattern,
+                        gssize      pattern_length,
+                        guint32     flags,
+                        GError    **error)
+{
+        return vte_regex_new(VteRegexPurpose::match,
+                             pattern, pattern_length,
+                             flags,
+                             error);
+}
+
+/**
+ * vte_regex_new_for_search:
+ * @pattern: a regex pattern string
+ * @pattern_length: the length of @pattern in bytes, or -1 if the
+ *  string is NUL-terminated and the length is unknown
+ * @flags: PCRE2 compile flags
+ * @error: (allow-none): return location for a #GError, or %NULL
+ *
+ * Compiles @pattern into a regex for use as a search regex
+ * with vte_terminal_search_set_regex().
+ *
+ * See man:pcre2pattern(3) for information
+ * about the supported regex language.
+ *
+ * The regex will be compiled using %PCRE2_UTF and possibly other flags, in
+ * addition to the flags supplied in @flags.
+ *
+ * Returns: (transfer full): a newly created #VteRegex, or %NULL with @error filled in
+ */
+VteRegex *
+vte_regex_new_for_search(const char *pattern,
+                         gssize      pattern_length,
+                         guint32     flags,
+                         GError    **error)
+{
+        return vte_regex_new(VteRegexPurpose::search,
+                             pattern, pattern_length,
+                             flags,
+                             error);
+}
+
 #if 0
 /*
  * vte_regex_new_pcre:
@@ -239,6 +290,13 @@ vte_regex_new_pcre(pcre2_code_8 *code,
 }
 #endif
 
+gboolean
+_vte_regex_has_purpose(VteRegex *regex,
+                       VteRegexPurpose purpose)
+{
+        return regex->purpose == purpose;
+}
+
 #ifdef WITH_PCRE2
 /*
  * _vte_regex_get_pcre:
diff --git a/src/vteregexinternal.hh b/src/vteregexinternal.hh
index b12d25a..88fb848 100644
--- a/src/vteregexinternal.hh
+++ b/src/vteregexinternal.hh
@@ -17,6 +17,14 @@
 
 #pragma once
 
+enum class VteRegexPurpose {
+        match,
+        search
+};
+
+gboolean _vte_regex_has_purpose(VteRegex *regex,
+                                VteRegexPurpose purpose);
+
 gboolean _vte_regex_get_jited(VteRegex *regex);
 
 #ifdef WITH_PCRE2


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