2 #include "string-list.h"
4 /* if there is no exact match, point to the index where the entry could be
6 static int get_entry_index(const struct string_list *list, const char *string,
9 int left = -1, right = list->nr;
10 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
12 while (left + 1 < right) {
13 int middle = (left + right) / 2;
14 int compare = cmp(string, list->items[middle].string);
29 /* returns -1-index if already exists */
30 static int add_entry(int insert_at, struct string_list *list, const char *string)
33 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
38 if (list->nr + 1 >= list->alloc) {
40 list->items = xrealloc(list->items, list->alloc
41 * sizeof(struct string_list_item));
44 memmove(list->items + index + 1, list->items + index,
46 * sizeof(struct string_list_item));
47 list->items[index].string = list->strdup_strings ?
48 xstrdup(string) : (char *)string;
49 list->items[index].util = NULL;
55 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
57 return string_list_insert_at_index(list, -1, string);
60 struct string_list_item *string_list_insert_at_index(struct string_list *list,
61 int insert_at, const char *string)
63 int index = add_entry(insert_at, list, string);
68 return list->items + index;
71 int string_list_has_string(const struct string_list *list, const char *string)
74 get_entry_index(list, string, &exact_match);
78 int string_list_find_insert_index(const struct string_list *list, const char *string,
79 int negative_existing_index)
82 int index = get_entry_index(list, string, &exact_match);
84 index = -1 - (negative_existing_index ? index : 0);
88 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
90 int exact_match, i = get_entry_index(list, string, &exact_match);
93 return list->items + i;
96 void string_list_remove_duplicates(struct string_list *list, int free_util)
100 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
101 for (src = dst = 1; src < list->nr; src++) {
102 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
103 if (list->strdup_strings)
104 free(list->items[src].string);
106 free(list->items[src].util);
108 list->items[dst++] = list->items[src];
114 int for_each_string_list(struct string_list *list,
115 string_list_each_func_t fn, void *cb_data)
118 for (i = 0; i < list->nr; i++)
119 if ((ret = fn(&list->items[i], cb_data)))
124 void filter_string_list(struct string_list *list, int free_util,
125 string_list_each_func_t want, void *cb_data)
128 for (src = 0; src < list->nr; src++) {
129 if (want(&list->items[src], cb_data)) {
130 list->items[dst++] = list->items[src];
132 if (list->strdup_strings)
133 free(list->items[src].string);
135 free(list->items[src].util);
141 static int item_is_not_empty(struct string_list_item *item, void *unused)
143 return *item->string != '\0';
146 void string_list_remove_empty_items(struct string_list *list, int free_util) {
147 filter_string_list(list, free_util, item_is_not_empty, NULL);
150 void string_list_clear(struct string_list *list, int free_util)
154 if (list->strdup_strings) {
155 for (i = 0; i < list->nr; i++)
156 free(list->items[i].string);
159 for (i = 0; i < list->nr; i++)
160 free(list->items[i].util);
165 list->nr = list->alloc = 0;
168 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
173 for (i = 0; i < list->nr; i++)
174 clearfunc(list->items[i].util, list->items[i].string);
176 if (list->strdup_strings) {
177 for (i = 0; i < list->nr; i++)
178 free(list->items[i].string);
183 list->nr = list->alloc = 0;
187 void print_string_list(const struct string_list *p, const char *text)
191 printf("%s\n", text);
192 for (i = 0; i < p->nr; i++)
193 printf("%s:%p\n", p->items[i].string, p->items[i].util);
196 struct string_list_item *string_list_append_nodup(struct string_list *list,
199 struct string_list_item *retval;
200 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
201 retval = &list->items[list->nr++];
202 retval->string = string;
207 struct string_list_item *string_list_append(struct string_list *list,
210 return string_list_append_nodup(
212 list->strdup_strings ? xstrdup(string) : (char *)string);
216 static compare_strings_fn compare_for_qsort;
218 /* Only call this from inside sort_string_list! */
219 static int cmp_items(const void *a, const void *b)
221 const struct string_list_item *one = a;
222 const struct string_list_item *two = b;
223 return compare_for_qsort(one->string, two->string);
226 void sort_string_list(struct string_list *list)
228 compare_for_qsort = list->cmp ? list->cmp : strcmp;
229 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
232 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
236 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
238 for (i = 0; i < list->nr; i++)
239 if (!cmp(string, list->items[i].string))
240 return list->items + i;
244 int unsorted_string_list_has_string(struct string_list *list,
247 return unsorted_string_list_lookup(list, string) != NULL;
250 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
252 if (list->strdup_strings)
253 free(list->items[i].string);
255 free(list->items[i].util);
256 list->items[i] = list->items[list->nr-1];
260 int string_list_split(struct string_list *list, const char *string,
261 int delim, int maxsplit)
264 const char *p = string, *end;
266 if (!list->strdup_strings)
267 die("internal error in string_list_split(): "
268 "list->strdup_strings must be set");
271 if (maxsplit >= 0 && count > maxsplit) {
272 string_list_append(list, p);
275 end = strchr(p, delim);
277 string_list_append_nodup(list, xmemdupz(p, end - p));
280 string_list_append(list, p);
286 int string_list_split_in_place(struct string_list *list, char *string,
287 int delim, int maxsplit)
290 char *p = string, *end;
292 if (list->strdup_strings)
293 die("internal error in string_list_split_in_place(): "
294 "list->strdup_strings must not be set");
297 if (maxsplit >= 0 && count > maxsplit) {
298 string_list_append(list, p);
301 end = strchr(p, delim);
304 string_list_append(list, p);
307 string_list_append(list, p);