update PieceTable::Iterator to allow empty pieces

This commit is contained in:
Josh Holtrop 2016-10-25 20:36:08 -04:00
parent b9c23084be
commit 1f59e4d042
2 changed files with 48 additions and 24 deletions

View File

@ -2,11 +2,13 @@
PieceTable::PieceTable() PieceTable::PieceTable()
{ {
start_piece = &m_pieces[PIECE_INDEX_START]; m_piece_index = 0u;
end_piece = &m_pieces[PIECE_INDEX_END]; start_piece = add_piece();
end_piece = add_piece();
start_piece->length = 0u;
end_piece->length = 0u;
start_piece->next = end_piece; start_piece->next = end_piece;
end_piece->prev = start_piece; end_piece->prev = start_piece;
m_piece_index = 2u;
tabstop = 4u; tabstop = 4u;
m_append_buffer_index = 0u; m_append_buffer_index = 0u;
m_changes_index = 0u; m_changes_index = 0u;
@ -37,6 +39,10 @@ PieceTable::Iterator::Iterator(PieceTable * pt)
{ {
piece_table = pt; piece_table = pt;
piece = pt->start_piece->next; piece = pt->start_piece->next;
while ((piece->length == 0u) && (piece != piece_table->end_piece))
{
piece = piece->next;
}
offset = 0u; offset = 0u;
} }
@ -54,31 +60,46 @@ uint8_t PieceTable::Iterator::num_bytes_in_code_point() const
void PieceTable::Iterator::go_next_position() void PieceTable::Iterator::go_next_position()
{ {
uint8_t code_point_size = num_bytes_in_code_point(); if (valid())
if ((offset + code_point_size) >= piece->length)
{ {
go_next_piece(); uint8_t code_point_size = num_bytes_in_code_point();
return; if ((offset + code_point_size) >= piece->length)
{
do
{
piece = piece->next;
} while ((piece != piece_table->end_piece) &&
(piece->length == 0u));
offset = 0u;
return;
}
offset += code_point_size;
} }
offset += code_point_size;
} }
void PieceTable::Iterator::go_prev_position() void PieceTable::Iterator::go_prev_position()
{ {
if (offset > 0) if (piece != piece_table->start_piece)
{ {
offset = Encoding::beginning_of_code_point(piece_table->encoding, &piece->start[offset - 1u]) - piece->start; if (offset > 0)
}
else if (piece != piece_table->start_piece)
{
piece = piece->prev;
if (piece->length > 0u)
{ {
offset = Encoding::beginning_of_code_point(piece_table->encoding, &piece->start[piece->length - 1u]) - piece->start; offset = Encoding::beginning_of_code_point(piece_table->encoding, &piece->start[offset - 1u]) - piece->start;
} }
else else
{ {
offset = 0u; do
{
piece = piece->prev;
} while ((piece != piece_table->start_piece) &&
(piece->length == 0u));
if (piece->length > 0u)
{
offset = Encoding::beginning_of_code_point(piece_table->encoding, &piece->start[piece->length - 1u]) - piece->start;
}
else
{
offset = 0u;
}
} }
} }
} }
@ -99,7 +120,11 @@ void PieceTable::Iterator::go_next_piece()
{ {
if (piece != piece_table->end_piece) if (piece != piece_table->end_piece)
{ {
piece = piece->next; do
{
piece = piece->next;
} while ((piece != piece_table->end_piece) &&
(piece->length == 0u));
offset = 0u; offset = 0u;
} }
} }
@ -108,7 +133,11 @@ void PieceTable::Iterator::go_prev_piece()
{ {
if (piece != piece_table->start_piece) if (piece != piece_table->start_piece)
{ {
piece = piece->prev; do
{
piece = piece->prev;
} while ((piece != piece_table->start_piece) &&
(piece->length == 0u));
offset = 0u; offset = 0u;
} }
} }

View File

@ -11,11 +11,6 @@
class PieceTable class PieceTable
{ {
public: public:
enum
{
PIECE_INDEX_START = 0,
PIECE_INDEX_END = 1,
};
enum enum
{ {
INTERNAL_EOL = '\n', INTERNAL_EOL = '\n',