summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c58c4ed)
raw | patch | inline | side by side (parent: c58c4ed)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 31 Oct 2013 17:56:06 +0000 (18:56 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 31 Oct 2013 17:56:06 +0000 (18:56 +0100) |
This function may be used to remove bytes and the beginning of the buffer.
src/include/utils/strbuf.h | patch | blob | history | |
src/utils/strbuf.c | patch | blob | history | |
t/utils/strbuf_test.c | patch | blob | history |
index ebbd9e3afff3dcc4bf143afa6019114c7cb4038f..af4a9ccd0251326386bb5ed2d946f6d5b55ecf66 100644 (file)
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 bffcfc633c1b6669d391627ad67526d01e61c28f..2b64480c55fc5960c816f83ef3a6b3bf626c1faf 100644 (file)
--- a/src/utils/strbuf.c
+++ b/src/utils/strbuf.c
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 ad04b87e66a0f617812b7be96638f9f0155ebd9f..db392faad45bbeeb95a9f946be0b1bafbf4088af 100644 (file)
--- a/t/utils/strbuf_test.c
+++ b/t/utils/strbuf_test.c
}
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;
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);