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

View File

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