From 0b825562a1da4930973f5e6065e46f77a7d144ce Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 20 Nov 2018 18:36:45 -0500 Subject: Add bufferDelete --- bin/edi/buffer.c | 16 ++++++++++++++++ bin/edi/edi.h | 1 + 2 files changed, 17 insertions(+) diff --git a/bin/edi/buffer.c b/bin/edi/buffer.c index e9422951..62f55bee 100644 --- a/bin/edi/buffer.c +++ b/bin/edi/buffer.c @@ -66,6 +66,12 @@ void bufferAppend(struct Buffer *buf, wchar_t ch) { buf->slice.len++; } +void bufferDelete(struct Buffer *buf) { + if (!buf->slice.len) return; + buf->slice.len--; + buf->len--; +} + wchar_t *bufferDest(struct Buffer *buf, size_t len) { if (buf->len + len > buf->cap) { while (len > buf->cap) buf->cap *= 2; @@ -115,6 +121,16 @@ int main() { assert(!wcsncmp(L"ABCDEF", buf.slice.ptr, buf.slice.len)); bufferFree(&buf); + buf = bufferAlloc(4); + bufferInsert(&buf); + bufferAppend(&buf, L'A'); + bufferAppend(&buf, L'B'); + bufferDelete(&buf); + assert(!wcsncmp(L"A", buf.slice.ptr, buf.slice.len)); + bufferAppend(&buf, L'C'); + assert(!wcsncmp(L"AC", buf.slice.ptr, buf.slice.len)); + bufferFree(&buf); + buf = bufferAlloc(4); wchar_t *dest = bufferDest(&buf, 2); diff --git a/bin/edi/edi.h b/bin/edi/edi.h index 9b300739..4e127b94 100644 --- a/bin/edi/edi.h +++ b/bin/edi/edi.h @@ -44,6 +44,7 @@ struct Buffer bufferAlloc(size_t cap); void bufferFree(struct Buffer *buf); void bufferInsert(struct Buffer *buf); void bufferAppend(struct Buffer *buf, wchar_t ch); +void bufferDelete(struct Buffer *buf); wchar_t *bufferDest(struct Buffer *buf, size_t len); struct Table { -- cgit 1.4.1