From 13ca7afd0c568731b4a710dea28419ee4492fc04 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 21 Jan 2017 14:15:54 -0500 Subject: [PATCH] Buffer::get_string() returns an EncodedString --- src/core/Buffer.cc | 6 ++++++ src/core/Buffer.h | 4 ++-- src/core/EncodedString.h | 1 + src/core/GapBuffer.cc | 2 ++ src/core/GapBuffer.h | 30 +++++++++++++++++------------- src/gui/Window.cc | 1 - 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 84fdd2f..9e16d38 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -471,3 +471,9 @@ void Buffer::clear() *iterator = b; } } + +EncodedString Buffer::get_string() +{ + m_gap_buffer->compact(); + return EncodedString(m_gap_buffer->address(0u), m_gap_buffer->size(), m_encoding); +} diff --git a/src/core/Buffer.h b/src/core/Buffer.h index 55bed65..89c1867 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -9,6 +9,7 @@ #include #include "ChangeOperation.h" #include +#include "EncodedString.h" class Buffer { @@ -120,8 +121,7 @@ public: return iterator; } void clear(); - auto get_string() { return m_gap_buffer->get_string(); } - void set_string(const std::string & s) { m_gap_buffer->set_string(s); } + EncodedString get_string(); size_t size() const { return m_gap_buffer->size(); } uint8_t * address(size_t offset) const { return m_gap_buffer->address(offset); } Encoding::Type encoding() const { return m_encoding; } diff --git a/src/core/EncodedString.h b/src/core/EncodedString.h index fe9495d..0e4557f 100644 --- a/src/core/EncodedString.h +++ b/src/core/EncodedString.h @@ -4,6 +4,7 @@ #include "Encoding.h" #include #include +#include class EncodedString { diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index aea4cfd..103e8d2 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -56,6 +56,7 @@ void GapBuffer::clear() m_gap_position = 0u; } +#ifdef ENABLE_TESTING std::string GapBuffer::get_string() { compact(); @@ -67,6 +68,7 @@ void GapBuffer::set_string(const std::string & s) clear(); insert(0u, (const uint8_t *)s.c_str(), s.size()); } +#endif void GapBuffer::ensure_free(size_t length) { diff --git a/src/core/GapBuffer.h b/src/core/GapBuffer.h index cd49261..21a5003 100644 --- a/src/core/GapBuffer.h +++ b/src/core/GapBuffer.h @@ -3,7 +3,9 @@ #include #include +#ifdef ENABLE_TESTING #include +#endif class GapBuffer { @@ -61,19 +63,6 @@ public: */ void clear(); - /** - * Get the contents of the gap buffer as a string. - * - * This makes a copy of the entire buffer in a std::string. This method is - * intended for use by unit tests. - */ - std::string get_string(); - - /** - * Replace the buffer contents with a string. - */ - void set_string(const std::string & s); - /** * Get the size of the data stored within the buffer. */ @@ -123,6 +112,21 @@ public: */ void copy_to(size_t source_position, size_t length, GapBuffer & other, size_t destination_position); +#ifdef ENABLE_TESTING + /** + * Get the contents of the gap buffer as a string. + * + * This makes a copy of the entire buffer in a std::string. This method is + * intended for use by unit tests. + */ + std::string get_string(); + + /** + * Replace the buffer contents with a string. + */ + void set_string(const std::string & s); +#endif + protected: /** Address of the allocated memory buffer. */ uint8_t * m_buffer; diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 0a6a548..080e997 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -323,7 +323,6 @@ void Window::handle_keyval(uint32_t keyval) { if ((keyval == '\n') && (m_focused_buffer_pane == m_command_buffer_pane)) { - std::string command = m_command_buffer->get_string(); m_command_buffer_pane->clear(); m_command_buffer_screen_rows = 1; change_focus(m_buffer_pane);