@matheusbsbs / matheus-bagliano / commits / cfa1ee6b34
Fork 0

Add find_insert_index, insert_at_index and clear_func functions to string_list

string_list_find_insert_index() and string_list_insert_at_index() enables you to see if an item is in the string_list, and to insert at the appropriate index in the list, if not there. This is usefull if you need to manipulate an existing item, if present, and insert a new item if not. Future mailmap code will use this construct to enable complex (old_name, old_email) -> (new_name, new_email) lookups. The string_list_clear_func() allows to call a custom cleanup function on each item in a string_list, which is useful is the util member points to a complex structure. Signed-off-by: Marius Storm-Olsen <marius@trolltech.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Marius Storm-Olsen committed Feb 8, 2009 at 15:34 UTC cfa1ee6b340172a415049704cd848593392b9064
2 files changed +48 -4
string-list.c
+39 -4
@@ -26,10 +26,10 @@ static int get_entry_index(const struct string_list *list, const char *string,
26 }
27
28 /* returns -1-index if already exists */
29 -static int add_entry(struct string_list *list, const char *string)
29 +static int add_entry(int insert_at, struct string_list *list, const char *string)
30 {
31 - int exact_match;
32 - int index = get_entry_index(list, string, &exact_match);
31 + int exact_match = 0;
32 + int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
33
34 if (exact_match)
35 return -1 - index;
@@ -53,7 +53,13 @@ static int add_entry(struct string_list *list, const char *string)
53
54 struct string_list_item *string_list_insert(const char *string, struct string_list *list)
55 {
56 - int index = add_entry(list, string);
56 + return string_list_insert_at_index(-1, string, list);
57 +}
58 +
59 +struct string_list_item *string_list_insert_at_index(int insert_at,
60 + const char *string, struct string_list *list)
61 +{
62 + int index = add_entry(insert_at, list, string);
63
64 if (index < 0)
65 index = -1 - index;
@@ -68,6 +74,16 @@ int string_list_has_string(const struct string_list *list, const char *string)
74 return exact_match;
75 }
76
77 +int string_list_find_insert_index(const struct string_list *list, const char *string,
78 + int negative_existing_index)
79 +{
80 + int exact_match;
81 + int index = get_entry_index(list, string, &exact_match);
82 + if (exact_match)
83 + index = -1 - (negative_existing_index ? index : 0);
84 + return index;
85 +}
86 +
87 struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
88 {
89 int exact_match, i = get_entry_index(list, string, &exact_match);
@@ -94,6 +110,25 @@ void string_list_clear(struct string_list *list, int free_util)
110 list->nr = list->alloc = 0;
111 }
112
113 +void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
114 +{
115 + if (list->items) {
116 + int i;
117 + if (clearfunc) {
118 + for (i = 0; i < list->nr; i++)
119 + clearfunc(list->items[i].util, list->items[i].string);
120 + }
121 + if (list->strdup_strings) {
122 + for (i = 0; i < list->nr; i++)
123 + free(list->items[i].string);
124 + }
125 + free(list->items);
126 + }
127 + list->items = NULL;
128 + list->nr = list->alloc = 0;
129 +}
130 +
131 +
132 void print_string_list(const char *text, const struct string_list *p)
133 {
134 int i;
string-list.h
+9
@@ -15,9 +15,18 @@ struct string_list
15 void print_string_list(const char *text, const struct string_list *p);
16 void string_list_clear(struct string_list *list, int free_util);
17
18 +/* Use this function to call a custom clear function on each util pointer */
19 +/* The string associated with the util pointer is passed as the second argument */
20 +typedef void (*string_list_clear_func_t)(void *p, const char *str);
21 +void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
22 +
23 /* Use these functions only on sorted lists: */
24 int string_list_has_string(const struct string_list *list, const char *string);
25 +int string_list_find_insert_index(const struct string_list *list, const char *string,
26 + int negative_existing_index);
27 struct string_list_item *string_list_insert(const char *string, struct string_list *list);
28 +struct string_list_item *string_list_insert_at_index(int insert_at,
29 + const char *string, struct string_list *list);
30 struct string_list_item *string_list_lookup(const char *string, struct string_list *list);
31
32 /* Use these functions only on unsorted lists: */