2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved
3 * Copyright (C) Research In Motion Limited 2009. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "AtomicString.h"
26 #include "WTFString.h"
27 #include <wtf/Forward.h>
28 #include <wtf/HashTraits.h>
29 #include <wtf/StringHasher.h>
30 #include <wtf/unicode/Unicode.h>
34 // The hash() functions on StringHash and CaseFoldingHash do not support
35 // null strings. get(), contains(), and add() on HashMap<String,..., StringHash>
36 // cause a null-pointer dereference when passed null strings.
38 // FIXME: We should really figure out a way to put the computeHash function that's
39 // currently a member function of StringImpl into this file so we can be a little
40 // closer to having all the nearly-identical hash functions in one place.
43 static unsigned hash(StringImpl
* key
) { return key
->hash(); }
44 static bool equal(const StringImpl
* a
, const StringImpl
* b
)
51 unsigned aLength
= a
->length();
52 unsigned bLength
= b
->length();
53 if (aLength
!= bLength
)
56 // FIXME: perhaps we should have a more abstract macro that indicates when
57 // going 4 bytes at a time is unsafe
58 #if CPU(ARM) || CPU(SH4) || CPU(MIPS)
59 const UChar
* aChars
= a
->characters();
60 const UChar
* bChars
= b
->characters();
61 for (unsigned i
= 0; i
!= aLength
; ++i
) {
62 if (*aChars
++ != *bChars
++)
67 /* Do it 4-bytes-at-a-time on architectures where it's safe */
68 const uint32_t* aChars
= reinterpret_cast<const uint32_t*>(a
->characters());
69 const uint32_t* bChars
= reinterpret_cast<const uint32_t*>(b
->characters());
71 unsigned halfLength
= aLength
>> 1;
72 for (unsigned i
= 0; i
!= halfLength
; ++i
)
73 if (*aChars
++ != *bChars
++)
76 if (aLength
& 1 && *reinterpret_cast<const uint16_t*>(aChars
) != *reinterpret_cast<const uint16_t*>(bChars
))
83 static unsigned hash(const RefPtr
<StringImpl
>& key
) { return key
->hash(); }
84 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
86 return equal(a
.get(), b
.get());
89 static unsigned hash(const String
& key
) { return key
.impl()->hash(); }
90 static bool equal(const String
& a
, const String
& b
)
92 return equal(a
.impl(), b
.impl());
95 static const bool safeToCompareToEmptyOrDeleted
= false;
98 class CaseFoldingHash
{
100 template<typename T
> static inline UChar
foldCase(T ch
)
102 return WTF::Unicode::foldCase(ch
);
105 static unsigned hash(const UChar
* data
, unsigned length
)
107 return StringHasher::computeHash
<UChar
, foldCase
<UChar
> >(data
, length
);
110 static unsigned hash(StringImpl
* str
)
112 return hash(str
->characters(), str
->length());
115 static unsigned hash(const char* data
, unsigned length
)
117 return StringHasher::computeHash
<char, foldCase
<char> >(data
, length
);
120 static bool equal(const StringImpl
* a
, const StringImpl
* b
)
126 unsigned length
= a
->length();
127 if (length
!= b
->length())
129 return WTF::Unicode::umemcasecmp(a
->characters(), b
->characters(), length
) == 0;
132 static unsigned hash(const RefPtr
<StringImpl
>& key
)
134 return hash(key
.get());
137 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
139 return equal(a
.get(), b
.get());
142 static unsigned hash(const String
& key
)
144 return hash(key
.impl());
146 static unsigned hash(const AtomicString
& key
)
148 return hash(key
.impl());
150 static bool equal(const String
& a
, const String
& b
)
152 return equal(a
.impl(), b
.impl());
154 static bool equal(const AtomicString
& a
, const AtomicString
& b
)
156 return (a
== b
) || equal(a
.impl(), b
.impl());
159 static const bool safeToCompareToEmptyOrDeleted
= false;
162 // This hash can be used in cases where the key is a hash of a string, but we don't
163 // want to store the string. It's not really specific to string hashing, but all our
164 // current uses of it are for strings.
165 struct AlreadyHashed
: IntHash
<unsigned> {
166 static unsigned hash(unsigned key
) { return key
; }
168 // To use a hash value as a key for a hash table, we need to eliminate the
169 // "deleted" value, which is negative one. That could be done by changing
170 // the string hash function to never generate negative one, but this works
171 // and is still relatively efficient.
172 static unsigned avoidDeletedValue(unsigned hash
)
175 unsigned newHash
= hash
| (!(hash
+ 1) << 31);
177 ASSERT(newHash
!= 0xFFFFFFFF);
182 template<> struct HashTraits
<String
> : SimpleClassHashTraits
<String
> { };
186 using WTF::StringHash
;
187 using WTF::CaseFoldingHash
;
188 using WTF::AlreadyHashed
;