Merge tag 'l10n-2.14.0-rnd2' of git://github.com/git-l10n/git-po
[git/raj.git] / string-list.c
blobc650500c6e51d983dc45a4be3b8dad98f1dece92
1 #include "cache.h"
2 #include "string-list.h"
4 void string_list_init(struct string_list *list, int strdup_strings)
6 memset(list, 0, sizeof(*list));
7 list->strdup_strings = strdup_strings;
10 /* if there is no exact match, point to the index where the entry could be
11 * inserted */
12 static int get_entry_index(const struct string_list *list, const char *string,
13 int *exact_match)
15 int left = -1, right = list->nr;
16 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
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;
31 *exact_match = 0;
32 return right;
35 /* returns -1-index if already exists */
36 static int add_entry(int insert_at, struct string_list *list, const char *string)
38 int exact_match = 0;
39 int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
41 if (exact_match)
42 return -1 - index;
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++;
54 return index;
57 struct string_list_item *string_list_insert(struct string_list *list, const char *string)
59 int index = add_entry(-1, list, string);
61 if (index < 0)
62 index = -1 - index;
64 return list->items + index;
67 void string_list_remove(struct string_list *list, const char *string,
68 int free_util)
70 int exact_match;
71 int i = get_entry_index(list, string, &exact_match);
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);
79 list->nr--;
80 memmove(list->items + i, list->items + i + 1,
81 (list->nr - i) * sizeof(struct string_list_item));
85 int string_list_has_string(const struct string_list *list, const char *string)
87 int exact_match;
88 get_entry_index(list, string, &exact_match);
89 return exact_match;
92 int string_list_find_insert_index(const struct string_list *list, const char *string,
93 int negative_existing_index)
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;
102 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
104 int exact_match, i = get_entry_index(list, string, &exact_match);
105 if (!exact_match)
106 return NULL;
107 return list->items + i;
110 void string_list_remove_duplicates(struct string_list *list, int free_util)
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];
124 list->nr = dst;
128 int for_each_string_list(struct string_list *list,
129 string_list_each_func_t fn, void *cb_data)
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;
138 void filter_string_list(struct string_list *list, int free_util,
139 string_list_each_func_t want, void *cb_data)
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);
152 list->nr = dst;
155 static int item_is_not_empty(struct string_list_item *item, void *unused)
157 return *item->string != '\0';
160 void string_list_remove_empty_items(struct string_list *list, int free_util) {
161 filter_string_list(list, free_util, item_is_not_empty, NULL);
164 void string_list_clear(struct string_list *list, int free_util)
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);
172 if (free_util) {
173 for (i = 0; i < list->nr; i++)
174 free(list->items[i].util);
176 free(list->items);
178 list->items = NULL;
179 list->nr = list->alloc = 0;
182 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
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);
190 if (list->strdup_strings) {
191 for (i = 0; i < list->nr; i++)
192 free(list->items[i].string);
194 free(list->items);
196 list->items = NULL;
197 list->nr = list->alloc = 0;
201 void print_string_list(const struct string_list *p, const char *text)
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);
210 struct string_list_item *string_list_append_nodup(struct string_list *list,
211 char *string)
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;
221 struct string_list_item *string_list_append(struct string_list *list,
222 const char *string)
224 return string_list_append_nodup(
225 list,
226 list->strdup_strings ? xstrdup(string) : (char *)string);
229 static int cmp_items(const void *a, const void *b, void *ctx)
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);
237 void string_list_sort(struct string_list *list)
239 QSORT_S(list->items, list->nr, cmp_items,
240 list->cmp ? list->cmp : strcmp);
243 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
244 const char *string)
246 struct string_list_item *item;
247 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
249 for_each_string_list_item(item, list)
250 if (!cmp(string, item->string))
251 return item;
252 return NULL;
255 int unsorted_string_list_has_string(struct string_list *list,
256 const char *string)
258 return unsorted_string_list_lookup(list, string) != NULL;
261 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
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--;
271 int string_list_split(struct string_list *list, const char *string,
272 int delim, int maxsplit)
274 int count = 0;
275 const char *p = string, *end;
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;
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;
297 int string_list_split_in_place(struct string_list *list, char *string,
298 int delim, int maxsplit)
300 int count = 0;
301 char *p = string, *end;
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;
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;