diff options
-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 */ |