1 #include "git-compat-util.h"
2 #include "string-list.h"
4 void string_list_init_nodup(struct string_list
*list
)
6 struct string_list blank
= STRING_LIST_INIT_NODUP
;
7 memcpy(list
, &blank
, sizeof(*list
));
10 void string_list_init_dup(struct string_list
*list
)
12 struct string_list blank
= STRING_LIST_INIT_DUP
;
13 memcpy(list
, &blank
, sizeof(*list
));
16 /* if there is no exact match, point to the index where the entry could be
18 static int get_entry_index(const struct string_list
*list
, const char *string
,
21 int left
= -1, right
= list
->nr
;
22 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
24 while (left
+ 1 < right
) {
25 int middle
= left
+ (right
- left
) / 2;
26 int compare
= cmp(string
, list
->items
[middle
].string
);
41 /* returns -1-index if already exists */
42 static int add_entry(int insert_at
, struct string_list
*list
, const char *string
)
45 int index
= insert_at
!= -1 ? insert_at
: get_entry_index(list
, string
, &exact_match
);
50 ALLOC_GROW(list
->items
, list
->nr
+1, list
->alloc
);
52 MOVE_ARRAY(list
->items
+ index
+ 1, list
->items
+ index
,
54 list
->items
[index
].string
= list
->strdup_strings
?
55 xstrdup(string
) : (char *)string
;
56 list
->items
[index
].util
= NULL
;
62 struct string_list_item
*string_list_insert(struct string_list
*list
, const char *string
)
64 int index
= add_entry(-1, list
, string
);
69 return list
->items
+ index
;
72 void string_list_remove(struct string_list
*list
, const char *string
,
76 int i
= get_entry_index(list
, string
, &exact_match
);
79 if (list
->strdup_strings
)
80 free(list
->items
[i
].string
);
82 free(list
->items
[i
].util
);
85 MOVE_ARRAY(list
->items
+ i
, list
->items
+ i
+ 1, list
->nr
- i
);
89 int string_list_has_string(const struct string_list
*list
, const char *string
)
92 get_entry_index(list
, string
, &exact_match
);
96 int string_list_find_insert_index(const struct string_list
*list
, const char *string
,
97 int negative_existing_index
)
100 int index
= get_entry_index(list
, string
, &exact_match
);
102 index
= -1 - (negative_existing_index
? index
: 0);
106 struct string_list_item
*string_list_lookup(struct string_list
*list
, const char *string
)
108 int exact_match
, i
= get_entry_index(list
, string
, &exact_match
);
111 return list
->items
+ i
;
114 void string_list_remove_duplicates(struct string_list
*list
, int free_util
)
118 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
119 for (src
= dst
= 1; src
< list
->nr
; src
++) {
120 if (!cmp(list
->items
[dst
- 1].string
, list
->items
[src
].string
)) {
121 if (list
->strdup_strings
)
122 free(list
->items
[src
].string
);
124 free(list
->items
[src
].util
);
126 list
->items
[dst
++] = list
->items
[src
];
132 int for_each_string_list(struct string_list
*list
,
133 string_list_each_func_t fn
, void *cb_data
)
136 for (i
= 0; i
< list
->nr
; i
++)
137 if ((ret
= fn(&list
->items
[i
], cb_data
)))
142 void filter_string_list(struct string_list
*list
, int free_util
,
143 string_list_each_func_t want
, void *cb_data
)
146 for (src
= 0; src
< list
->nr
; src
++) {
147 if (want(&list
->items
[src
], cb_data
)) {
148 list
->items
[dst
++] = list
->items
[src
];
150 if (list
->strdup_strings
)
151 free(list
->items
[src
].string
);
153 free(list
->items
[src
].util
);
159 static int item_is_not_empty(struct string_list_item
*item
, void *data UNUSED
)
161 return *item
->string
!= '\0';
164 void string_list_remove_empty_items(struct string_list
*list
, int free_util
)
166 filter_string_list(list
, free_util
, item_is_not_empty
, NULL
);
169 void string_list_clear(struct string_list
*list
, int free_util
)
173 if (list
->strdup_strings
) {
174 for (i
= 0; i
< list
->nr
; i
++)
175 free(list
->items
[i
].string
);
178 for (i
= 0; i
< list
->nr
; i
++)
179 free(list
->items
[i
].util
);
184 list
->nr
= list
->alloc
= 0;
187 void string_list_clear_func(struct string_list
*list
, string_list_clear_func_t clearfunc
)
192 for (i
= 0; i
< list
->nr
; i
++)
193 clearfunc(list
->items
[i
].util
, list
->items
[i
].string
);
195 if (list
->strdup_strings
) {
196 for (i
= 0; i
< list
->nr
; i
++)
197 free(list
->items
[i
].string
);
202 list
->nr
= list
->alloc
= 0;
205 void string_list_setlen(struct string_list
*list
, size_t nr
)
207 if (list
->strdup_strings
)
208 BUG("cannot setlen a string_list which owns its entries");
210 BUG("cannot grow a string_list with setlen");
214 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
217 struct string_list_item
*retval
;
218 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
219 retval
= &list
->items
[list
->nr
++];
220 retval
->string
= string
;
225 struct string_list_item
*string_list_append(struct string_list
*list
,
228 return string_list_append_nodup(
230 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
234 * Encapsulate the compare function pointer because ISO C99 forbids
235 * casting from void * to a function pointer and vice versa.
237 struct string_list_sort_ctx
239 compare_strings_fn cmp
;
242 static int cmp_items(const void *a
, const void *b
, void *ctx
)
244 struct string_list_sort_ctx
*sort_ctx
= ctx
;
245 const struct string_list_item
*one
= a
;
246 const struct string_list_item
*two
= b
;
247 return sort_ctx
->cmp(one
->string
, two
->string
);
250 void string_list_sort(struct string_list
*list
)
252 struct string_list_sort_ctx sort_ctx
= {list
->cmp
? list
->cmp
: strcmp
};
254 QSORT_S(list
->items
, list
->nr
, cmp_items
, &sort_ctx
);
257 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
260 struct string_list_item
*item
;
261 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
263 for_each_string_list_item(item
, list
)
264 if (!cmp(string
, item
->string
))
269 int unsorted_string_list_has_string(struct string_list
*list
,
272 return unsorted_string_list_lookup(list
, string
) != NULL
;
275 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
277 if (list
->strdup_strings
)
278 free(list
->items
[i
].string
);
280 free(list
->items
[i
].util
);
281 list
->items
[i
] = list
->items
[list
->nr
-1];
285 int string_list_split(struct string_list
*list
, const char *string
,
286 int delim
, int maxsplit
)
289 const char *p
= string
, *end
;
291 if (!list
->strdup_strings
)
292 die("internal error in string_list_split(): "
293 "list->strdup_strings must be set");
296 if (maxsplit
>= 0 && count
> maxsplit
) {
297 string_list_append(list
, p
);
300 end
= strchr(p
, delim
);
302 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
305 string_list_append(list
, p
);
311 int string_list_split_in_place(struct string_list
*list
, char *string
,
312 const char *delim
, int maxsplit
)
315 char *p
= string
, *end
;
317 if (list
->strdup_strings
)
318 die("internal error in string_list_split_in_place(): "
319 "list->strdup_strings must not be set");
322 if (maxsplit
>= 0 && count
> maxsplit
) {
323 string_list_append(list
, p
);
326 end
= strpbrk(p
, delim
);
329 string_list_append(list
, p
);
332 string_list_append(list
, p
);