From 8273f6e5a549f732f82d052283a11e781fb45cf3 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Wed, 9 Jan 2019 15:14:02 +0100 Subject: [PATCH] strbuf: introduce strbuf_fmt helper This helper completely replace the contents of a strbuf with its content. This is useful when building temporary strings in the shader source generation code, and it a bit semantically cleaner (and more efficient) than appending to an empty buffer. Signed-off-by: Erik Faye-Lund Reviewed-By: Gert Wollny --- src/vrend_strbuf.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/vrend_strbuf.h b/src/vrend_strbuf.h index fd7fb18..629c884 100644 --- a/src/vrend_strbuf.h +++ b/src/vrend_strbuf.h @@ -136,6 +136,29 @@ static inline void strbuf_appendf(struct vrend_strbuf *sb, const char *fmt, ...) va_end(va); } +static inline void strbuf_vfmt(struct vrend_strbuf *sb, const char *fmt, va_list ap) +{ + va_list cp; + va_copy(cp, ap); + + int len = vsnprintf(sb->buf, sb->alloc_size, fmt, ap); + if (len >= (int)(sb->alloc_size)) { + if (!strbuf_grow(sb, len)) + return; + vsnprintf(sb->buf, sb->alloc_size, fmt, cp); + } + sb->size = len; +} + +__attribute__((format(printf, 2, 3))) +static inline void strbuf_fmt(struct vrend_strbuf *sb, const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + strbuf_vfmt(sb, fmt, va); + va_end(va); +} + struct vrend_strarray { int num_strings; int num_alloced_strings;