string-list: add string_list_remove function
[git] / string-list.c
1 #include "cache.h"
2 #include "string-list.h"
3
4 void string_list_init(struct string_list *list, int strdup_strings)
5 {
6         memset(list, 0, sizeof(*list));
7         list->strdup_strings = strdup_strings;
8 }
9
10 /* if there is no exact match, point to the index where the entry could be
11  * inserted */
12 static int get_entry_index(const struct string_list *list, const char *string,
13                 int *exact_match)
14 {
15         int left = -1, right = list->nr;
16         compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
17
18         while (left + 1 < right) {
19                 int middle = (left + right) / 2;
20                 int compare = cmp(string, list->items[middle].string);
21                 if (compare < 0)
22                         right = middle;
23                 else if (compare > 0)
24                         left = middle;
25                 else {
26                         *exact_match = 1;
27                         return middle;
28                 }
29         }
30
31         *exact_match = 0;
32         return right;
33 }
34
35 /* returns -1-index if already exists */
36 static int add_entry(int insert_at, struct string_list *list, const char *string)
37 {
38         int exact_match = 0;
39         int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
40
41         if (exact_match)
42                 return -1 - index;
43
44         if (list->nr + 1 >= list->alloc) {
45                 list->alloc += 32;
46                 REALLOC_ARRAY(list->items, list->alloc);
47         }
48         if (index < list->nr)
49                 memmove(list->items + index + 1, list->items + index,
50                                 (list->nr - index)
51                                 * sizeof(struct string_list_item));
52         list->items[index].string = list->strdup_strings ?
53                 xstrdup(string) : (char *)string;
54         list->items[index].util = NULL;
55         list->nr++;
56
57         return index;
58 }
59
60 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
61 {
62         int index = add_entry(-1, list, string);
63
64         if (index < 0)
65                 index = -1 - index;
66
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;
91         get_entry_index(list, string, &exact_match);
92         return exact_match;
93 }
94
95 int string_list_find_insert_index(const struct string_list *list, const char *string,
96                                   int negative_existing_index)
97 {
98         int exact_match;
99         int index = get_entry_index(list, string, &exact_match);
100         if (exact_match)
101                 index = -1 - (negative_existing_index ? index : 0);
102         return index;
103 }
104
105 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
106 {
107         int exact_match, i = get_entry_index(list, string, &exact_match);
108         if (!exact_match)
109                 return NULL;
110         return list->items + i;
111 }
112
113 void string_list_remove_duplicates(struct string_list *list, int free_util)
114 {
115         if (list->nr > 1) {
116                 int src, dst;
117                 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
118                 for (src = dst = 1; src < list->nr; src++) {
119                         if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
120                                 if (list->strdup_strings)
121                                         free(list->items[src].string);
122                                 if (free_util)
123                                         free(list->items[src].util);
124                         } else
125                                 list->items[dst++] = list->items[src];
126                 }
127                 list->nr = dst;
128         }
129 }
130
131 int for_each_string_list(struct string_list *list,
132                          string_list_each_func_t fn, void *cb_data)
133 {
134         int i, ret = 0;
135         for (i = 0; i < list->nr; i++)
136                 if ((ret = fn(&list->items[i], cb_data)))
137                         break;
138         return ret;
139 }
140
141 void filter_string_list(struct string_list *list, int free_util,
142                         string_list_each_func_t want, void *cb_data)
143 {
144         int src, dst = 0;
145         for (src = 0; src < list->nr; src++) {
146                 if (want(&list->items[src], cb_data)) {
147                         list->items[dst++] = list->items[src];
148                 } else {
149                         if (list->strdup_strings)
150                                 free(list->items[src].string);
151                         if (free_util)
152                                 free(list->items[src].util);
153                 }
154         }
155         list->nr = dst;
156 }
157
158 static int item_is_not_empty(struct string_list_item *item, void *unused)
159 {
160         return *item->string != '\0';
161 }
162
163 void string_list_remove_empty_items(struct string_list *list, int free_util) {
164         filter_string_list(list, free_util, item_is_not_empty, NULL);
165 }
166
167 void string_list_clear(struct string_list *list, int free_util)
168 {
169         if (list->items) {
170                 int i;
171                 if (list->strdup_strings) {
172                         for (i = 0; i < list->nr; i++)
173                                 free(list->items[i].string);
174                 }
175                 if (free_util) {
176                         for (i = 0; i < list->nr; i++)
177                                 free(list->items[i].util);
178                 }
179                 free(list->items);
180         }
181         list->items = NULL;
182         list->nr = list->alloc = 0;
183 }
184
185 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
186 {
187         if (list->items) {
188                 int i;
189                 if (clearfunc) {
190                         for (i = 0; i < list->nr; i++)
191                                 clearfunc(list->items[i].util, list->items[i].string);
192                 }
193                 if (list->strdup_strings) {
194                         for (i = 0; i < list->nr; i++)
195                                 free(list->items[i].string);
196                 }
197                 free(list->items);
198         }
199         list->items = NULL;
200         list->nr = list->alloc = 0;
201 }
202
203
204 void print_string_list(const struct string_list *p, const char *text)
205 {
206         int i;
207         if ( text )
208                 printf("%s\n", text);
209         for (i = 0; i < p->nr; i++)
210                 printf("%s:%p\n", p->items[i].string, p->items[i].util);
211 }
212
213 struct string_list_item *string_list_append_nodup(struct string_list *list,
214                                                   char *string)
215 {
216         struct string_list_item *retval;
217         ALLOC_GROW(list->items, list->nr + 1, list->alloc);
218         retval = &list->items[list->nr++];
219         retval->string = string;
220         retval->util = NULL;
221         return retval;
222 }
223
224 struct string_list_item *string_list_append(struct string_list *list,
225                                             const char *string)
226 {
227         return string_list_append_nodup(
228                         list,
229                         list->strdup_strings ? xstrdup(string) : (char *)string);
230 }
231
232 static int cmp_items(const void *a, const void *b, void *ctx)
233 {
234         compare_strings_fn cmp = ctx;
235         const struct string_list_item *one = a;
236         const struct string_list_item *two = b;
237         return cmp(one->string, two->string);
238 }
239
240 void string_list_sort(struct string_list *list)
241 {
242         QSORT_S(list->items, list->nr, cmp_items,
243                 list->cmp ? list->cmp : strcmp);
244 }
245
246 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
247                                                      const char *string)
248 {
249         struct string_list_item *item;
250         compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
251
252         for_each_string_list_item(item, list)
253                 if (!cmp(string, item->string))
254                         return item;
255         return NULL;
256 }
257
258 int unsorted_string_list_has_string(struct string_list *list,
259                                     const char *string)
260 {
261         return unsorted_string_list_lookup(list, string) != NULL;
262 }
263
264 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
265 {
266         if (list->strdup_strings)
267                 free(list->items[i].string);
268         if (free_util)
269                 free(list->items[i].util);
270         list->items[i] = list->items[list->nr-1];
271         list->nr--;
272 }
273
274 int string_list_split(struct string_list *list, const char *string,
275                       int delim, int maxsplit)
276 {
277         int count = 0;
278         const char *p = string, *end;
279
280         if (!list->strdup_strings)
281                 die("internal error in string_list_split(): "
282                     "list->strdup_strings must be set");
283         for (;;) {
284                 count++;
285                 if (maxsplit >= 0 && count > maxsplit) {
286                         string_list_append(list, p);
287                         return count;
288                 }
289                 end = strchr(p, delim);
290                 if (end) {
291                         string_list_append_nodup(list, xmemdupz(p, end - p));
292                         p = end + 1;
293                 } else {
294                         string_list_append(list, p);
295                         return count;
296                 }
297         }
298 }
299
300 int string_list_split_in_place(struct string_list *list, char *string,
301                                int delim, int maxsplit)
302 {
303         int count = 0;
304         char *p = string, *end;
305
306         if (list->strdup_strings)
307                 die("internal error in string_list_split_in_place(): "
308                     "list->strdup_strings must not be set");
309         for (;;) {
310                 count++;
311                 if (maxsplit >= 0 && count > maxsplit) {
312                         string_list_append(list, p);
313                         return count;
314                 }
315                 end = strchr(p, delim);
316                 if (end) {
317                         *end = '\0';
318                         string_list_append(list, p);
319                         p = end + 1;
320                 } else {
321                         string_list_append(list, p);
322                         return count;
323                 }
324         }
325 }