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 | |
Julian Phillips | b684e97 | 2010-06-26 00:41:34 +0100 | [diff] [blame] | 95 | int for_each_string_list(struct string_list *list, |
| 96 | string_list_each_func_t fn, void *cb_data) |
Jay Soffian | c6f5a7a | 2009-02-25 03:32:18 -0500 | [diff] [blame] | 97 | { |
| 98 | int i, ret = 0; |
| 99 | for (i = 0; i < list->nr; i++) |
| 100 | if ((ret = fn(&list->items[i], cb_data))) |
| 101 | break; |
| 102 | return ret; |
| 103 | } |
| 104 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 105 | void string_list_clear(struct string_list *list, int free_util) |
| 106 | { |
| 107 | if (list->items) { |
| 108 | int i; |
| 109 | if (list->strdup_strings) { |
| 110 | for (i = 0; i < list->nr; i++) |
| 111 | free(list->items[i].string); |
| 112 | } |
| 113 | if (free_util) { |
| 114 | for (i = 0; i < list->nr; i++) |
| 115 | free(list->items[i].util); |
| 116 | } |
| 117 | free(list->items); |
| 118 | } |
| 119 | list->items = NULL; |
| 120 | list->nr = list->alloc = 0; |
| 121 | } |
| 122 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 123 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) |
| 124 | { |
| 125 | if (list->items) { |
| 126 | int i; |
| 127 | if (clearfunc) { |
| 128 | for (i = 0; i < list->nr; i++) |
| 129 | clearfunc(list->items[i].util, list->items[i].string); |
| 130 | } |
| 131 | if (list->strdup_strings) { |
| 132 | for (i = 0; i < list->nr; i++) |
| 133 | free(list->items[i].string); |
| 134 | } |
| 135 | free(list->items); |
| 136 | } |
| 137 | list->items = NULL; |
| 138 | list->nr = list->alloc = 0; |
| 139 | } |
| 140 | |
| 141 | |
Julian Phillips | cb944f6 | 2010-06-26 00:41:33 +0100 | [diff] [blame] | 142 | void print_string_list(const struct string_list *p, const char *text) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 143 | { |
| 144 | int i; |
| 145 | if ( text ) |
| 146 | printf("%s\n", text); |
| 147 | for (i = 0; i < p->nr; i++) |
| 148 | printf("%s:%p\n", p->items[i].string, p->items[i].util); |
| 149 | } |
| 150 | |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 151 | struct string_list_item *string_list_append(struct string_list *list, const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 152 | { |
| 153 | ALLOC_GROW(list->items, list->nr + 1, list->alloc); |
| 154 | list->items[list->nr].string = |
| 155 | list->strdup_strings ? xstrdup(string) : (char *)string; |
Jeff King | 62b8102 | 2011-02-12 00:18:51 -0500 | [diff] [blame] | 156 | list->items[list->nr].util = NULL; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 157 | return list->items + list->nr++; |
| 158 | } |
| 159 | |
| 160 | static int cmp_items(const void *a, const void *b) |
| 161 | { |
| 162 | const struct string_list_item *one = a; |
| 163 | const struct string_list_item *two = b; |
| 164 | return strcmp(one->string, two->string); |
| 165 | } |
| 166 | |
| 167 | void sort_string_list(struct string_list *list) |
| 168 | { |
| 169 | qsort(list->items, list->nr, sizeof(*list->items), cmp_items); |
| 170 | } |
| 171 | |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 172 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
| 173 | const char *string) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 174 | { |
| 175 | int i; |
| 176 | for (i = 0; i < list->nr; i++) |
| 177 | if (!strcmp(string, list->items[i].string)) |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 178 | return list->items + i; |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | int unsorted_string_list_has_string(struct string_list *list, |
| 183 | const char *string) |
| 184 | { |
| 185 | return unsorted_string_list_lookup(list, string) != NULL; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Johannes Sixt | 86d4b52 | 2011-08-11 23:20:00 -0600 | [diff] [blame] | 188 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util) |
| 189 | { |
| 190 | if (list->strdup_strings) |
| 191 | free(list->items[i].string); |
| 192 | if (free_util) |
| 193 | free(list->items[i].util); |
| 194 | list->items[i] = list->items[list->nr-1]; |
| 195 | list->nr--; |
| 196 | } |