Buffer::get_string() returns an EncodedString

This commit is contained in:
Josh Holtrop 2017-01-21 14:15:54 -05:00
parent 7e7b0497a7
commit 13ca7afd0c
6 changed files with 28 additions and 16 deletions

View File

@ -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);
}

View File

@ -9,6 +9,7 @@
#include <string>
#include "ChangeOperation.h"
#include <vector>
#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; }

View File

@ -4,6 +4,7 @@
#include "Encoding.h"
#include <stdint.h>
#include <string.h>
#include <string>
class EncodedString
{

View File

@ -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)
{

View File

@ -3,7 +3,9 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef ENABLE_TESTING
#include <string>
#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;

View File

@ -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);