From ffdc1d83601be1b4cbc37c5c3f92d55e3a6dd1e0 Mon Sep 17 00:00:00 2001 From: janwas Date: Sun, 27 Mar 2005 01:31:39 +0000 Subject: [PATCH] add lower-case hash (used by VFS) and revise match_wildcard to accept anything if pattern=0 This was SVN commit r2054. --- source/lib/lib.cpp | 39 +++++++++++++++++++++++++++++++++++++++ source/lib/lib.h | 9 ++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index ecbe041504..6dabecff45 100755 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -98,6 +98,41 @@ u64 fnv_hash64(const void* buf, const size_t len) } +// special version for strings: first converts to lowercase +// (useful for comparing mixed-case filenames). +// note: still need , e.g. to support non-0-terminated strings +u32 fnv_lc_hash(const char* str, const size_t len) +{ + u32 h = 0x811c9dc5u; + // give distinct values for different length 0 buffers. + // value taken from FNV; it has no special significance. + + // expected case: string + if(!len) + { + while(*str) + { + h ^= tolower(*str++); + h *= 0x01000193u; + } + } + else + { + size_t bytes_left = len; + while(bytes_left != 0) + { + h ^= tolower(*str++); + h *= 0x01000193u; + + bytes_left--; + } + } + + return h; +} + + + bool is_pow2(const long n) @@ -259,8 +294,12 @@ void base32(const int len, const u8* in, u8* out) // case-insensitive check if string matches the pattern , // which may contain '?' or '*' wildcards. if so, return 1, otherwise 0. // idea from http://www.codeproject.com/string/wildcmp.asp . +// note: NULL wildcard pattern matches everything! int match_wildcard(const char* s, const char* w) { + if(!w) + return 1; + // saved position in both strings, used to expand '*': // s2 is advanced until match. // initially 0 - we abort on mismatch before the first '*'. diff --git a/source/lib/lib.h b/source/lib/lib.h index 60066be3ed..da254a4469 100755 --- a/source/lib/lib.h +++ b/source/lib/lib.h @@ -100,7 +100,7 @@ STMT(\ int err__ = (int)((func) & UINT_MAX);\ if(err__ < 0)\ {\ - assert(0 && "FYI: CHECK_ERR reports that a function failed."\ + assert2(0 && "FYI: CHECK_ERR reports that a function failed."\ "feel free to ignore or suppress this warning.");\ return err__;\ }\ @@ -269,14 +269,16 @@ const size_t GiB = 1ul << 30; - - // FNV1-A hash - good for strings. // if len = 0 (default), treat buf as a C-string; // otherwise, hash bytes of buf. extern u32 fnv_hash(const void* buf, const size_t len = 0); extern u64 fnv_hash64(const void* buf, const size_t len = 0); +// special version for strings: first converts to lowercase +// (useful for comparing mixed-case filenames) +extern u32 fnv_lc_hash(const char* str, const size_t len = 0); + // hash (currently FNV) of a filename typedef u32 FnHash; @@ -306,6 +308,7 @@ extern void base32(const int len, const u8* in, u8* out); // case-insensitive check if string matches the pattern , // which may contain '?' or '*' wildcards. if so, return 1, otherwise 0. +// note: NULL wildcard pattern matches everything! extern int match_wildcard(const char* s, const char* w);