Those are helpers to build functions that transform a buffer into a strbuf, allowing the "buffer" to be taken from the destination buffer. Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx> --- strbuf.c | 25 +++++++++++++++++++++++++ strbuf.h | 4 ++++ 2 files changed, 29 insertions(+), 0 deletions(-) diff --git a/strbuf.c b/strbuf.c index f4201e1..40b3f87 100644 --- a/strbuf.c +++ b/strbuf.c @@ -51,6 +51,31 @@ void strbuf_grow(struct strbuf *sb, size_t extra) ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); } +void *strbuf_start_filter(struct strbuf *sb, const char *src, ssize_t hint) +{ + if (src < sb->buf || src >= sb->buf + sb->alloc) { + if (hint > 0 && (size_t)hint >= sb->alloc) + strbuf_grow(sb, hint - sb->len); + return NULL; + } + + if (hint < 0) + return strbuf_detach(sb, NULL); + + if ((size_t)hint >= sb->alloc) { + void *tmp = strbuf_detach(sb, NULL); + strbuf_grow(sb, hint); + return tmp; + } + + return NULL; +} + +void strbuf_end_filter(void *p) +{ + free(p); +} + void strbuf_rtrim(struct strbuf *sb) { while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) diff --git a/strbuf.h b/strbuf.h index 9b9e861..640bca4 100644 --- a/strbuf.h +++ b/strbuf.h @@ -76,6 +76,10 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) { } #define strbuf_reset(sb) strbuf_setlen(sb, 0) +/*----- filter API -----*/ +extern void *strbuf_start_filter(struct strbuf *, const char *, ssize_t); +extern void strbuf_end_filter(void *p); + /*----- content related -----*/ extern void strbuf_rtrim(struct strbuf *); extern int strbuf_cmp(struct strbuf *, struct strbuf *); -- 1.5.3.4.208.gc1d91-dirty - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html