From 05f641a5a9155855f922743148b3094d1ba6f21c Mon Sep 17 00:00:00 2001 From: MinusGix Date: Thu, 12 Sep 2019 06:13:07 -0500 Subject: [PATCH] StringView: Add starts_with method --- AK/StringView.cpp | 13 +++++++++++++ AK/StringView.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/AK/StringView.cpp b/AK/StringView.cpp index ae4d857c12a..798fd50e3c4 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -40,6 +40,19 @@ Vector StringView::split_view(const char separator) const return v; } +bool StringView::starts_with(const StringView& str) const +{ + if (str.is_empty()) + return true; + if (is_empty()) + return false; + if (str.length() > length()) + return false; + if (characters_without_null_termination() == str.characters_without_null_termination()) + return true; + return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length()); +} + StringView StringView::substring_view(int start, int length) const { if (!length) diff --git a/AK/StringView.h b/AK/StringView.h index 350f5fcd3d4..181e26607f5 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -41,6 +41,8 @@ public: unsigned hash() const; + bool starts_with(const StringView&) const; + StringView substring_view(int start, int length) const; Vector split_view(char) const;