rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
ints_to_testsuite.cpp
Go to the documentation of this file.
1#ifdef RYML_SINGLE_HEADER_INTS
2 #ifndef _RYML_SINGLE_HEADER_AMALGAMATED_HPP_
3 #include <ryml_ints.hpp>
4 #endif
5#elif defined(RYML_SINGLE_HEADER)
6 #ifndef _RYML_SINGLE_HEADER_AMALGAMATED_HPP_
7 #include <ryml_all.hpp>
8 #endif
9#endif
10
11#ifndef _C4_YML_ESCAPE_SCALAR_HPP_
13#endif
14
15#ifndef _C4_YML_EXTRA_INTS_UTILS_HPP_
17#endif
18
19#ifndef _C4_BITMASK_HPP_
20#include "c4/bitmask.hpp"
21#endif
22
23
24C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wold-style-cast")
25C4_SUPPRESS_WARNING_CLANG_WITH_PUSH("-Wold-style-cast")
26// NOLINTBEGIN(hicpp-signed-bitwise)
27
28//-----------------------------------------------------------------------------
29//-----------------------------------------------------------------------------
30//-----------------------------------------------------------------------------
31
32namespace c4 {
33namespace yml {
34namespace extra {
35
36C4_NODISCARD RYML_EXPORT size_t events_ints_to_testsuite(csubstr parsed_yaml,
37 csubstr arena,
38 ievt::DataType const* evts_ints,
39 ievt::DataType evts_ints_sz,
40 substr evts_test_suite)
41{
42 auto getstr = [&](ievt::DataType i){
43 bool in_arena = evts_ints[i] & ievt::AREN;
44 csubstr region = !in_arena ? parsed_yaml : arena;
45 return region.sub((size_t)evts_ints[i+1], (size_t)evts_ints[i+2]);
46 };
47 size_t sz = 0;
48 auto append = [&](csubstr s){
49 size_t next = sz + s.len;
50 if (s.len && (next <= evts_test_suite.len && evts_test_suite.len))
51 memcpy(evts_test_suite.str + sz, s.str, s.len);
52 sz = next;
53 };
54 bool has_tag = false;
55 csubstr tag;
56 auto maybe_append_tag = [&]{
57 if(has_tag)
58 {
59 if(tag.begins_with('<'))
60 {
61 append(" ");
62 append(tag);
63 }
64 else if(tag.begins_with("!<"))
65 {
66 append(" ");
67 append(tag.sub(1));
68 }
69 else if(tag.begins_with('!'))
70 {
71 append(" <");
72 append(tag);
73 append(">");
74 }
75 else
76 {
77 append(" <!");
78 append(tag);
79 append(">");
80 }
81 }
82 has_tag = false;
83 };
84 bool has_anchor = false;
85 csubstr anchor;
86 auto maybe_append_anchor = [&]{
87 if(has_anchor)
88 {
89 append(" &");
90 append(anchor);
91 }
92 has_anchor = false;
93 };
94 auto append_cont = [&](csubstr evt, csubstr style){
95 append(evt);
96 if(style.len)
97 {
98 append(" ");
99 append(style);
100 }
101 maybe_append_anchor();
102 maybe_append_tag();
103 append("\n");
104 };
105 auto append_esc = [&](csubstr str){
106 substr buf = sz <= evts_test_suite.len ? evts_test_suite.sub(sz) : evts_test_suite.last(0);
107 sz += escape_scalar(buf, str);
108 append("\n");
109 };
110 auto append_val = [&](csubstr evt, csubstr val){
111 append("=VAL");
112 maybe_append_anchor();
113 maybe_append_tag();
114 append(" ");
115 append(evt);
116 append_esc(val);
117 };
118 ievt::DataType evt = 0;
119 for(ievt::DataType i = 0; i < evts_ints_sz; i += (evt & ievt::WSTR) ? 3 : 1)
120 {
121 evt = evts_ints[i];
122 if(evt & ievt::SCLR)
123 {
124 csubstr s = getstr(i);
125 if(evt & ievt::SQUO)
126 append_val("'", s);
127 else if(evt & ievt::DQUO)
128 append_val("\"", s);
129 else if(evt & ievt::LITL)
130 append_val("|", s);
131 else if(evt & ievt::FOLD)
132 append_val(">", s);
133 else //if(evt & ievt::PLAI)
134 append_val(":", s);
135 }
136 else if((evt & ievt::BSEQ) == ievt::BSEQ)
137 {
138 if(evt & ievt::FLOW)
139 append_cont("+SEQ", "[]");
140 else
141 append_cont("+SEQ", "");
142 }
143 else if((evt & ievt::ESEQ) == ievt::ESEQ)
144 {
145 append("-SEQ\n");
146 }
147 else if((evt & ievt::BMAP) == ievt::BMAP)
148 {
149 if(evt & ievt::FLOW)
150 append_cont("+MAP", "{}");
151 else
152 append_cont("+MAP", "");
153 }
154 else if((evt & ievt::EMAP) == ievt::EMAP)
155 {
156 append("-MAP\n");
157 }
158 else if(evt & ievt::ALIA)
159 {
160 append("=ALI *");
161 append(getstr(i));
162 append("\n");
163 }
164 else if(evt & ievt::TAG_)
165 {
166 has_tag = true;
167 tag = getstr(i);
168 }
169 else if(evt & ievt::ANCH)
170 {
171 has_anchor = true;
172 anchor = getstr(i);
173 }
174 else if((evt & ievt::BDOC) == ievt::BDOC)
175 {
176 if(evt & ievt::EXPL)
177 append("+DOC ---\n");
178 else
179 append("+DOC\n");
180 }
181 else if((evt & ievt::EDOC) == ievt::EDOC)
182 {
183 if(evt & ievt::EXPL)
184 append("-DOC ...\n");
185 else
186 append("-DOC\n");
187 }
188 else if((evt & ievt::BSTR) == ievt::BSTR)
189 {
190 append("+STR\n");
191 }
192 else if((evt & ievt::ESTR) == ievt::ESTR)
193 {
194 append("-STR\n");
195 }
196 }
197 return sz;
198}
199
200} // namespace extra
201} // namespace yml
202} // namespace c4
203
204// NOLINTEND(hicpp-signed-bitwise)
205C4_SUPPRESS_WARNING_CLANG_POP
206C4_SUPPRESS_WARNING_GCC_POP
#define RYML_EXPORT
Definition export.hpp:18
size_t events_ints_to_testsuite(csubstr parsed_yaml, csubstr arena, ievt::DataType const *evts_ints, ievt::DataType evts_ints_sz, substr evts_test_suite)
Create a testsuite event string from integer events.
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
int32_t DataType
data type for integer events.
@ SCLR
scalar (=VAL in test suite events)
@ LITL
scalar: block literal (|)
@ EMAP
end map (-MAP in test suite events)
@ DQUO
scalar: double-quoted ("")
@ FOLD
scalar: block folded (>)
@ BMAP
begin map (+MAP in test suite events)
@ ESTR
end stream (-STR in test suite events)
@ BSTR
begin stream (+STR in test suite events)
@ BSEQ
begin seq (+SEQ in test suite events)
@ ESEQ
end seq (-SEQ in test suite events)
@ WSTR
WithSTRing: mask of all the events that encode a string following the event. For such events,...
@ FLOW
container: flow: [] for seqs or {} for maps
@ BDOC
begin doc (+DOC in test suite events)
@ AREN
IMPORTANT. Marks events whose string was placed in the arena. This happens when the filtered string i...
@ EDOC
end doc (-DOC in test suite events)
@ EXPL
--- (with BDOC) or ... (with EDOC)
@ SQUO
scalar: single-quoted (')
size_t escape_scalar(substr buffer, csubstr scalar, bool keep_newlines=false)
Escape a scalar to an existing buffer, using escape_scalar_fn.
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
bool begins_with(const C c) const noexcept
true if the first character of the string is c
Definition substr.hpp:851
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring last(size_t num) const noexcept
return the last num elements: [len-num,len[
Definition substr.hpp:537
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216