Make hash_name_lookup able to do case-independent lookups
Right now nobody uses it, but "index_name_exists()" gets a flag so you can enable it on a case-by-case basis. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Linus Torvalds committed
Mar 21, 2008 at 15:55 UTC
cd2fef59edf72a1d9792d9cb72aae1e6f6c7b1d4
4 files changed
+52
-6
cache.h
+2
-2
@@ -264,7 +264,7 @@ static inline void remove_name_hash(struct cache_entry *ce)
264
#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL)
265
#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
266
#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
267
-#define cache_name_exists(name, namelen) index_name_exists(&the_index, (name), (namelen))
267
+#define cache_name_exists(name, namelen, igncase) index_name_exists(&the_index, (name), (namelen), (igncase))
268
#endif
269
270
enum object_type {
@@ -353,7 +353,7 @@ extern int write_index(const struct index_state *, int newfd);
353
extern int discard_index(struct index_state *);
354
extern int unmerged_index(const struct index_state *);
355
extern int verify_path(const char *path);
356
-extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen);
356
+extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int igncase);
357
extern int index_name_pos(const struct index_state *, const char *name, int namelen);
358
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
359
#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
dir.c
+1
-1
@@ -371,7 +371,7 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
371
372
struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
373
{
374
- if (cache_name_exists(pathname, len))
374
+ if (cache_name_exists(pathname, len, 0))
375
return NULL;
376
377
ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
name-hash.c
+48
-2
@@ -8,12 +8,25 @@
8
#define NO_THE_INDEX_COMPATIBILITY_MACROS
9
#include "cache.h"
10
11
+/*
12
+ * This removes bit 5 if bit 6 is set.
13
+ *
14
+ * That will make US-ASCII characters hash to their upper-case
15
+ * equivalent. We could easily do this one whole word at a time,
16
+ * but that's for future worries.
17
+ */
18
+static inline unsigned char icase_hash(unsigned char c)
19
+{
20
+ return c & ~((c & 0x40) >> 1);
21
+}
22
+
23
static unsigned int hash_name(const char *name, int namelen)
24
{
25
unsigned int hash = 0x123;
26
27
do {
28
unsigned char c = *name++;
29
+ c = icase_hash(c);
30
hash = hash*101 + c;
31
} while (--namelen);
32
return hash;
@@ -54,7 +67,40 @@ void add_name_hash(struct index_state *istate, struct cache_entry *ce)
67
hash_index_entry(istate, ce);
68
}
69
57
-struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen)
70
+static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
71
+{
72
+ if (len1 != len2)
73
+ return 0;
74
+
75
+ while (len1) {
76
+ unsigned char c1 = *name1++;
77
+ unsigned char c2 = *name2++;
78
+ len1--;
79
+ if (c1 != c2) {
80
+ c1 = toupper(c1);
81
+ c2 = toupper(c2);
82
+ if (c1 != c2)
83
+ return 0;
84
+ }
85
+ }
86
+ return 1;
87
+}
88
+
89
+static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
90
+{
91
+ int len = ce_namelen(ce);
92
+
93
+ /*
94
+ * Always do exact compare, even if we want a case-ignoring comparison;
95
+ * we do the quick exact one first, because it will be the common case.
96
+ */
97
+ if (len == namelen && !cache_name_compare(name, namelen, ce->name, len))
98
+ return 1;
99
+
100
+ return icase && slow_same_name(name, namelen, ce->name, len);
101
+}
102
+
103
+struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
104
{
105
unsigned int hash = hash_name(name, namelen);
106
struct cache_entry *ce;
@@ -64,7 +110,7 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
110
111
while (ce) {
112
if (!(ce->ce_flags & CE_UNHASHED)) {
67
- if (!cache_name_compare(name, namelen, ce->name, ce->ce_flags))
113
+ if (same_name(ce, name, namelen, icase))
114
return ce;
115
}
116
ce = ce->next;
unpack-trees.c
+1
-1
@@ -582,7 +582,7 @@ static int verify_absent(struct cache_entry *ce, const char *action,
582
* delete this path, which is in a subdirectory that
583
* is being replaced with a blob.
584
*/
585
- result = index_name_exists(&o->result, ce->name, ce_namelen(ce));
585
+ result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
586
if (result) {
587
if (result->ce_flags & CE_REMOVE)
588
return 0;