Code

util strbuf: Let skip() support an offset.
authorSebastian Harl <sh@tokkee.org>
Thu, 5 Dec 2013 08:09:41 +0000 (09:09 +0100)
committerSebastian 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
src/include/utils/strbuf.h
src/utils/strbuf.c
t/utils/strbuf_test.c

index dbc0f4e709081a339d2e4ba23aada92e82f412f1..096a7590444c3ef467372f606ee70dda9cdbbe72 100644 (file)
@@ -204,7 +204,7 @@ command_handle(sdb_conn_t *conn)
 
        /* 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;
@@ -224,7 +224,7 @@ command_init(sdb_conn_t *conn)
        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)
@@ -139,10 +139,10 @@ sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
 
 /*
  * 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:
index 77d19f044d71a1fec89ec629877d0deda5b3585d..6190e3a8a045a650c26067a0d637003bf321ded9 100644 (file)
@@ -283,19 +283,30 @@ sdb_strbuf_chomp(sdb_strbuf_t *strbuf)
 } /* 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 */
index d76c0f20adcdb9556ae8fa6910ea310cdf661ffa..d455c0ec1c0666499664243240dc9430ad9ece4b 100644 (file)
@@ -62,8 +62,8 @@ START_TEST(test_empty)
 
        /* 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,
@@ -389,7 +389,7 @@ START_TEST(test_sdb_strbuf_skip)
                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,