#include "TextLoader.h" #include /** Create a TextLoader. */ TextLoader::TextLoader() { m_line_endings = LINE_ENDING_LF; m_lines = NULL; m_eol_at_eof = true; } /** * Scan text to detect line endings and record their positions. * * @param buffer Buffer containing the text to import. * @param size Size of the buffer. */ void TextLoader::load_buffer(uint8_t * buffer, size_t size) { LineIndexPairListRef 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(); } 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])); 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])); n_lf++; i++; line_start[LINE_ENDING_CRLF] = i + 1; } else { crlf = false; } } } else if (buffer[i] == '\n') { lines[LINE_ENDING_LF]->push_back(LineIndexPair(&buffer[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF])); crlf = false; n_lf++; line_start[LINE_ENDING_LF] = i + 1; } } if (crlf && (n_lf > 0u)) { m_line_endings = LINE_ENDING_CRLF; m_lines = lines[LINE_ENDING_CRLF]; } else if ((n_cr > 0u) && (n_lf == 0u)) { m_line_endings = LINE_ENDING_CR; m_lines = lines[LINE_ENDING_CR]; } else { m_line_endings = LINE_ENDING_LF; m_lines = lines[LINE_ENDING_LF]; } }