buffer: Add `git_buf_vprintf`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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);