summaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-06-18 20:01:34 (GMT)
committerJunio C Hamano <gitster@pobox.com>2014-06-19 19:25:17 (GMT)
commit30a0ddb705678d512185e359831479a6b3567147 (patch)
treee3395a628ea1abeb3d47bee9591cf9e650523db0 /strbuf.h
parentcb682f8cfe63ecd0da08a526f404d295e51e3ab1 (diff)
downloadgit-30a0ddb705678d512185e359831479a6b3567147.zip
git-30a0ddb705678d512185e359831479a6b3567147.tar.gz
git-30a0ddb705678d512185e359831479a6b3567147.tar.bz2
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/strbuf.h b/strbuf.h
index e9ad03e..a594c24 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -187,4 +187,13 @@ extern int fprintf_ln(FILE *fp, const char *fmt, ...);
char *xstrdup_tolower(const char *);
+/*
+ * Create a newly allocated string using printf format. You can do this easily
+ * with a strbuf, but this provides a shortcut to save a few lines.
+ */
+__attribute__((format (printf, 1, 0)))
+char *xstrvfmt(const char *fmt, va_list ap);
+__attribute__((format (printf, 1, 2)))
+char *xstrfmt(const char *fmt, ...);
+
#endif /* STRBUF_H */