From: Jann Horn Date: Mon, 19 Aug 2013 20:51:50 +0000 (+0200) Subject: string: add TPRINTF macro for constructing temporary small strings X-Git-Url: http://git.thejh.net/?a=commitdiff_plain;h=3aef24fab6d9f51ebbe8094b79de895ff1cdce17;p=libjh.git string: add TPRINTF macro for constructing temporary small strings --- diff --git a/string.c b/string.c index b0db54c..f58fd3c 100644 --- a/string.c +++ b/string.c @@ -7,6 +7,16 @@ HEADER #include HEADER #define streq(a,b) (!strcmp((a),(b))) +HEADER #define TPRINTF(name, ...) \ +HEADER char *name; \ +HEADER do { \ +HEADER int __tprintf_size = snprintf(NULL, 0, __VA_ARGS__); \ +HEADER assert(__tprintf_size != -1); \ +HEADER name = alloca(__tprintf_size+1); \ +HEADER int __tprintf_size2 = snprintf(name, __tprintf_size+1, __VA_ARGS__); \ +HEADER assert(__tprintf_size == __tprintf_size2); \ +HEADER } while (0); ////////////////////////////////////////////////////////// + PUBLIC_FN int count_char_occurences(char *s, char c) { int n=0; while (*s) {