git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Antoine Pelisse <apelisse@gmail.com>
Subject: Re: [PATCH v2 01/10] string-list: allow case-insensitive string list
Date: Thu, 10 Jan 2013 22:35:05 +0100	[thread overview]
Message-ID: <50EF3409.6050805@lsrfire.ath.cx> (raw)
In-Reply-To: <1357603821-8647-2-git-send-email-gitster@pobox.com>

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@pobox.com>

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

  reply	other threads:[~2013-01-10 21:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-08  0:10 [PATCH v2 00/10] reroll of ap/log-mailmap Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 01/10] string-list: allow case-insensitive string list Junio C Hamano
2013-01-10 21:35   ` René Scharfe [this message]
2013-01-08  0:10 ` [PATCH v2 02/10] Use split_ident_line to parse author and committer Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 03/10] mailmap: remove email copy and length limitation Junio C Hamano
2013-01-09 17:35   ` Antoine Pelisse
2013-01-09 17:46     ` Junio C Hamano
2013-01-09 17:56       ` Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 04/10] mailmap: simplify map_user() interface Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 05/10] mailmap: add mailmap structure to rev_info and pp Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 06/10] pretty: use mailmap to display username and email Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 07/10] log: add --use-mailmap option Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 08/10] test: add test for " Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 09/10] log: grep author/committer using mailmap Junio C Hamano
2013-01-08  0:10 ` [PATCH v2 10/10] log: add log.mailmap configuration option Junio C Hamano
2013-01-08  7:27 ` [PATCH v2 00/10] reroll of ap/log-mailmap Antoine Pelisse
2013-01-08  7:39   ` Junio C Hamano
2013-01-08  8:02     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50EF3409.6050805@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=apelisse@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).