Rename StringCopier to StringBuilder

This commit is contained in:
Benoit Blanchon
2023-05-10 10:12:55 +02:00
parent ff0deee793
commit 044a4753d2
7 changed files with 37 additions and 37 deletions

View File

@ -7,7 +7,7 @@ add_executable(MemoryPoolTests
clear.cpp clear.cpp
saveString.cpp saveString.cpp
size.cpp size.cpp
StringCopier.cpp StringBuilder.cpp
) )
add_test(MemoryPool MemoryPoolTests) add_test(MemoryPool MemoryPoolTests)

View File

@ -2,20 +2,20 @@
// Copyright © 2014-2023, Benoit BLANCHON // Copyright © 2014-2023, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/StringStorage/StringCopier.hpp> #include <ArduinoJson/Memory/StringBuilder.hpp>
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"
using namespace ArduinoJson::detail; using namespace ArduinoJson::detail;
TEST_CASE("StringCopier") { TEST_CASE("StringBuilder") {
ControllableAllocator controllableAllocator; ControllableAllocator controllableAllocator;
SpyingAllocator spyingAllocator(&controllableAllocator); SpyingAllocator spyingAllocator(&controllableAllocator);
MemoryPool pool(0, &spyingAllocator); MemoryPool pool(0, &spyingAllocator);
SECTION("Empty string") { SECTION("Empty string") {
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
str.save(); str.save();
@ -29,7 +29,7 @@ TEST_CASE("StringCopier") {
} }
SECTION("Short string fits in first allocation") { SECTION("Short string fits in first allocation") {
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
str.append("hello"); str.append("hello");
@ -42,7 +42,7 @@ TEST_CASE("StringCopier") {
} }
SECTION("Long string needs reallocation") { SECTION("Long string needs reallocation") {
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
str.append( str.append(
@ -63,7 +63,7 @@ TEST_CASE("StringCopier") {
} }
SECTION("Realloc fails") { SECTION("Realloc fails") {
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
controllableAllocator.disable(); controllableAllocator.disable();
@ -81,7 +81,7 @@ TEST_CASE("StringCopier") {
} }
SECTION("Initial allocation fails") { SECTION("Initial allocation fails") {
StringCopier str(&pool); StringBuilder str(&pool);
controllableAllocator.disable(); controllableAllocator.disable();
str.startString(); str.startString();
@ -94,13 +94,13 @@ TEST_CASE("StringCopier") {
} }
static StringNode* addStringToPool(MemoryPool& pool, const char* s) { static StringNode* addStringToPool(MemoryPool& pool, const char* s) {
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
str.append(s); str.append(s);
return str.save(); return str.save();
} }
TEST_CASE("StringCopier::save() deduplicates strings") { TEST_CASE("StringBuilder::save() deduplicates strings") {
MemoryPool pool(4096); MemoryPool pool(4096);
SECTION("Basic") { SECTION("Basic") {

View File

@ -11,7 +11,7 @@ using namespace ArduinoJson::detail;
static void testCodepoint(uint32_t codepoint, std::string expected) { static void testCodepoint(uint32_t codepoint, std::string expected) {
MemoryPool pool(4096); MemoryPool pool(4096);
StringCopier str(&pool); StringBuilder str(&pool);
str.startString(); str.startString();
CAPTURE(codepoint); CAPTURE(codepoint);

View File

@ -22,7 +22,7 @@ template <typename TReader>
class JsonDeserializer { class JsonDeserializer {
public: public:
JsonDeserializer(MemoryPool* pool, TReader reader) JsonDeserializer(MemoryPool* pool, TReader reader)
: stringStorage_(pool), : stringBuilder_(pool),
foundSomething_(false), foundSomething_(false),
latch_(reader), latch_(reader),
pool_(pool) {} pool_(pool) {}
@ -268,7 +268,7 @@ class JsonDeserializer {
if (!eat(':')) if (!eat(':'))
return DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
JsonString key = stringStorage_.str(); JsonString key = stringBuilder_.str();
TFilter memberFilter = filter[key.c_str()]; TFilter memberFilter = filter[key.c_str()];
@ -276,7 +276,7 @@ class JsonDeserializer {
VariantSlot* slot = object.get(adaptString(key.c_str())); VariantSlot* slot = object.get(adaptString(key.c_str()));
if (!slot) { if (!slot) {
// Save key in memory pool. // Save key in memory pool.
auto savedKey = stringStorage_.save(); auto savedKey = stringBuilder_.save();
// Allocate slot in object // Allocate slot in object
slot = pool_->allocVariant(); slot = pool_->allocVariant();
@ -375,7 +375,7 @@ class JsonDeserializer {
} }
DeserializationError::Code parseKey() { DeserializationError::Code parseKey() {
stringStorage_.startString(); stringBuilder_.startString();
if (isQuote(current())) { if (isQuote(current())) {
return parseQuotedString(); return parseQuotedString();
} else { } else {
@ -386,13 +386,13 @@ class JsonDeserializer {
DeserializationError::Code parseStringValue(VariantData& variant) { DeserializationError::Code parseStringValue(VariantData& variant) {
DeserializationError::Code err; DeserializationError::Code err;
stringStorage_.startString(); stringBuilder_.startString();
err = parseQuotedString(); err = parseQuotedString();
if (err) if (err)
return err; return err;
variant.setString(stringStorage_.save()); variant.setString(stringBuilder_.save());
return DeserializationError::Ok; return DeserializationError::Ok;
} }
@ -428,9 +428,9 @@ class JsonDeserializer {
if (err) if (err)
return err; return err;
if (codepoint.append(codeunit)) if (codepoint.append(codeunit))
Utf8::encodeCodepoint(codepoint.value(), stringStorage_); Utf8::encodeCodepoint(codepoint.value(), stringBuilder_);
#else #else
stringStorage_.append('\\'); stringBuilder_.append('\\');
#endif #endif
continue; continue;
} }
@ -442,10 +442,10 @@ class JsonDeserializer {
move(); move();
} }
stringStorage_.append(c); stringBuilder_.append(c);
} }
if (!stringStorage_.isValid()) if (!stringBuilder_.isValid())
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
return DeserializationError::Ok; return DeserializationError::Ok;
@ -458,14 +458,14 @@ class JsonDeserializer {
if (canBeInNonQuotedString(c)) { // no quotes if (canBeInNonQuotedString(c)) { // no quotes
do { do {
move(); move();
stringStorage_.append(c); stringBuilder_.append(c);
c = current(); c = current();
} while (canBeInNonQuotedString(c)); } while (canBeInNonQuotedString(c));
} else { } else {
return DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
} }
if (!stringStorage_.isValid()) if (!stringBuilder_.isValid())
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
return DeserializationError::Ok; return DeserializationError::Ok;
@ -657,7 +657,7 @@ class JsonDeserializer {
return DeserializationError::Ok; return DeserializationError::Ok;
} }
StringCopier stringStorage_; StringBuilder stringBuilder_;
bool foundSomething_; bool foundSomething_;
Latch<TReader> latch_; Latch<TReader> latch_;
MemoryPool* pool_; MemoryPool* pool_;

View File

@ -8,13 +8,13 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
class StringCopier { class StringBuilder {
public: public:
static const size_t initialCapacity = 31; static const size_t initialCapacity = 31;
StringCopier(MemoryPool* pool) : pool_(pool) {} StringBuilder(MemoryPool* pool) : pool_(pool) {}
~StringCopier() { ~StringBuilder() {
if (node_) if (node_)
pool_->deallocString(node_); pool_->deallocString(node_);
} }

View File

@ -19,7 +19,7 @@ class MsgPackDeserializer {
MsgPackDeserializer(MemoryPool* pool, TReader reader) MsgPackDeserializer(MemoryPool* pool, TReader reader)
: pool_(pool), : pool_(pool),
reader_(reader), reader_(reader),
stringStorage_(pool), stringBuilder_(pool),
foundSomething_(false) {} foundSomething_(false) {}
template <typename TFilter> template <typename TFilter>
@ -370,14 +370,14 @@ class MsgPackDeserializer {
if (err) if (err)
return err; return err;
variant->setString(stringStorage_.save()); variant->setString(stringBuilder_.save());
return DeserializationError::Ok; return DeserializationError::Ok;
} }
DeserializationError::Code readString(size_t n) { DeserializationError::Code readString(size_t n) {
DeserializationError::Code err; DeserializationError::Code err;
stringStorage_.startString(); stringBuilder_.startString();
for (; n; --n) { for (; n; --n) {
uint8_t c; uint8_t c;
@ -385,10 +385,10 @@ class MsgPackDeserializer {
if (err) if (err)
return err; return err;
stringStorage_.append(static_cast<char>(c)); stringBuilder_.append(static_cast<char>(c));
} }
if (!stringStorage_.isValid()) if (!stringBuilder_.isValid())
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
return DeserializationError::Ok; return DeserializationError::Ok;
@ -485,7 +485,7 @@ class MsgPackDeserializer {
if (err) if (err)
return err; return err;
JsonString key = stringStorage_.str(); JsonString key = stringBuilder_.str();
TFilter memberFilter = filter[key.c_str()]; TFilter memberFilter = filter[key.c_str()];
VariantData* member; VariantData* member;
@ -493,7 +493,7 @@ class MsgPackDeserializer {
ARDUINOJSON_ASSERT(object != 0); ARDUINOJSON_ASSERT(object != 0);
// Save key in memory pool. // Save key in memory pool.
auto savedKey = stringStorage_.save(); auto savedKey = stringBuilder_.save();
VariantSlot* slot = pool_->allocVariant(); VariantSlot* slot = pool_->allocVariant();
if (!slot) if (!slot)
@ -555,7 +555,7 @@ class MsgPackDeserializer {
MemoryPool* pool_; MemoryPool* pool_;
TReader reader_; TReader reader_;
StringCopier stringStorage_; StringBuilder stringBuilder_;
bool foundSomething_; bool foundSomething_;
}; };

View File

@ -5,7 +5,7 @@
#pragma once #pragma once
#include <ArduinoJson/Json/JsonSerializer.hpp> #include <ArduinoJson/Json/JsonSerializer.hpp>
#include <ArduinoJson/StringStorage/StringCopier.hpp> #include <ArduinoJson/Memory/StringBuilder.hpp>
#include <ArduinoJson/Variant/JsonVariantConst.hpp> #include <ArduinoJson/Variant/JsonVariantConst.hpp>
#include <ArduinoJson/Variant/VariantFunctions.hpp> #include <ArduinoJson/Variant/VariantFunctions.hpp>
@ -212,7 +212,7 @@ class MemoryPoolPrint : public Print {
} }
private: private:
StringCopier copier_; StringBuilder copier_;
}; };
} // namespace detail } // namespace detail