BAN: overloaded operator== for more types

This commit is contained in:
Bananymous 2022-12-13 20:55:09 +02:00
parent fd6e0ed0f7
commit 5345b6b8c3
4 changed files with 34 additions and 2 deletions

View File

@ -134,6 +134,20 @@ namespace BAN
return memcmp(m_data, other.m_data, m_size) == 0;
}
bool String::operator==(StringView other) const
{
if (m_size != other.Size())
return false;
return memcmp(m_data, other.Data(), m_size) == 0;
}
bool String::operator==(const char* other) const
{
if (m_size != strlen(other))
return false;
return memcmp(m_data, other, m_size) == 0;
}
ErrorOr<void> String::Resize(size_type size, char ch)
{
if (size < m_size)

View File

@ -29,13 +29,27 @@ namespace BAN
return m_data[index];
}
bool StringView::operator==(const StringView& other) const
bool StringView::operator==(const String& other) const
{
if (m_size != other.Size())
return false;
return memcmp(m_data, other.Data(), m_size) == 0;
}
bool StringView::operator==(StringView other) const
{
if (m_size != other.m_size)
return false;
return memcmp(m_data, other.m_data, m_size) == 0;
}
bool StringView::operator==(const char* other) const
{
if (m_size != strlen(other))
return false;
return memcmp(m_data, other, m_size) == 0;
}
StringView StringView::Substring(size_type index, size_type len) const
{
assert(index <= m_size);

View File

@ -37,6 +37,8 @@ namespace BAN
char& operator[](size_type);
bool operator==(const String&) const;
bool operator==(StringView) const;
bool operator==(const char*) const;
ErrorOr<void> Resize(size_type, char = '\0');
ErrorOr<void> Reserve(size_type);

View File

@ -18,7 +18,9 @@ namespace BAN
char operator[](size_type) const;
bool operator==(const StringView&) const;
bool operator==(const String&) const;
bool operator==(StringView) const;
bool operator==(const char*) const;
StringView Substring(size_type, size_type = -1) const;