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 - left) / 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 ALLOC_GROW(list->items, list->nr+1, list->alloc);
46 MOVE_ARRAY(list->items + index + 1, list->items + index,
48 list->items[index].string = list->strdup_strings ?
49 xstrdup(string) : (char *)string;
50 list->items[index].util = NULL;
56 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
58 int index = add_entry(-1, list, string);
63 return list->items + index;
66 void string_list_remove(struct string_list *list, const char *string,
70 int i = get_entry_index(list, string, &exact_match);
73 if (list->strdup_strings)
74 free(list->items[i].string);
76 free(list->items[i].util);
79 MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
83 int string_list_has_string(const struct string_list *list, const char *string)
86 get_entry_index(list, string, &exact_match);
90 int string_list_find_insert_index(const struct string_list *list, const char *string,
91 int negative_existing_index)
94 int index = get_entry_index(list, string, &exact_match);
96 index = -1 - (negative_existing_index ? index : 0);
100 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
102 int exact_match, i = get_entry_index(list, string, &exact_match);
105 return list->items + i;
108 void string_list_remove_duplicates(struct string_list *list, int free_util)
112 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
113 for (src = dst = 1; src < list->nr; src++) {
114 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
115 if (list->strdup_strings)
116 free(list->items[src].string);
118 free(list->items[src].util);
120 list->items[dst++] = list->items[src];
126 int for_each_string_list(struct string_list *list,
127 string_list_each_func_t fn, void *cb_data)
130 for (i = 0; i < list->nr; i++)
131 if ((ret = fn(&list->items[i], cb_data)))
136 void filter_string_list(struct string_list *list, int free_util,
137 string_list_each_func_t want, void *cb_data)
140 for (src = 0; src < list->nr; src++) {
141 if (want(&list->items[src], cb_data)) {
142 list->items[dst++] = list->items[src];
144 if (list->strdup_strings)
145 free(list->items[src].string);
147 free(list->items[src].util);
153 static int item_is_not_empty(struct string_list_item *item, void *unused)
155 return *item->string != '\0';
158 void string_list_remove_empty_items(struct string_list *list, int free_util) {
159 filter_string_list(list, free_util, item_is_not_empty, NULL);
162 void string_list_clear(struct string_list *list, int free_util)
166 if (list->strdup_strings) {
167 for (i = 0; i < list->nr; i++)
168 free(list->items[i].string);
171 for (i = 0; i < list->nr; i++)
172 free(list->items[i].util);
177 list->nr = list->alloc = 0;
180 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
185 for (i = 0; i < list->nr; i++)
186 clearfunc(list->items[i].util, list->items[i].string);
188 if (list->strdup_strings) {
189 for (i = 0; i < list->nr; i++)
190 free(list->items[i].string);
195 list->nr = list->alloc = 0;
199 void print_string_list(const struct string_list *p, const char *text)
203 printf("%s\n", text);
204 for (i = 0; i < p->nr; i++)
205 printf("%s:%p\n", p->items[i].string, p->items[i].util);
208 struct string_list_item *string_list_append_nodup(struct string_list *list,
211 struct string_list_item *retval;
212 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
213 retval = &list->items[list->nr++];
214 retval->string = string;
219 struct string_list_item *string_list_append(struct string_list *list,
222 return string_list_append_nodup(
224 list->strdup_strings ? xstrdup(string) : (char *)string);
228 * Encapsulate the compare function pointer because ISO C99 forbids
229 * casting from void * to a function pointer and vice versa.
231 struct string_list_sort_ctx
233 compare_strings_fn cmp;
236 static int cmp_items(const void *a, const void *b, void *ctx)
238 struct string_list_sort_ctx *sort_ctx = ctx;
239 const struct string_list_item *one = a;
240 const struct string_list_item *two = b;
241 return sort_ctx->cmp(one->string, two->string);
244 void string_list_sort(struct string_list *list)
246 struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
248 QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
251 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
254 struct string_list_item *item;
255 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
257 for_each_string_list_item(item, list)
258 if (!cmp(string, item->string))
263 int unsorted_string_list_has_string(struct string_list *list,
266 return unsorted_string_list_lookup(list, string) != NULL;
269 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
271 if (list->strdup_strings)
272 free(list->items[i].string);
274 free(list->items[i].util);
275 list->items[i] = list->items[list->nr-1];
279 int string_list_split(struct string_list *list, const char *string,
280 int delim, int maxsplit)
283 const char *p = string, *end;
285 if (!list->strdup_strings)
286 die("internal error in string_list_split(): "
287 "list->strdup_strings must be set");
290 if (maxsplit >= 0 && count > maxsplit) {
291 string_list_append(list, p);
294 end = strchr(p, delim);
296 string_list_append_nodup(list, xmemdupz(p, end - p));
299 string_list_append(list, p);
305 int string_list_split_in_place(struct string_list *list, char *string,
306 int delim, int maxsplit)
309 char *p = string, *end;
311 if (list->strdup_strings)
312 die("internal error in string_list_split_in_place(): "
313 "list->strdup_strings must not be set");
316 if (maxsplit >= 0 && count > maxsplit) {
317 string_list_append(list, p);
320 end = strchr(p, delim);
323 string_list_append(list, p);
326 string_list_append(list, p);