Skip to content

Commit

Permalink
Remove prependf
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomorain committed Jan 2, 2015
1 parent fa73193 commit 2374425
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 66 deletions.
65 changes: 12 additions & 53 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<length; i++) {
int next = (i + n) % length;
buf_swap(self, i, next);
}
}

/*
* Prepend a printf-style formatted string to the buffer.
*/
int buffer_prependf(buffer_t *self, const char *format, ...) {
const int initial_length = buffer_length(self);
va_list ap;
va_start(ap, format);
int result = buffer_appendfv(self, format, ap);
va_end(ap);

// a b c d e 1 2 3
// 1 2 3 a b c d e
if (result != -1) {
buf_rotate(self, initial_length);
}

return result;
return (bytes_formatted < 0) ? -1 : 0;
}


/*
* Append `str` to `self` and return 0 on success, -1 on failure.
*/
Expand Down
3 changes: 0 additions & 3 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ buffer_free(buffer_t *self);
int
buffer_prepend(buffer_t *self, char *str);

int
buffer_prependf(buffer_t *self, const char *format, ...);

int
buffer_append(buffer_t *self, const char *str);

Expand Down
13 changes: 3 additions & 10 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,13 @@ test_buffer_equals() {
}

void test_buffer_formatting() {

buffer_t *buf = buffer_new_with_copy("123");
buffer_prependf(buf, "abcde");
equal("abcde123", buffer_string(buf));
buffer_free(buf);

//buffer_t *
buf = buffer_new();
buffer_t *buf = buffer_new();
int result = buffer_appendf(buf, "%d %s", 3, "cow");
assert(0 == result);
equal("3 cow", buffer_string(buf));
result = buffer_prependf(buf, "0x%08X - ", 0xdeadbeef);
result = buffer_appendf(buf, " - 0x%08X", 0xdeadbeef);
assert(0 == result);
equal("0xDEADBEEF - 3 cow", buffer_string(buf));
equal("3 cow - 0xDEADBEEF", buffer_string(buf));
buffer_free(buf);
}

Expand Down

0 comments on commit 2374425

Please sign in to comment.