Skip to content

Commit

Permalink
string-list.[ch]: add a string_list_init_{nodup,dup}()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
avar authored and gitster committed Jul 1, 2021
1 parent ce93a4c commit 770feda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 16 additions & 2 deletions string-list.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#include "cache.h"
#include "string-list.h"

void string_list_init_nodup(struct string_list *list)
{
struct string_list blank = STRING_LIST_INIT_NODUP;
memcpy(list, &blank, sizeof(*list));
}

void string_list_init_dup(struct string_list *list)
{
struct string_list blank = STRING_LIST_INIT_DUP;
memcpy(list, &blank, sizeof(*list));
}

void string_list_init(struct string_list *list, int strdup_strings)
{
memset(list, 0, sizeof(*list));
list->strdup_strings = strdup_strings;
if (strdup_strings)
string_list_init_dup(list);
else
string_list_init_nodup(list);
}

/* if there is no exact match, point to the index where the entry could be
Expand Down
11 changes: 9 additions & 2 deletions string-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ struct string_list {
/* General functions which work with both sorted and unsorted lists. */

/**
* Initialize the members of the string_list, set `strdup_strings`
* member according to the value of the second parameter.
* Initialize the members of a string_list pointer in the same way as
* the corresponding `STRING_LIST_INIT_NODUP` and
* `STRING_LIST_INIT_DUP` macros.
*/
void string_list_init_nodup(struct string_list *list);
void string_list_init_dup(struct string_list *list);

/**
* TODO remove: For compatibility with any in-flight older API users
*/
void string_list_init(struct string_list *list, int strdup_strings);

Expand Down

0 comments on commit 770feda

Please sign in to comment.