From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Antoine Pelisse <apelisse@gmail.com>
Subject: [PATCH v2 01/10] string-list: allow case-insensitive string list
Date: Mon, 7 Jan 2013 16:10:12 -0800 [thread overview]
Message-ID: <1357603821-8647-2-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1357603821-8647-1-git-send-email-gitster@pobox.com>
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>
---
string-list.c | 17 +++++++++++++----
string-list.h | 4 ++++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/string-list.c b/string-list.c
index 397e6cf..6f3d8cf 100644
--- a/string-list.c
+++ b/string-list.c
@@ -7,10 +7,11 @@ 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;
while (left + 1 < right) {
int middle = (left + right) / 2;
- int compare = strcmp(string, list->items[middle].string);
+ int compare = cmp(string, list->items[middle].string);
if (compare < 0)
right = middle;
else if (compare > 0)
@@ -96,8 +97,9 @@ 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;
for (src = dst = 1; src < list->nr; src++) {
- if (!strcmp(list->items[dst - 1].string, list->items[src].string)) {
+ if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
if (list->strdup_strings)
free(list->items[src].string);
if (free_util)
@@ -230,15 +232,20 @@ 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;
+
+/* 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 strcmp(one->string, two->string);
+ return compare_for_qsort(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);
}
@@ -246,8 +253,10 @@ 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;
+
for (i = 0; i < list->nr; i++)
- if (!strcmp(string, list->items[i].string))
+ if (!cmp(string, list->items[i].string))
return list->items + i;
return NULL;
}
diff --git a/string-list.h b/string-list.h
index c50b0d0..446e79e 100644
--- a/string-list.h
+++ b/string-list.h
@@ -5,10 +5,14 @@ struct string_list_item {
char *string;
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() */
};
#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
--
1.8.1.304.gf036638
next prev parent reply other threads:[~2013-01-08 0:10 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 ` Junio C Hamano [this message]
2013-01-10 21:35 ` [PATCH v2 01/10] string-list: allow case-insensitive string list René Scharfe
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1357603821-8647-2-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=apelisse@gmail.com \
--cc=git@vger.kernel.org \
/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.
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 NNTP newsgroup(s).