From 5345b6b8c350509440a46d20a1d9406f73c9635c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 13 Dec 2022 20:55:09 +0200 Subject: [PATCH] BAN: overloaded operator== for more types --- BAN/BAN/String.cpp | 14 ++++++++++++++ BAN/BAN/StringView.cpp | 16 +++++++++++++++- BAN/include/BAN/String.h | 2 ++ BAN/include/BAN/StringView.h | 4 +++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp index c79360ed3..9644f6349 100644 --- a/BAN/BAN/String.cpp +++ b/BAN/BAN/String.cpp @@ -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 String::Resize(size_type size, char ch) { if (size < m_size) diff --git a/BAN/BAN/StringView.cpp b/BAN/BAN/StringView.cpp index 97295ab97..b86d78c73 100644 --- a/BAN/BAN/StringView.cpp +++ b/BAN/BAN/StringView.cpp @@ -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); diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index c37fe22b8..371b10f45 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -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 Resize(size_type, char = '\0'); ErrorOr Reserve(size_type); diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index 2c92c339f..0bc531a56 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -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;