Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / string-list.c
blobdb473f273e1f77c45f2a5b0cf7a70b106bc19d02
1 #include "git-compat-util.h"
2 #include "string-list.h"
3 #include "alloc.h"
5 void string_list_init_nodup(struct string_list *list)
7 struct string_list blank = STRING_LIST_INIT_NODUP;
8 memcpy(list, &blank, sizeof(*list));
11 void string_list_init_dup(struct string_list *list)
13 struct string_list blank = STRING_LIST_INIT_DUP;
14 memcpy(list, &blank, sizeof(*list));
17 /* if there is no exact match, point to the index where the entry could be
18 * inserted */
19 static int get_entry_index(const struct string_list *list, const char *string,
20 int *exact_match)
22 int left = -1, right = list->nr;
23 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
25 while (left + 1 < right) {
26 int middle = left + (right - left) / 2;
27 int compare = cmp(string, list->items[middle].string);
28 if (compare < 0)
29 right = middle;
30 else if (compare > 0)
31 left = middle;
32 else {
33 *exact_match = 1;
34 return middle;
38 *exact_match = 0;
39 return right;
42 /* returns -1-index if already exists */
43 static int add_entry(int insert_at, struct string_list *list, const char *string)
45 int exact_match = 0;
46 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
48 if (exact_match)
49 return -1 - index;
51 ALLOC_GROW(list->items, list->nr+1, list->alloc);
52 if (index < list->nr)
53 MOVE_ARRAY(list->items + index + 1, list->items + index,
54 list->nr - index);
55 list->items[index].string = list->strdup_strings ?
56 xstrdup(string) : (char *)string;
57 list->items[index].util = NULL;
58 list->nr++;
60 return index;
63 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
65 int index = add_entry(-1, list, string);
67 if (index < 0)
68 index = -1 - index;
70 return list->items + index;
73 void string_list_remove(struct string_list *list, const char *string,
74 int free_util)
76 int exact_match;
77 int i = get_entry_index(list, string, &exact_match);
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);
85 list->nr--;
86 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
90 int string_list_has_string(const struct string_list *list, const char *string)
92 int exact_match;
93 get_entry_index(list, string, &exact_match);
94 return exact_match;
97 int string_list_find_insert_index(const struct string_list *list, const char *string,
98 int negative_existing_index)
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;
107 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
109 int exact_match, i = get_entry_index(list, string, &exact_match);
110 if (!exact_match)
111 return NULL;
112 return list->items + i;
115 void string_list_remove_duplicates(struct string_list *list, int free_util)
117 if (list->nr > 1) {
118 int src, dst;
119 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
120 for (src = dst = 1; src < list->nr; src++) {
121 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
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];
129 list->nr = dst;
133 int for_each_string_list(struct string_list *list,
134 string_list_each_func_t fn, void *cb_data)
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;
143 void filter_string_list(struct string_list *list, int free_util,
144 string_list_each_func_t want, void *cb_data)
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);
157 list->nr = dst;
160 static int item_is_not_empty(struct string_list_item *item, void *data UNUSED)
162 return *item->string != '\0';
165 void string_list_remove_empty_items(struct string_list *list, int free_util)
167 filter_string_list(list, free_util, item_is_not_empty, NULL);
170 void string_list_clear(struct string_list *list, int free_util)
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);
178 if (free_util) {
179 for (i = 0; i < list->nr; i++)
180 free(list->items[i].util);
182 free(list->items);
184 list->items = NULL;
185 list->nr = list->alloc = 0;
188 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
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);
196 if (list->strdup_strings) {
197 for (i = 0; i < list->nr; i++)
198 free(list->items[i].string);
200 free(list->items);
202 list->items = NULL;
203 list->nr = list->alloc = 0;
206 struct string_list_item *string_list_append_nodup(struct string_list *list,
207 char *string)
209 struct string_list_item *retval;
210 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
211 retval = &list->items[list->nr++];
212 retval->string = string;
213 retval->util = NULL;
214 return retval;
217 struct string_list_item *string_list_append(struct string_list *list,
218 const char *string)
220 return string_list_append_nodup(
221 list,
222 list->strdup_strings ? xstrdup(string) : (char *)string);
226 * Encapsulate the compare function pointer because ISO C99 forbids
227 * casting from void * to a function pointer and vice versa.
229 struct string_list_sort_ctx
231 compare_strings_fn cmp;
234 static int cmp_items(const void *a, const void *b, void *ctx)
236 struct string_list_sort_ctx *sort_ctx = ctx;
237 const struct string_list_item *one = a;
238 const struct string_list_item *two = b;
239 return sort_ctx->cmp(one->string, two->string);
242 void string_list_sort(struct string_list *list)
244 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
246 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
249 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
250 const char *string)
252 struct string_list_item *item;
253 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
255 for_each_string_list_item(item, list)
256 if (!cmp(string, item->string))
257 return item;
258 return NULL;
261 int unsorted_string_list_has_string(struct string_list *list,
262 const char *string)
264 return unsorted_string_list_lookup(list, string) != NULL;
267 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
269 if (list->strdup_strings)
270 free(list->items[i].string);
271 if (free_util)
272 free(list->items[i].util);
273 list->items[i] = list->items[list->nr-1];
274 list->nr--;
277 int string_list_split(struct string_list *list, const char *string,
278 int delim, int maxsplit)
280 int count = 0;
281 const char *p = string, *end;
283 if (!list->strdup_strings)
284 die("internal error in string_list_split(): "
285 "list->strdup_strings must be set");
286 for (;;) {
287 count++;
288 if (maxsplit >= 0 && count > maxsplit) {
289 string_list_append(list, p);
290 return count;
292 end = strchr(p, delim);
293 if (end) {
294 string_list_append_nodup(list, xmemdupz(p, end - p));
295 p = end + 1;
296 } else {
297 string_list_append(list, p);
298 return count;
303 int string_list_split_in_place(struct string_list *list, char *string,
304 int delim, int maxsplit)
306 int count = 0;
307 char *p = string, *end;
309 if (list->strdup_strings)
310 die("internal error in string_list_split_in_place(): "
311 "list->strdup_strings must not be set");
312 for (;;) {
313 count++;
314 if (maxsplit >= 0 && count > maxsplit) {
315 string_list_append(list, p);
316 return count;
318 end = strchr(p, delim);
319 if (end) {
320 *end = '\0';
321 string_list_append(list, p);
322 p = end + 1;
323 } else {
324 string_list_append(list, p);
325 return count;