From 9fb079db93a6964c160439d851270b3433db59f5 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 31 Oct 2013 18:56:06 +0100 Subject: [PATCH] utils strbuf: Added sdb_strbuf_skip(). This function may be used to remove bytes and the beginning of the buffer. --- src/include/utils/strbuf.h | 7 +++++++ src/utils/strbuf.c | 18 ++++++++++++++++++ t/utils/strbuf_test.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/include/utils/strbuf.h b/src/include/utils/strbuf.h index ebbd9e3..af4a9cc 100644 --- a/src/include/utils/strbuf.h +++ b/src/include/utils/strbuf.h @@ -136,6 +136,13 @@ sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n); ssize_t sdb_strbuf_chomp(sdb_strbuf_t *strbuf); +/* + * sdb_strbuf_skip: + * Removes the first 'n' bytes from the buffer. + */ +void +sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t n); + /* * sdb_strbuf_string: * Returns the content of the string buffer. The caller may not modify the diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c index bffcfc6..2b64480 100644 --- a/src/utils/strbuf.c +++ b/src/utils/strbuf.c @@ -268,6 +268,24 @@ sdb_strbuf_chomp(sdb_strbuf_t *strbuf) return ret; } /* sdb_strbuf_chomp */ +void +sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t n) +{ + if ((! strbuf) || (! n)) + return; + + if (n >= strbuf->pos) { + strbuf->string[0] = '\0'; + strbuf->pos = 0; + return; + } + + assert(n < strbuf->pos); + memmove(strbuf->string, strbuf->string + n, strbuf->pos - n); + strbuf->pos -= n; + strbuf->string[strbuf->pos] = '\0'; +} /* sdb_strbuf_skip */ + const char * sdb_strbuf_string(sdb_strbuf_t *strbuf) { diff --git a/t/utils/strbuf_test.c b/t/utils/strbuf_test.c index ad04b87..db392fa 100644 --- a/t/utils/strbuf_test.c +++ b/t/utils/strbuf_test.c @@ -304,6 +304,43 @@ START_TEST(test_sdb_strbuf_chomp) } END_TEST +/* input is "1234567890" */ +static struct { + size_t n; + const char *expected; +} skip_golden_data[] = { + { 0, "1234567890" }, + { 1, "234567890" }, + { 2, "34567890" }, + { 9, "0" }, + { 10, "" }, + { 11, "" }, + { 100, "" }, +}; + +START_TEST(test_sdb_strbuf_skip) +{ + const char *input = "1234567890"; + size_t i; + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(skip_golden_data); ++i) { + const char *check; + + sdb_strbuf_sprintf(buf, input); + sdb_strbuf_skip(buf, skip_golden_data[i].n); + + check = sdb_strbuf_string(buf); + fail_unless(!!check, + "sdb_strbuf_string() = NULL (after skip); expected: string"); + + fail_unless(! strcmp(skip_golden_data[i].expected, check), + "sdb_strbuf_skip('%s', %zu) did not skip correctly; " + "got string '%s'; expected: '%s'", input, + skip_golden_data[i].n, check, skip_golden_data[i].expected); + } +} +END_TEST + static struct { const char *input; const char *expected; @@ -370,6 +407,7 @@ util_strbuf_suite(void) tcase_add_test(tc, test_sdb_strbuf_memcpy); tcase_add_test(tc, test_sdb_strbuf_memappend); tcase_add_test(tc, test_sdb_strbuf_chomp); + tcase_add_test(tc, test_sdb_strbuf_skip); tcase_add_test(tc, test_sdb_strbuf_string); tcase_add_test(tc, test_sdb_strbuf_len); suite_add_tcase(s, tc); -- 2.30.2