strbuf: add strbuf_vaddf

In a variable-args function, the code for writing into a strbuf is
non-trivial. We ended up cutting and pasting it in several places
because there was no vprintf-style function for strbufs (which in turn
was held up by a lack of va_copy).

Now that we have a fallback va_copy, we can add strbuf_vaddf, the
strbuf equivalent of vsprintf. And we can clean up the cut and paste
mess.

Signed-off-by: Jeff King <peff@peff.net>
Improved-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King
2011-02-25 23:08:53 -06:00
committed by Junio C Hamano
parent ab8632ae36
commit ebeb60900f
5 changed files with 25 additions and 63 deletions

View File

@ -137,7 +137,6 @@ static void flush_output(struct merge_options *o)
__attribute__((format (printf, 3, 4)))
static void output(struct merge_options *o, int v, const char *fmt, ...)
{
int len;
va_list ap;
if (!show(o, v))
@ -148,21 +147,9 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
va_start(ap, fmt);
len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
strbuf_vaddf(&o->obuf, fmt, ap);
va_end(ap);
if (len < 0)
len = 0;
if (len >= strbuf_avail(&o->obuf)) {
strbuf_grow(&o->obuf, len + 2);
va_start(ap, fmt);
len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
va_end(ap);
if (len >= strbuf_avail(&o->obuf)) {
die("this should not happen, your snprintf is broken");
}
}
strbuf_setlen(&o->obuf, o->obuf.len + len);
strbuf_add(&o->obuf, "\n", 1);
if (!o->buffer_output)
flush_output(o);