add macro REALLOC_ARRAY
[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) {
19 int middle = (left + right) / 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
44 if (list->nr + 1 >= list->alloc) {
45 list->alloc += 32;
46 list->items = xrealloc(list->items, list->alloc
47 * sizeof(struct string_list_item));
48 }
49 if (index < list->nr)
50 memmove(list->items + index + 1, list->items + index,
51 (list->nr - index)
52 * sizeof(struct string_list_item));
53 list->items[index].string = list->strdup_strings ?
54 xstrdup(string) : (char *)string;
55 list->items[index].util = NULL;
56 list->nr++;
57
58 return index;
59}
60
78a395d3 61struct string_list_item *string_list_insert(struct string_list *list, const char *string)
c455c87c 62{
aadceea6 63 return string_list_insert_at_index(list, -1, string);
cfa1ee6b
MSO
64}
65
aadceea6
JP
66struct string_list_item *string_list_insert_at_index(struct string_list *list,
67 int insert_at, const char *string)
cfa1ee6b
MSO
68{
69 int index = add_entry(insert_at, list, string);
c455c87c
JS
70
71 if (index < 0)
72 index = -1 - index;
73
74 return list->items + index;
75}
76
77int string_list_has_string(const struct string_list *list, const char *string)
78{
79 int exact_match;
80 get_entry_index(list, string, &exact_match);
81 return exact_match;
82}
83
cfa1ee6b
MSO
84int string_list_find_insert_index(const struct string_list *list, const char *string,
85 int negative_existing_index)
86{
87 int exact_match;
88 int index = get_entry_index(list, string, &exact_match);
89 if (exact_match)
90 index = -1 - (negative_existing_index ? index : 0);
91 return index;
92}
93
e8c8b713 94struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
c455c87c
JS
95{
96 int exact_match, i = get_entry_index(list, string, &exact_match);
97 if (!exact_match)
98 return NULL;
99 return list->items + i;
100}
101
31d5451e
MH
102void string_list_remove_duplicates(struct string_list *list, int free_util)
103{
104 if (list->nr > 1) {
105 int src, dst;
8dd5afc9 106 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
31d5451e 107 for (src = dst = 1; src < list->nr; src++) {
8dd5afc9 108 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
31d5451e
MH
109 if (list->strdup_strings)
110 free(list->items[src].string);
111 if (free_util)
112 free(list->items[src].util);
113 } else
114 list->items[dst++] = list->items[src];
115 }
116 list->nr = dst;
117 }
118}
119
b684e977
JP
120int for_each_string_list(struct string_list *list,
121 string_list_each_func_t fn, void *cb_data)
c6f5a7a9
JS
122{
123 int i, ret = 0;
124 for (i = 0; i < list->nr; i++)
125 if ((ret = fn(&list->items[i], cb_data)))
126 break;
127 return ret;
128}
129
eb5f0c7a
MH
130void filter_string_list(struct string_list *list, int free_util,
131 string_list_each_func_t want, void *cb_data)
132{
133 int src, dst = 0;
134 for (src = 0; src < list->nr; src++) {
135 if (want(&list->items[src], cb_data)) {
136 list->items[dst++] = list->items[src];
137 } else {
138 if (list->strdup_strings)
139 free(list->items[src].string);
140 if (free_util)
141 free(list->items[src].util);
142 }
143 }
144 list->nr = dst;
145}
146
6bb2a137
MH
147static int item_is_not_empty(struct string_list_item *item, void *unused)
148{
149 return *item->string != '\0';
150}
151
152void string_list_remove_empty_items(struct string_list *list, int free_util) {
153 filter_string_list(list, free_util, item_is_not_empty, NULL);
154}
155
c455c87c
JS
156void string_list_clear(struct string_list *list, int free_util)
157{
158 if (list->items) {
159 int i;
160 if (list->strdup_strings) {
161 for (i = 0; i < list->nr; i++)
162 free(list->items[i].string);
163 }
164 if (free_util) {
165 for (i = 0; i < list->nr; i++)
166 free(list->items[i].util);
167 }
168 free(list->items);
169 }
170 list->items = NULL;
171 list->nr = list->alloc = 0;
172}
173
cfa1ee6b
MSO
174void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
175{
176 if (list->items) {
177 int i;
178 if (clearfunc) {
179 for (i = 0; i < list->nr; i++)
180 clearfunc(list->items[i].util, list->items[i].string);
181 }
182 if (list->strdup_strings) {
183 for (i = 0; i < list->nr; i++)
184 free(list->items[i].string);
185 }
186 free(list->items);
187 }
188 list->items = NULL;
189 list->nr = list->alloc = 0;
190}
191
192
cb944f6b 193void print_string_list(const struct string_list *p, const char *text)
c455c87c
JS
194{
195 int i;
196 if ( text )
197 printf("%s\n", text);
198 for (i = 0; i < p->nr; i++)
199 printf("%s:%p\n", p->items[i].string, p->items[i].util);
200}
201
e448fed8
MH
202struct string_list_item *string_list_append_nodup(struct string_list *list,
203 char *string)
c455c87c 204{
e448fed8 205 struct string_list_item *retval;
c455c87c 206 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
e448fed8
MH
207 retval = &list->items[list->nr++];
208 retval->string = string;
209 retval->util = NULL;
210 return retval;
211}
212
213struct string_list_item *string_list_append(struct string_list *list,
214 const char *string)
215{
216 return string_list_append_nodup(
217 list,
218 list->strdup_strings ? xstrdup(string) : (char *)string);
c455c87c
JS
219}
220
8dd5afc9
JH
221/* Yuck */
222static compare_strings_fn compare_for_qsort;
223
224/* Only call this from inside sort_string_list! */
c455c87c
JS
225static int cmp_items(const void *a, const void *b)
226{
227 const struct string_list_item *one = a;
228 const struct string_list_item *two = b;
8dd5afc9 229 return compare_for_qsort(one->string, two->string);
c455c87c
JS
230}
231
232void sort_string_list(struct string_list *list)
233{
8dd5afc9 234 compare_for_qsort = list->cmp ? list->cmp : strcmp;
c455c87c
JS
235 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
236}
237
e2421480
SB
238struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
239 const char *string)
c455c87c
JS
240{
241 int i;
8dd5afc9
JH
242 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
243
c455c87c 244 for (i = 0; i < list->nr; i++)
8dd5afc9 245 if (!cmp(string, list->items[i].string))
e2421480
SB
246 return list->items + i;
247 return NULL;
248}
249
250int unsorted_string_list_has_string(struct string_list *list,
251 const char *string)
252{
253 return unsorted_string_list_lookup(list, string) != NULL;
c455c87c
JS
254}
255
86d4b528
JS
256void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
257{
258 if (list->strdup_strings)
259 free(list->items[i].string);
260 if (free_util)
261 free(list->items[i].util);
262 list->items[i] = list->items[list->nr-1];
263 list->nr--;
264}
ff919f96
MH
265
266int string_list_split(struct string_list *list, const char *string,
267 int delim, int maxsplit)
268{
269 int count = 0;
270 const char *p = string, *end;
271
272 if (!list->strdup_strings)
273 die("internal error in string_list_split(): "
274 "list->strdup_strings must be set");
275 for (;;) {
276 count++;
277 if (maxsplit >= 0 && count > maxsplit) {
278 string_list_append(list, p);
279 return count;
280 }
281 end = strchr(p, delim);
282 if (end) {
283 string_list_append_nodup(list, xmemdupz(p, end - p));
284 p = end + 1;
285 } else {
286 string_list_append(list, p);
287 return count;
288 }
289 }
290}
291
292int string_list_split_in_place(struct string_list *list, char *string,
293 int delim, int maxsplit)
294{
295 int count = 0;
296 char *p = string, *end;
297
298 if (list->strdup_strings)
299 die("internal error in string_list_split_in_place(): "
300 "list->strdup_strings must not be set");
301 for (;;) {
302 count++;
303 if (maxsplit >= 0 && count > maxsplit) {
304 string_list_append(list, p);
305 return count;
306 }
307 end = strchr(p, delim);
308 if (end) {
309 *end = '\0';
310 string_list_append(list, p);
311 p = end + 1;
312 } else {
313 string_list_append(list, p);
314 return count;
315 }
316 }
317}