| 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 | if (list->nr + 1 >= list->alloc) { |
| 45 | list->alloc += 32; |
| 46 | REALLOC_ARRAY(list->items, list->alloc); |
| 47 | } |
| 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++; |
| 56 | |
| 57 | return index; |
| 58 | } |
| 59 | |
| 60 | struct string_list_item *string_list_insert(struct string_list *list, const char *string) |
| 61 | { |
| 62 | return string_list_insert_at_index(list, -1, string); |
| 63 | } |
| 64 | |
| 65 | struct string_list_item *string_list_insert_at_index(struct string_list *list, |
| 66 | int insert_at, const char *string) |
| 67 | { |
| 68 | int index = add_entry(insert_at, list, string); |
| 69 | |
| 70 | if (index < 0) |
| 71 | index = -1 - index; |
| 72 | |
| 73 | return list->items + index; |
| 74 | } |
| 75 | |
| 76 | int string_list_has_string(const struct string_list *list, const char *string) |
| 77 | { |
| 78 | int exact_match; |
| 79 | get_entry_index(list, string, &exact_match); |
| 80 | return exact_match; |
| 81 | } |
| 82 | |
| 83 | int string_list_find_insert_index(const struct string_list *list, const char *string, |
| 84 | int negative_existing_index) |
| 85 | { |
| 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; |
| 91 | } |
| 92 | |
| 93 | struct string_list_item *string_list_lookup(struct string_list *list, const char *string) |
| 94 | { |
| 95 | int exact_match, i = get_entry_index(list, string, &exact_match); |
| 96 | if (!exact_match) |
| 97 | return NULL; |
| 98 | return list->items + i; |
| 99 | } |
| 100 | |
| 101 | void string_list_remove_duplicates(struct string_list *list, int free_util) |
| 102 | { |
| 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]; |
| 114 | } |
| 115 | list->nr = dst; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | int for_each_string_list(struct string_list *list, |
| 120 | string_list_each_func_t fn, void *cb_data) |
| 121 | { |
| 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; |
| 127 | } |
| 128 | |
| 129 | void filter_string_list(struct string_list *list, int free_util, |
| 130 | string_list_each_func_t want, void *cb_data) |
| 131 | { |
| 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); |
| 141 | } |
| 142 | } |
| 143 | list->nr = dst; |
| 144 | } |
| 145 | |
| 146 | static int item_is_not_empty(struct string_list_item *item, void *unused) |
| 147 | { |
| 148 | return *item->string != '\0'; |
| 149 | } |
| 150 | |
| 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); |
| 153 | } |
| 154 | |
| 155 | void string_list_clear(struct string_list *list, int free_util) |
| 156 | { |
| 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); |
| 162 | } |
| 163 | if (free_util) { |
| 164 | for (i = 0; i < list->nr; i++) |
| 165 | free(list->items[i].util); |
| 166 | } |
| 167 | free(list->items); |
| 168 | } |
| 169 | list->items = NULL; |
| 170 | list->nr = list->alloc = 0; |
| 171 | } |
| 172 | |
| 173 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) |
| 174 | { |
| 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); |
| 180 | } |
| 181 | if (list->strdup_strings) { |
| 182 | for (i = 0; i < list->nr; i++) |
| 183 | free(list->items[i].string); |
| 184 | } |
| 185 | free(list->items); |
| 186 | } |
| 187 | list->items = NULL; |
| 188 | list->nr = list->alloc = 0; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | void print_string_list(const struct string_list *p, const char *text) |
| 193 | { |
| 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); |
| 199 | } |
| 200 | |
| 201 | struct string_list_item *string_list_append_nodup(struct string_list *list, |
| 202 | char *string) |
| 203 | { |
| 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; |
| 210 | } |
| 211 | |
| 212 | struct string_list_item *string_list_append(struct string_list *list, |
| 213 | const char *string) |
| 214 | { |
| 215 | return string_list_append_nodup( |
| 216 | list, |
| 217 | list->strdup_strings ? xstrdup(string) : (char *)string); |
| 218 | } |
| 219 | |
| 220 | /* Yuck */ |
| 221 | static compare_strings_fn compare_for_qsort; |
| 222 | |
| 223 | /* Only call this from inside sort_string_list! */ |
| 224 | static int cmp_items(const void *a, const void *b) |
| 225 | { |
| 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); |
| 229 | } |
| 230 | |
| 231 | void sort_string_list(struct string_list *list) |
| 232 | { |
| 233 | compare_for_qsort = list->cmp ? list->cmp : strcmp; |
| 234 | qsort(list->items, list->nr, sizeof(*list->items), cmp_items); |
| 235 | } |
| 236 | |
| 237 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
| 238 | const char *string) |
| 239 | { |
| 240 | int i; |
| 241 | compare_strings_fn cmp = list->cmp ? list->cmp : strcmp; |
| 242 | |
| 243 | for (i = 0; i < list->nr; i++) |
| 244 | if (!cmp(string, list->items[i].string)) |
| 245 | return list->items + i; |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | int unsorted_string_list_has_string(struct string_list *list, |
| 250 | const char *string) |
| 251 | { |
| 252 | return unsorted_string_list_lookup(list, string) != NULL; |
| 253 | } |
| 254 | |
| 255 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util) |
| 256 | { |
| 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--; |
| 263 | } |
| 264 | |
| 265 | int string_list_split(struct string_list *list, const char *string, |
| 266 | int delim, int maxsplit) |
| 267 | { |
| 268 | int count = 0; |
| 269 | const char *p = string, *end; |
| 270 | |
| 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; |
| 279 | } |
| 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; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | int string_list_split_in_place(struct string_list *list, char *string, |
| 292 | int delim, int maxsplit) |
| 293 | { |
| 294 | int count = 0; |
| 295 | char *p = string, *end; |
| 296 | |
| 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; |
| 305 | } |
| 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; |
| 314 | } |
| 315 | } |
| 316 | } |