piece-chain

Research & implementation of a Piece Chain
git clone git://src.gearsix.net/piece-chain.git
Log | Files | Refs | Atom | README

commit 69a77b1f16268e013b48a324d3d831494eb84288
parent 7cc429d267d5ed23fd26750965873449f07185cb
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  5 Jul 2022 14:34:18 +0100

started work on bufdel

Diffstat:
Mbuf.c | 18+++++++++++++++++-
Mbuf.h | 3+++
Mtest.c | 44++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/buf.c b/buf.c @@ -124,7 +124,7 @@ size_t bufins(Buf *b, size_t pos, const char *s) p->f = b->append; p->off = ftell(b->append); p->len = slen; -/* p->undo = ?? */ + p->undo = NULL; p->redo = NULL; p->prev = b->pos; p->next = b->pos->next; @@ -143,3 +143,19 @@ size_t bufins(Buf *b, size_t pos, const char *s) return (p == 0) ? b->size : (b->size += slen); } + +size_t bufdel(Buf *b, size_t pos, size_t num) +{ + Piece *pre, *post; + size_t end = pos+num; + + pre = bufidx(b, pos); + post = bufidx(b, (end > b->size) ? b->size : end)->next; + if (!post) post = b->head; + + pre->next = post; + post->prev = pre; + + b->idx = pos; + return (b->size -= num); +} diff --git a/buf.h b/buf.h @@ -20,3 +20,5 @@ Buf *bufinit(const char *fpath); Piece *bufidx(Buf *b, size_t pos); size_t bufins(Buf *b, size_t pos, const char *s); + +size_t bufdel(Buf *b, size_t pos, size_t num); +\ No newline at end of file diff --git a/test.c b/test.c @@ -34,6 +34,7 @@ void print(Buf *b) puts(""); } while ((p = p->next)); printf("\n"); + fflush(stdout); } void test_bufinit() @@ -78,6 +79,7 @@ void test_bufins() assert(b->size == strlen(INBUF) + len); assert(b->idx == idx + len); assert(b->pos == b->tail->next->next); + assert(b->pos->f == b->read); assert(b->pos->off == 2); assert(b->pos->len == 3); @@ -87,12 +89,54 @@ void test_bufins() assert(b->pos->next == b->head); } +void test_bufdel() +{ + const size_t idx = 3, len = 9; + bufdel(b, idx, len); + + assert(b->size == strlen(INBUF)+1-len); /* +1 for bufins */ + assert(b->idx == idx); + assert(b->pos == b->head); + assert(b->tail->next->next->next == b->pos); + + assert(b->pos->f == b->read); + assert(b->pos->off == 11); + assert(b->pos->len == 0); + assert(b->pos->undo == NULL); + assert(b->pos->redo == NULL); + assert(b->pos->prev == b->tail->next->next); + assert(b->pos->next == NULL); +} + +void test_bufins1() +{ + const char *buf = " buddy!"; + const size_t idx = 3, len = strlen(buf), bsiz = b->size; + bufins(b, idx, buf); + + print(b); + assert(b->size == bsiz + len); + assert(b->idx == idx + len); + assert(b->pos == b->head); + assert(b->pos == b->tail->next->next->next); + + assert(b->pos->f == b->append); + assert(b->pos->off == 1); + assert(b->pos->len == len); + assert(b->pos->undo == NULL); + assert(b->pos->redo == NULL); + assert(b->pos->prev == b->tail->next->next->next); + assert(b->pos->next == NULL); +} + int main() { setup(); test_bufinit(); test_bufidx(); test_bufins(); + test_bufdel(); + test_bufins1(); puts("success - no assertions failed"); return 0; }