rapidyaml  0.12.0
parse and emit YAML, and do it fast
ints_utils.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_INTS_UTILS_HPP_
13 #endif
14 
15 #ifndef _C4_YML_ERROR_HPP_
16 #include "c4/yml/error.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 namespace c4 {
30 template<>
31 c4::EnumSymbols<yml::extra::ievt::EventFlags> esyms<yml::extra::ievt::EventFlags>()
32 {
33  static constexpr const EnumSymbols<yml::extra::ievt::EventFlags>::Sym syms[] = {
34  {yml::extra::ievt::KEY_, "KEY_"},
35  {yml::extra::ievt::VAL_, "VAL_"},
36  {yml::extra::ievt::SCLR, "SCLR"},
37  {yml::extra::ievt::BSEQ, "BSEQ"},
38  {yml::extra::ievt::ESEQ, "ESEQ"},
39  {yml::extra::ievt::BMAP, "BMAP"},
40  {yml::extra::ievt::EMAP, "EMAP"},
41  {yml::extra::ievt::ALIA, "ALIA"},
42  {yml::extra::ievt::ANCH, "ANCH"},
43  {yml::extra::ievt::TAG_, "TAG_"},
44  {yml::extra::ievt::PLAI, "PLAI"},
45  {yml::extra::ievt::SQUO, "SQUO"},
46  {yml::extra::ievt::DQUO, "DQUO"},
47  {yml::extra::ievt::LITL, "LITL"},
48  {yml::extra::ievt::FOLD, "FOLD"},
49  {yml::extra::ievt::FLOW, "FLOW"},
50  {yml::extra::ievt::BLCK, "BLCK"},
51  {yml::extra::ievt::BDOC, "BDOC"},
52  {yml::extra::ievt::EDOC, "EDOC"},
53  {yml::extra::ievt::BSTR, "BSTR"},
54  {yml::extra::ievt::ESTR, "ESTR"},
55  {yml::extra::ievt::EXPL, "EXPL"},
56  {yml::extra::ievt::AREN, "AREN"},
57  {yml::extra::ievt::PSTR, "PSTR"},
58  {yml::extra::ievt::UNFILT, "UNFILT"},
59  {yml::extra::ievt::YAML, "YAML"},
60  {yml::extra::ievt::TAGH, "TAGH"},
61  {yml::extra::ievt::TAGP, "TAGP"},
62  };
63  return EnumSymbols<yml::extra::ievt::EventFlags>(syms);
64 }
65 namespace yml {
66 namespace extra {
67 namespace ievt {
68 size_t to_chars(substr buf, ievt::DataType flags)
69 {
70  flags &= ievt::MASK; // clear any other bits
71  return c4::bm2str<ievt::EventFlags>(flags, buf.str, buf.len);
72 }
73 csubstr to_chars_sub(substr buf, ievt::DataType flags)
74 {
75  size_t reqsize = ievt::to_chars(buf, flags);
76  _RYML_CHECK_BASIC(reqsize > 0u);
77  _RYML_CHECK_BASIC(reqsize < buf.len);
78  return buf.first(reqsize - 1u);
79 }
80 } // namespace ievt
81 } // namespace extra
82 } // namespace yml
83 } // namespace c4
84 
85 
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 
90 namespace c4 {
91 namespace yml {
92 namespace extra {
93 
94 void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::DataType const* evts, ievt::DataType evts_sz)
95 {
96  char buf[200];
97  for(ievt::DataType evtpos = 0, evtnumber = 0;
98  evtpos < evts_sz;
99  ++evtnumber,
100  evtpos += ((evts[evtpos] & ievt::WSTR) ? 3 : 1))
101  {
102  ievt::DataType evt = evts[evtpos];
103  csubstr flags = ievt::to_chars_sub(buf, evt);
104  printf("[%d][%d] %.*s(0x%x)", evtnumber, evtpos, (int)flags.len, flags.str, evt);
105  if (evt & ievt::WSTR)
106  {
107  bool in_arena = evt & ievt::AREN;
108  csubstr region = !in_arena ? parsed_yaml : arena;
109  bool safe = (evts[evtpos + 1] >= 0)
110  && (evts[evtpos + 2] >= 0)
111  && (evts[evtpos + 1] <= (int)region.len)
112  && (evts[evtpos + 2] <= ((int)region.len - evts[evtpos + 1]));
113  const char *str = safe ? (region.str + evts[evtpos + 1]) : "ERR!!!";
114  int len = safe ? evts[evtpos + 2] : 6;
115  printf(": %d [%d]~~~%.*s~~~", evts[evtpos+1], evts[evtpos+2], len, str);
116  if(in_arena)
117  printf(" (arenasz=%zu)", arena.len);
118  else
119  printf(" (srcsz=%zu)", parsed_yaml.len);
120  }
121  printf("\n");
122  }
123 }
124 
125 } // namespace extra
126 } // namespace yml
127 } // namespace c4
128 
129 // NOLINTEND(hicpp-signed-bitwise)
130 C4_SUPPRESS_WARNING_CLANG_POP
131 C4_SUPPRESS_WARNING_GCC_POP
Error utilities used by ryml.
void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::DataType const *evts, ievt::DataType evts_sz)
Print integer events to stdout.
Definition: ints_utils.cpp:94
csubstr to_chars_sub(substr buf, ievt::DataType flags)
Convert bit mask of ievt::EventFlags to text.
Definition: ints_utils.cpp:73
@ SCLR
scalar (=VAL in test suite events)
@ LITL
scalar: block literal (|)
@ UNFILT
special flag to mark a scalar as unfiltered (when the parser is set not to filter).
@ EMAP
end map (-MAP in test suite events)
@ DQUO
scalar: double-quoted ("")
@ FOLD
scalar: block folded (>)
@ BMAP
begin map (+MAP in test suite events)
@ TAGH
tag directive, handle: \TAG <handle> ........
@ MASK
a mask of all bits in this enumeration
@ 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
@ TAGP
tag directive, prefix: \TAG ........ <prefix>
@ VAL_
as value special flag to enable look-back in the event array. it signifies that the previous event ha...
@ BDOC
begin doc (+DOC in test suite events)
@ BLCK
container: block
@ AREN
IMPORTANT. Marks events whose string was placed in the arena. This happens when the filtered string i...
@ YAML
yaml directive: \YAML <version>
@ ALIA
*ref (reference)
@ EDOC
end doc (-DOC in test suite events)
@ EXPL
--- (with BDOC) or ... (with EDOC)
@ SQUO
scalar: single-quoted (')
int32_t DataType
data type for integer events.
size_t to_chars(substr buf, ievt::DataType flags)
Convert bit mask of ievt::EventFlags to text.
Definition: ints_utils.cpp:68
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14