summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 61e96ed)
raw | patch | inline | side by side (parent: 61e96ed)
author | Sebastian Harl <sh@tokkee.org> | |
Sat, 10 Sep 2016 23:03:00 +0000 (19:03 -0400) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sat, 10 Sep 2016 23:03:00 +0000 (19:03 -0400) |
src/include/utils/strings.h | patch | blob | history | |
src/utils/strings.c | patch | blob | history | |
t/unit/utils/strings_test.c | patch | blob | history |
index 24f7fc11f8aa8f0bef5d7227c7c7e5d38200e11b..46666318ce45b4571cba160cd74325650e4cc551 100644 (file)
*/
int
stringv_append(char ***s, size_t *s_len, const char *elem);
+int
+stringv_append_if_missing(char ***s, size_t *s_len, const char *elem);
/*
* stringv_free:
diff --git a/src/utils/strings.c b/src/utils/strings.c
index b6a49e10ab36831d709f559cda7de4f5cfd8d79a..d7668f076a27fc087262420e0c84ac86a83308d3 100644 (file)
--- a/src/utils/strings.c
+++ b/src/utils/strings.c
return 0;
} /* stringv_append */
+int
+stringv_append_if_missing(char ***s, size_t *s_len, const char *elem)
+{
+ size_t i;
+
+ if ((! s) || (! s_len))
+ return -1;
+
+ for (i = 0; i < *s_len; i++)
+ if (! strcmp((*s)[i], elem))
+ return 0;
+
+ return stringv_append(s, s_len, elem);
+} /* stringv_append_if_missing */
+
void
stringv_free(char ***s, size_t *s_len)
{
index 326a073ce37cf878c27109e1ab5923ea1a378e45..3442b506050057f9c95a4de002cfe19fbc1322fb 100644 (file)
char **dst = NULL;
size_t dst_len = 0;
- char *src[] = { "a", "b", "c" };
+ char *src[] = { "a", "b", "c", "d", "e" };
size_t src_len = SDB_STATIC_ARRAY_LEN(src);
size_t i;
char buf1[256], buf2[256];
size_t j;
- check = stringv_append(&dst, &dst_len, src[i]);
+ if (i < 3) {
+ check = stringv_append(&dst, &dst_len, src[i]);
+ fail_unless(check == 0,
+ "stringv_append(<s>, <len>, '%s') = %d; expected: 0",
+ src[i], check);
+ fail_unless((dst != NULL) && (dst_len == i + 1),
+ "stringv_append(<s>, <len>, '%s') produced s=%p, len=%zu; "
+ "expected: s=<v>, len=%zu", src[i], dst, dst_len, i + 1);
+ }
+
+ /* A no-op for the first three iterations but then it appends the
+ * other elements. */
+ check = stringv_append_if_missing(&dst, &dst_len, src[i]);
fail_unless(check == 0,
- "stringv_append(<s>, <len>, '%s') = %d; expected: 0",
+ "stringv_append_if_missing(<s>, <len>, '%s') = %d; expected: 0",
src[i], check);
fail_unless((dst != NULL) && (dst_len == i + 1),
- "stringv_append(<s>, <len>, '%s') produced s=%p, len=%zu; "
+ "stringv_append_if_missing(<s>, <len>, '%s') produced s=%p, len=%zu; "
"expected: s=<v>, len=%zu", src[i], dst, dst_len, i + 1);
for (j = 0; j <= i; j++)