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
10namespace v8 {
11
12namespace base {
13template <typename T>
14class Vector;
15}  // namespace base
16
17namespace internal {
18
19// Helper class for incrementally calculating string hashes in a form suitable
20// for storing into Name::raw_hash_field.
21class V8_EXPORT_PRIVATE StringHasher final {
22 public:
23  StringHasher() = delete;
24  template <typename char_t>
25  static inline uint32_t HashSequentialString(const char_t* chars, int length,
26                                              uint64_t seed);
27
28  // Calculated hash value for a string consisting of 1 to
29  // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
30  // value is represented decimal value.
31  static uint32_t MakeArrayIndexHash(uint32_t value, int length);
32
33  // No string is allowed to have a hash of zero.  That value is reserved
34  // for internal properties.  If the hash calculation yields zero then we
35  // use 27 instead.
36  static const int kZeroHash = 27;
37
38  // Reusable parts of the hashing algorithm.
39  V8_INLINE static uint32_t AddCharacterCore(uint32_t running_hash, uint16_t c);
40  V8_INLINE static uint32_t GetHashCore(uint32_t running_hash);
41
42  static inline uint32_t GetTrivialHash(int length);
43};
44
45// Useful for std containers that require something ()'able.
46struct SeededStringHasher {
47  explicit SeededStringHasher(uint64_t hashseed) : hashseed_(hashseed) {}
48  inline std::size_t operator()(const char* name) const;
49
50  uint64_t hashseed_;
51};
52
53// Useful for std containers that require something ()'able.
54struct StringEquals {
55  bool operator()(const char* name1, const char* name2) const {
56    return strcmp(name1, name2) == 0;
57  }
58};
59
60}  // namespace internal
61}  // namespace v8
62
63#endif  // V8_STRINGS_STRING_HASHER_H_
64