Skip to content

Commit

Permalink
buffer: add buffer_remove implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Aug 20, 2016
1 parent 2b4a550 commit c7df560
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ bool buffer_put0(Buffer *buf, const char *data) {
return buffer_put(buf, data, strlen(data)+1);
}

bool buffer_remove(Buffer *buf, size_t pos, size_t len) {
if (len == 0)
return true;
if (pos + len > buf->len)
return false;
memmove(buf->data + pos, buf->data + pos + len, buf->len - pos - len);
buf->len -= len;
return true;
}

bool buffer_insert(Buffer *buf, size_t pos, const void *data, size_t len) {
if (pos > buf->len)
return false;
Expand Down
2 changes: 2 additions & 0 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ bool buffer_terminate(Buffer*);
bool buffer_put(Buffer*, const void *data, size_t len);
/* same but with NUL-terminated data */
bool buffer_put0(Buffer*, const char *data);
/* remove len bytes from the buffer starting at pos */
bool buffer_remove(Buffer*, size_t pos, size_t len);
/* insert arbitrary data of length len at pos (in [0, buf->len]) */
bool buffer_insert(Buffer*, size_t pos, const void *data, size_t len);
/* insert NUL-terminate data at pos (in [0, buf->len]) */
Expand Down

0 comments on commit c7df560

Please sign in to comment.