1
0
mirror of https://github.com/git/git.git synced 2024-11-18 21:23:51 +01:00

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>
This commit is contained in:
Marius Storm-Olsen 2009-02-08 15:34:28 +01:00 committed by Junio C Hamano
parent d551a48816
commit cfa1ee6b34
2 changed files with 48 additions and 4 deletions

@ -26,10 +26,10 @@ static int get_entry_index(const struct string_list *list, const char *string,
}
/* returns -1-index if already exists */
static int add_entry(struct string_list *list, const char *string)
static int add_entry(int insert_at, struct string_list *list, const char *string)
{
int exact_match;
int index = get_entry_index(list, string, &exact_match);
int exact_match = 0;
int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
if (exact_match)
return -1 - index;
@ -53,7 +53,13 @@ static int add_entry(struct string_list *list, const char *string)
struct string_list_item *string_list_insert(const char *string, struct string_list *list)
{
int index = add_entry(list, string);
return string_list_insert_at_index(-1, string, list);
}
struct string_list_item *string_list_insert_at_index(int insert_at,
const char *string, struct string_list *list)
{
int index = add_entry(insert_at, list, string);
if (index < 0)
index = -1 - index;
@ -68,6 +74,16 @@ int string_list_has_string(const struct string_list *list, const char *string)
return exact_match;
}
int string_list_find_insert_index(const struct string_list *list, const char *string,
int negative_existing_index)
{
int exact_match;
int index = get_entry_index(list, string, &exact_match);
if (exact_match)
index = -1 - (negative_existing_index ? index : 0);
return index;
}
struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
{
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)
list->nr = list->alloc = 0;
}
void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
{
if (list->items) {
int i;
if (clearfunc) {
for (i = 0; i < list->nr; i++)
clearfunc(list->items[i].util, list->items[i].string);
}
if (list->strdup_strings) {
for (i = 0; i < list->nr; i++)
free(list->items[i].string);
}
free(list->items);
}
list->items = NULL;
list->nr = list->alloc = 0;
}
void print_string_list(const char *text, const struct string_list *p)
{
int i;

@ -15,9 +15,18 @@ struct string_list
void print_string_list(const char *text, const struct string_list *p);
void string_list_clear(struct string_list *list, int free_util);
/* Use this function to call a custom clear function on each util pointer */
/* The string associated with the util pointer is passed as the second argument */
typedef void (*string_list_clear_func_t)(void *p, const char *str);
void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
/* Use these functions only on sorted lists: */
int string_list_has_string(const struct string_list *list, const char *string);
int string_list_find_insert_index(const struct string_list *list, const char *string,
int negative_existing_index);
struct string_list_item *string_list_insert(const char *string, struct string_list *list);
struct string_list_item *string_list_insert_at_index(int insert_at,
const char *string, struct string_list *list);
struct string_list_item *string_list_lookup(const char *string, struct string_list *list);
/* Use these functions only on unsorted lists: */