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 int string_list_has_string(const struct string_list
*list
, const char *string
)
70 get_entry_index(list
, string
, &exact_match
);
74 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
75 int negative_existing_index
)
78 int index
= get_entry_index(list
, string
, &exact_match
);
80 index
= -1 - (negative_existing_index
? index
: 0);
84 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
86 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
89 return list
->items
+ i
;
92 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
96 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
97 for (src
= dst
= 1; src
< list
->nr
; src
++) {
98 if (!cmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
99 if (list
->strdup_strings
)
100 free(list
->items
[src
].string
);
102 free(list
->items
[src
].util
);
104 list
->items
[dst
++] = list
->items
[src
];
110 int for_each_string_list(struct string_list
*list
,
111 string_list_each_func_t fn
, void *cb_data
)
114 for (i
= 0; i
< list
->nr
; i
++)
115 if ((ret
= fn(&list
->items
[i
], cb_data
)))
120 void filter_string_list(struct string_list
*list
, int free_util
,
121 string_list_each_func_t want
, void *cb_data
)
124 for (src
= 0; src
< list
->nr
; src
++) {
125 if (want(&list
->items
[src
], cb_data
)) {
126 list
->items
[dst
++] = list
->items
[src
];
128 if (list
->strdup_strings
)
129 free(list
->items
[src
].string
);
131 free(list
->items
[src
].util
);
137 static int item_is_not_empty(struct string_list_item
*item
, void *unused
)
139 return *item
->string
!= '\0';
142 void string_list_remove_empty_items(struct string_list
*list
, int free_util
) {
143 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
146 void string_list_clear(struct string_list
*list
, int free_util
)
150 if (list
->strdup_strings
) {
151 for (i
= 0; i
< list
->nr
; i
++)
152 free(list
->items
[i
].string
);
155 for (i
= 0; i
< list
->nr
; i
++)
156 free(list
->items
[i
].util
);
161 list
->nr
= list
->alloc
= 0;
164 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
169 for (i
= 0; i
< list
->nr
; i
++)
170 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
172 if (list
->strdup_strings
) {
173 for (i
= 0; i
< list
->nr
; i
++)
174 free(list
->items
[i
].string
);
179 list
->nr
= list
->alloc
= 0;
183 void print_string_list(const struct string_list
*p
, const char *text
)
187 printf("%s\n", text
);
188 for (i
= 0; i
< p
->nr
; i
++)
189 printf("%s:%p\n", p
->items
[i
].string
, p
->items
[i
].util
);
192 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
195 struct string_list_item
*retval
;
196 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
197 retval
= &list
->items
[list
->nr
++];
198 retval
->string
= string
;
203 struct string_list_item
*string_list_append(struct string_list
*list
,
206 return string_list_append_nodup(
208 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
211 static int cmp_items(const void *a
, const void *b
, void *ctx
)
213 compare_strings_fn cmp
= ctx
;
214 const struct string_list_item
*one
= a
;
215 const struct string_list_item
*two
= b
;
216 return cmp(one
->string
, two
->string
);
219 void string_list_sort(struct string_list
*list
)
221 QSORT_S(list
->items
, list
->nr
, cmp_items
,
222 list
->cmp
? list
->cmp
: strcmp
);
225 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
228 struct string_list_item
*item
;
229 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
231 for_each_string_list_item(item
, list
)
232 if (!cmp(string
, item
->string
))
237 int unsorted_string_list_has_string(struct string_list
*list
,
240 return unsorted_string_list_lookup(list
, string
) != NULL
;
243 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
245 if (list
->strdup_strings
)
246 free(list
->items
[i
].string
);
248 free(list
->items
[i
].util
);
249 list
->items
[i
] = list
->items
[list
->nr
-1];
253 int string_list_split(struct string_list
*list
, const char *string
,
254 int delim
, int maxsplit
)
257 const char *p
= string
, *end
;
259 if (!list
->strdup_strings
)
260 die("internal error in string_list_split(): "
261 "list->strdup_strings must be set");
264 if (maxsplit
>= 0 && count
> maxsplit
) {
265 string_list_append(list
, p
);
268 end
= strchr(p
, delim
);
270 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
273 string_list_append(list
, p
);
279 int string_list_split_in_place(struct string_list
*list
, char *string
,
280 int delim
, int maxsplit
)
283 char *p
= string
, *end
;
285 if (list
->strdup_strings
)
286 die("internal error in string_list_split_in_place(): "
287 "list->strdup_strings must not be set");
290 if (maxsplit
>= 0 && count
> maxsplit
) {
291 string_list_append(list
, p
);
294 end
= strchr(p
, delim
);
297 string_list_append(list
, p
);
300 string_list_append(list
, p
);