From d6af8c312f1d5bdeb8f8642a79205ab87c4b56ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 23 Jan 2016 10:32:56 +0100 Subject: [PATCH] core: fix number of arguments returned by string_split When keep_eol is 2 and separators are found at the end of string, the function returned argc + 1 instead of argc. --- ChangeLog.asciidoc | 2 ++ src/core/wee-string.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/ChangeLog.asciidoc b/ChangeLog.asciidoc index 380ba0f30..fc03b7073 100644 --- a/ChangeLog.asciidoc +++ b/ChangeLog.asciidoc @@ -27,6 +27,8 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] [[1.5_bugs]] === Bugs fixed +* api: fix number of arguments returned by function string_split() when + keep_eol is 2 and the string ends with separators * irc: add missing completion "*" for target in command /msg * irc: fix /msg command with multiple targets including "*" diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 859ce65db..cbc773549 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -1587,17 +1587,30 @@ string_replace_regex (const char *string, void *regex, const char *replace, * This function must not be called directly (call string_split or * string_split_shared instead). * + * According to keep_eol value: + * 0: standard split + * 1: each argument contains the argument and all the following arguments + * 2: same as 1, and separator is kept at the end of string. + * * Examples: - * string_split ("abc de fghi", " ", 0, 0, NULL) - * ==> array[0] = "abc" - * array[1] = "de" - * array[2] = "fghi" - * array[3] = NULL - * string_split ("abc de fghi", " ", 1, 0, NULL) - * ==> array[0] = "abc de fghi" - * array[1] = "de fghi" - * array[2] = "fghi" - * array[3] = NULL + * string_split ("abc de fghi ", " ", 0, 0, &argc) + * ==> argc == 3 + * array[0] == "abc" + * array[1] == "de" + * array[2] == "fghi" + * array[3] == NULL + * string_split ("abc de fghi ", " ", 1, 0, &argc) + * ==> argc == 3 + * array[0] == "abc de fghi" + * array[1] == "de fghi" + * array[2] == "fghi" + * array[3] == NULL + * string_split ("abc de fghi ", " ", 2, 0, &argc) + * ==> argc == 3 + * array[0] == "abc de fghi " + * array[1] == "de fghi " + * array[2] == "fghi " + * array[3] == NULL */ char ** @@ -1628,7 +1641,8 @@ string_split_internal (const char *string, const char *separators, int keep_eol, { ptr++; } - i++; + if (ptr[0]) + i++; } n_items = i;