Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 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 | |
| 11 | while (left + 1 < right) { |
| 12 | int middle = (left + right) / 2; |
| 13 | int compare = strcmp(string, list->items[middle].string); |
| 14 | if (compare < 0) |
| 15 | right = middle; |
| 16 | else if (compare > 0) |
| 17 | left = middle; |
| 18 | else { |
| 19 | *exact_match = 1; |
| 20 | return middle; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | *exact_match = 0; |
| 25 | return right; |
| 26 | } |
| 27 | |
| 28 | /* returns -1-index if already exists */ |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 29 | 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] | 30 | { |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 31 | int exact_match = 0; |
| 32 | 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] | 33 | |
| 34 | if (exact_match) |
| 35 | return -1 - index; |
| 36 | |
| 37 | if (list->nr + 1 >= list->alloc) { |
| 38 | list->alloc += 32; |
| 39 | list->items = xrealloc(list->items, list->alloc |
| 40 | * sizeof(struct string_list_item)); |
| 41 | } |
| 42 | if (index < list->nr) |
| 43 | memmove(list->items + index + 1, list->items + index, |
| 44 | (list->nr - index) |
| 45 | * sizeof(struct string_list_item)); |
| 46 | list->items[index].string = list->strdup_strings ? |
| 47 | xstrdup(string) : (char *)string; |
| 48 | list->items[index].util = NULL; |
| 49 | list->nr++; |
| 50 | |
| 51 | return index; |
| 52 | } |
| 53 | |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 54 | 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] | 55 | { |
Julian Phillips | aadceea | 2010-06-26 00:41:36 +0100 | [diff] [blame] | 56 | return string_list_insert_at_index(list, -1, string); |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Julian Phillips | aadceea | 2010-06-26 00:41:36 +0100 | [diff] [blame] | 59 | struct string_list_item *string_list_insert_at_index(struct string_list *list, |
| 60 | int insert_at, const char *string) |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 61 | { |
| 62 | int index = add_entry(insert_at, list, string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 63 | |
| 64 | if (index < 0) |
| 65 | index = -1 - index; |
| 66 | |
| 67 | return list->items + index; |
| 68 | } |
| 69 | |
| 70 | int string_list_has_string(const struct string_list *list, const char *string) |
| 71 | { |
| 72 | int exact_match; |
| 73 | get_entry_index(list, string, &exact_match); |
| 74 | return exact_match; |
| 75 | } |
| 76 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 77 | int string_list_find_insert_index(const struct string_list *list, const char *string, |
| 78 | int negative_existing_index) |
| 79 | { |
| 80 | int exact_match; |
| 81 | int index = get_entry_index(list, string, &exact_match); |
| 82 | if (exact_match) |
| 83 | index = -1 - (negative_existing_index ? index : 0); |
| 84 | return index; |
| 85 | } |
| 86 | |
Julian Phillips | e8c8b71 | 2010-06-26 00:41:37 +0100 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | int exact_match, i = get_entry_index(list, string, &exact_match); |
| 90 | if (!exact_match) |
| 91 | return NULL; |
| 92 | return list->items + i; |
| 93 | } |
| 94 | |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 95 | void string_list_remove_duplicates(struct string_list *list, int free_util) |
| 96 | { |
| 97 | if (list->nr > 1) { |
| 98 | int src, dst; |
| 99 | for (src = dst = 1; src < list->nr; src++) { |
| 100 | if (!strcmp(list->items[dst - 1].string, list->items[src].string)) { |
| 101 | if (list->strdup_strings) |
| 102 | free(list->items[src].string); |
| 103 | if (free_util) |
| 104 | free(list->items[src].util); |
| 105 | } else |
| 106 | list->items[dst++] = list->items[src]; |
| 107 | } |
| 108 | list->nr = dst; |
| 109 | } |
| 110 | } |
| 111 | |
Julian Phillips | b684e97 | 2010-06-26 00:41:34 +0100 | [diff] [blame] | 112 | int for_each_string_list(struct string_list *list, |
| 113 | string_list_each_func_t fn, void *cb_data) |
Jay Soffian | c6f5a7a | 2009-02-25 03:32:18 -0500 | [diff] [blame] | 114 | { |
| 115 | int i, ret = 0; |
| 116 | for (i = 0; i < list->nr; i++) |
| 117 | if ((ret = fn(&list->items[i], cb_data))) |
| 118 | break; |
| 119 | return ret; |
| 120 | } |
| 121 | |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 122 | void filter_string_list(struct string_list *list, int free_util, |
| 123 | string_list_each_func_t want, void *cb_data) |
| 124 | { |
| 125 | int src, dst = 0; |
| 126 | for (src = 0; src < list->nr; src++) { |
| 127 | if (want(&list->items[src], cb_data)) { |
| 128 | list->items[dst++] = list->items[src]; |
| 129 | } else { |
| 130 | if (list->strdup_strings) |
| 131 | free(list->items[src].string); |
| 132 | if (free_util) |
| 133 | free(list->items[src].util); |
| 134 | } |
| 135 | } |
| 136 | list->nr = dst; |
| 137 | } |
| 138 | |
Michael Haggerty | 6bb2a13 | 2012-11-04 08:07:06 +0100 | [diff] [blame] | 139 | static int item_is_not_empty(struct string_list_item *item, void *unused) |
| 140 | { |
| 141 | return *item->string != '\0'; |
| 142 | } |
| 143 | |
| 144 | void string_list_remove_empty_items(struct string_list *list, int free_util) { |
| 145 | filter_string_list(list, free_util, item_is_not_empty, NULL); |
| 146 | } |
| 147 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 148 | void string_list_clear(struct string_list *list, int free_util) |
| 149 | { |
| 150 | if (list->items) { |
| 151 | int i; |
| 152 | if (list->strdup_strings) { |
| 153 | for (i = 0; i < list->nr; i++) |
| 154 | free(list->items[i].string); |
| 155 | } |
| 156 | if (free_util) { |
| 157 | for (i = 0; i < list->nr; i++) |
| 158 | free(list->items[i].util); |
| 159 | } |
| 160 | free(list->items); |
| 161 | } |
| 162 | list->items = NULL; |
| 163 | list->nr = list->alloc = 0; |
| 164 | } |
| 165 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 166 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) |
| 167 | { |
| 168 | if (list->items) { |
| 169 | int i; |
| 170 | if (clearfunc) { |
| 171 | for (i = 0; i < list->nr; i++) |
| 172 | clearfunc(list->items[i].util, list->items[i].string); |
| 173 | } |
| 174 | if (list->strdup_strings) { |
| 175 | for (i = 0; i < list->nr; i++) |
| 176 | free(list->items[i].string); |
| 177 | } |
| 178 | free(list->items); |
| 179 | } |
| 180 | list->items = NULL; |
| 181 | list->nr = list->alloc = 0; |
| 182 | } |
| 183 | |
| 184 | |
Julian Phillips | cb944f6 | 2010-06-26 00:41:33 +0100 | [diff] [blame] | 185 | void print_string_list(const struct string_list *p, const char *text) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 186 | { |
| 187 | int i; |
| 188 | if ( text ) |
| 189 | printf("%s\n", text); |
| 190 | for (i = 0; i < p->nr; i++) |
| 191 | printf("%s:%p\n", p->items[i].string, p->items[i].util); |
| 192 | } |
| 193 | |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 194 | struct string_list_item *string_list_append_nodup(struct string_list *list, |
| 195 | char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 196 | { |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 197 | struct string_list_item *retval; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 198 | ALLOC_GROW(list->items, list->nr + 1, list->alloc); |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 199 | retval = &list->items[list->nr++]; |
| 200 | retval->string = string; |
| 201 | retval->util = NULL; |
| 202 | return retval; |
| 203 | } |
| 204 | |
| 205 | struct string_list_item *string_list_append(struct string_list *list, |
| 206 | const char *string) |
| 207 | { |
| 208 | return string_list_append_nodup( |
| 209 | list, |
| 210 | list->strdup_strings ? xstrdup(string) : (char *)string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int cmp_items(const void *a, const void *b) |
| 214 | { |
| 215 | const struct string_list_item *one = a; |
| 216 | const struct string_list_item *two = b; |
| 217 | return strcmp(one->string, two->string); |
| 218 | } |
| 219 | |
| 220 | void sort_string_list(struct string_list *list) |
| 221 | { |
| 222 | qsort(list->items, list->nr, sizeof(*list->items), cmp_items); |
| 223 | } |
| 224 | |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 225 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
| 226 | const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 227 | { |
| 228 | int i; |
| 229 | for (i = 0; i < list->nr; i++) |
| 230 | if (!strcmp(string, list->items[i].string)) |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 231 | return list->items + i; |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | int unsorted_string_list_has_string(struct string_list *list, |
| 236 | const char *string) |
| 237 | { |
| 238 | return unsorted_string_list_lookup(list, string) != NULL; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Johannes Sixt | 86d4b52 | 2011-08-11 23:20:00 -0600 | [diff] [blame] | 241 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util) |
| 242 | { |
| 243 | if (list->strdup_strings) |
| 244 | free(list->items[i].string); |
| 245 | if (free_util) |
| 246 | free(list->items[i].util); |
| 247 | list->items[i] = list->items[list->nr-1]; |
| 248 | list->nr--; |
| 249 | } |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 250 | |
| 251 | int string_list_split(struct string_list *list, const char *string, |
| 252 | int delim, int maxsplit) |
| 253 | { |
| 254 | int count = 0; |
| 255 | const char *p = string, *end; |
| 256 | |
| 257 | if (!list->strdup_strings) |
| 258 | die("internal error in string_list_split(): " |
| 259 | "list->strdup_strings must be set"); |
| 260 | for (;;) { |
| 261 | count++; |
| 262 | if (maxsplit >= 0 && count > maxsplit) { |
| 263 | string_list_append(list, p); |
| 264 | return count; |
| 265 | } |
| 266 | end = strchr(p, delim); |
| 267 | if (end) { |
| 268 | string_list_append_nodup(list, xmemdupz(p, end - p)); |
| 269 | p = end + 1; |
| 270 | } else { |
| 271 | string_list_append(list, p); |
| 272 | return count; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | int string_list_split_in_place(struct string_list *list, char *string, |
| 278 | int delim, int maxsplit) |
| 279 | { |
| 280 | int count = 0; |
| 281 | char *p = string, *end; |
| 282 | |
| 283 | if (list->strdup_strings) |
| 284 | die("internal error in string_list_split_in_place(): " |
| 285 | "list->strdup_strings must not be set"); |
| 286 | for (;;) { |
| 287 | count++; |
| 288 | if (maxsplit >= 0 && count > maxsplit) { |
| 289 | string_list_append(list, p); |
| 290 | return count; |
| 291 | } |
| 292 | end = strchr(p, delim); |
| 293 | if (end) { |
| 294 | *end = '\0'; |
| 295 | string_list_append(list, p); |
| 296 | p = end + 1; |
| 297 | } else { |
| 298 | string_list_append(list, p); |
| 299 | return count; |
| 300 | } |
| 301 | } |
| 302 | } |