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/HashTraits.h>
28 #include <wtf/StringHashFunctions.h>
29 #include <wtf/unicode/Unicode.h>
31 // FIXME: This is a temporary layering violation while we move string code to WTF.
32 // Landing the file moves in one patch, will follow on with patches to change the namespaces.
35 // The hash() functions on StringHash and CaseFoldingHash do not support
36 // null strings. get(), contains(), and add() on HashMap<String,..., StringHash>
37 // cause a null-pointer dereference when passed null strings.
39 // FIXME: We should really figure out a way to put the computeHash function that's
40 // currently a member function of StringImpl into this file so we can be a little
41 // closer to having all the nearly-identical hash functions in one place.
44 static unsigned hash(StringImpl
* key
) { return key
->hash(); }
45 static bool equal(const StringImpl
* a
, const StringImpl
* b
)
52 unsigned aLength
= a
->length();
53 unsigned bLength
= b
->length();
54 if (aLength
!= bLength
)
57 // FIXME: perhaps we should have a more abstract macro that indicates when
58 // going 4 bytes at a time is unsafe
59 #if CPU(ARM) || CPU(SH4)
60 const UChar
* aChars
= a
->characters();
61 const UChar
* bChars
= b
->characters();
62 for (unsigned i
= 0; i
!= aLength
; ++i
) {
63 if (*aChars
++ != *bChars
++)
68 /* Do it 4-bytes-at-a-time on architectures where it's safe */
69 const uint32_t* aChars
= reinterpret_cast<const uint32_t*>(a
->characters());
70 const uint32_t* bChars
= reinterpret_cast<const uint32_t*>(b
->characters());
72 unsigned halfLength
= aLength
>> 1;
73 for (unsigned i
= 0; i
!= halfLength
; ++i
)
74 if (*aChars
++ != *bChars
++)
77 if (aLength
& 1 && *reinterpret_cast<const uint16_t*>(aChars
) != *reinterpret_cast<const uint16_t*>(bChars
))
84 static unsigned hash(const RefPtr
<StringImpl
>& key
) { return key
->hash(); }
85 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
87 return equal(a
.get(), b
.get());
90 static unsigned hash(const String
& key
) { return key
.impl()->hash(); }
91 static bool equal(const String
& a
, const String
& b
)
93 return equal(a
.impl(), b
.impl());
96 static const bool safeToCompareToEmptyOrDeleted
= false;
99 class CaseFoldingHash
{
101 // Paul Hsieh's SuperFastHash
102 // http://www.azillionmonkeys.com/qed/hash.html
103 static unsigned hash(const UChar
* data
, unsigned length
)
106 const UChar
* s
= data
;
107 uint32_t hash
= WTF::stringHashingStartValue
;
115 hash
+= WTF::Unicode::foldCase(s
[0]);
116 tmp
= (WTF::Unicode::foldCase(s
[1]) << 11) ^ hash
;
117 hash
= (hash
<< 16) ^ tmp
;
124 hash
+= WTF::Unicode::foldCase(s
[0]);
129 // Force "avalanching" of final 127 bits.
136 // This avoids ever returning a hash code of 0, since that is used to
137 // signal "hash not computed yet", using a value that is likely to be
138 // effectively the same as 0 when the low bits are masked.
144 static unsigned hash(StringImpl
* str
)
146 return hash(str
->characters(), str
->length());
149 static unsigned hash(const char* str
, unsigned length
)
151 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
152 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
153 // were 16-bit chunks, which will give matching results.
157 uint32_t hash
= WTF::stringHashingStartValue
;
165 hash
+= WTF::Unicode::foldCase(s
[0]);
166 tmp
= (WTF::Unicode::foldCase(s
[1]) << 11) ^ hash
;
167 hash
= (hash
<< 16) ^ tmp
;
174 hash
+= WTF::Unicode::foldCase(s
[0]);
179 // Force "avalanching" of final 127 bits
186 // this avoids ever returning a hash code of 0, since that is used to
187 // signal "hash not computed yet", using a value that is likely to be
188 // effectively the same as 0 when the low bits are masked
194 static bool equal(const StringImpl
* a
, const StringImpl
* b
)
200 unsigned length
= a
->length();
201 if (length
!= b
->length())
203 return WTF::Unicode::umemcasecmp(a
->characters(), b
->characters(), length
) == 0;
206 static unsigned hash(const RefPtr
<StringImpl
>& key
)
208 return hash(key
.get());
211 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
213 return equal(a
.get(), b
.get());
216 static unsigned hash(const String
& key
)
218 return hash(key
.impl());
220 static unsigned hash(const AtomicString
& key
)
222 return hash(key
.impl());
224 static bool equal(const String
& a
, const String
& b
)
226 return equal(a
.impl(), b
.impl());
228 static bool equal(const AtomicString
& a
, const AtomicString
& b
)
230 return (a
== b
) || equal(a
.impl(), b
.impl());
233 static const bool safeToCompareToEmptyOrDeleted
= false;
236 // This hash can be used in cases where the key is a hash of a string, but we don't
237 // want to store the string. It's not really specific to string hashing, but all our
238 // current uses of it are for strings.
239 struct AlreadyHashed
: IntHash
<unsigned> {
240 static unsigned hash(unsigned key
) { return key
; }
242 // To use a hash value as a key for a hash table, we need to eliminate the
243 // "deleted" value, which is negative one. That could be done by changing
244 // the string hash function to never generate negative one, but this works
245 // and is still relatively efficient.
246 static unsigned avoidDeletedValue(unsigned hash
)
249 unsigned newHash
= hash
| (!(hash
+ 1) << 31);
251 ASSERT(newHash
!= 0xFFFFFFFF);
260 template<> struct HashTraits
<WebCore::String
> : GenericHashTraits
<WebCore::String
> {
261 static const bool emptyValueIsZero
= true;
262 static void constructDeletedValue(WebCore::String
& slot
) { new (&slot
) WebCore::String(HashTableDeletedValue
); }
263 static bool isDeletedValue(const WebCore::String
& slot
) { return slot
.isHashTableDeletedValue(); }