vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 9f16ef0b66770a19312200501086146912331d1e
parent 8ccfaadac06c6b597526691defe21fcc6d17e0b7
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 19 Sep 2015 16:11:27 +0200

buffer: add functions to prepend data to an existing buffer

Diffstat:
Mbuffer.c | 13+++++++++++++
Mbuffer.h | 3+++
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/buffer.c b/buffer.c @@ -62,3 +62,16 @@ bool buffer_append0(Buffer *buf, const char *data) { buf->len--; return buffer_append(buf, data, strlen(data)) && buffer_append(buf, "\0", 1); } + +bool buffer_prepend(Buffer *buf, const void *data, size_t len) { + if (!buffer_grow(buf, buf->len + len)) + return false; + memmove(buf->data + len, buf->data, buf->len); + memcpy(buf->data, data, len); + buf->len += len; + return true; +} + +bool buffer_prepend0(Buffer *buf, const char *data) { + return buffer_prepend(buf, data, strlen(data) + (buf->len == 0)); +} diff --git a/buffer.h b/buffer.h @@ -28,4 +28,7 @@ bool buffer_append(Buffer*, const void *data, size_t len); /* append NUl-terminated data */ bool buffer_append0(Buffer*, const char *data); +bool buffer_prepend(Buffer*, const void *data, size_t len); +bool buffer_prepend0(Buffer*, const char *data); + #endif