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>
shejialuo committed
Oct 6, 2025 at 14:32 UTC
03ef7762ea12f3b034a2281040bd61c74fd36386
2 files changed
+11
-10
string-list.c
+10
-9
@@ -16,7 +16,7 @@ void string_list_init_dup(struct string_list *list)
16
/* if there is no exact match, point to the index where the entry could be
17
* inserted */
18
static size_t get_entry_index(const struct string_list *list, const char *string,
19
- int *exact_match)
19
+ bool *exact_match)
20
{
21
size_t left = 0, right = list->nr;
22
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
29
else if (compare > 0)
30
left = middle + 1;
31
else {
32
- *exact_match = 1;
32
+ *exact_match = true;
33
return middle;
34
}
35
}
36
37
- *exact_match = 0;
37
+ *exact_match = false;
38
return right;
39
}
40
41
static size_t add_entry(struct string_list *list, const char *string)
42
{
43
- int exact_match = 0;
43
+ bool exact_match;
44
size_t index = get_entry_index(list, string, &exact_match);
45
46
if (exact_match)
@@ -68,7 +68,7 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
68
void string_list_remove(struct string_list *list, const char *string,
69
int free_util)
70
{
71
- int exact_match;
71
+ bool exact_match;
72
int i = get_entry_index(list, string, &exact_match);
73
74
if (exact_match) {
@@ -82,9 +82,9 @@ void string_list_remove(struct string_list *list, const char *string,
82
}
83
}
84
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)
86
{
87
- int exact_match;
87
+ bool exact_match;
88
get_entry_index(list, string, &exact_match);
89
return exact_match;
90
}
@@ -92,7 +92,7 @@ int string_list_has_string(const struct string_list *list, const char *string)
92
int string_list_find_insert_index(const struct string_list *list, const char *string,
93
int negative_existing_index)
94
{
95
- int exact_match;
95
+ bool exact_match;
96
int index = get_entry_index(list, string, &exact_match);
97
if (exact_match)
98
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
101
102
struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
103
{
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);
106
if (!exact_match)
107
return NULL;
108
return list->items + i;
string-list.h
+1
-1
@@ -172,7 +172,7 @@ void string_list_remove_empty_items(struct string_list *list, int free_util);
172
/* Use these functions only on sorted lists: */
173
174
/** 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);
176
int string_list_find_insert_index(const struct string_list *list, const char *string,
177
int negative_existing_index);
178