diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 4978062..1d493fc 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -59,7 +59,7 @@ bool Buffer::load_from_file(const char * filename) next++; bool eol = next != text_loader.end(); /* TODO: correctly calculate n_chars based on current file encoding. */ - piece_table->append_initial_line_piece(it->first, it->second, it->second, eol); + piece_table->append_initial_line_piece(it->start, it->length, it->length, eol); } m_eol_at_eof = text_loader.get_eol_at_eof(); diff --git a/src/core/Span.h b/src/core/Span.h new file mode 100644 index 0000000..586d600 --- /dev/null +++ b/src/core/Span.h @@ -0,0 +1,18 @@ +#ifndef SPAN_H +#define SPAN_H + +#include + +struct Span +{ + uint8_t * start; + uint32_t length; + + Span(uint8_t * _start, uint32_t _length) + : start(_start), + length(_length) + { + } +}; + +#endif diff --git a/src/core/TextLoader.cc b/src/core/TextLoader.cc index b45b9b5..25934ce 100644 --- a/src/core/TextLoader.cc +++ b/src/core/TextLoader.cc @@ -17,27 +17,27 @@ TextLoader::TextLoader() */ void TextLoader::load_buffer(uint8_t * buffer, size_t size) { - LineIndexPairListRef lines[LINE_ENDING_COUNT]; + std::shared_ptr> lines[LINE_ENDING_COUNT]; size_t line_start[LINE_ENDING_COUNT] = {0}; unsigned int n_cr = 0; unsigned int n_lf = 0; bool crlf = true; for (size_t i = 0; i < LINE_ENDING_COUNT; i++) { - lines[i] = std::make_shared(); + lines[i] = std::make_shared>(); } for (size_t i = 0; i < size; i++) { if (buffer[i] == '\r') { - lines[LINE_ENDING_CR]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR])); + lines[LINE_ENDING_CR]->push_back(Span(&buffer[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR])); n_cr++; line_start[LINE_ENDING_CR] = i + 1; if (crlf) { if ((i < (size - 1)) && (buffer[i + 1] == '\n')) { - lines[LINE_ENDING_CRLF]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF])); + lines[LINE_ENDING_CRLF]->push_back(Span(&buffer[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF])); n_lf++; i++; line_start[LINE_ENDING_CRLF] = i + 1; @@ -50,7 +50,7 @@ void TextLoader::load_buffer(uint8_t * buffer, size_t size) } else if (buffer[i] == '\n') { - lines[LINE_ENDING_LF]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF])); + lines[LINE_ENDING_LF]->push_back(Span(&buffer[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF])); crlf = false; n_lf++; line_start[LINE_ENDING_LF] = i + 1; @@ -76,7 +76,7 @@ void TextLoader::load_buffer(uint8_t * buffer, size_t size) * the end of the file. */ if (line_start[m_line_endings] < size) { - m_lines->push_back(LineIndexPair(&buffer[line_start[m_line_endings]], size - line_start[m_line_endings])); + m_lines->push_back(Span(&buffer[line_start[m_line_endings]], size - line_start[m_line_endings])); m_eol_at_eof = false; } } diff --git a/src/core/TextLoader.h b/src/core/TextLoader.h index 7da57b3..241623b 100644 --- a/src/core/TextLoader.h +++ b/src/core/TextLoader.h @@ -4,6 +4,7 @@ #include #include #include +#include "Span.h" class TextLoader { @@ -34,14 +35,10 @@ public: auto end() { return m_lines->end(); } bool get_eol_at_eof() { return m_eol_at_eof; } - typedef std::pair LineIndexPair; - typedef std::list LineIndexPairList; - typedef std::shared_ptr LineIndexPairListRef; - protected: int m_line_endings; bool m_eol_at_eof; - LineIndexPairListRef m_lines; + std::shared_ptr> m_lines; }; #endif diff --git a/test/src/test_TextLoader.cc b/test/src/test_TextLoader.cc index 9f9866d..20d1bec 100644 --- a/test/src/test_TextLoader.cc +++ b/test/src/test_TextLoader.cc @@ -6,9 +6,9 @@ using namespace std; -static string line_to_string(const TextLoader::LineIndexPairList::iterator & it) +static string line_to_string(const std::list::iterator & it) { - return string((char *)it->first, it->second); + return string((char *)it->start, it->length); } TEST(TextLoaderTest, num_lines_defaults_to_0)