diff options
Diffstat (limited to 'dstring.c')
-rw-r--r-- | dstring.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -15,6 +15,9 @@ ****************************************************************************/ #include "dstring.h" +#include <assert.h> +#include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -117,6 +120,31 @@ int dstrapp(char **str, char c) { return dstrcat_impl(str, &c, 1); } +char *dstrprintf(const char *format, ...) { + va_list args; + + va_start(args, format); + int len = vsnprintf(NULL, 0, format, args); + va_end(args); + + assert(len > 0); + + char *str = dstralloc(len); + if (!str) { + return NULL; + } + + va_start(args, format); + len = vsnprintf(str, len + 1, format, args); + va_end(args); + + struct dstring *header = dstrheader(str); + assert(len == header->capacity); + header->length = len; + + return str; +} + void dstrfree(char *dstr) { if (dstr) { free(dstrheader(dstr)); |