]> git.ipfire.org Git - thirdparty/git.git/blame - string-list.c
SubmittingPatches: use of older maintenance tracks is an exception
[thirdparty/git.git] / string-list.c
CommitLineData
36bf1958 1#include "git-compat-util.h"
c455c87c 2#include "string-list.h"
36bf1958 3#include "alloc.h"
c455c87c 4
770fedaf
ÆAB
5void string_list_init_nodup(struct string_list *list)
6{
7 struct string_list blank = STRING_LIST_INIT_NODUP;
8 memcpy(list, &blank, sizeof(*list));
9}
10
11void string_list_init_dup(struct string_list *list)
12{
13 struct string_list blank = STRING_LIST_INIT_DUP;
14 memcpy(list, &blank, sizeof(*list));
15}
16
c455c87c
JS
17/* if there is no exact match, point to the index where the entry could be
18 * inserted */
19static int get_entry_index(const struct string_list *list, const char *string,
20 int *exact_match)
21{
22 int left = -1, right = list->nr;
8dd5afc9 23 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
c455c87c
JS
24
25 while (left + 1 < right) {
19716b21 26 int middle = left + (right - left) / 2;
8dd5afc9 27 int compare = cmp(string, list->items[middle].string);
c455c87c
JS
28 if (compare < 0)
29 right = middle;
30 else if (compare > 0)
31 left = middle;
32 else {
33 *exact_match = 1;
34 return middle;
35 }
36 }
37
38 *exact_match = 0;
39 return right;
40}
41
42/* returns -1-index if already exists */
cfa1ee6b 43static int add_entry(int insert_at, struct string_list *list, const char *string)
c455c87c 44{
cfa1ee6b
MSO
45 int exact_match = 0;
46 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
c455c87c
JS
47
48 if (exact_match)
49 return -1 - index;
50
950a234c 51 ALLOC_GROW(list->items, list->nr+1, list->alloc);
c455c87c 52 if (index < list->nr)
f331ab9d
RS
53 MOVE_ARRAY(list->items + index + 1, list->items + index,
54 list->nr - index);
c455c87c
JS
55 list->items[index].string = list->strdup_strings ?
56 xstrdup(string) : (char *)string;
57 list->items[index].util = NULL;
58 list->nr++;
59
60 return index;
61}
62
78a395d3 63struct string_list_item *string_list_insert(struct string_list *list, const char *string)
c455c87c 64{
f8c4ab61 65 int index = add_entry(-1, list, string);
c455c87c
JS
66
67 if (index < 0)
68 index = -1 - index;
69
70 return list->items + index;
71}
72
3a300333
BW
73void string_list_remove(struct string_list *list, const char *string,
74 int free_util)
75{
76 int exact_match;
77 int i = get_entry_index(list, string, &exact_match);
78
79 if (exact_match) {
80 if (list->strdup_strings)
81 free(list->items[i].string);
82 if (free_util)
83 free(list->items[i].util);
84
85 list->nr--;
f331ab9d 86 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
3a300333
BW
87 }
88}
89
c455c87c
JS
90int string_list_has_string(const struct string_list *list, const char *string)
91{
92 int exact_match;
93 get_entry_index(list, string, &exact_match);
94 return exact_match;
95}
96
cfa1ee6b
MSO
97int string_list_find_insert_index(const struct string_list *list, const char *string,
98 int negative_existing_index)
99{
100 int exact_match;
101 int index = get_entry_index(list, string, &exact_match);
102 if (exact_match)
103 index = -1 - (negative_existing_index ? index : 0);
104 return index;
105}
106
e8c8b713 107struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
c455c87c
JS
108{
109 int exact_match, i = get_entry_index(list, string, &exact_match);
110 if (!exact_match)
111 return NULL;
112 return list->items + i;
113}
114
31d5451e
MH
115void string_list_remove_duplicates(struct string_list *list, int free_util)
116{
117 if (list->nr > 1) {
118 int src, dst;
8dd5afc9 119 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
31d5451e 120 for (src = dst = 1; src < list->nr; src++) {
8dd5afc9 121 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
31d5451e
MH
122 if (list->strdup_strings)
123 free(list->items[src].string);
124 if (free_util)
125 free(list->items[src].util);
126 } else
127 list->items[dst++] = list->items[src];
128 }
129 list->nr = dst;
130 }
131}
132
b684e977
JP
133int for_each_string_list(struct string_list *list,
134 string_list_each_func_t fn, void *cb_data)
c6f5a7a9
JS
135{
136 int i, ret = 0;
137 for (i = 0; i < list->nr; i++)
138 if ((ret = fn(&list->items[i], cb_data)))
139 break;
140 return ret;
141}
142
eb5f0c7a
MH
143void filter_string_list(struct string_list *list, int free_util,
144 string_list_each_func_t want, void *cb_data)
145{
146 int src, dst = 0;
147 for (src = 0; src < list->nr; src++) {
148 if (want(&list->items[src], cb_data)) {
149 list->items[dst++] = list->items[src];
150 } else {
151 if (list->strdup_strings)
152 free(list->items[src].string);
153 if (free_util)
154 free(list->items[src].util);
155 }
156 }
157 list->nr = dst;
158}
159
1ee34710 160static int item_is_not_empty(struct string_list_item *item, void *data UNUSED)
6bb2a137
MH
161{
162 return *item->string != '\0';
163}
164
3b335762
NTND
165void string_list_remove_empty_items(struct string_list *list, int free_util)
166{
6bb2a137
MH
167 filter_string_list(list, free_util, item_is_not_empty, NULL);
168}
169
c455c87c
JS
170void string_list_clear(struct string_list *list, int free_util)
171{
172 if (list->items) {
173 int i;
174 if (list->strdup_strings) {
175 for (i = 0; i < list->nr; i++)
176 free(list->items[i].string);
177 }
178 if (free_util) {
179 for (i = 0; i < list->nr; i++)
180 free(list->items[i].util);
181 }
182 free(list->items);
183 }
184 list->items = NULL;
185 list->nr = list->alloc = 0;
186}
187
cfa1ee6b
MSO
188void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
189{
190 if (list->items) {
191 int i;
192 if (clearfunc) {
193 for (i = 0; i < list->nr; i++)
194 clearfunc(list->items[i].util, list->items[i].string);
195 }
196 if (list->strdup_strings) {
197 for (i = 0; i < list->nr; i++)
198 free(list->items[i].string);
199 }
200 free(list->items);
201 }
202 list->items = NULL;
203 list->nr = list->alloc = 0;
204}
205
492ba813
TB
206void string_list_setlen(struct string_list *list, size_t nr)
207{
208 if (list->strdup_strings)
209 BUG("cannot setlen a string_list which owns its entries");
210 if (nr > list->nr)
211 BUG("cannot grow a string_list with setlen");
212 list->nr = nr;
213}
214
e448fed8
MH
215struct string_list_item *string_list_append_nodup(struct string_list *list,
216 char *string)
c455c87c 217{
e448fed8 218 struct string_list_item *retval;
c455c87c 219 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
e448fed8
MH
220 retval = &list->items[list->nr++];
221 retval->string = string;
222 retval->util = NULL;
223 return retval;
224}
225
226struct string_list_item *string_list_append(struct string_list *list,
227 const char *string)
228{
229 return string_list_append_nodup(
230 list,
231 list->strdup_strings ? xstrdup(string) : (char *)string);
c455c87c
JS
232}
233
b6d3f5a9
BB
234/*
235 * Encapsulate the compare function pointer because ISO C99 forbids
236 * casting from void * to a function pointer and vice versa.
237 */
238struct string_list_sort_ctx
239{
240 compare_strings_fn cmp;
241};
242
5ebd9472 243static int cmp_items(const void *a, const void *b, void *ctx)
c455c87c 244{
b6d3f5a9 245 struct string_list_sort_ctx *sort_ctx = ctx;
c455c87c
JS
246 const struct string_list_item *one = a;
247 const struct string_list_item *two = b;
b6d3f5a9 248 return sort_ctx->cmp(one->string, two->string);
c455c87c
JS
249}
250
3383e199 251void string_list_sort(struct string_list *list)
c455c87c 252{
b6d3f5a9
BB
253 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
254
255 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
c455c87c
JS
256}
257
e2421480
SB
258struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
259 const char *string)
c455c87c 260{
d16df0c0 261 struct string_list_item *item;
8dd5afc9
JH
262 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
263
d16df0c0
RT
264 for_each_string_list_item(item, list)
265 if (!cmp(string, item->string))
266 return item;
e2421480
SB
267 return NULL;
268}
269
270int unsorted_string_list_has_string(struct string_list *list,
271 const char *string)
272{
273 return unsorted_string_list_lookup(list, string) != NULL;
c455c87c
JS
274}
275
86d4b528
JS
276void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
277{
278 if (list->strdup_strings)
279 free(list->items[i].string);
280 if (free_util)
281 free(list->items[i].util);
282 list->items[i] = list->items[list->nr-1];
283 list->nr--;
284}
ff919f96
MH
285
286int string_list_split(struct string_list *list, const char *string,
287 int delim, int maxsplit)
288{
289 int count = 0;
290 const char *p = string, *end;
291
292 if (!list->strdup_strings)
293 die("internal error in string_list_split(): "
294 "list->strdup_strings must be set");
295 for (;;) {
296 count++;
297 if (maxsplit >= 0 && count > maxsplit) {
298 string_list_append(list, p);
299 return count;
300 }
301 end = strchr(p, delim);
302 if (end) {
303 string_list_append_nodup(list, xmemdupz(p, end - p));
304 p = end + 1;
305 } else {
306 string_list_append(list, p);
307 return count;
308 }
309 }
310}
311
312int string_list_split_in_place(struct string_list *list, char *string,
52acddf3 313 const char *delim, int maxsplit)
ff919f96
MH
314{
315 int count = 0;
316 char *p = string, *end;
317
318 if (list->strdup_strings)
319 die("internal error in string_list_split_in_place(): "
320 "list->strdup_strings must not be set");
321 for (;;) {
322 count++;
323 if (maxsplit >= 0 && count > maxsplit) {
324 string_list_append(list, p);
325 return count;
326 }
52acddf3 327 end = strpbrk(p, delim);
ff919f96
MH
328 if (end) {
329 *end = '\0';
330 string_list_append(list, p);
331 p = end + 1;
332 } else {
333 string_list_append(list, p);
334 return count;
335 }
336 }
337}