diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-24 22:31:06 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-25 06:45:31 +0200 |
commit | d38bd3935b4674a492e55c097866656dd745a2ac (patch) | |
tree | cc62d5c4f950654a70e1a8d4fe713d4c8a943a51 | |
parent | bb32fd8bfa93ac3a70381af367ec8809624d59eb (diff) | |
download | serenity-d38bd3935b4674a492e55c097866656dd745a2ac.zip |
AK: Add StringView::hash()
This grabs the hash from the underlying StringImpl if there is one,
otherwise it's computed on the fly.
-rw-r--r-- | AK/StringView.cpp | 9 | ||||
-rw-r--r-- | AK/StringView.h | 2 |
2 files changed, 11 insertions, 0 deletions
diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 2e33eb9645..95786eaf7c 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 7fceb76885..350f5fcd3d 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<StringView> split_view(char) const; |