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 struct string_list_item
*string_list_append_nodup(struct string_list
*list
,
209 struct string_list_item
*retval
;
210 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
211 retval
= &list
->items
[list
->nr
++];
212 retval
->string
= string
;
217 struct string_list_item
*string_list_append(struct string_list
*list
,
220 return string_list_append_nodup(
222 list
->strdup_strings
? xstrdup(string
) : (char *)string
);
226 * Encapsulate the compare function pointer because ISO C99 forbids
227 * casting from void * to a function pointer and vice versa.
229 struct string_list_sort_ctx
231 compare_strings_fn cmp
;
234 static int cmp_items(const void *a
, const void *b
, void *ctx
)
236 struct string_list_sort_ctx
*sort_ctx
= ctx
;
237 const struct string_list_item
*one
= a
;
238 const struct string_list_item
*two
= b
;
239 return sort_ctx
->cmp(one
->string
, two
->string
);
242 void string_list_sort(struct string_list
*list
)
244 struct string_list_sort_ctx sort_ctx
= {list
->cmp
? list
->cmp
: strcmp
};
246 QSORT_S(list
->items
, list
->nr
, cmp_items
, &sort_ctx
);
249 struct string_list_item
*unsorted_string_list_lookup(struct string_list
*list
,
252 struct string_list_item
*item
;
253 compare_strings_fn cmp
= list
->cmp
? list
->cmp
: strcmp
;
255 for_each_string_list_item(item
, list
)
256 if (!cmp(string
, item
->string
))
261 int unsorted_string_list_has_string(struct string_list
*list
,
264 return unsorted_string_list_lookup(list
, string
) != NULL
;
267 void unsorted_string_list_delete_item(struct string_list
*list
, int i
, int free_util
)
269 if (list
->strdup_strings
)
270 free(list
->items
[i
].string
);
272 free(list
->items
[i
].util
);
273 list
->items
[i
] = list
->items
[list
->nr
-1];
277 int string_list_split(struct string_list
*list
, const char *string
,
278 int delim
, int maxsplit
)
281 const char *p
= string
, *end
;
283 if (!list
->strdup_strings
)
284 die("internal error in string_list_split(): "
285 "list->strdup_strings must be set");
288 if (maxsplit
>= 0 && count
> maxsplit
) {
289 string_list_append(list
, p
);
292 end
= strchr(p
, delim
);
294 string_list_append_nodup(list
, xmemdupz(p
, end
- p
));
297 string_list_append(list
, p
);
303 int string_list_split_in_place(struct string_list
*list
, char *string
,
304 int delim
, int maxsplit
)
307 char *p
= string
, *end
;
309 if (list
->strdup_strings
)
310 die("internal error in string_list_split_in_place(): "
311 "list->strdup_strings must not be set");
314 if (maxsplit
>= 0 && count
> maxsplit
) {
315 string_list_append(list
, p
);
318 end
= strchr(p
, delim
);
321 string_list_append(list
, p
);
324 string_list_append(list
, p
);