diff --git a/AK/AKString.h b/AK/AKString.h index 8f1119d80e..612dae31cc 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -126,6 +126,7 @@ public: return (*m_impl)[i]; } + bool starts_with(const StringView&) const; bool ends_with(const StringView&) const; bool operator==(const String&) const; diff --git a/AK/String.cpp b/AK/String.cpp index a7ac12fded..974f2dba1b 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -180,6 +180,17 @@ String String::format(const char* fmt, ...) return builder.to_string(); } +bool String::starts_with(const StringView& str) const +{ + if (str.is_empty()) + return true; + if (is_empty()) + return false; + if (str.length() > length()) + return false; + return !memcmp(characters(), str.characters(), str.length()); +} + bool String::ends_with(const StringView& str) const { if (str.is_empty())