Re: [PATCH v2 01/10] string-list: allow case-insensitive string list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 08.01.2013 01:10, schrieb Junio C Hamano:
> Some string list needs to be searched case insensitively, and for
> that to work correctly, the string needs to be sorted case
> insensitively from the beginning.
> 
> Allow a custom comparison function to be defined on a string list
> instance and use it throughout in place of strcmp().
> 
> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx>

We can avoid using a static variable to pass the comparison function from
sort_string_list() to cmp_items() by dialling back the flexibility a bit
and only have a flag to switch between strcmp() and strcasecmp().  I bet
this suffices for quite a while yet.  Slightly more code, less yuck,
squashable.

René


---
 mailmap.c     |  2 +-
 string-list.c | 23 ++++++++++++++---------
 string-list.h |  4 +---
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/mailmap.c b/mailmap.c
index 276c54f..08126b1 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -235,7 +235,7 @@ int read_mailmap(struct string_list *map, char **repo_abbrev)
 	int err = 0;
 
 	map->strdup_strings = 1;
-	map->cmp = strcasecmp;
+	map->ignore_case = 1;
 
 	if (!git_mailmap_blob && is_bare_repository())
 		git_mailmap_blob = "HEAD:.mailmap";
diff --git a/string-list.c b/string-list.c
index aabb25e..ddecc23 100644
--- a/string-list.c
+++ b/string-list.c
@@ -1,13 +1,15 @@
 #include "cache.h"
 #include "string-list.h"
 
+typedef int (*compare_strings_fn)(const char *, const char *);
+
 /* 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 left = -1, right = list->nr;
-	compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
+	compare_strings_fn cmp = list->ignore_case ? strcasecmp : strcmp;
 
 	while (left + 1 < right) {
 		int middle = (left + right) / 2;
@@ -97,7 +99,7 @@ void string_list_remove_duplicates(struct string_list *list, int free_util)
 {
 	if (list->nr > 1) {
 		int src, dst;
-		compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
+		compare_strings_fn cmp = list->ignore_case ? strcasecmp : strcmp;
 		for (src = dst = 1; src < list->nr; src++) {
 			if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
 				if (list->strdup_strings)
@@ -212,28 +214,31 @@ struct string_list_item *string_list_append(struct string_list *list,
 			list->strdup_strings ? xstrdup(string) : (char *)string);
 }
 
-/* Yuck */
-static compare_strings_fn compare_for_qsort;
+static int cmp_items_ignore_case(const void *a, const void *b)
+{
+	const struct string_list_item *one = a;
+	const struct string_list_item *two = b;
+	return strcasecmp(one->string, two->string);
+}
 
-/* Only call this from inside sort_string_list! */
 static int cmp_items(const void *a, const void *b)
 {
 	const struct string_list_item *one = a;
 	const struct string_list_item *two = b;
-	return compare_for_qsort(one->string, two->string);
+	return strcmp(one->string, two->string);
 }
 
 void sort_string_list(struct string_list *list)
 {
-	compare_for_qsort = list->cmp ? list->cmp : strcmp;
-	qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
+	qsort(list->items, list->nr, sizeof(*list->items),
+	      list->ignore_case ? cmp_items_ignore_case : cmp_items);
 }
 
 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 						     const char *string)
 {
 	int i;
-	compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
+	compare_strings_fn cmp = list->ignore_case ? strcasecmp : strcmp;
 
 	for (i = 0; i < list->nr; i++)
 		if (!cmp(string, list->items[i].string))
diff --git a/string-list.h b/string-list.h
index de6769c..33f2e9c 100644
--- a/string-list.h
+++ b/string-list.h
@@ -6,13 +6,11 @@ struct string_list_item {
 	void *util;
 };
 
-typedef int (*compare_strings_fn)(const char *, const char *);
-
 struct string_list {
 	struct string_list_item *items;
 	unsigned int nr, alloc;
 	unsigned int strdup_strings:1;
-	compare_strings_fn cmp; /* NULL uses strcmp() */
+	unsigned int ignore_case:1;
 };
 
 #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
-- 
1.8.0


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]