Code

utils strings: Add stringv_append_if_missing().
authorSebastian Harl <sh@tokkee.org>
Sat, 10 Sep 2016 23:03:00 +0000 (19:03 -0400)
committerSebastian Harl <sh@tokkee.org>
Sat, 10 Sep 2016 23:03:00 +0000 (19:03 -0400)
src/include/utils/strings.h
src/utils/strings.c
t/unit/utils/strings_test.c

index 24f7fc11f8aa8f0bef5d7227c7c7e5d38200e11b..46666318ce45b4571cba160cd74325650e4cc551 100644 (file)
@@ -58,6 +58,8 @@ stringv_copy(char ***dst, size_t *dst_len,
  */
 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:
index b6a49e10ab36831d709f559cda7de4f5cfd8d79a..d7668f076a27fc087262420e0c84ac86a83308d3 100644 (file)
@@ -114,6 +114,21 @@ stringv_append(char ***s, size_t *s_len, const char *elem)
        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)
@@ -44,7 +44,7 @@ START_TEST(test_stringv)
        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;
 
@@ -74,12 +74,24 @@ START_TEST(test_stringv)
                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++)