]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: introduce strv_extend_joined() and strv_extend_joined_with_size()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Oct 2025 13:19:31 +0000 (22:19 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Oct 2025 15:59:36 +0000 (00:59 +0900)
src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c

index 4f61a42114fdad69d7fb3409c6d1e8346f68d39d..a19e05edc3dfe34b31dd793b38ae54b30276d8b1 100644 (file)
@@ -921,6 +921,18 @@ int strv_extendf_with_size(char ***l, size_t *n, const char *format, ...) {
         return strv_consume_with_size(l, n, x);
 }
 
+int strv_extend_joined_with_size_sentinel(char ***l, size_t *n, ...) {
+        va_list ap;
+
+        va_start(ap, n);
+        char *x = strextendv_with_separator(/* x= */ NULL, /* separator=*/ NULL, ap);
+        va_end(ap);
+        if (!x)
+                return -ENOMEM;
+
+        return strv_consume_with_size(l, n, x);
+}
+
 char* startswith_strv(const char *s, char * const *l) {
         STRV_FOREACH(i, l) {
                 char *found = startswith(s, *i);
index 1133895158448359801acd5bf5fe9a42395c6c8e..7b0923a69832c757a1cd6f3926c4b06ba8088ba5 100644 (file)
@@ -59,6 +59,10 @@ int strv_extend_many_internal(char ***l, const char *value, ...);
 int strv_extendf_with_size(char ***l, size_t *n, const char *format, ...) _printf_(3,4);
 #define strv_extendf(l, ...) strv_extendf_with_size(l, NULL, __VA_ARGS__)
 
+int strv_extend_joined_with_size_sentinel(char ***l, size_t *n, ...) _sentinel_;
+#define strv_extend_joined_with_size(l, n, ...) strv_extend_joined_with_size_sentinel(l, n, __VA_ARGS__, NULL)
+#define strv_extend_joined(l, ...) strv_extend_joined_with_size(l, NULL, __VA_ARGS__)
+
 int strv_push_with_size(char ***l, size_t *n, char *value);
 static inline int strv_push(char ***l, char *value) {
         return strv_push_with_size(l, NULL, value);
index 49558850d618646741af0680060eb50d7113edb7..b3468b1cfac62e0b3ea369050079540faf863d2e 100644 (file)
@@ -762,6 +762,30 @@ TEST(strv_extendf_with_size) {
         ASSERT_STREQ(a[1], "test3 bar foo 128");
 }
 
+TEST(strv_extend_joined) {
+        _cleanup_strv_free_ char **a = NULL;
+
+        ASSERT_OK(strv_extend_joined(&a, "hoge"));
+        ASSERT_OK(strv_extend_joined(&a, "aaa", "bbb", "ccc"));
+
+        ASSERT_EQ(strv_length(a), 2u);
+        ASSERT_STREQ(a[0], "hoge");
+        ASSERT_STREQ(a[1], "aaabbbccc");
+}
+
+TEST(strv_extend_joined_with_size) {
+        _cleanup_strv_free_ char **a = NULL;
+        size_t n = 0;
+
+        ASSERT_OK(strv_extend_joined_with_size(&a, &n, "hoge"));
+        ASSERT_OK(strv_extend_joined_with_size(&a, &n, "aaa", "bbb", "ccc"));
+
+        ASSERT_EQ(n, 2u);
+        ASSERT_EQ(strv_length(a), n);
+        ASSERT_STREQ(a[0], "hoge");
+        ASSERT_STREQ(a[1], "aaabbbccc");
+}
+
 TEST(strv_foreach) {
         _cleanup_strv_free_ char **a;
         unsigned i = 0;