1 #include "git-compat-util.h"
2 #include "string-list.h"
5 void string_list_init_nodup(struct string_list
*list
)
7 struct string_list blank
= STRING_LIST_INIT_NODUP
;
8 memcpy(list
, &blank
, sizeof(*list
));
11 void string_list_init_dup(struct string_list
*list
)
13 struct string_list blank
= STRING_LIST_INIT_DUP
;
14 memcpy(list
, &blank
, sizeof(*list
));
17 /* if there is no exact match, point to the index where the entry could be
19 static int get_entry_index(const struct string_list
*list
, const char *string
,
22 int left
= -1, right
= list
->nr
;
23 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
25 while (left
+ 1 < right
) {
26 int middle
= left
+ (right
- left
) / 2;
27 int compare
= cmp(string
, list
->items
[middle
].string
);
42 /* returns -1-index if already exists */
43 static int add_entry(int insert_at
, struct string_list
*list
, const char *string
)
46 int index
= insert_at
!= -1 ? insert_at
: get_entry_index(list
, string
, &exact_match
);
51 ALLOC_GROW(list
->items
, list
->nr
+1, list
->alloc
);
53 MOVE_ARRAY(list
->items
+ index
+ 1, list
->items
+ index
,
55 list
->items
[index
].string
= list
->strdup_strings
?
56 xstrdup(string
) : (char *)string
;
57 list
->items
[index
].util
= NULL
;
63 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
65 int index
= add_entry(-1, list
, string
);
70 return list
->items
+ index
;
73 void string_list_remove(struct string_list
*list
, const char *string
,
77 int i
= get_entry_index(list
, string
, &exact_match
);
80 if (list
->strdup_strings
)
81 free(list
->items
[i
].string
);
83 free(list
->items
[i
].util
);
86 MOVE_ARRAY(list
->items
+ i
, list
->items
+ i
+ 1, list
->nr
- i
);
90 int string_list_has_string(const struct string_list
*list
, const char *string
)
93 get_entry_index(list
, string
, &exact_match
);
97 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
98 int negative_existing_index
)
101 int index
= get_entry_index(list
, string
, &exact_match
);
103 index
= -1 - (negative_existing_index
? index
: 0);
107 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
109 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
112 return list
->items
+ i
;
115 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
119 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
120 for (src
= dst
= 1; src
< list
->nr
; src
++) {
121 if (!cmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
122 if (list
->strdup_strings
)
123 free(list
->items
[src
].string
);
125 free(list
->items
[src
].util
);
127 list
->items
[dst
++] = list
->items
[src
];
133 int for_each_string_list(struct string_list
*list
,
134 string_list_each_func_t fn
, void *cb_data
)
137 for (i
= 0; i
< list
->nr
; i
++)
138 if ((ret
= fn(&list
->items
[i
], cb_data
)))
143 void filter_string_list(struct string_list
*list
, int free_util
,
144 string_list_each_func_t want
, void *cb_data
)
147 for (src
= 0; src
< list
->nr
; src
++) {
148 if (want(&list
->items
[src
], cb_data
)) {
149 list
->items
[dst
++] = list
->items
[src
];
151 if (list
->strdup_strings
)
152 free(list
->items
[src
].string
);
154 free(list
->items
[src
].util
);
160 static int item_is_not_empty(struct string_list_item
*item
, void *data UNUSED
)
162 return *item
->string
!= '\0';
165 void string_list_remove_empty_items(struct string_list
*list
, int free_util
)
167 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
170 void string_list_clear(struct string_list
*list
, int free_util
)
174 if (list
->strdup_strings
) {
175 for (i
= 0; i
< list
->nr
; i
++)
176 free(list
->items
[i
].string
);
179 for (i
= 0; i
< list
->nr
; i
++)
180 free(list
->items
[i
].util
);
185 list
->nr
= list
->alloc
= 0;
188 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
193 for (i
= 0; i
< list
->nr
; i
++)
194 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
196 if (list
->strdup_strings
) {
197 for (i
= 0; i
< list
->nr
; i
++)
198 free(list
->items
[i
].string
);
203 list
->nr
= list
->alloc
= 0;
206 void string_list_setlen(struct string_list
*list
, size_t nr
)
208 if (list
->strdup_strings
)
209 BUG("cannot setlen a string_list which owns its entries");
211 BUG("cannot grow a string_list with setlen");
215 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
218 struct string_list_item
*retval
;
219 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
220 retval
= &list
->items
[list
->nr
++];
221 retval
->string
= string
;
226 struct string_list_item
*string_list_append(struct string_list
*list
,
229 return string_list_append_nodup(
231 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
235 * Encapsulate the compare function pointer because ISO C99 forbids
236 * casting from void * to a function pointer and vice versa.
238 struct string_list_sort_ctx
240 compare_strings_fn cmp
;
243 static int cmp_items(const void *a
, const void *b
, void *ctx
)
245 struct string_list_sort_ctx
*sort_ctx
= ctx
;
246 const struct string_list_item
*one
= a
;
247 const struct string_list_item
*two
= b
;
248 return sort_ctx
->cmp(one
->string
, two
->string
);
251 void string_list_sort(struct string_list
*list
)
253 struct string_list_sort_ctx sort_ctx
= {list
->cmp
? list
->cmp
: strcmp
};
255 QSORT_S(list
->items
, list
->nr
, cmp_items
, &sort_ctx
);
258 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
261 struct string_list_item
*item
;
262 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
264 for_each_string_list_item(item
, list
)
265 if (!cmp(string
, item
->string
))
270 int unsorted_string_list_has_string(struct string_list
*list
,
273 return unsorted_string_list_lookup(list
, string
) != NULL
;
276 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
278 if (list
->strdup_strings
)
279 free(list
->items
[i
].string
);
281 free(list
->items
[i
].util
);
282 list
->items
[i
] = list
->items
[list
->nr
-1];
286 int string_list_split(struct string_list
*list
, const char *string
,
287 int delim
, int maxsplit
)
290 const char *p
= string
, *end
;
292 if (!list
->strdup_strings
)
293 die("internal error in string_list_split(): "
294 "list->strdup_strings must be set");
297 if (maxsplit
>= 0 && count
> maxsplit
) {
298 string_list_append(list
, p
);
301 end
= strchr(p
, delim
);
303 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
306 string_list_append(list
, p
);
312 int string_list_split_in_place(struct string_list
*list
, char *string
,
313 const char *delim
, int maxsplit
)
316 char *p
= string
, *end
;
318 if (list
->strdup_strings
)
319 die("internal error in string_list_split_in_place(): "
320 "list->strdup_strings must not be set");
323 if (maxsplit
>= 0 && count
> maxsplit
) {
324 string_list_append(list
, p
);
327 end
= strpbrk(p
, delim
);
330 string_list_append(list
, p
);
333 string_list_append(list
, p
);