From e318098f0b5c761348d09b80d35585afc40a0905 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Fri, 22 Mar 2024 19:12:45 +1100 Subject: [PATCH] Add equality operator for Directives (fixes networking bug) --- source/core/StarDirectives.cpp | 35 +++++++++++++++++++++++++++++++--- source/core/StarDirectives.hpp | 14 ++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp index 245c8c0..4770200 100644 --- a/source/core/StarDirectives.cpp +++ b/source/core/StarDirectives.cpp @@ -46,7 +46,7 @@ Directives::Shared::Shared(List&& givenEntries, String&& givenString, Str entries = std::move(givenEntries); string = std::move(givenString); prefix = givenPrefix; - hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size()); + hash = string.empty() ? 0 : XXH3_64bits(string.utf8Ptr(), string.utf8Size()); } Directives::Directives() {} @@ -63,7 +63,7 @@ Directives::Directives(const char* directives) { parse(directives); } -Directives::Directives(Directives&& directives) { +Directives::Directives(Directives&& directives) noexcept { *this = std::move(directives); } @@ -99,7 +99,7 @@ Directives& Directives::operator=(const char* s) { return *this; } -Directives& Directives::operator=(Directives&& other) { +Directives& Directives::operator=(Directives&& other) noexcept { shared = std::move(other.shared); return *this; } @@ -215,6 +215,23 @@ bool Directives::empty() const { Directives::operator bool() const { return !empty(); } +bool Directives::equals(Directives const& other) const { + return hash() == other.hash(); +} + +bool Directives::equals(String const& string) const { + auto directiveString = stringPtr(); + return directiveString ? string == *directiveString : string.empty(); +} + +bool Directives::operator==(Directives const& other) const { + return equals(other); +} + +bool Directives::operator==(String const& string) const { + return equals(string); +} + DataStream& operator>>(DataStream& ds, Directives& directives) { String string; ds.read(string); @@ -233,6 +250,18 @@ DataStream& operator<<(DataStream & ds, Directives const& directives) { return ds; } +bool operator==(Directives const& d1, Directives const& d2) { + return d1.equals(d2); +} + +bool operator==(String const& string, Directives const& directives) { + return directives.equals(string); +} + +bool operator==(Directives const& directives, String const& string) { + return directives.equals(string); +} + DirectivesGroup::DirectivesGroup() : m_count(0) {} DirectivesGroup::DirectivesGroup(String const& directives) : m_count(0) { if (directives.empty()) diff --git a/source/core/StarDirectives.hpp b/source/core/StarDirectives.hpp index ec43b9d..e272915 100644 --- a/source/core/StarDirectives.hpp +++ b/source/core/StarDirectives.hpp @@ -44,13 +44,13 @@ public: Directives(String&& directives); Directives(const char* directives); Directives(Directives const& directives); - Directives(Directives&& directives); + Directives(Directives&& directives) noexcept; ~Directives(); Directives& operator=(String const& s); Directives& operator=(String&& s); Directives& operator=(const char* s); - Directives& operator=(Directives&& other); + Directives& operator=(Directives&& other) noexcept; Directives& operator=(Directives const& other); void loadOperations() const; @@ -65,9 +65,19 @@ public: bool empty() const; operator bool() const; + bool equals(Directives const& other) const; + bool equals(String const& string) const; + + bool operator==(Directives const& other) const; + bool operator==(String const& string) const; + friend DataStream& operator>>(DataStream& ds, Directives& directives); friend DataStream& operator<<(DataStream& ds, Directives const& directives); + //friend bool operator==(Directives const& d1, String const& d2); + //friend bool operator==(Directives const& directives, String const& string); + //friend bool operator==(String const& string, Directives const& directives); + std::shared_ptr shared; };