From 23744255b7862c86567a9a8819a1780a8ec3e654 Mon Sep 17 00:00:00 2001 From: Marc O'Morain Date: Fri, 2 Jan 2015 23:33:12 +0000 Subject: [PATCH] Remove prependf --- buffer.c | 65 +++++++++++--------------------------------------------- buffer.h | 3 --- test.c | 13 +++--------- 3 files changed, 15 insertions(+), 66 deletions(-) diff --git a/buffer.c b/buffer.c index ee89a35..94819f5 100644 --- a/buffer.c +++ b/buffer.c @@ -140,76 +140,35 @@ buffer_resize(buffer_t *self, size_t n) { return 0; } -int buffer_appendfv(buffer_t *self, const char* format, va_list ap) { - printf("Appending '%s' to '%s'\n", format, self->data); +/* + * Append a printf-style formatted string to the buffer. + */ +int buffer_appendf(buffer_t *self, const char *format, ...) { + va_list ap; + va_start(ap, format); const int initial_len = buffer_length(self); - /* - * First, we compute how many bytes are needed for the formatted string - * and allocate that much more space in the buffer. - */ + + // First, we compute how many bytes are needed for the formatted string + // and allocate that much more space in the buffer. va_list tmpa; va_copy(tmpa, ap); const int space_required = vsnprintf(NULL, 0, format, tmpa); va_end(tmpa); const int resized = buffer_resize(self, initial_len + space_required); if (resized == -1) { + va_end(ap); return -1; } + // Next format the string into the space that we have made room for. char* dst = self->data + initial_len; const int bytes_formatted = vsnprintf(dst, 1+space_required, format, ap); printf("Result '%s'\n", self->data); - return (bytes_formatted < 0) ? -1 : 0; -} - - -/* - * Append a printf-style formatted string to the buffer. - */ -int buffer_appendf(buffer_t *self, const char *format, ...) { - va_list ap; - va_start(ap, format); - int result = buffer_appendfv(self, format, ap); va_end(ap); - return result; -} - -static inline void buf_swap(buffer_t *self, int a, int b) { - const char tmp = self->data[a]; - self->data[a] = self->data[b]; - self->data[b] = tmp; -} - -static inline void buf_rotate(buffer_t *self, int n) { - int length = buffer_length(self); - printf("init: %d final: %d\n", n, length); - for (int i=0; i