string-list: align string_list_split() with its _in_place() counterpart

The string_list_split_in_place() function was updated by 52acddf3 (string-list: multi-delimiter `string_list_split_in_place()`, 2023-04-24) to take more than one delimiter characters, hoping that we can later use it to replace our uses of strtok(). We however did not make a matching change to the string_list_split() function, which is very similar. Before giving both functions more features in future commits, allow string_list_split() to also take more than one delimiter characters to make them closer to each other. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 1, 2025 at 15:04 UTC 9f6dfe43c8a55b833ae16486bcafe29b543461f9
19 files changed +37 -35
builtin/blame.c
+1 -1
@@ -420,7 +420,7 @@ static void parse_color_fields(const char *s)
420 colorfield_nr = 0;
421
422 /* Ideally this would be stripped and split at the same time? */
423 - string_list_split(&l, s, ',', -1);
423 + string_list_split(&l, s, ",", -1);
424 ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
425
426 for_each_string_list_item(item, &l) {
builtin/merge.c
+1 -1
@@ -875,7 +875,7 @@ static void add_strategies(const char *string, unsigned attr)
875 if (string) {
876 struct string_list list = STRING_LIST_INIT_DUP;
877 struct string_list_item *item;
878 - string_list_split(&list, string, ' ', -1);
878 + string_list_split(&list, string, " ", -1);
879 for_each_string_list_item(item, &list)
880 append_strategy(get_strategy(item->string));
881 string_list_clear(&list, 0);
builtin/var.c
+1 -1
@@ -181,7 +181,7 @@ static void list_vars(void)
181 if (ptr->multivalued && *val) {
182 struct string_list list = STRING_LIST_INIT_DUP;
183
184 - string_list_split(&list, val, '\n', -1);
184 + string_list_split(&list, val, "\n", -1);
185 for (size_t i = 0; i < list.nr; i++)
186 printf("%s=%s\n", ptr->name, list.items[i].string);
187 string_list_clear(&list, 0);
connect.c
+1 -1
@@ -407,7 +407,7 @@ static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
407 * name. Subsequent fields (symref-target and peeled) are optional and
408 * don't have a particular order.
409 */
410 - if (string_list_split(&line_sections, line, ' ', -1) < 2) {
410 + if (string_list_split(&line_sections, line, " ", -1) < 2) {
411 ret = 0;
412 goto out;
413 }
diff.c
+1 -1
@@ -327,7 +327,7 @@ static unsigned parse_color_moved_ws(const char *arg)
327 struct string_list l = STRING_LIST_INIT_DUP;
328 struct string_list_item *i;
329
330 - string_list_split(&l, arg, ',', -1);
330 + string_list_split(&l, arg, ",", -1);
331
332 for_each_string_list_item(i, &l) {
333 struct strbuf sb = STRBUF_INIT;
fetch-pack.c
+1 -1
@@ -1914,7 +1914,7 @@ static void fetch_pack_config(void)
1914 char *str;
1915
1916 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1917 - string_list_split(&uri_protocols, str, ',', -1);
1917 + string_list_split(&uri_protocols, str, ",", -1);
1918 free(str);
1919 }
1920 }
notes.c
+1 -1
@@ -892,7 +892,7 @@ static int string_list_add_note_lines(struct string_list *list,
892 * later, along with any empty strings that came from empty
893 * lines within the file.
894 */
895 - string_list_split(list, data, '\n', -1);
895 + string_list_split(list, data, "\n", -1);
896 free(data);
897 return 0;
898 }
parse-options.c
+1 -1
@@ -1338,7 +1338,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1338 if (!saw_empty_line && !*str)
1339 saw_empty_line = 1;
1340
1341 - string_list_split(&list, str, '\n', -1);
1341 + string_list_split(&list, str, "\n", -1);
1342 for (j = 0; j < list.nr; j++) {
1343 const char *line = list.items[j].string;
1344
pathspec.c
+1 -1
@@ -201,7 +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);
204 + string_list_split(&list, value, " ", -1);
205 string_list_remove_empty_items(&list, 0);
206
207 item->attr_check = attr_check_alloc();
protocol.c
+1 -1
@@ -61,7 +61,7 @@ enum protocol_version determine_protocol_version_server(void)
61 if (git_protocol) {
62 struct string_list list = STRING_LIST_INIT_DUP;
63 const struct string_list_item *item;
64 - string_list_split(&list, git_protocol, ':', -1);
64 + string_list_split(&list, git_protocol, ":", -1);
65
66 for_each_string_list_item(item, &list) {
67 const char *value;
ref-filter.c
+2 -2
@@ -435,7 +435,7 @@ static int remote_ref_atom_parser(struct ref_format *format UNUSED,
435 }
436
437 atom->u.remote_ref.nobracket = 0;
438 - string_list_split(&params, arg, ',', -1);
438 + string_list_split(&params, arg, ",", -1);
439
440 for (i = 0; i < params.nr; i++) {
441 const char *s = params.items[i].string;
@@ -831,7 +831,7 @@ static int align_atom_parser(struct ref_format *format UNUSED,
831
832 align->position = ALIGN_LEFT;
833
834 - string_list_split(&params, arg, ',', -1);
834 + string_list_split(&params, arg, ",", -1);
835 for (i = 0; i < params.nr; i++) {
836 const char *s = params.items[i].string;
837 int position;
setup.c
+2 -1
@@ -1460,8 +1460,9 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1460
1461 if (env_ceiling_dirs) {
1462 int empty_entry_found = 0;
1463 + static const char path_sep[] = { PATH_SEP, '\0' };
1464
1464 - string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
1465 + string_list_split(&ceiling_dirs, env_ceiling_dirs, path_sep, -1);
1466 filter_string_list(&ceiling_dirs, 0,
1467 canonicalize_ceiling_entry, &empty_entry_found);
1468 ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs);
string-list.c
+2 -2
@@ -277,7 +277,7 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
277 }
278
279 int string_list_split(struct string_list *list, const char *string,
280 - int delim, int maxsplit)
280 + const char *delim, int maxsplit)
281 {
282 int count = 0;
283 const char *p = string, *end;
@@ -291,7 +291,7 @@ int string_list_split(struct string_list *list, const char *string,
291 string_list_append(list, p);
292 return count;
293 }
294 - end = strchr(p, delim);
294 + end = strpbrk(p, delim);
295 if (end) {
296 string_list_append_nodup(list, xmemdupz(p, end - p));
297 p = end + 1;
string-list.h
+8 -8
@@ -254,7 +254,7 @@ struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
254 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
255
256 /**
257 - * Split string into substrings on character `delim` and append the
257 + * Split string into substrings on characters in `delim` and append the
258 * substrings to `list`. The input string is not modified.
259 * list->strdup_strings must be set, as new memory needs to be
260 * allocated to hold the substrings. If maxsplit is non-negative,
@@ -262,15 +262,15 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
262 * appended to list.
263 *
264 * Examples:
265 - * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
266 - * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
267 - * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
268 - * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
269 - * string_list_split(l, "", ':', -1) -> [""]
270 - * string_list_split(l, ":", ':', -1) -> ["", ""]
265 + * string_list_split(l, "foo:bar:baz", ":", -1) -> ["foo", "bar", "baz"]
266 + * string_list_split(l, "foo:bar:baz", ":", 0) -> ["foo:bar:baz"]
267 + * string_list_split(l, "foo:bar:baz", ":", 1) -> ["foo", "bar:baz"]
268 + * string_list_split(l, "foo:bar:", ":", -1) -> ["foo", "bar", ""]
269 + * string_list_split(l, "", ":", -1) -> [""]
270 + * string_list_split(l, ":", ":", -1) -> ["", ""]
271 */
272 int string_list_split(struct string_list *list, const char *string,
273 - int delim, int maxsplit);
273 + const char *delim, int maxsplit);
274
275 /*
276 * Like string_list_split(), except that string is split in-place: the
t/helper/test-path-utils.c
+2 -1
@@ -348,6 +348,7 @@ int cmd__path_utils(int argc, const char **argv)
348 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
349 int len;
350 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
351 + const char path_sep[] = { PATH_SEP, '\0' };
352 char *path = xstrdup(argv[2]);
353
354 /*
@@ -362,7 +363,7 @@ int cmd__path_utils(int argc, const char **argv)
363 */
364 if (normalize_path_copy(path, path))
365 die("Path \"%s\" could not be normalized", argv[2]);
365 - string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
366 + string_list_split(&ceiling_dirs, argv[3], path_sep, -1);
367 filter_string_list(&ceiling_dirs, 0,
368 normalize_ceiling_entry, NULL);
369 len = longest_ancestor_length(path, &ceiling_dirs);
t/helper/test-ref-store.c
+1 -1
@@ -29,7 +29,7 @@ static unsigned int parse_flags(const char *str, struct flag_definition *defs)
29 if (!strcmp(str, "0"))
30 return 0;
31
32 - string_list_split(&masks, str, ',', 64);
32 + string_list_split(&masks, str, ",", 64);
33 for (size_t i = 0; i < masks.nr; i++) {
34 const char *name = masks.items[i].string;
35 struct flag_definition *def = defs;
t/unit-tests/u-string-list.c
+8 -8
@@ -43,7 +43,7 @@ static void t_string_list_equal(struct string_list *list,
43 expected_strings->items[i].string);
44 }
45
46 -static void t_string_list_split(const char *data, int delim, int maxsplit, ...)
46 +static void t_string_list_split(const char *data, const char *delim, int maxsplit, ...)
47 {
48 struct string_list expected_strings = STRING_LIST_INIT_DUP;
49 struct string_list list = STRING_LIST_INIT_DUP;
@@ -65,13 +65,13 @@ static void t_string_list_split(const char *data, int delim, int maxsplit, ...)
65
66 void test_string_list__split(void)
67 {
68 - t_string_list_split("foo:bar:baz", ':', -1, "foo", "bar", "baz", NULL);
69 - t_string_list_split("foo:bar:baz", ':', 0, "foo:bar:baz", NULL);
70 - t_string_list_split("foo:bar:baz", ':', 1, "foo", "bar:baz", NULL);
71 - t_string_list_split("foo:bar:baz", ':', 2, "foo", "bar", "baz", NULL);
72 - t_string_list_split("foo:bar:", ':', -1, "foo", "bar", "", NULL);
73 - t_string_list_split("", ':', -1, "", NULL);
74 - t_string_list_split(":", ':', -1, "", "", NULL);
68 + t_string_list_split("foo:bar:baz", ":", -1, "foo", "bar", "baz", NULL);
69 + t_string_list_split("foo:bar:baz", ":", 0, "foo:bar:baz", NULL);
70 + t_string_list_split("foo:bar:baz", ":", 1, "foo", "bar:baz", NULL);
71 + t_string_list_split("foo:bar:baz", ":", 2, "foo", "bar", "baz", NULL);
72 + t_string_list_split("foo:bar:", ":", -1, "foo", "bar", "", NULL);
73 + t_string_list_split("", ":", -1, "", NULL);
74 + t_string_list_split(":", ":", -1, "", "", NULL);
75 }
76
77 static void t_string_list_split_in_place(const char *data, const char *delim,
transport.c
+1 -1
@@ -1042,7 +1042,7 @@ static const struct string_list *protocol_allow_list(void)
1042 if (enabled < 0) {
1043 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1044 if (v) {
1045 - string_list_split(&allowed, v, ':', -1);
1045 + string_list_split(&allowed, v, ":", -1);
1046 string_list_sort(&allowed);
1047 enabled = 1;
1048 } else {
upload-pack.c
+1 -1
@@ -1685,7 +1685,7 @@ static void process_args(struct packet_reader *request,
1685 if (data->uri_protocols.nr)
1686 send_err_and_die(data,
1687 "multiple packfile-uris lines forbidden");
1688 - string_list_split(&data->uri_protocols, p, ',', -1);
1688 + string_list_split(&data->uri_protocols, p, ",", -1);
1689 continue;
1690 }
1691