rapidyaml  0.8.0
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 26 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 

(NOT IMPLEMENTED, work in progress) 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 30 of file node_type.hpp.

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

Function Documentation

◆ operator|()

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

Definition at line 102 of file node_type.hpp.

102 { 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 103 of file node_type.hpp.

103 { 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 104 of file node_type.hpp.

104 { 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 105 of file node_type.hpp.

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

◆ operator~()

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

Definition at line 106 of file node_type.hpp.

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

◆ operator&=()

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

Definition at line 107 of file node_type.hpp.

107 { 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 108 of file node_type.hpp.

108 { 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 171 of file node_type.cpp.

172 {
173  if(s.len)
174  {
175  if(s.begins_with_any(" \n\t")
176  ||
177  s.ends_with_any(" \n\t"))
178  {
179  return SCALAR_DQUO;
180  }
181  else if( ! scalar_style_query_plain(s))
182  {
184  }
185  // nothing remarkable - use plain
186  return SCALAR_PLAIN;
187  }
188  return s.str ? SCALAR_SQUO : SCALAR_PLAIN;
189 }
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 191 of file node_type.cpp.

192 {
193  // do not quote special cases
194  bool plain = (
195  (s == "true" || s == "false" || s == "null")
196  ||
197  (
198  // do not quote numbers
199  s.is_number()
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  return plain ? SCALAR_PLAIN : SCALAR_DQUO;
212 }
@ npos
a null string position
Definition: common.hpp:267

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 }

Referenced by c4::yml::scalar_style_choose().

◆ 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 != ':'
163  && ( ! s.begins_with_any("-:?*&,'\"{}[]|>%#@`\r")) // @ and ` are reserved characters
164  && ( ! s.ends_with_any(":#"))
165  // make this check in the last place, as it has linear
166  // complexity, while the previous ones are
167  // constant-time
168  && (s.first_of("\n#:[]{},") == npos);
169 }

References c4::yml::npos.

Referenced by c4::yml::scalar_style_choose().

◆ 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 255 of file node_type.hpp.

256 {
257  return s.str == nullptr ||
258  s == "~" ||
259  s == "null" ||
260  s == "Null" ||
261  s == "NULL";
262 }

Referenced by c4::yml::Tree::key_is_null(), sample::sample_empty_null_values(), and c4::yml::Tree::val_is_null().