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
12 static int get_entry_index(const struct string_list
*list
, const char *string
,
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
);
35 /* returns -1-index if already exists */
36 static int add_entry(int insert_at
, struct string_list
*list
, const char *string
)
39 int index
= insert_at
!= -1 ? insert_at
: get_entry_index(list
, string
, &exact_match
);
44 ALLOC_GROW(list
->items
, list
->nr
+1, list
->alloc
);
46 memmove(list
->items
+ index
+ 1, list
->items
+ 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
;
57 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
59 int index
= add_entry(-1, list
, string
);
64 return list
->items
+ index
;
67 void string_list_remove(struct string_list
*list
, const char *string
,
71 int i
= get_entry_index(list
, string
, &exact_match
);
74 if (list
->strdup_strings
)
75 free(list
->items
[i
].string
);
77 free(list
->items
[i
].util
);
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
)
88 get_entry_index(list
, string
, &exact_match
);
92 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
93 int negative_existing_index
)
96 int index
= get_entry_index(list
, string
, &exact_match
);
98 index
= -1 - (negative_existing_index
? index
: 0);
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
);
107 return list
->items
+ i
;
110 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
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
);
120 free(list
->items
[src
].util
);
122 list
->items
[dst
++] = list
->items
[src
];
128 int for_each_string_list(struct string_list
*list
,
129 string_list_each_func_t fn
, void *cb_data
)
132 for (i
= 0; i
< list
->nr
; i
++)
133 if ((ret
= fn(&list
->items
[i
], cb_data
)))
138 void filter_string_list(struct string_list
*list
, int free_util
,
139 string_list_each_func_t want
, void *cb_data
)
142 for (src
= 0; src
< list
->nr
; src
++) {
143 if (want(&list
->items
[src
], cb_data
)) {
144 list
->items
[dst
++] = list
->items
[src
];
146 if (list
->strdup_strings
)
147 free(list
->items
[src
].string
);
149 free(list
->items
[src
].util
);
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
)
168 if (list
->strdup_strings
) {
169 for (i
= 0; i
< list
->nr
; i
++)
170 free(list
->items
[i
].string
);
173 for (i
= 0; i
< list
->nr
; i
++)
174 free(list
->items
[i
].util
);
179 list
->nr
= list
->alloc
= 0;
182 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t 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
);
197 list
->nr
= list
->alloc
= 0;
201 void print_string_list(const struct string_list
*p
, const char *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
,
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
;
221 struct string_list_item
*string_list_append(struct string_list
*list
,
224 return string_list_append_nodup(
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
,
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
))
255 int unsorted_string_list_has_string(struct string_list
*list
,
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
);
266 free(list
->items
[i
].util
);
267 list
->items
[i
] = list
->items
[list
->nr
-1];
271 int string_list_split(struct string_list
*list
, const char *string
,
272 int delim
, int maxsplit
)
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");
282 if (maxsplit
>= 0 && count
> maxsplit
) {
283 string_list_append(list
, p
);
286 end
= strchr(p
, delim
);
288 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
291 string_list_append(list
, p
);
297 int string_list_split_in_place(struct string_list
*list
, char *string
,
298 int delim
, int maxsplit
)
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");
308 if (maxsplit
>= 0 && count
> maxsplit
) {
309 string_list_append(list
, p
);
312 end
= strchr(p
, delim
);
315 string_list_append(list
, p
);
318 string_list_append(list
, p
);