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>

Brandon Williams committed Apr 19, 2017 at 16:13 UTC 3a30033327337323e91dcbcb87396d3245260585
2 files changed +25
string-list.c
+18
@@ -67,6 +67,24 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
67 return list->items + index;
68 }
69
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 +
88 int string_list_has_string(const struct string_list *list, const char *string)
89 {
90 int exact_match;
string-list.h
+7
@@ -62,6 +62,13 @@ int string_list_find_insert_index(const struct string_list *list, const char *st
62 */
63 struct string_list_item *string_list_insert(struct string_list *list, const char *string);
64
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 +
72 /*
73 * Checks if the given string is part of a sorted list. If it is part of the list,
74 * return the coresponding string_list_item, NULL otherwise.