string-list.con commit Merge branch 'rf/completion' (9743f18)
   1#include "cache.h"
   2#include "string-list.h"
   3
   4void string_list_init(struct string_list *list, int strdup_strings)
   5{
   6        memset(list, 0, sizeof(*list));
   7        list->strdup_strings = strdup_strings;
   8}
   9
  10/* if there is no exact match, point to the index where the entry could be
  11 * inserted */
  12static int get_entry_index(const struct string_list *list, const char *string,
  13                int *exact_match)
  14{
  15        int left = -1, right = list->nr;
  16        compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
  17
  18        while (left + 1 < right) {
  19                int middle = (left + right) / 2;
  20                int compare = cmp(string, list->items[middle].string);
  21                if (compare < 0)
  22                        right = middle;
  23                else if (compare > 0)
  24                        left = middle;
  25                else {
  26                        *exact_match = 1;
  27                        return middle;
  28                }
  29        }
  30
  31        *exact_match = 0;
  32        return right;
  33}
  34
  35/* returns -1-index if already exists */
  36static int add_entry(int insert_at, struct string_list *list, const char *string)
  37{
  38        int exact_match = 0;
  39        int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
  40
  41        if (exact_match)
  42                return -1 - index;
  43
  44        ALLOC_GROW(list->items, list->nr+1, list->alloc);
  45        if (index < list->nr)
  46                memmove(list->items + index + 1, list->items + index,
  47                                (list->nr - index)
  48                                * sizeof(struct string_list_item));
  49        list->items[index].string = list->strdup_strings ?
  50                xstrdup(string) : (char *)string;
  51        list->items[index].util = NULL;
  52        list->nr++;
  53
  54        return index;
  55}
  56
  57struct string_list_item *string_list_insert(struct string_list *list, const char *string)
  58{
  59        int index = add_entry(-1, list, string);
  60
  61        if (index < 0)
  62                index = -1 - index;
  63
  64        return list->items + index;
  65}
  66
  67void string_list_remove(struct string_list *list, const char *string,
  68                        int free_util)
  69{
  70        int exact_match;
  71        int i = get_entry_index(list, string, &exact_match);
  72
  73        if (exact_match) {
  74                if (list->strdup_strings)
  75                        free(list->items[i].string);
  76                if (free_util)
  77                        free(list->items[i].util);
  78
  79                list->nr--;
  80                memmove(list->items + i, list->items + i + 1,
  81                        (list->nr - i) * sizeof(struct string_list_item));
  82        }
  83}
  84
  85int string_list_has_string(const struct string_list *list, const char *string)
  86{
  87        int exact_match;
  88        get_entry_index(list, string, &exact_match);
  89        return exact_match;
  90}
  91
  92int string_list_find_insert_index(const struct string_list *list, const char *string,
  93                                  int negative_existing_index)
  94{
  95        int exact_match;
  96        int index = get_entry_index(list, string, &exact_match);
  97        if (exact_match)
  98                index = -1 - (negative_existing_index ? index : 0);
  99        return index;
 100}
 101
 102struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
 103{
 104        int exact_match, i = get_entry_index(list, string, &exact_match);
 105        if (!exact_match)
 106                return NULL;
 107        return list->items + i;
 108}
 109
 110void string_list_remove_duplicates(struct string_list *list, int free_util)
 111{
 112        if (list->nr > 1) {
 113                int src, dst;
 114                compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 115                for (src = dst = 1; src < list->nr; src++) {
 116                        if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
 117                                if (list->strdup_strings)
 118                                        free(list->items[src].string);
 119                                if (free_util)
 120                                        free(list->items[src].util);
 121                        } else
 122                                list->items[dst++] = list->items[src];
 123                }
 124                list->nr = dst;
 125        }
 126}
 127
 128int for_each_string_list(struct string_list *list,
 129                         string_list_each_func_t fn, void *cb_data)
 130{
 131        int i, ret = 0;
 132        for (i = 0; i < list->nr; i++)
 133                if ((ret = fn(&list->items[i], cb_data)))
 134                        break;
 135        return ret;
 136}
 137
 138void filter_string_list(struct string_list *list, int free_util,
 139                        string_list_each_func_t want, void *cb_data)
 140{
 141        int src, dst = 0;
 142        for (src = 0; src < list->nr; src++) {
 143                if (want(&list->items[src], cb_data)) {
 144                        list->items[dst++] = list->items[src];
 145                } else {
 146                        if (list->strdup_strings)
 147                                free(list->items[src].string);
 148                        if (free_util)
 149                                free(list->items[src].util);
 150                }
 151        }
 152        list->nr = dst;
 153}
 154
 155static int item_is_not_empty(struct string_list_item *item, void *unused)
 156{
 157        return *item->string != '\0';
 158}
 159
 160void string_list_remove_empty_items(struct string_list *list, int free_util) {
 161        filter_string_list(list, free_util, item_is_not_empty, NULL);
 162}
 163
 164void string_list_clear(struct string_list *list, int free_util)
 165{
 166        if (list->items) {
 167                int i;
 168                if (list->strdup_strings) {
 169                        for (i = 0; i < list->nr; i++)
 170                                free(list->items[i].string);
 171                }
 172                if (free_util) {
 173                        for (i = 0; i < list->nr; i++)
 174                                free(list->items[i].util);
 175                }
 176                free(list->items);
 177        }
 178        list->items = NULL;
 179        list->nr = list->alloc = 0;
 180}
 181
 182void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
 183{
 184        if (list->items) {
 185                int i;
 186                if (clearfunc) {
 187                        for (i = 0; i < list->nr; i++)
 188                                clearfunc(list->items[i].util, list->items[i].string);
 189                }
 190                if (list->strdup_strings) {
 191                        for (i = 0; i < list->nr; i++)
 192                                free(list->items[i].string);
 193                }
 194                free(list->items);
 195        }
 196        list->items = NULL;
 197        list->nr = list->alloc = 0;
 198}
 199
 200
 201void print_string_list(const struct string_list *p, const char *text)
 202{
 203        int i;
 204        if ( text )
 205                printf("%s\n", text);
 206        for (i = 0; i < p->nr; i++)
 207                printf("%s:%p\n", p->items[i].string, p->items[i].util);
 208}
 209
 210struct string_list_item *string_list_append_nodup(struct string_list *list,
 211                                                  char *string)
 212{
 213        struct string_list_item *retval;
 214        ALLOC_GROW(list->items, list->nr + 1, list->alloc);
 215        retval = &list->items[list->nr++];
 216        retval->string = string;
 217        retval->util = NULL;
 218        return retval;
 219}
 220
 221struct string_list_item *string_list_append(struct string_list *list,
 222                                            const char *string)
 223{
 224        return string_list_append_nodup(
 225                        list,
 226                        list->strdup_strings ? xstrdup(string) : (char *)string);
 227}
 228
 229static int cmp_items(const void *a, const void *b, void *ctx)
 230{
 231        compare_strings_fn cmp = ctx;
 232        const struct string_list_item *one = a;
 233        const struct string_list_item *two = b;
 234        return cmp(one->string, two->string);
 235}
 236
 237void string_list_sort(struct string_list *list)
 238{
 239        QSORT_S(list->items, list->nr, cmp_items,
 240                list->cmp ? list->cmp : strcmp);
 241}
 242
 243struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 244                                                     const char *string)
 245{
 246        struct string_list_item *item;
 247        compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 248
 249        for_each_string_list_item(item, list)
 250                if (!cmp(string, item->string))
 251                        return item;
 252        return NULL;
 253}
 254
 255int unsorted_string_list_has_string(struct string_list *list,
 256                                    const char *string)
 257{
 258        return unsorted_string_list_lookup(list, string) != NULL;
 259}
 260
 261void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
 262{
 263        if (list->strdup_strings)
 264                free(list->items[i].string);
 265        if (free_util)
 266                free(list->items[i].util);
 267        list->items[i] = list->items[list->nr-1];
 268        list->nr--;
 269}
 270
 271int string_list_split(struct string_list *list, const char *string,
 272                      int delim, int maxsplit)
 273{
 274        int count = 0;
 275        const char *p = string, *end;
 276
 277        if (!list->strdup_strings)
 278                die("internal error in string_list_split(): "
 279                    "list->strdup_strings must be set");
 280        for (;;) {
 281                count++;
 282                if (maxsplit >= 0 && count > maxsplit) {
 283                        string_list_append(list, p);
 284                        return count;
 285                }
 286                end = strchr(p, delim);
 287                if (end) {
 288                        string_list_append_nodup(list, xmemdupz(p, end - p));
 289                        p = end + 1;
 290                } else {
 291                        string_list_append(list, p);
 292                        return count;
 293                }
 294        }
 295}
 296
 297int string_list_split_in_place(struct string_list *list, char *string,
 298                               int delim, int maxsplit)
 299{
 300        int count = 0;
 301        char *p = string, *end;
 302
 303        if (list->strdup_strings)
 304                die("internal error in string_list_split_in_place(): "
 305                    "list->strdup_strings must not be set");
 306        for (;;) {
 307                count++;
 308                if (maxsplit >= 0 && count > maxsplit) {
 309                        string_list_append(list, p);
 310                        return count;
 311                }
 312                end = strchr(p, delim);
 313                if (end) {
 314                        *end = '\0';
 315                        string_list_append(list, p);
 316                        p = end + 1;
 317                } else {
 318                        string_list_append(list, p);
 319                        return count;
 320                }
 321        }
 322}