string-list.[ch]: add a string_list_init_{nodup,dup}()

In order to use the new "memcpy() a 'blank' struct on the stack" pattern for string_list_init(), and to make the macro initialization consistent with the function initialization introduce two new string_list_init_{nodup,dup}() functions. These are like the old string_list_init() when called with a false and true second argument, respectively. I think this not only makes things more consistent, but also easier to read. I often had to lookup what the ", 0)" or ", 1)" in these invocations meant, now it's right there in the function name, and corresponds to the macros. A subsequent commit will convert existing API users to this pattern, but as this is a very common API let's leave a compatibility function in place for later removal. This intermediate state also proves that the compatibility function works. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 1, 2021 at 12:51 UTC 770fedaf9fb156bd8c18da41770eac0cb63fba63
2 files changed +25 -4
string-list.c
+16 -2
@@ -1,10 +1,24 @@
1 #include "cache.h"
2 #include "string-list.h"
3
4 +void string_list_init_nodup(struct string_list *list)
5 +{
6 + struct string_list blank = STRING_LIST_INIT_NODUP;
7 + memcpy(list, &blank, sizeof(*list));
8 +}
9 +
10 +void string_list_init_dup(struct string_list *list)
11 +{
12 + struct string_list blank = STRING_LIST_INIT_DUP;
13 + memcpy(list, &blank, sizeof(*list));
14 +}
15 +
16 void string_list_init(struct string_list *list, int strdup_strings)
17 {
6 - memset(list, 0, sizeof(*list));
7 - list->strdup_strings = strdup_strings;
18 + if (strdup_strings)
19 + string_list_init_dup(list);
20 + else
21 + string_list_init_nodup(list);
22 }
23
24 /* if there is no exact match, point to the index where the entry could be
string-list.h
+9 -2
@@ -97,8 +97,15 @@ struct string_list {
97 /* General functions which work with both sorted and unsorted lists. */
98
99 /**
100 - * Initialize the members of the string_list, set `strdup_strings`
101 - * member according to the value of the second parameter.
100 + * Initialize the members of a string_list pointer in the same way as
101 + * the corresponding `STRING_LIST_INIT_NODUP` and
102 + * `STRING_LIST_INIT_DUP` macros.
103 + */
104 +void string_list_init_nodup(struct string_list *list);
105 +void string_list_init_dup(struct string_list *list);
106 +
107 +/**
108 + * TODO remove: For compatibility with any in-flight older API users
109 */
110 void string_list_init(struct string_list *list, int strdup_strings);
111