vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 69b2b5cdcd1b52d19243ff9160912bffe397341b parent 93b304d3ac3cea20b45c75ad7d2efca4478dc41d Author: Marc André Tanner <mat@brain-dump.org> Date: Sun, 3 Apr 2016 15:12:44 +0200 buffer: add buffer_content0 utility function Diffstat:
| M | buffer.c | | | 6 | ++++++ |
| M | buffer.h | | | 3 | +++ |
2 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/buffer.c b/buffer.c @@ -103,3 +103,9 @@ size_t buffer_length(Buffer *buf) { const char *buffer_content(Buffer *buf) { return buf->data; } + +const char *buffer_content0(Buffer *buf) { + if (buf->len == 0 || (buf->data[buf->len-1] != '\0' && !buffer_append(buf, "\0", 1))) + return ""; + return buf->data; +} diff --git a/buffer.h b/buffer.h @@ -42,6 +42,9 @@ bool buffer_prepend0(Buffer*, const char *data); size_t buffer_length0(Buffer*); /* return length of a buffer including possible NUL byte */ size_t buffer_length(Buffer*); +/* pointer to buffer data, guaranteed to return a NUL terminated + * string even if buffer is empty */ +const char *buffer_content0(Buffer*); /* pointer to buffer data, might be NULL if empty, might not be NUL terminated */ const char *buffer_content(Buffer*);