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