forked from bblanchon/ArduinoJson
Fix JsonString
operator ==
and !=
for non-zero-terminated string
This commit is contained in:
@ -5,6 +5,7 @@ HEAD
|
|||||||
----
|
----
|
||||||
|
|
||||||
* Fix `call of overloaded 'String(const char*, int)' is ambiguous`
|
* Fix `call of overloaded 'String(const char*, int)' is ambiguous`
|
||||||
|
* Fix `JsonString` operator `==` and `!=` for non-zero-terminated string
|
||||||
|
|
||||||
v6.19.2 (2022-02-14)
|
v6.19.2 (2022-02-14)
|
||||||
-------
|
-------
|
||||||
|
@ -14,44 +14,53 @@ TEST_CASE("JsonString") {
|
|||||||
CHECK(s.isNull() == true);
|
CHECK(s.isNull() == true);
|
||||||
CHECK(s.c_str() == 0);
|
CHECK(s.c_str() == 0);
|
||||||
CHECK(s.isLinked() == true);
|
CHECK(s.isLinked() == true);
|
||||||
|
CHECK(s == JsonString());
|
||||||
|
CHECK(s != "");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare null with boolean") {
|
SECTION("Null converts to false") {
|
||||||
JsonString s;
|
JsonString s;
|
||||||
|
|
||||||
CHECK(bool(s) == false);
|
CHECK(bool(s) == false);
|
||||||
CHECK(false == bool(s));
|
|
||||||
CHECK(bool(s) != true);
|
|
||||||
CHECK(true != bool(s));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare non-null with boolean") {
|
SECTION("Empty string converts to true") {
|
||||||
JsonString s("hello");
|
JsonString s("");
|
||||||
|
|
||||||
CHECK(bool(s) == true);
|
CHECK(bool(s) == true);
|
||||||
CHECK(true == bool(s));
|
|
||||||
CHECK(bool(s) != false);
|
|
||||||
CHECK(false != bool(s));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare null with null") {
|
SECTION("Non-empty string converts to true") {
|
||||||
|
JsonString s("");
|
||||||
|
|
||||||
|
CHECK(bool(s) == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Null strings equals each others") {
|
||||||
JsonString a, b;
|
JsonString a, b;
|
||||||
|
|
||||||
CHECK(a == b);
|
CHECK(a == b);
|
||||||
CHECK_FALSE(a != b);
|
CHECK_FALSE(a != b);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare null with non-null") {
|
SECTION("Null and empty strings differ") {
|
||||||
JsonString a(0), b("hello");
|
JsonString a, b("");
|
||||||
|
|
||||||
CHECK_FALSE(a == b);
|
CHECK_FALSE(a == b);
|
||||||
CHECK(a != b);
|
CHECK(a != b);
|
||||||
|
|
||||||
|
CHECK_FALSE(b == a);
|
||||||
|
CHECK(b != a);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare non-null with null") {
|
SECTION("Null and non-empty strings differ") {
|
||||||
JsonString a("hello"), b(0);
|
JsonString a, b("hello");
|
||||||
|
|
||||||
CHECK_FALSE(a == b);
|
CHECK_FALSE(a == b);
|
||||||
CHECK(a != b);
|
CHECK(a != b);
|
||||||
|
|
||||||
|
CHECK_FALSE(b == a);
|
||||||
|
CHECK(b != a);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Compare different strings") {
|
SECTION("Compare different strings") {
|
||||||
@ -88,5 +97,7 @@ TEST_CASE("JsonString") {
|
|||||||
|
|
||||||
CHECK(s.size() == 5);
|
CHECK(s.size() == 5);
|
||||||
CHECK(s.isLinked() == true);
|
CHECK(s.isLinked() == true);
|
||||||
|
CHECK(s == "hello");
|
||||||
|
CHECK(s != "hello world");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,23 +46,19 @@ class String : public SafeBoolIdom<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator==(String lhs, String rhs) {
|
friend bool operator==(String lhs, String rhs) {
|
||||||
|
if (lhs._size != rhs._size)
|
||||||
|
return false;
|
||||||
if (lhs._data == rhs._data)
|
if (lhs._data == rhs._data)
|
||||||
return true;
|
return true;
|
||||||
if (!lhs._data)
|
if (!lhs._data)
|
||||||
return false;
|
return false;
|
||||||
if (!rhs._data)
|
if (!rhs._data)
|
||||||
return false;
|
return false;
|
||||||
return strcmp(lhs._data, rhs._data) == 0;
|
return memcmp(lhs._data, rhs._data, lhs._size) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator!=(String lhs, String rhs) {
|
friend bool operator!=(String lhs, String rhs) {
|
||||||
if (lhs._data == rhs._data)
|
return !(lhs == rhs);
|
||||||
return false;
|
|
||||||
if (!lhs._data)
|
|
||||||
return true;
|
|
||||||
if (!rhs._data)
|
|
||||||
return true;
|
|
||||||
return strcmp(lhs._data, rhs._data) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||||
|
Reference in New Issue
Block a user