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