From 10e89c74386c57122f0eb1b4c91ed8a7163ba9dc Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 4 Apr 2013 17:41:18 +1030 Subject: [PATCH] Add strbuf_remaining() Improve some comments in strbuf.h --- strbuf.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/strbuf.h b/strbuf.h index 767d8983..c6a0c7a3 100644 --- a/strbuf.h +++ b/strbuf.h @@ -231,8 +231,8 @@ strbuf strbuf_reset(strbuf sb); * 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 * concatenations can be chained in a single line: eg, - * strbuf_ncat(strbuf_ncat(sb, "abc", 1), "bcd", 2) gives a strbuf containing - * "abc"; + * strbuf_ncat(strbuf_ncat(sb, "abc", 1), "def", 2) gives a strbuf containing + * "ade"; * * After these operations: * n = strbuf_len(sb); @@ -396,7 +396,8 @@ __STRBUF_INLINE size_t strbuf_is_empty(const_strbuf sb) { } -/** Return the size of the backing buffer. +/** Return the size of the backing buffer. Return -1 if the buffer is of + * undefined size. * * This is the same as the 'size' argument passed to the most recent * strbuf_init(). @@ -420,6 +421,17 @@ __STRBUF_INLINE size_t strbuf_len(const_strbuf sb) { } +/** Return remaining space in the strbuf, not counting the terminating nul. Return + * the maximum possible size_t value if the strbuf is of undefined size. + * + * Invariant: strbuf_size(sb) == -1 || strbuf_remaining(sb) == strbuf_size(sb) - strbuf_len(sb) + * + * @author Andrew Bettison + */ +__STRBUF_INLINE size_t strbuf_remaining(const_strbuf sb) { + return sb->end ? sb->end - strbuf_end(sb) : ~(size_t)0; +} + /** Return the number of chars appended to the strbuf so far, not counting the * terminating nul. *