diff --git a/AK/StringHash.h b/AK/StringHash.h new file mode 100644 index 00000000000..7b01b48bba0 --- /dev/null +++ b/AK/StringHash.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018-2021, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +namespace AK { + +constexpr u32 string_hash(char const* characters, size_t length) +{ + u32 hash = 0; + for (size_t i = 0; i < length; ++i) { + hash += (u32)characters[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + hash += hash << 3; + hash ^= hash >> 11; + hash += hash << 15; + return hash; +} + +} + +using AK::string_hash; diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index ea7c4f52550..5903487bd46 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -6,9 +6,9 @@ #include #include -#include #include #include +#include #include #include diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 436c476bc7c..316321ef5d9 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -96,20 +96,6 @@ private: char m_inline_buffer[0]; }; -constexpr u32 string_hash(const char* characters, size_t length) -{ - u32 hash = 0; - for (size_t i = 0; i < length; ++i) { - hash += (u32)characters[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } - hash += hash << 3; - hash ^= hash >> 11; - hash += hash << 15; - return hash; -} - template<> struct Formatter : Formatter { void format(FormatBuilder& builder, const StringImpl& value) @@ -122,5 +108,4 @@ struct Formatter : Formatter { using AK::Chomp; using AK::NoChomp; -using AK::string_hash; using AK::StringImpl; diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 3f8e283f7c9..4bb4c1b9f47 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -214,13 +214,6 @@ template Optional StringView::to_uint() const; template Optional StringView::to_uint() const; template Optional StringView::to_uint() const; -unsigned StringView::hash() const -{ - if (is_empty()) - return 0; - return string_hash(characters_without_null_termination(), length()); -} - bool StringView::operator==(const String& string) const { if (string.is_null()) diff --git a/AK/StringView.h b/AK/StringView.h index f32de9c8ccf..dab15292fb9 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -49,7 +50,7 @@ public: [[nodiscard]] constexpr bool is_null() const { return !m_characters; } [[nodiscard]] constexpr bool is_empty() const { return m_length == 0; } - [[nodiscard]] const char* characters_without_null_termination() const { return m_characters; } + [[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; } [[nodiscard]] constexpr size_t length() const { return m_length; } [[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; } @@ -61,7 +62,12 @@ public: [[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); } [[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); } - [[nodiscard]] unsigned hash() const; + [[nodiscard]] constexpr unsigned hash() const + { + if (is_empty()) + return 0; + return string_hash(characters_without_null_termination(), length()); + } [[nodiscard]] bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;