string-list: add string_list initializer helper function

The string-list API has STRING_LIST_INIT_* macros to be used to define variables with initializers, but lacks functions to initialize an uninitialized piece of memory to be used as a string-list at the run-time. Introduce `string_list_init()` function for that. Signed-off-by: Tanay Abhra <tanayabh@gmail.com> Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tanay Abhra committed Jul 18, 2014 at 02:18 UTC 3ed3f5fe85ac3fa7f94ccaab59408a20db8c7a41
3 files changed +13
Documentation/technical/api-string-list.txt
+5
@@ -68,6 +68,11 @@ Functions
68
69 * General ones (works with sorted and unsorted lists as well)
70
71 +`string_list_init`::
72 +
73 + Initialize the members of the string_list, set `strdup_strings`
74 + member according to the value of the second parameter.
75 +
76 `filter_string_list`::
77
78 Apply a function to each item in a list, retaining only the
string-list.c
+6
@@ -1,6 +1,12 @@
1 #include "cache.h"
2 #include "string-list.h"
3
4 +void string_list_init(struct string_list *list, int strdup_strings)
5 +{
6 + memset(list, 0, sizeof(*list));
7 + list->strdup_strings = strdup_strings;
8 +}
9 +
10 /* if there is no exact match, point to the index where the entry could be
11 * inserted */
12 static int get_entry_index(const struct string_list *list, const char *string,
string-list.h
+2
@@ -18,6 +18,8 @@ struct string_list {
18 #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
19 #define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL }
20
21 +void string_list_init(struct string_list *list, int strdup_strings);
22 +
23 void print_string_list(const struct string_list *p, const char *text);
24 void string_list_clear(struct string_list *list, int free_util);
25