Skip to content

Commit

Permalink
buffer: add functions to prepend data to an existing buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Oct 5, 2015
1 parent 0c17a55 commit 5c5fd62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ bool buffer_append0(Buffer *buf, const char *data) {
buf->len--;
return buffer_append(buf, data, strlen(data)) && buffer_append(buf, "\0", 1);
}

bool buffer_prepend(Buffer *buf, const void *data, size_t len) {
if (!buffer_grow(buf, buf->len + len))
return false;
memmove(buf->data + len, buf->data, buf->len);
memcpy(buf->data, data, len);
buf->len += len;
return true;
}

bool buffer_prepend0(Buffer *buf, const char *data) {
return buffer_prepend(buf, data, strlen(data) + (buf->len == 0));
}
3 changes: 3 additions & 0 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ bool buffer_append(Buffer*, const void *data, size_t len);
/* append NUl-terminated data */
bool buffer_append0(Buffer*, const char *data);

bool buffer_prepend(Buffer*, const void *data, size_t len);
bool buffer_prepend0(Buffer*, const char *data);

#endif

0 comments on commit 5c5fd62

Please sign in to comment.