]> Pileus Git - ~andy/git/commitdiff
string_list: add a function string_list_remove_empty_items()
authorMichael Haggerty <mhagger@alum.mit.edu>
Sun, 4 Nov 2012 07:07:06 +0000 (08:07 +0100)
committerJeff King <peff@peff.net>
Thu, 8 Nov 2012 16:34:08 +0000 (11:34 -0500)
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Acked-by: Johan Herland <johan@herland.net>
Signed-off-by: Jeff King <peff@peff.net>
Documentation/technical/api-string-list.txt
string-list.c
string-list.h

index 94d7a2bd999ce5e8603f6d6639ba34d224c2bee5..7386bcab3ec30a06f4663c953fa46e78ed26c07b 100644 (file)
@@ -38,7 +38,8 @@ member (you need this if you add things later) and you should set the
   `unsorted_string_list_delete_item`.
 
 . Can remove items not matching a criterion from a sorted or unsorted
-  list using `filter_string_list`.
+  list using `filter_string_list`, or remove empty strings using
+  `string_list_remove_empty_items`.
 
 . Finally it should free the list using `string_list_clear`.
 
@@ -75,6 +76,12 @@ Functions
        to be deleted.  Preserve the order of the items that are
        retained.
 
+`string_list_remove_empty_items`::
+
+       Remove any empty strings from the list.  If free_util is true,
+       call free() on the util members of any items that have to be
+       deleted.  Preserve the order of the items that are retained.
+
 `string_list_longest_prefix`::
 
        Return the longest string within a string_list that is a
index c54b816244f69614c67c532d20719081a4b34bd4..397e6cfa7db82bdb17f7cdbe4662a1607c0773af 100644 (file)
@@ -136,6 +136,15 @@ void filter_string_list(struct string_list *list, int free_util,
        list->nr = dst;
 }
 
+static int item_is_not_empty(struct string_list_item *item, void *unused)
+{
+       return *item->string != '\0';
+}
+
+void string_list_remove_empty_items(struct string_list *list, int free_util) {
+       filter_string_list(list, free_util, item_is_not_empty, NULL);
+}
+
 char *string_list_longest_prefix(const struct string_list *prefixes,
                                 const char *string)
 {
index 5efd07b44e020428326ff32ff258449ec347861f..c50b0d0deac086cd5a50a5c021103a5f5e76c1cd 100644 (file)
@@ -38,6 +38,13 @@ int for_each_string_list(struct string_list *list,
 void filter_string_list(struct string_list *list, int free_util,
                        string_list_each_func_t want, void *cb_data);
 
+/*
+ * Remove any empty strings from the list.  If free_util is true, call
+ * free() on the util members of any items that have to be deleted.
+ * Preserve the order of the items that are retained.
+ */
+void string_list_remove_empty_items(struct string_list *list, int free_util);
+
 /*
  * Return the longest string in prefixes that is a prefix (in the
  * sense of prefixcmp()) of string, or NULL if no such prefix exists.