git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 1/4] string-list: use bool instead of int for "exact_match"

From
shejialuo <shejialuo@gmail.com>
Date
Oct 6, 2025, 06:32 UTC
Message-ID
<aONid6ZhaRFx2mgP@ArchLinux>
In-Reply-To
<aONhmrE0otiyZ16f@ArchLinux>

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>
---
 string-list.c | 19 ++++++++++---------
 string-list.h |  2 +-
 2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/string-list.c b/string-list.c
index 343cf1ca90..d8da3dd414 100644
--- a/string-list.c
+++ b/string-list.c
@@ -16,7 +16,7 @@ void string_list_init_dup(struct string_list *list)
 /* if there is no exact match, point to the index where the entry could be
  * inserted */
 static size_t get_entry_index(const struct string_list *list, const char *string,
-			      int *exact_match)
+			      bool *exact_match)
 {
 	size_t left = 0, right = list->nr;
 	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
 		else if (compare > 0)
 			left = middle + 1;
 		else {
-			*exact_match = 1;
+			*exact_match = true;
 			return middle;
 		}
 	}
 
-	*exact_match = 0;
+	*exact_match = false;
 	return right;
 }
 
 static size_t add_entry(struct string_list *list, const char *string)
 {
-	int exact_match = 0;
+	bool exact_match;
 	size_t index = get_entry_index(list, string, &exact_match);
 
 	if (exact_match)
@@ -68,7 +68,7 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
 void string_list_remove(struct string_list *list, const char *string,
 			int free_util)
 {
-	int exact_match;
+	bool exact_match;
 	int i = get_entry_index(list, string, &exact_match);
 
 	if (exact_match) {
@@ -82,9 +82,9 @@ void string_list_remove(struct string_list *list, const char *string,
 	}
 }
 
-int string_list_has_string(const struct string_list *list, const char *string)
+bool string_list_has_string(const struct string_list *list, const char *string)
 {
-	int exact_match;
+	bool exact_match;
 	get_entry_index(list, string, &exact_match);
 	return exact_match;
 }
@@ -92,7 +92,7 @@ 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)
 {
-	int exact_match;
+	bool exact_match;
 	int index = get_entry_index(list, string, &exact_match);
 	if (exact_match)
 		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
 
 struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
 {
-	int exact_match, i = get_entry_index(list, string, &exact_match);
+	bool exact_match;
+	size_t i = get_entry_index(list, string, &exact_match);
 	if (!exact_match)
 		return NULL;
 	return list->items + i;
diff --git a/string-list.h b/string-list.h
index 2b438c7733..bc7f38022e 100644
--- a/string-list.h
+++ b/string-list.h
@@ -172,7 +172,7 @@ void string_list_remove_empty_items(struct string_list *list, int free_util);
 /* Use these functions only on sorted lists: */
 
 /** Determine if the string_list has a given string or not. */
-int string_list_has_string(const struct string_list *list, const char *string);
+bool 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);
 
-- 
2.51.0
Previous: shejialuoNext: shejialuo
Message 35 of 43 in “enhance string-list API to fix sign compare warnings”
  1. 0/4 enhance string-list API to fix sign compare warningsshejialuo, Sep 7, 2025
  2. 1/4 string-list: allow passing NULL for `get_entry_index`shejialuo, Sep 7, 2025
  3. Patrick SteinhardtSep 9, 2025
  4. 2/4 string-list: replace negative index encoding with "exact_match" parametershejialuo, Sep 7, 2025
  5. Patrick SteinhardtSep 9, 2025
  6. shejialuoSep 15, 2025
  7. 3/4 string-list: change "string_list_find_insert_index" return type to "size_t"shejialuo, Sep 7, 2025
  8. Patrick SteinhardtSep 9, 2025
  9. Junio C HamanoSep 9, 2025
  10. Patrick SteinhardtSep 10, 2025
  11. 4/4 refs: enable sign compare warnings checkshejialuo, Sep 7, 2025
  12. Patrick SteinhardtSep 9, 2025
  13. shejialuoSep 7, 2025
  14. 0/4 enhance string-list API to fix sign compare warningsshejialuo, Sep 17, 2025
  15. 1/4 string-list: use bool instead of int for "exact_match"shejialuo, Sep 17, 2025
  16. 2/4 string-list: replace negative index encoding with "exact_match" parametershejialuo, Sep 17, 2025
  17. Patrick SteinhardtSep 23, 2025
  18. shejialuoOct 5, 2025
  19. Karthik NayakSep 23, 2025
  20. Junio C HamanoSep 23, 2025
  21. Jeff KingSep 24, 2025
  22. Junio C HamanoSep 24, 2025
  23. Jeff KingSep 25, 2025
  24. Junio C HamanoSep 25, 2025
  25. Jeff KingOct 9, 2025
  26. Collin FunkOct 8, 2025
  27. Jeff KingOct 9, 2025
  28. shejialuoOct 5, 2025
  29. shejialuoOct 5, 2025
  30. 3/4 string-list: change "string_list_find_insert_index" return type to "size_t"shejialuo, Sep 17, 2025
  31. Karthik NayakSep 23, 2025
  32. shejialuoOct 5, 2025
  33. 4/4 refs: enable sign compare warnings checkshejialuo, Sep 17, 2025
  34. 0/4 enhance string-list API to fix sign compare warningsshejialuo, Oct 6, 2025
  35. 1/4 string-list: use bool instead of int for "exact_match"shejialuo, Oct 6, 2025
  36. 2/4 string-list: replace negative index encoding with "exact_match" parametershejialuo, Oct 6, 2025
  37. 3/4 string-list: change "string_list_find_insert_index" return type to "size_t"shejialuo, Oct 6, 2025
  38. Jeff KingOct 9, 2025
  39. 4/4 refs: enable sign compare warnings checkshejialuo, Oct 6, 2025
  40. Junio C HamanoOct 6, 2025
  41. Collin FunkOct 8, 2025
  42. Junio C HamanoOct 8, 2025
  43. Karthik NayakOct 8, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.