hex: retire get_sha1_hex()
[alt-git.git] / string-list.c
blob0f8ac117fd584828978568ff96ce21a63a8b160f
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 void string_list_setlen(struct string_list *list, size_t nr)
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;
215 struct string_list_item *string_list_append_nodup(struct string_list *list,
216 char *string)
218 struct string_list_item *retval;
219 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
220 retval = &list->items[list->nr++];
221 retval->string = string;
222 retval->util = NULL;
223 return retval;
226 struct string_list_item *string_list_append(struct string_list *list,
227 const char *string)
229 return string_list_append_nodup(
230 list,
231 list->strdup_strings ? xstrdup(string) : (char *)string);
235 * Encapsulate the compare function pointer because ISO C99 forbids
236 * casting from void * to a function pointer and vice versa.
238 struct string_list_sort_ctx
240 compare_strings_fn cmp;
243 static int cmp_items(const void *a, const void *b, void *ctx)
245 struct string_list_sort_ctx *sort_ctx = ctx;
246 const struct string_list_item *one = a;
247 const struct string_list_item *two = b;
248 return sort_ctx->cmp(one->string, two->string);
251 void string_list_sort(struct string_list *list)
253 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
255 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
258 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
259 const char *string)
261 struct string_list_item *item;
262 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
264 for_each_string_list_item(item, list)
265 if (!cmp(string, item->string))
266 return item;
267 return NULL;
270 int unsorted_string_list_has_string(struct string_list *list,
271 const char *string)
273 return unsorted_string_list_lookup(list, string) != NULL;
276 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
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--;
286 int string_list_split(struct string_list *list, const char *string,
287 int delim, int maxsplit)
289 int count = 0;
290 const char *p = string, *end;
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;
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;
312 int string_list_split_in_place(struct string_list *list, char *string,
313 const char *delim, int maxsplit)
315 int count = 0;
316 char *p = string, *end;
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;
327 end = strpbrk(p, delim);
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;