]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added [pt]_array_const_string_join()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Sun, 21 Feb 2016 16:32:50 +0000 (18:32 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Sun, 21 Feb 2016 16:32:50 +0000 (18:32 +0200)
Based on patch by Aki Tuomi

src/lib/strfuncs.c
src/lib/strfuncs.h
src/lib/test-strfuncs.c

index de83565474c50f8866634daa8c67bd6e069fe4eb..602ceb4637c27a1ce3044979215ed8d4d0ee70c7 100644 (file)
@@ -5,6 +5,7 @@
 #include "lib.h"
 #include "printf-format-fix.h"
 #include "strfuncs.h"
+#include "array.h"
 
 #include <stdio.h>
 #include <limits.h>
@@ -726,3 +727,11 @@ const char *dec2str(uintmax_t number)
        i_assert(pos >= 0);
        return buffer + pos;
 }
+
+char *p_array_const_string_join(pool_t pool, const ARRAY_TYPE(const_string) *arr,
+                               const char *separator)
+{
+       if (array_count(arr) == 0)
+               return "";
+       return p_strarray_join_n(pool, array_idx(arr, 0), array_count(arr), separator);
+}
index 3c566adf1e561499f7b65f6bb4b53e802e51e254..c84533bacd945357880994e0ec594d7f4f52716d 100644 (file)
@@ -100,6 +100,12 @@ bool str_array_icase_find(const char *const *arr, const char *value);
 const char **p_strarray_dup(pool_t pool, const char *const *arr)
        ATTR_MALLOC ATTR_RETURNS_NONNULL;
 
+/* Join ARRAY_TYPE(const_string) to a string, similar to t_strarray_join() */
+char *p_array_const_string_join(pool_t pool, const ARRAY_TYPE(const_string) *arr,
+                               const char *separator);
+#define t_array_const_string_join(arr, separator) \
+       ((const char *)p_array_const_string_join(unsafe_data_stack_pool, arr, separator))
+
 /* FIXME: v2.3 - sort and search APIs belong into their own header, not here */
 #include "sort.h"
 
index 4c6411b68a3e35d2938661680a4641b3b0904a49..96921dee437fdf27872073e73b1831726bf4a3c8 100644 (file)
@@ -1,7 +1,7 @@
 /* Copyright (c) 2009-2016 Dovecot authors, see the included COPYING file */
 
 #include "test-lib.h"
-
+#include "array.h"
 
 static void test_p_strarray_dup(void)
 {
@@ -190,6 +190,31 @@ static void test_t_strarray_join(void)
        test_end();
 }
 
+static void test_p_array_const_string_join(void)
+{
+       ARRAY_TYPE(const_string) arr;
+       unsigned int i;
+       char *res;
+
+       test_begin("p_array_const_string_join()");
+
+       i_array_init(&arr, 2);
+       /* empty array -> empty string */
+       test_assert(strcmp(t_array_const_string_join(&arr, " "), "") == 0);
+
+       array_append(&arr, test_strarray_input,
+                    str_array_length(test_strarray_input));
+       for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
+               res = p_array_const_string_join(default_pool, &arr,
+                                               test_strarray_outputs[i].separator);
+               test_assert_idx(strcmp(res, test_strarray_outputs[i].output) == 0, i);
+               i_free(res);
+       }
+
+       array_free(&arr);
+       test_end();
+}
+
 void test_strfuncs(void)
 {
        test_p_strarray_dup();
@@ -200,4 +225,5 @@ void test_strfuncs(void)
        test_t_str_ltrim();
        test_t_str_rtrim();
        test_t_strarray_join();
+       test_p_array_const_string_join();
 }