From 7fc8ccdf72bb5d2f9ba1b34d02109dbc97651d4e Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 21 Sep 2022 02:36:56 +0300 Subject: [PATCH] system: fix view comparison note that it is equality, not byte comparison based on memcmp or strcmp return values --- code/espurna/main.cpp | 8 +++++--- code/espurna/types.h | 6 +++--- code/test/unit/src/terminal/terminal.cpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/code/espurna/main.cpp b/code/espurna/main.cpp index 22b90e4f..c89a6111 100644 --- a/code/espurna/main.cpp +++ b/code/espurna/main.cpp @@ -308,15 +308,17 @@ void setup() { } // namespace -bool StringView::compare(StringView other) const { +bool StringView::equals(StringView other) const { if (other._len == _len) { - if (inFlash(_ptr)) { + if (inFlash(_ptr) && inFlash(other._ptr)) { + return _ptr == other._ptr; + } else if (inFlash(_ptr)) { return memcmp_P(other._ptr, _ptr, _len) == 0; } else if (inFlash(other._ptr)) { return memcmp_P(_ptr, other._ptr, _len) == 0; } - return __builtin_memcmp(_ptr, other._ptr, _len); + return __builtin_memcmp(_ptr, other._ptr, _len) == 0; } return false; diff --git a/code/espurna/types.h b/code/espurna/types.h index debcf1b9..c1d9a5ed 100644 --- a/code/espurna/types.h +++ b/code/espurna/types.h @@ -161,7 +161,7 @@ struct StringView { return toString(); } - bool compare(StringView other) const; + bool equals(StringView other) const; private: static bool inFlash(const char* ptr) { @@ -177,11 +177,11 @@ private: }; inline bool operator==(StringView lhs, StringView rhs) { - return lhs.compare(rhs); + return lhs.equals(rhs); } inline bool operator!=(StringView lhs, StringView rhs) { - return !lhs.compare(rhs); + return !lhs.equals(rhs); } inline String operator+(String&& lhs, StringView rhs) { diff --git a/code/test/unit/src/terminal/terminal.cpp b/code/test/unit/src/terminal/terminal.cpp index 0c15f593..dc6298fd 100644 --- a/code/test/unit/src/terminal/terminal.cpp +++ b/code/test/unit/src/terminal/terminal.cpp @@ -8,7 +8,7 @@ namespace espurna { // no special cases for flash strings -bool StringView::compare(espurna::StringView other) const { +bool StringView::equals(espurna::StringView other) const { return _ptr == other._ptr || (_len == other._len && (0 == __builtin_memcmp(_ptr, other._ptr, _len))); }