Skip to content

Commit

Permalink
Remove string size from VariantContent (#1650)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed May 2, 2023
1 parent 167ea08 commit 9321f8f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
6 changes: 2 additions & 4 deletions src/ArduinoJson/Variant/VariantContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ union VariantContent {
JsonUInt asUnsignedInteger;
JsonInteger asSignedInteger;
CollectionData asCollection;
struct {
const char* data;
size_t size;
} asString;
const char* asLinkedString;
struct StringNode* asOwnedString;
};

ARDUINOJSON_END_PRIVATE_NAMESPACE
24 changes: 12 additions & 12 deletions src/ArduinoJson/Variant/VariantData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ class VariantData {
return visitor.visitObject(content_.asCollection);

case VALUE_IS_LINKED_STRING:
return visitor.visitString(content_.asLinkedString,
strlen(content_.asLinkedString));

case VALUE_IS_OWNED_STRING:
return visitor.visitString(content_.asString.data,
content_.asString.size);
return visitor.visitString(content_.asOwnedString->data,
content_.asOwnedString->length);

case VALUE_IS_RAW_STRING:
return visitor.visitRawString(content_.asString.data,
content_.asString.size);
return visitor.visitRawString(content_.asOwnedString->data,
content_.asOwnedString->length);

case VALUE_IS_SIGNED_INTEGER:
return visitor.visitSignedInteger(content_.asSignedInteger);
Expand Down Expand Up @@ -76,7 +79,7 @@ class VariantData {

const char* getOwnedString() const {
if (flags_ & OWNED_VALUE_BIT)
return content_.asString.data;
return content_.asOwnedString->data;
else
return nullptr;
}
Expand Down Expand Up @@ -160,8 +163,7 @@ class VariantData {
void setRawString(StringNode* s) {
ARDUINOJSON_ASSERT(s);
setType(VALUE_IS_RAW_STRING);
content_.asString.data = s->data;
content_.asString.size = s->length;
content_.asOwnedString = s;
}

template <typename T>
Expand All @@ -183,15 +185,13 @@ class VariantData {
void setString(StringNode* s) {
ARDUINOJSON_ASSERT(s);
setType(VALUE_IS_OWNED_STRING);
content_.asString.data = s->data;
content_.asString.size = s->length;
content_.asOwnedString = s;
}

void setString(const char* s) {
ARDUINOJSON_ASSERT(s);
setType(VALUE_IS_LINKED_STRING);
content_.asString.data = s;
content_.asString.size = strlen(s);
content_.asLinkedString = s;
}

CollectionData& toArray() {
Expand All @@ -210,7 +210,7 @@ class VariantData {
switch (type()) {
case VALUE_IS_OWNED_STRING:
case VALUE_IS_RAW_STRING:
return sizeofString(content_.asString.size);
return sizeofString(content_.asOwnedString->length);
case VALUE_IS_OBJECT:
case VALUE_IS_ARRAY:
return content_.asCollection.memoryUsage();
Expand Down
16 changes: 8 additions & 8 deletions src/ArduinoJson/Variant/VariantImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ inline T VariantData::asIntegral() const {
case VALUE_IS_SIGNED_INTEGER:
return convertNumber<T>(content_.asSignedInteger);
case VALUE_IS_LINKED_STRING:
return parseNumber<T>(content_.asLinkedString);
case VALUE_IS_OWNED_STRING:
return parseNumber<T>(content_.asString.data);
return parseNumber<T>(content_.asOwnedString->data);
case VALUE_IS_FLOAT:
return convertNumber<T>(content_.asFloat);
default:
Expand Down Expand Up @@ -62,7 +63,7 @@ inline T VariantData::asFloat() const {
return static_cast<T>(content_.asSignedInteger);
case VALUE_IS_LINKED_STRING:
case VALUE_IS_OWNED_STRING:
return parseNumber<T>(content_.asString.data);
return parseNumber<T>(content_.asOwnedString->data);
case VALUE_IS_FLOAT:
return static_cast<T>(content_.asFloat);
default:
Expand All @@ -73,11 +74,10 @@ inline T VariantData::asFloat() const {
inline JsonString VariantData::asString() const {
switch (type()) {
case VALUE_IS_LINKED_STRING:
return JsonString(content_.asString.data, content_.asString.size,
JsonString::Linked);
return JsonString(content_.asLinkedString, JsonString::Linked);
case VALUE_IS_OWNED_STRING:
return JsonString(content_.asString.data, content_.asString.size,
JsonString::Copied);
return JsonString(content_.asOwnedString->data,
content_.asOwnedString->length, JsonString::Copied);
default:
return JsonString();
}
Expand All @@ -86,8 +86,8 @@ inline JsonString VariantData::asString() const {
inline JsonString VariantData::asRawString() const {
switch (type()) {
case VALUE_IS_RAW_STRING:
return JsonString(content_.asString.data, content_.asString.size,
JsonString::Copied);
return JsonString(content_.asOwnedString->data,
content_.asOwnedString->length, JsonString::Copied);
default:
return JsonString();
}
Expand Down

0 comments on commit 9321f8f

Please sign in to comment.