string-list: allow case-insensitive string list
[git] / string-list.c
1 #include "cache.h"
2 #include "string-list.h"
3
4 /* if there is no exact match, point to the index where the entry could be
5  * inserted */
6 static int get_entry_index(const struct string_list *list, const char *string,
7                 int *exact_match)
8 {
9         int left = -1, right = list->nr;
10         compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
11
12         while (left + 1 < right) {
13                 int middle = (left + right) / 2;
14                 int compare = cmp(string, list->items[middle].string);
15                 if (compare < 0)
16                         right = middle;
17                 else if (compare > 0)
18                         left = middle;
19                 else {
20                         *exact_match = 1;
21                         return middle;
22                 }
23         }
24
25         *exact_match = 0;
26         return right;
27 }
28
29 /* returns -1-index if already exists */
30 static int add_entry(int insert_at, struct string_list *list, const char *string)
31 {
32         int exact_match = 0;
33         int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
34
35         if (exact_match)
36                 return -1 - index;
37
38         if (list->nr + 1 >= list->alloc) {
39                 list->alloc += 32;
40                 list->items = xrealloc(list->items, list->alloc
41                                 * sizeof(struct string_list_item));
42         }
43         if (index < list->nr)
44                 memmove(list->items + index + 1, list->items + index,
45                                 (list->nr - 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;
50         list->nr++;
51
52         return index;
53 }
54
55 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
56 {
57         return string_list_insert_at_index(list, -1, string);
58 }
59
60 struct string_list_item *string_list_insert_at_index(struct string_list *list,
61                                                      int insert_at, const char *string)
62 {
63         int index = add_entry(insert_at, list, string);
64
65         if (index < 0)
66                 index = -1 - index;
67
68         return list->items + index;
69 }
70
71 int string_list_has_string(const struct string_list *list, const char *string)
72 {
73         int exact_match;
74         get_entry_index(list, string, &exact_match);
75         return exact_match;
76 }
77
78 int string_list_find_insert_index(const struct string_list *list, const char *string,
79                                   int negative_existing_index)
80 {
81         int exact_match;
82         int index = get_entry_index(list, string, &exact_match);
83         if (exact_match)
84                 index = -1 - (negative_existing_index ? index : 0);
85         return index;
86 }
87
88 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
89 {
90         int exact_match, i = get_entry_index(list, string, &exact_match);
91         if (!exact_match)
92                 return NULL;
93         return list->items + i;
94 }
95
96 void string_list_remove_duplicates(struct string_list *list, int free_util)
97 {
98         if (list->nr > 1) {
99                 int src, dst;
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);
105                                 if (free_util)
106                                         free(list->items[src].util);
107                         } else
108                                 list->items[dst++] = list->items[src];
109                 }
110                 list->nr = dst;
111         }
112 }
113
114 int for_each_string_list(struct string_list *list,
115                          string_list_each_func_t fn, void *cb_data)
116 {
117         int i, ret = 0;
118         for (i = 0; i < list->nr; i++)
119                 if ((ret = fn(&list->items[i], cb_data)))
120                         break;
121         return ret;
122 }
123
124 void filter_string_list(struct string_list *list, int free_util,
125                         string_list_each_func_t want, void *cb_data)
126 {
127         int src, dst = 0;
128         for (src = 0; src < list->nr; src++) {
129                 if (want(&list->items[src], cb_data)) {
130                         list->items[dst++] = list->items[src];
131                 } else {
132                         if (list->strdup_strings)
133                                 free(list->items[src].string);
134                         if (free_util)
135                                 free(list->items[src].util);
136                 }
137         }
138         list->nr = dst;
139 }
140
141 static int item_is_not_empty(struct string_list_item *item, void *unused)
142 {
143         return *item->string != '\0';
144 }
145
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);
148 }
149
150 char *string_list_longest_prefix(const struct string_list *prefixes,
151                                  const char *string)
152 {
153         int i, max_len = -1;
154         char *retval = NULL;
155
156         for (i = 0; i < prefixes->nr; i++) {
157                 char *prefix = prefixes->items[i].string;
158                 if (!prefixcmp(string, prefix)) {
159                         int len = strlen(prefix);
160                         if (len > max_len) {
161                                 retval = prefix;
162                                 max_len = len;
163                         }
164                 }
165         }
166
167         return retval;
168 }
169
170 void string_list_clear(struct string_list *list, int free_util)
171 {
172         if (list->items) {
173                 int i;
174                 if (list->strdup_strings) {
175                         for (i = 0; i < list->nr; i++)
176                                 free(list->items[i].string);
177                 }
178                 if (free_util) {
179                         for (i = 0; i < list->nr; i++)
180                                 free(list->items[i].util);
181                 }
182                 free(list->items);
183         }
184         list->items = NULL;
185         list->nr = list->alloc = 0;
186 }
187
188 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
189 {
190         if (list->items) {
191                 int i;
192                 if (clearfunc) {
193                         for (i = 0; i < list->nr; i++)
194                                 clearfunc(list->items[i].util, list->items[i].string);
195                 }
196                 if (list->strdup_strings) {
197                         for (i = 0; i < list->nr; i++)
198                                 free(list->items[i].string);
199                 }
200                 free(list->items);
201         }
202         list->items = NULL;
203         list->nr = list->alloc = 0;
204 }
205
206
207 void print_string_list(const struct string_list *p, const char *text)
208 {
209         int i;
210         if ( text )
211                 printf("%s\n", text);
212         for (i = 0; i < p->nr; i++)
213                 printf("%s:%p\n", p->items[i].string, p->items[i].util);
214 }
215
216 struct string_list_item *string_list_append_nodup(struct string_list *list,
217                                                   char *string)
218 {
219         struct string_list_item *retval;
220         ALLOC_GROW(list->items, list->nr + 1, list->alloc);
221         retval = &list->items[list->nr++];
222         retval->string = string;
223         retval->util = NULL;
224         return retval;
225 }
226
227 struct string_list_item *string_list_append(struct string_list *list,
228                                             const char *string)
229 {
230         return string_list_append_nodup(
231                         list,
232                         list->strdup_strings ? xstrdup(string) : (char *)string);
233 }
234
235 /* Yuck */
236 static compare_strings_fn compare_for_qsort;
237
238 /* Only call this from inside sort_string_list! */
239 static int cmp_items(const void *a, const void *b)
240 {
241         const struct string_list_item *one = a;
242         const struct string_list_item *two = b;
243         return compare_for_qsort(one->string, two->string);
244 }
245
246 void sort_string_list(struct string_list *list)
247 {
248         compare_for_qsort = list->cmp ? list->cmp : strcmp;
249         qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
250 }
251
252 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
253                                                      const char *string)
254 {
255         int i;
256         compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
257
258         for (i = 0; i < list->nr; i++)
259                 if (!cmp(string, list->items[i].string))
260                         return list->items + i;
261         return NULL;
262 }
263
264 int unsorted_string_list_has_string(struct string_list *list,
265                                     const char *string)
266 {
267         return unsorted_string_list_lookup(list, string) != NULL;
268 }
269
270 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
271 {
272         if (list->strdup_strings)
273                 free(list->items[i].string);
274         if (free_util)
275                 free(list->items[i].util);
276         list->items[i] = list->items[list->nr-1];
277         list->nr--;
278 }
279
280 int string_list_split(struct string_list *list, const char *string,
281                       int delim, int maxsplit)
282 {
283         int count = 0;
284         const char *p = string, *end;
285
286         if (!list->strdup_strings)
287                 die("internal error in string_list_split(): "
288                     "list->strdup_strings must be set");
289         for (;;) {
290                 count++;
291                 if (maxsplit >= 0 && count > maxsplit) {
292                         string_list_append(list, p);
293                         return count;
294                 }
295                 end = strchr(p, delim);
296                 if (end) {
297                         string_list_append_nodup(list, xmemdupz(p, end - p));
298                         p = end + 1;
299                 } else {
300                         string_list_append(list, p);
301                         return count;
302                 }
303         }
304 }
305
306 int string_list_split_in_place(struct string_list *list, char *string,
307                                int delim, int maxsplit)
308 {
309         int count = 0;
310         char *p = string, *end;
311
312         if (list->strdup_strings)
313                 die("internal error in string_list_split_in_place(): "
314                     "list->strdup_strings must not be set");
315         for (;;) {
316                 count++;
317                 if (maxsplit >= 0 && count > maxsplit) {
318                         string_list_append(list, p);
319                         return count;
320                 }
321                 end = strchr(p, delim);
322                 if (end) {
323                         *end = '\0';
324                         string_list_append(list, p);
325                         p = end + 1;
326                 } else {
327                         string_list_append(list, p);
328                         return count;
329                 }
330         }
331 }