Skip to content

Commit e448fed

Browse files
mhaggergitster
authored andcommitted
string_list: add function string_list_append_nodup()
Add a new function that appends a string to a string_list without copying it. This can be used to pass ownership of an already-copied string to a string_list that has strdup_strings set. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ce9864 commit e448fed

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

Documentation/technical/api-string-list.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ If you need something advanced, you can manually malloc() the `items`
2020
member (you need this if you add things later) and you should set the
2121
`nr` and `alloc` members in that case, too.
2222

23-
. Adds new items to the list, using `string_list_append` or
24-
`string_list_insert`.
23+
. Adds new items to the list, using `string_list_append`,
24+
`string_list_append_nodup`, or `string_list_insert`.
2525

2626
. Can check if a string is in the list using `string_list_has_string` or
2727
`unsorted_string_list_has_string` and get it from the list using
@@ -100,7 +100,18 @@ write `string_list_insert(...)->util = ...;`.
100100

101101
`string_list_append`::
102102

103-
Append a new string to the end of the string_list.
103+
Append a new string to the end of the string_list. If
104+
`strdup_string` is set, then the string argument is copied;
105+
otherwise the new `string_list_entry` refers to the input
106+
string.
107+
108+
`string_list_append_nodup`::
109+
110+
Append a new string to the end of the string_list. The new
111+
`string_list_entry` always refers to the input string, even if
112+
`strdup_string` is set. This function can be used to hand
113+
ownership of a malloc()ed string to a `string_list` that has
114+
`strdup_string` set.
104115

105116
`sort_string_list`::
106117

string-list.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,23 @@ void print_string_list(const struct string_list *p, const char *text)
148148
printf("%s:%p\n", p->items[i].string, p->items[i].util);
149149
}
150150

151-
struct string_list_item *string_list_append(struct string_list *list, const char *string)
151+
struct string_list_item *string_list_append_nodup(struct string_list *list,
152+
char *string)
152153
{
154+
struct string_list_item *retval;
153155
ALLOC_GROW(list->items, list->nr + 1, list->alloc);
154-
list->items[list->nr].string =
155-
list->strdup_strings ? xstrdup(string) : (char *)string;
156-
list->items[list->nr].util = NULL;
157-
return list->items + list->nr++;
156+
retval = &list->items[list->nr++];
157+
retval->string = string;
158+
retval->util = NULL;
159+
return retval;
160+
}
161+
162+
struct string_list_item *string_list_append(struct string_list *list,
163+
const char *string)
164+
{
165+
return string_list_append_nodup(
166+
list,
167+
list->strdup_strings ? xstrdup(string) : (char *)string);
158168
}
159169

160170
static int cmp_items(const void *a, const void *b)

string-list.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int for_each_string_list(struct string_list *list,
2929
#define for_each_string_list_item(item,list) \
3030
for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
3131

32+
3233
/* Use these functions only on sorted lists: */
3334
int string_list_has_string(const struct string_list *list, const char *string);
3435
int string_list_find_insert_index(const struct string_list *list, const char *string,
@@ -38,11 +39,28 @@ struct string_list_item *string_list_insert_at_index(struct string_list *list,
3839
int insert_at, const char *string);
3940
struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
4041

42+
4143
/* Use these functions only on unsorted lists: */
44+
45+
/*
46+
* Add string to the end of list. If list->strdup_string is set, then
47+
* string is copied; otherwise the new string_list_entry refers to the
48+
* input string.
49+
*/
4250
struct string_list_item *string_list_append(struct string_list *list, const char *string);
51+
52+
/*
53+
* Like string_list_append(), except string is never copied. When
54+
* list->strdup_strings is set, this function can be used to hand
55+
* ownership of a malloc()ed string to list without making an extra
56+
* copy.
57+
*/
58+
struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
59+
4360
void sort_string_list(struct string_list *list);
4461
int unsorted_string_list_has_string(struct string_list *list, const char *string);
4562
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
4663
const char *string);
64+
4765
void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
4866
#endif /* STRING_LIST_H */

0 commit comments

Comments
 (0)