]>
Commit | Line | Data |
---|---|---|
c455c87c JS |
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 */ | |
6 | static 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 */ | |
cfa1ee6b | 29 | static int add_entry(int insert_at, struct string_list *list, const char *string) |
c455c87c | 30 | { |
cfa1ee6b MSO |
31 | int exact_match = 0; |
32 | int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match); | |
c455c87c JS |
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 | ||
78a395d3 | 54 | struct string_list_item *string_list_insert(struct string_list *list, const char *string) |
c455c87c | 55 | { |
aadceea6 | 56 | return string_list_insert_at_index(list, -1, string); |
cfa1ee6b MSO |
57 | } |
58 | ||
aadceea6 JP |
59 | struct string_list_item *string_list_insert_at_index(struct string_list *list, |
60 | int insert_at, const char *string) | |
cfa1ee6b MSO |
61 | { |
62 | int index = add_entry(insert_at, list, string); | |
c455c87c JS |
63 | |
64 | if (index < 0) | |
65 | index = -1 - index; | |
66 | ||
67 | return list->items + index; | |
68 | } | |
69 | ||
70 | int 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 | ||
cfa1ee6b MSO |
77 | int 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 | ||
e8c8b713 | 87 | struct string_list_item *string_list_lookup(struct string_list *list, const char *string) |
c455c87c JS |
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 | ||
b684e977 JP |
95 | int for_each_string_list(struct string_list *list, |
96 | string_list_each_func_t fn, void *cb_data) | |
c6f5a7a9 JS |
97 | { |
98 | int i, ret = 0; | |
99 | for (i = 0; i < list->nr; i++) | |
100 | if ((ret = fn(&list->items[i], cb_data))) | |
101 | break; | |
102 | return ret; | |
103 | } | |
104 | ||
eb5f0c7a MH |
105 | void filter_string_list(struct string_list *list, int free_util, |
106 | string_list_each_func_t want, void *cb_data) | |
107 | { | |
108 | int src, dst = 0; | |
109 | for (src = 0; src < list->nr; src++) { | |
110 | if (want(&list->items[src], cb_data)) { | |
111 | list->items[dst++] = list->items[src]; | |
112 | } else { | |
113 | if (list->strdup_strings) | |
114 | free(list->items[src].string); | |
115 | if (free_util) | |
116 | free(list->items[src].util); | |
117 | } | |
118 | } | |
119 | list->nr = dst; | |
120 | } | |
121 | ||
c455c87c JS |
122 | void string_list_clear(struct string_list *list, int free_util) |
123 | { | |
124 | if (list->items) { | |
125 | int i; | |
126 | if (list->strdup_strings) { | |
127 | for (i = 0; i < list->nr; i++) | |
128 | free(list->items[i].string); | |
129 | } | |
130 | if (free_util) { | |
131 | for (i = 0; i < list->nr; i++) | |
132 | free(list->items[i].util); | |
133 | } | |
134 | free(list->items); | |
135 | } | |
136 | list->items = NULL; | |
137 | list->nr = list->alloc = 0; | |
138 | } | |
139 | ||
cfa1ee6b MSO |
140 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) |
141 | { | |
142 | if (list->items) { | |
143 | int i; | |
144 | if (clearfunc) { | |
145 | for (i = 0; i < list->nr; i++) | |
146 | clearfunc(list->items[i].util, list->items[i].string); | |
147 | } | |
148 | if (list->strdup_strings) { | |
149 | for (i = 0; i < list->nr; i++) | |
150 | free(list->items[i].string); | |
151 | } | |
152 | free(list->items); | |
153 | } | |
154 | list->items = NULL; | |
155 | list->nr = list->alloc = 0; | |
156 | } | |
157 | ||
158 | ||
cb944f6b | 159 | void print_string_list(const struct string_list *p, const char *text) |
c455c87c JS |
160 | { |
161 | int i; | |
162 | if ( text ) | |
163 | printf("%s\n", text); | |
164 | for (i = 0; i < p->nr; i++) | |
165 | printf("%s:%p\n", p->items[i].string, p->items[i].util); | |
166 | } | |
167 | ||
e448fed8 MH |
168 | struct string_list_item *string_list_append_nodup(struct string_list *list, |
169 | char *string) | |
c455c87c | 170 | { |
e448fed8 | 171 | struct string_list_item *retval; |
c455c87c | 172 | ALLOC_GROW(list->items, list->nr + 1, list->alloc); |
e448fed8 MH |
173 | retval = &list->items[list->nr++]; |
174 | retval->string = string; | |
175 | retval->util = NULL; | |
176 | return retval; | |
177 | } | |
178 | ||
179 | struct string_list_item *string_list_append(struct string_list *list, | |
180 | const char *string) | |
181 | { | |
182 | return string_list_append_nodup( | |
183 | list, | |
184 | list->strdup_strings ? xstrdup(string) : (char *)string); | |
c455c87c JS |
185 | } |
186 | ||
187 | static int cmp_items(const void *a, const void *b) | |
188 | { | |
189 | const struct string_list_item *one = a; | |
190 | const struct string_list_item *two = b; | |
191 | return strcmp(one->string, two->string); | |
192 | } | |
193 | ||
194 | void sort_string_list(struct string_list *list) | |
195 | { | |
196 | qsort(list->items, list->nr, sizeof(*list->items), cmp_items); | |
197 | } | |
198 | ||
e2421480 SB |
199 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
200 | const char *string) | |
c455c87c JS |
201 | { |
202 | int i; | |
203 | for (i = 0; i < list->nr; i++) | |
204 | if (!strcmp(string, list->items[i].string)) | |
e2421480 SB |
205 | return list->items + i; |
206 | return NULL; | |
207 | } | |
208 | ||
209 | int unsorted_string_list_has_string(struct string_list *list, | |
210 | const char *string) | |
211 | { | |
212 | return unsorted_string_list_lookup(list, string) != NULL; | |
c455c87c JS |
213 | } |
214 | ||
86d4b528 JS |
215 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util) |
216 | { | |
217 | if (list->strdup_strings) | |
218 | free(list->items[i].string); | |
219 | if (free_util) | |
220 | free(list->items[i].util); | |
221 | list->items[i] = list->items[list->nr-1]; | |
222 | list->nr--; | |
223 | } | |
ff919f96 MH |
224 | |
225 | int string_list_split(struct string_list *list, const char *string, | |
226 | int delim, int maxsplit) | |
227 | { | |
228 | int count = 0; | |
229 | const char *p = string, *end; | |
230 | ||
231 | if (!list->strdup_strings) | |
232 | die("internal error in string_list_split(): " | |
233 | "list->strdup_strings must be set"); | |
234 | for (;;) { | |
235 | count++; | |
236 | if (maxsplit >= 0 && count > maxsplit) { | |
237 | string_list_append(list, p); | |
238 | return count; | |
239 | } | |
240 | end = strchr(p, delim); | |
241 | if (end) { | |
242 | string_list_append_nodup(list, xmemdupz(p, end - p)); | |
243 | p = end + 1; | |
244 | } else { | |
245 | string_list_append(list, p); | |
246 | return count; | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | int string_list_split_in_place(struct string_list *list, char *string, | |
252 | int delim, int maxsplit) | |
253 | { | |
254 | int count = 0; | |
255 | char *p = string, *end; | |
256 | ||
257 | if (list->strdup_strings) | |
258 | die("internal error in string_list_split_in_place(): " | |
259 | "list->strdup_strings must not be set"); | |
260 | for (;;) { | |
261 | count++; | |
262 | if (maxsplit >= 0 && count > maxsplit) { | |
263 | string_list_append(list, p); | |
264 | return count; | |
265 | } | |
266 | end = strchr(p, delim); | |
267 | if (end) { | |
268 | *end = '\0'; | |
269 | string_list_append(list, p); | |
270 | p = end + 1; | |
271 | } else { | |
272 | string_list_append(list, p); | |
273 | return count; | |
274 | } | |
275 | } | |
276 | } |