#ifndef StringHash_h
#define StringHash_h
#include "AtomicStringImpl.h"
#include "PlatformString.h"
#include <wtf/HashTraits.h>
#include <unicode/uchar.h>
#include <unicode/ustring.h>
namespace WTF {
template<typename T> struct StrHash;
template<> struct StrHash<WebCore::StringImpl*> {
static unsigned hash(const WebCore::StringImpl* key) { return key->hash(); }
static bool equal(const WebCore::StringImpl* a, const WebCore::StringImpl* b)
{
if (a == b)
return true;
if (!a || !b)
return false;
unsigned aLength = a->length();
unsigned bLength = b->length();
if (aLength != bLength)
return false;
const uint32_t* aChars = reinterpret_cast<const uint32_t*>(a->characters());
const uint32_t* bChars = reinterpret_cast<const uint32_t*>(b->characters());
unsigned halfLength = aLength >> 1;
for (unsigned i = 0; i != halfLength; ++i)
if (*aChars++ != *bChars++)
return false;
if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
return false;
return true;
}
};
class CaseInsensitiveHash {
private:
static const unsigned PHI = 0x9e3779b9U;
public:
static unsigned hash(const WebCore::StringImpl* str)
{
unsigned l = str->length();
const UChar* s = str->characters();
uint32_t hash = PHI;
uint32_t tmp;
int rem = l & 1;
l >>= 1;
for (; l > 0; l--) {
hash += u_foldCase(s[0], U_FOLD_CASE_DEFAULT);
tmp = (u_foldCase(s[1], U_FOLD_CASE_DEFAULT) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
s += 2;
hash += hash >> 11;
}
if (rem) {
hash += u_foldCase(s[0], U_FOLD_CASE_DEFAULT);
hash ^= hash << 11;
hash += hash >> 17;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
if (hash == 0)
hash = 0x80000000;
return hash;
}
static unsigned hash(const char* str, unsigned length)
{
unsigned l = length;
const char* s = str;
uint32_t hash = PHI;
uint32_t tmp;
int rem = l & 1;
l >>= 1;
for (; l > 0; l--) {
hash += u_foldCase(s[0], U_FOLD_CASE_DEFAULT);
tmp = (u_foldCase(s[1], U_FOLD_CASE_DEFAULT) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
s += 2;
hash += hash >> 11;
}
if (rem) {
hash += u_foldCase(s[0], U_FOLD_CASE_DEFAULT);
hash ^= hash << 11;
hash += hash >> 17;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
if (hash == 0)
hash = 0x80000000;
return hash;
}
static bool equal(const WebCore::StringImpl* a, const WebCore::StringImpl* b)
{
if (a == b)
return true;
if (!a || !b)
return false;
unsigned length = a->length();
if (length != b->length())
return false;
return u_memcasecmp(a->characters(), b->characters(), length, U_FOLD_CASE_DEFAULT) == 0;
}
};
template<> struct StrHash<WebCore::AtomicStringImpl*> : public StrHash<WebCore::StringImpl*> { };
template<> struct StrHash<RefPtr<WebCore::StringImpl> > {
static unsigned hash(const RefPtr<WebCore::StringImpl>& key) { return key->hash(); }
static bool equal(const RefPtr<WebCore::StringImpl>& a, const RefPtr<WebCore::StringImpl>& b)
{
return StrHash<WebCore::StringImpl*>::equal(a.get(), b.get());
}
};
template<> struct StrHash<WebCore::String> {
static unsigned hash(const WebCore::String& key) { return key.impl()->hash(); }
static bool equal(const WebCore::String& a, const WebCore::String& b)
{
return StrHash<WebCore::StringImpl*>::equal(a.impl(), b.impl());
}
};
template<> struct HashTraits<WebCore::String> : GenericHashTraits<WebCore::String> {
typedef HashTraits<WebCore::StringImpl*>::StorageTraits StorageTraits;
static const bool emptyValueIsZero = true;
static const bool needsRef = true;
static void ref(const WebCore::String& s) { if (s.impl()) s.impl()->ref(); }
static void deref(const WebCore::String& s) { if (s.impl()) s.impl()->deref(); }
};
template<> struct HashKeyStorageTraits<StrHash<RefPtr<WebCore::StringImpl> >, HashTraits<RefPtr<WebCore::StringImpl> > > {
typedef StrHash<WebCore::StringImpl*> Hash;
typedef HashTraits<WebCore::StringImpl*> Traits;
};
template<> struct HashKeyStorageTraits<StrHash<WebCore::String>, HashTraits<WebCore::String> > {
typedef StrHash<WebCore::StringImpl*> Hash;
typedef HashTraits<WebCore::StringImpl*> Traits;
};
}
using WTF::CaseInsensitiveHash;
#endif