JsonArray::remove() and JsonObject::remove() now release the memory of strings

This commit is contained in:
Benoit Blanchon
2018-11-09 17:27:32 +01:00
parent e842838a23
commit f375459d53
68 changed files with 1504 additions and 740 deletions

View File

@@ -11,25 +11,43 @@ namespace ARDUINOJSON_NAMESPACE {
class StringBuilder {
public:
explicit StringBuilder(MemoryPool* parent)
: _parent(parent), _start(0), _size(0) {
_start = _parent->alloc(1);
typedef StringInMemoryPool StringType;
explicit StringBuilder(MemoryPool* parent) : _parent(parent), _size(0) {
_slot = _parent->allocExpandableString();
}
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) {
_start = _parent->realloc(_start, _size + 1, _size + 2);
if (_start) _start[_size++] = c;
if (!_slot) return;
if (_size >= _slot->size) {
_slot = _parent->expandString(_slot);
if (!_slot) return;
}
_slot->value[_size++] = c;
}
StringInMemoryPool complete() {
if (_start) _start[_size] = 0;
return _start;
StringType complete() {
append('\0');
if (_slot) {
_parent->freezeString(_slot, _size);
}
return _slot;
}
private:
MemoryPool* _parent;
char* _start;
size_t _size;
StringSlot* _slot;
};
} // namespace ARDUINOJSON_NAMESPACE