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

[PATCH 01/10] list_lookup: create case and length search

From
Antoine Pelisse <apelisse@gmail.com>
Date
Jan 5, 2013, 21:26 UTC
Message-ID
<1357421206-5014-2-git-send-email-apelisse@gmail.com>
In-Reply-To
<1357421206-5014-1-git-send-email-apelisse@gmail.com>
Create a new function to look-up a string in a string_list, but:
 - add a new parameter to ignore case differences
 - add a length parameter to search for a substring

The idea is to avoid several copies (lowering a string before searching it when we just want to ignore case), or copying a substring of a bigger string to search it in the string_list

Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
---
 string-list.c | 30 ++++++++++++++++++++++++------
 string-list.h |  2 ++
 2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/string-list.c b/string-list.c
index 397e6cf..f06e110 100644
--- a/string-list.c
+++ b/string-list.c
@@ -4,13 +4,21 @@
 /* if there is no exact match, point to the index where the entry could be
  * inserted */
 static int get_entry_index(const struct string_list *list, const char *string,
-		int *exact_match)
+		int *exact_match, int case_sensitive, size_t n)
 {
 	int left = -1, right = list->nr;
 
 	while (left + 1 < right) {
+		int compare;
 		int middle = (left + right) / 2;
-		int compare = strcmp(string, list->items[middle].string);
+		if (case_sensitive)
+			compare = strncmp(string, list->items[middle].string, n);
+		else
+			compare = strncasecmp(string, list->items[middle].string, n);
+		/* Make sure our string is not a substring of item string */
+		if (!compare && n != -1)
+			if (list->items[middle].string[n] != '\0')
+				compare = -1;
 		if (compare < 0)
 			right = middle;
 		else if (compare > 0)
@@ -29,7 +37,7 @@ static int get_entry_index(const struct string_list *list, const char *string,
 static int add_entry(int insert_at, struct string_list *list, const char *string)
 {
 	int exact_match = 0;
-	int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
+	int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match, 1, -1);
 
 	if (exact_match)
 		return -1 - index;
@@ -70,7 +78,7 @@ struct string_list_item *string_list_insert_at_index(struct string_list *list,
 int string_list_has_string(const struct string_list *list, const char *string)
 {
 	int exact_match;
-	get_entry_index(list, string, &exact_match);
+	get_entry_index(list, string, &exact_match, 1, -1);
 	return exact_match;
 }
 
@@ -78,7 +86,7 @@ int string_list_find_insert_index(const struct string_list *list, const char *st
 				  int negative_existing_index)
 {
 	int exact_match;
-	int index = get_entry_index(list, string, &exact_match);
+	int index = get_entry_index(list, string, &exact_match, 1, -1);
 	if (exact_match)
 		index = -1 - (negative_existing_index ? index : 0);
 	return index;
@@ -86,7 +94,17 @@ 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);
+	int exact_match, i = get_entry_index(list, string, &exact_match, 1, -1);
+	if (!exact_match)
+		return NULL;
+	return list->items + i;
+}
+
+struct string_list_item *string_list_lookup_extended(struct string_list *list,
+    const char *string, int case_sensitive, size_t n)
+{
+	int exact_match, i = get_entry_index(list, string, &exact_match,
+					     case_sensitive, n);
 	if (!exact_match)
 		return NULL;
 	return list->items + i;
diff --git a/string-list.h b/string-list.h
index c50b0d0..4f5ae19 100644
--- a/string-list.h
+++ b/string-list.h
@@ -62,6 +62,8 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
 struct string_list_item *string_list_insert_at_index(struct string_list *list,
 						     int insert_at, const char *string);
 struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
+struct string_list_item *string_list_lookup_extended(struct string_list *list,
+    const char *string, int case_sensitive, size_t n);
 
 /*
  * Remove all but the first of consecutive entries with the same
-- 
1.7.12.4.3.g2036a08.dirty
Previous: Antoine PelisseNext: Junio C Hamano
Message 2 of 14 in “Log mailmap optimization”
  1. 00/10 Log mailmap optimizationAntoine Pelisse, Jan 5, 2013
  2. 01/10 list_lookup: create case and length searchAntoine Pelisse, Jan 5, 2013
  3. Junio C HamanoJan 5, 2013
  4. Junio C HamanoJan 7, 2013
  5. 02/10 Use split_ident_line to parse author and committerAntoine Pelisse, Jan 5, 2013
  6. 03/10 mailmap: remove email copy and length limitationAntoine Pelisse, Jan 5, 2013
  7. 04/10 mailmap: simplify map_user() interfaceAntoine Pelisse, Jan 5, 2013
  8. Junio C HamanoJan 6, 2013
  9. 05/10 mailmap: add mailmap structure to rev_info and ppAntoine Pelisse, Jan 5, 2013
  10. 06/10 pretty: use mailmap to display username and emailAntoine Pelisse, Jan 5, 2013
  11. 07/10 log: add --use-mailmap optionAntoine Pelisse, Jan 5, 2013
  12. 08/10 test: add test for --use-mailmap optionAntoine Pelisse, Jan 5, 2013
  13. 09/10 log: grep author/committer using mailmapAntoine Pelisse, Jan 5, 2013
  14. 10/10 log: add log.mailmap configuration optionAntoine Pelisse, Jan 5, 2013

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.