Skip to content

Commit 03ef776

Browse files
shejialuogitster
authored andcommitted
string-list: use bool instead of int for "exact_match"
The "exact_match" parameter in "get_entry_index" is used to indicate whether a string is found or not, which is fundamentally a true/false value. As we allow the use of bool, let's use bool instead of int to make the function more semantically clear. Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2462961 commit 03ef776

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

string-list.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void string_list_init_dup(struct string_list *list)
1616
/* if there is no exact match, point to the index where the entry could be
1717
* inserted */
1818
static size_t get_entry_index(const struct string_list *list, const char *string,
19-
int *exact_match)
19+
bool *exact_match)
2020
{
2121
size_t left = 0, right = list->nr;
2222
compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
@@ -29,18 +29,18 @@ static size_t get_entry_index(const struct string_list *list, const char *string
2929
else if (compare > 0)
3030
left = middle + 1;
3131
else {
32-
*exact_match = 1;
32+
*exact_match = true;
3333
return middle;
3434
}
3535
}
3636

37-
*exact_match = 0;
37+
*exact_match = false;
3838
return right;
3939
}
4040

4141
static size_t add_entry(struct string_list *list, const char *string)
4242
{
43-
int exact_match = 0;
43+
bool exact_match;
4444
size_t index = get_entry_index(list, string, &exact_match);
4545

4646
if (exact_match)
@@ -68,7 +68,7 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
6868
void string_list_remove(struct string_list *list, const char *string,
6969
int free_util)
7070
{
71-
int exact_match;
71+
bool exact_match;
7272
int i = get_entry_index(list, string, &exact_match);
7373

7474
if (exact_match) {
@@ -82,17 +82,17 @@ void string_list_remove(struct string_list *list, const char *string,
8282
}
8383
}
8484

85-
int string_list_has_string(const struct string_list *list, const char *string)
85+
bool string_list_has_string(const struct string_list *list, const char *string)
8686
{
87-
int exact_match;
87+
bool exact_match;
8888
get_entry_index(list, string, &exact_match);
8989
return exact_match;
9090
}
9191

9292
int string_list_find_insert_index(const struct string_list *list, const char *string,
9393
int negative_existing_index)
9494
{
95-
int exact_match;
95+
bool exact_match;
9696
int index = get_entry_index(list, string, &exact_match);
9797
if (exact_match)
9898
index = -1 - (negative_existing_index ? index : 0);
@@ -101,7 +101,8 @@ int string_list_find_insert_index(const struct string_list *list, const char *st
101101

102102
struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
103103
{
104-
int exact_match, i = get_entry_index(list, string, &exact_match);
104+
bool exact_match;
105+
size_t i = get_entry_index(list, string, &exact_match);
105106
if (!exact_match)
106107
return NULL;
107108
return list->items + i;

string-list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void string_list_remove_empty_items(struct string_list *list, int free_util);
172172
/* Use these functions only on sorted lists: */
173173

174174
/** Determine if the string_list has a given string or not. */
175-
int string_list_has_string(const struct string_list *list, const char *string);
175+
bool string_list_has_string(const struct string_list *list, const char *string);
176176
int string_list_find_insert_index(const struct string_list *list, const char *string,
177177
int negative_existing_index);
178178

0 commit comments

Comments
 (0)