Git 2.20
[git/git.git] / string-list.c
CommitLineData
c455c87c
JS
1#include "cache.h"
2#include "string-list.h"
3
3ed3f5fe
TA
4void 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
c455c87c
JS
10/* 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;
8dd5afc9 16 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
c455c87c
JS
17
18 while (left + 1 < right) {
19716b21 19 int middle = left + (right - left) / 2;
8dd5afc9 20 int compare = cmp(string, list->items[middle].string);
c455c87c
JS
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 */
cfa1ee6b 36static int add_entry(int insert_at, struct string_list *list, const char *string)
c455c87c 37{
cfa1ee6b
MSO
38 int exact_match = 0;
39 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
c455c87c
JS
40
41 if (exact_match)
42 return -1 - index;
43
950a234c 44 ALLOC_GROW(list->items, list->nr+1, list->alloc);
c455c87c 45 if (index < list->nr)
f331ab9d
RS
46 MOVE_ARRAY(list->items + index + 1, list->items + index,
47 list->nr - index);
c455c87c
JS
48 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
78a395d3 56struct string_list_item *string_list_insert(struct string_list *list, const char *string)
c455c87c 57{
f8c4ab61 58 int index = add_entry(-1, list, string);
c455c87c
JS
59
60 if (index < 0)
61 index = -1 - index;
62
63 return list->items + index;
64}
65
3a300333
BW
66void 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--;
f331ab9d 79 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
3a300333
BW
80 }
81}
82
c455c87c
JS
83int 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
cfa1ee6b
MSO
90int 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
e8c8b713 100struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
c455c87c
JS
101{
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
31d5451e
MH
108void string_list_remove_duplicates(struct string_list *list, int free_util)
109{
110 if (list->nr > 1) {
111 int src, dst;
8dd5afc9 112 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
31d5451e 113 for (src = dst = 1; src < list->nr; src++) {
8dd5afc9 114 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
31d5451e
MH
115 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
b684e977
JP
126int for_each_string_list(struct string_list *list,
127 string_list_each_func_t fn, void *cb_data)
c6f5a7a9
JS
128{
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
eb5f0c7a
MH
136void 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
6bb2a137
MH
153static 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
c455c87c
JS
162void 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
cfa1ee6b
MSO
180void 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
e448fed8
MH
198struct string_list_item *string_list_append_nodup(struct string_list *list,
199 char *string)
c455c87c 200{
e448fed8 201 struct string_list_item *retval;
c455c87c 202 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
e448fed8
MH
203 retval = &list->items[list->nr++];
204 retval->string = string;
205 retval->util = NULL;
206 return retval;
207}
208
209struct string_list_item *string_list_append(struct string_list *list,
210 const char *string)
211{
212 return string_list_append_nodup(
213 list,
214 list->strdup_strings ? xstrdup(string) : (char *)string);
c455c87c
JS
215}
216
b6d3f5a9
BB
217/*
218 * Encapsulate the compare function pointer because ISO C99 forbids
219 * casting from void * to a function pointer and vice versa.
220 */
221struct string_list_sort_ctx
222{
223 compare_strings_fn cmp;
224};
225
5ebd9472 226static int cmp_items(const void *a, const void *b, void *ctx)
c455c87c 227{
b6d3f5a9 228 struct string_list_sort_ctx *sort_ctx = ctx;
c455c87c
JS
229 const struct string_list_item *one = a;
230 const struct string_list_item *two = b;
b6d3f5a9 231 return sort_ctx->cmp(one->string, two->string);
c455c87c
JS
232}
233
3383e199 234void string_list_sort(struct string_list *list)
c455c87c 235{
b6d3f5a9
BB
236 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
237
238 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
c455c87c
JS
239}
240
e2421480
SB
241struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
242 const char *string)
c455c87c 243{
d16df0c0 244 struct string_list_item *item;
8dd5afc9
JH
245 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
246
d16df0c0
RT
247 for_each_string_list_item(item, list)
248 if (!cmp(string, item->string))
249 return item;
e2421480
SB
250 return NULL;
251}
252
253int unsorted_string_list_has_string(struct string_list *list,
254 const char *string)
255{
256 return unsorted_string_list_lookup(list, string) != NULL;
c455c87c
JS
257}
258
86d4b528
JS
259void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
260{
261 if (list->strdup_strings)
262 free(list->items[i].string);
263 if (free_util)
264 free(list->items[i].util);
265 list->items[i] = list->items[list->nr-1];
266 list->nr--;
267}
ff919f96
MH
268
269int string_list_split(struct string_list *list, const char *string,
270 int delim, int maxsplit)
271{
272 int count = 0;
273 const char *p = string, *end;
274
275 if (!list->strdup_strings)
276 die("internal error in string_list_split(): "
277 "list->strdup_strings must be set");
278 for (;;) {
279 count++;
280 if (maxsplit >= 0 && count > maxsplit) {
281 string_list_append(list, p);
282 return count;
283 }
284 end = strchr(p, delim);
285 if (end) {
286 string_list_append_nodup(list, xmemdupz(p, end - p));
287 p = end + 1;
288 } else {
289 string_list_append(list, p);
290 return count;
291 }
292 }
293}
294
295int string_list_split_in_place(struct string_list *list, char *string,
296 int delim, int maxsplit)
297{
298 int count = 0;
299 char *p = string, *end;
300
301 if (list->strdup_strings)
302 die("internal error in string_list_split_in_place(): "
303 "list->strdup_strings must not be set");
304 for (;;) {
305 count++;
306 if (maxsplit >= 0 && count > maxsplit) {
307 string_list_append(list, p);
308 return count;
309 }
310 end = strchr(p, delim);
311 if (end) {
312 *end = '\0';
313 string_list_append(list, p);
314 p = end + 1;
315 } else {
316 string_list_append(list, p);
317 return count;
318 }
319 }
320}