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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 12, 2012 at 16:04 UTC e448fed8e6ba9cd9237b7c8045c11cc40afc8595
3 files changed +47 -8
Documentation/technical/api-string-list.txt
+14 -3
@@ -20,8 +20,8 @@ If you need something advanced, you can manually malloc() the `items`
20 member (you need this if you add things later) and you should set the
21 `nr` and `alloc` members in that case, too.
22
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`.
25
26 . Can check if a string is in the list using `string_list_has_string` or
27 `unsorted_string_list_has_string` and get it from the list using
@@ -100,7 +100,18 @@ write `string_list_insert(...)->util = ...;`.
100
101 `string_list_append`::
102
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.
115
116 `sort_string_list`::
117
string-list.c
+15 -5
@@ -148,13 +148,23 @@ void print_string_list(const struct string_list *p, const char *text)
148 printf("%s:%p\n", p->items[i].string, p->items[i].util);
149 }
150
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)
153 {
154 + struct string_list_item *retval;
155 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);
168 }
169
170 static int cmp_items(const void *a, const void *b)
string-list.h
+18
@@ -29,6 +29,7 @@ int for_each_string_list(struct string_list *list,
29 #define for_each_string_list_item(item,list) \
30 for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
31
32 +
33 /* Use these functions only on sorted lists: */
34 int string_list_has_string(const struct string_list *list, const char *string);
35 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,
39 int insert_at, const char *string);
40 struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
41
42 +
43 /* 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 + */
50 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 +
60 void sort_string_list(struct string_list *list);
61 int unsorted_string_list_has_string(struct string_list *list, const char *string);
62 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
63 const char *string);
64 +
65 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
66 #endif /* STRING_LIST_H */