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 if (list
->nr
+ 1 >= list
->alloc
) {
46 list
->items
= xrealloc(list
->items
, list
->alloc
47 * sizeof(struct string_list_item
));
50 memmove(list
->items
+ index
+ 1, list
->items
+ index
,
52 * sizeof(struct string_list_item
));
53 list
->items
[index
].string
= list
->strdup_strings
?
54 xstrdup(string
) : (char *)string
;
55 list
->items
[index
].util
= NULL
;
61 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
63 return string_list_insert_at_index(list
, -1, string
);
66 struct string_list_item
*string_list_insert_at_index(struct string_list
*list
,
67 int insert_at
, const char *string
)
69 int index
= add_entry(insert_at
, list
, string
);
74 return list
->items
+ index
;
77 int string_list_has_string(const struct string_list
*list
, const char *string
)
80 get_entry_index(list
, string
, &exact_match
);
84 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
85 int negative_existing_index
)
88 int index
= get_entry_index(list
, string
, &exact_match
);
90 index
= -1 - (negative_existing_index
? index
: 0);
94 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
96 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
99 return list
->items
+ i
;
102 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
106 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
107 for (src
= dst
= 1; src
< list
->nr
; src
++) {
108 if (!cmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
109 if (list
->strdup_strings
)
110 free(list
->items
[src
].string
);
112 free(list
->items
[src
].util
);
114 list
->items
[dst
++] = list
->items
[src
];
120 int for_each_string_list(struct string_list
*list
,
121 string_list_each_func_t fn
, void *cb_data
)
124 for (i
= 0; i
< list
->nr
; i
++)
125 if ((ret
= fn(&list
->items
[i
], cb_data
)))
130 void filter_string_list(struct string_list
*list
, int free_util
,
131 string_list_each_func_t want
, void *cb_data
)
134 for (src
= 0; src
< list
->nr
; src
++) {
135 if (want(&list
->items
[src
], cb_data
)) {
136 list
->items
[dst
++] = list
->items
[src
];
138 if (list
->strdup_strings
)
139 free(list
->items
[src
].string
);
141 free(list
->items
[src
].util
);
147 static int item_is_not_empty(struct string_list_item
*item
, void *unused
)
149 return *item
->string
!= '\0';
152 void string_list_remove_empty_items(struct string_list
*list
, int free_util
) {
153 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
156 void string_list_clear(struct string_list
*list
, int free_util
)
160 if (list
->strdup_strings
) {
161 for (i
= 0; i
< list
->nr
; i
++)
162 free(list
->items
[i
].string
);
165 for (i
= 0; i
< list
->nr
; i
++)
166 free(list
->items
[i
].util
);
171 list
->nr
= list
->alloc
= 0;
174 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
179 for (i
= 0; i
< list
->nr
; i
++)
180 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
182 if (list
->strdup_strings
) {
183 for (i
= 0; i
< list
->nr
; i
++)
184 free(list
->items
[i
].string
);
189 list
->nr
= list
->alloc
= 0;
193 void print_string_list(const struct string_list
*p
, const char *text
)
197 printf("%s\n", text
);
198 for (i
= 0; i
< p
->nr
; i
++)
199 printf("%s:%p\n", p
->items
[i
].string
, p
->items
[i
].util
);
202 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
205 struct string_list_item
*retval
;
206 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
207 retval
= &list
->items
[list
->nr
++];
208 retval
->string
= string
;
213 struct string_list_item
*string_list_append(struct string_list
*list
,
216 return string_list_append_nodup(
218 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
222 static compare_strings_fn compare_for_qsort
;
224 /* Only call this from inside sort_string_list! */
225 static int cmp_items(const void *a
, const void *b
)
227 const struct string_list_item
*one
= a
;
228 const struct string_list_item
*two
= b
;
229 return compare_for_qsort(one
->string
, two
->string
);
232 void sort_string_list(struct string_list
*list
)
234 compare_for_qsort
= list
->cmp
? list
->cmp
: strcmp
;
235 qsort(list
->items
, list
->nr
, sizeof(*list
->items
), cmp_items
);
238 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
242 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
244 for (i
= 0; i
< list
->nr
; i
++)
245 if (!cmp(string
, list
->items
[i
].string
))
246 return list
->items
+ i
;
250 int unsorted_string_list_has_string(struct string_list
*list
,
253 return unsorted_string_list_lookup(list
, string
) != NULL
;
256 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
258 if (list
->strdup_strings
)
259 free(list
->items
[i
].string
);
261 free(list
->items
[i
].util
);
262 list
->items
[i
] = list
->items
[list
->nr
-1];
266 int string_list_split(struct string_list
*list
, const char *string
,
267 int delim
, int maxsplit
)
270 const char *p
= string
, *end
;
272 if (!list
->strdup_strings
)
273 die("internal error in string_list_split(): "
274 "list->strdup_strings must be set");
277 if (maxsplit
>= 0 && count
> maxsplit
) {
278 string_list_append(list
, p
);
281 end
= strchr(p
, delim
);
283 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
286 string_list_append(list
, p
);
292 int string_list_split_in_place(struct string_list
*list
, char *string
,
293 int delim
, int maxsplit
)
296 char *p
= string
, *end
;
298 if (list
->strdup_strings
)
299 die("internal error in string_list_split_in_place(): "
300 "list->strdup_strings must not be set");
303 if (maxsplit
>= 0 && count
> maxsplit
) {
304 string_list_append(list
, p
);
307 end
= strchr(p
, delim
);
310 string_list_append(list
, p
);
313 string_list_append(list
, p
);