system: fix view comparison

note that it is equality, not byte comparison based on
memcmp or strcmp return values
This commit is contained in:
Maxim Prokhorov
2022-09-21 02:36:56 +03:00
parent 009d1b0014
commit 7fc8ccdf72
3 changed files with 9 additions and 7 deletions

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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)));
}