Mercurial > dovecot > core-2.2
changeset 19791:5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Based on patch by Aki Tuomi
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Sun, 21 Feb 2016 18:32:50 +0200 |
parents | 8d3736bf4a90 |
children | 8c5311fc1202 |
files | src/lib/strfuncs.c src/lib/strfuncs.h src/lib/test-strfuncs.c |
diffstat | 3 files changed, 42 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/strfuncs.c Sun Feb 21 18:32:03 2016 +0200 +++ b/src/lib/strfuncs.c Sun Feb 21 18:32:50 2016 +0200 @@ -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 @@ 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); +}
--- a/src/lib/strfuncs.h Sun Feb 21 18:32:03 2016 +0200 +++ b/src/lib/strfuncs.h Sun Feb 21 18:32:50 2016 +0200 @@ -100,6 +100,12 @@ 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"
--- a/src/lib/test-strfuncs.c Sun Feb 21 18:32:03 2016 +0200 +++ b/src/lib/test-strfuncs.c Sun Feb 21 18:32:50 2016 +0200 @@ -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 @@ 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 @@ test_t_str_ltrim(); test_t_str_rtrim(); test_t_strarray_join(); + test_p_array_const_string_join(); }