1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_STRINGS_STRING_HASHER_H_
6 #define V8_STRINGS_STRING_HASHER_H_
7 
8 #include "src/common/globals.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 template <typename T>
14 class Vector;
15 
16 class V8_EXPORT_PRIVATE StringHasher final {
17  public:
18   StringHasher() = delete;
19   template <typename char_t>
20   static inline uint32_t HashSequentialString(const char_t* chars, int length,
21                                               uint64_t seed);
22 
23   // Calculated hash value for a string consisting of 1 to
24   // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
25   // value is represented decimal value.
26   static uint32_t MakeArrayIndexHash(uint32_t value, int length);
27 
28   // No string is allowed to have a hash of zero.  That value is reserved
29   // for internal properties.  If the hash calculation yields zero then we
30   // use 27 instead.
31   static const int kZeroHash = 27;
32 
33   // Reusable parts of the hashing algorithm.
34   V8_INLINE static uint32_t AddCharacterCore(uint32_t running_hash, uint16_t c);
35   V8_INLINE static uint32_t GetHashCore(uint32_t running_hash);
36 
37   static inline uint32_t GetTrivialHash(int length);
38 };
39 
40 // Useful for std containers that require something ()'able.
41 struct SeededStringHasher {
SeededStringHasherSeededStringHasher42   explicit SeededStringHasher(uint64_t hashseed) : hashseed_(hashseed) {}
43   inline std::size_t operator()(const char* name) const;
44 
45   uint64_t hashseed_;
46 };
47 
48 // Useful for std containers that require something ()'able.
49 struct StringEquals {
operatorStringEquals50   bool operator()(const char* name1, const char* name2) const {
51     return strcmp(name1, name2) == 0;
52   }
53 };
54 
55 }  // namespace internal
56 }  // namespace v8
57 
58 #endif  // V8_STRINGS_STRING_HASHER_H_
59