Refactored StringBuilder into StringStorage

This commit is contained in:
Benoit Blanchon
2020-07-11 17:51:39 +02:00
parent 04c59985a1
commit 1600d39693
13 changed files with 128 additions and 193 deletions

View File

@@ -5,25 +5,51 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Memory/StringBuilder.hpp>
namespace ARDUINOJSON_NAMESPACE {
class StringCopier {
public:
typedef ARDUINOJSON_NAMESPACE::StringBuilder StringBuilder;
StringCopier(MemoryPool* pool) : _pool(pool) {}
StringBuilder startString() {
return StringBuilder(_pool);
void startString(MemoryPool* pool) {
_slot = pool->allocExpandableString();
_size = 0;
}
void reclaim(const char* s) {
_pool->reclaimLastString(s);
void commit(MemoryPool* pool) {
ARDUINOJSON_ASSERT(_slot.value);
pool->freezeString(_slot, _size);
}
void append(const char* s) {
while (*s) append(*s++);
}
void append(const char* s, size_t n) {
while (n-- > 0) append(*s++);
}
void append(char c) {
if (!_slot.value)
return;
if (_size >= _slot.size) {
_slot.value = 0;
return;
}
_slot.value[_size++] = c;
}
bool isValid() {
return _slot.value != 0;
}
const char* c_str() {
return _slot.value;
}
private:
MemoryPool* _pool;
size_t _size;
StringSlot _slot;
};
} // namespace ARDUINOJSON_NAMESPACE