Skip to content

Commit 4d2bce7

Browse files
shejialuogitster
authored andcommitted
string-list: return index directly when inserting an existing element
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. 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1ae9100 commit 4d2bce7

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

string-list.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ static int get_entry_index(const struct string_list *list, const char *string,
4040
return right;
4141
}
4242

43-
/* returns -1-index if already exists */
4443
static int add_entry(struct string_list *list, const char *string)
4544
{
4645
int exact_match = 0;
4746
int index = get_entry_index(list, string, &exact_match);
4847

4948
if (exact_match)
50-
return -1 - index;
49+
return index;
5150

5251
ALLOC_GROW(list->items, list->nr+1, list->alloc);
5352
if (index < list->nr)
@@ -65,9 +64,6 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
6564
{
6665
int index = add_entry(list, string);
6766

68-
if (index < 0)
69-
index = -1 - index;
70-
7167
return list->items + index;
7268
}
7369

0 commit comments

Comments
 (0)