Skip to content

Commit cfa1ee6

Browse files
Marius Storm-Olsengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d551a48 commit cfa1ee6

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

string-list.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ static int get_entry_index(const struct string_list *list, const char *string,
2626
}
2727

2828
/* 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)
3030
{
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);
3333

3434
if (exact_match)
3535
return -1 - index;
@@ -53,7 +53,13 @@ static int add_entry(struct string_list *list, const char *string)
5353

5454
struct string_list_item *string_list_insert(const char *string, struct string_list *list)
5555
{
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);
5763

5864
if (index < 0)
5965
index = -1 - index;
@@ -68,6 +74,16 @@ int string_list_has_string(const struct string_list *list, const char *string)
6874
return exact_match;
6975
}
7076

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+
7187
struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
7288
{
7389
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)
94110
list->nr = list->alloc = 0;
95111
}
96112

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+
97132
void print_string_list(const char *text, const struct string_list *p)
98133
{
99134
int i;

string-list.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ struct string_list
1515
void print_string_list(const char *text, const struct string_list *p);
1616
void string_list_clear(struct string_list *list, int free_util);
1717

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+
1823
/* Use these functions only on sorted lists: */
1924
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);
2027
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);
2130
struct string_list_item *string_list_lookup(const char *string, struct string_list *list);
2231

2332
/* Use these functions only on unsorted lists: */

0 commit comments

Comments
 (0)