2 #include "string-list.h"
4 /* if there is no exact match, point to the index where the entry could be
6 static int get_entry_index(const struct string_list
*list
, const char *string
,
9 int left
= -1, right
= list
->nr
;
11 while (left
+ 1 < right
) {
12 int middle
= (left
+ right
) / 2;
13 int compare
= strcmp(string
, list
->items
[middle
].string
);
28 /* returns -1-index if already exists */
29 static int add_entry(int insert_at
, struct string_list
*list
, const char *string
)
32 int index
= insert_at
!= -1 ? insert_at
: get_entry_index(list
, string
, &exact_match
);
37 if (list
->nr
+ 1 >= list
->alloc
) {
39 list
->items
= xrealloc(list
->items
, list
->alloc
40 * sizeof(struct string_list_item
));
43 memmove(list
->items
+ index
+ 1, list
->items
+ index
,
45 * sizeof(struct string_list_item
));
46 list
->items
[index
].string
= list
->strdup_strings
?
47 xstrdup(string
) : (char *)string
;
48 list
->items
[index
].util
= NULL
;
54 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
56 return string_list_insert_at_index(list
, -1, string
);
59 struct string_list_item
*string_list_insert_at_index(struct string_list
*list
,
60 int insert_at
, const char *string
)
62 int index
= add_entry(insert_at
, list
, string
);
67 return list
->items
+ index
;
70 int string_list_has_string(const struct string_list
*list
, const char *string
)
73 get_entry_index(list
, string
, &exact_match
);
77 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
78 int negative_existing_index
)
81 int index
= get_entry_index(list
, string
, &exact_match
);
83 index
= -1 - (negative_existing_index
? index
: 0);
87 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
89 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
92 return list
->items
+ i
;
95 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
99 for (src
= dst
= 1; src
< list
->nr
; src
++) {
100 if (!strcmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
101 if (list
->strdup_strings
)
102 free(list
->items
[src
].string
);
104 free(list
->items
[src
].util
);
106 list
->items
[dst
++] = list
->items
[src
];
112 int for_each_string_list(struct string_list
*list
,
113 string_list_each_func_t fn
, void *cb_data
)
116 for (i
= 0; i
< list
->nr
; i
++)
117 if ((ret
= fn(&list
->items
[i
], cb_data
)))
122 void filter_string_list(struct string_list
*list
, int free_util
,
123 string_list_each_func_t want
, void *cb_data
)
126 for (src
= 0; src
< list
->nr
; src
++) {
127 if (want(&list
->items
[src
], cb_data
)) {
128 list
->items
[dst
++] = list
->items
[src
];
130 if (list
->strdup_strings
)
131 free(list
->items
[src
].string
);
133 free(list
->items
[src
].util
);
139 static int item_is_not_empty(struct string_list_item
*item
, void *unused
)
141 return *item
->string
!= '\0';
144 void string_list_remove_empty_items(struct string_list
*list
, int free_util
) {
145 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
148 void string_list_clear(struct string_list
*list
, int free_util
)
152 if (list
->strdup_strings
) {
153 for (i
= 0; i
< list
->nr
; i
++)
154 free(list
->items
[i
].string
);
157 for (i
= 0; i
< list
->nr
; i
++)
158 free(list
->items
[i
].util
);
163 list
->nr
= list
->alloc
= 0;
166 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
171 for (i
= 0; i
< list
->nr
; i
++)
172 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
174 if (list
->strdup_strings
) {
175 for (i
= 0; i
< list
->nr
; i
++)
176 free(list
->items
[i
].string
);
181 list
->nr
= list
->alloc
= 0;
185 void print_string_list(const struct string_list
*p
, const char *text
)
189 printf("%s\n", text
);
190 for (i
= 0; i
< p
->nr
; i
++)
191 printf("%s:%p\n", p
->items
[i
].string
, p
->items
[i
].util
);
194 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
197 struct string_list_item
*retval
;
198 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
199 retval
= &list
->items
[list
->nr
++];
200 retval
->string
= string
;
205 struct string_list_item
*string_list_append(struct string_list
*list
,
208 return string_list_append_nodup(
210 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
213 static int cmp_items(const void *a
, const void *b
)
215 const struct string_list_item
*one
= a
;
216 const struct string_list_item
*two
= b
;
217 return strcmp(one
->string
, two
->string
);
220 void sort_string_list(struct string_list
*list
)
222 qsort(list
->items
, list
->nr
, sizeof(*list
->items
), cmp_items
);
225 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
229 for (i
= 0; i
< list
->nr
; i
++)
230 if (!strcmp(string
, list
->items
[i
].string
))
231 return list
->items
+ i
;
235 int unsorted_string_list_has_string(struct string_list
*list
,
238 return unsorted_string_list_lookup(list
, string
) != NULL
;
241 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
243 if (list
->strdup_strings
)
244 free(list
->items
[i
].string
);
246 free(list
->items
[i
].util
);
247 list
->items
[i
] = list
->items
[list
->nr
-1];
251 int string_list_split(struct string_list
*list
, const char *string
,
252 int delim
, int maxsplit
)
255 const char *p
= string
, *end
;
257 if (!list
->strdup_strings
)
258 die("internal error in string_list_split(): "
259 "list->strdup_strings must be set");
262 if (maxsplit
>= 0 && count
> maxsplit
) {
263 string_list_append(list
, p
);
266 end
= strchr(p
, delim
);
268 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
271 string_list_append(list
, p
);
277 int string_list_split_in_place(struct string_list
*list
, char *string
,
278 int delim
, int maxsplit
)
281 char *p
= string
, *end
;
283 if (list
->strdup_strings
)
284 die("internal error in string_list_split_in_place(): "
285 "list->strdup_strings must not be set");
288 if (maxsplit
>= 0 && count
> maxsplit
) {
289 string_list_append(list
, p
);
292 end
= strchr(p
, delim
);
295 string_list_append(list
, p
);
298 string_list_append(list
, p
);