Change StringBuilder::save() to take a VariantData*

This commit is contained in:
Benoit Blanchon
2025-02-28 09:59:46 +01:00
parent b06cee8f4d
commit 05b68fc7cc
4 changed files with 60 additions and 36 deletions

View File

@ -6,6 +6,7 @@
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"
#include "Literals.hpp"
using namespace ArduinoJson::detail; using namespace ArduinoJson::detail;
@ -16,9 +17,10 @@ TEST_CASE("StringBuilder") {
SECTION("Empty string") { SECTION("Empty string") {
StringBuilder str(&resources); StringBuilder str(&resources);
VariantData data;
str.startString(); str.startString();
str.save(); str.save(&data);
REQUIRE(resources.size() == sizeofString("")); REQUIRE(resources.size() == sizeofString(""));
REQUIRE(resources.overflowed() == false); REQUIRE(resources.overflowed() == false);
@ -96,48 +98,69 @@ TEST_CASE("StringBuilder") {
} }
} }
static StringNode* addStringToPool(ResourceManager& resources, const char* s) { static const char* saveString(StringBuilder& builder, const char* s) {
StringBuilder str(&resources); VariantData data;
str.startString(); builder.startString();
str.append(s); builder.append(s);
return str.save(); builder.save(&data);
return data.asString().c_str();
} }
TEST_CASE("StringBuilder::save() deduplicates strings") { TEST_CASE("StringBuilder::save() deduplicates strings") {
ResourceManager resources; SpyingAllocator spy;
ResourceManager resources(&spy);
StringBuilder builder(&resources);
SECTION("Basic") { SECTION("Basic") {
auto s1 = addStringToPool(resources, "hello"); auto s1 = saveString(builder, "hello");
auto s2 = addStringToPool(resources, "world"); auto s2 = saveString(builder, "world");
auto s3 = addStringToPool(resources, "hello"); auto s3 = saveString(builder, "hello");
REQUIRE(s1 == s3); REQUIRE(s1 == "hello"_s);
REQUIRE(s2 != s3); REQUIRE(s2 == "world"_s);
REQUIRE(s1->references == 2); REQUIRE(+s1 == +s3); // same address
REQUIRE(s2->references == 1);
REQUIRE(s3->references == 2); REQUIRE(spy.log() ==
REQUIRE(resources.size() == sizeofString("hello") + sizeofString("world")); AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")),
Allocate(sizeofStringBuffer()),
});
} }
SECTION("Requires terminator") { SECTION("Requires terminator") {
auto s1 = addStringToPool(resources, "hello world"); auto s1 = saveString(builder, "hello world");
auto s2 = addStringToPool(resources, "hello"); auto s2 = saveString(builder, "hello");
REQUIRE(s2 != s1); REQUIRE(s1 == "hello world"_s);
REQUIRE(s1->references == 1); REQUIRE(s2 == "hello"_s);
REQUIRE(s2->references == 1); REQUIRE(+s2 != +s1); // different address
REQUIRE(resources.size() ==
sizeofString("hello world") + sizeofString("hello")); REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
});
} }
SECTION("Don't overrun") { SECTION("Don't overrun") {
auto s1 = addStringToPool(resources, "hello world"); auto s1 = saveString(builder, "hello world");
auto s2 = addStringToPool(resources, "wor"); auto s2 = saveString(builder, "wor");
REQUIRE(s1 == "hello world"_s);
REQUIRE(s2 == "wor"_s);
REQUIRE(s2 != s1); REQUIRE(s2 != s1);
REQUIRE(s1->references == 1);
REQUIRE(s2->references == 1); REQUIRE(spy.log() ==
REQUIRE(resources.size() == AllocatorLog{
sizeofString("hello world") + sizeofString("wor")); Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("wor")),
});
} }
} }

View File

@ -279,7 +279,7 @@ class JsonDeserializer {
if (!keyVariant) if (!keyVariant)
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
keyVariant->setOwnedString(stringBuilder_.save()); stringBuilder_.save(keyVariant);
} else { } else {
member->clear(resources_); member->clear(resources_);
} }
@ -388,7 +388,7 @@ class JsonDeserializer {
if (err) if (err)
return err; return err;
variant.setOwnedString(stringBuilder_.save()); stringBuilder_.save(&variant);
return DeserializationError::Ok; return DeserializationError::Ok;
} }

View File

@ -25,7 +25,8 @@ class StringBuilder {
node_ = resources_->createString(initialCapacity); node_ = resources_->createString(initialCapacity);
} }
StringNode* save() { void save(VariantData* variant) {
ARDUINOJSON_ASSERT(variant != nullptr);
ARDUINOJSON_ASSERT(node_ != nullptr); ARDUINOJSON_ASSERT(node_ != nullptr);
node_->data[size_] = 0; node_->data[size_] = 0;
StringNode* node = resources_->getString(adaptString(node_->data, size_)); StringNode* node = resources_->getString(adaptString(node_->data, size_));
@ -37,7 +38,7 @@ class StringBuilder {
} else { } else {
node->references++; node->references++;
} }
return node; variant->setOwnedString(node);
} }
void append(const char* s) { void append(const char* s) {

View File

@ -230,9 +230,9 @@ class StringBuilderPrint : public Print {
copier_.startString(); copier_.startString();
} }
StringNode* save() { void save(VariantData* data) {
ARDUINOJSON_ASSERT(!overflowed()); ARDUINOJSON_ASSERT(!overflowed());
return copier_.save(); copier_.save(data);
} }
size_t write(uint8_t c) { size_t write(uint8_t c) {
@ -268,7 +268,7 @@ inline void convertToJson(const ::Printable& src, JsonVariant dst) {
src.printTo(print); src.printTo(print);
if (print.overflowed()) if (print.overflowed())
return; return;
data->setOwnedString(print.save()); print.save(data);
} }
#endif #endif