From 5f3831965e0a0217131cd38f5df5fdeb9f2dcb9c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 7 Dec 2016 23:05:38 -0500 Subject: [PATCH] remove encoding from GapBuffer --- src/core/Buffer.cc | 4 ++-- src/core/GapBuffer.cc | 25 ++++++++++++------------- src/core/GapBuffer.h | 11 ++++------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 9ca0e74..5010aea 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -29,7 +29,7 @@ Buffer::Buffer(const uint8_t * data, size_t data_length) void Buffer::load_empty_buffer() { - m_gap_buffer = std::make_shared(Encoding::UTF_8); + m_gap_buffer = std::make_shared(); m_eol_at_eof = true; m_line_endings = LineEndings::LF; } @@ -88,7 +88,7 @@ void Buffer::load_text_in_buffer(uint8_t * buffer, size_t buffer_size, size_t da size_t loaded_size; text_loader.load_buffer(buffer, data_length, &loaded_size); - m_gap_buffer = std::make_shared(buffer, buffer_size, loaded_size, text_loader.get_encoding()); + m_gap_buffer = std::make_shared(buffer, buffer_size, loaded_size); m_encoding = text_loader.get_encoding(); m_eol_at_eof = text_loader.get_eol_at_eof(); m_line_endings = text_loader.get_line_endings(); diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index 8dd2d4e..9cfbcfc 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -2,23 +2,21 @@ #include "System.h" #include -GapBuffer::GapBuffer(Encoding::Type encoding) +GapBuffer::GapBuffer() { m_buffer = (uint8_t *)System::alloc_pages(1u); m_buffer_size = System::page_size; m_size = 0u; m_gap_position = 0u; - m_encoding = encoding; tabstop = 4u; } -GapBuffer::GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size, Encoding::Type encoding) +GapBuffer::GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size) { m_buffer = buffer; m_buffer_size = buffer_size; m_size = size; m_gap_position = size; - m_encoding = encoding; tabstop = 4u; } @@ -40,15 +38,16 @@ void GapBuffer::compact() * Insert code_point into the gap buffer at position position. * * @param position Position in the gap buffer to insert the code point at. - * @param code_point The code point to insert. + * @param data Pointer to the data to insert. + * @param length Length of the data to insert. */ -void GapBuffer::insert(size_t position, uint32_t code_point) +void GapBuffer::insert(size_t position, const uint8_t * data, size_t length) { - check_grow(); + ensure_free(length); move_gap(position); - size_t size = Encoding::encode(code_point, m_encoding, &m_buffer[m_gap_position]); - m_gap_position += size; - m_size += size; + memcpy(&m_buffer[m_gap_position], data, length); + m_gap_position += length; + m_size += length; } /** @@ -67,11 +66,11 @@ std::string GapBuffer::get_string() * Verify that there is enough free space in the gap, and if not, grow the gap * and move data. */ -void GapBuffer::check_grow() +void GapBuffer::ensure_free(size_t length) { - if (gap_size() < Encoding::MAX_CODE_POINT_SIZE) + if (gap_size() < length) { - /* We're out of space. Allocate more and move. */ + /* We're out of space. Allocate more and move data. */ size_t new_size = (m_buffer_size + (128u * 1024u)) & System::page_base_mask; size_t new_num_pages = new_size >> System::page_size_log; uint8_t * new_buffer = (uint8_t *)System::alloc_pages(new_num_pages); diff --git a/src/core/GapBuffer.h b/src/core/GapBuffer.h index a61278b..8fd6a03 100644 --- a/src/core/GapBuffer.h +++ b/src/core/GapBuffer.h @@ -3,7 +3,6 @@ #include #include -#include "Encoding.h" #include #include #include @@ -13,8 +12,8 @@ class GapBuffer public: uint8_t tabstop; - GapBuffer(Encoding::Type encoding); - GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size, Encoding::Type encoding); + GapBuffer(); + GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size); ~GapBuffer(); size_t buffer_size() const { return m_buffer_size; } size_t size() const { return m_size; } @@ -31,18 +30,16 @@ public: } size_t gap_size() const { return m_buffer_size - m_size; } void compact(); - void insert(size_t position, uint32_t code_point); + void insert(size_t position, const uint8_t * data, size_t length); std::string get_string(); - Encoding::Type encoding() { return m_encoding; } protected: uint8_t * m_buffer; size_t m_buffer_size; size_t m_size; size_t m_gap_position; - Encoding::Type m_encoding; - void check_grow(); + void ensure_free(size_t length); void move_gap(size_t position); };