diff --git a/src/core/Buffer.h b/src/core/Buffer.h index cb7d41d..2625afe 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -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; diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index 0d97caf..550a1eb 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -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. diff --git a/src/core/GapBuffer.h b/src/core/GapBuffer.h index 1158ddf..fb631d3 100644 --- a/src/core/GapBuffer.h +++ b/src/core/GapBuffer.h @@ -6,6 +6,7 @@ #include "Encoding.h" #include #include +#include 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;