summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-03-09 00:09:11 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-03-09 00:39:25 +0100
commit15551604c8eebf9404df561c7c6139dcf1d6a6a9 (patch)
treea1162dd290431b29cba549c2dd90e8967541e25a /src
parent13e3354fff61c37555432d260a9b5d94ded4dd65 (diff)
src: piece-table: Add insert + tests for it
Diffstat (limited to 'src')
-rw-r--r--src/piece-table.c33
-rw-r--r--src/piece-table.h2
2 files changed, 28 insertions, 7 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 );
diff --git a/src/piece-table.h b/src/piece-table.h
index 8a13ae3..b65da15 100644
--- a/src/piece-table.h
+++ b/src/piece-table.h
@@ -26,7 +26,7 @@ void init_piece_table(piece_table *pt, char *orig);
void free_piece_table(piece_table *pt);
char index_piece_table(piece_table *pt, size_t pos);
-void insert_piece_table(piece_table *pt, char val, size_t pos);
+void insert_piece_table(piece_table *pt, size_t pos, char *val, size_t len);
void delete_piece_table(piece_table *pt, size_t pos, size_t len);
void print_piece_table(piece_table *pt);