remove number of characters from a Piece

This commit is contained in:
Josh Holtrop 2016-08-01 21:17:43 -04:00
parent b0553fdbd2
commit e913c15212
3 changed files with 10 additions and 15 deletions

View File

@ -58,8 +58,7 @@ bool Buffer::load_from_file(const char * filename)
auto next = it; auto next = it;
next++; next++;
bool eol = next != text_loader.end(); bool eol = next != text_loader.end();
/* TODO: correctly calculate n_chars based on current file encoding. */ piece_table->append_initial_line_piece(it->start, it->length, eol);
piece_table->append_initial_line_piece(it->start, it->length, it->length, eol);
} }
m_eol_at_eof = text_loader.get_eol_at_eof(); m_eol_at_eof = text_loader.get_eol_at_eof();

View File

@ -11,16 +11,16 @@ PieceTable::PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_si
m_piece_index = 2u; m_piece_index = 2u;
} }
void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol) void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, bool eol)
{ {
Piece * piece = add_piece(); Piece * piece = add_piece();
piece->prev = end_piece->prev; piece->prev = end_piece->prev;
piece->next = end_piece; piece->next = end_piece;
piece->start = start; piece->start = start;
piece->length = length; piece->length = length;
piece->n_chars = n_chars; piece->flags = 0u;
if (eol) if (eol)
piece->n_chars |= Piece::N_CHARS_EOL_FLAG; piece->flags |= Piece::EOL_FLAG;
end_piece->prev->next = piece; end_piece->prev->next = piece;
end_piece->prev = piece; end_piece->prev = piece;
m_num_lines++; m_num_lines++;

View File

@ -20,7 +20,7 @@ public:
{ {
enum enum
{ {
N_CHARS_EOL_FLAG = 0x80000000u, EOL_FLAG = 0x80000000u,
}; };
/** The previous piece in the chain. */ /** The previous piece in the chain. */
@ -36,19 +36,15 @@ public:
uint32_t length; uint32_t length;
/** /**
* Number of characters in the text. * Flags.
* Also, the highest bit is the eol flag.
*/ */
uint32_t n_chars; uint32_t flags;
/** Get the number of characters pointed to by this piece. */
uint32_t get_n_chars() { return n_chars & (~N_CHARS_EOL_FLAG); }
/** Get whether this piece is the end of a line. */ /** Get whether this piece is the end of a line. */
bool eol() { return (n_chars & N_CHARS_EOL_FLAG) != 0u; } bool eol() { return (flags & EOL_FLAG) != 0u; }
/** Toggle whether this piece is the end of a line. */ /** Toggle whether this piece is the end of a line. */
void toggle_eol() { n_chars ^= N_CHARS_EOL_FLAG; } void toggle_eol() { flags ^= EOL_FLAG; }
}; };
struct Cursor struct Cursor
@ -80,7 +76,7 @@ public:
Piece * start_piece; Piece * start_piece;
Piece * end_piece; Piece * end_piece;
void append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol); void append_initial_line_piece(uint8_t * start, uint32_t length, bool eol);
Piece * get_start_of_line(Piece * start) const; Piece * get_start_of_line(Piece * start) const;