diff options
author | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2008-03-21 15:55:19 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2008-04-09 01:22:25 -0700 |
commit | cd2fef59edf72a1d9792d9cb72aae1e6f6c7b1d4 (patch) | |
tree | 720d22370ec0831e4ff8800d96ddd19c90106d69 /name-hash.c | |
parent | df292c791ab790340cc9e3577a073bcb9d1900ea (diff) | |
download | git-cd2fef59edf72a1d9792d9cb72aae1e6f6c7b1d4.tar.gz |
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>
Diffstat (limited to 'name-hash.c')
-rw-r--r-- | name-hash.c | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/name-hash.c b/name-hash.c index 2678148937..0031d78e8c 100644 --- a/name-hash.c +++ b/name-hash.c @@ -8,12 +8,25 @@ #define NO_THE_INDEX_COMPATIBILITY_MACROS #include "cache.h" +/* + * This removes bit 5 if bit 6 is set. + * + * That will make US-ASCII characters hash to their upper-case + * equivalent. We could easily do this one whole word at a time, + * but that's for future worries. + */ +static inline unsigned char icase_hash(unsigned char c) +{ + return c & ~((c & 0x40) >> 1); +} + static unsigned int hash_name(const char *name, int namelen) { unsigned int hash = 0x123; do { unsigned char c = *name++; + c = icase_hash(c); hash = hash*101 + c; } while (--namelen); return hash; @@ -54,7 +67,40 @@ void add_name_hash(struct index_state *istate, struct cache_entry *ce) hash_index_entry(istate, ce); } -struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen) +static int slow_same_name(const char *name1, int len1, const char *name2, int len2) +{ + if (len1 != len2) + return 0; + + while (len1) { + unsigned char c1 = *name1++; + unsigned char c2 = *name2++; + len1--; + if (c1 != c2) { + c1 = toupper(c1); + c2 = toupper(c2); + if (c1 != c2) + return 0; + } + } + return 1; +} + +static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase) +{ + int len = ce_namelen(ce); + + /* + * Always do exact compare, even if we want a case-ignoring comparison; + * we do the quick exact one first, because it will be the common case. + */ + if (len == namelen && !cache_name_compare(name, namelen, ce->name, len)) + return 1; + + return icase && slow_same_name(name, namelen, ce->name, len); +} + +struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase) { unsigned int hash = hash_name(name, namelen); struct cache_entry *ce; @@ -64,7 +110,7 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na while (ce) { if (!(ce->ce_flags & CE_UNHASHED)) { - if (!cache_name_compare(name, namelen, ce->name, ce->ce_flags)) + if (same_name(ce, name, namelen, icase)) return ce; } ce = ce->next; |