Skip to content

Commit 3a30033

Browse files
bmwillgitster
authored andcommitted
string-list: add string_list_remove function
Teach string-list to be able to remove a string from a sorted 'struct string_list'. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e3a4344 commit 3a30033

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

string-list.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
6767
return list->items + index;
6868
}
6969

70+
void string_list_remove(struct string_list *list, const char *string,
71+
int free_util)
72+
{
73+
int exact_match;
74+
int i = get_entry_index(list, string, &exact_match);
75+
76+
if (exact_match) {
77+
if (list->strdup_strings)
78+
free(list->items[i].string);
79+
if (free_util)
80+
free(list->items[i].util);
81+
82+
list->nr--;
83+
memmove(list->items + i, list->items + i + 1,
84+
(list->nr - i) * sizeof(struct string_list_item));
85+
}
86+
}
87+
7088
int string_list_has_string(const struct string_list *list, const char *string)
7189
{
7290
int exact_match;

string-list.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ int string_list_find_insert_index(const struct string_list *list, const char *st
6262
*/
6363
struct string_list_item *string_list_insert(struct string_list *list, const char *string);
6464

65+
/*
66+
* Removes the given string from the sorted list.
67+
* If the string doesn't exist, the list is not altered.
68+
*/
69+
extern void string_list_remove(struct string_list *list, const char *string,
70+
int free_util);
71+
6572
/*
6673
* Checks if the given string is part of a sorted list. If it is part of the list,
6774
* return the coresponding string_list_item, NULL otherwise.

0 commit comments

Comments
 (0)