rapidyaml  0.10.0
parse and emit YAML, and do it fast
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_EXTRA_SCALAR_HPP_
12 #include "c4/yml/extra/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 
24 C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wold-style-cast")
25 C4_SUPPRESS_WARNING_CLANG_WITH_PUSH("-Wold-style-cast")
26 // NOLINTBEGIN(hicpp-signed-bitwise)
27 
28 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
31 
32 namespace c4 {
33 namespace yml {
34 namespace extra {
35 
36 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  #ifdef RYML_NO_COVERAGE__TO_BE_DELETED
60  if(tag.begins_with('<'))
61  {
62  append(" ");
63  append(tag);
64  }
65  else
66  #endif
67  if(tag.begins_with("!<"))
68  {
69  append(" ");
70  append(tag.sub(1));
71  }
72  else if(tag.begins_with('!'))
73  {
74  append(" <");
75  append(tag);
76  append(">");
77  }
78  else
79  {
80  append(" <!");
81  append(tag);
82  append(">");
83  }
84  }
85  has_tag = false;
86  };
87  bool has_anchor = false;
88  csubstr anchor;
89  auto maybe_append_anchor = [&]{
90  if(has_anchor)
91  {
92  append(" &");
93  append(anchor);
94  }
95  has_anchor = false;
96  };
97  auto append_cont = [&](csubstr evt, csubstr style){
98  append(evt);
99  if(style.len)
100  {
101  append(" ");
102  append(style);
103  }
104  maybe_append_anchor();
105  maybe_append_tag();
106  append("\n");
107  };
108  auto append_val = [&](csubstr evt, csubstr val){
109  append("=VAL");
110  maybe_append_anchor();
111  maybe_append_tag();
112  append(" ");
113  append(evt);
114  substr buf = sz <= evts_test_suite.len ? evts_test_suite.sub(sz) : evts_test_suite.last(0);
115  sz += escape_scalar(buf, val);
116  append("\n");
117  };
118  for(ievt::DataType i = 0; i < evts_ints_sz; )
119  {
120  ievt::DataType evt = evts_ints[i];
121  if(evt & ievt::SCLR)
122  {
123  csubstr s = getstr(i);
124  if(evt & ievt::SQUO)
125  append_val("'", s);
126  else if(evt & ievt::DQUO)
127  append_val("\"", s);
128  else if(evt & ievt::LITL)
129  append_val("|", s);
130  else if(evt & ievt::FOLD)
131  append_val(">", s);
132  else //if(evt & ievt::PLAI)
133  append_val(":", s);
134  }
135  else if(evt & ievt::BSEQ)
136  {
137  if(evt & ievt::FLOW)
138  append_cont("+SEQ", "[]");
139  else
140  append_cont("+SEQ", "");
141  }
142  else if(evt & ievt::ESEQ)
143  {
144  append("-SEQ\n");
145  }
146  else if(evt & ievt::BMAP)
147  {
148  if(evt & ievt::FLOW)
149  append_cont("+MAP", "{}");
150  else
151  append_cont("+MAP", "");
152  }
153  else if(evt & ievt::EMAP)
154  {
155  append("-MAP\n");
156  }
157  else if(evt & ievt::ALIA)
158  {
159  append("=ALI *");
160  append(getstr(i));
161  append("\n");
162  }
163  else if(evt & ievt::TAG_)
164  {
165  has_tag = true;
166  tag = getstr(i);
167  }
168  else if(evt & ievt::ANCH)
169  {
170  has_anchor = true;
171  anchor = getstr(i);
172  }
173  else if(evt & ievt::BDOC)
174  {
175  if(evt & ievt::EXPL)
176  append("+DOC ---\n");
177  else
178  append("+DOC\n");
179  }
180  else if(evt & ievt::EDOC)
181  {
182  if(evt & ievt::EXPL)
183  append("-DOC ...\n");
184  else
185  append("-DOC\n");
186  }
187  else if(evt & ievt::BSTR)
188  {
189  append("+STR\n");
190  }
191  else if(evt & ievt::ESTR)
192  {
193  append("-STR\n");
194  }
195 
196  i += (evt & ievt::WSTR) ? 3 : 1;
197  }
198  return sz;
199 }
200 
201 } // namespace extra
202 } // namespace yml
203 } // namespace c4
204 
205 // NOLINTEND(hicpp-signed-bitwise)
206 C4_SUPPRESS_WARNING_CLANG_POP
207 C4_SUPPRESS_WARNING_GCC_POP
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.
size_t escape_scalar(substr buffer, csubstr val)
Definition: scalar.cpp:20
@ LITL
block literal scalar (|)
@ DQUO
double-quoted scalar ("")
@ FOLD
block folded scalar (>)
@ BSTR
+STR begin stream
@ AREN
IMPORTANT. Marks events whose string was placed in the arena. This happens when the filtered string i...
@ ALIA
*ref =ALI alias (reference)
@ EXPL
--- (with BDOC) or ... (with EDOC)
@ SQUO
single-quoted scalar (')
int32_t DataType
data type for integer events.
@ FLOW
reading is inside explicit flow chars: [] or {}
Definition: common.cpp:12