2 * Copyright (C) 2006, 2007, 2008, 2012, 2013 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 "wtf/text/AtomicString.h"
26 #include "wtf/HashTraits.h"
27 #include "wtf/StringHasher.h"
31 inline bool HashTraits
<String
>::isEmptyValue(const String
& value
)
33 return value
.isNull();
36 // The hash() functions on StringHash and CaseFoldingHash do not support
37 // null strings. get(), contains(), and add() on HashMap<String,..., StringHash>
38 // cause a null-pointer dereference when passed null strings.
40 // FIXME: We should really figure out a way to put the computeHash function that's
41 // currently a member function of StringImpl into this file so we can be a little
42 // closer to having all the nearly-identical hash functions in one place.
45 static unsigned hash(StringImpl
* key
) { return key
->hash(); }
46 static inline bool equal(const StringImpl
* a
, const StringImpl
* b
)
48 return equalNonNull(a
, b
);
51 static unsigned hash(const RefPtr
<StringImpl
>& key
) { return key
->hash(); }
52 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
54 return equal(a
.get(), b
.get());
57 static unsigned hash(const String
& key
) { return key
.impl()->hash(); }
58 static bool equal(const String
& a
, const String
& b
)
60 return equal(a
.impl(), b
.impl());
63 static const bool safeToCompareToEmptyOrDeleted
= false;
66 class CaseFoldingHash
{
68 static unsigned hash(const UChar
* data
, unsigned length
)
70 return StringHasher::computeHashAndMaskTop8Bits
<UChar
, foldCase
<UChar
>>(data
, length
);
73 static unsigned hash(StringImpl
* str
)
76 return hash(str
->characters8(), str
->length());
77 return hash(str
->characters16(), str
->length());
80 static unsigned hash(const LChar
* data
, unsigned length
)
82 return StringHasher::computeHashAndMaskTop8Bits
<LChar
, foldCase
<LChar
>>(data
, length
);
85 static inline unsigned hash(const char* data
, unsigned length
)
87 return CaseFoldingHash::hash(reinterpret_cast<const LChar
*>(data
), length
);
90 static inline bool equal(const StringImpl
* a
, const StringImpl
* b
)
92 return equalIgnoringCaseNonNull(a
, b
);
95 static unsigned hash(const RefPtr
<StringImpl
>& key
)
97 return hash(key
.get());
100 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
102 return equal(a
.get(), b
.get());
105 static unsigned hash(const String
& key
)
107 return hash(key
.impl());
109 static unsigned hash(const AtomicString
& key
)
111 return hash(key
.impl());
113 static bool equal(const String
& a
, const String
& b
)
115 return equal(a
.impl(), b
.impl());
117 static bool equal(const AtomicString
& a
, const AtomicString
& b
)
119 return (a
== b
) || equal(a
.impl(), b
.impl());
122 static const bool safeToCompareToEmptyOrDeleted
= false;
125 // Private so no one uses this in the belief that it will return the
126 // correctly-folded code point in all cases (see comment below).
127 template<typename T
> static inline UChar
foldCase(T ch
)
129 if (IsSameType
<T
, LChar
>::value
)
130 return StringImpl::latin1CaseFoldTable
[ch
];
131 // It's possible for WTF::Unicode::foldCase() to return a 32-bit
132 // value that's not representable as a UChar. However, since this
133 // is rare and deterministic, and the result of this is merely used
134 // for hashing, go ahead and clamp the value.
135 return static_cast<UChar
>(WTF::Unicode::foldCase(ch
));
139 // This hash can be used in cases where the key is a hash of a string, but we don't
140 // want to store the string. It's not really specific to string hashing, but all our
141 // current uses of it are for strings.
142 struct AlreadyHashed
: IntHash
<unsigned> {
143 static unsigned hash(unsigned key
) { return key
; }
145 // To use a hash value as a key for a hash table, we need to eliminate the
146 // "deleted" value, which is negative one. That could be done by changing
147 // the string hash function to never generate negative one, but this works
148 // and is still relatively efficient.
149 static unsigned avoidDeletedValue(unsigned hash
)
152 unsigned newHash
= hash
| (!(hash
+ 1) << 31);
154 ASSERT(newHash
!= 0xFFFFFFFF);
161 using WTF::AlreadyHashed
;
162 using WTF::CaseFoldingHash
;
163 using WTF::StringHash
;