[vte] terminal: Use string_view



commit fcbf3e9640f6aa98473ee0471c13343a9df133da
Author: Christian Persch <chpe src gnome org>
Date:   Thu Nov 21 20:01:30 2019 +0100

    terminal: Use string_view

 src/icu-converter.cc | 11 ++++-----
 src/icu-converter.hh |  3 +--
 src/keymap.cc        |  4 +--
 src/keymap.h         |  4 +--
 src/vte.cc           | 69 +++++++++++++++++-----------------------------------
 src/vteinternal.hh   |  8 ++----
 6 files changed, 34 insertions(+), 65 deletions(-)
---
diff --git a/src/icu-converter.cc b/src/icu-converter.cc
index 7ac18a34..5185e4d8 100644
--- a/src/icu-converter.cc
+++ b/src/icu-converter.cc
@@ -52,8 +52,7 @@ ICUConverter::make(char const* charset,
 }
 
 std::string
-ICUConverter::convert(char const* data,
-                      size_t length)
+ICUConverter::convert(std::string_view const& data)
 {
         /* We can't use ucnv_convertEx since that doesn't support preflighting.
          * Instead, convert to UTF-16 first, and the to the target, with
@@ -61,7 +60,7 @@ ICUConverter::convert(char const* data,
          * code path, so we don't care.
          */
 
-        if (length == 0)
+        if (data.size() == 0)
                 return {};
 
         ucnv_resetToUnicode(m_u8_converter.get());
@@ -69,7 +68,7 @@ ICUConverter::convert(char const* data,
         auto err = icu::ErrorCode{};
         auto u16_size = ucnv_toUChars(m_u8_converter.get(),
                                       nullptr, 0,
-                                      data, length,
+                                      data.data(), data.size(),
                                       err);
         if (err.isFailure() && (err.get() != U_BUFFER_OVERFLOW_ERROR)) {
                 _vte_debug_print(VTE_DEBUG_CONVERSION,
@@ -87,8 +86,8 @@ ICUConverter::convert(char const* data,
         u16_size = ucnv_toUChars(m_u8_converter.get(),
                                  u16_buffer.data(),
                                  u16_buffer.size(),
-                                 data,
-                                 length,
+                                 data.data(),
+                                 data.size(),
                                  err);
         if (err.isFailure()) {
                 _vte_debug_print(VTE_DEBUG_CONVERSION,
diff --git a/src/icu-converter.hh b/src/icu-converter.hh
index 9f1c3731..5a99cb32 100644
--- a/src/icu-converter.hh
+++ b/src/icu-converter.hh
@@ -61,8 +61,7 @@ public:
         auto u32_converter() noexcept     { return m_u32_converter.get();     }
         auto u8_converter() noexcept      { return m_u8_converter.get();      }
 
-        std::string convert(char const* data,
-                            size_t length);
+        std::string convert(std::string_view const& data);
 
 private:
         std::string m_charset;
diff --git a/src/keymap.cc b/src/keymap.cc
index 2bee305d..601707f6 100644
--- a/src/keymap.cc
+++ b/src/keymap.cc
@@ -713,7 +713,7 @@ _vte_keymap_map(guint keyval,
                gboolean app_cursor_keys,
                gboolean app_keypad_keys,
                char **normal,
-               gssize *normal_length)
+               gsize *normal_length)
 {
        gsize i;
        const struct _vte_keymap_entry *entries;
@@ -942,7 +942,7 @@ _vte_keymap_key_add_key_modifiers(guint keyval,
                                  guint modifiers,
                                  gboolean cursor_app_mode,
                                  char **normal,
-                                 gssize *normal_length)
+                                 gsize *normal_length)
 {
        int modifier, offset;
        char *nnormal;
diff --git a/src/keymap.h b/src/keymap.h
index 5b8566bd..c1c800e1 100644
--- a/src/keymap.h
+++ b/src/keymap.h
@@ -36,7 +36,7 @@ void _vte_keymap_map(guint keyval,
                     gboolean app_cursor_keys,
                     gboolean app_keypad_keys,
                     char **normal,
-                    gssize *normal_length);
+                    gsize *normal_length);
 
 /* Return TRUE if a keyval is just a modifier key. */
 gboolean _vte_keymap_key_is_modifier(guint keyval);
@@ -46,7 +46,7 @@ void _vte_keymap_key_add_key_modifiers(guint keyval,
                                       guint modifiers,
                                       gboolean app_cursor_keys,
                                       char **normal,
-                                      gssize *normal_length);
+                                      gsize *normal_length);
 
 G_END_DECLS
 
diff --git a/src/vte.cc b/src/vte.cc
index 0a5afca6..25988e42 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -727,32 +727,24 @@ Terminal::emit_selection_changed()
        g_signal_emit(m_terminal, signals[SIGNAL_SELECTION_CHANGED], 0);
 }
 
-/* Emit a "commit" signal. */
+/* Emit a "commit" signal.
+ * FIXMEchpe: remove this function
+ */
 void
-Terminal::emit_commit(char const* text,
-                      gssize length)
+Terminal::emit_commit(std::string_view const& str)
 {
-        if (length == 0)
+        if (str.size() == 0)
                 return;
 
-       char const* result = NULL;
-       char *wrapped = NULL;
-
        _vte_debug_print(VTE_DEBUG_SIGNALS,
-                       "Emitting `commit' of %" G_GSSIZE_FORMAT" bytes.\n", length);
+                         "Emitting `commit' of %" G_GSSIZE_FORMAT" bytes.\n", str.size());
 
-       if (length == -1) {
-               length = strlen(text);
-               result = text;
-       } else {
-                // FIXMEchpe why use the slice allocator here?
-               result = wrapped = (char *) g_slice_alloc(length + 1);
-               memcpy(wrapped, text, length);
-               wrapped[length] = '\0';
-       }
+        // FIXMEchpe we do know for a fact that all uses of this function
+        // actually passed a 0-terminated string, so we can use @str directly
+        std::string result{str}; // 0-terminated
 
         _VTE_DEBUG_IF(VTE_DEBUG_KEYBOARD) {
-                for (gssize i = 0; i < length; i++) {
+                for (size_t i = 0; i < result.size(); i++) {
                         if ((((guint8) result[i]) < 32) ||
                             (((guint8) result[i]) > 127)) {
                                 g_printerr(
@@ -768,10 +760,7 @@ Terminal::emit_commit(char const* text,
                 }
         }
 
-       g_signal_emit(m_terminal, signals[SIGNAL_COMMIT], 0, result, (guint)length);
-
-       if(wrapped)
-               g_slice_free1(length+1, wrapped);
+       g_signal_emit(m_terminal, signals[SIGNAL_COMMIT], 0, result.c_str(), (guint)result.size());
 }
 
 void
@@ -4145,41 +4134,29 @@ Terminal::pty_io_write(int const fd,
         return _vte_byte_array_length(m_outgoing) != 0;
 }
 
-void
-Terminal::send_child(std::string_view const& str)
-{
-        send_child(str.data(), str.size());
-}
-
 /* Send some UTF-8 data to the child. */
 void
-Terminal::send_child(char const* data,
-                     gssize length) noexcept
+Terminal::send_child(std::string_view const& data)
 {
+        // FIXMEchpe remove
         if (!m_input_enabled)
                 return;
 
-        if (length == -1)
-                length = strlen(data);
-        if (length == 0)
-                return;
-
         /* If there's a place for it to go, add the data to the
          * outgoing buffer. */
-        // FIXMEchpe: shouldn't require pty for this
         if (!pty())
                 return;
 
         switch (data_syntax()) {
         case DataSyntax::eECMA48_UTF8:
-                emit_commit(data, length);
-                _vte_byte_array_append(m_outgoing, data, length);
+                emit_commit(data);
+                _vte_byte_array_append(m_outgoing, data.data(), data.size());
                 break;
 
         case DataSyntax::eECMA48_PCTERM: {
-                auto converted = m_converter->convert(data, length);
+                auto converted = m_converter->convert(data);
 
-                emit_commit(converted.data(), converted.size());
+                emit_commit(converted);
                 _vte_byte_array_append(m_outgoing, converted.data(), converted.size());
                 break;
         }
@@ -4228,7 +4205,6 @@ Terminal::feed_child_binary(std::string_view const& data)
 
         /* If there's a place for it to go, add the data to the
          * outgoing buffer. */
-        // FIXMEchpe shouldn't require a PTY
         if (!pty())
                 return;
 
@@ -4575,8 +4551,7 @@ bool
 Terminal::widget_key_press(GdkEventKey *event)
 {
        char *normal = NULL;
-       gssize normal_length = 0;
-       int i;
+       gsize normal_length = 0;
        struct termios tio;
        gboolean scrolled = FALSE, steal = FALSE, modifier = FALSE, handled,
                 suppress_meta_esc = FALSE, add_modifiers = FALSE;
@@ -4989,7 +4964,7 @@ Terminal::widget_key_press(GdkEventKey *event)
                            (m_modifiers & GDK_CONTROL_MASK)) {
                                /* Replace characters which have "control"
                                 * counterparts with those counterparts. */
-                               for (i = 0; i < normal_length; i++) {
+                               for (size_t i = 0; i < normal_length; i++) {
                                        if ((((guint8)normal[i]) >= 0x40) &&
                                            (((guint8)normal[i]) <  0x80)) {
                                                normal[i] &= (~(0x60));
@@ -5020,7 +4995,7 @@ Terminal::widget_key_press(GdkEventKey *event)
                                feed_child(_VTE_CAP_ESC, 1);
                        }
                        if (normal_length > 0) {
-                               send_child(normal, normal_length);
+                               send_child({normal, normal_length});
                        }
                        g_free(normal);
                }
@@ -9565,7 +9540,7 @@ Terminal::widget_scroll(GdkEventScroll *event)
        if (m_screen == &m_alternate_screen &&
             m_modes_private.XTERM_ALTBUF_SCROLL()) {
                char *normal;
-               gssize normal_length;
+               gsize normal_length;
 
                cnt = v * m_mouse_smooth_scroll_delta;
                if (cnt == 0)
@@ -9588,7 +9563,7 @@ Terminal::widget_scroll(GdkEventScroll *event)
                if (cnt < 0)
                        cnt = -cnt;
                for (i = 0; i < cnt; i++) {
-                       send_child(normal, normal_length);
+                       send_child({normal, normal_length});
                }
                g_free (normal);
        } else {
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 78f5c297..43ebeb9f 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -972,9 +972,7 @@ public:
         bool pty_io_write(int const fd,
                           GIOCondition const condition);
 
-        void send_child(std::string_view const& str);
-        void send_child(char const* data,
-                        gssize length) noexcept;
+        void send_child(std::string_view const& data);
 
         void watch_child (pid_t child_pid);
         bool terminate_child () noexcept;
@@ -1098,9 +1096,7 @@ public:
         void beep();
 
         void emit_adjustment_changed();
-        void emit_commit(char const* text,
-                         gssize length);
-        void emit_commit(std::string_view const& str) { emit_commit(str.data(), str.size()); }
+        void emit_commit(std::string_view const& str);
         void emit_eof();
         void emit_selection_changed();
         void queue_adjustment_changed();


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