string-list: split-then-remove-empty can be done while splitting

Thanks to the new STRING_LIST_SPLIT_NONEMPTY flag, a common pattern to split a string into a string list and then remove empty items in the resulting list is no longer needed. Instead, just tell the string_list_split*() to omit empty ones while splitting. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 1, 2025 at 15:04 UTC 2ab2aac73d234ae75096e2186b07cc14c57d2586
4 files changed +7 -8
notes.c
+2 -2
@@ -970,8 +970,8 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
970 char *globs_copy = xstrdup(globs);
971 int i;
972
973 - string_list_split_in_place(&split, globs_copy, ":", -1);
974 - string_list_remove_empty_items(&split, 0);
973 + string_list_split_in_place_f(&split, globs_copy, ":", -1,
974 + STRING_LIST_SPLIT_NONEMPTY);
975
976 for (i = 0; i < split.nr; i++)
977 string_list_add_refs_by_glob(list, split.items[i].string);
pathspec.c
+1 -2
@@ -201,8 +201,7 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
201 if (!value || !*value)
202 die(_("attr spec must not be empty"));
203
204 - string_list_split(&list, value, " ", -1);
205 - string_list_remove_empty_items(&list, 0);
204 + string_list_split_f(&list, value, " ", -1, STRING_LIST_SPLIT_NONEMPTY);
205
206 item->attr_check = attr_check_alloc();
207 CALLOC_ARRAY(item->attr_match, list.nr);
t/helper/test-hashmap.c
+2 -2
@@ -149,8 +149,8 @@ int cmd__hashmap(int argc UNUSED, const char **argv UNUSED)
149
150 /* break line into command and up to two parameters */
151 string_list_setlen(&parts, 0);
152 - string_list_split_in_place(&parts, line.buf, DELIM, 2);
153 - string_list_remove_empty_items(&parts, 0);
152 + string_list_split_in_place_f(&parts, line.buf, DELIM, 2,
153 + STRING_LIST_SPLIT_NONEMPTY);
154
155 /* ignore empty lines */
156 if (!parts.nr)
t/helper/test-json-writer.c
+2 -2
@@ -492,8 +492,8 @@ static int scripted(void)
492
493 /* break line into command and zero or more tokens */
494 string_list_setlen(&parts, 0);
495 - string_list_split_in_place(&parts, line, " ", -1);
496 - string_list_remove_empty_items(&parts, 0);
495 + string_list_split_in_place_f(&parts, line, " ", -1,
496 + STRING_LIST_SPLIT_NONEMPTY);
497
498 /* ignore empty lines */
499 if (!parts.nr || !*parts.items[0].string)