add Buffer::get_string() and GapBuffer::get_string()

This commit is contained in:
Josh Holtrop 2016-11-27 18:54:03 -05:00
parent 1404c33a46
commit e57c71855a
3 changed files with 15 additions and 0 deletions

View File

@ -13,6 +13,7 @@ public:
Buffer(const char * filename = nullptr);
bool write_to_file(const char * filename);
auto add_cursor() { return m_gap_buffer->add_cursor(); }
auto get_string() { return m_gap_buffer->get_string(); }
protected:
bool m_eol_at_eof;

View File

@ -76,6 +76,18 @@ void GapBuffer::insert(Cursor & insert_cursor, uint32_t code_point)
}
}
/**
* 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 GapBuffer::get_string()
{
compact();
return std::string((char *)m_buffer, m_size);
}
/**
* Verify that there is enough free space in the gap, and if not, grow the gap
* and move data.

View File

@ -6,6 +6,7 @@
#include "Encoding.h"
#include <memory>
#include <list>
#include <string>
class GapBuffer
{
@ -115,6 +116,7 @@ public:
size_t gap_size() const { return m_buffer_size - m_size; }
void compact();
void insert(Cursor & insert_cursor, uint32_t code_point);
std::string get_string();
protected:
uint8_t * m_buffer;