Documentation: fix mismatched delimiters in git-imap-send
[git/debian.git] / string-list.c
blobc5aa0765e8e0a044e9e84e43280d5964f232ef63
1 #include "cache.h"
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
11 * inserted */
12 static int get_entry_index(const struct string_list *list, const char *string,
13 int *exact_match)
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);
21 if (compare < 0)
22 right = middle;
23 else if (compare > 0)
24 left = middle;
25 else {
26 *exact_match = 1;
27 return middle;
31 *exact_match = 0;
32 return right;
35 /* returns -1-index if already exists */
36 static int add_entry(int insert_at, struct string_list *list, const char *string)
38 int exact_match = 0;
39 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
41 if (exact_match)
42 return -1 - index;
44 if (list->nr + 1 >= list->alloc) {
45 list->alloc += 32;
46 REALLOC_ARRAY(list->items, list->alloc);
48 if (index < list->nr)
49 memmove(list->items + index + 1, list->items + index,
50 (list->nr - 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;
55 list->nr++;
57 return index;
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);
70 if (index < 0)
71 index = -1 - index;
73 return list->items + index;
76 int string_list_has_string(const struct string_list *list, const char *string)
78 int exact_match;
79 get_entry_index(list, string, &exact_match);
80 return exact_match;
83 int string_list_find_insert_index(const struct string_list *list, const char *string,
84 int negative_existing_index)
86 int exact_match;
87 int index = get_entry_index(list, string, &exact_match);
88 if (exact_match)
89 index = -1 - (negative_existing_index ? index : 0);
90 return index;
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);
96 if (!exact_match)
97 return NULL;
98 return list->items + i;
101 void string_list_remove_duplicates(struct string_list *list, int free_util)
103 if (list->nr > 1) {
104 int src, dst;
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);
110 if (free_util)
111 free(list->items[src].util);
112 } else
113 list->items[dst++] = list->items[src];
115 list->nr = dst;
119 int for_each_string_list(struct string_list *list,
120 string_list_each_func_t fn, void *cb_data)
122 int i, ret = 0;
123 for (i = 0; i < list->nr; i++)
124 if ((ret = fn(&list->items[i], cb_data)))
125 break;
126 return ret;
129 void filter_string_list(struct string_list *list, int free_util,
130 string_list_each_func_t want, void *cb_data)
132 int src, dst = 0;
133 for (src = 0; src < list->nr; src++) {
134 if (want(&list->items[src], cb_data)) {
135 list->items[dst++] = list->items[src];
136 } else {
137 if (list->strdup_strings)
138 free(list->items[src].string);
139 if (free_util)
140 free(list->items[src].util);
143 list->nr = dst;
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)
157 if (list->items) {
158 int i;
159 if (list->strdup_strings) {
160 for (i = 0; i < list->nr; i++)
161 free(list->items[i].string);
163 if (free_util) {
164 for (i = 0; i < list->nr; i++)
165 free(list->items[i].util);
167 free(list->items);
169 list->items = NULL;
170 list->nr = list->alloc = 0;
173 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
175 if (list->items) {
176 int i;
177 if (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);
185 free(list->items);
187 list->items = NULL;
188 list->nr = list->alloc = 0;
192 void print_string_list(const struct string_list *p, const char *text)
194 int i;
195 if ( 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,
202 char *string)
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;
208 retval->util = NULL;
209 return retval;
212 struct string_list_item *string_list_append(struct string_list *list,
213 const char *string)
215 return string_list_append_nodup(
216 list,
217 list->strdup_strings ? xstrdup(string) : (char *)string);
220 /* Yuck */
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,
238 const char *string)
240 int i;
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;
246 return NULL;
249 int unsorted_string_list_has_string(struct string_list *list,
250 const char *string)
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);
259 if (free_util)
260 free(list->items[i].util);
261 list->items[i] = list->items[list->nr-1];
262 list->nr--;
265 int string_list_split(struct string_list *list, const char *string,
266 int delim, int maxsplit)
268 int count = 0;
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");
274 for (;;) {
275 count++;
276 if (maxsplit >= 0 && count > maxsplit) {
277 string_list_append(list, p);
278 return count;
280 end = strchr(p, delim);
281 if (end) {
282 string_list_append_nodup(list, xmemdupz(p, end - p));
283 p = end + 1;
284 } else {
285 string_list_append(list, p);
286 return count;
291 int string_list_split_in_place(struct string_list *list, char *string,
292 int delim, int maxsplit)
294 int count = 0;
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");
300 for (;;) {
301 count++;
302 if (maxsplit >= 0 && count > maxsplit) {
303 string_list_append(list, p);
304 return count;
306 end = strchr(p, delim);
307 if (end) {
308 *end = '\0';
309 string_list_append(list, p);
310 p = end + 1;
311 } else {
312 string_list_append(list, p);
313 return count;