Subject: Re: [PATCH 2/8] strbuf: add strbuf_addv

From: Jonathan Nieder <[email-masked]>
Date: Thu, 24 Feb 2011 14:51:28 -0600

Jeff King wrote:

> 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_addv,
> the strbuf equivalent of vsprintf.

"strbuf_vaddf" would follow the vsnprintf scheme a little better.
(I think strbuf_addv is just as intuitive, though.)

Thanks, I was looking for something like this.

And apparently it has been a wish for 2 years now. :)  See
http://thread.gmane.org/gmane.comp.version-control.git/75250/focus=76396
for amusement.

>  5 files changed, 24 insertions(+), 63 deletions(-)

Nice.

> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -120,6 +120,7 @@ extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *
>  
>  __attribute__((format (printf,2,3)))
>  extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
> +extern void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap);

"__attribute__(format (printf,2,0))" could tell gcc to check that the format
string is well formed and give it a chance to carp if you try

	static inline void foo(struct strbuf *sb, const char *fmt, ...)
	{
		va_list ap;

		va_start(ap, fmt);
		strbuf_addv(sb, fmt, ap);
		va_end(ap);
	}

	...
		foo(some_user_supplied_string);
---
diff --git a/strbuf.h b/strbuf.h
index 65b44a0..0c788d5 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -120,6 +120,7 @@ extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *
 
 __attribute__((format (printf,2,3)))
 extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf,2,0)))
 extern void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap);
 
 extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);