• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 13505335486

24 Feb 2025 06:37PM UTC coverage: 99.227%. Remained the same
13505335486

push

github

bblanchon
Store static string in a dedicated pool

32 of 35 new or added lines in 7 files covered. (91.43%)

3 existing lines in 1 file now uncovered.

3979 of 4010 relevant lines covered (99.23%)

10910.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.65
/src/ArduinoJson/Variant/VariantImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Memory/ResourceManager.hpp>
8
#include <ArduinoJson/Variant/VariantData.hpp>
9

10
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
11

12
template <typename T>
13
inline void VariantData::setRawString(SerializedValue<T> value,
34✔
14
                                      ResourceManager* resources) {
15
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
16
  auto dup = resources->saveString(adaptString(value.data(), value.size()));
34✔
17
  if (dup)
34✔
18
    setRawString(dup);
30✔
19
}
34✔
20

21
inline bool VariantData::setLinkedString(const char* s,
1,157✔
22
                                         ResourceManager* resources) {
23
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
24
  ARDUINOJSON_ASSERT(s);
25

26
  auto slotId = resources->saveStaticString(s);
1,157✔
27
  if (slotId == NULL_SLOT)
1,157✔
NEW
28
    return false;
×
29

30
  type_ = VariantType::LinkedString;
1,157✔
31
  content_.asSlotId = slotId;
1,157✔
32
  return true;
1,157✔
33
}
34

35
template <typename TAdaptedString>
36
inline bool VariantData::setString(TAdaptedString value,
67,040✔
37
                                   ResourceManager* resources) {
38
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
39

40
  if (value.isNull())
67,040✔
41
    return false;
65,552✔
42

43
  if (value.isStatic())
1,488✔
44
    return setLinkedString(value.data(), resources);
1,157✔
45

46
  auto dup = resources->saveString(value);
331✔
47
  if (dup) {
331✔
48
    setOwnedString(dup);
319✔
49
    return true;
319✔
50
  }
51

52
  return false;
12✔
53
}
54

55
inline void VariantData::clear(ResourceManager* resources) {
69,418✔
56
  if (type_ & VariantTypeBits::OwnedStringBit)
69,418✔
57
    resources->dereferenceString(content_.asOwnedString->data);
26✔
58

59
#if ARDUINOJSON_USE_EXTENSIONS
60
  if (type_ & VariantTypeBits::ExtensionBit)
69,418✔
61
    resources->freeExtension(content_.asSlotId);
3✔
62
#endif
63

64
  auto collection = asCollection();
69,418✔
65
  if (collection)
69,418✔
66
    collection->clear(resources);
17✔
67

68
  type_ = VariantType::Null;
69,418✔
69
}
69,418✔
70

71
#if ARDUINOJSON_USE_EXTENSIONS
72
inline const VariantExtension* VariantData::getExtension(
140,285✔
73
    const ResourceManager* resources) const {
74
  return type_ & VariantTypeBits::ExtensionBit
140,285✔
75
             ? resources->getExtension(content_.asSlotId)
140,285✔
76
             : nullptr;
140,285✔
77
}
78
#endif
79

80
inline const char* VariantData::asLinkedString(
5,822✔
81
    const ResourceManager* resources) const {
82
  ARDUINOJSON_ASSERT(type_ == VariantType::LinkedString);
83
  return resources->getStaticString(content_.asSlotId);
5,822✔
84
}
85

86
template <typename T>
87
enable_if_t<sizeof(T) == 8, bool> VariantData::setFloat(
80✔
88
    T value, ResourceManager* resources) {
89
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
90
  (void)resources;                                 // silence warning
91

92
  float valueAsFloat = static_cast<float>(value);
80✔
93

94
#if ARDUINOJSON_USE_DOUBLE
95
  if (value == valueAsFloat) {
78✔
96
    type_ = VariantType::Float;
32✔
97
    content_.asFloat = valueAsFloat;
32✔
98
  } else {
99
    auto extension = resources->allocExtension();
46✔
100
    if (!extension)
46✔
101
      return false;
3✔
102
    type_ = VariantType::Double;
43✔
103
    content_.asSlotId = extension.id();
43✔
104
    extension->asDouble = value;
43✔
105
  }
106
#else
107
  type_ = VariantType::Float;
2✔
108
  content_.asFloat = valueAsFloat;
2✔
109
#endif
110
  return true;
77✔
111
}
112

113
template <typename T>
114
enable_if_t<is_signed<T>::value, bool> VariantData::setInteger(
1,551✔
115
    T value, ResourceManager* resources) {
116
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
117
  (void)resources;                                 // silence warning
118

119
  if (canConvertNumber<int32_t>(value)) {
1,551✔
120
    type_ = VariantType::Int32;
1,534✔
121
    content_.asInt32 = static_cast<int32_t>(value);
1,534✔
122
  }
123
#if ARDUINOJSON_USE_LONG_LONG
124
  else {
125
    auto extension = resources->allocExtension();
17✔
126
    if (!extension)
17✔
127
      return false;
3✔
128
    type_ = VariantType::Int64;
14✔
129
    content_.asSlotId = extension.id();
14✔
130
    extension->asInt64 = value;
14✔
131
  }
132
#endif
133
  return true;
1,548✔
134
}
135

136
template <typename T>
137
enable_if_t<is_unsigned<T>::value, bool> VariantData::setInteger(
2,299✔
138
    T value, ResourceManager* resources) {
139
  ARDUINOJSON_ASSERT(type_ == VariantType::Null);  // must call clear() first
140
  (void)resources;                                 // silence warning
141

142
  if (canConvertNumber<uint32_t>(value)) {
2,299✔
143
    type_ = VariantType::Uint32;
2,282✔
144
    content_.asUint32 = static_cast<uint32_t>(value);
2,282✔
145
  }
146
#if ARDUINOJSON_USE_LONG_LONG
147
  else {
148
    auto extension = resources->allocExtension();
16✔
149
    if (!extension)
16✔
150
      return false;
3✔
151
    type_ = VariantType::Uint64;
13✔
152
    content_.asSlotId = extension.id();
13✔
153
    extension->asUint64 = value;
13✔
154
  }
155
#endif
156
  return true;
2,296✔
157
}
158

159
ARDUINOJSON_END_PRIVATE_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc