From 95cc4c7e748b6bf77d132a05e962a6830b5f7622 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 28 Mar 2020 09:11:00 +0100 Subject: [PATCH] AK: Add some string comparison operators Some of these are very inefficient. It's nice to have some optimization opportunities in the future though. :^) --- AK/FlyString.cpp | 17 +++++++++++++++++ AK/FlyString.h | 9 +++++++++ AK/String.cpp | 5 +++++ AK/String.h | 3 +++ 4 files changed, 34 insertions(+) diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 86acdd8570..36f5c8a241 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -104,4 +104,21 @@ StringView FlyString::view() const return { characters(), length() }; } +bool FlyString::operator==(const String& string) const +{ + if (m_impl == string.impl()) + return true; + return String(m_impl.ptr()) == string; +} + +bool FlyString::operator==(const StringView& string) const +{ + return String(string) == String(m_impl.ptr()); +} + +bool FlyString::operator==(const char* string) const +{ + return String(string) == String(m_impl.ptr()); +} + } diff --git a/AK/FlyString.h b/AK/FlyString.h index 6dc9391c32..fc89e840b2 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -42,6 +42,15 @@ public: bool operator==(const FlyString& other) const { return m_impl == other.m_impl; } bool operator!=(const FlyString& other) const { return m_impl != other.m_impl; } + bool operator==(const String&) const; + bool operator!=(const String& string) const { return !(*this == string); } + + bool operator==(const StringView&) const; + bool operator!=(const StringView& string) const { return !(*this == string); } + + bool operator==(const char*) const; + bool operator!=(const char* string) const { return !(*this == string); } + const StringImpl* impl() const { return m_impl; } const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } size_t length() const { return m_impl ? m_impl->length() : 0; } diff --git a/AK/String.cpp b/AK/String.cpp index 96d426a025..c718858d37 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -50,6 +50,11 @@ String::String(const StringView& view) m_impl = StringImpl::create(view.characters_without_null_termination(), view.length()); } +bool String::operator==(const FlyString& fly_string) const +{ + return *this == String(fly_string.impl()); +} + bool String::operator==(const String& other) const { if (!m_impl) diff --git a/AK/String.h b/AK/String.h index ce7ad196e2..22a4ed1dfe 100644 --- a/AK/String.h +++ b/AK/String.h @@ -148,6 +148,9 @@ public: bool operator==(const StringView&) const; bool operator!=(const StringView& other) const { return !(*this == other); } + bool operator==(const FlyString&) const; + bool operator!=(const FlyString& other) const { return !(*this == other); } + bool operator<(const String&) const; bool operator<(const char*) const; bool operator>=(const String& other) const { return !(*this < other); }