Elijah Newren | 36bf195 | 2023-02-24 00:09:24 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 2 | #include "string-list.h" |
| 3 | |
Ævar Arnfjörð Bjarmason | 770feda | 2021-07-01 12:51:28 +0200 | [diff] [blame] | 4 | void string_list_init_nodup(struct string_list *list) |
| 5 | { |
| 6 | struct string_list blank = STRING_LIST_INIT_NODUP; |
| 7 | memcpy(list, &blank, sizeof(*list)); |
| 8 | } |
| 9 | |
| 10 | void string_list_init_dup(struct string_list *list) |
| 11 | { |
| 12 | struct string_list blank = STRING_LIST_INIT_DUP; |
| 13 | memcpy(list, &blank, sizeof(*list)); |
| 14 | } |
| 15 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 16 | /* if there is no exact match, point to the index where the entry could be |
| 17 | * inserted */ |
| 18 | static int get_entry_index(const struct string_list *list, const char *string, |
| 19 | int *exact_match) |
| 20 | { |
| 21 | int left = -1, right = list->nr; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 22 | compare_strings_fn cmp = list->cmp ? list->cmp : strcmp; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 23 | |
| 24 | while (left + 1 < right) { |
Derrick Stolee | 19716b2 | 2017-10-08 14:29:37 -0400 | [diff] [blame] | 25 | int middle = left + (right - left) / 2; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 26 | int compare = cmp(string, list->items[middle].string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 27 | if (compare < 0) |
| 28 | right = middle; |
| 29 | else if (compare > 0) |
| 30 | left = middle; |
| 31 | else { |
| 32 | *exact_match = 1; |
| 33 | return middle; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | *exact_match = 0; |
| 38 | return right; |
| 39 | } |
| 40 | |
| 41 | /* returns -1-index if already exists */ |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 42 | static int add_entry(int insert_at, struct string_list *list, const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 43 | { |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 44 | int exact_match = 0; |
| 45 | int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 46 | |
| 47 | if (exact_match) |
| 48 | return -1 - index; |
| 49 | |
Jeff Hostetler | 950a234 | 2017-04-14 19:51:52 +0000 | [diff] [blame] | 50 | ALLOC_GROW(list->items, list->nr+1, list->alloc); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 51 | if (index < list->nr) |
René Scharfe | f331ab9 | 2017-07-15 22:00:45 +0200 | [diff] [blame] | 52 | MOVE_ARRAY(list->items + index + 1, list->items + index, |
| 53 | list->nr - index); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 54 | list->items[index].string = list->strdup_strings ? |
| 55 | xstrdup(string) : (char *)string; |
| 56 | list->items[index].util = NULL; |
| 57 | list->nr++; |
| 58 | |
| 59 | return index; |
| 60 | } |
| 61 | |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 62 | struct string_list_item *string_list_insert(struct string_list *list, const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 63 | { |
Stefan Beller | f8c4ab6 | 2014-11-24 13:22:04 -0800 | [diff] [blame] | 64 | int index = add_entry(-1, list, string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 65 | |
| 66 | if (index < 0) |
| 67 | index = -1 - index; |
| 68 | |
| 69 | return list->items + index; |
| 70 | } |
| 71 | |
Brandon Williams | 3a30033 | 2017-04-19 16:13:21 -0700 | [diff] [blame] | 72 | void string_list_remove(struct string_list *list, const char *string, |
| 73 | int free_util) |
| 74 | { |
| 75 | int exact_match; |
| 76 | int i = get_entry_index(list, string, &exact_match); |
| 77 | |
| 78 | if (exact_match) { |
| 79 | if (list->strdup_strings) |
| 80 | free(list->items[i].string); |
| 81 | if (free_util) |
| 82 | free(list->items[i].util); |
| 83 | |
| 84 | list->nr--; |
René Scharfe | f331ab9 | 2017-07-15 22:00:45 +0200 | [diff] [blame] | 85 | MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i); |
Brandon Williams | 3a30033 | 2017-04-19 16:13:21 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 89 | int string_list_has_string(const struct string_list *list, const char *string) |
| 90 | { |
| 91 | int exact_match; |
| 92 | get_entry_index(list, string, &exact_match); |
| 93 | return exact_match; |
| 94 | } |
| 95 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 96 | int string_list_find_insert_index(const struct string_list *list, const char *string, |
| 97 | int negative_existing_index) |
| 98 | { |
| 99 | int exact_match; |
| 100 | int index = get_entry_index(list, string, &exact_match); |
| 101 | if (exact_match) |
| 102 | index = -1 - (negative_existing_index ? index : 0); |
| 103 | return index; |
| 104 | } |
| 105 | |
Julian Phillips | e8c8b71 | 2010-06-26 00:41:37 +0100 | [diff] [blame] | 106 | struct string_list_item *string_list_lookup(struct string_list *list, const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 107 | { |
| 108 | int exact_match, i = get_entry_index(list, string, &exact_match); |
| 109 | if (!exact_match) |
| 110 | return NULL; |
| 111 | return list->items + i; |
| 112 | } |
| 113 | |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 114 | void string_list_remove_duplicates(struct string_list *list, int free_util) |
| 115 | { |
| 116 | if (list->nr > 1) { |
| 117 | int src, dst; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 118 | compare_strings_fn cmp = list->cmp ? list->cmp : strcmp; |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 119 | for (src = dst = 1; src < list->nr; src++) { |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 120 | if (!cmp(list->items[dst - 1].string, list->items[src].string)) { |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 121 | if (list->strdup_strings) |
| 122 | free(list->items[src].string); |
| 123 | if (free_util) |
| 124 | free(list->items[src].util); |
| 125 | } else |
| 126 | list->items[dst++] = list->items[src]; |
| 127 | } |
| 128 | list->nr = dst; |
| 129 | } |
| 130 | } |
| 131 | |
Julian Phillips | b684e97 | 2010-06-26 00:41:34 +0100 | [diff] [blame] | 132 | int for_each_string_list(struct string_list *list, |
| 133 | string_list_each_func_t fn, void *cb_data) |
Jay Soffian | c6f5a7a | 2009-02-25 03:32:18 -0500 | [diff] [blame] | 134 | { |
| 135 | int i, ret = 0; |
| 136 | for (i = 0; i < list->nr; i++) |
| 137 | if ((ret = fn(&list->items[i], cb_data))) |
| 138 | break; |
| 139 | return ret; |
| 140 | } |
| 141 | |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 142 | void filter_string_list(struct string_list *list, int free_util, |
| 143 | string_list_each_func_t want, void *cb_data) |
| 144 | { |
| 145 | int src, dst = 0; |
| 146 | for (src = 0; src < list->nr; src++) { |
| 147 | if (want(&list->items[src], cb_data)) { |
| 148 | list->items[dst++] = list->items[src]; |
| 149 | } else { |
| 150 | if (list->strdup_strings) |
| 151 | free(list->items[src].string); |
| 152 | if (free_util) |
| 153 | free(list->items[src].util); |
| 154 | } |
| 155 | } |
| 156 | list->nr = dst; |
| 157 | } |
| 158 | |
Jeff King | 1ee3471 | 2022-10-17 21:05:32 -0400 | [diff] [blame] | 159 | static int item_is_not_empty(struct string_list_item *item, void *data UNUSED) |
Michael Haggerty | 6bb2a13 | 2012-11-04 08:07:06 +0100 | [diff] [blame] | 160 | { |
| 161 | return *item->string != '\0'; |
| 162 | } |
| 163 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 164 | void string_list_remove_empty_items(struct string_list *list, int free_util) |
| 165 | { |
Michael Haggerty | 6bb2a13 | 2012-11-04 08:07:06 +0100 | [diff] [blame] | 166 | filter_string_list(list, free_util, item_is_not_empty, NULL); |
| 167 | } |
| 168 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 169 | void string_list_clear(struct string_list *list, int free_util) |
| 170 | { |
| 171 | if (list->items) { |
| 172 | int i; |
| 173 | if (list->strdup_strings) { |
| 174 | for (i = 0; i < list->nr; i++) |
| 175 | free(list->items[i].string); |
| 176 | } |
| 177 | if (free_util) { |
| 178 | for (i = 0; i < list->nr; i++) |
| 179 | free(list->items[i].util); |
| 180 | } |
| 181 | free(list->items); |
| 182 | } |
| 183 | list->items = NULL; |
| 184 | list->nr = list->alloc = 0; |
| 185 | } |
| 186 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 187 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) |
| 188 | { |
| 189 | if (list->items) { |
| 190 | int i; |
| 191 | if (clearfunc) { |
| 192 | for (i = 0; i < list->nr; i++) |
| 193 | clearfunc(list->items[i].util, list->items[i].string); |
| 194 | } |
| 195 | if (list->strdup_strings) { |
| 196 | for (i = 0; i < list->nr; i++) |
| 197 | free(list->items[i].string); |
| 198 | } |
| 199 | free(list->items); |
| 200 | } |
| 201 | list->items = NULL; |
| 202 | list->nr = list->alloc = 0; |
| 203 | } |
| 204 | |
Taylor Blau | 492ba81 | 2023-04-24 18:20:14 -0400 | [diff] [blame] | 205 | void string_list_setlen(struct string_list *list, size_t nr) |
| 206 | { |
| 207 | if (list->strdup_strings) |
| 208 | BUG("cannot setlen a string_list which owns its entries"); |
| 209 | if (nr > list->nr) |
| 210 | BUG("cannot grow a string_list with setlen"); |
| 211 | list->nr = nr; |
| 212 | } |
| 213 | |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 214 | struct string_list_item *string_list_append_nodup(struct string_list *list, |
| 215 | char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 216 | { |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 217 | struct string_list_item *retval; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 218 | ALLOC_GROW(list->items, list->nr + 1, list->alloc); |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 219 | retval = &list->items[list->nr++]; |
| 220 | retval->string = string; |
| 221 | retval->util = NULL; |
| 222 | return retval; |
| 223 | } |
| 224 | |
| 225 | struct string_list_item *string_list_append(struct string_list *list, |
| 226 | const char *string) |
| 227 | { |
| 228 | return string_list_append_nodup( |
| 229 | list, |
| 230 | list->strdup_strings ? xstrdup(string) : (char *)string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Beat Bolli | b6d3f5a | 2018-07-09 21:25:36 +0200 | [diff] [blame] | 233 | /* |
| 234 | * Encapsulate the compare function pointer because ISO C99 forbids |
| 235 | * casting from void * to a function pointer and vice versa. |
| 236 | */ |
| 237 | struct string_list_sort_ctx |
| 238 | { |
| 239 | compare_strings_fn cmp; |
| 240 | }; |
| 241 | |
René Scharfe | 5ebd947 | 2017-01-22 18:57:09 +0100 | [diff] [blame] | 242 | static int cmp_items(const void *a, const void *b, void *ctx) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 243 | { |
Beat Bolli | b6d3f5a | 2018-07-09 21:25:36 +0200 | [diff] [blame] | 244 | struct string_list_sort_ctx *sort_ctx = ctx; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 245 | const struct string_list_item *one = a; |
| 246 | const struct string_list_item *two = b; |
Beat Bolli | b6d3f5a | 2018-07-09 21:25:36 +0200 | [diff] [blame] | 247 | return sort_ctx->cmp(one->string, two->string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 250 | void string_list_sort(struct string_list *list) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 251 | { |
Beat Bolli | b6d3f5a | 2018-07-09 21:25:36 +0200 | [diff] [blame] | 252 | struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp}; |
| 253 | |
| 254 | QSORT_S(list->items, list->nr, cmp_items, &sort_ctx); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 257 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
| 258 | const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 259 | { |
Ralf Thielow | d16df0c | 2016-04-25 19:40:00 +0200 | [diff] [blame] | 260 | struct string_list_item *item; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 261 | compare_strings_fn cmp = list->cmp ? list->cmp : strcmp; |
| 262 | |
Ralf Thielow | d16df0c | 2016-04-25 19:40:00 +0200 | [diff] [blame] | 263 | for_each_string_list_item(item, list) |
| 264 | if (!cmp(string, item->string)) |
| 265 | return item; |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | int unsorted_string_list_has_string(struct string_list *list, |
| 270 | const char *string) |
| 271 | { |
| 272 | return unsorted_string_list_lookup(list, string) != NULL; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Johannes Sixt | 86d4b52 | 2011-08-11 23:20:00 -0600 | [diff] [blame] | 275 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util) |
| 276 | { |
| 277 | if (list->strdup_strings) |
| 278 | free(list->items[i].string); |
| 279 | if (free_util) |
| 280 | free(list->items[i].util); |
| 281 | list->items[i] = list->items[list->nr-1]; |
| 282 | list->nr--; |
| 283 | } |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 284 | |
| 285 | int string_list_split(struct string_list *list, const char *string, |
| 286 | int delim, int maxsplit) |
| 287 | { |
| 288 | int count = 0; |
| 289 | const char *p = string, *end; |
| 290 | |
| 291 | if (!list->strdup_strings) |
| 292 | die("internal error in string_list_split(): " |
| 293 | "list->strdup_strings must be set"); |
| 294 | for (;;) { |
| 295 | count++; |
| 296 | if (maxsplit >= 0 && count > maxsplit) { |
| 297 | string_list_append(list, p); |
| 298 | return count; |
| 299 | } |
| 300 | end = strchr(p, delim); |
| 301 | if (end) { |
| 302 | string_list_append_nodup(list, xmemdupz(p, end - p)); |
| 303 | p = end + 1; |
| 304 | } else { |
| 305 | string_list_append(list, p); |
| 306 | return count; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | int string_list_split_in_place(struct string_list *list, char *string, |
Taylor Blau | 52acddf | 2023-04-24 18:20:10 -0400 | [diff] [blame] | 312 | const char *delim, int maxsplit) |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 313 | { |
| 314 | int count = 0; |
| 315 | char *p = string, *end; |
| 316 | |
| 317 | if (list->strdup_strings) |
| 318 | die("internal error in string_list_split_in_place(): " |
| 319 | "list->strdup_strings must not be set"); |
| 320 | for (;;) { |
| 321 | count++; |
| 322 | if (maxsplit >= 0 && count > maxsplit) { |
| 323 | string_list_append(list, p); |
| 324 | return count; |
| 325 | } |
Taylor Blau | 52acddf | 2023-04-24 18:20:10 -0400 | [diff] [blame] | 326 | end = strpbrk(p, delim); |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 327 | if (end) { |
| 328 | *end = '\0'; |
| 329 | string_list_append(list, p); |
| 330 | p = end + 1; |
| 331 | } else { |
| 332 | string_list_append(list, p); |
| 333 | return count; |
| 334 | } |
| 335 | } |
| 336 | } |