]> git.ipfire.org Git - thirdparty/git.git/commitdiff
string-list: return index directly when inserting an existing element
authorshejialuo <shejialuo@gmail.com>
Sun, 29 Jun 2025 04:27:57 +0000 (12:27 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Jul 2025 15:07:46 +0000 (08:07 -0700)
When inserting an existing element, "add_entry" would convert "index"
value to "-1-index" to indicate the caller that this element is in the
list already. However, in "string_list_insert", we would simply convert
this to the original positive index without any further action.

In 8fd2cb4069 (Extract helper bits from c-merge-recursive work,
2006-07-25), we create "path-list.c" and then introduce above code path.

Let's directly return the index as we don't care about whether the
element is in the list by using "add_entry". In the future, if we want
to let "add_entry" tell the caller, we may add "int *exact_match"
parameter to "add_entry" instead of converting the index to negative to
indicate.

Signed-off-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
string-list.c

index 8540c29bc9e255d3cbf87ef6459c77e48b87bc88..171cef5dbbd9ba1fa2c89f0090b96b5c6af74671 100644 (file)
@@ -40,14 +40,13 @@ static int get_entry_index(const struct string_list *list, const char *string,
        return right;
 }
 
-/* returns -1-index if already exists */
 static int add_entry(struct string_list *list, const char *string)
 {
        int exact_match = 0;
        int index = get_entry_index(list, string, &exact_match);
 
        if (exact_match)
-               return -1 - index;
+               return index;
 
        ALLOC_GROW(list->items, list->nr+1, list->alloc);
        if (index < list->nr)
@@ -65,9 +64,6 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
 {
        int index = add_entry(list, string);
 
-       if (index < 0)
-               index = -1 - index;
-
        return list->items + index;
 }