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

bblanchon / ArduinoJson / 16401907765

20 Jul 2025 04:23PM UTC coverage: 99.172% (-0.04%) from 99.216%
16401907765

push

github

bblanchon
VariantImpl: add copy functions

82 of 88 new or added lines in 10 files covered. (93.18%)

3954 of 3987 relevant lines covered (99.17%)

10920.43 hits per line

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

98.44
/src/ArduinoJson/Array/ArrayImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Collection/CollectionIterator.hpp>
8
#include <ArduinoJson/Variant/VariantImpl.hpp>
9

10
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
11

12
inline VariantImpl::iterator VariantImpl::at(size_t index) const {
494 ✔
13
  if (!isArray())
494 ✔
14
    return iterator();
23 ✔
15

16
  auto it = createIterator();
471 ✔
17
  while (!it.done() && index) {
794 ✔
18
    it.move();
323 ✔
19
    --index;
323 ✔
20
  }
21
  return it;
471 ✔
22
}
23

24
inline VariantData* VariantImpl::addElement() {
3,034 ✔
25
  if (!isArray())
3,034 ✔
26
    return nullptr;
8 ✔
27
  auto slot = allocVariant();
3,026 ✔
28
  if (!slot)
3,026 ✔
29
    return nullptr;
11 ✔
30
  addElement(slot);
3,015 ✔
31
  return slot.ptr();
3,015 ✔
32
}
33

34
inline void VariantImpl::addElement(Slot<VariantData> slot) {
69,682 ✔
35
  auto coll = getCollectionData();
69,682 ✔
36

37
  if (coll->tail != NULL_SLOT) {
69,682 ✔
38
    auto tail = getVariant(coll->tail);
68,532 ✔
39
    tail->next = slot.id();
68,532 ✔
40
    coll->tail = slot.id();
68,532 ✔
41
  } else {
42
    coll->head = slot.id();
1,150 ✔
43
    coll->tail = slot.id();
1,150 ✔
44
  }
45
}
69,682 ✔
46

47
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
153 ✔
48
  auto it = createIterator();
153 ✔
49
  while (!it.done() && index > 0) {
178 ✔
50
    it.move();
25 ✔
51
    index--;
25 ✔
52
  }
53
  if (it.done())
153 ✔
54
    index++;
116 ✔
55
  VariantData* element = it->data();
153 ✔
56
  while (index > 0) {
532 ✔
57
    element = addElement();
381 ✔
58
    if (!element)
381 ✔
59
      return nullptr;
2 ✔
60
    index--;
379 ✔
61
  }
62
  return element;
151 ✔
63
}
64

65
inline VariantData* VariantImpl::getElement(size_t index) const {
478 ✔
66
  return at(index)->data();
478 ✔
67
}
68

69
inline void VariantImpl::removeElement(iterator it) {
22 ✔
70
  if (!isArray())
22 ✔
71
    return;
3 ✔
72
  removeOne(it);
19 ✔
73
}
74

75
inline void VariantImpl::removeElement(size_t index) {
16 ✔
76
  removeElement(at(index));
16 ✔
77
}
16 ✔
78

79
inline bool VariantImpl::copyArray(const VariantImpl& src) {
13 ✔
80
  ARDUINOJSON_ASSERT(isNull());
81

82
  if (!data_)
13 ✔
NEW
83
    return false;
×
84

85
  data_->toArray();
13 ✔
86

87
  for (auto it = src.createIterator(); !it.done(); it.move()) {
19 ✔
88
    auto slot = allocVariant();
7 ✔
89
    if (!slot)
7 ✔
90
      return false;
1 ✔
91

92
    VariantImpl element(slot.ptr(), resources_);
7 ✔
93
    if (!element.copyVariant(*it)) {
7 ✔
94
      freeVariant(slot);
1 ✔
95
      return false;
1 ✔
96
    }
97

98
    addElement(slot);
6 ✔
99
  }
100

101
  return true;
12 ✔
102
}
103

104
// Returns the size (in bytes) of an array with n elements.
105
constexpr size_t sizeofArray(size_t n) {
84 ✔
106
  return n * sizeof(VariantData);
84 ✔
107
}
108

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

© 2026 Coveralls, Inc