Browse Source

system: fix view comparison

note that it is equality, not byte comparison based on
memcmp or strcmp return values
pull/2552/head
Maxim Prokhorov 1 year ago
parent
commit
7fc8ccdf72
3 changed files with 9 additions and 7 deletions
  1. +5
    -3
      code/espurna/main.cpp
  2. +3
    -3
      code/espurna/types.h
  3. +1
    -1
      code/test/unit/src/terminal/terminal.cpp

+ 5
- 3
code/espurna/main.cpp 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;


+ 3
- 3
code/espurna/types.h 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) {


+ 1
- 1
code/test/unit/src/terminal/terminal.cpp 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)));
}


Loading…
Cancel
Save