remove more logic from GapBuffer
This commit is contained in:
parent
41c65f2dd9
commit
7384128d40
@ -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);
|
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_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_eol_at_eof = text_loader.get_eol_at_eof();
|
||||||
m_line_endings = text_loader.get_line_endings();
|
m_line_endings = text_loader.get_line_endings();
|
||||||
}
|
}
|
||||||
@ -103,7 +104,7 @@ bool Buffer::write_to_file(const char * filename)
|
|||||||
|
|
||||||
m_gap_buffer->compact();
|
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;
|
size_t bytes_written = 0u;
|
||||||
|
|
||||||
while (start_of_line.valid())
|
while (start_of_line.valid())
|
||||||
|
@ -16,7 +16,10 @@ public:
|
|||||||
Buffer(const uint8_t * data, size_t data_length);
|
Buffer(const uint8_t * data, size_t data_length);
|
||||||
bool write_to_file(const char * filename);
|
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(); }
|
auto get_string() { return m_gap_buffer->get_string(); }
|
||||||
void insert(Cursor & insert_cursor, uint32_t code_point)
|
void insert(Cursor & insert_cursor, uint32_t code_point)
|
||||||
{
|
{
|
||||||
@ -27,6 +30,7 @@ protected:
|
|||||||
bool m_eol_at_eof;
|
bool m_eol_at_eof;
|
||||||
LineEndings::Type m_line_endings;
|
LineEndings::Type m_line_endings;
|
||||||
std::shared_ptr<GapBuffer> m_gap_buffer;
|
std::shared_ptr<GapBuffer> m_gap_buffer;
|
||||||
|
Encoding::Type m_encoding;
|
||||||
|
|
||||||
bool load_from_file(const char * filename);
|
bool load_from_file(const char * filename);
|
||||||
void load_empty_buffer();
|
void load_empty_buffer();
|
||||||
|
@ -4,7 +4,7 @@ void Iterator::go_forward()
|
|||||||
{
|
{
|
||||||
if (valid())
|
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
|
else
|
||||||
{
|
{
|
||||||
const uint8_t * a = m_gap_buffer->address(m_offset - 1u);
|
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);
|
m_offset -= ((a - beginning_of_code_point) + 1u);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,11 @@
|
|||||||
class Iterator
|
class Iterator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Iterator(GapBuffer * gap_buffer)
|
Iterator(GapBuffer * gap_buffer, Encoding::Type encoding)
|
||||||
{
|
{
|
||||||
m_gap_buffer = gap_buffer;
|
m_gap_buffer = gap_buffer;
|
||||||
m_offset = 0u;
|
m_offset = 0u;
|
||||||
|
m_encoding = encoding;
|
||||||
}
|
}
|
||||||
bool valid() const
|
bool valid() const
|
||||||
{
|
{
|
||||||
@ -30,7 +31,7 @@ public:
|
|||||||
{
|
{
|
||||||
if (valid())
|
if (valid())
|
||||||
{
|
{
|
||||||
return Encoding::decode(m_gap_buffer->encoding(), address());
|
return Encoding::decode(m_encoding, address());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -48,13 +49,14 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
GapBuffer * m_gap_buffer;
|
GapBuffer * m_gap_buffer;
|
||||||
size_t m_offset;
|
size_t m_offset;
|
||||||
|
Encoding::Type m_encoding;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Cursor
|
class Cursor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Cursor(GapBuffer * gap_buffer)
|
Cursor(GapBuffer * gap_buffer, Encoding::Type encoding)
|
||||||
: m_iterator(gap_buffer)
|
: m_iterator(gap_buffer, encoding)
|
||||||
{
|
{
|
||||||
m_line = 0u;
|
m_line = 0u;
|
||||||
init_column();
|
init_column();
|
||||||
|
@ -28,13 +28,6 @@ GapBuffer::~GapBuffer()
|
|||||||
System::free_pages(m_buffer, m_buffer_size >> System::page_size_log);
|
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()
|
void GapBuffer::compact()
|
||||||
{
|
{
|
||||||
if (m_gap_position < m_size)
|
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]);
|
size_t size = Encoding::encode(code_point, m_encoding, &m_buffer[m_gap_position]);
|
||||||
m_gap_position += size;
|
m_gap_position += size;
|
||||||
m_size += 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,7 +20,6 @@ public:
|
|||||||
~GapBuffer();
|
~GapBuffer();
|
||||||
size_t buffer_size() const { return m_buffer_size; }
|
size_t buffer_size() const { return m_buffer_size; }
|
||||||
size_t size() const { return m_size; }
|
size_t size() const { return m_size; }
|
||||||
std::shared_ptr<Cursor> add_cursor();
|
|
||||||
uint8_t * address(size_t offset) const
|
uint8_t * address(size_t offset) const
|
||||||
{
|
{
|
||||||
if (offset < m_gap_position)
|
if (offset < m_gap_position)
|
||||||
@ -44,7 +43,6 @@ protected:
|
|||||||
size_t m_size;
|
size_t m_size;
|
||||||
size_t m_gap_position;
|
size_t m_gap_position;
|
||||||
Encoding::Type m_encoding;
|
Encoding::Type m_encoding;
|
||||||
std::list<std::shared_ptr<Cursor>> m_cursors;
|
|
||||||
|
|
||||||
void check_grow();
|
void check_grow();
|
||||||
void move_gap(size_t position);
|
void move_gap(size_t position);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user