Add equality operator for Directives (fixes networking bug)

This commit is contained in:
Kae 2024-03-22 19:12:45 +11:00
parent 9b10964a3e
commit e318098f0b
2 changed files with 44 additions and 5 deletions

View File

@ -46,7 +46,7 @@ Directives::Shared::Shared(List<Entry>&& 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())

View File

@ -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 const> shared;
};