rapidyaml  0.7.0
parse and emit YAML, and do it fast
node_type.cpp
Go to the documentation of this file.
1 #include "c4/yml/node_type.hpp"
2 
3 namespace c4 {
4 namespace yml {
5 
6 const char* NodeType::type_str(NodeType_e ty) noexcept
7 {
8  switch(ty & _TYMASK)
9  {
10  case KEYVAL:
11  return "KEYVAL";
12  case KEY:
13  return "KEY";
14  case VAL:
15  return "VAL";
16  case MAP:
17  return "MAP";
18  case SEQ:
19  return "SEQ";
20  case KEYMAP:
21  return "KEYMAP";
22  case KEYSEQ:
23  return "KEYSEQ";
24  case DOCSEQ:
25  return "DOCSEQ";
26  case DOCMAP:
27  return "DOCMAP";
28  case DOCVAL:
29  return "DOCVAL";
30  case DOC:
31  return "DOC";
32  case STREAM:
33  return "STREAM";
34  case NOTYPE:
35  return "NOTYPE";
36  default:
37  if((ty & KEYVAL) == KEYVAL)
38  return "KEYVAL***";
39  if((ty & KEYMAP) == KEYMAP)
40  return "KEYMAP***";
41  if((ty & KEYSEQ) == KEYSEQ)
42  return "KEYSEQ***";
43  if((ty & DOCSEQ) == DOCSEQ)
44  return "DOCSEQ***";
45  if((ty & DOCMAP) == DOCMAP)
46  return "DOCMAP***";
47  if((ty & DOCVAL) == DOCVAL)
48  return "DOCVAL***";
49  if(ty & KEY)
50  return "KEY***";
51  if(ty & VAL)
52  return "VAL***";
53  if(ty & MAP)
54  return "MAP***";
55  if(ty & SEQ)
56  return "SEQ***";
57  if(ty & DOC)
58  return "DOC***";
59  return "(unk)";
60  }
61 }
62 
63 csubstr NodeType::type_str(substr buf, NodeType_e flags) noexcept
64 {
65  size_t pos = 0;
66  bool gotone = false;
67 
68  #define _prflag(fl, txt) \
69  do { \
70  if((flags & fl) == (fl)) \
71  { \
72  if(gotone) \
73  { \
74  if(pos + 1 < buf.len) \
75  buf[pos] = '|'; \
76  ++pos; \
77  } \
78  csubstr fltxt = txt; \
79  if(pos + fltxt.len <= buf.len) \
80  memcpy(buf.str + pos, fltxt.str, fltxt.len); \
81  pos += fltxt.len; \
82  gotone = true; \
83  flags = (flags & ~fl); /*remove the flag*/ \
84  } \
85  } while(0)
86 
87  _prflag(STREAM, "STREAM");
88  _prflag(DOC, "DOC");
89  // key properties
90  _prflag(KEY, "KEY");
91  _prflag(KEYTAG, "KTAG");
92  _prflag(KEYANCH, "KANCH");
93  _prflag(KEYREF, "KREF");
94  _prflag(KEY_LITERAL, "KLITERAL");
95  _prflag(KEY_FOLDED, "KFOLDED");
96  _prflag(KEY_SQUO, "KSQUO");
97  _prflag(KEY_DQUO, "KDQUO");
98  _prflag(KEY_PLAIN, "KPLAIN");
99  _prflag(KEY_UNFILT, "KUNFILT");
100  // val properties
101  _prflag(VAL, "VAL");
102  _prflag(VALTAG, "VTAG");
103  _prflag(VALANCH, "VANCH");
104  _prflag(VALREF, "VREF");
105  _prflag(VAL_UNFILT, "VUNFILT");
106  _prflag(VAL_LITERAL, "VLITERAL");
107  _prflag(VAL_FOLDED, "VFOLDED");
108  _prflag(VAL_SQUO, "VSQUO");
109  _prflag(VAL_DQUO, "VDQUO");
110  _prflag(VAL_PLAIN, "VPLAIN");
111  _prflag(VAL_UNFILT, "VUNFILT");
112  // container properties
113  _prflag(MAP, "MAP");
114  _prflag(SEQ, "SEQ");
115  _prflag(FLOW_SL, "FLOWSL");
116  _prflag(FLOW_ML, "FLOWML");
117  _prflag(BLOCK, "BLCK");
118  if(pos == 0)
119  _prflag(NOTYPE, "NOTYPE");
120 
121  #undef _prflag
122 
123  if(pos < buf.len)
124  {
125  buf[pos] = '\0';
126  return buf.first(pos);
127  }
128  else
129  {
130  csubstr failed;
131  failed.len = pos + 1;
132  failed.str = nullptr;
133  return failed;
134  }
135 }
136 
137 
138 //-----------------------------------------------------------------------------
139 
140 // see https://www.yaml.info/learn/quote.html#noplain
141 bool scalar_style_query_squo(csubstr s) noexcept
142 {
143  return ! s.first_of_any("\n ", "\n\t");
144 }
145 
146 // see https://www.yaml.info/learn/quote.html#noplain
147 bool scalar_style_query_plain(csubstr s) noexcept
148 {
149  if(s.begins_with("-."))
150  {
151  if(s == "-.inf" || s == "-.INF")
152  return true;
153  else if(s.sub(2).is_number())
154  return true;
155  }
156  return s != ':'
157  && ( ! s.begins_with_any("-:?*&,'\"{}[]|>%#@`\r")) // @ and ` are reserved characters
158  && ( ! s.ends_with_any(":#"))
159  // make this check in the last place, as it has linear
160  // complexity, while the previous ones are
161  // constant-time
162  && (s.first_of("\n#:[]{},") == npos);
163 }
164 
165 NodeType_e scalar_style_choose(csubstr s) noexcept
166 {
167  if(s.len)
168  {
169  if(s.begins_with_any(" \n\t")
170  ||
171  s.ends_with_any(" \n\t"))
172  {
173  return SCALAR_DQUO;
174  }
175  else if( ! scalar_style_query_plain(s))
176  {
178  }
179  // nothing remarkable - use plain
180  return SCALAR_PLAIN;
181  }
182  return s.str ? SCALAR_SQUO : SCALAR_PLAIN;
183 }
184 
186 {
187  // do not quote special cases
188  bool plain = (
189  (s == "true" || s == "false" || s == "null")
190  ||
191  (
192  // do not quote numbers
193  s.is_number()
194  &&
195  (
196  // quote integral numbers if they have a leading 0
197  // https://github.com/biojppm/rapidyaml/issues/291
198  (!(s.len > 1 && s.begins_with('0')))
199  // do not quote reals with leading 0
200  // https://github.com/biojppm/rapidyaml/issues/313
201  || (s.find('.') != csubstr::npos)
202  )
203  )
204  );
205  return plain ? SCALAR_PLAIN : SCALAR_DQUO;
206 }
207 
208 } // namespace yml
209 } // namespace c4
NodeType_e scalar_style_json_choose(csubstr s) noexcept
choose a json style based on the scalar's contents
Definition: node_type.cpp:185
bool scalar_style_query_squo(csubstr s) noexcept
query whether a scalar can be encoded using single quotes.
Definition: node_type.cpp:141
bool scalar_style_query_plain(csubstr s) noexcept
query whether a scalar can be encoded using plain style (no quotes, not a literal/folded block scalar...
Definition: node_type.cpp:147
NodeType_e scalar_style_choose(csubstr s) noexcept
choose a YAML emitting style based on the scalar's contents
Definition: node_type.cpp:165
NodeType_e
a bit mask for marking node types and styles
Definition: node_type.hpp:30
@ VALANCH
the val has an &anchor
Definition: node_type.hpp:42
@ NOTYPE
no node type or style is set
Definition: node_type.hpp:32
@ KEY_DQUO
mark key scalar as double quoted "
Definition: node_type.hpp:63
@ VALREF
a *reference: the val references an &anchor
Definition: node_type.hpp:40
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:35
@ STREAM
a stream: a seq of docs
Definition: node_type.hpp:38
@ KEY
is member of a map, must have non-empty key
Definition: node_type.hpp:33
@ VAL_FOLDED
mark val scalar as multiline, block folded >
Definition: node_type.hpp:60
@ KEYTAG
the key has a tag
Definition: node_type.hpp:43
@ SCALAR_SQUO
Definition: node_type.hpp:81
@ FLOW_SL
mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,...
Definition: node_type.hpp:54
@ FLOW_ML
(NOT IMPLEMENTED, work in progress) mark container with multi-line flow style (seqs as '[ val1,...
Definition: node_type.hpp:55
@ VAL_UNFILT
the val scalar was left unfiltered; the parser was set not to filter.
Definition: node_type.hpp:50
@ VAL
a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or S...
Definition: node_type.hpp:34
@ VALTAG
the val has a tag
Definition: node_type.hpp:44
@ _TYMASK
all the bits up to here
Definition: node_type.hpp:45
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:36
@ SCALAR_DQUO
Definition: node_type.hpp:82
@ VAL_SQUO
mark val scalar as single quoted '
Definition: node_type.hpp:62
@ VAL_PLAIN
mark val scalar as plain scalar (unquoted, even when multiline)
Definition: node_type.hpp:66
@ KEYREF
a *reference: the key references an &anchor
Definition: node_type.hpp:39
@ BLOCK
mark container with block style (seqs as '- val ', maps as 'key: val')
Definition: node_type.hpp:56
@ KEYANCH
the key has an &anchor
Definition: node_type.hpp:41
@ VAL_DQUO
mark val scalar as double quoted "
Definition: node_type.hpp:64
@ KEY_UNFILT
the key scalar was left unfiltered; the parser was set not to filter.
Definition: node_type.hpp:49
@ KEY_SQUO
mark key scalar as single quoted '
Definition: node_type.hpp:61
@ VAL_LITERAL
mark val scalar as multiline, block literal |
Definition: node_type.hpp:58
@ KEY_LITERAL
mark key scalar as multiline, block literal |
Definition: node_type.hpp:57
@ KEY_PLAIN
mark key scalar as plain scalar (unquoted, even when multiline)
Definition: node_type.hpp:65
@ SCALAR_PLAIN
Definition: node_type.hpp:83
@ KEY_FOLDED
mark key scalar as multiline, block folded >
Definition: node_type.hpp:59
@ DOC
a document
Definition: node_type.hpp:37
@ npos
a null string position
Definition: common.hpp:266
Definition: common.cpp:12
#define _prflag(fl, txt)
const char * type_str() const noexcept
return a preset string based on the node type
Definition: node_type.hpp:148