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

[PATCH 5/7] string-list: add unsorted_string_list_lookup()

From
Stephen Boyd <bebarino@gmail.com>
Date
Mar 24, 2010, 07:16 UTC
Message-ID
<1269414964-9518-6-git-send-email-bebarino@gmail.com>
In-Reply-To
<1269414964-9518-1-git-send-email-bebarino@gmail.com>

Sometimes users need to lookup a string in an unsorted string_list. In that case they should use this function instead of the version for sorted strings.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
 Documentation/technical/api-string-list.txt |    6 +++++-
 string-list.c                               |   13 ++++++++++---
 string-list.h                               |    3 ++-
 3 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/Documentation/technical/api-string-list.txt b/Documentation/technical/api-string-list.txt
index 293bb15..6d8c24b 100644
--- a/Documentation/technical/api-string-list.txt
+++ b/Documentation/technical/api-string-list.txt
@@ -104,8 +104,12 @@ write `string_list_insert(...)->util = ...;`.
 `unsorted_string_list_has_string`::
 
 	It's like `string_list_has_string()` but for unsorted lists.
+
+`unsorted_string_list_lookup`::
+
+	It's like `string_list_lookup()` but for unsorted lists.
 +
-This function needs to look through all items, as opposed to its
+The above two functions need to look through all items, as opposed to their
 counterpart for sorted lists, which performs a binary search.
 
 Data structures
diff --git a/string-list.c b/string-list.c
index 1ac536e..c9ad7fc 100644
--- a/string-list.c
+++ b/string-list.c
@@ -168,12 +168,19 @@ void sort_string_list(struct string_list *list)
 	qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
 }
 
-int unsorted_string_list_has_string(struct string_list *list, const char *string)
+struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
+						     const char *string)
 {
 	int i;
 	for (i = 0; i < list->nr; i++)
 		if (!strcmp(string, list->items[i].string))
-			return 1;
-	return 0;
+			return list->items + i;
+	return NULL;
+}
+
+int unsorted_string_list_has_string(struct string_list *list,
+				    const char *string)
+{
+	return unsorted_string_list_lookup(list, string) != NULL;
 }
 
diff --git a/string-list.h b/string-list.h
index 6569cf6..63b69c8 100644
--- a/string-list.h
+++ b/string-list.h
@@ -38,5 +38,6 @@ struct string_list_item *string_list_lookup(const char *string, struct string_li
 struct string_list_item *string_list_append(const char *string, struct string_list *list);
 void sort_string_list(struct string_list *list);
 int unsorted_string_list_has_string(struct string_list *list, const char *string);
-
+struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
+						     const char *string);
 #endif /* STRING_LIST_H */
-- 
1.7.0.3.254.g4503b
Previous: Stephen BoydNext: Stephen Boyd
Message 8 of 17 in “fmt-merge-msg improvements”
  1. 0/7 fmt-merge-msg improvementsStephen Boyd, Mar 24, 2010
  2. 1/7 fmt-merge-msg: be quiet if nothing to mergeStephen Boyd, Mar 24, 2010
  3. Junio C HamanoMar 25, 2010
  4. Stephen BoydMar 25, 2010
  5. 2/7 t6200: modernize with test_tickStephen Boyd, Mar 24, 2010
  6. 3/7 t6200: test fmt-merge-msg moreStephen Boyd, Mar 24, 2010
  7. 4/7 fmt-merge-msg: use pretty.c routinesStephen Boyd, Mar 24, 2010
  8. 5/7 string-list: add unsorted_string_list_lookup()Stephen Boyd, Mar 24, 2010
  9. 6/7 fmt-merge-msg: remove custom string_list implementationStephen Boyd, Mar 24, 2010
  10. Junio C HamanoMar 30, 2010
  11. Stephen BoydApr 1, 2010
  12. 8/7 Make string_list_append() consistent with everything elseStephen Boyd, Apr 1, 2010
  13. Julian PhillipsApr 1, 2010
  14. 7/7 fmt-merge-msg: hide summary optionStephen Boyd, Mar 24, 2010
  15. Junio C HamanoMar 25, 2010
  16. Stephen BoydMar 25, 2010
  17. Junio C HamanoMar 26, 2010

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.