Git fork

string-list: change "string_list_find_insert_index" return type to "size_t"

As "string_list_find_insert_index" is a simple wrapper of
"get_entry_index" and the return type of "get_entry_index" is already
"size_t", we could simply change its return type to "size_t".

Update all callers to use size_t variables for storing the return value.
The tricky fix is the loop condition in "mailmap.c" to properly handle
"size_t" underflow by changing from `0 <= --i` to `i--`.

Remove "DISABLE_SIGN_COMPARE_WARNINGS" from "mailmap.c" as it's no
longer needed with the proper unsigned types.

Signed-off-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

shejialuo and committed by
Junio C Hamano
51c3385e e8a32e76

+14 -11
+1 -1
add-interactive.c
··· 222 static ssize_t find_unique(const char *string, struct prefix_item_list *list) 223 { 224 bool exact_match; 225 - int index = string_list_find_insert_index(&list->sorted, string, &exact_match); 226 struct string_list_item *item; 227 228 if (list->items.nr != list->sorted.nr)
··· 222 static ssize_t find_unique(const char *string, struct prefix_item_list *list) 223 { 224 bool exact_match; 225 + size_t index = string_list_find_insert_index(&list->sorted, string, &exact_match); 226 struct string_list_item *item; 227 228 if (list->items.nr != list->sorted.nr)
+2 -3
mailmap.c
··· 1 #define USE_THE_REPOSITORY_VARIABLE 2 - #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "environment.h" ··· 244 const char *string, size_t len) 245 { 246 bool exact_match; 247 - int i = string_list_find_insert_index(map, string, &exact_match); 248 if (exact_match) { 249 if (!string[len]) 250 return &map->items[i]; ··· 266 * overlong key would be inserted, which must come after the 267 * real location of the key if one exists. 268 */ 269 - while (0 <= --i && i < map->nr) { 270 int cmp = strncasecmp(map->items[i].string, string, len); 271 if (cmp < 0) 272 /*
··· 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "environment.h" ··· 243 const char *string, size_t len) 244 { 245 bool exact_match; 246 + size_t i = string_list_find_insert_index(map, string, &exact_match); 247 if (exact_match) { 248 if (!string[len]) 249 return &map->items[i]; ··· 265 * overlong key would be inserted, which must come after the 266 * real location of the key if one exists. 267 */ 268 + while (i-- && i < map->nr) { 269 int cmp = strncasecmp(map->items[i].string, string, len); 270 if (cmp < 0) 271 /*
+1 -3
refs.c
··· 1688 const struct string_list *extras, 1689 const struct string_list *skip) 1690 { 1691 - int pos; 1692 - 1693 if (!extras) 1694 return NULL; 1695 ··· 1699 * with dirname (remember, dirname includes the trailing 1700 * slash) and is not in skip, then we have a conflict. 1701 */ 1702 - for (pos = string_list_find_insert_index(extras, dirname, NULL); 1703 pos < extras->nr; pos++) { 1704 const char *extra_refname = extras->items[pos].string; 1705
··· 1688 const struct string_list *extras, 1689 const struct string_list *skip) 1690 { 1691 if (!extras) 1692 return NULL; 1693 ··· 1697 * with dirname (remember, dirname includes the trailing 1698 * slash) and is not in skip, then we have a conflict. 1699 */ 1700 + for (size_t pos = string_list_find_insert_index(extras, dirname, NULL); 1701 pos < extras->nr; pos++) { 1702 const char *extra_refname = extras->items[pos].string; 1703
+2 -2
string-list.c
··· 91 return exact_match; 92 } 93 94 - int string_list_find_insert_index(const struct string_list *list, const char *string, 95 - bool *exact_match) 96 { 97 return get_entry_index(list, string, exact_match); 98 }
··· 91 return exact_match; 92 } 93 94 + size_t string_list_find_insert_index(const struct string_list *list, const char *string, 95 + bool *exact_match) 96 { 97 return get_entry_index(list, string, exact_match); 98 }
+8 -2
string-list.h
··· 173 174 /** Determine if the string_list has a given string or not. */ 175 bool string_list_has_string(const struct string_list *list, const char *string); 176 - int string_list_find_insert_index(const struct string_list *list, const char *string, 177 - bool *exact_match); 178 179 /** 180 * Insert a new element to the string_list. The returned pointer can
··· 173 174 /** Determine if the string_list has a given string or not. */ 175 bool string_list_has_string(const struct string_list *list, const char *string); 176 + 177 + /** 178 + * Find the index at which a new element should be inserted into the 179 + * string_list to maintain sorted order. If exact_match is not NULL, 180 + * it will be set to true if the string already exists in the list. 181 + */ 182 + size_t string_list_find_insert_index(const struct string_list *list, const char *string, 183 + bool *exact_match); 184 185 /** 186 * Insert a new element to the string_list. The returned pointer can