diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-03-09 00:09:11 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-03-09 00:39:25 +0100 |
commit | 15551604c8eebf9404df561c7c6139dcf1d6a6a9 (patch) | |
tree | a1162dd290431b29cba549c2dd90e8967541e25a /src/piece-table.c | |
parent | 13e3354fff61c37555432d260a9b5d94ded4dd65 (diff) |
src: piece-table: Add insert + tests for it
Diffstat (limited to 'src/piece-table.c')
-rw-r--r-- | src/piece-table.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/piece-table.c b/src/piece-table.c index 6db05d7..2e7f0a2 100644 --- a/src/piece-table.c +++ b/src/piece-table.c @@ -138,16 +138,37 @@ static piece *split_piece(piece *p, size_t pos) { return p; } -void insert_piece_table(piece_table *pt, char val, size_t pos) { - /* At the moment inserts only one char */ - // piece *cur; - assert(pos <= pt->length ); +void insert_piece_table(piece_table *pt, size_t pos, char *in, size_t in_len) { + /* TODO: Make sure it doesn't screw up the cache */ + piece *start, *end, *new; - append_growable_buffer(&pt->add, &val, 1); - // TODO + assert( pos <= pt->length ); + + append_growable_buffer(&pt->add, in, in_len); + + if ( pos == 0 ) { + start = pt->sentinel; + } else if ( pos == pt->length ) { + start = pt->sentinel->prev; + } else { + find_piece_by_pos(pt, pos); + start = split_piece(pt->cached, pos - pt->cached_offset); + } + end = start->next; + + new = make_piece(); + new->start = pt->add.content + pt->add.used - in_len; + new->length = in_len; + + start->next = new; + new->prev = start; + new->next = end; + end->prev = new; + pt->length += in_len; } void delete_piece_table(piece_table *pt, size_t pos, size_t len) { + /* TODO: Make sure it doesn't screw up the cache */ piece *cur, *next, *start, *end; assert( pos + len <= pt->length ); |