string_list: add a function string_list_remove_empty_items()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Acked-by: Johan Herland <johan@herland.net> Signed-off-by: Jeff King <peff@peff.net>

Michael Haggerty committed Nov 4, 2012 at 08:07 UTC 6bb2a1377b4b7a044f69d0107c38479180694fac
3 files changed +24 -1
Documentation/technical/api-string-list.txt
+8 -1
@@ -38,7 +38,8 @@ member (you need this if you add things later) and you should set the
38 `unsorted_string_list_delete_item`.
39
40 . Can remove items not matching a criterion from a sorted or unsorted
41 - list using `filter_string_list`.
41 + list using `filter_string_list`, or remove empty strings using
42 + `string_list_remove_empty_items`.
43
44 . Finally it should free the list using `string_list_clear`.
45
@@ -75,6 +76,12 @@ Functions
76 to be deleted. Preserve the order of the items that are
77 retained.
78
79 +`string_list_remove_empty_items`::
80 +
81 + Remove any empty strings from the list. If free_util is true,
82 + call free() on the util members of any items that have to be
83 + deleted. Preserve the order of the items that are retained.
84 +
85 `string_list_longest_prefix`::
86
87 Return the longest string within a string_list that is a
string-list.c
+9
@@ -136,6 +136,15 @@ void filter_string_list(struct string_list *list, int free_util,
136 list->nr = dst;
137 }
138
139 +static int item_is_not_empty(struct string_list_item *item, void *unused)
140 +{
141 + return *item->string != '\0';
142 +}
143 +
144 +void string_list_remove_empty_items(struct string_list *list, int free_util) {
145 + filter_string_list(list, free_util, item_is_not_empty, NULL);
146 +}
147 +
148 char *string_list_longest_prefix(const struct string_list *prefixes,
149 const char *string)
150 {
string-list.h
+7
@@ -38,6 +38,13 @@ int for_each_string_list(struct string_list *list,
38 void filter_string_list(struct string_list *list, int free_util,
39 string_list_each_func_t want, void *cb_data);
40
41 +/*
42 + * Remove any empty strings from the list. If free_util is true, call
43 + * free() on the util members of any items that have to be deleted.
44 + * Preserve the order of the items that are retained.
45 + */
46 +void string_list_remove_empty_items(struct string_list *list, int free_util);
47 +
48 /*
49 * Return the longest string in prefixes that is a prefix (in the
50 * sense of prefixcmp()) of string, or NULL if no such prefix exists.