blob: 771c4550980e24eb8bf9db14e27c49f983c1ed4b [file] [log] [blame]
Johannes Schindelinc455c872008-07-21 19:03:49 +01001#include "cache.h"
2#include "string-list.h"
3
Tanay Abhra3ed3f5f2014-07-18 02:18:59 -07004void 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
Johannes Schindelinc455c872008-07-21 19:03:49 +010010/* if there is no exact match, point to the index where the entry could be
11 * inserted */
12static int get_entry_index(const struct string_list *list, const char *string,
13 int *exact_match)
14{
15 int left = -1, right = list->nr;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -080016 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
Johannes Schindelinc455c872008-07-21 19:03:49 +010017
18 while (left + 1 < right) {
Derrick Stolee19716b22017-10-08 14:29:37 -040019 int middle = left + (right - left) / 2;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -080020 int compare = cmp(string, list->items[middle].string);
Johannes Schindelinc455c872008-07-21 19:03:49 +010021 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 */
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010036static int add_entry(int insert_at, struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +010037{
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010038 int exact_match = 0;
39 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
Johannes Schindelinc455c872008-07-21 19:03:49 +010040
41 if (exact_match)
42 return -1 - index;
43
Jeff Hostetler950a2342017-04-14 19:51:52 +000044 ALLOC_GROW(list->items, list->nr+1, list->alloc);
Johannes Schindelinc455c872008-07-21 19:03:49 +010045 if (index < list->nr)
René Scharfef331ab92017-07-15 22:00:45 +020046 MOVE_ARRAY(list->items + index + 1, list->items + index,
47 list->nr - index);
Johannes Schindelinc455c872008-07-21 19:03:49 +010048 list->items[index].string = list->strdup_strings ?
49 xstrdup(string) : (char *)string;
50 list->items[index].util = NULL;
51 list->nr++;
52
53 return index;
54}
55
Julian Phillips78a395d2010-06-26 00:41:35 +010056struct string_list_item *string_list_insert(struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +010057{
Stefan Bellerf8c4ab62014-11-24 13:22:04 -080058 int index = add_entry(-1, list, string);
Johannes Schindelinc455c872008-07-21 19:03:49 +010059
60 if (index < 0)
61 index = -1 - index;
62
63 return list->items + index;
64}
65
Brandon Williams3a300332017-04-19 16:13:21 -070066void string_list_remove(struct string_list *list, const char *string,
67 int free_util)
68{
69 int exact_match;
70 int i = get_entry_index(list, string, &exact_match);
71
72 if (exact_match) {
73 if (list->strdup_strings)
74 free(list->items[i].string);
75 if (free_util)
76 free(list->items[i].util);
77
78 list->nr--;
René Scharfef331ab92017-07-15 22:00:45 +020079 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
Brandon Williams3a300332017-04-19 16:13:21 -070080 }
81}
82
Johannes Schindelinc455c872008-07-21 19:03:49 +010083int string_list_has_string(const struct string_list *list, const char *string)
84{
85 int exact_match;
86 get_entry_index(list, string, &exact_match);
87 return exact_match;
88}
89
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010090int string_list_find_insert_index(const struct string_list *list, const char *string,
91 int negative_existing_index)
92{
93 int exact_match;
94 int index = get_entry_index(list, string, &exact_match);
95 if (exact_match)
96 index = -1 - (negative_existing_index ? index : 0);
97 return index;
98}
99
Julian Phillipse8c8b712010-06-26 00:41:37 +0100100struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100101{
102 int exact_match, i = get_entry_index(list, string, &exact_match);
103 if (!exact_match)
104 return NULL;
105 return list->items + i;
106}
107
Michael Haggerty31d54512012-09-12 16:04:45 +0200108void string_list_remove_duplicates(struct string_list *list, int free_util)
109{
110 if (list->nr > 1) {
111 int src, dst;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800112 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
Michael Haggerty31d54512012-09-12 16:04:45 +0200113 for (src = dst = 1; src < list->nr; src++) {
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800114 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
Michael Haggerty31d54512012-09-12 16:04:45 +0200115 if (list->strdup_strings)
116 free(list->items[src].string);
117 if (free_util)
118 free(list->items[src].util);
119 } else
120 list->items[dst++] = list->items[src];
121 }
122 list->nr = dst;
123 }
124}
125
Julian Phillipsb684e972010-06-26 00:41:34 +0100126int for_each_string_list(struct string_list *list,
127 string_list_each_func_t fn, void *cb_data)
Jay Soffianc6f5a7a2009-02-25 03:32:18 -0500128{
129 int i, ret = 0;
130 for (i = 0; i < list->nr; i++)
131 if ((ret = fn(&list->items[i], cb_data)))
132 break;
133 return ret;
134}
135
Michael Haggertyeb5f0c72012-09-12 16:04:44 +0200136void filter_string_list(struct string_list *list, int free_util,
137 string_list_each_func_t want, void *cb_data)
138{
139 int src, dst = 0;
140 for (src = 0; src < list->nr; src++) {
141 if (want(&list->items[src], cb_data)) {
142 list->items[dst++] = list->items[src];
143 } else {
144 if (list->strdup_strings)
145 free(list->items[src].string);
146 if (free_util)
147 free(list->items[src].util);
148 }
149 }
150 list->nr = dst;
151}
152
Michael Haggerty6bb2a132012-11-04 08:07:06 +0100153static int item_is_not_empty(struct string_list_item *item, void *unused)
154{
155 return *item->string != '\0';
156}
157
158void string_list_remove_empty_items(struct string_list *list, int free_util) {
159 filter_string_list(list, free_util, item_is_not_empty, NULL);
160}
161
Johannes Schindelinc455c872008-07-21 19:03:49 +0100162void string_list_clear(struct string_list *list, int free_util)
163{
164 if (list->items) {
165 int i;
166 if (list->strdup_strings) {
167 for (i = 0; i < list->nr; i++)
168 free(list->items[i].string);
169 }
170 if (free_util) {
171 for (i = 0; i < list->nr; i++)
172 free(list->items[i].util);
173 }
174 free(list->items);
175 }
176 list->items = NULL;
177 list->nr = list->alloc = 0;
178}
179
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +0100180void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
181{
182 if (list->items) {
183 int i;
184 if (clearfunc) {
185 for (i = 0; i < list->nr; i++)
186 clearfunc(list->items[i].util, list->items[i].string);
187 }
188 if (list->strdup_strings) {
189 for (i = 0; i < list->nr; i++)
190 free(list->items[i].string);
191 }
192 free(list->items);
193 }
194 list->items = NULL;
195 list->nr = list->alloc = 0;
196}
197
198
Julian Phillipscb944f62010-06-26 00:41:33 +0100199void print_string_list(const struct string_list *p, const char *text)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100200{
201 int i;
202 if ( text )
203 printf("%s\n", text);
204 for (i = 0; i < p->nr; i++)
205 printf("%s:%p\n", p->items[i].string, p->items[i].util);
206}
207
Michael Haggertye448fed2012-09-12 16:04:42 +0200208struct string_list_item *string_list_append_nodup(struct string_list *list,
209 char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100210{
Michael Haggertye448fed2012-09-12 16:04:42 +0200211 struct string_list_item *retval;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100212 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
Michael Haggertye448fed2012-09-12 16:04:42 +0200213 retval = &list->items[list->nr++];
214 retval->string = string;
215 retval->util = NULL;
216 return retval;
217}
218
219struct string_list_item *string_list_append(struct string_list *list,
220 const char *string)
221{
222 return string_list_append_nodup(
223 list,
224 list->strdup_strings ? xstrdup(string) : (char *)string);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100225}
226
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200227/*
228 * Encapsulate the compare function pointer because ISO C99 forbids
229 * casting from void * to a function pointer and vice versa.
230 */
231struct string_list_sort_ctx
232{
233 compare_strings_fn cmp;
234};
235
René Scharfe5ebd9472017-01-22 18:57:09 +0100236static int cmp_items(const void *a, const void *b, void *ctx)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100237{
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200238 struct string_list_sort_ctx *sort_ctx = ctx;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100239 const struct string_list_item *one = a;
240 const struct string_list_item *two = b;
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200241 return sort_ctx->cmp(one->string, two->string);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100242}
243
Michael Haggerty3383e192014-11-25 09:02:35 +0100244void string_list_sort(struct string_list *list)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100245{
Beat Bollib6d3f5a2018-07-09 21:25:36 +0200246 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
247
248 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100249}
250
Stephen Boyde2421482010-03-24 00:16:02 -0700251struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
252 const char *string)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100253{
Ralf Thielowd16df0c2016-04-25 19:40:00 +0200254 struct string_list_item *item;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -0800255 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
256
Ralf Thielowd16df0c2016-04-25 19:40:00 +0200257 for_each_string_list_item(item, list)
258 if (!cmp(string, item->string))
259 return item;
Stephen Boyde2421482010-03-24 00:16:02 -0700260 return NULL;
261}
262
263int unsorted_string_list_has_string(struct string_list *list,
264 const char *string)
265{
266 return unsorted_string_list_lookup(list, string) != NULL;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100267}
268
Johannes Sixt86d4b522011-08-11 23:20:00 -0600269void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
270{
271 if (list->strdup_strings)
272 free(list->items[i].string);
273 if (free_util)
274 free(list->items[i].util);
275 list->items[i] = list->items[list->nr-1];
276 list->nr--;
277}
Michael Haggertyff919f92012-09-12 16:04:43 +0200278
279int string_list_split(struct string_list *list, const char *string,
280 int delim, int maxsplit)
281{
282 int count = 0;
283 const char *p = string, *end;
284
285 if (!list->strdup_strings)
286 die("internal error in string_list_split(): "
287 "list->strdup_strings must be set");
288 for (;;) {
289 count++;
290 if (maxsplit >= 0 && count > maxsplit) {
291 string_list_append(list, p);
292 return count;
293 }
294 end = strchr(p, delim);
295 if (end) {
296 string_list_append_nodup(list, xmemdupz(p, end - p));
297 p = end + 1;
298 } else {
299 string_list_append(list, p);
300 return count;
301 }
302 }
303}
304
305int string_list_split_in_place(struct string_list *list, char *string,
306 int delim, int maxsplit)
307{
308 int count = 0;
309 char *p = string, *end;
310
311 if (list->strdup_strings)
312 die("internal error in string_list_split_in_place(): "
313 "list->strdup_strings must not be set");
314 for (;;) {
315 count++;
316 if (maxsplit >= 0 && count > maxsplit) {
317 string_list_append(list, p);
318 return count;
319 }
320 end = strchr(p, delim);
321 if (end) {
322 *end = '\0';
323 string_list_append(list, p);
324 p = end + 1;
325 } else {
326 string_list_append(list, p);
327 return count;
328 }
329 }
330}