mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	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 `JsonString` operator `==` and `!=` for non-zero-terminated string | ||||
|  | ||||
| v6.19.2 (2022-02-14) | ||||
| ------- | ||||
|   | ||||
| @@ -14,44 +14,53 @@ TEST_CASE("JsonString") { | ||||
|     CHECK(s.isNull() == true); | ||||
|     CHECK(s.c_str() == 0); | ||||
|     CHECK(s.isLinked() == true); | ||||
|     CHECK(s == JsonString()); | ||||
|     CHECK(s != ""); | ||||
|   } | ||||
|  | ||||
|   SECTION("Compare null with boolean") { | ||||
|   SECTION("Null converts to false") { | ||||
|     JsonString s; | ||||
|  | ||||
|     CHECK(bool(s) == false); | ||||
|     CHECK(false == bool(s)); | ||||
|     CHECK(bool(s) != true); | ||||
|     CHECK(true != bool(s)); | ||||
|   } | ||||
|  | ||||
|   SECTION("Compare non-null with boolean") { | ||||
|     JsonString s("hello"); | ||||
|   SECTION("Empty string converts to true") { | ||||
|     JsonString s(""); | ||||
|  | ||||
|     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; | ||||
|  | ||||
|     CHECK(a == b); | ||||
|     CHECK_FALSE(a != b); | ||||
|   } | ||||
|  | ||||
|   SECTION("Compare null with non-null") { | ||||
|     JsonString a(0), b("hello"); | ||||
|   SECTION("Null and empty strings differ") { | ||||
|     JsonString a, b(""); | ||||
|  | ||||
|     CHECK_FALSE(a == b); | ||||
|     CHECK(a != b); | ||||
|  | ||||
|     CHECK_FALSE(b == a); | ||||
|     CHECK(b != a); | ||||
|   } | ||||
|  | ||||
|   SECTION("Compare non-null with null") { | ||||
|     JsonString a("hello"), b(0); | ||||
|   SECTION("Null and non-empty strings differ") { | ||||
|     JsonString a, b("hello"); | ||||
|  | ||||
|     CHECK_FALSE(a == b); | ||||
|     CHECK(a != b); | ||||
|  | ||||
|     CHECK_FALSE(b == a); | ||||
|     CHECK(b != a); | ||||
|   } | ||||
|  | ||||
|   SECTION("Compare different strings") { | ||||
| @@ -88,5 +97,7 @@ TEST_CASE("JsonString") { | ||||
|  | ||||
|     CHECK(s.size() == 5); | ||||
|     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) { | ||||
|     if (lhs._size != rhs._size) | ||||
|       return false; | ||||
|     if (lhs._data == rhs._data) | ||||
|       return true; | ||||
|     if (!lhs._data) | ||||
|       return false; | ||||
|     if (!rhs._data) | ||||
|       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) { | ||||
|     if (lhs._data == rhs._data) | ||||
|       return false; | ||||
|     if (!lhs._data) | ||||
|       return true; | ||||
|     if (!rhs._data) | ||||
|       return true; | ||||
|     return strcmp(lhs._data, rhs._data) != 0; | ||||
|     return !(lhs == rhs); | ||||
|   } | ||||
|  | ||||
| #if ARDUINOJSON_ENABLE_STD_STREAM | ||||
|   | ||||
		Reference in New Issue
	
	Block a user