From fc66505c53f1a471e63b6122b2d629daca6e705b Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Mon, 24 Nov 2014 13:22:02 -0800 Subject: string_list: document string_list_(insert,lookup) Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano diff --git a/string-list.h b/string-list.h index 494eb5d..40ffe0c 100644 --- a/string-list.h +++ b/string-list.h @@ -55,9 +55,19 @@ void string_list_remove_empty_items(struct string_list *list, int free_util); 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); +/* + * Inserts the given string into the sorted list. + * If the string already exists, the list is not altered. + * Returns the string_list_item, the string is part of. + */ struct string_list_item *string_list_insert(struct string_list *list, const char *string); struct string_list_item *string_list_insert_at_index(struct string_list *list, int insert_at, const char *string); + +/* + * Checks if the given string is part of a sorted list. If it is part of the list, + * return the coresponding string_list_item, NULL otherwise. + */ struct string_list_item *string_list_lookup(struct string_list *list, const char *string); /* -- cgit v0.10.2-6-g49f6 From 63226218ba39e971c8a75d9419a7668b075ad85e Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Mon, 24 Nov 2014 19:44:14 -0800 Subject: mailmap: use higher level string list functions No functional changes intended. This commit makes use of higher level and better documented functions of the string list API, so the code is more understandable. Note that also the required computational amount should not change in principal as we need to look up the item no matter if it is already part of the list or not. Once looked up, insertion comes for free. Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano diff --git a/mailmap.c b/mailmap.c index 81890a6..9e95897 100644 --- a/mailmap.c +++ b/mailmap.c @@ -71,22 +71,17 @@ static void add_mapping(struct string_list *map, char *old_name, char *old_email) { struct mailmap_entry *me; - int index; + struct string_list_item *item; if (old_email == NULL) { old_email = new_email; new_email = NULL; } - if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) { - /* mailmap entry exists, invert index value */ - index = -1 - index; - me = (struct mailmap_entry *)map->items[index].util; + item = string_list_insert(map, old_email); + if (item->util) { + me = (struct mailmap_entry *)item->util; } else { - /* create mailmap entry */ - struct string_list_item *item; - - item = string_list_insert_at_index(map, index, old_email); me = xcalloc(1, sizeof(struct mailmap_entry)); me->namemap.strdup_strings = 1; me->namemap.cmp = namemap_cmp; @@ -94,8 +89,8 @@ static void add_mapping(struct string_list *map, } if (old_name == NULL) { - debug_mm("mailmap: adding (simple) entry for %s at index %d\n", - old_email, index); + debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email); + /* Replace current name and new email for simple entry */ if (new_name) { free(me->name); @@ -107,8 +102,7 @@ static void add_mapping(struct string_list *map, } } else { struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info)); - debug_mm("mailmap: adding (complex) entry for %s at index %d\n", - old_email, index); + debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email); if (new_name) mi->name = xstrdup(new_name); if (new_email) -- cgit v0.10.2-6-g49f6 From f8c4ab611ac5b714b6b4fd296c59512f97f87195 Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Mon, 24 Nov 2014 13:22:04 -0800 Subject: string_list: remove string_list_insert_at_index() from its API There no longer is a caller to this function. Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano diff --git a/string-list.c b/string-list.c index c5aa076..9584fa6 100644 --- a/string-list.c +++ b/string-list.c @@ -59,13 +59,7 @@ static int add_entry(int insert_at, struct string_list *list, const char *string struct string_list_item *string_list_insert(struct string_list *list, const char *string) { - return string_list_insert_at_index(list, -1, string); -} - -struct string_list_item *string_list_insert_at_index(struct string_list *list, - int insert_at, const char *string) -{ - int index = add_entry(insert_at, list, string); + int index = add_entry(-1, list, string); if (index < 0) index = -1 - index; diff --git a/string-list.h b/string-list.h index 40ffe0c..ee9b100 100644 --- a/string-list.h +++ b/string-list.h @@ -61,8 +61,6 @@ int string_list_find_insert_index(const struct string_list *list, const char *st * Returns the string_list_item, the string is part of. */ struct string_list_item *string_list_insert(struct string_list *list, const char *string); -struct string_list_item *string_list_insert_at_index(struct string_list *list, - int insert_at, const char *string); /* * Checks if the given string is part of a sorted list. If it is part of the list, -- cgit v0.10.2-6-g49f6