signed-push: add "pushee" header to push certificate
[git/jrn.git] / string-list.c
blobdb38b62b46fd15d849ed405f0b60cb7c72eaee40
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 list->items = xrealloc(list->items, list->alloc
47 * sizeof(struct string_list_item));
49 if (index < list->nr)
50 memmove(list->items + index + 1, list->items + index,
51 (list->nr - index)
52 * sizeof(struct string_list_item));
53 list->items[index].string = list->strdup_strings ?
54 xstrdup(string) : (char *)string;
55 list->items[index].util = NULL;
56 list->nr++;
58 return index;
61 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
63 return string_list_insert_at_index(list, -1, string);
66 struct string_list_item *string_list_insert_at_index(struct string_list *list,
67 int insert_at, const char *string)
69 int index = add_entry(insert_at, list, string);
71 if (index < 0)
72 index = -1 - index;
74 return list->items + index;
77 int string_list_has_string(const struct string_list *list, const char *string)
79 int exact_match;
80 get_entry_index(list, string, &exact_match);
81 return exact_match;
84 int string_list_find_insert_index(const struct string_list *list, const char *string,
85 int negative_existing_index)
87 int exact_match;
88 int index = get_entry_index(list, string, &exact_match);
89 if (exact_match)
90 index = -1 - (negative_existing_index ? index : 0);
91 return index;
94 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
96 int exact_match, i = get_entry_index(list, string, &exact_match);
97 if (!exact_match)
98 return NULL;
99 return list->items + i;
102 void string_list_remove_duplicates(struct string_list *list, int free_util)
104 if (list->nr > 1) {
105 int src, dst;
106 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
107 for (src = dst = 1; src < list->nr; src++) {
108 if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
109 if (list->strdup_strings)
110 free(list->items[src].string);
111 if (free_util)
112 free(list->items[src].util);
113 } else
114 list->items[dst++] = list->items[src];
116 list->nr = dst;
120 int for_each_string_list(struct string_list *list,
121 string_list_each_func_t fn, void *cb_data)
123 int i, ret = 0;
124 for (i = 0; i < list->nr; i++)
125 if ((ret = fn(&list->items[i], cb_data)))
126 break;
127 return ret;
130 void filter_string_list(struct string_list *list, int free_util,
131 string_list_each_func_t want, void *cb_data)
133 int src, dst = 0;
134 for (src = 0; src < list->nr; src++) {
135 if (want(&list->items[src], cb_data)) {
136 list->items[dst++] = list->items[src];
137 } else {
138 if (list->strdup_strings)
139 free(list->items[src].string);
140 if (free_util)
141 free(list->items[src].util);
144 list->nr = dst;
147 static int item_is_not_empty(struct string_list_item *item, void *unused)
149 return *item->string != '\0';
152 void string_list_remove_empty_items(struct string_list *list, int free_util) {
153 filter_string_list(list, free_util, item_is_not_empty, NULL);
156 void string_list_clear(struct string_list *list, int free_util)
158 if (list->items) {
159 int i;
160 if (list->strdup_strings) {
161 for (i = 0; i < list->nr; i++)
162 free(list->items[i].string);
164 if (free_util) {
165 for (i = 0; i < list->nr; i++)
166 free(list->items[i].util);
168 free(list->items);
170 list->items = NULL;
171 list->nr = list->alloc = 0;
174 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
176 if (list->items) {
177 int i;
178 if (clearfunc) {
179 for (i = 0; i < list->nr; i++)
180 clearfunc(list->items[i].util, list->items[i].string);
182 if (list->strdup_strings) {
183 for (i = 0; i < list->nr; i++)
184 free(list->items[i].string);
186 free(list->items);
188 list->items = NULL;
189 list->nr = list->alloc = 0;
193 void print_string_list(const struct string_list *p, const char *text)
195 int i;
196 if ( text )
197 printf("%s\n", text);
198 for (i = 0; i < p->nr; i++)
199 printf("%s:%p\n", p->items[i].string, p->items[i].util);
202 struct string_list_item *string_list_append_nodup(struct string_list *list,
203 char *string)
205 struct string_list_item *retval;
206 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
207 retval = &list->items[list->nr++];
208 retval->string = string;
209 retval->util = NULL;
210 return retval;
213 struct string_list_item *string_list_append(struct string_list *list,
214 const char *string)
216 return string_list_append_nodup(
217 list,
218 list->strdup_strings ? xstrdup(string) : (char *)string);
221 /* Yuck */
222 static compare_strings_fn compare_for_qsort;
224 /* Only call this from inside sort_string_list! */
225 static int cmp_items(const void *a, const void *b)
227 const struct string_list_item *one = a;
228 const struct string_list_item *two = b;
229 return compare_for_qsort(one->string, two->string);
232 void sort_string_list(struct string_list *list)
234 compare_for_qsort = list->cmp ? list->cmp : strcmp;
235 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
238 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
239 const char *string)
241 int i;
242 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
244 for (i = 0; i < list->nr; i++)
245 if (!cmp(string, list->items[i].string))
246 return list->items + i;
247 return NULL;
250 int unsorted_string_list_has_string(struct string_list *list,
251 const char *string)
253 return unsorted_string_list_lookup(list, string) != NULL;
256 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
258 if (list->strdup_strings)
259 free(list->items[i].string);
260 if (free_util)
261 free(list->items[i].util);
262 list->items[i] = list->items[list->nr-1];
263 list->nr--;
266 int string_list_split(struct string_list *list, const char *string,
267 int delim, int maxsplit)
269 int count = 0;
270 const char *p = string, *end;
272 if (!list->strdup_strings)
273 die("internal error in string_list_split(): "
274 "list->strdup_strings must be set");
275 for (;;) {
276 count++;
277 if (maxsplit >= 0 && count > maxsplit) {
278 string_list_append(list, p);
279 return count;
281 end = strchr(p, delim);
282 if (end) {
283 string_list_append_nodup(list, xmemdupz(p, end - p));
284 p = end + 1;
285 } else {
286 string_list_append(list, p);
287 return count;
292 int string_list_split_in_place(struct string_list *list, char *string,
293 int delim, int maxsplit)
295 int count = 0;
296 char *p = string, *end;
298 if (list->strdup_strings)
299 die("internal error in string_list_split_in_place(): "
300 "list->strdup_strings must not be set");
301 for (;;) {
302 count++;
303 if (maxsplit >= 0 && count > maxsplit) {
304 string_list_append(list, p);
305 return count;
307 end = strchr(p, delim);
308 if (end) {
309 *end = '\0';
310 string_list_append(list, p);
311 p = end + 1;
312 } else {
313 string_list_append(list, p);
314 return count;