mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Implement all comparison operators for StringView
This commit is contained in:
parent
2e5a9b4fab
commit
ca58c71faa
Notes:
sideshowbarker
2024-07-17 20:00:57 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/ca58c71faa6 Pull-request: https://github.com/SerenityOS/serenity/pull/12201
2 changed files with 24 additions and 23 deletions
|
@ -203,15 +203,7 @@ template Optional<long long> StringView::to_uint() const;
|
|||
|
||||
bool StringView::operator==(const String& string) const
|
||||
{
|
||||
if (string.is_null())
|
||||
return !m_characters;
|
||||
if (!m_characters)
|
||||
return false;
|
||||
if (m_length != string.length())
|
||||
return false;
|
||||
if (m_characters == string.characters())
|
||||
return true;
|
||||
return !__builtin_memcmp(m_characters, string.characters(), m_length);
|
||||
return *this == string.view();
|
||||
}
|
||||
|
||||
String StringView::to_string() const { return String { *this }; }
|
||||
|
|
|
@ -204,28 +204,37 @@ public:
|
|||
|
||||
bool operator==(const String&) const;
|
||||
|
||||
[[nodiscard]] constexpr int compare(StringView other) const
|
||||
{
|
||||
size_t rlen = min(length(), other.length());
|
||||
int c = (rlen != 0) ? __builtin_memcmp(m_characters, other.m_characters, rlen) : 0;
|
||||
if (c == 0) {
|
||||
if (length() < other.length())
|
||||
return -1;
|
||||
if (length() == other.length())
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
constexpr bool operator==(StringView other) const
|
||||
{
|
||||
if (is_null())
|
||||
return other.is_null();
|
||||
if (other.is_null())
|
||||
return false;
|
||||
if (length() != other.length())
|
||||
return false;
|
||||
return __builtin_memcmp(m_characters, other.m_characters, m_length) == 0;
|
||||
return length() == other.length() && compare(other) == 0;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(StringView other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
return length() != other.length() || compare(other) != 0;
|
||||
}
|
||||
|
||||
bool operator<(StringView other) const
|
||||
{
|
||||
if (int c = __builtin_memcmp(m_characters, other.m_characters, min(m_length, other.m_length)))
|
||||
return c < 0;
|
||||
return m_length < other.m_length;
|
||||
}
|
||||
constexpr bool operator<(StringView other) const { return compare(other) < 0; }
|
||||
|
||||
constexpr bool operator<=(StringView other) const { return compare(other) <= 0; }
|
||||
|
||||
constexpr bool operator>(StringView other) const { return compare(other) > 0; }
|
||||
|
||||
constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
|
||||
|
||||
[[nodiscard]] String to_string() const;
|
||||
|
||||
|
|
Loading…
Reference in a new issue