Commit baaf1c47101acd0be28bbdeb2792c208d22b3a20

Vicent Martí 2012-05-02T23:44:22

buffer: Add `git_buf_vprintf`

diff --git a/src/buffer.c b/src/buffer.c
index 24a0abd..0785b53 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -146,17 +146,21 @@ int git_buf_puts(git_buf *buf, const char *string)
 	return git_buf_put(buf, string, strlen(string));
 }
 
-int git_buf_printf(git_buf *buf, const char *format, ...)
+int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
 {
 	int len;
-	va_list arglist;
 
-	ENSURE_SIZE(buf, buf->size + 1);
+	ENSURE_SIZE(buf, buf->size + (strlen(format) * 2));
 
 	while (1) {
-		va_start(arglist, format);
-		len = p_vsnprintf(buf->ptr + buf->size, buf->asize - buf->size, format, arglist);
-		va_end(arglist);
+		va_list args;
+		va_copy(args, ap);
+
+		len = p_vsnprintf(
+			buf->ptr + buf->size,
+			buf->asize - buf->size,
+			format, args
+		);
 
 		if (len < 0) {
 			git__free(buf->ptr);
@@ -175,6 +179,18 @@ int git_buf_printf(git_buf *buf, const char *format, ...)
 	return 0;
 }
 
+int git_buf_printf(git_buf *buf, const char *format, ...)
+{
+	int r;
+	va_list ap;
+
+	va_start(ap, format);
+	r = git_buf_vprintf(buf, format, ap);
+	va_end(ap);
+
+	return r;
+}
+
 void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf)
 {
 	size_t copylen;
diff --git a/src/buffer.h b/src/buffer.h
index 1cf588a..f15fdaa 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -76,6 +76,7 @@ int git_buf_putc(git_buf *buf, char c);
 int git_buf_put(git_buf *buf, const char *data, size_t len);
 int git_buf_puts(git_buf *buf, const char *string);
 int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
+int git_buf_vprintf(git_buf *buf, const char *format, va_list ap);
 void git_buf_clear(git_buf *buf);
 void git_buf_consume(git_buf *buf, const char *end);
 void git_buf_truncate(git_buf *buf, size_t len);