From 8e4b858b3ff61a969fdc5c695a0affb76354ed51 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Tue, 26 May 2020 02:58:34 -0700 Subject: AK: Move String::ends_with implementation to StringUtils Centralizing so it can be used by other string implementations --- AK/String.cpp | 8 +------- AK/StringUtils.cpp | 12 ++++++++++++ AK/StringUtils.h | 2 ++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/AK/String.cpp b/AK/String.cpp index 789b18dc8c..14f03c3f38 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -280,13 +280,7 @@ bool String::starts_with(char ch) const bool String::ends_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() + (length() - str.length()), str.characters_without_null_termination(), str.length()); + return StringUtils::ends_with(*this, str); } bool String::ends_with(char ch) const diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 4bd2b7aada..09e4661710 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -25,6 +25,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -195,6 +196,17 @@ bool equals_ignoring_case(const StringView& a, const StringView& b) return true; } +bool ends_with(const StringView& str, const StringView& end) +{ + if (end.is_empty()) + return true; + if (str.is_empty()) + return false; + if (end.length() > str.length()) + return false; + return !memcmp(str.characters_without_null_termination() + (str.length() - end.length()), end.characters_without_null_termination(), end.length()); +} + } } diff --git a/AK/StringUtils.h b/AK/StringUtils.h index 7e9007f102..5ba124f9de 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -43,6 +43,8 @@ int convert_to_int(const StringView&, bool& ok); unsigned convert_to_uint(const StringView&, bool& ok); unsigned convert_to_uint_from_hex(const StringView&, bool& ok); bool equals_ignoring_case(const StringView&, const StringView&); +bool ends_with(const StringView& str, const StringView& end); + } } -- cgit v1.2.3