diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-03-04 17:54:30 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-03-04 17:54:30 +0100 |
commit | 9c921269e69a10c0bdde189f1e4b4fd226647495 (patch) | |
tree | 9877d45c0a00111d435319e58ce13cabc0433ff4 /src | |
parent | b07f6fdef77b6ffe12f7e5993bc2379b09b1123e (diff) |
piece-table: split better
Diffstat (limited to 'src')
-rw-r--r-- | src/piece-table.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/piece-table.c b/src/piece-table.c index 5f2b171..e869ae2 100644 --- a/src/piece-table.c +++ b/src/piece-table.c @@ -119,17 +119,23 @@ char index_piece_table(piece_table *pt, size_t pos) { } -static void split_piece(piece *p, size_t pos) { +static piece *split_piece(piece *p, size_t pos) { + /* Returns the first piece */ + piece *second; assert(pos < p->length); assert(p->start != NULL); /* Not a sentinel piece */ assert(p->start != 0); /* Not an empty piece (they should not exist) */ - piece *second = make_piece(); + if ( pos == 0 ) { + return p->prev; + } + second = make_piece(); second->length = p->length - pos; p->length = pos; second->next = p->next; p->next = second; second->prev = p; second->start = p->start + pos; + return p; } void insert_piece_table(piece_table *pt, char val, size_t pos) { @@ -149,16 +155,14 @@ void delete_piece_table(piece_table *pt, size_t pos, size_t len) { start = pt->sentinel; } else { find_piece_by_pos(pt, pos); - split_piece(pt->cached, pos - pt->cached_offset); - start = pt->cached; + start = split_piece(pt->cached, pos - pt->cached_offset); } if (pos + len == pt->length) { end = pt->sentinel; } else { find_piece_by_pos(pt, pos + len); - split_piece(pt->cached, pos + len - pt->cached_offset); - end = pt->cached->next; + end = split_piece(pt->cached, pos + len - pt->cached_offset)->next; } /* Free unneeded pieces */ |