summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d3416a7)
raw | patch | inline | side by side (parent: d3416a7)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 5 Dec 2013 08:09:41 +0000 (09:09 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 5 Dec 2013 08:09:41 +0000 (09:09 +0100) |
That is, add support for removing bytes from the middle of the buffer.
src/frontend/connection.c | patch | blob | history | |
src/include/utils/strbuf.h | patch | blob | history | |
src/utils/strbuf.c | patch | blob | history | |
t/utils/strbuf_test.c | patch | blob | history |
index dbc0f4e709081a339d2e4ba23aada92e82f412f1..096a7590444c3ef467372f606ee70dda9cdbbe72 100644 (file)
/* remove the command from the buffer */
if (conn->cmd_len)
- sdb_strbuf_skip(conn->buf, conn->cmd_len);
+ sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
conn->cmd = CONNECTION_IDLE;
conn->cmd_len = 0;
return status;
len = 2 * sizeof(uint32_t);
if (conn->cmd == CONNECTION_IDLE)
len += conn->cmd_len;
- sdb_strbuf_skip(conn->buf, len);
+ sdb_strbuf_skip(conn->buf, 0, len);
return 0;
} /* command_init */
index 9577a64f2956ce0a600ea9498adc485cf892f2c8..900a4530511020ae9c5d8374b065c943922c23ad 100644 (file)
/*
* sdb_strbuf_skip:
- * Removes the first 'n' bytes from the buffer.
+ * Removes 'n' bytes from the buffer starting at offset 'offset'.
*/
void
-sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t n);
+sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t offset, size_t n);
/*
* sdb_strbuf_string:
diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c
index 77d19f044d71a1fec89ec629877d0deda5b3585d..6190e3a8a045a650c26067a0d637003bf321ded9 100644 (file)
--- a/src/utils/strbuf.c
+++ b/src/utils/strbuf.c
} /* sdb_strbuf_chomp */
void
-sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t n)
+sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t offset, size_t n)
{
+ char *start;
+ size_t len;
+
if ((! strbuf) || (! n))
return;
- if (n >= strbuf->pos) {
- strbuf->string[0] = '\0';
- strbuf->pos = 0;
+ if (offset >= strbuf->pos)
+ return;
+
+ len = strbuf->pos - offset;
+
+ if (n >= len) {
+ strbuf->string[offset] = '\0';
+ strbuf->pos = offset;
return;
}
- assert(n < strbuf->pos);
- memmove(strbuf->string, strbuf->string + n, strbuf->pos - n);
+ assert(offset + n < strbuf->pos);
+ assert(offset < strbuf->pos);
+
+ start = strbuf->string + offset;
+ memmove(start, start + n, strbuf->pos - n);
strbuf->pos -= n;
strbuf->string[strbuf->pos] = '\0';
} /* sdb_strbuf_skip */
diff --git a/t/utils/strbuf_test.c b/t/utils/strbuf_test.c
index d76c0f20adcdb9556ae8fa6910ea310cdf661ffa..d455c0ec1c0666499664243240dc9430ad9ece4b 100644 (file)
--- a/t/utils/strbuf_test.c
+++ b/t/utils/strbuf_test.c
/* check that methods don't crash */
sdb_strbuf_destroy(b);
- sdb_strbuf_skip(b, 0);
- sdb_strbuf_skip(b, 10);
+ sdb_strbuf_skip(b, 0, 0);
+ sdb_strbuf_skip(b, 0, 10);
/* check that methods return an error */
fail_unless(sdb_strbuf_vappend(b, "test", ap) < 0,
size_t n;
sdb_strbuf_sprintf(buf, input);
- sdb_strbuf_skip(buf, skip_golden_data[i].n);
+ sdb_strbuf_skip(buf, 0, skip_golden_data[i].n);
n = sdb_strbuf_len(buf);
fail_unless(n == skip_golden_data[i].expected_len,