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 int for_each_string_list(struct string_list
*list
,
96 string_list_each_func_t fn
, void *cb_data
)
99 for (i
= 0; i
< list
->nr
; i
++)
100 if ((ret
= fn(&list
->items
[i
], cb_data
)))
105 void string_list_clear(struct string_list
*list
, int free_util
)
109 if (list
->strdup_strings
) {
110 for (i
= 0; i
< list
->nr
; i
++)
111 free(list
->items
[i
].string
);
114 for (i
= 0; i
< list
->nr
; i
++)
115 free(list
->items
[i
].util
);
120 list
->nr
= list
->alloc
= 0;
123 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
128 for (i
= 0; i
< list
->nr
; i
++)
129 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
131 if (list
->strdup_strings
) {
132 for (i
= 0; i
< list
->nr
; i
++)
133 free(list
->items
[i
].string
);
138 list
->nr
= list
->alloc
= 0;
142 void print_string_list(const struct string_list
*p
, const char *text
)
146 printf("%s\n", text
);
147 for (i
= 0; i
< p
->nr
; i
++)
148 printf("%s:%p\n", p
->items
[i
].string
, p
->items
[i
].util
);
151 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
154 struct string_list_item
*retval
;
155 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
156 retval
= &list
->items
[list
->nr
++];
157 retval
->string
= string
;
162 struct string_list_item
*string_list_append(struct string_list
*list
,
165 return string_list_append_nodup(
167 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
170 static int cmp_items(const void *a
, const void *b
)
172 const struct string_list_item
*one
= a
;
173 const struct string_list_item
*two
= b
;
174 return strcmp(one
->string
, two
->string
);
177 void sort_string_list(struct string_list
*list
)
179 qsort(list
->items
, list
->nr
, sizeof(*list
->items
), cmp_items
);
182 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
186 for (i
= 0; i
< list
->nr
; i
++)
187 if (!strcmp(string
, list
->items
[i
].string
))
188 return list
->items
+ i
;
192 int unsorted_string_list_has_string(struct string_list
*list
,
195 return unsorted_string_list_lookup(list
, string
) != NULL
;
198 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
200 if (list
->strdup_strings
)
201 free(list
->items
[i
].string
);
203 free(list
->items
[i
].util
);
204 list
->items
[i
] = list
->items
[list
->nr
-1];