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

biojppm / rapidyaml / 14071541066

25 Mar 2025 10:45PM UTC coverage: 97.524% (-0.06%) from 97.586%
14071541066

Pull #508

github

web-flow
Merge d27170bc2 into d3132a25e
Pull Request #508: fix build with cmake 4

11539 of 11832 relevant lines covered (97.52%)

764866.73 hits per line

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

94.94
/src/c4/yml/tree.cpp
1
#include "c4/yml/tree.hpp"
2
#include "c4/yml/detail/parser_dbg.hpp"
3
#include "c4/yml/node.hpp"
4
#include "c4/yml/reference_resolver.hpp"
5

6

7
C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4296/*expression is always 'boolean_value'*/)
8
C4_SUPPRESS_WARNING_MSVC(4702/*unreachable code*/)
9
C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wold-style-cast")
10
C4_SUPPRESS_WARNING_GCC("-Wtype-limits")
11
C4_SUPPRESS_WARNING_GCC("-Wuseless-cast")
12

13
namespace c4 {
14
namespace yml {
15

16

17
//-----------------------------------------------------------------------------
18
//-----------------------------------------------------------------------------
19
//-----------------------------------------------------------------------------
20

21
NodeRef Tree::rootref()
131,848✔
22
{
23
    return NodeRef(this, root_id());
131,848✔
24
}
25
ConstNodeRef Tree::rootref() const
94,920✔
26
{
27
    return ConstNodeRef(this, root_id());
94,920✔
28
}
29

30
ConstNodeRef Tree::crootref() const
72✔
31
{
32
    return ConstNodeRef(this, root_id());
72✔
33
}
34

35
NodeRef Tree::ref(id_type id)
3,776✔
36
{
37
    _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
3,776✔
38
    return NodeRef(this, id);
3,552✔
39
}
40
ConstNodeRef Tree::ref(id_type id) const
80✔
41
{
42
    _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
80✔
43
    return ConstNodeRef(this, id);
80✔
44
}
45
ConstNodeRef Tree::cref(id_type id) const
5,936✔
46
{
47
    _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
5,936✔
48
    return ConstNodeRef(this, id);
5,904✔
49
}
50

51
NodeRef Tree::operator[] (csubstr key)
10,800✔
52
{
53
    return rootref()[key];
21,600✔
54
}
55
ConstNodeRef Tree::operator[] (csubstr key) const
14,664✔
56
{
57
    return rootref()[key];
29,304✔
58
}
59

60
NodeRef Tree::operator[] (id_type i)
2,352✔
61
{
62
    return rootref()[i];
4,704✔
63
}
64
ConstNodeRef Tree::operator[] (id_type i) const
7,752✔
65
{
66
    return rootref()[i];
15,488✔
67
}
68

69
NodeRef Tree::docref(id_type i)
1,208✔
70
{
71
    return ref(doc(i));
1,208✔
72
}
73
ConstNodeRef Tree::docref(id_type i) const
5,584✔
74
{
75
    return cref(doc(i));
5,584✔
76
}
77
ConstNodeRef Tree::cdocref(id_type i) const
32✔
78
{
79
    return cref(doc(i));
32✔
80
}
81

82

83
//-----------------------------------------------------------------------------
84
Tree::Tree(Callbacks const& cb)
1,344,856✔
85
    : m_buf(nullptr)
1,344,856✔
86
    , m_cap(0)
1,344,856✔
87
    , m_size(0)
1,344,856✔
88
    , m_free_head(NONE)
1,344,856✔
89
    , m_free_tail(NONE)
1,344,856✔
90
    , m_arena()
1,344,856✔
91
    , m_arena_pos(0)
1,344,856✔
92
    , m_callbacks(cb)
1,344,856✔
93
    , m_tag_directives()
6,724,280✔
94
{
95
}
1,344,856✔
96

97
Tree::Tree(id_type node_capacity, size_t arena_capacity, Callbacks const& cb)
40✔
98
    : Tree(cb)
40✔
99
{
100
    reserve(node_capacity);
40✔
101
    reserve_arena(arena_capacity);
40✔
102
}
40✔
103

104
Tree::~Tree()
1,344,856✔
105
{
106
    _free();
1,344,856✔
107
}
1,344,856✔
108

109

110
Tree::Tree(Tree const& that) : Tree(that.m_callbacks)
96✔
111
{
112
    _copy(that);
96✔
113
}
96✔
114

115
Tree& Tree::operator= (Tree const& that)
16✔
116
{
117
    if(&that != this)
16✔
118
    {
119
        _free();
16✔
120
        m_callbacks = that.m_callbacks;
16✔
121
        _copy(that);
16✔
122
    }
123
    return *this;
16✔
124
}
125

126
Tree::Tree(Tree && that) noexcept : Tree(that.m_callbacks)
32✔
127
{
128
    _move(that);
32✔
129
}
32✔
130

131
Tree& Tree::operator= (Tree && that) noexcept
380,008✔
132
{
133
    if(&that != this)
380,008✔
134
    {
135
        _free();
380,008✔
136
        m_callbacks = that.m_callbacks;
380,008✔
137
        _move(that);
380,008✔
138
    }
139
    return *this;
380,008✔
140
}
141

142
void Tree::_free()
1,724,880✔
143
{
144
    if(m_buf)
1,724,880✔
145
    {
146
        _RYML_CB_ASSERT(m_callbacks, m_cap > 0);
824,888✔
147
        _RYML_CB_FREE(m_callbacks, m_buf, NodeData, (size_t)m_cap);
824,888✔
148
    }
149
    if(m_arena.str)
1,724,880✔
150
    {
151
        _RYML_CB_ASSERT(m_callbacks, m_arena.len > 0);
214,064✔
152
        _RYML_CB_FREE(m_callbacks, m_arena.str, char, m_arena.len);
214,064✔
153
    }
154
    _clear();
1,724,880✔
155
}
1,724,880✔
156

157

158
C4_SUPPRESS_WARNING_GCC_PUSH
159
#if defined(__GNUC__) && __GNUC__>= 8
160
    C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wclass-memaccess") // error: ‘void* memset(void*, int, size_t)’ clearing an object of type ‘class c4::yml::Tree’ with no trivial copy-assignment; use assignment or value-initialization instead
161
#endif
162

163
void Tree::_clear()
2,104,920✔
164
{
165
    m_buf = nullptr;
2,104,920✔
166
    m_cap = 0;
2,104,920✔
167
    m_size = 0;
2,104,920✔
168
    m_free_head = 0;
2,104,920✔
169
    m_free_tail = 0;
2,104,920✔
170
    m_arena = {};
2,104,920✔
171
    m_arena_pos = 0;
2,104,920✔
172
    for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
10,524,600✔
173
        m_tag_directives[i] = {};
8,419,680✔
174
}
2,104,920✔
175

176
void Tree::_copy(Tree const& that)
112✔
177
{
178
    _RYML_CB_ASSERT(m_callbacks, m_buf == nullptr);
112✔
179
    _RYML_CB_ASSERT(m_callbacks, m_arena.str == nullptr);
112✔
180
    _RYML_CB_ASSERT(m_callbacks, m_arena.len == 0);
112✔
181
    if(that.m_cap)
112✔
182
    {
183
        m_buf = _RYML_CB_ALLOC_HINT(m_callbacks, NodeData, (size_t)that.m_cap, that.m_buf);
96✔
184
        memcpy(m_buf, that.m_buf, (size_t)that.m_cap * sizeof(NodeData));
96✔
185
    }
186
    m_cap = that.m_cap;
112✔
187
    m_size = that.m_size;
112✔
188
    m_free_head = that.m_free_head;
112✔
189
    m_free_tail = that.m_free_tail;
112✔
190
    m_arena_pos = that.m_arena_pos;
112✔
191
    m_arena = that.m_arena;
112✔
192
    if(that.m_arena.str)
112✔
193
    {
194
        _RYML_CB_ASSERT(m_callbacks, that.m_arena.len > 0);
96✔
195
        substr arena;
96✔
196
        arena.str = _RYML_CB_ALLOC_HINT(m_callbacks, char, that.m_arena.len, that.m_arena.str);
96✔
197
        arena.len = that.m_arena.len;
96✔
198
        _relocate(arena); // does a memcpy of the arena and updates nodes using the old arena
96✔
199
        m_arena = arena;
96✔
200
    }
201
    for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
560✔
202
        m_tag_directives[i] = that.m_tag_directives[i];
448✔
203
}
112✔
204

205
void Tree::_move(Tree & that) noexcept
380,040✔
206
{
207
    _RYML_CB_ASSERT(m_callbacks, m_buf == nullptr);
380,040✔
208
    _RYML_CB_ASSERT(m_callbacks, m_arena.str == nullptr);
380,040✔
209
    _RYML_CB_ASSERT(m_callbacks, m_arena.len == 0);
380,040✔
210
    m_buf = that.m_buf;
380,040✔
211
    m_cap = that.m_cap;
380,040✔
212
    m_size = that.m_size;
380,040✔
213
    m_free_head = that.m_free_head;
380,040✔
214
    m_free_tail = that.m_free_tail;
380,040✔
215
    m_arena = that.m_arena;
380,040✔
216
    m_arena_pos = that.m_arena_pos;
380,040✔
217
    for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
1,900,200✔
218
        m_tag_directives[i] = that.m_tag_directives[i];
1,520,160✔
219
    that._clear();
380,040✔
220
}
380,040✔
221

222
void Tree::_relocate(substr next_arena)
7,544✔
223
{
224
    _RYML_CB_ASSERT(m_callbacks, next_arena.not_empty());
7,544✔
225
    _RYML_CB_ASSERT(m_callbacks, next_arena.len >= m_arena.len);
7,544✔
226
    if(m_arena_pos)
7,544✔
227
        memcpy(next_arena.str, m_arena.str, m_arena_pos);
7,520✔
228
    for(NodeData *C4_RESTRICT n = m_buf, *e = m_buf + m_cap; n != e; ++n)
138,568✔
229
    {
230
        if(in_arena(n->m_key.scalar))
131,024✔
231
            n->m_key.scalar = _relocated(n->m_key.scalar, next_arena);
7,456✔
232
        if(in_arena(n->m_key.tag))
131,024✔
233
            n->m_key.tag = _relocated(n->m_key.tag, next_arena);
176✔
234
        if(in_arena(n->m_key.anchor))
131,024✔
235
            n->m_key.anchor = _relocated(n->m_key.anchor, next_arena);
960✔
236
        if(in_arena(n->m_val.scalar))
131,024✔
237
            n->m_val.scalar = _relocated(n->m_val.scalar, next_arena);
26,864✔
238
        if(in_arena(n->m_val.tag))
131,024✔
239
            n->m_val.tag = _relocated(n->m_val.tag, next_arena);
3,088✔
240
        if(in_arena(n->m_val.anchor))
131,024✔
241
            n->m_val.anchor = _relocated(n->m_val.anchor, next_arena);
960✔
242
    }
243
    for(TagDirective &C4_RESTRICT td : m_tag_directives)
37,720✔
244
    {
245
        if(in_arena(td.prefix))
30,176✔
246
            td.prefix = _relocated(td.prefix, next_arena);
1,376✔
247
        if(in_arena(td.handle))
30,176✔
248
            td.handle = _relocated(td.handle, next_arena);
1,376✔
249
    }
250
}
7,544✔
251

252

253
//-----------------------------------------------------------------------------
254
void Tree::reserve(id_type cap)
849,600✔
255
{
256
    if(cap > m_cap)
849,600✔
257
    {
258
        NodeData *buf = _RYML_CB_ALLOC_HINT(m_callbacks, NodeData, (size_t)cap, m_buf);
833,672✔
259
        if(m_buf)
833,672✔
260
        {
261
            memcpy(buf, m_buf, (size_t)m_cap * sizeof(NodeData));
8,880✔
262
            _RYML_CB_FREE(m_callbacks, m_buf, NodeData, (size_t)m_cap);
8,880✔
263
        }
264
        id_type first = m_cap, del = cap - m_cap;
833,672✔
265
        m_cap = cap;
833,672✔
266
        m_buf = buf;
833,672✔
267
        _clear_range(first, del);
833,672✔
268
        if(m_free_head != NONE)
833,672✔
269
        {
270
            _RYML_CB_ASSERT(m_callbacks, m_buf != nullptr);
32✔
271
            _RYML_CB_ASSERT(m_callbacks, m_free_tail != NONE);
32✔
272
            m_buf[m_free_tail].m_next_sibling = first;
32✔
273
            m_buf[first].m_prev_sibling = m_free_tail;
32✔
274
            m_free_tail = cap-1;
32✔
275
        }
276
        else
277
        {
278
            _RYML_CB_ASSERT(m_callbacks, m_free_tail == NONE);
833,640✔
279
            m_free_head = first;
833,640✔
280
            m_free_tail = cap-1;
833,640✔
281
        }
282
        _RYML_CB_ASSERT(m_callbacks, m_free_head == NONE || (m_free_head >= 0 && m_free_head < cap));
833,672✔
283
        _RYML_CB_ASSERT(m_callbacks, m_free_tail == NONE || (m_free_tail >= 0 && m_free_tail < cap));
833,672✔
284

285
        if( ! m_size)
833,672✔
286
            _claim_root();
824,792✔
287
    }
288
}
849,600✔
289

290

291
//-----------------------------------------------------------------------------
292
void Tree::clear()
424,872✔
293
{
294
    _clear_range(0, m_cap);
424,872✔
295
    m_size = 0;
424,872✔
296
    if(m_buf)
424,872✔
297
    {
298
        _RYML_CB_ASSERT(m_callbacks, m_cap >= 0);
299
        m_free_head = 0;
50,648✔
300
        m_free_tail = m_cap-1;
50,648✔
301
        _claim_root();
50,648✔
302
    }
303
    else
304
    {
305
        m_free_head = NONE;
374,224✔
306
        m_free_tail = NONE;
374,224✔
307
    }
308
    for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
2,124,360✔
309
        m_tag_directives[i] = {};
1,699,488✔
310
}
424,872✔
311

312
void Tree::_claim_root()
875,440✔
313
{
314
    id_type r = _claim();
875,440✔
315
    _RYML_CB_ASSERT(m_callbacks, r == 0);
875,440✔
316
    _set_hierarchy(r, NONE, NONE);
875,440✔
317
}
875,440✔
318

319

320
//-----------------------------------------------------------------------------
321
void Tree::_clear_range(id_type first, id_type num)
1,258,544✔
322
{
323
    if(num == 0)
1,258,544✔
324
        return; // prevent overflow when subtracting
374,224✔
325
    _RYML_CB_ASSERT(m_callbacks, first >= 0 && first + num <= m_cap);
884,320✔
326
    memset(m_buf + first, 0, (size_t)num * sizeof(NodeData)); // TODO we should not need this
884,320✔
327
    for(id_type i = first, e = first + num; i < e; ++i)
14,934,224✔
328
    {
329
        _clear(i);
14,049,904✔
330
        NodeData *n = m_buf + i;
14,049,904✔
331
        n->m_prev_sibling = i - 1;
14,049,904✔
332
        n->m_next_sibling = i + 1;
14,049,904✔
333
    }
334
    m_buf[first + num - 1].m_next_sibling = NONE;
884,320✔
335
}
336

337
C4_SUPPRESS_WARNING_GCC_POP
338

339

340
//-----------------------------------------------------------------------------
341
void Tree::_release(id_type i)
160,856✔
342
{
343
    _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
160,856✔
344

345
    _rem_hierarchy(i);
160,856✔
346
    _free_list_add(i);
160,856✔
347
    _clear(i);
160,856✔
348

349
    --m_size;
160,856✔
350
}
160,856✔
351

352
//-----------------------------------------------------------------------------
353
// add to the front of the free list
354
void Tree::_free_list_add(id_type i)
160,968✔
355
{
356
    _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
160,968✔
357
    NodeData &C4_RESTRICT w = m_buf[i];
160,968✔
358

359
    w.m_parent = NONE;
160,968✔
360
    w.m_next_sibling = m_free_head;
160,968✔
361
    w.m_prev_sibling = NONE;
160,968✔
362
    if(m_free_head != NONE)
160,968✔
363
        m_buf[m_free_head].m_prev_sibling = i;
160,904✔
364
    m_free_head = i;
160,968✔
365
    if(m_free_tail == NONE)
160,968✔
366
        m_free_tail = m_free_head;
64✔
367
}
160,968✔
368

369
void Tree::_free_list_rem(id_type i)
112✔
370
{
371
    if(m_free_head == i)
112✔
372
        m_free_head = _p(i)->m_next_sibling;
32✔
373
    _rem_hierarchy(i);
112✔
374
}
112✔
375

376
//-----------------------------------------------------------------------------
377
id_type Tree::_claim()
2,584,544✔
378
{
379
    if(m_free_head == NONE || m_buf == nullptr)
2,584,544✔
380
    {
381
        id_type sz = 2 * m_cap;
8,848✔
382
        sz = sz ? sz : 16;
8,848✔
383
        reserve(sz);
8,848✔
384
        _RYML_CB_ASSERT(m_callbacks, m_free_head != NONE);
8,848✔
385
    }
386

387
    _RYML_CB_ASSERT(m_callbacks, m_size < m_cap);
2,584,544✔
388
    _RYML_CB_ASSERT(m_callbacks, m_free_head >= 0 && m_free_head < m_cap);
2,584,544✔
389

390
    id_type ichild = m_free_head;
2,584,544✔
391
    NodeData *child = m_buf + ichild;
2,584,544✔
392

393
    ++m_size;
2,584,544✔
394
    m_free_head = child->m_next_sibling;
2,584,544✔
395
    if(m_free_head == NONE)
2,584,544✔
396
    {
397
        m_free_tail = NONE;
19,152✔
398
        _RYML_CB_ASSERT(m_callbacks, m_size == m_cap);
19,152✔
399
    }
400

401
    _clear(ichild);
2,584,544✔
402

403
    return ichild;
2,584,544✔
404
}
405

406
//-----------------------------------------------------------------------------
407

408
C4_SUPPRESS_WARNING_GCC_PUSH
409
C4_SUPPRESS_WARNING_CLANG_PUSH
410
C4_SUPPRESS_WARNING_CLANG("-Wnull-dereference")
411
#if defined(__GNUC__)
412
#if (__GNUC__ >= 6)
413
C4_SUPPRESS_WARNING_GCC("-Wnull-dereference")
414
#endif
415
#if (__GNUC__ > 9)
416
C4_SUPPRESS_WARNING_GCC("-Wanalyzer-fd-leak")
417
#endif
418
#endif
419

420
void Tree::_set_hierarchy(id_type ichild, id_type iparent, id_type iprev_sibling)
2,585,648✔
421
{
422
    _RYML_CB_ASSERT(m_callbacks, ichild >= 0 && ichild < m_cap);
2,585,648✔
423
    _RYML_CB_ASSERT(m_callbacks, iparent == NONE || (iparent >= 0 && iparent < m_cap));
2,585,648✔
424
    _RYML_CB_ASSERT(m_callbacks, iprev_sibling == NONE || (iprev_sibling >= 0 && iprev_sibling < m_cap));
2,585,648✔
425

426
    NodeData *C4_RESTRICT child = _p(ichild);
2,585,648✔
427

428
    child->m_parent = iparent;
2,585,648✔
429
    child->m_prev_sibling = NONE;
2,585,648✔
430
    child->m_next_sibling = NONE;
2,585,648✔
431

432
    if(iparent == NONE)
2,585,648✔
433
    {
434
        _RYML_CB_ASSERT(m_callbacks, ichild == 0);
875,440✔
435
        _RYML_CB_ASSERT(m_callbacks, iprev_sibling == NONE);
875,440✔
436
    }
437

438
    if(iparent == NONE)
2,585,648✔
439
        return;
875,440✔
440

441
    id_type inext_sibling = iprev_sibling != NONE ? next_sibling(iprev_sibling) : first_child(iparent);
1,710,208✔
442
    NodeData *C4_RESTRICT parent = get(iparent);
1,710,208✔
443
    NodeData *C4_RESTRICT psib   = get(iprev_sibling);
1,710,208✔
444
    NodeData *C4_RESTRICT nsib   = get(inext_sibling);
1,710,208✔
445

446
    if(psib)
1,710,208✔
447
    {
448
        _RYML_CB_ASSERT(m_callbacks, next_sibling(iprev_sibling) == id(nsib));
897,720✔
449
        child->m_prev_sibling = id(psib);
897,720✔
450
        psib->m_next_sibling = id(child);
897,720✔
451
        _RYML_CB_ASSERT(m_callbacks, psib->m_prev_sibling != psib->m_next_sibling || psib->m_prev_sibling == NONE);
897,720✔
452
    }
453

454
    if(nsib)
1,710,208✔
455
    {
456
        _RYML_CB_ASSERT(m_callbacks, prev_sibling(inext_sibling) == id(psib));
840✔
457
        child->m_next_sibling = id(nsib);
840✔
458
        nsib->m_prev_sibling = id(child);
840✔
459
        _RYML_CB_ASSERT(m_callbacks, nsib->m_prev_sibling != nsib->m_next_sibling || nsib->m_prev_sibling == NONE);
840✔
460
    }
461

462
    if(parent->m_first_child == NONE)
1,710,208✔
463
    {
464
        _RYML_CB_ASSERT(m_callbacks, parent->m_last_child == NONE);
812,088✔
465
        parent->m_first_child = id(child);
812,088✔
466
        parent->m_last_child = id(child);
812,088✔
467
    }
468
    else
469
    {
470
        if(child->m_next_sibling == parent->m_first_child)
898,120✔
471
            parent->m_first_child = id(child);
400✔
472

473
        if(child->m_prev_sibling == parent->m_last_child)
898,120✔
474
            parent->m_last_child = id(child);
897,280✔
475
    }
476
}
477

478
C4_SUPPRESS_WARNING_GCC_POP
479
C4_SUPPRESS_WARNING_CLANG_POP
480

481

482
//-----------------------------------------------------------------------------
483
void Tree::_rem_hierarchy(id_type i)
162,072✔
484
{
485
    _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
162,072✔
486

487
    NodeData &C4_RESTRICT w = m_buf[i];
162,072✔
488

489
    // remove from the parent
490
    if(w.m_parent != NONE)
162,072✔
491
    {
492
        NodeData &C4_RESTRICT p = m_buf[w.m_parent];
161,960✔
493
        if(p.m_first_child == i)
161,960✔
494
        {
495
            p.m_first_child = w.m_next_sibling;
14,360✔
496
        }
497
        if(p.m_last_child == i)
161,960✔
498
        {
499
            p.m_last_child = w.m_prev_sibling;
159,584✔
500
        }
501
    }
502

503
    // remove from the used list
504
    if(w.m_prev_sibling != NONE)
162,072✔
505
    {
506
        NodeData *C4_RESTRICT prev = get(w.m_prev_sibling);
147,680✔
507
        prev->m_next_sibling = w.m_next_sibling;
147,680✔
508
    }
509
    if(w.m_next_sibling != NONE)
162,072✔
510
    {
511
        NodeData *C4_RESTRICT next = get(w.m_next_sibling);
2,488✔
512
        next->m_prev_sibling = w.m_prev_sibling;
2,488✔
513
    }
514
}
162,072✔
515

516
//-----------------------------------------------------------------------------
517
/** @cond dev */
518
id_type Tree::_do_reorder(id_type *node, id_type count)
3,392✔
519
{
520
    // swap this node if it's not in place
521
    if(*node != count)
3,392✔
522
    {
523
        _swap(*node, count);
720✔
524
        *node = count;
720✔
525
    }
526
    ++count; // bump the count from this node
3,392✔
527

528
    // now descend in the hierarchy
529
    for(id_type i = first_child(*node); i != NONE; i = next_sibling(i))
6,592✔
530
    {
531
        // this child may have been relocated to a different index,
532
        // so get an updated version
533
        count = _do_reorder(&i, count);
3,200✔
534
    }
535
    return count;
3,392✔
536
}
537
/** @endcond */
538

539
void Tree::reorder()
192✔
540
{
541
    id_type r = root_id();
192✔
542
    _do_reorder(&r, 0);
192✔
543
}
192✔
544

545

546
//-----------------------------------------------------------------------------
547
/** @cond dev */
548
void Tree::_swap(id_type n_, id_type m_)
720✔
549
{
550
    _RYML_CB_ASSERT(m_callbacks, (parent(n_) != NONE) || type(n_) == NOTYPE);
720✔
551
    _RYML_CB_ASSERT(m_callbacks, (parent(m_) != NONE) || type(m_) == NOTYPE);
832✔
552
    NodeType tn = type(n_);
720✔
553
    NodeType tm = type(m_);
720✔
554
    if(tn != NOTYPE && tm != NOTYPE)
1,440✔
555
    {
556
        _swap_props(n_, m_);
608✔
557
        _swap_hierarchy(n_, m_);
608✔
558
    }
559
    else if(tn == NOTYPE && tm != NOTYPE)
112✔
560
    {
561
        _copy_props(n_, m_);
×
562
        _free_list_rem(n_);
×
563
        _copy_hierarchy(n_, m_);
×
564
        _clear(m_);
×
565
        _free_list_add(m_);
×
566
    }
567
    else if(tn != NOTYPE && tm == NOTYPE)
224✔
568
    {
569
        _copy_props(m_, n_);
112✔
570
        _free_list_rem(m_);
112✔
571
        _copy_hierarchy(m_, n_);
112✔
572
        _clear(n_);
112✔
573
        _free_list_add(n_);
112✔
574
    }
575
    else
576
    {
577
        C4_NEVER_REACH();
×
578
    }
579
}
720✔
580

581
//-----------------------------------------------------------------------------
582
void Tree::_swap_hierarchy(id_type ia, id_type ib)
608✔
583
{
584
    if(ia == ib) return;
608✔
585

586
    for(id_type i = first_child(ia); i != NONE; i = next_sibling(i))
896✔
587
    {
588
        if(i == ib || i == ia)
288✔
589
            continue;
16✔
590
        _p(i)->m_parent = ib;
272✔
591
    }
592

593
    for(id_type i = first_child(ib); i != NONE; i = next_sibling(i))
1,392✔
594
    {
595
        if(i == ib || i == ia)
784✔
596
            continue;
×
597
        _p(i)->m_parent = ia;
784✔
598
    }
599

600
    auto & C4_RESTRICT a  = *_p(ia);
608✔
601
    auto & C4_RESTRICT b  = *_p(ib);
608✔
602
    auto & C4_RESTRICT pa = *_p(a.m_parent);
608✔
603
    auto & C4_RESTRICT pb = *_p(b.m_parent);
608✔
604

605
    if(&pa == &pb)
608✔
606
    {
607
        if((pa.m_first_child == ib && pa.m_last_child == ia)
112✔
608
            ||
112✔
609
           (pa.m_first_child == ia && pa.m_last_child == ib))
112✔
610
        {
611
            std::swap(pa.m_first_child, pa.m_last_child);
16✔
612
        }
613
        else
614
        {
615
            bool changed = false;
96✔
616
            if(pa.m_first_child == ia)
96✔
617
            {
618
                pa.m_first_child = ib;
32✔
619
                changed = true;
32✔
620
            }
621
            if(pa.m_last_child  == ia)
96✔
622
            {
623
                pa.m_last_child = ib;
×
624
                changed = true;
×
625
            }
626
            if(pb.m_first_child == ib && !changed)
96✔
627
            {
628
                pb.m_first_child = ia;
×
629
            }
630
            if(pb.m_last_child  == ib && !changed)
96✔
631
            {
632
                pb.m_last_child  = ia;
48✔
633
            }
634
        }
635
    }
636
    else
637
    {
638
        if(pa.m_first_child == ia)
496✔
639
            pa.m_first_child = ib;
176✔
640
        if(pa.m_last_child  == ia)
496✔
641
            pa.m_last_child  = ib;
160✔
642
        if(pb.m_first_child == ib)
496✔
643
            pb.m_first_child = ia;
208✔
644
        if(pb.m_last_child  == ib)
496✔
645
            pb.m_last_child  = ia;
272✔
646
    }
647
    std::swap(a.m_first_child , b.m_first_child);
608✔
648
    std::swap(a.m_last_child  , b.m_last_child);
608✔
649

650
    if(a.m_prev_sibling != ib && b.m_prev_sibling != ia &&
608✔
651
       a.m_next_sibling != ib && b.m_next_sibling != ia)
528✔
652
    {
653
        if(a.m_prev_sibling != NONE && a.m_prev_sibling != ib)
528✔
654
            _p(a.m_prev_sibling)->m_next_sibling = ib;
320✔
655
        if(a.m_next_sibling != NONE && a.m_next_sibling != ib)
528✔
656
            _p(a.m_next_sibling)->m_prev_sibling = ib;
368✔
657
        if(b.m_prev_sibling != NONE && b.m_prev_sibling != ia)
528✔
658
            _p(b.m_prev_sibling)->m_next_sibling = ia;
320✔
659
        if(b.m_next_sibling != NONE && b.m_next_sibling != ia)
528✔
660
            _p(b.m_next_sibling)->m_prev_sibling = ia;
240✔
661
        std::swap(a.m_prev_sibling, b.m_prev_sibling);
528✔
662
        std::swap(a.m_next_sibling, b.m_next_sibling);
528✔
663
    }
664
    else
665
    {
666
        if(a.m_next_sibling == ib) // n will go after m
80✔
667
        {
668
            _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling == ia);
80✔
669
            if(a.m_prev_sibling != NONE)
80✔
670
            {
671
                _RYML_CB_ASSERT(m_callbacks, a.m_prev_sibling != ib);
64✔
672
                _p(a.m_prev_sibling)->m_next_sibling = ib;
64✔
673
            }
674
            if(b.m_next_sibling != NONE)
80✔
675
            {
676
                _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling != ia);
32✔
677
                _p(b.m_next_sibling)->m_prev_sibling = ia;
32✔
678
            }
679
            id_type ns = b.m_next_sibling;
80✔
680
            b.m_prev_sibling = a.m_prev_sibling;
80✔
681
            b.m_next_sibling = ia;
80✔
682
            a.m_prev_sibling = ib;
80✔
683
            a.m_next_sibling = ns;
80✔
684
        }
685
        else if(a.m_prev_sibling == ib) // m will go after n
×
686
        {
687
            _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling == ia);
×
688
            if(b.m_prev_sibling != NONE)
×
689
            {
690
                _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling != ia);
×
691
                _p(b.m_prev_sibling)->m_next_sibling = ia;
×
692
            }
693
            if(a.m_next_sibling != NONE)
×
694
            {
695
                _RYML_CB_ASSERT(m_callbacks, a.m_next_sibling != ib);
×
696
                _p(a.m_next_sibling)->m_prev_sibling = ib;
×
697
            }
698
            id_type ns = b.m_prev_sibling;
×
699
            a.m_prev_sibling = b.m_prev_sibling;
×
700
            a.m_next_sibling = ib;
×
701
            b.m_prev_sibling = ia;
×
702
            b.m_next_sibling = ns;
×
703
        }
704
        else
705
        {
706
            C4_NEVER_REACH();
×
707
        }
708
    }
709
    _RYML_CB_ASSERT(m_callbacks, a.m_next_sibling != ia);
608✔
710
    _RYML_CB_ASSERT(m_callbacks, a.m_prev_sibling != ia);
608✔
711
    _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling != ib);
608✔
712
    _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling != ib);
608✔
713

714
    if(a.m_parent != ib && b.m_parent != ia)
608✔
715
    {
716
        std::swap(a.m_parent, b.m_parent);
592✔
717
    }
718
    else
719
    {
720
        if(a.m_parent == ib && b.m_parent != ia)
16✔
721
        {
722
            a.m_parent = b.m_parent;
×
723
            b.m_parent = ia;
×
724
        }
725
        else if(a.m_parent != ib && b.m_parent == ia)
16✔
726
        {
727
            b.m_parent = a.m_parent;
16✔
728
            a.m_parent = ib;
16✔
729
        }
730
        else
731
        {
732
            C4_NEVER_REACH();
×
733
        }
734
    }
735
}
736

737
//-----------------------------------------------------------------------------
738
void Tree::_copy_hierarchy(id_type dst_, id_type src_)
112✔
739
{
740
    auto const& C4_RESTRICT src = *_p(src_);
112✔
741
    auto      & C4_RESTRICT dst = *_p(dst_);
112✔
742
    auto      & C4_RESTRICT prt = *_p(src.m_parent);
112✔
743
    for(id_type i = src.m_first_child; i != NONE; i = next_sibling(i))
240✔
744
    {
745
        _p(i)->m_parent = dst_;
128✔
746
    }
747
    if(src.m_prev_sibling != NONE)
112✔
748
    {
749
        _p(src.m_prev_sibling)->m_next_sibling = dst_;
80✔
750
    }
751
    if(src.m_next_sibling != NONE)
112✔
752
    {
753
        _p(src.m_next_sibling)->m_prev_sibling = dst_;
80✔
754
    }
755
    if(prt.m_first_child == src_)
112✔
756
    {
757
        prt.m_first_child = dst_;
32✔
758
    }
759
    if(prt.m_last_child  == src_)
112✔
760
    {
761
        prt.m_last_child  = dst_;
32✔
762
    }
763
    dst.m_parent       = src.m_parent;
112✔
764
    dst.m_first_child  = src.m_first_child;
112✔
765
    dst.m_last_child   = src.m_last_child;
112✔
766
    dst.m_prev_sibling = src.m_prev_sibling;
112✔
767
    dst.m_next_sibling = src.m_next_sibling;
112✔
768
}
112✔
769

770
//-----------------------------------------------------------------------------
771
void Tree::_swap_props(id_type n_, id_type m_)
608✔
772
{
773
    NodeData &C4_RESTRICT n = *_p(n_);
608✔
774
    NodeData &C4_RESTRICT m = *_p(m_);
608✔
775
    std::swap(n.m_type, m.m_type);
608✔
776
    std::swap(n.m_key, m.m_key);
608✔
777
    std::swap(n.m_val, m.m_val);
608✔
778
}
608✔
779
/** @endcond */
780

781
//-----------------------------------------------------------------------------
782
void Tree::move(id_type node, id_type after)
64✔
783
{
784
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
64✔
785
    _RYML_CB_ASSERT(m_callbacks, node != after);
64✔
786
    _RYML_CB_ASSERT(m_callbacks,  ! is_root(node));
64✔
787
    _RYML_CB_ASSERT(m_callbacks, (after == NONE) || (has_sibling(node, after) && has_sibling(after, node)));
64✔
788

789
    _rem_hierarchy(node);
64✔
790
    _set_hierarchy(node, parent(node), after);
64✔
791
}
64✔
792

793
//-----------------------------------------------------------------------------
794

795
void Tree::move(id_type node, id_type new_parent, id_type after)
1,040✔
796
{
797
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
1,040✔
798
    _RYML_CB_ASSERT(m_callbacks, node != after);
1,040✔
799
    _RYML_CB_ASSERT(m_callbacks, new_parent != NONE);
1,040✔
800
    _RYML_CB_ASSERT(m_callbacks, new_parent != node);
1,040✔
801
    _RYML_CB_ASSERT(m_callbacks, new_parent != after);
1,040✔
802
    _RYML_CB_ASSERT(m_callbacks,  ! is_root(node));
1,040✔
803

804
    _rem_hierarchy(node);
1,040✔
805
    _set_hierarchy(node, new_parent, after);
1,040✔
806
}
1,040✔
807

808
id_type Tree::move(Tree *src, id_type node, id_type new_parent, id_type after)
32✔
809
{
810
    _RYML_CB_ASSERT(m_callbacks, src != nullptr);
32✔
811
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
32✔
812
    _RYML_CB_ASSERT(m_callbacks, new_parent != NONE);
32✔
813
    _RYML_CB_ASSERT(m_callbacks, new_parent != after);
32✔
814

815
    id_type dup = duplicate(src, node, new_parent, after);
32✔
816
    src->remove(node);
32✔
817
    return dup;
32✔
818
}
819

820
void Tree::set_root_as_stream()
133,616✔
821
{
822
    id_type root = root_id();
133,616✔
823
    if(is_stream(root))
133,616✔
824
        return;
576✔
825
    // don't use _add_flags() because it's checked and will fail
826
    if(!has_children(root))
133,040✔
827
    {
828
        if(is_val(root))
132,208✔
829
        {
830
            _p(root)->m_type.add(SEQ);
132,200✔
831
            id_type next_doc = append_child(root);
132,200✔
832
            _copy_props_wo_key(next_doc, root);
132,200✔
833
            _p(next_doc)->m_type.add(DOC);
132,200✔
834
            _p(next_doc)->m_type.rem(SEQ);
132,200✔
835
        }
836
        _p(root)->m_type = STREAM;
132,208✔
837
        return;
132,208✔
838
    }
839
    _RYML_CB_ASSERT(m_callbacks, !has_key(root));
832✔
840
    id_type next_doc = append_child(root);
832✔
841
    _copy_props_wo_key(next_doc, root);
832✔
842
    _add_flags(next_doc, DOC);
832✔
843
    for(id_type prev = NONE, ch = first_child(root), next = next_sibling(ch); ch != NONE; )
1,848✔
844
    {
845
        if(ch == next_doc)
1,848✔
846
            break;
832✔
847
        move(ch, next_doc, prev);
1,016✔
848
        prev = ch;
1,016✔
849
        ch = next;
1,016✔
850
        next = next_sibling(next);
1,016✔
851
    }
852
    _p(root)->m_type = STREAM;
832✔
853
}
854

855

856
//-----------------------------------------------------------------------------
857
void Tree::remove_children(id_type node)
161,000✔
858
{
859
    _RYML_CB_ASSERT(m_callbacks, get(node) != nullptr);
161,000✔
860
    id_type ich = get(node)->m_first_child;
161,000✔
861
    while(ich != NONE)
162,368✔
862
    {
863
        remove_children(ich);
1,368✔
864
        _RYML_CB_ASSERT(m_callbacks, get(ich) != nullptr);
1,368✔
865
        id_type next = get(ich)->m_next_sibling;
1,368✔
866
        _release(ich);
1,368✔
867
        if(ich == get(node)->m_last_child)
1,368✔
868
            break;
×
869
        ich = next;
1,368✔
870
    }
871
}
161,000✔
872

873
bool Tree::change_type(id_type node, NodeType type)
144✔
874
{
875
    _RYML_CB_ASSERT(m_callbacks, type.is_val() || type.is_map() || type.is_seq());
288✔
876
    _RYML_CB_ASSERT(m_callbacks, type.is_val() + type.is_map() + type.is_seq() == 1);
432✔
877
    _RYML_CB_ASSERT(m_callbacks, type.has_key() == has_key(node) || (has_key(node) && !type.has_key()));
432✔
878
    NodeData *d = _p(node);
144✔
879
    if(type.is_map() && is_map(node))
192✔
880
        return false;
16✔
881
    else if(type.is_seq() && is_seq(node))
176✔
882
        return false;
16✔
883
    else if(type.is_val() && is_val(node))
160✔
884
        return false;
8✔
885
    d->m_type = (d->m_type & (~(MAP|SEQ|VAL))) | type;
728✔
886
    remove_children(node);
104✔
887
    return true;
104✔
888
}
889

890

891
//-----------------------------------------------------------------------------
892
id_type Tree::duplicate(id_type node, id_type parent, id_type after)
24✔
893
{
894
    return duplicate(this, node, parent, after);
24✔
895
}
896

897
id_type Tree::duplicate(Tree const* src, id_type node, id_type parent, id_type after)
1,416✔
898
{
899
    _RYML_CB_ASSERT(m_callbacks, src != nullptr);
1,416✔
900
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
1,416✔
901
    _RYML_CB_ASSERT(m_callbacks, parent != NONE);
1,416✔
902
    _RYML_CB_ASSERT(m_callbacks,  ! src->is_root(node));
1,416✔
903

904
    id_type copy = _claim();
1,416✔
905

906
    _copy_props(copy, src, node);
1,416✔
907
    _set_hierarchy(copy, parent, after);
1,416✔
908
    duplicate_children(src, node, copy, NONE);
1,416✔
909

910
    return copy;
1,416✔
911
}
912

913
//-----------------------------------------------------------------------------
914
id_type Tree::duplicate_children(id_type node, id_type parent, id_type after)
×
915
{
916
    return duplicate_children(this, node, parent, after);
×
917
}
918

919
id_type Tree::duplicate_children(Tree const* src, id_type node, id_type parent, id_type after)
1,680✔
920
{
921
    _RYML_CB_ASSERT(m_callbacks, src != nullptr);
1,680✔
922
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
1,680✔
923
    _RYML_CB_ASSERT(m_callbacks, parent != NONE);
1,680✔
924
    _RYML_CB_ASSERT(m_callbacks, after == NONE || has_child(parent, after));
1,680✔
925

926
    id_type prev = after;
1,680✔
927
    for(id_type i = src->first_child(node); i != NONE; i = src->next_sibling(i))
2,664✔
928
    {
929
        prev = duplicate(src, i, parent, prev);
984✔
930
    }
931

932
    return prev;
1,680✔
933
}
934

935
//-----------------------------------------------------------------------------
936
void Tree::duplicate_contents(id_type node, id_type where)
264✔
937
{
938
    duplicate_contents(this, node, where);
264✔
939
}
264✔
940

941
void Tree::duplicate_contents(Tree const *src, id_type node, id_type where)
264✔
942
{
943
    _RYML_CB_ASSERT(m_callbacks, src != nullptr);
264✔
944
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
264✔
945
    _RYML_CB_ASSERT(m_callbacks, where != NONE);
264✔
946
    _copy_props_wo_key(where, src, node);
264✔
947
    duplicate_children(src, node, where, last_child(where));
264✔
948
}
264✔
949

950
//-----------------------------------------------------------------------------
951
id_type Tree::duplicate_children_no_rep(id_type node, id_type parent, id_type after)
248✔
952
{
953
    return duplicate_children_no_rep(this, node, parent, after);
248✔
954
}
955

956
id_type Tree::duplicate_children_no_rep(Tree const *src, id_type node, id_type parent, id_type after)
248✔
957
{
958
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
248✔
959
    _RYML_CB_ASSERT(m_callbacks, parent != NONE);
248✔
960
    _RYML_CB_ASSERT(m_callbacks, after == NONE || has_child(parent, after));
248✔
961

962
    // don't loop using pointers as there may be a relocation
963

964
    // find the position where "after" is
965
    id_type after_pos = NONE;
248✔
966
    if(after != NONE)
248✔
967
    {
968
        for(id_type i = first_child(parent), icount = 0; i != NONE; ++icount, i = next_sibling(i))
232✔
969
        {
970
            if(i == after)
232✔
971
            {
972
                after_pos = icount;
112✔
973
                break;
112✔
974
            }
975
        }
976
        _RYML_CB_ASSERT(m_callbacks, after_pos != NONE);
112✔
977
    }
978

979
    // for each child to be duplicated...
980
    id_type prev = after;
248✔
981
    for(id_type i = src->first_child(node); i != NONE; i = src->next_sibling(i))
640✔
982
    {
983
        if(is_seq(parent))
392✔
984
        {
985
            prev = duplicate(i, parent, prev);
×
986
        }
987
        else
988
        {
989
            _RYML_CB_ASSERT(m_callbacks, is_map(parent));
392✔
990
            // does the parent already have a node with key equal to that of the current duplicate?
991
            id_type rep = NONE, rep_pos = NONE;
392✔
992
            for(id_type j = first_child(parent), jcount = 0; j != NONE; ++jcount, j = next_sibling(j))
1,440✔
993
            {
994
                if(key(j) == key(i))
2,208✔
995
                {
996
                    rep = j;
56✔
997
                    rep_pos = jcount;
56✔
998
                    break;
56✔
999
                }
1000
            }
1001
            if(rep == NONE) // there is no repetition; just duplicate
392✔
1002
            {
1003
                prev = duplicate(src, i, parent, prev);
336✔
1004
            }
1005
            else  // yes, there is a repetition
1006
            {
1007
                if(after_pos != NONE && rep_pos < after_pos)
56✔
1008
                {
1009
                    // rep is located before the node which will be inserted,
1010
                    // and will be overridden by the duplicate. So replace it.
1011
                    remove(rep);
16✔
1012
                    prev = duplicate(src, i, parent, prev);
16✔
1013
                }
1014
                else if(prev == NONE)
40✔
1015
                {
1016
                    // first iteration with prev = after = NONE and repetition
1017
                    prev = rep;
8✔
1018
                }
1019
                else if(rep != prev)
32✔
1020
                {
1021
                    // rep is located after the node which will be inserted
1022
                    // and overrides it. So move the rep into this node's place.
1023
                    move(rep, prev);
32✔
1024
                    prev = rep;
32✔
1025
                }
1026
            } // there's a repetition
1027
        }
1028
    }
1029

1030
    return prev;
248✔
1031
}
1032

1033

1034
//-----------------------------------------------------------------------------
1035

1036
void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
1,424✔
1037
{
1038
    _RYML_CB_ASSERT(m_callbacks, src != nullptr);
1,424✔
1039
    if(src_node == NONE)
1,424✔
1040
        src_node = src->root_id();
464✔
1041
    if(dst_node == NONE)
1,424✔
1042
        dst_node = root_id();
464✔
1043
    _RYML_CB_ASSERT(m_callbacks, src->has_val(src_node) || src->is_seq(src_node) || src->is_map(src_node));
2,360✔
1044
    if(src->has_val(src_node))
1,424✔
1045
    {
1046
        type_bits mask_src = ~STYLE; // keep the existing style if it is already a val
880✔
1047
        if( ! has_val(dst_node))
880✔
1048
        {
1049
            if(has_children(dst_node))
24✔
1050
                remove_children(dst_node);
24✔
1051
            mask_src |= VAL_STYLE; // copy the src style
24✔
1052
        }
1053
        if(src->is_keyval(src_node))
880✔
1054
        {
1055
            _copy_props(dst_node, src, src_node, mask_src);
504✔
1056
        }
1057
        else
1058
        {
1059
            _RYML_CB_ASSERT(m_callbacks, src->is_val(src_node));
376✔
1060
            _copy_props_wo_key(dst_node, src, src_node, mask_src);
376✔
1061
        }
1062
    }
1063
    else if(src->is_seq(src_node))
544✔
1064
    {
1065
        if( ! is_seq(dst_node))
152✔
1066
        {
1067
            if(has_children(dst_node))
48✔
1068
                remove_children(dst_node);
×
1069
            _clear_type(dst_node);
48✔
1070
            if(src->has_key(src_node))
48✔
1071
                to_seq(dst_node, src->key(src_node));
16✔
1072
            else
1073
                to_seq(dst_node);
32✔
1074
            _p(dst_node)->m_type = src->_p(src_node)->m_type;
48✔
1075
        }
1076
        for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
528✔
1077
        {
1078
            id_type dch = append_child(dst_node);
376✔
1079
            _copy_props_wo_key(dch, src, sch);
376✔
1080
            merge_with(src, sch, dch);
376✔
1081
        }
1082
    }
1083
    else
1084
    {
1085
        _RYML_CB_ASSERT(m_callbacks, src->is_map(src_node));
392✔
1086
        if( ! is_map(dst_node))
392✔
1087
        {
1088
            if(has_children(dst_node))
144✔
1089
                remove_children(dst_node);
8✔
1090
            _clear_type(dst_node);
144✔
1091
            if(src->has_key(src_node))
144✔
1092
                to_map(dst_node, src->key(src_node));
16✔
1093
            else
1094
                to_map(dst_node);
128✔
1095
            _p(dst_node)->m_type = src->_p(src_node)->m_type;
144✔
1096
        }
1097
        for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
968✔
1098
        {
1099
            id_type dch = find_child(dst_node, src->key(sch));
576✔
1100
            if(dch == NONE)
576✔
1101
            {
1102
                dch = append_child(dst_node);
368✔
1103
                _copy_props(dch, src, sch);
368✔
1104
            }
1105
            merge_with(src, sch, dch);
576✔
1106
        }
1107
    }
1108
}
1,424✔
1109

1110

1111
//-----------------------------------------------------------------------------
1112

1113
void Tree::resolve()
360✔
1114
{
1115
    if(m_size == 0)
360✔
1116
        return;
8✔
1117
    ReferenceResolver rr;
352✔
1118
    resolve(&rr);
352✔
1119
}
352✔
1120

1121
void Tree::resolve(ReferenceResolver *C4_RESTRICT rr)
352✔
1122
{
1123
    if(m_size == 0)
352✔
1124
        return;
×
1125
    rr->resolve(this);
352✔
1126
}
1127

1128

1129
//-----------------------------------------------------------------------------
1130

1131
id_type Tree::num_children(id_type node) const
12,645,004✔
1132
{
1133
    id_type count = 0;
12,645,004✔
1134
    for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
49,731,044✔
1135
        ++count;
37,086,040✔
1136
    return count;
12,644,988✔
1137
}
1138

1139
id_type Tree::child(id_type node, id_type pos) const
3,107,312✔
1140
{
1141
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
3,107,312✔
1142
    id_type count = 0;
3,107,304✔
1143
    for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
11,416,712✔
1144
    {
1145
        if(count++ == pos)
11,415,712✔
1146
            return i;
3,106,304✔
1147
    }
1148
    return NONE;
992✔
1149
}
1150

1151
id_type Tree::child_pos(id_type node, id_type ch) const
32✔
1152
{
1153
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
32✔
1154
    id_type count = 0;
32✔
1155
    for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
32✔
1156
    {
1157
        if(i == ch)
32✔
1158
            return count;
32✔
1159
        ++count;
×
1160
    }
1161
    return NONE;
×
1162
}
1163

1164
#if defined(__clang__)
1165
#   pragma clang diagnostic push
1166
#   pragma GCC diagnostic ignored "-Wnull-dereference"
1167
#elif defined(__GNUC__)
1168
#   pragma GCC diagnostic push
1169
#   if __GNUC__ >= 6
1170
#       pragma GCC diagnostic ignored "-Wnull-dereference"
1171
#   endif
1172
#   if __GNUC__ > 9
1173
#       pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
1174
#   endif
1175
#endif
1176

1177
id_type Tree::find_child(id_type node, csubstr const& name) const
2,744,256✔
1178
{
1179
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
2,744,256✔
1180
    _RYML_CB_ASSERT(m_callbacks, is_map(node));
2,744,240✔
1181
    if(get(node)->m_first_child == NONE)
2,744,232✔
1182
    {
1183
        _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child == NONE);
608✔
1184
        return NONE;
608✔
1185
    }
1186
    else
1187
    {
1188
        _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child != NONE);
2,743,624✔
1189
    }
1190
    for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
13,945,664✔
1191
    {
1192
        if(_p(i)->m_key.scalar == name)
27,887,728✔
1193
        {
1194
            return i;
2,741,824✔
1195
        }
1196
    }
1197
    return NONE;
1,800✔
1198
}
1199

1200
#if defined(__clang__)
1201
#   pragma clang diagnostic pop
1202
#elif defined(__GNUC__)
1203
#   pragma GCC diagnostic pop
1204
#endif
1205

1206
namespace {
1207
id_type depth_desc_(Tree const& C4_RESTRICT t, id_type id, id_type currdepth=0, id_type maxdepth=0)
2,928✔
1208
{
1209
    maxdepth = currdepth > maxdepth ? currdepth : maxdepth;
2,928✔
1210
    for(id_type child = t.first_child(id); child != NONE; child = t.next_sibling(child))
4,888✔
1211
    {
1212
        const id_type d = depth_desc_(t, child, currdepth+1, maxdepth);
1,960✔
1213
        maxdepth = d > maxdepth ? d : maxdepth;
1,960✔
1214
    }
1215
    return maxdepth;
2,920✔
1216
}
1217
}
1218

1219
id_type Tree::depth_desc(id_type node) const
976✔
1220
{
1221
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
976✔
1222
    return depth_desc_(*this, node);
968✔
1223
}
1224

1225
id_type Tree::depth_asc(id_type node) const
160✔
1226
{
1227
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
160✔
1228
    id_type depth = 0;
152✔
1229
    while(!is_root(node))
416✔
1230
    {
1231
        ++depth;
264✔
1232
        node = parent(node);
264✔
1233
    }
1234
    return depth;
144✔
1235
}
1236

1237

1238
//-----------------------------------------------------------------------------
1239

1240
void Tree::to_val(id_type node, csubstr val, type_bits more_flags)
20,888✔
1241
{
1242
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
20,888✔
1243
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node));
39,232✔
1244
    _set_flags(node, VAL|more_flags);
20,888✔
1245
    _p(node)->m_key.clear();
20,888✔
1246
    _p(node)->m_val = val;
20,888✔
1247
}
20,888✔
1248

1249
void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags)
25,976✔
1250
{
1251
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
25,976✔
1252
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
51,952✔
1253
    _set_flags(node, KEYVAL|more_flags);
25,976✔
1254
    _p(node)->m_key = key;
25,976✔
1255
    _p(node)->m_val = val;
25,976✔
1256
}
25,976✔
1257

1258
void Tree::to_map(id_type node, type_bits more_flags)
8,152✔
1259
{
1260
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
8,152✔
1261
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node)); // parent must not have children with keys
16,160✔
1262
    _set_flags(node, MAP|more_flags);
8,152✔
1263
    _p(node)->m_key.clear();
8,152✔
1264
    _p(node)->m_val.clear();
8,152✔
1265
}
8,152✔
1266

1267
void Tree::to_map(id_type node, csubstr key, type_bits more_flags)
2,504✔
1268
{
1269
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
2,504✔
1270
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
5,008✔
1271
    _set_flags(node, KEY|MAP|more_flags);
2,504✔
1272
    _p(node)->m_key = key;
2,504✔
1273
    _p(node)->m_val.clear();
2,504✔
1274
}
2,504✔
1275

1276
void Tree::to_seq(id_type node, type_bits more_flags)
3,216✔
1277
{
1278
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
3,216✔
1279
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_seq(node));
6,400✔
1280
    _set_flags(node, SEQ|more_flags);
3,216✔
1281
    _p(node)->m_key.clear();
3,216✔
1282
    _p(node)->m_val.clear();
3,216✔
1283
}
3,216✔
1284

1285
void Tree::to_seq(id_type node, csubstr key, type_bits more_flags)
2,608✔
1286
{
1287
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
2,608✔
1288
    _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
5,216✔
1289
    _set_flags(node, KEY|SEQ|more_flags);
2,608✔
1290
    _p(node)->m_key = key;
2,608✔
1291
    _p(node)->m_val.clear();
2,608✔
1292
}
2,608✔
1293

1294
void Tree::to_doc(id_type node, type_bits more_flags)
9,032✔
1295
{
1296
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
9,032✔
1297
    _set_flags(node, DOC|more_flags);
9,032✔
1298
    _p(node)->m_key.clear();
9,032✔
1299
    _p(node)->m_val.clear();
9,032✔
1300
}
9,032✔
1301

1302
void Tree::to_stream(id_type node, type_bits more_flags)
8✔
1303
{
1304
    _RYML_CB_ASSERT(m_callbacks,  ! has_children(node));
8✔
1305
    _set_flags(node, STREAM|more_flags);
8✔
1306
    _p(node)->m_key.clear();
8✔
1307
    _p(node)->m_val.clear();
8✔
1308
}
8✔
1309

1310

1311
//-----------------------------------------------------------------------------
1312
id_type Tree::num_tag_directives() const
2,234,152✔
1313
{
1314
    // this assumes we have a very small number of tag directives
1315
    for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
2,259,728✔
1316
        if(m_tag_directives[i].handle.empty())
4,519,408✔
1317
            return i;
2,234,128✔
1318
    return RYML_MAX_TAG_DIRECTIVES;
24✔
1319
}
1320

1321
void Tree::clear_tag_directives()
8✔
1322
{
1323
    for(TagDirective &td : m_tag_directives)
40✔
1324
        td = {};
32✔
1325
}
8✔
1326

1327
id_type Tree::add_tag_directive(TagDirective const& td)
5,648✔
1328
{
1329
    _RYML_CB_CHECK(m_callbacks, !td.handle.empty());
11,296✔
1330
    _RYML_CB_CHECK(m_callbacks, !td.prefix.empty());
11,296✔
1331
    _RYML_CB_CHECK(m_callbacks, td.handle.begins_with('!'));
5,640✔
1332
    _RYML_CB_CHECK(m_callbacks, td.handle.ends_with('!'));
5,640✔
1333
    // https://yaml.org/spec/1.2.2/#rule-ns-word-char
1334
    _RYML_CB_CHECK(m_callbacks, td.handle == '!' || td.handle == "!!" || td.handle.trim('!').first_not_of("01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-") == npos);
18,568✔
1335
    id_type pos = num_tag_directives();
5,640✔
1336
    _RYML_CB_CHECK(m_callbacks, pos < RYML_MAX_TAG_DIRECTIVES);
5,640✔
1337
    m_tag_directives[pos] = td;
5,632✔
1338
    return pos;
5,632✔
1339
}
1340

1341
bool Tree::add_tag_directive(csubstr directive_)
5,608✔
1342
{
1343
    TagDirective td;
5,608✔
1344
    if(td.create_from_str(directive_, this))
5,608✔
1345
    {
1346
        add_tag_directive(td);
5,592✔
1347
        return true;
5,584✔
1348
    }
1349
    return false;
×
1350
}
1351

1352
size_t Tree::resolve_tag(substr output, csubstr tag, id_type node_id) const
26,008✔
1353
{
1354
    // lookup from the end. We want to find the first directive that
1355
    // matches the tag and has a target node id leq than the given
1356
    // node_id.
1357
    for(id_type i = RYML_MAX_TAG_DIRECTIVES-1; i != (id_type)-1; --i)
119,312✔
1358
    {
1359
        auto const& td = m_tag_directives[i];
103,512✔
1360
        if(td.handle.empty())
207,024✔
1361
            continue;
80,280✔
1362
        if(tag.begins_with(td.handle) && td.next_node_id <= node_id)
23,232✔
1363
            return td.transform(tag, output, m_callbacks);
10,208✔
1364
    }
1365
    if(tag.begins_with('!'))
15,800✔
1366
    {
1367
        if(is_custom_tag(tag))
10,472✔
1368
        {
1369
            _RYML_CB_ERR(m_callbacks, "tag directive not found");
8✔
1370
        }
1371
    }
1372
    return 0; // return 0 to signal that the tag is local and cannot be resolved
15,792✔
1373
}
1374

1375
namespace {
1376
csubstr _transform_tag(Tree *t, csubstr tag, id_type node)
8,872✔
1377
{
1378
    _c4dbgpf("[{}] resolving tag ~~~{}~~~", node, tag);
4,436✔
1379
    size_t required_size = t->resolve_tag(substr{}, tag, node);
8,872✔
1380
    if(!required_size)
8,872✔
1381
    {
1382
        if(tag.begins_with("!<"))
7,144✔
1383
            tag = tag.sub(1);
5,432✔
1384
        _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, tag);
3,572✔
1385
        return tag;
7,144✔
1386
    }
1387
    const char *prev_arena = t->arena().str;(void)prev_arena;
1,728✔
1388
    substr buf = t->alloc_arena(required_size);
1,728✔
1389
    _RYML_CB_ASSERT(t->m_callbacks, t->arena().str == prev_arena);
1,728✔
1390
    size_t actual_size = t->resolve_tag(buf, tag, node);
1,728✔
1391
    _RYML_CB_ASSERT(t->m_callbacks, actual_size <= required_size);
1,728✔
1392
    _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, buf.first(actual_size));
864✔
1393
    return buf.first(actual_size);
3,456✔
1394
}
1395
void _resolve_tags(Tree *t, id_type node)
20,200✔
1396
{
1397
    NodeData *C4_RESTRICT d = t->_p(node);
20,200✔
1398
    if(d->m_type & KEYTAG)
60,600✔
1399
        d->m_key.tag = _transform_tag(t, d->m_key.tag, node);
16✔
1400
    if(d->m_type & VALTAG)
60,600✔
1401
        d->m_val.tag = _transform_tag(t, d->m_val.tag, node);
8,856✔
1402
    for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
35,720✔
1403
        _resolve_tags(t, child);
15,520✔
1404
}
20,200✔
1405
size_t _count_resolved_tags_size(Tree const* t, id_type node)
20,352✔
1406
{
1407
    size_t sz = 0;
20,352✔
1408
    NodeData const* C4_RESTRICT d = t->_p(node);
20,352✔
1409
    if(d->m_type & KEYTAG)
61,056✔
1410
        sz += t->resolve_tag(substr{}, d->m_key.tag, node);
16✔
1411
    if(d->m_type & VALTAG)
61,056✔
1412
        sz += t->resolve_tag(substr{}, d->m_val.tag, node);
8,912✔
1413
    for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
35,816✔
1414
        sz += _count_resolved_tags_size(t, child);
15,616✔
1415
    return sz;
20,200✔
1416
}
1417
void _normalize_tags(Tree *t, id_type node)
168✔
1418
{
1419
    NodeData *C4_RESTRICT d = t->_p(node);
168✔
1420
    if(d->m_type & KEYTAG)
504✔
1421
        d->m_key.tag = normalize_tag(d->m_key.tag);
8✔
1422
    if(d->m_type & VALTAG)
504✔
1423
        d->m_val.tag = normalize_tag(d->m_val.tag);
80✔
1424
    for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
328✔
1425
        _normalize_tags(t, child);
160✔
1426
}
168✔
1427
void _normalize_tags_long(Tree *t, id_type node)
1,557,096✔
1428
{
1429
    NodeData *C4_RESTRICT d = t->_p(node);
1,557,096✔
1430
    if(d->m_type & KEYTAG)
4,671,288✔
1431
        d->m_key.tag = normalize_tag_long(d->m_key.tag);
11,272✔
1432
    if(d->m_type & VALTAG)
4,671,288✔
1433
        d->m_val.tag = normalize_tag_long(d->m_val.tag);
72,912✔
1434
    for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
2,697,608✔
1435
        _normalize_tags_long(t, child);
1,140,512✔
1436
}
1,557,096✔
1437
} // namespace
1438

1439
void Tree::resolve_tags()
4,744✔
1440
{
1441
    if(empty())
4,744✔
1442
        return;
8✔
1443
    size_t needed_size = _count_resolved_tags_size(this, root_id());
4,736✔
1444
    if(needed_size)
4,680✔
1445
        reserve_arena(arena_size() + needed_size);
1,184✔
1446
    _resolve_tags(this, root_id());
4,680✔
1447
}
1448

1449
void Tree::normalize_tags()
8✔
1450
{
1451
    if(empty())
8✔
1452
        return;
×
1453
    _normalize_tags(this, root_id());
8✔
1454
}
1455

1456
void Tree::normalize_tags_long()
416,584✔
1457
{
1458
    if(empty())
416,584✔
1459
        return;
×
1460
    _normalize_tags_long(this, root_id());
416,584✔
1461
}
1462

1463

1464
//-----------------------------------------------------------------------------
1465

1466
csubstr Tree::lookup_result::resolved() const
40✔
1467
{
1468
    csubstr p = path.first(path_pos);
40✔
1469
    if(p.ends_with('.'))
40✔
1470
        p = p.first(p.len-1);
48✔
1471
    return p;
40✔
1472
}
1473

1474
csubstr Tree::lookup_result::unresolved() const
5,312✔
1475
{
1476
    return path.sub(path_pos);
10,624✔
1477
}
1478

1479
void Tree::_advance(lookup_result *r, size_t more)
2,072✔
1480
{
1481
    r->path_pos += more;
2,072✔
1482
    if(r->path.sub(r->path_pos).begins_with('.'))
4,144✔
1483
        ++r->path_pos;
320✔
1484
}
2,072✔
1485

1486
Tree::lookup_result Tree::lookup_path(csubstr path, id_type start) const
344✔
1487
{
1488
    if(start == NONE)
344✔
1489
        start = root_id();
240✔
1490
    lookup_result r(path, start);
344✔
1491
    if(path.empty())
344✔
1492
        return r;
×
1493
    _lookup_path(&r);
344✔
1494
    if(r.target == NONE && r.closest == start)
344✔
1495
        r.closest = NONE;
24✔
1496
    return r;
344✔
1497
}
1498

1499
id_type Tree::lookup_path_or_modify(csubstr default_value, csubstr path, id_type start)
176✔
1500
{
1501
    id_type target = _lookup_path_or_create(path, start);
176✔
1502
    if(parent_is_map(target))
176✔
1503
        to_keyval(target, key(target), default_value);
112✔
1504
    else
1505
        to_val(target, default_value);
64✔
1506
    return target;
176✔
1507
}
1508

1509
id_type Tree::lookup_path_or_modify(Tree const *src, id_type src_node, csubstr path, id_type start)
8✔
1510
{
1511
    id_type target = _lookup_path_or_create(path, start);
8✔
1512
    merge_with(src, src_node, target);
8✔
1513
    return target;
8✔
1514
}
1515

1516
id_type Tree::_lookup_path_or_create(csubstr path, id_type start)
184✔
1517
{
1518
    if(start == NONE)
184✔
1519
        start = root_id();
184✔
1520
    lookup_result r(path, start);
184✔
1521
    _lookup_path(&r);
184✔
1522
    if(r.target != NONE)
184✔
1523
    {
1524
        C4_ASSERT(r.unresolved().empty());
192✔
1525
        return r.target;
96✔
1526
    }
1527
    _lookup_path_modify(&r);
88✔
1528
    return r.target;
88✔
1529
}
1530

1531
void Tree::_lookup_path(lookup_result *r) const
528✔
1532
{
1533
    C4_ASSERT( ! r->unresolved().empty());
1,056✔
1534
    _lookup_path_token parent{"", type(r->closest)};
528✔
1535
    id_type node;
1536
    do
1537
    {
1538
        node = _next_node(r, &parent);
1,848✔
1539
        if(node != NONE)
1,848✔
1540
            r->closest = node;
1,696✔
1541
        if(r->unresolved().empty())
3,696✔
1542
        {
1543
            r->target = node;
376✔
1544
            return;
376✔
1545
        }
1546
    } while(node != NONE);
1,472✔
1547
}
1548

1549
void Tree::_lookup_path_modify(lookup_result *r)
88✔
1550
{
1551
    C4_ASSERT( ! r->unresolved().empty());
176✔
1552
    _lookup_path_token parent{"", type(r->closest)};
88✔
1553
    id_type node;
1554
    do
1555
    {
1556
        node = _next_node_modify(r, &parent);
224✔
1557
        if(node != NONE)
224✔
1558
            r->closest = node;
224✔
1559
        if(r->unresolved().empty())
448✔
1560
        {
1561
            r->target = node;
88✔
1562
            return;
88✔
1563
        }
1564
    } while(node != NONE);
136✔
1565
}
1566

1567
id_type Tree::_next_node(lookup_result * r, _lookup_path_token *parent) const
1,848✔
1568
{
1569
    _lookup_path_token token = _next_token(r, *parent);
1,848✔
1570
    if( ! token)
1,848✔
1571
        return NONE;
×
1572

1573
    id_type node = NONE;
1,848✔
1574
    csubstr prev = token.value;
1,848✔
1575
    if(token.type == MAP || token.type == SEQ)
3,120✔
1576
    {
1577
        _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
1,008✔
1578
        //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1579
        _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
2,016✔
1580
        node = find_child(r->closest, token.value);
1,008✔
1581
    }
1582
    else if(token.type == KEYVAL)
840✔
1583
    {
1584
        _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
704✔
1585
        if(is_map(r->closest))
704✔
1586
            node = find_child(r->closest, token.value);
344✔
1587
    }
1588
    else if(token.type == KEY)
488✔
1589
    {
1590
        _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
488✔
1591
        token.value = token.value.offs(1, 1).trim(' ');
488✔
1592
        id_type idx = 0;
488✔
1593
        _RYML_CB_CHECK(m_callbacks, from_chars(token.value, &idx));
488✔
1594
        node = child(r->closest, idx);
488✔
1595
    }
1596
    else
1597
    {
1598
        C4_NEVER_REACH();
×
1599
    }
1600

1601
    if(node != NONE)
1,848✔
1602
    {
1603
        *parent = token;
1,696✔
1604
    }
1605
    else
1606
    {
1607
        csubstr p = r->path.sub(r->path_pos > 0 ? r->path_pos - 1 : r->path_pos);
152✔
1608
        r->path_pos -= prev.len;
152✔
1609
        if(p.begins_with('.'))
152✔
1610
            r->path_pos -= 1u;
32✔
1611
    }
1612

1613
    return node;
1,848✔
1614
}
1615

1616
id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
224✔
1617
{
1618
    _lookup_path_token token = _next_token(r, *parent);
224✔
1619
    if( ! token)
224✔
1620
        return NONE;
×
1621

1622
    id_type node = NONE;
224✔
1623
    if(token.type == MAP || token.type == SEQ)
400✔
1624
    {
1625
        _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
88✔
1626
        //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1627
        if( ! is_container(r->closest))
176✔
1628
        {
1629
            if(has_key(r->closest))
112✔
1630
                to_map(r->closest, key(r->closest));
40✔
1631
            else
1632
                to_map(r->closest);
16✔
1633
        }
1634
        else
1635
        {
1636
            if(is_map(r->closest))
64✔
1637
                node = find_child(r->closest, token.value);
32✔
1638
            else
1639
            {
1640
                id_type pos = NONE;
×
1641
                _RYML_CB_CHECK(m_callbacks, c4::atox(token.value, &pos));
×
1642
                _RYML_CB_ASSERT(m_callbacks, pos != NONE);
×
1643
                node = child(r->closest, pos);
×
1644
            }
1645
        }
1646
        if(node == NONE)
88✔
1647
        {
1648
            _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
176✔
1649
            node = append_child(r->closest);
88✔
1650
            NodeData *n = _p(node);
88✔
1651
            n->m_key.scalar = token.value;
88✔
1652
            n->m_type.add(KEY);
88✔
1653
        }
1654
    }
1655
    else if(token.type == KEYVAL)
136✔
1656
    {
1657
        _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
128✔
1658
        if(is_map(r->closest))
128✔
1659
        {
1660
            node = find_child(r->closest, token.value);
40✔
1661
            if(node == NONE)
40✔
1662
                node = append_child(r->closest);
80✔
1663
        }
1664
        else
1665
        {
1666
            _RYML_CB_ASSERT(m_callbacks, !is_seq(r->closest));
48✔
1667
            _add_flags(r->closest, MAP);
24✔
1668
            node = append_child(r->closest);
48✔
1669
        }
1670
        NodeData *n = _p(node);
64✔
1671
        n->m_key.scalar = token.value;
64✔
1672
        n->m_val.scalar = "";
64✔
1673
        n->m_type.add(KEYVAL);
64✔
1674
    }
1675
    else if(token.type == KEY)
72✔
1676
    {
1677
        _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
72✔
1678
        token.value = token.value.offs(1, 1).trim(' ');
72✔
1679
        id_type idx;
1680
        if( ! from_chars(token.value, &idx))
72✔
1681
             return NONE;
×
1682
        if( ! is_container(r->closest))
144✔
1683
        {
1684
            if(has_key(r->closest))
112✔
1685
            {
1686
                csubstr k = key(r->closest);
40✔
1687
                _clear_type(r->closest);
40✔
1688
                to_seq(r->closest, k);
40✔
1689
            }
1690
            else
1691
            {
1692
                _clear_type(r->closest);
16✔
1693
                to_seq(r->closest);
16✔
1694
            }
1695
        }
1696
        _RYML_CB_ASSERT(m_callbacks, is_container(r->closest));
144✔
1697
        node = child(r->closest, idx);
72✔
1698
        if(node == NONE)
72✔
1699
        {
1700
            _RYML_CB_ASSERT(m_callbacks, num_children(r->closest) <= idx);
72✔
1701
            for(id_type i = num_children(r->closest); i <= idx; ++i)
224✔
1702
            {
1703
                node = append_child(r->closest);
152✔
1704
                if(i < idx)
152✔
1705
                {
1706
                    if(is_map(r->closest))
160✔
1707
                        to_keyval(node, /*"~"*/{}, /*"~"*/{});
×
1708
                    else if(is_seq(r->closest))
160✔
1709
                        to_val(node, /*"~"*/{});
80✔
1710
                }
1711
            }
1712
        }
1713
    }
1714
    else
1715
    {
1716
        C4_NEVER_REACH();
×
1717
    }
1718

1719
    _RYML_CB_ASSERT(m_callbacks, node != NONE);
224✔
1720
    *parent = token;
224✔
1721
    return node;
224✔
1722
}
1723

1724
/* types of tokens:
1725
 * - seeing "map."  ---> "map"/MAP
1726
 * - finishing "scalar" ---> "scalar"/KEYVAL
1727
 * - seeing "seq[n]" ---> "seq"/SEQ (--> "[n]"/KEY)
1728
 * - seeing "[n]" ---> "[n]"/KEY
1729
 */
1730
Tree::_lookup_path_token Tree::_next_token(lookup_result *r, _lookup_path_token const& parent) const
2,072✔
1731
{
1732
    csubstr unres = r->unresolved();
2,072✔
1733
    if(unres.empty())
2,072✔
1734
        return {};
×
1735

1736
    // is it an indexation like [0], [1], etc?
1737
    if(unres.begins_with('['))
2,072✔
1738
    {
1739
        size_t pos = unres.find(']');
560✔
1740
        if(pos == csubstr::npos)
560✔
1741
            return {};
×
1742
        csubstr idx = unres.first(pos + 1);
560✔
1743
        _advance(r, pos + 1);
560✔
1744
        return {idx, KEY};
560✔
1745
    }
1746

1747
    // no. so it must be a name
1748
    size_t pos = unres.first_of(".[");
1,512✔
1749
    if(pos == csubstr::npos)
1,512✔
1750
    {
1751
        _advance(r, unres.len);
416✔
1752
        NodeType t;
1753
        if(( ! parent) || parent.type.is_seq())
832✔
1754
            return {unres, VAL};
×
1755
        return {unres, KEYVAL};
416✔
1756
    }
1757

1758
    // it's either a map or a seq
1759
    _RYML_CB_ASSERT(m_callbacks, unres[pos] == '.' || unres[pos] == '[');
1,568✔
1760
    if(unres[pos] == '.')
1,096✔
1761
    {
1762
        _RYML_CB_ASSERT(m_callbacks, pos != 0);
624✔
1763
        _advance(r, pos + 1);
624✔
1764
        return {unres.first(pos), MAP};
624✔
1765
    }
1766

1767
    _RYML_CB_ASSERT(m_callbacks, unres[pos] == '[');
472✔
1768
    _advance(r, pos);
472✔
1769
    return {unres.first(pos), SEQ};
472✔
1770
}
1771

1772

1773
} // namespace yml
1774
} // namespace c4
1775

1776

1777
C4_SUPPRESS_WARNING_GCC_CLANG_POP
1778
C4_SUPPRESS_WARNING_MSVC_POP
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