decfa747fcc8b9c400433dea3e202ff154b3a034
   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
 139void string_list_clear(struct string_list *list, int free_util)
 140{
 141        if (list->items) {
 142                int i;
 143                if (list->strdup_strings) {
 144                        for (i = 0; i < list->nr; i++)
 145                                free(list->items[i].string);
 146                }
 147                if (free_util) {
 148                        for (i = 0; i < list->nr; i++)
 149                                free(list->items[i].util);
 150                }
 151                free(list->items);
 152        }
 153        list->items = NULL;
 154        list->nr = list->alloc = 0;
 155}
 156
 157void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
 158{
 159        if (list->items) {
 160                int i;
 161                if (clearfunc) {
 162                        for (i = 0; i < list->nr; i++)
 163                                clearfunc(list->items[i].util, list->items[i].string);
 164                }
 165                if (list->strdup_strings) {
 166                        for (i = 0; i < list->nr; i++)
 167                                free(list->items[i].string);
 168                }
 169                free(list->items);
 170        }
 171        list->items = NULL;
 172        list->nr = list->alloc = 0;
 173}
 174
 175
 176void print_string_list(const struct string_list *p, const char *text)
 177{
 178        int i;
 179        if ( text )
 180                printf("%s\n", text);
 181        for (i = 0; i < p->nr; i++)
 182                printf("%s:%p\n", p->items[i].string, p->items[i].util);
 183}
 184
 185struct string_list_item *string_list_append_nodup(struct string_list *list,
 186                                                  char *string)
 187{
 188        struct string_list_item *retval;
 189        ALLOC_GROW(list->items, list->nr + 1, list->alloc);
 190        retval = &list->items[list->nr++];
 191        retval->string = string;
 192        retval->util = NULL;
 193        return retval;
 194}
 195
 196struct string_list_item *string_list_append(struct string_list *list,
 197                                            const char *string)
 198{
 199        return string_list_append_nodup(
 200                        list,
 201                        list->strdup_strings ? xstrdup(string) : (char *)string);
 202}
 203
 204static int cmp_items(const void *a, const void *b)
 205{
 206        const struct string_list_item *one = a;
 207        const struct string_list_item *two = b;
 208        return strcmp(one->string, two->string);
 209}
 210
 211void sort_string_list(struct string_list *list)
 212{
 213        qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
 214}
 215
 216struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 217                                                     const char *string)
 218{
 219        int i;
 220        for (i = 0; i < list->nr; i++)
 221                if (!strcmp(string, list->items[i].string))
 222                        return list->items + i;
 223        return NULL;
 224}
 225
 226int unsorted_string_list_has_string(struct string_list *list,
 227                                    const char *string)
 228{
 229        return unsorted_string_list_lookup(list, string) != NULL;
 230}
 231
 232void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
 233{
 234        if (list->strdup_strings)
 235                free(list->items[i].string);
 236        if (free_util)
 237                free(list->items[i].util);
 238        list->items[i] = list->items[list->nr-1];
 239        list->nr--;
 240}
 241
 242int string_list_split(struct string_list *list, const char *string,
 243                      int delim, int maxsplit)
 244{
 245        int count = 0;
 246        const char *p = string, *end;
 247
 248        if (!list->strdup_strings)
 249                die("internal error in string_list_split(): "
 250                    "list->strdup_strings must be set");
 251        for (;;) {
 252                count++;
 253                if (maxsplit >= 0 && count > maxsplit) {
 254                        string_list_append(list, p);
 255                        return count;
 256                }
 257                end = strchr(p, delim);
 258                if (end) {
 259                        string_list_append_nodup(list, xmemdupz(p, end - p));
 260                        p = end + 1;
 261                } else {
 262                        string_list_append(list, p);
 263                        return count;
 264                }
 265        }
 266}
 267
 268int string_list_split_in_place(struct string_list *list, char *string,
 269                               int delim, int maxsplit)
 270{
 271        int count = 0;
 272        char *p = string, *end;
 273
 274        if (list->strdup_strings)
 275                die("internal error in string_list_split_in_place(): "
 276                    "list->strdup_strings must not be set");
 277        for (;;) {
 278                count++;
 279                if (maxsplit >= 0 && count > maxsplit) {
 280                        string_list_append(list, p);
 281                        return count;
 282                }
 283                end = strchr(p, delim);
 284                if (end) {
 285                        *end = '\0';
 286                        string_list_append(list, p);
 287                        p = end + 1;
 288                } else {
 289                        string_list_append(list, p);
 290                        return count;
 291                }
 292        }
 293}