]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Clarify *_strsplit_spaces() and add unit test
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 14 Feb 2017 08:54:29 +0000 (10:54 +0200)
committerGitLab <gitlab@git.dovecot.net>
Tue, 14 Feb 2017 09:18:30 +0000 (11:18 +0200)
src/lib/strfuncs.h
src/lib/test-strfuncs.c

index eecbcc67a76f9e4af9e7aa0f37efd5920cee8333..d52829befecd4750de4cde5046302a00c45039db 100644 (file)
@@ -82,7 +82,8 @@ char **p_strsplit(pool_t pool, const char *data, const char *separators)
 const char **t_strsplit(const char *data, const char *separators)
        ATTR_MALLOC ATTR_RETURNS_NONNULL;
 /* like p_strsplit(), but treats multiple adjacent separators as a single
-   separator. */
+   separator. separators at the beginning or at the end of the string are also
+   ignored, so it's not possible for the result to have any empty strings. */
 char **p_strsplit_spaces(pool_t pool, const char *data, const char *separators)
        ATTR_MALLOC ATTR_RETURNS_NONNULL;
 const char **t_strsplit_spaces(const char *data, const char *separators)
index 45d53e6089679f84eb5720289f8953825316644f..8171714975f7b002647c1b440451242cc6cd8b6f 100644 (file)
@@ -45,6 +45,27 @@ static void test_t_strsplit(void)
        test_end();
 }
 
+static void test_t_strsplit_spaces(void)
+{
+       const char *const *args;
+
+       test_begin("t_strsplit_spaces");
+       /* empty strings */
+       args = t_strsplit_spaces("", "\n");
+       test_assert(args[0] == NULL);
+       args = t_strsplit_spaces("\n", "\n");
+       test_assert(args[0] == NULL);
+       args = t_strsplit_spaces("\n\n", "\n");
+       test_assert(args[0] == NULL);
+
+       /* multiple separators */
+       args = t_strsplit_spaces(" , ,   ,str1  ,  ,,, , str2   , ", " ,");
+       test_assert(strcmp(args[0], "str1") == 0);
+       test_assert(strcmp(args[1], "str2") == 0);
+       test_assert(args[2] == NULL);
+       test_end();
+}
+
 static void test_t_str_replace(void)
 {
        test_begin("t_str_replace");
@@ -212,6 +233,7 @@ void test_strfuncs(void)
 {
        test_p_strarray_dup();
        test_t_strsplit();
+       test_t_strsplit_spaces();
        test_t_str_replace();
        test_t_str_trim();
        test_t_str_ltrim();