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

bblanchon / ArduinoJson / 5506398636

pending completion
5506398636

push

github

bblanchon
Reduce number of copies of `stringEquals()`

43 of 43 new or added lines in 4 files covered. (100.0%)

3422 of 3450 relevant lines covered (99.19%)

6435.16 hits per line

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

93.33
/src/ArduinoJson/Object/ObjectImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Object/ObjectData.hpp>
8
#include <ArduinoJson/Variant/VariantCompare.hpp>
9

10
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
11

12
inline VariantData* ObjectData::addMember(StringNode* key,
1,334✔
13
                                          ResourceManager* resources) {
14
  ARDUINOJSON_ASSERT(key != nullptr);
15
  auto slot = resources->allocVariant();
1,334✔
16
  if (!slot)
1,334✔
17
    return nullptr;
2✔
18

19
  slot->setKey(key);
1,332✔
20
  addSlot(slot);
1,332✔
21
  return slot->data();
1,332✔
22
}
23

24
template <typename TAdaptedString>
25
inline typename enable_if<string_traits<TAdaptedString>::has_data,
26
                          VariantData*>::type
27
ObjectData::addMember(TAdaptedString key, ResourceManager* resources) {
780✔
28
  ARDUINOJSON_ASSERT(!key.isNull());
29
  auto slot = resources->allocVariant();
780✔
30
  if (!slot)
780✔
31
    return nullptr;
3✔
32
  if (key.isLinked())
777✔
33
    slot->setKey(key.data());
720✔
34
  else {
35
    auto storedKey = resources->saveString(key);
57✔
36
    if (!storedKey)
57✔
37
      return nullptr;
1✔
38
    slot->setKey(storedKey);
56✔
39
  }
40
  addSlot(slot);
776✔
41
  return slot->data();
776✔
42
}
43

44
template <typename TAdaptedString>
45
inline typename enable_if<!string_traits<TAdaptedString>::has_data,
46
                          VariantData*>::type
47
ObjectData::addMember(TAdaptedString key, ResourceManager* resources) {
3✔
48
  ARDUINOJSON_ASSERT(!key.isNull());
49
  auto slot = resources->allocVariant();
3✔
50
  if (!slot)
3✔
51
    return nullptr;
×
52
  auto storedKey = resources->saveString(key);
3✔
53
  if (!storedKey)
3✔
54
    return nullptr;
×
55
  slot->setKey(storedKey);
3✔
56
  addSlot(slot);
3✔
57
  return slot->data();
3✔
58
}
59

60
inline bool ObjectData::copyFrom(const ObjectData& src,
25✔
61
                                 ResourceManager* resources) {
62
  clear(resources);
25✔
63

64
  for (auto it = src.begin(); it; ++it) {
41✔
65
    auto key = it.key();
19✔
66
    ARDUINOJSON_ASSERT(!key.isNull());
67
    auto var = addMember(adaptString(key), resources);
19✔
68
    if (!var)
19✔
69
      return false;
3✔
70
    if (!var->copyFrom(*it, resources))
17✔
71
      return false;
1✔
72
  }
73
  return true;
22✔
74
}
75

76
inline bool ObjectData::equals(const ObjectData& other) const {
254✔
77
  size_t count = 0;
254✔
78
  for (auto it = begin(); it; ++it) {
614✔
79
    auto a = it.data();
420✔
80
    auto b = other.getMember(adaptString(it.key()));
420✔
81
    if (!b)
420✔
82
      return false;
60✔
83
    if (compare(a, b) != COMPARE_RESULT_EQUAL)
414✔
84
      return false;
54✔
85
    count++;
360✔
86
  }
87
  return count == other.size();
194✔
88
}
89

90
template <typename TAdaptedString>
91
inline VariantData* ObjectData::getMember(TAdaptedString key) const {
3,634✔
92
  return findKey(key).data();
3,634✔
93
}
94

95
template <typename TAdaptedString>
96
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
797✔
97
                                        ResourceManager* resources) {
98
  auto it = findKey(key);
797✔
99
  if (it)
797✔
100
    return it.data();
33✔
101
  return addMember(key, resources);
764✔
102
}
103

104
template <typename TAdaptedString>
105
inline typename enable_if<!string_traits<TAdaptedString>::has_data,
106
                          ObjectData::iterator>::type
107
ObjectData::findKey(TAdaptedString key) const {
3✔
108
  if (key.isNull())
3✔
109
    return end();
×
110
  for (auto it = begin(); it; ++it) {
3✔
111
    if (stringEquals(key, adaptString(it.key())))
×
112
      return it;
×
113
  }
114
  return end();
3✔
115
}
116

117
template <typename TAdaptedString>
118
inline typename enable_if<string_traits<TAdaptedString>::has_data,
119
                          ObjectData::iterator>::type
120
ObjectData::findKey(TAdaptedString key) const {
4,453✔
121
  if (key.isNull())
4,453✔
122
    return end();
1✔
123
  for (auto it = begin(); it; ++it) {
14,499✔
124
    auto itKey = it.key();
11,253✔
125
    if (stringEquals(itKey.c_str(), itKey.size(), key.data(), key.size()))
11,253✔
126
      return it;
1,206✔
127
  }
128
  return end();
3,246✔
129
}
130

131
template <typename TAdaptedString>
132
inline void ObjectData::removeMember(TAdaptedString key,
25✔
133
                                     ResourceManager* resources) {
134
  remove(findKey(key), resources);
25✔
135
}
25✔
136

137
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

© 2025 Coveralls, Inc