blob: b5966adb3c2469e0de97a37e8cca4e1709319d1a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/*
* Copyright (c) 2018-2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace AK {
constexpr u32 string_hash(char const* characters, size_t length, u32 seed = 0)
{
u32 hash = seed;
for (size_t i = 0; i < length; ++i) {
hash += static_cast<u32>(characters[i]);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
constexpr u32 case_insensitive_string_hash(char const* characters, size_t length, u32 seed = 0)
{
// AK/CharacterTypes.h cannot be included from here.
auto to_lowercase = [](char ch) -> u32 {
if (ch >= 'A' && ch <= 'Z')
return static_cast<u32>(ch) + 0x20;
return static_cast<u32>(ch);
};
u32 hash = seed;
for (size_t i = 0; i < length; ++i) {
hash += to_lowercase(characters[i]);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
}
#if USING_AK_GLOBALLY
using AK::string_hash;
#endif
|