diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 5ef1e24..c5225fd 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -8,6 +8,7 @@ Buffer::Buffer() { m_file_buffer = nullptr; m_file_buffer_size = 0u; + m_piece_table = std::make_shared(nullptr, 0u); } Buffer::~Buffer() @@ -52,6 +53,21 @@ bool Buffer::load_from_file(const char * filename) m_lines.push_back(std::make_shared(it->first, it->second)); } + m_piece_table = std::make_shared(m_file_buffer, m_file_buffer_size); + for (auto it = text_loader.begin(); it != text_loader.end(); it++) + { + auto pd_n = m_piece_table->add_piece_descriptor(); + PieceTable::PieceDescriptor & pd = pd_n.second; + pd.prev = m_piece_table->end_descriptor->prev; + pd.next = PIECE_DESCRIPTOR_INDEX_END; + pd.start = it->first - m_file_buffer; + pd.length = it->second; + pd.n_chars = it->second; /* TODO: fix this for other encodings */ + pd.eol = 1u; + (*m_piece_table)[m_piece_table->end_descriptor->prev].next = pd_n.first; + m_piece_table->end_descriptor->prev = pd_n.first; + } + return true; } diff --git a/src/core/Buffer.h b/src/core/Buffer.h index b6ad56a..8d3921c 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -4,6 +4,7 @@ #include #include #include "Text.h" +#include "PieceTable.h" class Buffer { @@ -30,6 +31,7 @@ protected: LinesType m_lines; uint8_t * m_file_buffer; unsigned long m_file_buffer_size; + std::shared_ptr m_piece_table; void free_file_buffer(); }; diff --git a/src/core/PieceTable.h b/src/core/PieceTable.h index ac4fda5..e2a0da0 100644 --- a/src/core/PieceTable.h +++ b/src/core/PieceTable.h @@ -3,6 +3,7 @@ #include #include "PagedBuffer.h" +#include #define PIECE_DESCRIPTOR_INDEX_START 0u #define PIECE_DESCRIPTOR_INDEX_END 1u @@ -30,9 +31,17 @@ public: PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size); - PieceDescriptor & add_piece_descriptor() + std::pair add_piece_descriptor() { - return m_piece_descriptors[m_piece_descriptor_index++]; + uint32_t index = m_piece_descriptor_index; + m_piece_descriptor_index++; + PieceDescriptor & pd = m_piece_descriptors[index]; + return std::pair(index, pd); + } + + PieceDescriptor & operator[](uint32_t index) + { + return m_piece_descriptors[index]; } PieceDescriptor * start_descriptor;