remove more logic from GapBuffer

This commit is contained in:
Josh Holtrop 2016-12-07 22:56:08 -05:00
parent 41c65f2dd9
commit 7384128d40
6 changed files with 15 additions and 34 deletions

View File

@ -89,6 +89,7 @@ void Buffer::load_text_in_buffer(uint8_t * buffer, size_t buffer_size, size_t da
text_loader.load_buffer(buffer, data_length, &loaded_size);
m_gap_buffer = std::make_shared<GapBuffer>(buffer, buffer_size, loaded_size, text_loader.get_encoding());
m_encoding = text_loader.get_encoding();
m_eol_at_eof = text_loader.get_eol_at_eof();
m_line_endings = text_loader.get_line_endings();
}
@ -103,7 +104,7 @@ bool Buffer::write_to_file(const char * filename)
m_gap_buffer->compact();
Cursor start_of_line(&*m_gap_buffer);
Cursor start_of_line(&*m_gap_buffer, m_encoding);
size_t bytes_written = 0u;
while (start_of_line.valid())

View File

@ -16,7 +16,10 @@ public:
Buffer(const uint8_t * data, size_t data_length);
bool write_to_file(const char * filename);
auto add_cursor() { return m_gap_buffer->add_cursor(); }
std::shared_ptr<Cursor> add_cursor()
{
return std::make_shared<Cursor>(&*m_gap_buffer, m_encoding);
}
auto get_string() { return m_gap_buffer->get_string(); }
void insert(Cursor & insert_cursor, uint32_t code_point)
{
@ -27,6 +30,7 @@ protected:
bool m_eol_at_eof;
LineEndings::Type m_line_endings;
std::shared_ptr<GapBuffer> m_gap_buffer;
Encoding::Type m_encoding;
bool load_from_file(const char * filename);
void load_empty_buffer();

View File

@ -4,7 +4,7 @@ void Iterator::go_forward()
{
if (valid())
{
m_offset += Encoding::num_bytes_in_code_point(m_gap_buffer->encoding(), address());
m_offset += Encoding::num_bytes_in_code_point(m_encoding, address());
}
}
@ -34,7 +34,7 @@ void Iterator::go_back()
else
{
const uint8_t * a = m_gap_buffer->address(m_offset - 1u);
const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_gap_buffer->encoding(), a);
const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_encoding, a);
m_offset -= ((a - beginning_of_code_point) + 1u);
}
}

View File

@ -9,10 +9,11 @@
class Iterator
{
public:
Iterator(GapBuffer * gap_buffer)
Iterator(GapBuffer * gap_buffer, Encoding::Type encoding)
{
m_gap_buffer = gap_buffer;
m_offset = 0u;
m_encoding = encoding;
}
bool valid() const
{
@ -30,7 +31,7 @@ public:
{
if (valid())
{
return Encoding::decode(m_gap_buffer->encoding(), address());
return Encoding::decode(m_encoding, address());
}
else
{
@ -48,13 +49,14 @@ public:
protected:
GapBuffer * m_gap_buffer;
size_t m_offset;
Encoding::Type m_encoding;
};
class Cursor
{
public:
Cursor(GapBuffer * gap_buffer)
: m_iterator(gap_buffer)
Cursor(GapBuffer * gap_buffer, Encoding::Type encoding)
: m_iterator(gap_buffer, encoding)
{
m_line = 0u;
init_column();

View File

@ -28,13 +28,6 @@ GapBuffer::~GapBuffer()
System::free_pages(m_buffer, m_buffer_size >> System::page_size_log);
}
std::shared_ptr<Cursor> GapBuffer::add_cursor()
{
std::shared_ptr<Cursor> cursor = std::make_shared<Cursor>(this);
m_cursors.push_back(cursor);
return cursor;
}
void GapBuffer::compact()
{
if (m_gap_position < m_size)
@ -58,23 +51,6 @@ void GapBuffer::insert(Cursor & insert_cursor, uint32_t code_point)
size_t size = Encoding::encode(code_point, m_encoding, &m_buffer[m_gap_position]);
m_gap_position += size;
m_size += size;
/* Now adjust any cursors that were >= position. */
for (auto cursor : m_cursors)
{
if (cursor->iterator().offset() >= position)
{
cursor->iterator().set_offset(cursor->iterator().offset() + size);
if (cursor->line() == insert_cursor.line())
{
cursor->calculate_column();
}
if (code_point == '\n')
{
cursor->set_line(cursor->line() + 1u);
}
}
}
}
/**

View File

@ -20,7 +20,6 @@ public:
~GapBuffer();
size_t buffer_size() const { return m_buffer_size; }
size_t size() const { return m_size; }
std::shared_ptr<Cursor> add_cursor();
uint8_t * address(size_t offset) const
{
if (offset < m_gap_position)
@ -44,7 +43,6 @@ protected:
size_t m_size;
size_t m_gap_position;
Encoding::Type m_encoding;
std::list<std::shared_ptr<Cursor>> m_cursors;
void check_grow();
void move_gap(size_t position);