summary refs log tree commit diff
path: root/bin/edi/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bin/edi/buffer.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/bin/edi/buffer.c b/bin/edi/buffer.c
index a7ff74c9..311d88c0 100644
--- a/bin/edi/buffer.c
+++ b/bin/edi/buffer.c
@@ -73,11 +73,6 @@ void bufferDelete(struct Buffer *buf) {
 
 wchar_t *bufferDest(struct Buffer *buf, size_t len) {
 	if (buf->len + len > buf->cap) {
-		if (!buf->len) {
-			struct Block *empty = buf->block;
-			buf->block = buf->block->prev;
-			free(empty);
-		}
 		while (len > buf->cap) buf->cap *= 2;
 		buf->block = blockAlloc(buf->block, buf->cap);
 		buf->len = 0;
@@ -89,6 +84,12 @@ wchar_t *bufferDest(struct Buffer *buf, size_t len) {
 	return ptr;
 }
 
+void bufferTruncate(struct Buffer *buf, size_t len) {
+	if (len > buf->slice.len) return;
+	buf->len -= buf->slice.len - len;
+	buf->slice.len = len;
+}
+
 #ifdef TEST
 #include <assert.h>
 
@@ -159,6 +160,8 @@ int main() {
 	dest[4] = L'E';
 	dest[5] = L'F';
 	assert(!wcsncmp(L"ABCDEF", buf.slice.ptr, buf.slice.len));
+	bufferTruncate(&buf, 4);
+	assert(!wcsncmp(L"ABCD", buf.slice.ptr, buf.slice.len));
 	bufferFree(&buf);
 }