@@ -16,19 +16,11 @@ namespace c4 {
1616namespace yml {
1717
1818
19- csubstr serialize_to_arena (Tree * C4_RESTRICT tree, csubstr a)
19+ csubstr serialize_to_arena_str (Tree * C4_RESTRICT tree, csubstr a)
2020{
2121 if (a.len > 0 )
2222 {
23- substr rem (tree->m_arena .sub (tree->m_arena_pos ));
24- size_t num = to_chars (rem, a);
25- if (num > rem.len )
26- {
27- rem = tree->_grow_arena (num);
28- num = to_chars (rem, a);
29- _RYML_ASSERT_VISIT_ (tree->m_callbacks , num <= rem.len , tree, NONE);
30- }
31- return tree->_request_span (num);
23+ return serialize_to_arena_scalar<csubstr>(tree, a);
3224 }
3325 else
3426 {
@@ -89,7 +81,7 @@ NodeRef Tree::operator[] (csubstr key)
8981}
9082ConstNodeRef Tree::operator [] (csubstr key) const
9183{
92- return rootref ()[key];
84+ return crootref ()[key];
9385}
9486
9587NodeRef Tree::operator [] (id_type i)
@@ -98,7 +90,7 @@ NodeRef Tree::operator[] (id_type i)
9890}
9991ConstNodeRef Tree::operator [] (id_type i) const
10092{
101- return rootref ()[i];
93+ return crootref ()[i];
10294}
10395
10496NodeRef Tree::docref (id_type i)
@@ -107,11 +99,11 @@ NodeRef Tree::docref(id_type i)
10799}
108100ConstNodeRef Tree::docref (id_type i) const
109101{
110- return cref ( doc (i));
102+ return ConstNodeRef ( this , doc (i));
111103}
112104ConstNodeRef Tree::cdocref (id_type i) const
113105{
114- return cref ( doc (i));
106+ return ConstNodeRef ( this , doc (i));
115107}
116108
117109
@@ -937,6 +929,7 @@ void Tree::remove_children(id_type node)
937929 }
938930}
939931
932+ // -----------------------------------------------------------------------------
940933bool Tree::change_type (id_type node, NodeType type)
941934{
942935 _RYML_ASSERT_VISIT_ (m_callbacks, type.is_val () || type.is_map () || type.is_seq (), this , node);
@@ -1150,9 +1143,8 @@ void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
11501143 remove_children (dst_node);
11511144 _clear_type (dst_node);
11521145 if (src->has_key (src_node))
1153- to_seq (dst_node, src->key (src_node));
1154- else
1155- to_seq (dst_node);
1146+ set_key (dst_node, src->key (src_node));
1147+ set_seq (dst_node);
11561148 _p (dst_node)->m_type = src->_p (src_node)->m_type ;
11571149 }
11581150 for (id_type sch = src->first_child (src_node); sch != NONE; sch = src->next_sibling (sch))
@@ -1171,9 +1163,8 @@ void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
11711163 remove_children (dst_node);
11721164 _clear_type (dst_node);
11731165 if (src->has_key (src_node))
1174- to_map (dst_node, src->key (src_node));
1175- else
1176- to_map (dst_node);
1166+ set_key (dst_node, src->key (src_node));
1167+ set_map (dst_node);
11771168 _p (dst_node)->m_type = src->_p (src_node)->m_type ;
11781169 }
11791170 for (id_type sch = src->first_child (src_node); sch != NONE; sch = src->next_sibling (sch))
@@ -1259,7 +1250,7 @@ id_type Tree::find_child(id_type node, csubstr const& name) const
12591250{
12601251 _RYML_ASSERT_VISIT_ (m_callbacks, node != NONE, this , node);
12611252 _RYML_ASSERT_VISIT_ (m_callbacks, is_map (node), this , node);
1262- if (get (node)->m_first_child == NONE)
1253+ if (_p (node)->m_first_child == NONE)
12631254 {
12641255 _RYML_ASSERT_VISIT_ (m_callbacks, _p (node)->m_last_child == NONE, this , node);
12651256 return NONE;
@@ -1335,6 +1326,7 @@ void Tree::to_val(id_type node, csubstr val, type_bits more_flags)
13351326{
13361327 _RYML_ASSERT_VISIT_ (m_callbacks, ! has_children (node), this , node);
13371328 _RYML_ASSERT_VISIT_ (m_callbacks, parent (node) == NONE || ! parent_is_map (node), this , node);
1329+ _RYML_ASSERT_VISIT_ (m_callbacks, !is_seq (node) && !is_map (node), this , node);
13381330 _set_flags (node, VAL|more_flags);
13391331 _p (node)->m_key .clear ();
13401332 _p (node)->m_val = val;
@@ -1344,6 +1336,7 @@ void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flag
13441336{
13451337 _RYML_ASSERT_VISIT_ (m_callbacks, ! has_children (node), this , node);
13461338 _RYML_ASSERT_VISIT_ (m_callbacks, parent (node) == NONE || parent_is_map (node), this , node);
1339+ _RYML_ASSERT_VISIT_ (m_callbacks, !is_seq (node) && !is_map (node), this , node);
13471340 _set_flags (node, KEYVAL|more_flags);
13481341 _p (node)->m_key = key;
13491342 _p (node)->m_val = val;
@@ -1352,7 +1345,6 @@ void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flag
13521345void Tree::to_map (id_type node, type_bits more_flags)
13531346{
13541347 _RYML_ASSERT_VISIT_ (m_callbacks, ! has_children (node), this , node);
1355- _RYML_ASSERT_VISIT_ (m_callbacks, parent (node) == NONE || ! parent_is_map (node), this , node); // parent must not have children with keys
13561348 _set_flags (node, MAP|more_flags);
13571349 _p (node)->m_key .clear ();
13581350 _p (node)->m_val .clear ();
@@ -1605,10 +1597,7 @@ Tree::lookup_result Tree::lookup_path(csubstr path, id_type start) const
16051597id_type Tree::lookup_path_or_modify (csubstr default_value, csubstr path, id_type start)
16061598{
16071599 id_type target = _lookup_path_or_create (path, start);
1608- if (parent_is_map (target))
1609- to_keyval (target, key (target), default_value);
1610- else
1611- to_val (target, default_value);
1600+ set_val (target, default_value);
16121601 return target;
16131602}
16141603
@@ -1732,10 +1721,7 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
17321721 // _RYML_ASSERT_VISIT_(m_callbacks, is_container(r->closest) || r->closest == NONE);
17331722 if ( ! is_container (r->closest ))
17341723 {
1735- if (has_key (r->closest ))
1736- to_map (r->closest , key (r->closest ));
1737- else
1738- to_map (r->closest );
1724+ set_map (r->closest );
17391725 }
17401726 else
17411727 {
@@ -1789,17 +1775,7 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
17891775 return NONE;
17901776 if ( ! is_container (r->closest ))
17911777 {
1792- if (has_key (r->closest ))
1793- {
1794- csubstr k = key (r->closest );
1795- _clear_type (r->closest );
1796- to_seq (r->closest , k);
1797- }
1798- else
1799- {
1800- _clear_type (r->closest );
1801- to_seq (r->closest );
1802- }
1778+ set_seq (r->closest );
18031779 }
18041780 _RYML_ASSERT_VISIT_ (m_callbacks, is_container (r->closest ), this , r->closest );
18051781 node = child (r->closest , idx);
@@ -1812,9 +1788,16 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
18121788 if (i < idx)
18131789 {
18141790 if (is_map (r->closest ))
1815- to_keyval (node, /* "~"*/ {}, /* "~"*/ {});
1791+ {
1792+ _clear_type (node);
1793+ set_key (node, {});
1794+ set_val (node, {});
1795+ }
18161796 else if (is_seq (r->closest ))
1817- to_val (node, /* "~"*/ {});
1797+ {
1798+ _clear_type (node);
1799+ set_val (node, {});
1800+ }
18181801 }
18191802 }
18201803 }
0 commit comments