2 #include "string-list.h"
4 void string_list_init(struct string_list *list, int strdup_strings)
6 memset(list, 0, sizeof(*list));
7 list->strdup_strings = strdup_strings;
10 /* if there is no exact match, point to the index where the entry could be
12 static int get_entry_index(const struct string_list *list, const char *string,
15 int left = -1, right = list->nr;
16 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
18 while (left + 1 < right) {
19 int middle = (left + right) / 2;
20 int compare = cmp(string, list->items[middle].string);
35 /* returns -1-index if already exists */
36 static int add_entry(int insert_at, struct string_list *list, const char *string)
39 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
44 if (list->nr + 1 >= list->alloc) {
46 REALLOC_ARRAY(list->items, list->alloc);
49 memmove(list->items + index + 1, list->items + index,
51 * sizeof(struct string_list_item));
52 list->items[index].string = list->strdup_strings ?
53 xstrdup(string) : (char *)string;
54 list->items[index].util = NULL;
60 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
62 int index = add_entry(-1, list, string);
67 return list->items + index;
70 int string_list_has_string(const struct string_list *list, const char *string)
73 get_entry_index(list, string, &exact_match);
77 int string_list_find_insert_index(const struct string_list *list, const char *string,
78 int negative_existing_index)
81 int index = get_entry_index(list, string, &exact_match);
83 index = -1 - (negative_existing_index ? index : 0);
87 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
89 int exact_match, i = get_entry_index(list, string, &exact_match);
92 return list->items + i;
95 void string_list_remove_duplicates(struct string_list *list, int free_util)
99 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
100 for (src = dst = 1; src < list->nr; src++) {
101 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
102 if (list->strdup_strings)
103 free(list->items[src].string);
105 free(list->items[src].util);
107 list->items[dst++] = list->items[src];
113 int for_each_string_list(struct string_list *list,
114 string_list_each_func_t fn, void *cb_data)
117 for (i = 0; i < list->nr; i++)
118 if ((ret = fn(&list->items[i], cb_data)))
123 void filter_string_list(struct string_list *list, int free_util,
124 string_list_each_func_t want, void *cb_data)
127 for (src = 0; src < list->nr; src++) {
128 if (want(&list->items[src], cb_data)) {
129 list->items[dst++] = list->items[src];
131 if (list->strdup_strings)
132 free(list->items[src].string);
134 free(list->items[src].util);
140 static int item_is_not_empty(struct string_list_item *item, void *unused)
142 return *item->string != '\0';
145 void string_list_remove_empty_items(struct string_list *list, int free_util) {
146 filter_string_list(list, free_util, item_is_not_empty, NULL);
149 void string_list_clear(struct string_list *list, int free_util)
153 if (list->strdup_strings) {
154 for (i = 0; i < list->nr; i++)
155 free(list->items[i].string);
158 for (i = 0; i < list->nr; i++)
159 free(list->items[i].util);
164 list->nr = list->alloc = 0;
167 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
172 for (i = 0; i < list->nr; i++)
173 clearfunc(list->items[i].util, list->items[i].string);
175 if (list->strdup_strings) {
176 for (i = 0; i < list->nr; i++)
177 free(list->items[i].string);
182 list->nr = list->alloc = 0;
186 void print_string_list(const struct string_list *p, const char *text)
190 printf("%s\n", text);
191 for (i = 0; i < p->nr; i++)
192 printf("%s:%p\n", p->items[i].string, p->items[i].util);
195 struct string_list_item *string_list_append_nodup(struct string_list *list,
198 struct string_list_item *retval;
199 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
200 retval = &list->items[list->nr++];
201 retval->string = string;
206 struct string_list_item *string_list_append(struct string_list *list,
209 return string_list_append_nodup(
211 list->strdup_strings ? xstrdup(string) : (char *)string);
215 static compare_strings_fn compare_for_qsort;
217 /* Only call this from inside string_list_sort! */
218 static int cmp_items(const void *a, const void *b)
220 const struct string_list_item *one = a;
221 const struct string_list_item *two = b;
222 return compare_for_qsort(one->string, two->string);
225 void string_list_sort(struct string_list *list)
227 compare_for_qsort = list->cmp ? list->cmp : strcmp;
228 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
231 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
234 struct string_list_item *item;
235 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
237 for_each_string_list_item(item, list)
238 if (!cmp(string, item->string))
243 int unsorted_string_list_has_string(struct string_list *list,
246 return unsorted_string_list_lookup(list, string) != NULL;
249 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
251 if (list->strdup_strings)
252 free(list->items[i].string);
254 free(list->items[i].util);
255 list->items[i] = list->items[list->nr-1];
259 int string_list_split(struct string_list *list, const char *string,
260 int delim, int maxsplit)
263 const char *p = string, *end;
265 if (!list->strdup_strings)
266 die("internal error in string_list_split(): "
267 "list->strdup_strings must be set");
270 if (maxsplit >= 0 && count > maxsplit) {
271 string_list_append(list, p);
274 end = strchr(p, delim);
276 string_list_append_nodup(list, xmemdupz(p, end - p));
279 string_list_append(list, p);
285 int string_list_split_in_place(struct string_list *list, char *string,
286 int delim, int maxsplit)
289 char *p = string, *end;
291 if (list->strdup_strings)
292 die("internal error in string_list_split_in_place(): "
293 "list->strdup_strings must not be set");
296 if (maxsplit >= 0 && count > maxsplit) {
297 string_list_append(list, p);
300 end = strchr(p, delim);
303 string_list_append(list, p);
306 string_list_append(list, p);