[vte] terminal: Store word chars exceptions string in Widget



commit c07bd83c35df3d10f543756a5a4c6749f959c5d9
Author: Christian Persch <chpe src gnome org>
Date:   Mon Nov 18 22:42:22 2019 +0100

    terminal: Store word chars exceptions string in Widget

 src/vte.cc         | 61 +++++++++++++++++++-----------------------------------
 src/vtegtk.cc      |  5 ++---
 src/vteinternal.hh |  9 ++++----
 src/widget.cc      | 14 +++++++++++++
 src/widget.hh      |  5 +++++
 5 files changed, 46 insertions(+), 48 deletions(-)
---
diff --git a/src/vte.cc b/src/vte.cc
index c82cdb17..5e329230 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -71,6 +71,8 @@
 
 #include <new> /* placement new */
 
+using namespace std::literals;
+
 #ifndef HAVE_ROUND
 static inline double round(double x) {
        if(x - floor(x) < 0.5) {
@@ -81,7 +83,7 @@ static inline double round(double x) {
 }
 #endif
 
-#define WORD_CHAR_EXCEPTIONS_DEFAULT "-#%&+,./=?@\\_~\302\267"
+#define WORD_CHAR_EXCEPTIONS_DEFAULT "-#%&+,./=?@\\_~\302\267"sv
 
 #define I_(string) (g_intern_static_string(string))
 
@@ -5046,15 +5048,6 @@ Terminal::widget_key_release(GdkEventKey *event)
         return false;
 }
 
-static int
-compare_unichar_p(const void *u1p,
-                  const void *u2p)
-{
-        const char32_t u1 = *(char32_t*)u1p;
-        const char32_t u2 = *(char32_t*)u2p;
-        return u1 < u2 ? -1 : u1 > u2 ? 1 : 0;
-}
-
 static const guint8 word_char_by_category[] = {
         [G_UNICODE_CONTROL]             = 2,
         [G_UNICODE_FORMAT]              = 2,
@@ -5105,11 +5098,7 @@ Terminal::is_word_char(gunichar c) const
                 return v == 1;
 
         /* Do we have an exception? */
-        return bsearch(&c,
-                       m_word_char_exceptions.data(),
-                       m_word_char_exceptions.size(),
-                       sizeof(std::u32string::value_type),
-                       compare_unichar_p) != nullptr;
+        return std::find(std::begin(m_word_char_exceptions), std::end(m_word_char_exceptions), char32_t(c)) 
!= std::end(m_word_char_exceptions);
 }
 
 /* Check if the characters in the two given locations are in the same class
@@ -10962,13 +10951,15 @@ Terminal::set_input_enabled (bool enabled)
         return true;
 }
 
-bool
-Terminal::process_word_char_exceptions(char const *str,
-                                                 std::u32string& array) const noexcept
+std::optional<std::vector<char32_t>>
+Terminal::process_word_char_exceptions(std::string_view str_view) const noexcept
 {
-        if (str == nullptr)
-                str = WORD_CHAR_EXCEPTIONS_DEFAULT;
+        if (str_view.empty())
+                str_view = WORD_CHAR_EXCEPTIONS_DEFAULT;
+
+        auto str = str_view.data();
 
+        auto array = std::vector<char32_t>{};
         array.reserve(g_utf8_strlen(str, -1));
 
         for (auto const* p = str; *p; p = g_utf8_next_char(p)) {
@@ -10992,31 +10983,26 @@ Terminal::process_word_char_exceptions(char const *str,
         }
 
         /* Sort the result since we want to use bsearch on it */
-        // FIXME remove the const cast when upgrading to C++17
-        qsort(const_cast<char32_t*>(array.data()),
-              array.size(),
-              sizeof(std::u32string::value_type),
-              compare_unichar_p);
+        std::sort(std::begin(array), std::end(array));
 
         /* Check that no character occurs twice */
         for (size_t i = 1; i < array.size(); ++i) {
                 if (array[i-1] != array[i])
                         continue;
 
-                return false;
+                return std::nullopt;
         }
 
 #if 0
         /* Debug */
-        for (size_t i = 0; i < array.size(); i++) {
+        for (auto const c : array) {
                 char utf[7];
-                auto const c = array[i];
                 utf[g_unichar_to_utf8(c, utf)] = '\0';
                 g_printerr("Word char exception: U+%04X %s\n", c, utf);
         }
 #endif
 
-        return true;
+        return array;
 }
 
 /*
@@ -11037,19 +11023,14 @@ Terminal::process_word_char_exceptions(char const *str,
  * Returns: %true if the word char exceptions changed
  */
 bool
-Terminal::set_word_char_exceptions(char const* exceptions)
+Terminal::set_word_char_exceptions(std::string_view str)
 {
-        if (g_strcmp0(exceptions, m_word_char_exceptions_string.data()) == 0)
-                return false;
-
-        std::u32string array;
-        if (!process_word_char_exceptions(exceptions, array))
-                return false;
-
-        m_word_char_exceptions_string = exceptions ? exceptions : "";
-        m_word_char_exceptions.swap(array);
+        if (auto array = process_word_char_exceptions(str)) {
+                m_word_char_exceptions = *array;
+                return true;
+        }
 
-        return true;
+        return false;
 }
 
 void
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index c7164f93..efb6456f 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -4543,8 +4543,7 @@ vte_terminal_get_word_char_exceptions(VteTerminal *terminal)
 {
         g_return_val_if_fail(VTE_IS_TERMINAL(terminal), NULL);
 
-        auto impl = IMPL(terminal);
-        return impl->m_word_char_exceptions_string.empty() ? nullptr : 
impl->m_word_char_exceptions_string.data();
+        return WIDGET(terminal)->word_char_exceptions();
 }
 
 /**
@@ -4571,7 +4570,7 @@ vte_terminal_set_word_char_exceptions(VteTerminal *terminal,
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
 
-        if (IMPL(terminal)->set_word_char_exceptions(exceptions))
+        if (WIDGET(terminal)->set_word_char_exceptions(exceptions))
                 g_object_notify_by_pspec(G_OBJECT(terminal), pspecs[PROP_WORD_CHAR_EXCEPTIONS]);
 }
 
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 7ea8c0b4..48b72c4a 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -59,6 +59,7 @@
 
 #include <list>
 #include <queue>
+#include <optional>
 #include <string>
 #include <variant>
 #include <vector>
@@ -472,8 +473,7 @@ public:
         VteCharacterReplacement *m_character_replacement{&m_character_replacements[0]};
 
         /* Word chars */
-        std::string m_word_char_exceptions_string;
-        std::u32string m_word_char_exceptions;
+        std::vector<char32_t> m_word_char_exceptions;
 
        /* Selection information. */
         gboolean m_selecting;
@@ -1266,8 +1266,7 @@ public:
         void set_size(long columns,
                       long rows);
 
-        bool process_word_char_exceptions(char const *str,
-                                          std::u32string& array) const noexcept;
+        std::optional<std::vector<char32_t>> process_word_char_exceptions(std::string_view str) const 
noexcept;
 
         long get_cell_height() { ensure_font(); return m_cell_height; }
         long get_cell_width()  { ensure_font(); return m_cell_width;  }
@@ -1325,7 +1324,7 @@ public:
         bool set_scrollback_lines(long lines);
         bool set_scroll_on_keystroke(bool scroll);
         bool set_scroll_on_output(bool scroll);
-        bool set_word_char_exceptions(char const* exceptions);
+        bool set_word_char_exceptions(std::string_view str_view);
         void set_clear_background(bool setting);
 
         bool write_contents_sync (GOutputStream *stream,
diff --git a/src/widget.cc b/src/widget.cc
index 6a628762..afaace7b 100644
--- a/src/widget.cc
+++ b/src/widget.cc
@@ -384,6 +384,20 @@ Widget::set_pty(VtePty* pty_obj) noexcept
         return true;
 }
 
+bool
+Widget::set_word_char_exceptions(std::string_view str)
+{
+        if (m_word_char_exceptions == str)
+                return false;
+
+        if (terminal()->set_word_char_exceptions(str)) {
+                m_word_char_exceptions = str;
+                return true;
+        }
+
+        return false;
+}
+
 void
 Widget::unset_pty() noexcept
 {
diff --git a/src/widget.hh b/src/widget.hh
index 2e09c75c..6d17b0cb 100644
--- a/src/widget.hh
+++ b/src/widget.hh
@@ -114,6 +114,9 @@ public:
         bool set_text_blink_mode(VteTextBlinkMode mode) { return 
terminal()->set_text_blink_mode(vte::terminal::Terminal::TextBlinkMode(mode)); }
         auto text_blink_mode() const noexcept { return VteTextBlinkMode(terminal()->text_blink_mode()); }
 
+        bool set_word_char_exceptions(std::string_view str);
+        auto word_char_exceptions() const noexcept { return m_word_char_exceptions.empty() ? nullptr : 
m_word_char_exceptions.c_str(); }
+
         char const* encoding() const noexcept { return m_terminal->encoding(); }
 
         void emit_child_exited(int status) noexcept;
@@ -183,6 +186,8 @@ private:
         vte::glib::RefPtr<VtePty> m_pty;
 
         /* Misc */
+        std::string m_word_char_exceptions;
+
         vte::glib::RefPtr<GtkAdjustment> m_hadjustment{};
         uint32_t m_hscroll_policy : 1;
         uint32_t m_vscroll_policy : 1;


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