From 30a0ddb705678d512185e359831479a6b3567147 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 18 Jun 2014 16:01:34 -0400 Subject: strbuf: add xstrfmt helper You can use a strbuf to build up a string from parts, and then detach it. In the general case, you might use multiple strbuf_add* functions to do the building. However, in many cases, a single strbuf_addf is sufficient, and we end up with: struct strbuf buf = STRBUF_INIT; ... strbuf_addf(&buf, fmt, some, args); str = strbuf_detach(&buf, NULL); We can make this much more readable (and avoid introducing an extra variable, which can clutter the code) by introducing a convenience function: str = xstrfmt(fmt, some, args); Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- strbuf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index ac62982e67..12c78656ca 100644 --- a/strbuf.c +++ b/strbuf.c @@ -600,3 +600,22 @@ char *xstrdup_tolower(const char *string) result[i] = '\0'; return result; } + +char *xstrvfmt(const char *fmt, va_list ap) +{ + struct strbuf buf = STRBUF_INIT; + strbuf_vaddf(&buf, fmt, ap); + return strbuf_detach(&buf, NULL); +} + +char *xstrfmt(const char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start(ap, fmt); + ret = xstrvfmt(fmt, ap); + va_end(ap); + + return ret; +} -- cgit v1.2.3