2017-04-28 12:13:10 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef V8_STRING_HASHER_H_
|
|
|
|
#define V8_STRING_HASHER_H_
|
|
|
|
|
|
|
|
#include "src/globals.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Vector;
|
|
|
|
|
2019-05-08 12:18:34 +00:00
|
|
|
class V8_EXPORT_PRIVATE StringHasher final {
|
2017-04-28 12:13:10 +00:00
|
|
|
public:
|
2019-05-08 12:18:34 +00:00
|
|
|
StringHasher() = delete;
|
2017-04-28 12:13:10 +00:00
|
|
|
template <typename schar>
|
|
|
|
static inline uint32_t HashSequentialString(const schar* chars, int length,
|
2018-07-16 09:52:50 +00:00
|
|
|
uint64_t seed);
|
2017-04-28 12:13:10 +00:00
|
|
|
|
|
|
|
// Calculated hash value for a string consisting of 1 to
|
|
|
|
// String::kMaxArrayIndexSize digits with no leading zeros (except "0").
|
|
|
|
// value is represented decimal value.
|
|
|
|
static uint32_t MakeArrayIndexHash(uint32_t value, int length);
|
|
|
|
|
|
|
|
// No string is allowed to have a hash of zero. That value is reserved
|
|
|
|
// for internal properties. If the hash calculation yields zero then we
|
|
|
|
// use 27 instead.
|
|
|
|
static const int kZeroHash = 27;
|
|
|
|
|
|
|
|
// Reusable parts of the hashing algorithm.
|
2018-06-22 11:19:13 +00:00
|
|
|
V8_INLINE static uint32_t AddCharacterCore(uint32_t running_hash, uint16_t c);
|
|
|
|
V8_INLINE static uint32_t GetHashCore(uint32_t running_hash);
|
2017-04-28 12:13:10 +00:00
|
|
|
|
2019-05-08 12:18:34 +00:00
|
|
|
static inline uint32_t GetTrivialHash(int length);
|
2017-04-28 12:13:10 +00:00
|
|
|
};
|
|
|
|
|
2018-05-07 14:39:43 +00:00
|
|
|
// Useful for std containers that require something ()'able.
|
|
|
|
struct SeededStringHasher {
|
2018-07-16 09:52:50 +00:00
|
|
|
explicit SeededStringHasher(uint64_t hashseed) : hashseed_(hashseed) {}
|
2018-05-07 14:39:43 +00:00
|
|
|
inline std::size_t operator()(const char* name) const;
|
|
|
|
|
2018-07-16 09:52:50 +00:00
|
|
|
uint64_t hashseed_;
|
2018-05-07 14:39:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Useful for std containers that require something ()'able.
|
|
|
|
struct StringEquals {
|
|
|
|
bool operator()(const char* name1, const char* name2) const {
|
|
|
|
return strcmp(name1, name2) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-28 12:13:10 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_STRING_HASHER_H_
|