rapidyaml  0.10.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_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 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::TAGD, "TAGD"},
61  {yml::extra::ievt::TAGV, "TAGV"},
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  return c4::bm2str<ievt::EventFlags>((flags & ievt::MASK), buf.str, buf.len);
71 }
72 csubstr to_chars_sub(substr buf, ievt::DataType flags)
73 {
74  size_t reqsize = ievt::to_chars(buf, flags);
75  RYML_CHECK(reqsize > 0u);
76  RYML_CHECK(reqsize < buf.len);
77  return buf.first(reqsize - 1u);
78 }
79 } // namespace ievt
80 } // namespace extra
81 } // namespace yml
82 } // namespace c4
83 
84 
85 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 
89 namespace c4 {
90 namespace yml {
91 namespace extra {
92 
93 void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::DataType const* evts, ievt::DataType evts_sz)
94 {
95  char buf[200];
96  for(ievt::DataType evtpos = 0, evtnumber = 0;
97  evtpos < evts_sz;
98  ++evtnumber,
99  evtpos += ((evts[evtpos] & ievt::WSTR) ? 3 : 1))
100  {
101  ievt::DataType evt = evts[evtpos];
102  {
103  csubstr str = ievt::to_chars_sub(buf, evt);
104  printf("[%d][%d] %.*s(0x%x)", evtnumber, evtpos, (int)str.len, str.str, evt);
105  }
106  if (evt & ievt::WSTR)
107  {
108  bool in_arena = evt & ievt::AREN;
109  csubstr region = !in_arena ? parsed_yaml : arena;
110  bool safe = (evts[evtpos + 1] >= 0)
111  && (evts[evtpos + 2] >= 0)
112  && (evts[evtpos + 1] <= (int)region.len)
113  && ((evts[evtpos + 1] + evts[evtpos + 2]) <= (int)region.len);
114  const char *str = safe ? (region.str + evts[evtpos + 1]) : "ERR!!!";
115  int len = safe ? evts[evtpos + 2] : 6;
116  printf(": %d [%d]~~~%.*s~~~", evts[evtpos+1], evts[evtpos+2], len, str);
117  if(in_arena)
118  printf(" (arenasz=%zu)", arena.len);
119  else
120  printf(" (srcsz=%zu)", parsed_yaml.len);
121  }
122  printf("\n");
123  }
124 }
125 
126 } // namespace extra
127 } // namespace yml
128 } // namespace c4
129 
130 // NOLINTEND(hicpp-signed-bitwise)
131 C4_SUPPRESS_WARNING_CLANG_POP
132 C4_SUPPRESS_WARNING_GCC_POP
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:93
csubstr to_chars_sub(substr buf, ievt::DataType flags)
Convert bit mask of ievt::EventFlags to text.
Definition: ints_utils.cpp:72
@ PSTR
special flag to enable look-back in the event array. it signifies that the previous event has a strin...
@ TAGD
tag directive name : TAG <name> .......
@ LITL
block literal scalar (|)
@ UNFILT
special flag to mark a scalar as unfiltered (when the parser is set not to filter).
@ DQUO
double-quoted scalar ("")
@ FOLD
block folded scalar (>)
@ MASK
a mask of all bits in this enumeration with string: mask of all the events that encode a string follo...
@ BSTR
+STR begin stream
@ AREN
IMPORTANT. Marks events whose string was placed in the arena. This happens when the filtered string i...
@ TAGV
tag directive value: TAG ...... <value>
@ ALIA
*ref =ALI alias (reference)
@ EXPL
--- (with BDOC) or ... (with EDOC)
@ SQUO
single-quoted scalar (')
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
@ BLCK
reading in block mode
@ FLOW
reading is inside explicit flow chars: [] or {}
Definition: common.cpp:12