rapidyaml  0.11.1
parse and emit YAML, and do it fast
Node types

Classes

struct  c4::yml::NodeType
 wraps a NodeType_e element with some syntactic sugar and predicates More...
 

Typedefs

using c4::yml::type_bits = uint32_t
 the integral type necessary to cover all the bits for NodeType_e More...
 

Enumerations

enum  c4::yml::NodeType_e : type_bits {
  c4::yml::NOTYPE = 0 , c4::yml::KEY = __(0) , c4::yml::VAL = __(1) , c4::yml::MAP = __(2) ,
  c4::yml::SEQ = __(3) , c4::yml::DOC = __(4) , c4::yml::STREAM = __(5)|SEQ , c4::yml::KEYREF = __(6) ,
  c4::yml::VALREF = __(7) , c4::yml::KEYANCH = __(8) , c4::yml::VALANCH = __(9) , c4::yml::KEYTAG = __(10) ,
  c4::yml::VALTAG = __(11) , c4::yml::KEYNIL = __(12) , c4::yml::VALNIL = __(13) , c4::yml::_TYMASK = __(14)-1 ,
  c4::yml::KEY_UNFILT = __(14) , c4::yml::VAL_UNFILT = __(15) , c4::yml::FLOW_SL = __(16) , c4::yml::FLOW_ML = __(17) ,
  c4::yml::BLOCK = __(18) , c4::yml::KEY_LITERAL = __(19) , c4::yml::VAL_LITERAL = __(20) , c4::yml::KEY_FOLDED = __(21) ,
  c4::yml::VAL_FOLDED = __(22) , c4::yml::KEY_SQUO = __(23) , c4::yml::VAL_SQUO = __(24) , c4::yml::KEY_DQUO = __(25) ,
  c4::yml::VAL_DQUO = __(26) , c4::yml::KEY_PLAIN = __(27) , c4::yml::VAL_PLAIN = __(28) , c4::yml::KEYVAL = KEY|VAL ,
  c4::yml::KEYSEQ = KEY|SEQ , c4::yml::KEYMAP = KEY|MAP , c4::yml::DOCMAP = DOC|MAP , c4::yml::DOCSEQ = DOC|SEQ ,
  c4::yml::DOCVAL = DOC|VAL , c4::yml::SCALAR_LITERAL = KEY_LITERAL|VAL_LITERAL , c4::yml::SCALAR_FOLDED = KEY_FOLDED|VAL_FOLDED , c4::yml::SCALAR_SQUO = KEY_SQUO|VAL_SQUO ,
  c4::yml::SCALAR_DQUO = KEY_DQUO|VAL_DQUO , c4::yml::SCALAR_PLAIN = KEY_PLAIN|VAL_PLAIN , c4::yml::KEYQUO = KEY_SQUO|KEY_DQUO|KEY_FOLDED|KEY_LITERAL , c4::yml::VALQUO = VAL_SQUO|VAL_DQUO|VAL_FOLDED|VAL_LITERAL ,
  c4::yml::KEY_STYLE = KEY_LITERAL|KEY_FOLDED|KEY_SQUO|KEY_DQUO|KEY_PLAIN , c4::yml::VAL_STYLE = VAL_LITERAL|VAL_FOLDED|VAL_SQUO|VAL_DQUO|VAL_PLAIN , c4::yml::SCALAR_STYLE = KEY_STYLE|VAL_STYLE , c4::yml::CONTAINER_STYLE_FLOW = FLOW_SL|FLOW_ML ,
  c4::yml::CONTAINER_STYLE_BLOCK = BLOCK , c4::yml::CONTAINER_STYLE = FLOW_SL|FLOW_ML|BLOCK , c4::yml::STYLE = SCALAR_STYLE | CONTAINER_STYLE , c4::yml::_KEYMASK = KEY | KEYQUO | KEYANCH | KEYREF | KEYTAG ,
  c4::yml::_VALMASK = VAL | VALQUO | VALANCH | VALREF | VALTAG
}
 a bit mask for marking node types and styles More...
 

Functions

constexpr C4_CONST NodeType_e c4::yml::operator| (NodeType_e lhs, NodeType_e rhs) noexcept
 
constexpr C4_CONST NodeType_e c4::yml::operator& (NodeType_e lhs, NodeType_e rhs) noexcept
 
constexpr C4_CONST NodeType_e c4::yml::operator>> (NodeType_e bits, uint32_t n) noexcept
 
constexpr C4_CONST NodeType_e c4::yml::operator<< (NodeType_e bits, uint32_t n) noexcept
 
constexpr C4_CONST NodeType_e c4::yml::operator~ (NodeType_e bits) noexcept
 
NodeType_ec4::yml::operator&= (NodeType_e &subject, NodeType_e bits) noexcept
 
NodeType_ec4::yml::operator|= (NodeType_e &subject, NodeType_e bits) noexcept
 

scalar style helpers

NodeType_e c4::yml::scalar_style_choose (csubstr scalar) noexcept
 choose a YAML emitting style based on the scalar's contents More...
 
NodeType_e c4::yml::scalar_style_json_choose (csubstr scalar) noexcept
 choose a json style based on the scalar's contents More...
 
bool c4::yml::scalar_style_query_squo (csubstr s) noexcept
 query whether a scalar can be encoded using single quotes. More...
 
bool c4::yml::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). More...
 
bool c4::yml::scalar_is_null (csubstr s) noexcept
 YAML-sense query of nullity. More...
 

Detailed Description

Typedef Documentation

◆ type_bits

using c4::yml::type_bits = typedef uint32_t

the integral type necessary to cover all the bits for NodeType_e

Definition at line 29 of file node_type.hpp.

Enumeration Type Documentation

◆ NodeType_e

a bit mask for marking node types and styles

Enumerator
NOTYPE 

no node type or style is set

KEY 

is member of a map

VAL 

a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or SEQ

MAP 

a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes

SEQ 

a seq: a parent of VAL/SEQ/MAP nodes

DOC 

a document

STREAM 

a stream: a seq of docs

KEYREF 

a *reference: the key references an &anchor

VALREF 

a *reference: the val references an &anchor

KEYANCH 

the key has an &anchor

VALANCH 

the val has an &anchor

KEYTAG 

the key has a tag

VALTAG 

the val has a tag

KEYNIL 

the key is null (eg { : b} results in a null key)

VALNIL 

the val is null (eg {a : } results in a null val)

_TYMASK 

all the bits up to here

KEY_UNFILT 

the key scalar was left unfiltered; the parser was set not to filter.

See also
ParserOptions
VAL_UNFILT 

the val scalar was left unfiltered; the parser was set not to filter.

See also
ParserOptions
FLOW_SL 

mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,key2: val2}')

FLOW_ML 

mark container with multi-line flow style (seqs as '[
val1,
val2
], maps as '{
key: val,
key2: val2
}')

BLOCK 

mark container with block style (seqs as '- val
', maps as 'key: val')

KEY_LITERAL 

mark key scalar as multiline, block literal |

VAL_LITERAL 

mark val scalar as multiline, block literal |

KEY_FOLDED 

mark key scalar as multiline, block folded >

VAL_FOLDED 

mark val scalar as multiline, block folded >

KEY_SQUO 

mark key scalar as single quoted '

VAL_SQUO 

mark val scalar as single quoted '

KEY_DQUO 

mark key scalar as double quoted "

VAL_DQUO 

mark val scalar as double quoted "

KEY_PLAIN 

mark key scalar as plain scalar (unquoted, even when multiline)

VAL_PLAIN 

mark val scalar as plain scalar (unquoted, even when multiline)

KEYVAL 
KEYSEQ 
KEYMAP 
DOCMAP 
DOCSEQ 
DOCVAL 
SCALAR_LITERAL 
SCALAR_FOLDED 
SCALAR_SQUO 
SCALAR_DQUO 
SCALAR_PLAIN 
KEYQUO 

key style is one of ', ", > or |

VALQUO 

val style is one of ', ", > or |

KEY_STYLE 

mask of all the scalar styles for key (not container styles!)

VAL_STYLE 

mask of all the scalar styles for val (not container styles!)

SCALAR_STYLE 
CONTAINER_STYLE_FLOW 
CONTAINER_STYLE_BLOCK 
CONTAINER_STYLE 
STYLE 
_KEYMASK 
_VALMASK 

Definition at line 33 of file node_type.hpp.

33  : type_bits {
34  #define __(v) (type_bits(1) << v) // a convenience define, undefined below // NOLINT
35  NOTYPE = 0, ///< no node type or style is set
36  KEY = __(0), ///< is member of a map
37  VAL = __(1), ///< a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or SEQ
38  MAP = __(2), ///< a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
39  SEQ = __(3), ///< a seq: a parent of VAL/SEQ/MAP nodes
40  DOC = __(4), ///< a document
41  STREAM = __(5)|SEQ, ///< a stream: a seq of docs
42  KEYREF = __(6), ///< a *reference: the key references an &anchor
43  VALREF = __(7), ///< a *reference: the val references an &anchor
44  KEYANCH = __(8), ///< the key has an &anchor
45  VALANCH = __(9), ///< the val has an &anchor
46  KEYTAG = __(10), ///< the key has a tag
47  VALTAG = __(11), ///< the val has a tag
48  KEYNIL = __(12), ///< the key is null (eg `{ : b}` results in a null key)
49  VALNIL = __(13), ///< the val is null (eg `{a : }` results in a null val)
50  _TYMASK = __(14)-1, ///< all the bits up to here
51  //
52  // unfiltered flags:
53  //
54  KEY_UNFILT = __(14), ///< the key scalar was left unfiltered; the parser was set not to filter. @see ParserOptions
55  VAL_UNFILT = __(15), ///< the val scalar was left unfiltered; the parser was set not to filter. @see ParserOptions
56  //
57  // style flags:
58  //
59  FLOW_SL = __(16), ///< mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,key2: val2}')
60  FLOW_ML = __(17), ///< mark container with multi-line flow style (seqs as '[\n val1,\n val2\n], maps as '{\n key: val,\n key2: val2\n}')
61  BLOCK = __(18), ///< mark container with block style (seqs as '- val\n', maps as 'key: val')
62  KEY_LITERAL = __(19), ///< mark key scalar as multiline, block literal |
63  VAL_LITERAL = __(20), ///< mark val scalar as multiline, block literal |
64  KEY_FOLDED = __(21), ///< mark key scalar as multiline, block folded >
65  VAL_FOLDED = __(22), ///< mark val scalar as multiline, block folded >
66  KEY_SQUO = __(23), ///< mark key scalar as single quoted '
67  VAL_SQUO = __(24), ///< mark val scalar as single quoted '
68  KEY_DQUO = __(25), ///< mark key scalar as double quoted "
69  VAL_DQUO = __(26), ///< mark val scalar as double quoted "
70  KEY_PLAIN = __(27), ///< mark key scalar as plain scalar (unquoted, even when multiline)
71  VAL_PLAIN = __(28), ///< mark val scalar as plain scalar (unquoted, even when multiline)
72  //
73  // type combination masks:
74  //
75  KEYVAL = KEY|VAL,
76  KEYSEQ = KEY|SEQ,
77  KEYMAP = KEY|MAP,
78  DOCMAP = DOC|MAP,
79  DOCSEQ = DOC|SEQ,
80  DOCVAL = DOC|VAL,
81  //
82  // style combination masks:
83  //
89  KEYQUO = KEY_SQUO|KEY_DQUO|KEY_FOLDED|KEY_LITERAL, ///< key style is one of ', ", > or |
90  VALQUO = VAL_SQUO|VAL_DQUO|VAL_FOLDED|VAL_LITERAL, ///< val style is one of ', ", > or |
91  KEY_STYLE = KEY_LITERAL|KEY_FOLDED|KEY_SQUO|KEY_DQUO|KEY_PLAIN, ///< mask of all the scalar styles for key (not container styles!)
92  VAL_STYLE = VAL_LITERAL|VAL_FOLDED|VAL_SQUO|VAL_DQUO|VAL_PLAIN, ///< mask of all the scalar styles for val (not container styles!)
98  //
99  // mixed masks
102  #undef __
103 } NodeType_e;
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
Definition: node_type.hpp:29
NodeType_e
a bit mask for marking node types and styles
Definition: node_type.hpp:33
@ VALANCH
the val has an &anchor
Definition: node_type.hpp:45
@ NOTYPE
no node type or style is set
Definition: node_type.hpp:35
@ SCALAR_FOLDED
Definition: node_type.hpp:85
@ KEY_DQUO
mark key scalar as double quoted "
Definition: node_type.hpp:68
@ VALREF
a *reference: the val references an &anchor
Definition: node_type.hpp:43
@ VALNIL
the val is null (eg {a : } results in a null val)
Definition: node_type.hpp:49
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:38
@ STREAM
a stream: a seq of docs
Definition: node_type.hpp:41
@ KEY
is member of a map
Definition: node_type.hpp:36
@ KEYQUO
key style is one of ', ", > or |
Definition: node_type.hpp:89
@ VAL_FOLDED
mark val scalar as multiline, block folded >
Definition: node_type.hpp:65
@ CONTAINER_STYLE_BLOCK
Definition: node_type.hpp:95
@ VAL_STYLE
mask of all the scalar styles for val (not container styles!)
Definition: node_type.hpp:92
@ KEYTAG
the key has a tag
Definition: node_type.hpp:46
@ CONTAINER_STYLE_FLOW
Definition: node_type.hpp:94
@ SCALAR_SQUO
Definition: node_type.hpp:86
@ FLOW_SL
mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,...
Definition: node_type.hpp:59
@ FLOW_ML
mark container with multi-line flow style (seqs as '[ val1, val2 ], maps as '{ key: val,...
Definition: node_type.hpp:60
@ VAL_UNFILT
the val scalar was left unfiltered; the parser was set not to filter.
Definition: node_type.hpp:55
@ 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:37
@ VALTAG
the val has a tag
Definition: node_type.hpp:47
@ SCALAR_STYLE
Definition: node_type.hpp:93
@ _TYMASK
all the bits up to here
Definition: node_type.hpp:50
@ SCALAR_LITERAL
Definition: node_type.hpp:84
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:39
@ SCALAR_DQUO
Definition: node_type.hpp:87
@ VAL_SQUO
mark val scalar as single quoted '
Definition: node_type.hpp:67
@ KEY_STYLE
mask of all the scalar styles for key (not container styles!)
Definition: node_type.hpp:91
@ VAL_PLAIN
mark val scalar as plain scalar (unquoted, even when multiline)
Definition: node_type.hpp:71
@ KEYREF
a *reference: the key references an &anchor
Definition: node_type.hpp:42
@ BLOCK
mark container with block style (seqs as '- val ', maps as 'key: val')
Definition: node_type.hpp:61
@ KEYANCH
the key has an &anchor
Definition: node_type.hpp:44
@ VALQUO
val style is one of ', ", > or |
Definition: node_type.hpp:90
@ VAL_DQUO
mark val scalar as double quoted "
Definition: node_type.hpp:69
@ CONTAINER_STYLE
Definition: node_type.hpp:96
@ KEY_UNFILT
the key scalar was left unfiltered; the parser was set not to filter.
Definition: node_type.hpp:54
@ KEY_SQUO
mark key scalar as single quoted '
Definition: node_type.hpp:66
@ VAL_LITERAL
mark val scalar as multiline, block literal |
Definition: node_type.hpp:63
@ KEY_LITERAL
mark key scalar as multiline, block literal |
Definition: node_type.hpp:62
@ KEY_PLAIN
mark key scalar as plain scalar (unquoted, even when multiline)
Definition: node_type.hpp:70
@ SCALAR_PLAIN
Definition: node_type.hpp:88
@ KEY_FOLDED
mark key scalar as multiline, block folded >
Definition: node_type.hpp:64
@ KEYNIL
the key is null (eg { : b} results in a null key)
Definition: node_type.hpp:48
@ DOC
a document
Definition: node_type.hpp:40
#define __(v)
Definition: node_type.hpp:34

Function Documentation

◆ operator|()

constexpr C4_CONST NodeType_e c4::yml::operator| ( NodeType_e  lhs,
NodeType_e  rhs 
)
inlineconstexprnoexcept

Definition at line 105 of file node_type.hpp.

105 { return (NodeType_e)(((type_bits)lhs) | ((type_bits)rhs)); }

◆ operator&()

constexpr C4_CONST NodeType_e c4::yml::operator& ( NodeType_e  lhs,
NodeType_e  rhs 
)
inlineconstexprnoexcept

Definition at line 106 of file node_type.hpp.

106 { return (NodeType_e)(((type_bits)lhs) & ((type_bits)rhs)); }

◆ operator>>()

constexpr C4_CONST NodeType_e c4::yml::operator>> ( NodeType_e  bits,
uint32_t  n 
)
inlineconstexprnoexcept

Definition at line 107 of file node_type.hpp.

107 { return (NodeType_e)(((type_bits)bits) >> n); }

◆ operator<<()

constexpr C4_CONST NodeType_e c4::yml::operator<< ( NodeType_e  bits,
uint32_t  n 
)
inlineconstexprnoexcept

Definition at line 108 of file node_type.hpp.

108 { return (NodeType_e)(((type_bits)bits) << n); }

◆ operator~()

constexpr C4_CONST NodeType_e c4::yml::operator~ ( NodeType_e  bits)
inlineconstexprnoexcept

Definition at line 109 of file node_type.hpp.

109 { return (NodeType_e)(~(type_bits)bits); }

◆ operator&=()

NodeType_e& c4::yml::operator&= ( NodeType_e subject,
NodeType_e  bits 
)
inlinenoexcept

Definition at line 110 of file node_type.hpp.

110 { subject = (NodeType_e)((type_bits)subject & (type_bits)bits); return subject; }

◆ operator|=()

NodeType_e& c4::yml::operator|= ( NodeType_e subject,
NodeType_e  bits 
)
inlinenoexcept

Definition at line 111 of file node_type.hpp.

111 { subject = (NodeType_e)((type_bits)subject | (type_bits)bits); return subject; }

◆ scalar_style_choose()

NodeType_e c4::yml::scalar_style_choose ( csubstr  s)
noexcept

choose a YAML emitting style based on the scalar's contents

Definition at line 170 of file node_type.cpp.

171 {
172  if(s.len)
173  {
174  if(s.begins_with_any(" \n\t")
175  ||
176  s.ends_with_any(" \n\t"))
177  {
178  return SCALAR_DQUO;
179  }
180  else if( ! scalar_style_query_plain(s))
181  {
183  }
184  // nothing remarkable - use plain
185  return SCALAR_PLAIN;
186  }
187  return s.str ? SCALAR_SQUO : SCALAR_PLAIN;
188 }
bool scalar_style_query_squo(csubstr s) noexcept
query whether a scalar can be encoded using single quotes.
Definition: node_type.cpp:143
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:149

References c4::yml::SCALAR_DQUO, c4::yml::SCALAR_PLAIN, c4::yml::SCALAR_SQUO, c4::yml::scalar_style_query_plain(), and c4::yml::scalar_style_query_squo().

◆ scalar_style_json_choose()

NodeType_e c4::yml::scalar_style_json_choose ( csubstr  s)
noexcept

choose a json style based on the scalar's contents

Definition at line 190 of file node_type.cpp.

191 {
192  // do not quote special cases
193  bool plain = (
194  (s == "true" || s == "false" || s == "null")
195  ||
196  (
197  // do not quote numbers
198  s.is_number()
199  &&
200  (
201  (
202  // quote integral numbers if they have a leading 0
203  // https://github.com/biojppm/rapidyaml/issues/291
204  (!(s.len > 1 && s.begins_with('0')))
205  // do not quote reals with leading 0
206  // https://github.com/biojppm/rapidyaml/issues/313
207  || (s.find('.') != csubstr::npos)
208  )
209  )
210  )
211  ||
212  (
213  (s.len > 3)
214  &&
215  (
216  (s[0] == '.' && (s == ".inf" || s == ".Inf" || s == ".INF"
217  ||
218  s == ".nan" || s == ".NaN" || s == ".NAN"))
219  ||
220  (s[0] == '-' && (s == "-.inf" || s == "-.Inf" || s == "-.INF"))
221  )
222  )
223  );
224  return plain ? SCALAR_PLAIN : SCALAR_DQUO;
225 }
@ npos
a null string position
Definition: common.hpp:258

References c4::yml::npos, c4::yml::SCALAR_DQUO, and c4::yml::SCALAR_PLAIN.

◆ scalar_style_query_squo()

bool c4::yml::scalar_style_query_squo ( csubstr  s)
noexcept

query whether a scalar can be encoded using single quotes.

It may not be possible, notably when there is leading whitespace after a newline.

Definition at line 143 of file node_type.cpp.

144 {
145  return ! s.first_of_any("\n ", "\n\t");
146 }

◆ scalar_style_query_plain()

bool c4::yml::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 at line 149 of file node_type.cpp.

150 {
151  if(s.begins_with("-."))
152  {
153  if(s == "-.inf" || s == "-.INF")
154  return true;
155  else if(s.sub(2).is_number())
156  return true;
157  }
158  else if(s.begins_with_any("0123456789.-+") && s.is_number())
159  {
160  return true;
161  }
162  return ( ! s.begins_with_any("-:?*&,'\"{}[]|>%#@`\r")) // @ and ` are reserved characters
163  && ( ! s.ends_with_any(":#"))
164  // make this check in the last place, as it has linear
165  // complexity, while the previous ones are
166  // constant-time
167  && (s.first_of("\n#:[]{},") == npos);
168 }

References c4::yml::npos.

◆ scalar_is_null()

bool c4::yml::scalar_is_null ( csubstr  s)
inlinenoexcept

YAML-sense query of nullity.

returns true if the scalar points to nullptr or is otherwise equal to one of the strings "~","null","Null","NULL"

Definition at line 262 of file node_type.hpp.

263 {
264  return s.str == nullptr ||
265  s == "~" ||
266  s == "null" ||
267  s == "Null" ||
268  s == "NULL";
269 }