]> git.ipfire.org Git - thirdparty/git.git/commitdiff
string-list: align string_list_split() with its _in_place() counterpart
authorJunio C Hamano <gitster@pobox.com>
Fri, 1 Aug 2025 22:04:18 +0000 (15:04 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 3 Aug 2025 05:29:27 +0000 (22:29 -0700)
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>
19 files changed:
builtin/blame.c
builtin/merge.c
builtin/var.c
connect.c
diff.c
fetch-pack.c
notes.c
parse-options.c
pathspec.c
protocol.c
ref-filter.c
setup.c
string-list.c
string-list.h
t/helper/test-path-utils.c
t/helper/test-ref-store.c
t/unit-tests/u-string-list.c
transport.c
upload-pack.c

index 91586e6852b09e88d80172178890af2a02b543b7..70a64604018e998d7c2d9f06fbb385ccf12d70ba 100644 (file)
@@ -420,7 +420,7 @@ static void parse_color_fields(const char *s)
        colorfield_nr = 0;
 
        /* Ideally this would be stripped and split at the same time? */
-       string_list_split(&l, s, ',', -1);
+       string_list_split(&l, s, ",", -1);
        ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
 
        for_each_string_list_item(item, &l) {
index 18b22c0a26d6337a7c68075326c2367f5cfe5986..893f8950bfc057fe48d57f81acfbc2036d63127b 100644 (file)
@@ -875,7 +875,7 @@ static void add_strategies(const char *string, unsigned attr)
        if (string) {
                struct string_list list = STRING_LIST_INIT_DUP;
                struct string_list_item *item;
-               string_list_split(&list, string, ' ', -1);
+               string_list_split(&list, string, " ", -1);
                for_each_string_list_item(item, &list)
                        append_strategy(get_strategy(item->string));
                string_list_clear(&list, 0);
index ada642a9fe5257ed00454ecae769d023661cae74..4ae7af0eff96f790bbed99056f27b68b5103037c 100644 (file)
@@ -181,7 +181,7 @@ static void list_vars(void)
                        if (ptr->multivalued && *val) {
                                struct string_list list = STRING_LIST_INIT_DUP;
 
-                               string_list_split(&list, val, '\n', -1);
+                               string_list_split(&list, val, "\n", -1);
                                for (size_t i = 0; i < list.nr; i++)
                                        printf("%s=%s\n", ptr->name, list.items[i].string);
                                string_list_clear(&list, 0);
index e77287f426cdfd4c7fae62c8a4a8b82d8c06dfa7..867b12bde5a41280e2802986762b7ac2d2cab2d9 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -407,7 +407,7 @@ static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
         * name.  Subsequent fields (symref-target and peeled) are optional and
         * don't have a particular order.
         */
-       if (string_list_split(&line_sections, line, ' ', -1) < 2) {
+       if (string_list_split(&line_sections, line, " ", -1) < 2) {
                ret = 0;
                goto out;
        }
diff --git a/diff.c b/diff.c
index dca87e164fb61576b9a0c6a832849df8abd041c6..a81949a422065526a9fff4625fabc2d4c4ffdc95 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -327,7 +327,7 @@ static unsigned parse_color_moved_ws(const char *arg)
        struct string_list l = STRING_LIST_INIT_DUP;
        struct string_list_item *i;
 
-       string_list_split(&l, arg, ',', -1);
+       string_list_split(&l, arg, ",", -1);
 
        for_each_string_list_item(i, &l) {
                struct strbuf sb = STRBUF_INIT;
index c1be9b76eb63739d34e425ef343531fdffa92abe..98662706968dba72ed87a6c4708ea80855a6be3c 100644 (file)
@@ -1914,7 +1914,7 @@ static void fetch_pack_config(void)
                char *str;
 
                if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
-                       string_list_split(&uri_protocols, str, ',', -1);
+                       string_list_split(&uri_protocols, str, ",", -1);
                        free(str);
                }
        }
diff --git a/notes.c b/notes.c
index 97b995f3f2da6f154337b4672af6f67438ae0f2a..6afcf088b974855627725a6b6ad61afb2bc7c649 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -892,7 +892,7 @@ static int string_list_add_note_lines(struct string_list *list,
         * later, along with any empty strings that came from empty
         * lines within the file.
         */
-       string_list_split(list, data, '\n', -1);
+       string_list_split(list, data, "\n", -1);
        free(data);
        return 0;
 }
index 5224203ffe7bf8a70b4f58060bed20a5dad1afae..9e7cb7519276c019fe96d8665fb1b993beebe6d4 100644 (file)
@@ -1338,7 +1338,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
                if (!saw_empty_line && !*str)
                        saw_empty_line = 1;
 
-               string_list_split(&list, str, '\n', -1);
+               string_list_split(&list, str, "\n", -1);
                for (j = 0; j < list.nr; j++) {
                        const char *line = list.items[j].string;
 
index a3ddd701c740c94309a55e20e443ffa207646925..de325f7ef99df6ee7b69fb8307aab78033d01f93 100644 (file)
@@ -201,7 +201,7 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
        if (!value || !*value)
                die(_("attr spec must not be empty"));
 
-       string_list_split(&list, value, ' ', -1);
+       string_list_split(&list, value, " ", -1);
        string_list_remove_empty_items(&list, 0);
 
        item->attr_check = attr_check_alloc();
index bae7226ff4074f1f37a350bc4c901156af19bf27..54b9f49c01b59973b092f370b2de5da0af5719b7 100644 (file)
@@ -61,7 +61,7 @@ enum protocol_version determine_protocol_version_server(void)
        if (git_protocol) {
                struct string_list list = STRING_LIST_INIT_DUP;
                const struct string_list_item *item;
-               string_list_split(&list, git_protocol, ':', -1);
+               string_list_split(&list, git_protocol, ":", -1);
 
                for_each_string_list_item(item, &list) {
                        const char *value;
index f9f2c512a8c6e02fbae8d32018e2227d0abb79a7..4edfb9c83b239368ab969c5c54de5bb5eb71854b 100644 (file)
@@ -435,7 +435,7 @@ static int remote_ref_atom_parser(struct ref_format *format UNUSED,
        }
 
        atom->u.remote_ref.nobracket = 0;
-       string_list_split(&params, arg, ',', -1);
+       string_list_split(&params, arg, ",", -1);
 
        for (i = 0; i < params.nr; i++) {
                const char *s = params.items[i].string;
@@ -831,7 +831,7 @@ static int align_atom_parser(struct ref_format *format UNUSED,
 
        align->position = ALIGN_LEFT;
 
-       string_list_split(&params, arg, ',', -1);
+       string_list_split(&params, arg, ",", -1);
        for (i = 0; i < params.nr; i++) {
                const char *s = params.items[i].string;
                int position;
diff --git a/setup.c b/setup.c
index 6f52dab64cacb66d5856ced25111330cd75e52c7..b9f5eb8b51e2de4157f00c5519385d7a49ac427b 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1460,8 +1460,9 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 
        if (env_ceiling_dirs) {
                int empty_entry_found = 0;
+               static const char path_sep[] = { PATH_SEP, '\0' };
 
-               string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
+               string_list_split(&ceiling_dirs, env_ceiling_dirs, path_sep, -1);
                filter_string_list(&ceiling_dirs, 0,
                                   canonicalize_ceiling_entry, &empty_entry_found);
                ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs);
index 0cb920e9b0d5200acd79b7de2a44e90bd613419a..2284a009cba6a221c97caabc035802c326228856 100644 (file)
@@ -277,7 +277,7 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
 }
 
 int string_list_split(struct string_list *list, const char *string,
-                     int delim, int maxsplit)
+                     const char *delim, int maxsplit)
 {
        int count = 0;
        const char *p = string, *end;
@@ -291,7 +291,7 @@ int string_list_split(struct string_list *list, const char *string,
                        string_list_append(list, p);
                        return count;
                }
-               end = strchr(p, delim);
+               end = strpbrk(p, delim);
                if (end) {
                        string_list_append_nodup(list, xmemdupz(p, end - p));
                        p = end + 1;
index 122b3186419880403da702c928724aaefd8e34f3..6c8650efde0dfb43b953eec07db94eb5f1522f36 100644 (file)
@@ -254,7 +254,7 @@ struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
 
 /**
- * Split string into substrings on character `delim` and append the
+ * Split string into substrings on characters in `delim` and append the
  * substrings to `list`.  The input string is not modified.
  * list->strdup_strings must be set, as new memory needs to be
  * 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_
  * appended to list.
  *
  * Examples:
- *   string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
- *   string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
- *   string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
- *   string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
- *   string_list_split(l, "", ':', -1) -> [""]
- *   string_list_split(l, ":", ':', -1) -> ["", ""]
+ *   string_list_split(l, "foo:bar:baz", ":", -1) -> ["foo", "bar", "baz"]
+ *   string_list_split(l, "foo:bar:baz", ":", 0) -> ["foo:bar:baz"]
+ *   string_list_split(l, "foo:bar:baz", ":", 1) -> ["foo", "bar:baz"]
+ *   string_list_split(l, "foo:bar:", ":", -1) -> ["foo", "bar", ""]
+ *   string_list_split(l, "", ":", -1) -> [""]
+ *   string_list_split(l, ":", ":", -1) -> ["", ""]
  */
 int string_list_split(struct string_list *list, const char *string,
-                     int delim, int maxsplit);
+                     const char *delim, int maxsplit);
 
 /*
  * Like string_list_split(), except that string is split in-place: the
index 086238c826aadb2ba65973134a8697b716ded8a1..f5f33751da620dc73af1b9c6da0728c3d94415e6 100644 (file)
@@ -348,6 +348,7 @@ int cmd__path_utils(int argc, const char **argv)
        if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
                int len;
                struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
+               const char path_sep[] = { PATH_SEP, '\0' };
                char *path = xstrdup(argv[2]);
 
                /*
@@ -362,7 +363,7 @@ int cmd__path_utils(int argc, const char **argv)
                 */
                if (normalize_path_copy(path, path))
                        die("Path \"%s\" could not be normalized", argv[2]);
-               string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
+               string_list_split(&ceiling_dirs, argv[3], path_sep, -1);
                filter_string_list(&ceiling_dirs, 0,
                                   normalize_ceiling_entry, NULL);
                len = longest_ancestor_length(path, &ceiling_dirs);
index 8d9a271845c4b60fb568a77020e21340e0a759c5..aa1cb9b4acfb2a8c74aa69150f3f29fbbf0926a1 100644 (file)
@@ -29,7 +29,7 @@ static unsigned int parse_flags(const char *str, struct flag_definition *defs)
        if (!strcmp(str, "0"))
                return 0;
 
-       string_list_split(&masks, str, ',', 64);
+       string_list_split(&masks, str, ",", 64);
        for (size_t i = 0; i < masks.nr; i++) {
                const char *name = masks.items[i].string;
                struct flag_definition *def = defs;
index d4ba5f9fa52aa2ae1e9c8a69b825d64611f70e8c..150a5f505f5bee61dba37bb3ce9ca637dcfbcc46 100644 (file)
@@ -43,7 +43,7 @@ static void t_string_list_equal(struct string_list *list,
                                  expected_strings->items[i].string);
 }
 
-static void t_string_list_split(const char *data, int delim, int maxsplit, ...)
+static void t_string_list_split(const char *data, const char *delim, int maxsplit, ...)
 {
        struct string_list expected_strings = STRING_LIST_INIT_DUP;
        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, ...)
 
 void test_string_list__split(void)
 {
-       t_string_list_split("foo:bar:baz", ':', -1, "foo", "bar", "baz", NULL);
-       t_string_list_split("foo:bar:baz", ':', 0, "foo:bar:baz", NULL);
-       t_string_list_split("foo:bar:baz", ':', 1, "foo", "bar:baz", NULL);
-       t_string_list_split("foo:bar:baz", ':', 2, "foo", "bar", "baz", NULL);
-       t_string_list_split("foo:bar:", ':', -1, "foo", "bar", "", NULL);
-       t_string_list_split("", ':', -1, "", NULL);
-       t_string_list_split(":", ':', -1, "", "", NULL);
+       t_string_list_split("foo:bar:baz", ":", -1, "foo", "bar", "baz", NULL);
+       t_string_list_split("foo:bar:baz", ":", 0, "foo:bar:baz", NULL);
+       t_string_list_split("foo:bar:baz", ":", 1, "foo", "bar:baz", NULL);
+       t_string_list_split("foo:bar:baz", ":", 2, "foo", "bar", "baz", NULL);
+       t_string_list_split("foo:bar:", ":", -1, "foo", "bar", "", NULL);
+       t_string_list_split("", ":", -1, "", NULL);
+       t_string_list_split(":", ":", -1, "", "", NULL);
 }
 
 static void t_string_list_split_in_place(const char *data, const char *delim,
index c123ac1e38b815f0d34d66ac05eec73f0a66adc1..76487b54530098f636701e5ff062ef0a3d3fd832 100644 (file)
@@ -1042,7 +1042,7 @@ static const struct string_list *protocol_allow_list(void)
        if (enabled < 0) {
                const char *v = getenv("GIT_ALLOW_PROTOCOL");
                if (v) {
-                       string_list_split(&allowed, v, ':', -1);
+                       string_list_split(&allowed, v, ":", -1);
                        string_list_sort(&allowed);
                        enabled = 1;
                } else {
index 4f26f6afc77106ead4ba04bae6f529ed39af7e02..91fcdcad9b5b4880a8dd7fd9eaeb52a4e6e436bb 100644 (file)
@@ -1685,7 +1685,7 @@ static void process_args(struct packet_reader *request,
                        if (data->uri_protocols.nr)
                                send_err_and_die(data,
                                                 "multiple packfile-uris lines forbidden");
-                       string_list_split(&data->uri_protocols, p, ',', -1);
+                       string_list_split(&data->uri_protocols, p, ",", -1);
                        continue;
                }