remove encoding from GapBuffer

This commit is contained in:
Josh Holtrop 2016-12-07 23:05:38 -05:00
parent 3784b89ca9
commit 5f3831965e
3 changed files with 18 additions and 22 deletions

View File

@ -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<GapBuffer>(Encoding::UTF_8);
m_gap_buffer = std::make_shared<GapBuffer>();
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<GapBuffer>(buffer, buffer_size, loaded_size, text_loader.get_encoding());
m_gap_buffer = std::make_shared<GapBuffer>(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();

View File

@ -2,23 +2,21 @@
#include "System.h"
#include <string.h>
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);

View File

@ -3,7 +3,6 @@
#include <stdint.h>
#include <stdlib.h>
#include "Encoding.h"
#include <memory>
#include <list>
#include <string>
@ -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);
};