string-list.con commit format-patch: give --reroll-count a short synonym -v (7952ea6)
   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 */
   6static 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 */
  29static int add_entry(int insert_at, struct string_list *list, const char *string)
  30{
  31        int exact_match = 0;
  32        int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
  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
  54struct string_list_item *string_list_insert(struct string_list *list, const char *string)
  55{
  56        return string_list_insert_at_index(list, -1, string);
  57}
  58
  59struct string_list_item *string_list_insert_at_index(struct string_list *list,
  60                                                     int insert_at, const char *string)
  61{
  62        int index = add_entry(insert_at, list, string);
  63
  64        if (index < 0)
  65                index = -1 - index;
  66
  67        return list->items + index;
  68}
  69
  70int 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
  77int 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
  87struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
  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
  95void 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
 112int for_each_string_list(struct string_list *list,
 113                         string_list_each_func_t fn, void *cb_data)
 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
 122void 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
 139char *string_list_longest_prefix(const struct string_list *prefixes,
 140                                 const char *string)
 141{
 142        int i, max_len = -1;
 143        char *retval = NULL;
 144
 145        for (i = 0; i < prefixes->nr; i++) {
 146                char *prefix = prefixes->items[i].string;
 147                if (!prefixcmp(string, prefix)) {
 148                        int len = strlen(prefix);
 149                        if (len > max_len) {
 150                                retval = prefix;
 151                                max_len = len;
 152                        }
 153                }
 154        }
 155
 156        return retval;
 157}
 158
 159void string_list_clear(struct string_list *list, int free_util)
 160{
 161        if (list->items) {
 162                int i;
 163                if (list->strdup_strings) {
 164                        for (i = 0; i < list->nr; i++)
 165                                free(list->items[i].string);
 166                }
 167                if (free_util) {
 168                        for (i = 0; i < list->nr; i++)
 169                                free(list->items[i].util);
 170                }
 171                free(list->items);
 172        }
 173        list->items = NULL;
 174        list->nr = list->alloc = 0;
 175}
 176
 177void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
 178{
 179        if (list->items) {
 180                int i;
 181                if (clearfunc) {
 182                        for (i = 0; i < list->nr; i++)
 183                                clearfunc(list->items[i].util, list->items[i].string);
 184                }
 185                if (list->strdup_strings) {
 186                        for (i = 0; i < list->nr; i++)
 187                                free(list->items[i].string);
 188                }
 189                free(list->items);
 190        }
 191        list->items = NULL;
 192        list->nr = list->alloc = 0;
 193}
 194
 195
 196void print_string_list(const struct string_list *p, const char *text)
 197{
 198        int i;
 199        if ( text )
 200                printf("%s\n", text);
 201        for (i = 0; i < p->nr; i++)
 202                printf("%s:%p\n", p->items[i].string, p->items[i].util);
 203}
 204
 205struct string_list_item *string_list_append_nodup(struct string_list *list,
 206                                                  char *string)
 207{
 208        struct string_list_item *retval;
 209        ALLOC_GROW(list->items, list->nr + 1, list->alloc);
 210        retval = &list->items[list->nr++];
 211        retval->string = string;
 212        retval->util = NULL;
 213        return retval;
 214}
 215
 216struct string_list_item *string_list_append(struct string_list *list,
 217                                            const char *string)
 218{
 219        return string_list_append_nodup(
 220                        list,
 221                        list->strdup_strings ? xstrdup(string) : (char *)string);
 222}
 223
 224static 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 strcmp(one->string, two->string);
 229}
 230
 231void sort_string_list(struct string_list *list)
 232{
 233        qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
 234}
 235
 236struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 237                                                     const char *string)
 238{
 239        int i;
 240        for (i = 0; i < list->nr; i++)
 241                if (!strcmp(string, list->items[i].string))
 242                        return list->items + i;
 243        return NULL;
 244}
 245
 246int unsorted_string_list_has_string(struct string_list *list,
 247                                    const char *string)
 248{
 249        return unsorted_string_list_lookup(list, string) != NULL;
 250}
 251
 252void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
 253{
 254        if (list->strdup_strings)
 255                free(list->items[i].string);
 256        if (free_util)
 257                free(list->items[i].util);
 258        list->items[i] = list->items[list->nr-1];
 259        list->nr--;
 260}
 261
 262int string_list_split(struct string_list *list, const char *string,
 263                      int delim, int maxsplit)
 264{
 265        int count = 0;
 266        const char *p = string, *end;
 267
 268        if (!list->strdup_strings)
 269                die("internal error in string_list_split(): "
 270                    "list->strdup_strings must be set");
 271        for (;;) {
 272                count++;
 273                if (maxsplit >= 0 && count > maxsplit) {
 274                        string_list_append(list, p);
 275                        return count;
 276                }
 277                end = strchr(p, delim);
 278                if (end) {
 279                        string_list_append_nodup(list, xmemdupz(p, end - p));
 280                        p = end + 1;
 281                } else {
 282                        string_list_append(list, p);
 283                        return count;
 284                }
 285        }
 286}
 287
 288int string_list_split_in_place(struct string_list *list, char *string,
 289                               int delim, int maxsplit)
 290{
 291        int count = 0;
 292        char *p = string, *end;
 293
 294        if (list->strdup_strings)
 295                die("internal error in string_list_split_in_place(): "
 296                    "list->strdup_strings must not be set");
 297        for (;;) {
 298                count++;
 299                if (maxsplit >= 0 && count > maxsplit) {
 300                        string_list_append(list, p);
 301                        return count;
 302                }
 303                end = strchr(p, delim);
 304                if (end) {
 305                        *end = '\0';
 306                        string_list_append(list, p);
 307                        p = end + 1;
 308                } else {
 309                        string_list_append(list, p);
 310                        return count;
 311                }
 312        }
 313}