string_list: add a new function, filter_string_list()
[gitweb.git] / string-list.c
index acb1f5b6dc3381318048c2734add7d37851c5ec7..179fde4210139eee8c459a17a2c9ee35618b6349 100644 (file)
@@ -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) {