merge-base: die with an error message if not passed a commit ref
[git/git-bigfiles.git] / string-list.c
blobddd83c8c76112cecd5d23668aaca467601855a72
1 #include "cache.h"
2 #include "string-list.h"
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)
9 int left = -1, right = list->nr;
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;
24 *exact_match = 0;
25 return right;
28 /* returns -1-index if already exists */
29 static int add_entry(struct string_list *list, const char *string)
31 int exact_match;
32 int index = get_entry_index(list, string, &exact_match);
34 if (exact_match)
35 return -1 - index;
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));
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++;
51 return index;
54 struct string_list_item *string_list_insert(const char *string, struct string_list *list)
56 int index = add_entry(list, string);
58 if (index < 0)
59 index = -1 - index;
61 return list->items + index;
64 int string_list_has_string(const struct string_list *list, const char *string)
66 int exact_match;
67 get_entry_index(list, string, &exact_match);
68 return exact_match;
71 struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
73 int exact_match, i = get_entry_index(list, string, &exact_match);
74 if (!exact_match)
75 return NULL;
76 return list->items + i;
79 void string_list_clear(struct string_list *list, int free_util)
81 if (list->items) {
82 int i;
83 if (list->strdup_strings) {
84 for (i = 0; i < list->nr; i++)
85 free(list->items[i].string);
87 if (free_util) {
88 for (i = 0; i < list->nr; i++)
89 free(list->items[i].util);
91 free(list->items);
93 list->items = NULL;
94 list->nr = list->alloc = 0;
97 void print_string_list(const char *text, const struct string_list *p)
99 int i;
100 if ( text )
101 printf("%s\n", text);
102 for (i = 0; i < p->nr; i++)
103 printf("%s:%p\n", p->items[i].string, p->items[i].util);
106 struct string_list_item *string_list_append(const char *string, struct string_list *list)
108 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
109 list->items[list->nr].string =
110 list->strdup_strings ? xstrdup(string) : (char *)string;
111 return list->items + list->nr++;
114 static int cmp_items(const void *a, const void *b)
116 const struct string_list_item *one = a;
117 const struct string_list_item *two = b;
118 return strcmp(one->string, two->string);
121 void sort_string_list(struct string_list *list)
123 qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
126 int unsorted_string_list_has_string(struct string_list *list, const char *string)
128 int i;
129 for (i = 0; i < list->nr; i++)
130 if (!strcmp(string, list->items[i].string))
131 return 1;
132 return 0;