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 return string_list_insert_at_index(list, -1, string);
65 struct string_list_item *string_list_insert_at_index(struct string_list *list,
66 int insert_at, const char *string)
68 int index = add_entry(insert_at, list, string);
73 return list->items + index;
76 int string_list_has_string(const struct string_list *list, const char *string)
79 get_entry_index(list, string, &exact_match);
83 int string_list_find_insert_index(const struct string_list *list, const char *string,
84 int negative_existing_index)
87 int index = get_entry_index(list, string, &exact_match);
89 index = -1 - (negative_existing_index ? index : 0);
93 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
95 int exact_match, i = get_entry_index(list, string, &exact_match);
98 return list->items + i;
101 void string_list_remove_duplicates(struct string_list *list, int free_util)
105 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
106 for (src = dst = 1; src < list->nr; src++) {
107 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
108 if (list->strdup_strings)
109 free(list->items[src].string);
111 free(list->items[src].util);
113 list->items[dst++] = list->items[src];
119 int for_each_string_list(struct string_list *list,
120 string_list_each_func_t fn, void *cb_data)
123 for (i = 0; i < list->nr; i++)
124 if ((ret = fn(&list->items[i], cb_data)))
129 void filter_string_list(struct string_list *list, int free_util,
130 string_list_each_func_t want, void *cb_data)
133 for (src = 0; src < list->nr; src++) {
134 if (want(&list->items[src], cb_data)) {
135 list->items[dst++] = list->items[src];
137 if (list->strdup_strings)
138 free(list->items[src].string);
140 free(list->items[src].util);
146 static int item_is_not_empty(struct string_list_item *item, void *unused)
148 return *item->string != '\0';
151 void string_list_remove_empty_items(struct string_list *list, int free_util) {
152 filter_string_list(list, free_util, item_is_not_empty, NULL);
155 void string_list_clear(struct string_list *list, int free_util)
159 if (list->strdup_strings) {
160 for (i = 0; i < list->nr; i++)
161 free(list->items[i].string);
164 for (i = 0; i < list->nr; i++)
165 free(list->items[i].util);
170 list->nr = list->alloc = 0;
173 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
178 for (i = 0; i < list->nr; i++)
179 clearfunc(list->items[i].util, list->items[i].string);
181 if (list->strdup_strings) {
182 for (i = 0; i < list->nr; i++)
183 free(list->items[i].string);
188 list->nr = list->alloc = 0;
192 void print_string_list(const struct string_list *p, const char *text)
196 printf("%s\n", text);
197 for (i = 0; i < p->nr; i++)
198 printf("%s:%p\n", p->items[i].string, p->items[i].util);
201 struct string_list_item *string_list_append_nodup(struct string_list *list,
204 struct string_list_item *retval;
205 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
206 retval = &list->items[list->nr++];
207 retval->string = string;
212 struct string_list_item *string_list_append(struct string_list *list,
215 return string_list_append_nodup(
217 list->strdup_strings ? xstrdup(string) : (char *)string);
221 static compare_strings_fn compare_for_qsort;
223 /* Only call this from inside sort_string_list! */
224 static int cmp_items(const void *a, const void *b)
226 const struct string_list_item *one = a;
227 const struct string_list_item *two = b;
228 return compare_for_qsort(one->string, two->string);
231 void sort_string_list(struct string_list *list)
233 compare_for_qsort = list->cmp ? list->cmp : strcmp;
234 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
237 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
241 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
243 for (i = 0; i < list->nr; i++)
244 if (!cmp(string, list->items[i].string))
245 return list->items + i;
249 int unsorted_string_list_has_string(struct string_list *list,
252 return unsorted_string_list_lookup(list, string) != NULL;
255 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
257 if (list->strdup_strings)
258 free(list->items[i].string);
260 free(list->items[i].util);
261 list->items[i] = list->items[list->nr-1];
265 int string_list_split(struct string_list *list, const char *string,
266 int delim, int maxsplit)
269 const char *p = string, *end;
271 if (!list->strdup_strings)
272 die("internal error in string_list_split(): "
273 "list->strdup_strings must be set");
276 if (maxsplit >= 0 && count > maxsplit) {
277 string_list_append(list, p);
280 end = strchr(p, delim);
282 string_list_append_nodup(list, xmemdupz(p, end - p));
285 string_list_append(list, p);
291 int string_list_split_in_place(struct string_list *list, char *string,
292 int delim, int maxsplit)
295 char *p = string, *end;
297 if (list->strdup_strings)
298 die("internal error in string_list_split_in_place(): "
299 "list->strdup_strings must not be set");
302 if (maxsplit >= 0 && count > maxsplit) {
303 string_list_append(list, p);
306 end = strchr(p, delim);
309 string_list_append(list, p);
312 string_list_append(list, p);