add Span class and use it from TextLoader
This commit is contained in:
parent
b64b9bed23
commit
cdae9e51ae
@ -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();
|
||||
|
18
src/core/Span.h
Normal file
18
src/core/Span.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef SPAN_H
|
||||
#define SPAN_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct Span
|
||||
{
|
||||
uint8_t * start;
|
||||
uint32_t length;
|
||||
|
||||
Span(uint8_t * _start, uint32_t _length)
|
||||
: start(_start),
|
||||
length(_length)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
@ -17,27 +17,27 @@ TextLoader::TextLoader()
|
||||
*/
|
||||
void TextLoader::load_buffer(uint8_t * buffer, size_t size)
|
||||
{
|
||||
LineIndexPairListRef lines[LINE_ENDING_COUNT];
|
||||
std::shared_ptr<std::list<Span>> 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<LineIndexPairList>();
|
||||
lines[i] = std::make_shared<std::list<Span>>();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#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<uint8_t *, uint32_t> LineIndexPair;
|
||||
typedef std::list<LineIndexPair> LineIndexPairList;
|
||||
typedef std::shared_ptr<LineIndexPairList> LineIndexPairListRef;
|
||||
|
||||
protected:
|
||||
int m_line_endings;
|
||||
bool m_eol_at_eof;
|
||||
LineIndexPairListRef m_lines;
|
||||
std::shared_ptr<std::list<Span>> m_lines;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -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<Span>::iterator & it)
|
||||
{
|
||||
return string((char *)it->first, it->second);
|
||||
return string((char *)it->start, it->length);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, num_lines_defaults_to_0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user