From 5cff81e0c04a1795424a665e6d6f908f155d4171 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Mon, 25 Jun 2012 16:31:19 +0930 Subject: [PATCH] Add strbuf_reset() --- strbuf.c | 8 +++++++- strbuf.h | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/strbuf.c b/strbuf.c index 34c01e79..d2d0da1b 100644 --- a/strbuf.c +++ b/strbuf.c @@ -26,8 +26,14 @@ static inline size_t min(size_t a, size_t b) { strbuf strbuf_init(strbuf sb, char *buffer, size_t size) { - sb->start = sb->current = buffer; + sb->start = buffer; sb->end = sb->start + size - 1; + return strbuf_reset(sb); +} + +strbuf strbuf_reset(strbuf sb) +{ + sb->current = sb->start; if (sb->start && sb->end >= sb->start) { *sb->start = '\0'; *sb->end = '\0'; // should never get overwritten diff --git a/strbuf.h b/strbuf.h index 0f975d3b..c73281e4 100644 --- a/strbuf.h +++ b/strbuf.h @@ -184,6 +184,20 @@ __STRBUF_INLINE strbuf strbuf_make(char *buffer, size_t size) { } +/** Reset a strbuf. The current position is set to the start of the buffer, so + * the next append will write at the start of the buffer. The prior contents + * of the buffer are forgotten and will be overwritten. + * + * Immediately following strbuf_reset(sb), the following properties hold: + * strbuf_len(sb) == 0 + * strbuf_count(sb) == 0 + * strbuf_str()[0] == '\0' + * + * @author Andrew Bettison + */ +strbuf strbuf_reset(strbuf sb); + + /** Append a null-terminated string to the strbuf up to a maximum number, * truncating if necessary to avoid buffer overrun, and terminating with a nul * which is not counted in the maximum. Return a pointer to the strbuf so that @@ -257,6 +271,7 @@ strbuf strbuf_putc(strbuf sb, char ch); int strbuf_sprintf(strbuf sb, const char *fmt, ...); int strbuf_vsprintf(strbuf sb, const char *fmt, va_list ap); + /** Return a pointer to the current null-terminated string in the strbuf. * * This is the same as the 'buffer' argument passed to the most recent