From 15551604c8eebf9404df561c7c6139dcf1d6a6a9 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 9 Mar 2024 00:09:11 +0100 Subject: src: piece-table: Add insert + tests for it --- src/piece-table.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src/piece-table.c') 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 ); -- cgit v1.2.3