mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Changed the return type of strdup() to const char* (issue #658)
				
					
				
			This commit is contained in:
		| @@ -5,6 +5,7 @@ HEAD | ||||
| ---- | ||||
|  | ||||
| * Changed the rules of string duplication (issue #658) | ||||
| * Changed the return type of `strdup()` to `const char*` to prevent double duplication | ||||
|  | ||||
| > ### New rules for string duplication | ||||
| > | ||||
|   | ||||
| @@ -38,19 +38,19 @@ class JsonBuffer : Internals::NonCopyable { | ||||
|  | ||||
|   // Duplicates a string | ||||
|   // | ||||
|   // char* strdup(TValue); | ||||
|   // const char* strdup(TValue); | ||||
|   // TValue = const std::string&, const String&, | ||||
|   template <typename TString> | ||||
|   typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value, | ||||
|                                 char *>::type | ||||
|                                 const char *>::type | ||||
|   strdup(const TString &src) { | ||||
|     return Internals::StringTraits<TString>::duplicate(src, this); | ||||
|   } | ||||
|   // | ||||
|   // char* strdup(TValue); | ||||
|   // const char* strdup(TValue); | ||||
|   // TValue = const char*, const char[N], const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   char *strdup(const TString *src) { | ||||
|   const char *strdup(const TString *src) { | ||||
|     return Internals::StringTraits<const TString *>::duplicate(src, this); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -10,14 +10,14 @@ TEST_CASE("DynamicJsonBuffer::strdup()") { | ||||
|  | ||||
|   SECTION("Should return a copy") { | ||||
|     char original[] = "hello"; | ||||
|     char* copy = buffer.strdup(original); | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     strcpy(original, "world"); | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
|  | ||||
|   SECTION("Given NULL, return NULL") { | ||||
|     const char* original = NULL; | ||||
|     char* copy = buffer.strdup(original); | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     REQUIRE(0 == copy); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -243,7 +243,7 @@ TEST_CASE("std::string") { | ||||
|  | ||||
|   SECTION("JsonBuffer_strdup") { | ||||
|     std::string original("hello"); | ||||
|     char *copy = jb.strdup(original); | ||||
|     const char *copy = jb.strdup(original); | ||||
|     original[0] = 'w'; | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user