Skip to content

Commit da21f46

Browse files
committed
Update StringBuilder
1 parent 3104d77 commit da21f46

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

extras/tests/JsonDeserializer/object.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,23 @@ TEST_CASE("deserialize JSON object") {
292292
}
293293

294294
SECTION("Repeated key") {
295-
DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
295+
DeserializationError err =
296+
deserializeJson(doc, "{alfa:{bravo:{charlie:1}},alfa:2}");
296297

297298
REQUIRE(err == DeserializationError::Ok);
298-
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
299+
REQUIRE(doc.as<std::string>() == "{\"alfa\":2}");
299300
REQUIRE(spy.log() ==
300301
AllocatorLog{
301302
Allocate(sizeofStringBuffer()),
302303
Allocate(sizeofPool()),
303-
Reallocate(sizeofStringBuffer(), sizeofString("a")),
304+
Reallocate(sizeofStringBuffer(), sizeofString("alfa")),
304305
Allocate(sizeofStringBuffer()),
305-
Reallocate(sizeofStringBuffer(), sizeofString("b")),
306+
Reallocate(sizeofStringBuffer(), sizeofString("bravo")),
306307
Allocate(sizeofStringBuffer()),
307-
Reallocate(sizeofStringBuffer(), sizeofString("c")),
308+
Reallocate(sizeofStringBuffer(), sizeofString("charlie")),
308309
Allocate(sizeofStringBuffer()),
309-
Deallocate(sizeofString("b")),
310-
Deallocate(sizeofString("c")),
310+
Deallocate(sizeofString("bravo")),
311+
Deallocate(sizeofString("charlie")),
311312
Deallocate(sizeofStringBuffer()),
312313
Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
313314
});
@@ -389,11 +390,11 @@ TEST_CASE("deserialize JSON object under memory constraints") {
389390

390391
SECTION("string allocation fails") {
391392
timebomb.setCountdown(3);
392-
char input[] = "{\"a\":\"b\"}";
393+
char input[] = "{\"alfa\":\"bravo\"}";
393394

394395
DeserializationError err = deserializeJson(doc, input);
395396

396397
REQUIRE(err == DeserializationError::NoMemory);
397-
REQUIRE(doc.as<std::string>() == "{\"a\":null}");
398+
REQUIRE(doc.as<std::string>() == "{\"alfa\":null}");
398399
}
399400
}

extras/tests/ResourceManager/StringBuilder.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@ TEST_CASE("StringBuilder") {
2222
str.startString();
2323
str.save(&data);
2424

25-
REQUIRE(resources.size() == sizeofString(""));
2625
REQUIRE(resources.overflowed() == false);
27-
REQUIRE(spyingAllocator.log() ==
28-
AllocatorLog{
29-
Allocate(sizeofStringBuffer()),
30-
Reallocate(sizeofStringBuffer(), sizeofString("")),
31-
});
26+
REQUIRE(spyingAllocator.log() == AllocatorLog{
27+
Allocate(sizeofStringBuffer()),
28+
});
29+
REQUIRE(data.type() == VariantType::TinyString);
30+
}
31+
32+
SECTION("Tiny string") {
33+
StringBuilder str(&resources);
34+
35+
str.startString();
36+
str.append("url");
37+
38+
REQUIRE(str.isValid() == true);
39+
REQUIRE(str.str() == "url");
40+
REQUIRE(spyingAllocator.log() == AllocatorLog{
41+
Allocate(sizeofStringBuffer()),
42+
});
43+
44+
VariantData data;
45+
str.save(&data);
46+
47+
REQUIRE(resources.overflowed() == false);
48+
REQUIRE(data.type() == VariantType::TinyString);
49+
REQUIRE(data.asString() == "url");
3250
}
3351

3452
SECTION("Short string fits in first allocation") {
@@ -149,18 +167,18 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
149167

150168
SECTION("Don't overrun") {
151169
auto s1 = saveString(builder, "hello world");
152-
auto s2 = saveString(builder, "wor");
170+
auto s2 = saveString(builder, "worl");
153171

154172
REQUIRE(s1 == "hello world"_s);
155-
REQUIRE(s2 == "wor"_s);
173+
REQUIRE(s2 == "worl"_s);
156174
REQUIRE(s2 != s1);
157175

158176
REQUIRE(spy.log() ==
159177
AllocatorLog{
160178
Allocate(sizeofStringBuffer()),
161179
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
162180
Allocate(sizeofStringBuffer()),
163-
Reallocate(sizeofStringBuffer(), sizeofString("wor")),
181+
Reallocate(sizeofStringBuffer(), sizeofString("worl")),
164182
});
165183
}
166184
}

src/ArduinoJson/Memory/StringBuilder.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ class StringBuilder {
2828
void save(VariantData* variant) {
2929
ARDUINOJSON_ASSERT(variant != nullptr);
3030
ARDUINOJSON_ASSERT(node_ != nullptr);
31-
node_->data[size_] = 0;
32-
StringNode* node = resources_->getString(adaptString(node_->data, size_));
31+
32+
char* p = node_->data;
33+
if (isTinyString(p, size_)) {
34+
variant->setTinyString(p, static_cast<uint8_t>(size_));
35+
return;
36+
}
37+
38+
p[size_] = 0;
39+
StringNode* node = resources_->getString(adaptString(p, size_));
3340
if (!node) {
3441
node = resources_->resizeString(node_, size_);
3542
ARDUINOJSON_ASSERT(node != nullptr); // realloc to smaller can't fail

0 commit comments

Comments
 (0)