From eb5f0c7a616531a024a582b72ca6d8775ff98d46 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Wed, 12 Sep 2012 16:04:44 +0200 Subject: string_list: add a new function, filter_string_list() This function allows entries that don't match a specified criterion to be discarded from a string_list while preserving the order of the remaining entries. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- string-list.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'string-list.c') diff --git a/string-list.c b/string-list.c index acb1f5b6dc..179fde4210 100644 --- a/string-list.c +++ b/string-list.c @@ -102,6 +102,23 @@ int for_each_string_list(struct string_list *list, return ret; } +void filter_string_list(struct string_list *list, int free_util, + string_list_each_func_t want, void *cb_data) +{ + int src, dst = 0; + for (src = 0; src < list->nr; src++) { + if (want(&list->items[src], cb_data)) { + list->items[dst++] = list->items[src]; + } else { + if (list->strdup_strings) + free(list->items[src].string); + if (free_util) + free(list->items[src].util); + } + } + list->nr = dst; +} + void string_list_clear(struct string_list *list, int free_util) { if (list->items) { -- cgit v1.2.3