Skip to content

Commit 9eb8d87

Browse files
committed
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 <[email protected]>
1 parent d03f443 commit 9eb8d87

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

notes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,8 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
970970
char *globs_copy = xstrdup(globs);
971971
int i;
972972

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);
975975

976976
for (i = 0; i < split.nr; i++)
977977
string_list_add_refs_by_glob(list, split.items[i].string);

pathspec.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
201201
if (!value || !*value)
202202
die(_("attr spec must not be empty"));
203203

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);
206205

207206
item->attr_check = attr_check_alloc();
208207
CALLOC_ARRAY(item->attr_match, list.nr);

t/helper/test-hashmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ int cmd__hashmap(int argc UNUSED, const char **argv UNUSED)
149149

150150
/* break line into command and up to two parameters */
151151
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);
154154

155155
/* ignore empty lines */
156156
if (!parts.nr)

t/helper/test-json-writer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,8 @@ static int scripted(void)
492492

493493
/* break line into command and zero or more tokens */
494494
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);
497497

498498
/* ignore empty lines */
499499
if (!parts.nr || !*parts.items[0].string)

0 commit comments

Comments
 (0)