rapidyaml  0.7.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::_TYMASK = __(12)-1 , c4::yml::KEY_UNFILT = __(12) , c4::yml::VAL_UNFILT = __(13) ,
  c4::yml::FLOW_SL = __(14) , c4::yml::FLOW_ML = __(15) , c4::yml::BLOCK = __(16) , c4::yml::KEY_LITERAL = __(17) ,
  c4::yml::VAL_LITERAL = __(18) , c4::yml::KEY_FOLDED = __(19) , c4::yml::VAL_FOLDED = __(20) , c4::yml::KEY_SQUO = __(21) ,
  c4::yml::VAL_SQUO = __(22) , c4::yml::KEY_DQUO = __(23) , c4::yml::VAL_DQUO = __(24) , c4::yml::KEY_PLAIN = __(25) ,
  c4::yml::VAL_PLAIN = __(26) , 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, must have non-empty key

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

_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
32  NOTYPE = 0, ///< no node type or style is set
33  KEY = __(0), ///< is member of a map, must have non-empty key
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  _TYMASK = __(12)-1, ///< all the bits up to here
46  //
47  // unfiltered flags:
48  //
49  KEY_UNFILT = __(12), ///< the key scalar was left unfiltered; the parser was set not to filter. @see ParserOptions
50  VAL_UNFILT = __(13), ///< the val scalar was left unfiltered; the parser was set not to filter. @see ParserOptions
51  //
52  // style flags:
53  //
54  FLOW_SL = __(14), ///< mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,key2: val2}')
55  FLOW_ML = __(15), ///< (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}')
56  BLOCK = __(16), ///< mark container with block style (seqs as '- val\n', maps as 'key: val')
57  KEY_LITERAL = __(17), ///< mark key scalar as multiline, block literal |
58  VAL_LITERAL = __(18), ///< mark val scalar as multiline, block literal |
59  KEY_FOLDED = __(19), ///< mark key scalar as multiline, block folded >
60  VAL_FOLDED = __(20), ///< mark val scalar as multiline, block folded >
61  KEY_SQUO = __(21), ///< mark key scalar as single quoted '
62  VAL_SQUO = __(22), ///< mark val scalar as single quoted '
63  KEY_DQUO = __(23), ///< mark key scalar as double quoted "
64  VAL_DQUO = __(24), ///< mark val scalar as double quoted "
65  KEY_PLAIN = __(25), ///< mark key scalar as plain scalar (unquoted, even when multiline)
66  VAL_PLAIN = __(26), ///< mark val scalar as plain scalar (unquoted, even when multiline)
67  //
68  // type combination masks:
69  //
70  KEYVAL = KEY|VAL,
71  KEYSEQ = KEY|SEQ,
72  KEYMAP = KEY|MAP,
73  DOCMAP = DOC|MAP,
74  DOCSEQ = DOC|SEQ,
75  DOCVAL = DOC|VAL,
76  //
77  // style combination masks:
78  //
84  KEYQUO = KEY_SQUO|KEY_DQUO|KEY_FOLDED|KEY_LITERAL, ///< key style is one of ', ", > or |
85  VALQUO = VAL_SQUO|VAL_DQUO|VAL_FOLDED|VAL_LITERAL, ///< val style is one of ', ", > or |
86  KEY_STYLE = KEY_LITERAL|KEY_FOLDED|KEY_SQUO|KEY_DQUO|KEY_PLAIN, ///< mask of all the scalar styles for key (not container styles!)
87  VAL_STYLE = VAL_LITERAL|VAL_FOLDED|VAL_SQUO|VAL_DQUO|VAL_PLAIN, ///< mask of all the scalar styles for val (not container styles!)
93  //
94  // mixed masks
97  #undef __
98 } 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:80
@ 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
@ KEYQUO
key style is one of ', ", > or |
Definition: node_type.hpp:84
@ VAL_FOLDED
mark val scalar as multiline, block folded >
Definition: node_type.hpp:60
@ CONTAINER_STYLE_BLOCK
Definition: node_type.hpp:90
@ _KEYMASK
Definition: node_type.hpp:95
@ VAL_STYLE
mask of all the scalar styles for val (not container styles!)
Definition: node_type.hpp:87
@ KEYTAG
the key has a tag
Definition: node_type.hpp:43
@ CONTAINER_STYLE_FLOW
Definition: node_type.hpp:89
@ 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
@ SCALAR_STYLE
Definition: node_type.hpp:88
@ _TYMASK
all the bits up to here
Definition: node_type.hpp:45
@ SCALAR_LITERAL
Definition: node_type.hpp:79
@ 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
@ KEY_STYLE
mask of all the scalar styles for key (not container styles!)
Definition: node_type.hpp:86
@ 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
@ VALQUO
val style is one of ', ", > or |
Definition: node_type.hpp:85
@ VAL_DQUO
mark val scalar as double quoted "
Definition: node_type.hpp:64
@ CONTAINER_STYLE
Definition: node_type.hpp:91
@ 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
@ _VALMASK
Definition: node_type.hpp:96
@ KEY_FOLDED
mark key scalar as multiline, block folded >
Definition: node_type.hpp:59
@ 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 100 of file node_type.hpp.

100 { 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 101 of file node_type.hpp.

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

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

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

◆ operator~()

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

Definition at line 104 of file node_type.hpp.

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

◆ operator&=()

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

Definition at line 105 of file node_type.hpp.

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

106 { 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 169 of file node_type.cpp.

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

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 189 of file node_type.cpp.

190 {
191  // do not quote special cases
192  bool plain = (
193  (s == "true" || s == "false" || s == "null")
194  ||
195  (
196  // do not quote numbers
197  s.is_number()
198  &&
199  (
200  // quote integral numbers if they have a leading 0
201  // https://github.com/biojppm/rapidyaml/issues/291
202  (!(s.len > 1 && s.begins_with('0')))
203  // do not quote reals with leading 0
204  // https://github.com/biojppm/rapidyaml/issues/313
205  || (s.find('.') != csubstr::npos)
206  )
207  )
208  );
209  return plain ? SCALAR_PLAIN : SCALAR_DQUO;
210 }
@ npos
a null string position
Definition: common.hpp:266

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 141 of file node_type.cpp.

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

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 147 of file node_type.cpp.

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

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

252 {
253  return s.str == nullptr ||
254  s == "~" ||
255  s == "null" ||
256  s == "Null" ||
257  s == "NULL";
258 }

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