All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] util: Fix skipped characters in strsplit functions
@ 2016-01-08  0:26 Mat Martineau
  2016-01-08  0:26 ` [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety Mat Martineau
  0 siblings, 1 reply; 3+ messages in thread
From: Mat Martineau @ 2016-01-08  0:26 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1271 bytes --]

l_strsplit and l_strsplit_sep were skipping an extra character after
each delimiter because len was incremented after being set to 0 but
before the next iteration of the loop began. This caused problems with
trailing and consecutive delimiters.
---
 ell/util.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/ell/util.c b/ell/util.c
index 5bd7b3b..f920895 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -293,9 +293,15 @@ LIB_EXPORT char **l_strsplit(const char *str, const char sep)
 
 	ret = l_new(char *, len + 1);
 
-	for (i = 0, p = str, len = 0; p[len]; len++) {
-		if (p[len] != sep)
+	i = 0;
+	p = str;
+	len = 0;
+
+	while (p[len]) {
+		if (p[len] != sep) {
+			len += 1;
 			continue;
+		}
 
 		ret[i++] = l_strndup(p, len);
 		p += len + 1;
@@ -347,9 +353,15 @@ LIB_EXPORT char **l_strsplit_set(const char *str, const char *separators)
 
 	ret = l_new(char *, len + 1);
 
-	for (i = 0, p = str, len = 0; p[len]; len++) {
-		if (sep_table[(unsigned char) p[len]] != true)
+	i = 0;
+	p = str;
+	len = 0;
+
+	while (p[len]) {
+		if (sep_table[(unsigned char) p[len]] != true) {
+			len += 1;
 			continue;
+		}
 
 		ret[i++] = l_strndup(p, len);
 		p += len + 1;
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety
  2016-01-08  0:26 [PATCH 1/2] util: Fix skipped characters in strsplit functions Mat Martineau
@ 2016-01-08  0:26 ` Mat Martineau
  2016-01-08  0:38   ` Denis Kenzior
  0 siblings, 1 reply; 3+ messages in thread
From: Mat Martineau @ 2016-01-08  0:26 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 2021 bytes --]

---
 unit/test-string.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/unit/test-string.c b/unit/test-string.c
index 388e90f..156f658 100644
--- a/unit/test-string.c
+++ b/unit/test-string.c
@@ -124,6 +124,24 @@ static void test_strsplit(const void *test_data)
 	assert(!strcmp(strv[2], "bz"));
 	assert(strv[3] == NULL);
 	l_strfreev(strv);
+
+	strv = l_strsplit(":bar:::bz", ':');
+	assert(strv);
+	assert(!strcmp(strv[0], ""));
+	assert(!strcmp(strv[1], "bar"));
+	assert(!strcmp(strv[2], ""));
+	assert(!strcmp(strv[3], ""));
+	assert(!strcmp(strv[4], "bz"));
+	assert(strv[5] == NULL);
+	l_strfreev(strv);
+
+	strv = l_strsplit("Foo:bar:", ':');
+	assert(strv);
+	assert(!strcmp(strv[0], "Foo"));
+	assert(!strcmp(strv[1], "bar"));
+	assert(!strcmp(strv[2], ""));
+	assert(strv[3] == NULL);
+	l_strfreev(strv);
 }
 
 static void test_strsplit_set(const void *test_data)
@@ -137,6 +155,20 @@ static void test_strsplit_set(const void *test_data)
 	assert(!strcmp(strv[3], "Blu"));
 	assert(strv[4] == NULL);
 	l_strfreev(strv);
+
+	strv = l_strsplit_set("Foo:bar,Baz Blu,:,Fee:Fie ", ":, ");
+	assert(strv);
+	assert(!strcmp(strv[0], "Foo"));
+	assert(!strcmp(strv[1], "bar"));
+	assert(!strcmp(strv[2], "Baz"));
+	assert(!strcmp(strv[3], "Blu"));
+	assert(!strcmp(strv[4], ""));
+	assert(!strcmp(strv[5], ""));
+	assert(!strcmp(strv[6], "Fee"));
+	assert(!strcmp(strv[7], "Fie"));
+	assert(!strcmp(strv[8], ""));
+	assert(strv[9] == NULL);
+	l_strfreev(strv);
 }
 
 static void test_joinv(const void *test_data)
@@ -175,7 +207,7 @@ int main(int argc, char *argv[])
 	l_test_add("append_fixed test 2", test_fixed, &fixed_test2);
 	l_test_add("append_fixed test 3", test_fixed, &fixed_test3);
 
-	l_test_add("strplit", test_strsplit, NULL);
+	l_test_add("strsplit", test_strsplit, NULL);
 	l_test_add("strsplit_set", test_strsplit_set, NULL);
 
 	l_test_add("joinv", test_joinv, NULL);
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety
  2016-01-08  0:26 ` [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety Mat Martineau
@ 2016-01-08  0:38   ` Denis Kenzior
  0 siblings, 0 replies; 3+ messages in thread
From: Denis Kenzior @ 2016-01-08  0:38 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 231 bytes --]

Hi Mat,

On 01/07/2016 06:26 PM, Mat Martineau wrote:
> ---
>   unit/test-string.c | 34 +++++++++++++++++++++++++++++++++-
>   1 file changed, 33 insertions(+), 1 deletion(-)
>

Both applied, thanks!

Regards,
-Denis


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-08  0:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-08  0:26 [PATCH 1/2] util: Fix skipped characters in strsplit functions Mat Martineau
2016-01-08  0:26 ` [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety Mat Martineau
2016-01-08  0:38   ` Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.