2 #include "string-list.h"
4 void string_list_init_nodup(struct string_list
*list
)
6 struct string_list blank
= STRING_LIST_INIT_NODUP
;
7 memcpy(list
, &blank
, sizeof(*list
));
10 void string_list_init_dup(struct string_list
*list
)
12 struct string_list blank
= STRING_LIST_INIT_DUP
;
13 memcpy(list
, &blank
, sizeof(*list
));
16 void string_list_init(struct string_list
*list
, int strdup_strings
)
19 string_list_init_dup(list
);
21 string_list_init_nodup(list
);
24 /* if there is no exact match, point to the index where the entry could be
26 static int get_entry_index(const struct string_list
*list
, const char *string
,
29 int left
= -1, right
= list
->nr
;
30 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
32 while (left
+ 1 < right
) {
33 int middle
= left
+ (right
- left
) / 2;
34 int compare
= cmp(string
, list
->items
[middle
].string
);
49 /* returns -1-index if already exists */
50 static int add_entry(int insert_at
, struct string_list
*list
, const char *string
)
53 int index
= insert_at
!= -1 ? insert_at
: get_entry_index(list
, string
, &exact_match
);
58 ALLOC_GROW(list
->items
, list
->nr
+1, list
->alloc
);
60 MOVE_ARRAY(list
->items
+ index
+ 1, list
->items
+ index
,
62 list
->items
[index
].string
= list
->strdup_strings
?
63 xstrdup(string
) : (char *)string
;
64 list
->items
[index
].util
= NULL
;
70 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
72 int index
= add_entry(-1, list
, string
);
77 return list
->items
+ index
;
80 void string_list_remove(struct string_list
*list
, const char *string
,
84 int i
= get_entry_index(list
, string
, &exact_match
);
87 if (list
->strdup_strings
)
88 free(list
->items
[i
].string
);
90 free(list
->items
[i
].util
);
93 MOVE_ARRAY(list
->items
+ i
, list
->items
+ i
+ 1, list
->nr
- i
);
97 int string_list_has_string(const struct string_list
*list
, const char *string
)
100 get_entry_index(list
, string
, &exact_match
);
104 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
105 int negative_existing_index
)
108 int index
= get_entry_index(list
, string
, &exact_match
);
110 index
= -1 - (negative_existing_index
? index
: 0);
114 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
116 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
119 return list
->items
+ i
;
122 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
126 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
127 for (src
= dst
= 1; src
< list
->nr
; src
++) {
128 if (!cmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
129 if (list
->strdup_strings
)
130 free(list
->items
[src
].string
);
132 free(list
->items
[src
].util
);
134 list
->items
[dst
++] = list
->items
[src
];
140 int for_each_string_list(struct string_list
*list
,
141 string_list_each_func_t fn
, void *cb_data
)
144 for (i
= 0; i
< list
->nr
; i
++)
145 if ((ret
= fn(&list
->items
[i
], cb_data
)))
150 void filter_string_list(struct string_list
*list
, int free_util
,
151 string_list_each_func_t want
, void *cb_data
)
154 for (src
= 0; src
< list
->nr
; src
++) {
155 if (want(&list
->items
[src
], cb_data
)) {
156 list
->items
[dst
++] = list
->items
[src
];
158 if (list
->strdup_strings
)
159 free(list
->items
[src
].string
);
161 free(list
->items
[src
].util
);
167 static int item_is_not_empty(struct string_list_item
*item
, void *unused
)
169 return *item
->string
!= '\0';
172 void string_list_remove_empty_items(struct string_list
*list
, int free_util
)
174 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
177 void string_list_clear(struct string_list
*list
, int free_util
)
181 if (list
->strdup_strings
) {
182 for (i
= 0; i
< list
->nr
; i
++)
183 free(list
->items
[i
].string
);
186 for (i
= 0; i
< list
->nr
; i
++)
187 free(list
->items
[i
].util
);
192 list
->nr
= list
->alloc
= 0;
195 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
200 for (i
= 0; i
< list
->nr
; i
++)
201 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
203 if (list
->strdup_strings
) {
204 for (i
= 0; i
< list
->nr
; i
++)
205 free(list
->items
[i
].string
);
210 list
->nr
= list
->alloc
= 0;
213 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
216 struct string_list_item
*retval
;
217 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
218 retval
= &list
->items
[list
->nr
++];
219 retval
->string
= string
;
224 struct string_list_item
*string_list_append(struct string_list
*list
,
227 return string_list_append_nodup(
229 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
233 * Encapsulate the compare function pointer because ISO C99 forbids
234 * casting from void * to a function pointer and vice versa.
236 struct string_list_sort_ctx
238 compare_strings_fn cmp
;
241 static int cmp_items(const void *a
, const void *b
, void *ctx
)
243 struct string_list_sort_ctx
*sort_ctx
= ctx
;
244 const struct string_list_item
*one
= a
;
245 const struct string_list_item
*two
= b
;
246 return sort_ctx
->cmp(one
->string
, two
->string
);
249 void string_list_sort(struct string_list
*list
)
251 struct string_list_sort_ctx sort_ctx
= {list
->cmp
? list
->cmp
: strcmp
};
253 QSORT_S(list
->items
, list
->nr
, cmp_items
, &sort_ctx
);
256 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
259 struct string_list_item
*item
;
260 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
262 for_each_string_list_item(item
, list
)
263 if (!cmp(string
, item
->string
))
268 int unsorted_string_list_has_string(struct string_list
*list
,
271 return unsorted_string_list_lookup(list
, string
) != NULL
;
274 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
276 if (list
->strdup_strings
)
277 free(list
->items
[i
].string
);
279 free(list
->items
[i
].util
);
280 list
->items
[i
] = list
->items
[list
->nr
-1];
284 int string_list_split(struct string_list
*list
, const char *string
,
285 int delim
, int maxsplit
)
288 const char *p
= string
, *end
;
290 if (!list
->strdup_strings
)
291 die("internal error in string_list_split(): "
292 "list->strdup_strings must be set");
295 if (maxsplit
>= 0 && count
> maxsplit
) {
296 string_list_append(list
, p
);
299 end
= strchr(p
, delim
);
301 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
304 string_list_append(list
, p
);
310 int string_list_split_in_place(struct string_list
*list
, char *string
,
311 int delim
, int maxsplit
)
314 char *p
= string
, *end
;
316 if (list
->strdup_strings
)
317 die("internal error in string_list_split_in_place(): "
318 "list->strdup_strings must not be set");
321 if (maxsplit
>= 0 && count
> maxsplit
) {
322 string_list_append(list
, p
);
325 end
= strchr(p
, delim
);
328 string_list_append(list
, p
);
331 string_list_append(list
, p
);