From d38bd3935b4674a492e55c097866656dd745a2ac Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 24 Aug 2019 22:31:06 +0200 Subject: [PATCH] AK: Add StringView::hash() This grabs the hash from the underlying StringImpl if there is one, otherwise it's computed on the fly. --- AK/StringView.cpp | 9 +++++++++ AK/StringView.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 2e33eb9645b..95786eaf7c8 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -109,4 +109,13 @@ unsigned StringView::to_uint(bool& ok) const return value; } +unsigned StringView::hash() const +{ + if (is_empty()) + return 0; + if (m_impl) + return m_impl->hash(); + return string_hash(characters_without_null_termination(), length()); +} + } diff --git a/AK/StringView.h b/AK/StringView.h index 7fceb768857..350f5fcd3d4 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -39,6 +39,8 @@ public: int length() const { return m_length; } char operator[](int index) const { return m_characters[index]; } + unsigned hash() const; + StringView substring_view(int start, int length) const; Vector split_view(char) const;