string-list.con commit Merge branch 'sg/t6050-failing-editor-test-fix' (1576f78)
   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        if (list->nr + 1 >= list->alloc) {
  45                list->alloc += 32;
  46                REALLOC_ARRAY(list->items, list->alloc);
  47        }
  48        if (index < list->nr)
  49                memmove(list->items + index + 1, list->items + index,
  50                                (list->nr - index)
  51                                * sizeof(struct string_list_item));
  52        list->items[index].string = list->strdup_strings ?
  53                xstrdup(string) : (char *)string;
  54        list->items[index].util = NULL;
  55        list->nr++;
  56
  57        return index;
  58}
  59
  60struct string_list_item *string_list_insert(struct string_list *list, const char *string)
  61{
  62        int index = add_entry(-1, 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                compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 100                for (src = dst = 1; src < list->nr; src++) {
 101                        if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
 102                                if (list->strdup_strings)
 103                                        free(list->items[src].string);
 104                                if (free_util)
 105                                        free(list->items[src].util);
 106                        } else
 107                                list->items[dst++] = list->items[src];
 108                }
 109                list->nr = dst;
 110        }
 111}
 112
 113int for_each_string_list(struct string_list *list,
 114                         string_list_each_func_t fn, void *cb_data)
 115{
 116        int i, ret = 0;
 117        for (i = 0; i < list->nr; i++)
 118                if ((ret = fn(&list->items[i], cb_data)))
 119                        break;
 120        return ret;
 121}
 122
 123void filter_string_list(struct string_list *list, int free_util,
 124                        string_list_each_func_t want, void *cb_data)
 125{
 126        int src, dst = 0;
 127        for (src = 0; src < list->nr; src++) {
 128                if (want(&list->items[src], cb_data)) {
 129                        list->items[dst++] = list->items[src];
 130                } else {
 131                        if (list->strdup_strings)
 132                                free(list->items[src].string);
 133                        if (free_util)
 134                                free(list->items[src].util);
 135                }
 136        }
 137        list->nr = dst;
 138}
 139
 140static int item_is_not_empty(struct string_list_item *item, void *unused)
 141{
 142        return *item->string != '\0';
 143}
 144
 145void string_list_remove_empty_items(struct string_list *list, int free_util) {
 146        filter_string_list(list, free_util, item_is_not_empty, NULL);
 147}
 148
 149void string_list_clear(struct string_list *list, int free_util)
 150{
 151        if (list->items) {
 152                int i;
 153                if (list->strdup_strings) {
 154                        for (i = 0; i < list->nr; i++)
 155                                free(list->items[i].string);
 156                }
 157                if (free_util) {
 158                        for (i = 0; i < list->nr; i++)
 159                                free(list->items[i].util);
 160                }
 161                free(list->items);
 162        }
 163        list->items = NULL;
 164        list->nr = list->alloc = 0;
 165}
 166
 167void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
 168{
 169        if (list->items) {
 170                int i;
 171                if (clearfunc) {
 172                        for (i = 0; i < list->nr; i++)
 173                                clearfunc(list->items[i].util, list->items[i].string);
 174                }
 175                if (list->strdup_strings) {
 176                        for (i = 0; i < list->nr; i++)
 177                                free(list->items[i].string);
 178                }
 179                free(list->items);
 180        }
 181        list->items = NULL;
 182        list->nr = list->alloc = 0;
 183}
 184
 185
 186void print_string_list(const struct string_list *p, const char *text)
 187{
 188        int i;
 189        if ( text )
 190                printf("%s\n", text);
 191        for (i = 0; i < p->nr; i++)
 192                printf("%s:%p\n", p->items[i].string, p->items[i].util);
 193}
 194
 195struct string_list_item *string_list_append_nodup(struct string_list *list,
 196                                                  char *string)
 197{
 198        struct string_list_item *retval;
 199        ALLOC_GROW(list->items, list->nr + 1, list->alloc);
 200        retval = &list->items[list->nr++];
 201        retval->string = string;
 202        retval->util = NULL;
 203        return retval;
 204}
 205
 206struct string_list_item *string_list_append(struct string_list *list,
 207                                            const char *string)
 208{
 209        return string_list_append_nodup(
 210                        list,
 211                        list->strdup_strings ? xstrdup(string) : (char *)string);
 212}
 213
 214/* Yuck */
 215static compare_strings_fn compare_for_qsort;
 216
 217/* Only call this from inside string_list_sort! */
 218static int cmp_items(const void *a, const void *b)
 219{
 220        const struct string_list_item *one = a;
 221        const struct string_list_item *two = b;
 222        return compare_for_qsort(one->string, two->string);
 223}
 224
 225void string_list_sort(struct string_list *list)
 226{
 227        compare_for_qsort = list->cmp ? list->cmp : strcmp;
 228        qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
 229}
 230
 231struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 232                                                     const char *string)
 233{
 234        int i;
 235        compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 236
 237        for (i = 0; i < list->nr; i++)
 238                if (!cmp(string, list->items[i].string))
 239                        return list->items + i;
 240        return NULL;
 241}
 242
 243int unsorted_string_list_has_string(struct string_list *list,
 244                                    const char *string)
 245{
 246        return unsorted_string_list_lookup(list, string) != NULL;
 247}
 248
 249void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
 250{
 251        if (list->strdup_strings)
 252                free(list->items[i].string);
 253        if (free_util)
 254                free(list->items[i].util);
 255        list->items[i] = list->items[list->nr-1];
 256        list->nr--;
 257}
 258
 259int string_list_split(struct string_list *list, const char *string,
 260                      int delim, int maxsplit)
 261{
 262        int count = 0;
 263        const char *p = string, *end;
 264
 265        if (!list->strdup_strings)
 266                die("internal error in string_list_split(): "
 267                    "list->strdup_strings must be set");
 268        for (;;) {
 269                count++;
 270                if (maxsplit >= 0 && count > maxsplit) {
 271                        string_list_append(list, p);
 272                        return count;
 273                }
 274                end = strchr(p, delim);
 275                if (end) {
 276                        string_list_append_nodup(list, xmemdupz(p, end - p));
 277                        p = end + 1;
 278                } else {
 279                        string_list_append(list, p);
 280                        return count;
 281                }
 282        }
 283}
 284
 285int string_list_split_in_place(struct string_list *list, char *string,
 286                               int delim, int maxsplit)
 287{
 288        int count = 0;
 289        char *p = string, *end;
 290
 291        if (list->strdup_strings)
 292                die("internal error in string_list_split_in_place(): "
 293                    "list->strdup_strings must not be set");
 294        for (;;) {
 295                count++;
 296                if (maxsplit >= 0 && count > maxsplit) {
 297                        string_list_append(list, p);
 298                        return count;
 299                }
 300                end = strchr(p, delim);
 301                if (end) {
 302                        *end = '\0';
 303                        string_list_append(list, p);
 304                        p = end + 1;
 305                } else {
 306                        string_list_append(list, p);
 307                        return count;
 308                }
 309        }
 310}