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 <erik.faye-lund@collabora.com>
Reviewed-By: Gert Wollny <gert.wollny@collabora.com>
macos/master
Erik Faye-Lund 6 years ago
parent 2acbb410ce
commit 8273f6e5a5
  1. 23
      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;

Loading…
Cancel
Save