From: Sebastian Harl Date: Thu, 5 Dec 2013 08:09:41 +0000 (+0100) Subject: util strbuf: Let skip() support an offset. X-Git-Tag: sysdb-0.1.0~330 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3a48bac5a41bff96412b1ac8f15bdc97cb9e28be;p=sysdb.git util strbuf: Let skip() support an offset. That is, add support for removing bytes from the middle of the buffer. --- diff --git a/src/frontend/connection.c b/src/frontend/connection.c index dbc0f4e..096a759 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -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 */ diff --git a/src/include/utils/strbuf.h b/src/include/utils/strbuf.h index 9577a64..900a453 100644 --- a/src/include/utils/strbuf.h +++ b/src/include/utils/strbuf.h @@ -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: diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c index 77d19f0..6190e3a 100644 --- a/src/utils/strbuf.c +++ b/src/utils/strbuf.c @@ -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 */ diff --git a/t/utils/strbuf_test.c b/t/utils/strbuf_test.c index d76c0f2..d455c0e 100644 --- a/t/utils/strbuf_test.c +++ b/t/utils/strbuf_test.c @@ -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,