changeset 21535:fc183b141006

lib: Clarify *_strsplit_spaces() and add unit test
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 14 Feb 2017 10:54:29 +0200
parents 576ae10cd6cc
children 35d26415f0bc
files src/lib/strfuncs.h src/lib/test-strfuncs.c
diffstat 2 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/strfuncs.h	Mon Feb 13 23:46:15 2017 +0200
+++ b/src/lib/strfuncs.h	Tue Feb 14 10:54:29 2017 +0200
@@ -74,7 +74,8 @@
 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)
--- a/src/lib/test-strfuncs.c	Mon Feb 13 23:46:15 2017 +0200
+++ b/src/lib/test-strfuncs.c	Tue Feb 14 10:54:29 2017 +0200
@@ -92,6 +92,26 @@
 		}
 		strsplit_verify(buf);
 	}
+}
+
+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();
 }
 
@@ -220,6 +240,7 @@
 	test_p_strarray_dup();
 	test_t_strsplit();
 	test_t_strsplit_tab();
+	test_t_strsplit_spaces();
 	test_t_str_replace();
 	/*test_t_str_trim();*/
 	test_t_str_ltrim();