blob: 549fc416d68ea4de2d1eb6f513af7bf66f37ccb9 [file] [log] [blame]
Johannes Schindelinc455c872008-07-21 19:03:49 +01001#include "cache.h"
2#include "string-list.h"
3
Ævar Arnfjörð Bjarmason770feda2021-07-01 12:51:28 +02004void string_list_init_nodup(struct string_list *list)
5{
6 struct string_list blank = STRING_LIST_INIT_NODUP;
7 memcpy(list, &blank, sizeof(*list));
8}
9
10void string_list_init_dup(struct string_list *list)
11{
12 struct string_list blank = STRING_LIST_INIT_DUP;
13 memcpy(list, &blank, sizeof(*list));
14}
15
Johannes Schindelinc455c872008-07-21 19:03:49 +010016/* if there is no exact match, point to the index where the entry could be
17 * inserted */
18static int get_entry_index(const struct string_list *list, const char *string,
19 int *exact_match)
20{
21 int left = -1, right = list->nr;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -080022 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
Johannes Schindelinc455c872008-07-21 19:03:49 +010023
24 while (left + 1 < right) {
Derrick Stolee19716b22017-10-08 14:29:37 -040025 int middle = left + (right - left) / 2;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -080026 int compare = cmp(string, list->items[middle].string);
Johannes Schindelinc455c872008-07-21 19:03:49 +010027 if (compare < 0)
28 right = middle;
29 else if (compare > 0)
30 left = middle;
31 else {
32 *exact_match = 1;
33 return middle;
34 }
35 }
36
37 *exact_match = 0;
38 return right;
39}
40
41/* returns -1-index if already exists */
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010042static int add_entry(int insert_at, struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +010043{
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010044 int exact_match = 0;
45 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
Johannes Schindelinc455c872008-07-21 19:03:49 +010046
47 if (exact_match)
48 return -1 - index;
49
Jeff Hostetler950a2342017-04-14 19:51:52 +000050 ALLOC_GROW(list->items, list->nr+1, list->alloc);
Johannes Schindelinc455c872008-07-21 19:03:49 +010051 if (index < list->nr)
René Scharfef331ab92017-07-15 22:00:45 +020052 MOVE_ARRAY(list->items + index + 1, list->items + index,
53 list->nr - index);
Johannes Schindelinc455c872008-07-21 19:03:49 +010054 list->items[index].string = list->strdup_strings ?
55 xstrdup(string) : (char *)string;
56 list->items[index].util = NULL;
57 list->nr++;
58
59 return index;
60}
61
Julian Phillips78a395d2010-06-26 00:41:35 +010062struct string_list_item *string_list_insert(struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +010063{
Stefan Bellerf8c4ab62014-11-24 13:22:04 -080064 int index = add_entry(-1, list, string);
Johannes Schindelinc455c872008-07-21 19:03:49 +010065
66 if (index < 0)
67 index = -1 - index;
68
69 return list->items + index;
70}
71
Brandon Williams3a300332017-04-19 16:13:21 -070072void string_list_remove(struct string_list *list, const char *string,
73 int free_util)
74{
75 int exact_match;
76 int i = get_entry_index(list, string, &exact_match);
77
78 if (exact_match) {
79 if (list->strdup_strings)
80 free(list->items[i].string);
81 if (free_util)
82 free(list->items[i].util);
83
84 list->nr--;
René Scharfef331ab92017-07-15 22:00:45 +020085 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
Brandon Williams3a300332017-04-19 16:13:21 -070086 }
87}
88
Johannes Schindelinc455c872008-07-21 19:03:49 +010089int string_list_has_string(const struct string_list *list, const char *string)
90{
91 int exact_match;
92 get_entry_index(list, string, &exact_match);
93 return exact_match;
94}
95
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010096int string_list_find_insert_index(const struct string_list *list, const char *string,
97 int negative_existing_index)
98{
99 int exact_match;
100 int index = get_entry_index(list, string, &exact_match);
101 if (exact_match)
102 index = -1 - (negative_existing_index ? index : 0);
103 return index;
104}
105
Julian Phillipse8c8b712010-06-26 00:41:37 +0100106struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100107{
108 int exact_match, i = get_entry_index(list, string, &exact_match);
109 if (!exact_match)
110 return NULL;
111 return list->items + i;
112}
113
Michael Haggerty31d54512012-09-12 16:04:45 +0200114void string_list_remove_duplicates(struct string_list *list, int free_util)
115{
116 if (list->nr > 1) {
117 int src, dst;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800118 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
Michael Haggerty31d54512012-09-12 16:04:45 +0200119 for (src = dst = 1; src < list->nr; src++) {
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800120 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
Michael Haggerty31d54512012-09-12 16:04:45 +0200121 if (list->strdup_strings)
122 free(list->items[src].string);
123 if (free_util)
124 free(list->items[src].util);
125 } else
126 list->items[dst++] = list->items[src];
127 }
128 list->nr = dst;
129 }
130}
131
Julian Phillipsb684e972010-06-26 00:41:34 +0100132int for_each_string_list(struct string_list *list,
133 string_list_each_func_t fn, void *cb_data)
Jay Soffianc6f5a7a2009-02-25 03:32:18 -0500134{
135 int i, ret = 0;
136 for (i = 0; i < list->nr; i++)
137 if ((ret = fn(&list->items[i], cb_data)))
138 break;
139 return ret;
140}
141
Michael Haggertyeb5f0c72012-09-12 16:04:44 +0200142void filter_string_list(struct string_list *list, int free_util,
143 string_list_each_func_t want, void *cb_data)
144{
145 int src, dst = 0;
146 for (src = 0; src < list->nr; src++) {
147 if (want(&list->items[src], cb_data)) {
148 list->items[dst++] = list->items[src];
149 } else {
150 if (list->strdup_strings)
151 free(list->items[src].string);
152 if (free_util)
153 free(list->items[src].util);
154 }
155 }
156 list->nr = dst;
157}
158
Michael Haggerty6bb2a132012-11-04 08:07:06 +0100159static int item_is_not_empty(struct string_list_item *item, void *unused)
160{
161 return *item->string != '\0';
162}
163
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100164void string_list_remove_empty_items(struct string_list *list, int free_util)
165{
Michael Haggerty6bb2a132012-11-04 08:07:06 +0100166 filter_string_list(list, free_util, item_is_not_empty, NULL);
167}
168
Johannes Schindelinc455c872008-07-21 19:03:49 +0100169void string_list_clear(struct string_list *list, int free_util)
170{
171 if (list->items) {
172 int i;
173 if (list->strdup_strings) {
174 for (i = 0; i < list->nr; i++)
175 free(list->items[i].string);
176 }
177 if (free_util) {
178 for (i = 0; i < list->nr; i++)
179 free(list->items[i].util);
180 }
181 free(list->items);
182 }
183 list->items = NULL;
184 list->nr = list->alloc = 0;
185}
186
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +0100187void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
188{
189 if (list->items) {
190 int i;
191 if (clearfunc) {
192 for (i = 0; i < list->nr; i++)
193 clearfunc(list->items[i].util, list->items[i].string);
194 }
195 if (list->strdup_strings) {
196 for (i = 0; i < list->nr; i++)
197 free(list->items[i].string);
198 }
199 free(list->items);
200 }
201 list->items = NULL;
202 list->nr = list->alloc = 0;
203}
204
Michael Haggertye448fed2012-09-12 16:04:42 +0200205struct string_list_item *string_list_append_nodup(struct string_list *list,
206 char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100207{
Michael Haggertye448fed2012-09-12 16:04:42 +0200208 struct string_list_item *retval;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100209 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
Michael Haggertye448fed2012-09-12 16:04:42 +0200210 retval = &list->items[list->nr++];
211 retval->string = string;
212 retval->util = NULL;
213 return retval;
214}
215
216struct string_list_item *string_list_append(struct string_list *list,
217 const char *string)
218{
219 return string_list_append_nodup(
220 list,
221 list->strdup_strings ? xstrdup(string) : (char *)string);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100222}
223
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200224/*
225 * Encapsulate the compare function pointer because ISO C99 forbids
226 * casting from void * to a function pointer and vice versa.
227 */
228struct string_list_sort_ctx
229{
230 compare_strings_fn cmp;
231};
232
René Scharfe5ebd9472017-01-22 18:57:09 +0100233static int cmp_items(const void *a, const void *b, void *ctx)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100234{
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200235 struct string_list_sort_ctx *sort_ctx = ctx;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100236 const struct string_list_item *one = a;
237 const struct string_list_item *two = b;
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200238 return sort_ctx->cmp(one->string, two->string);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100239}
240
Michael Haggerty3383e192014-11-25 09:02:35 +0100241void string_list_sort(struct string_list *list)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100242{
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200243 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
244
245 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100246}
247
Stephen Boyde2421482010-03-24 00:16:02 -0700248struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
249 const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100250{
Ralf Thielowd16df0c2016-04-25 19:40:00 +0200251 struct string_list_item *item;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800252 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
253
Ralf Thielowd16df0c2016-04-25 19:40:00 +0200254 for_each_string_list_item(item, list)
255 if (!cmp(string, item->string))
256 return item;
Stephen Boyde2421482010-03-24 00:16:02 -0700257 return NULL;
258}
259
260int unsorted_string_list_has_string(struct string_list *list,
261 const char *string)
262{
263 return unsorted_string_list_lookup(list, string) != NULL;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100264}
265
Johannes Sixt86d4b522011-08-11 23:20:00 -0600266void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
267{
268 if (list->strdup_strings)
269 free(list->items[i].string);
270 if (free_util)
271 free(list->items[i].util);
272 list->items[i] = list->items[list->nr-1];
273 list->nr--;
274}
Michael Haggertyff919f92012-09-12 16:04:43 +0200275
276int string_list_split(struct string_list *list, const char *string,
277 int delim, int maxsplit)
278{
279 int count = 0;
280 const char *p = string, *end;
281
282 if (!list->strdup_strings)
283 die("internal error in string_list_split(): "
284 "list->strdup_strings must be set");
285 for (;;) {
286 count++;
287 if (maxsplit >= 0 && count > maxsplit) {
288 string_list_append(list, p);
289 return count;
290 }
291 end = strchr(p, delim);
292 if (end) {
293 string_list_append_nodup(list, xmemdupz(p, end - p));
294 p = end + 1;
295 } else {
296 string_list_append(list, p);
297 return count;
298 }
299 }
300}
301
302int string_list_split_in_place(struct string_list *list, char *string,
303 int delim, int maxsplit)
304{
305 int count = 0;
306 char *p = string, *end;
307
308 if (list->strdup_strings)
309 die("internal error in string_list_split_in_place(): "
310 "list->strdup_strings must not be set");
311 for (;;) {
312 count++;
313 if (maxsplit >= 0 && count > maxsplit) {
314 string_list_append(list, p);
315 return count;
316 }
317 end = strchr(p, delim);
318 if (end) {
319 *end = '\0';
320 string_list_append(list, p);
321 p = end + 1;
322 } else {
323 string_list_append(list, p);
324 return count;
325 }
326 }
327}