Fix JsonVariant::memoryUsage() for raw strings

This commit is contained in:
Benoit Blanchon
2021-11-12 16:55:41 +01:00
parent 0429016ff1
commit 599e927590
3 changed files with 7 additions and 2 deletions

View File

@ -11,6 +11,7 @@ HEAD
* Add `as<JsonString>()` and `is<JsonString>()` * Add `as<JsonString>()` and `is<JsonString>()`
* Add safe bool idiom in `JsonString` * Add safe bool idiom in `JsonString`
* Remove `DeserializationError == bool` and `DeserializationError != bool` * Remove `DeserializationError == bool` and `DeserializationError != bool`
* Fix `JsonVariant::memoryUsage()` for raw strings
v6.18.5 (2021-09-28) v6.18.5 (2021-09-28)
------- -------

View File

@ -30,10 +30,12 @@ TEST_CASE("JsonVariant::memoryUsage()") {
SECTION("returns size of owned string") { SECTION("returns size of owned string") {
var.set(std::string("hello")); var.set(std::string("hello"));
REQUIRE(var.memoryUsage() == 6); REQUIRE(var.memoryUsage() == 6);
REQUIRE(var.memoryUsage() == doc.memoryUsage());
} }
SECTION("returns size of owned raw") { SECTION("returns size of owned raw") {
var.set(serialized(std::string("hello"))); var.set(serialized(std::string("hello")));
REQUIRE(var.memoryUsage() == 5); REQUIRE(var.memoryUsage() == 6);
REQUIRE(var.memoryUsage() == doc.memoryUsage());
} }
} }

View File

@ -257,7 +257,9 @@ class VariantData {
case VALUE_IS_OWNED_STRING: case VALUE_IS_OWNED_STRING:
return strlen(_content.asString) + 1; return strlen(_content.asString) + 1;
case VALUE_IS_OWNED_RAW: case VALUE_IS_OWNED_RAW:
return _content.asRaw.size; // We always add a zero at the end: the deduplication function uses it
// to detect the beginning of the next string.
return _content.asRaw.size + 1;
case VALUE_IS_OBJECT: case VALUE_IS_OBJECT:
case VALUE_IS_ARRAY: case VALUE_IS_ARRAY:
return _content.asCollection.memoryUsage(); return _content.asCollection.memoryUsage();