]> git.ipfire.org Git - thirdparty/git.git/commitdiff
string-list: optionally omit empty string pieces in string_list_split*()
authorJunio C Hamano <gitster@pobox.com>
Fri, 1 Aug 2025 22:04:22 +0000 (15:04 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 3 Aug 2025 05:34:45 +0000 (22:34 -0700)
Teach the unified split_string() machinery a new flag bit,
STRING_LIST_SPLIT_NONEMPTY, to cause empty split pieces to be
omitted from the resulting string list.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
string-list.c
string-list.h
t/unit-tests/u-string-list.c

index 86a309f8fbd25eb753aa4f07f1822f293c33372c..343cf1ca90d2ace20b315973c6c7549ef2e93a28 100644 (file)
@@ -294,6 +294,9 @@ static int append_one(struct string_list *list,
                                break;
        }
 
+       if ((flags & STRING_LIST_SPLIT_NONEMPTY) && (end <= p))
+               return 0;
+
        if (in_place) {
                *((char *)end) = '\0';
                string_list_append(list, p);
index 40e148712dacca2ede93d5d0cd26dcc489f3205c..2b438c7733d869f23b7ec96b8b6f02513138afb7 100644 (file)
@@ -289,6 +289,8 @@ enum {
         * it to the list
         */
        STRING_LIST_SPLIT_TRIM = (1 << 0),
+       /* omit adding empty string piece to the resulting list */
+       STRING_LIST_SPLIT_NONEMPTY = (1 << 1),
 };
 
 int string_list_split_f(struct string_list *, const char *string,
index daa9307e45ea41f2e92b901a519e127ec2d704a2..a2457d7b1ec8fa6170b64cc400d8e9c50e98a222 100644 (file)
@@ -92,6 +92,13 @@ void test_string_list__split_f(void)
                              "foo", "bar", "baz", NULL);
        t_string_list_split_f("  a  b c  ", " ", 1, STRING_LIST_SPLIT_TRIM,
                              "a", "b c", NULL);
+       t_string_list_split_f("::foo::bar:baz:", ":", -1, STRING_LIST_SPLIT_NONEMPTY,
+                             "foo", "bar", "baz", NULL);
+       t_string_list_split_f("foo:baz", ":", -1, STRING_LIST_SPLIT_NONEMPTY,
+                             "foo", "baz", NULL);
+       t_string_list_split_f("foo :: : baz", ":", -1,
+                             STRING_LIST_SPLIT_NONEMPTY | STRING_LIST_SPLIT_TRIM,
+                             "foo", "baz", NULL);
 }
 
 static void t_string_list_split_in_place_f(const char *data_, const char *delim,
@@ -125,6 +132,14 @@ void test_string_list__split_in_place_f(void)
                                       "foo", "bar", "baz", NULL);
        t_string_list_split_in_place_f("  a  b c  ", " ", 1, STRING_LIST_SPLIT_TRIM,
                                       "a", "b c", NULL);
+       t_string_list_split_in_place_f("::foo::bar:baz:", ":", -1,
+                                      STRING_LIST_SPLIT_NONEMPTY,
+                                      "foo", "bar", "baz", NULL);
+       t_string_list_split_in_place_f("foo:baz", ":", -1, STRING_LIST_SPLIT_NONEMPTY,
+                                      "foo", "baz", NULL);
+       t_string_list_split_in_place_f("foo :: : baz", ":", -1,
+                                      STRING_LIST_SPLIT_NONEMPTY | STRING_LIST_SPLIT_TRIM,
+                                      "foo", "baz", NULL);
 }
 
 void test_string_list__split(void)