rapidyaml  0.12.1
parse and emit YAML, and do it fast
Tag utilities

Classes

struct  c4::yml::TagCache
 Accelerator structure to reduce memory requirements by enabling reuse of resolved tags. More...
 
struct  c4::yml::TagDirective
 
struct  c4::yml::TagDirectiveRange
 
struct  c4::yml::TagDirectives
 

Macros

#define RYML_MAX_TAG_DIRECTIVES   4
 the maximum number of tag directives in a Tree More...
 

Typedefs

using c4::yml::tag_bits = uint16_t
 the integral type necessary to cover all the bits marking node tags More...
 

Enumerations

enum  c4::yml::YamlTag_e : tag_bits {
  c4::yml::TAG_NONE = 0 , c4::yml::TAG_MAP = 1 , c4::yml::TAG_OMAP = 2 , c4::yml::TAG_PAIRS = 3 ,
  c4::yml::TAG_SET = 4 , c4::yml::TAG_SEQ = 5 , c4::yml::TAG_BINARY = 6 , c4::yml::TAG_BOOL = 7 ,
  c4::yml::TAG_FLOAT = 8 , c4::yml::TAG_INT = 9 , c4::yml::TAG_MERGE = 10 , c4::yml::TAG_NULL = 11 ,
  c4::yml::TAG_STR = 12 , c4::yml::TAG_TIMESTAMP = 13 , c4::yml::TAG_VALUE = 14 , c4::yml::TAG_YAML = 15
}
 a bit mask for marking tags for types More...
 

Functions

YamlTag_e c4::yml::to_tag (csubstr tag)
 
csubstr c4::yml::from_tag (YamlTag_e tag)
 
csubstr c4::yml::from_tag_long (YamlTag_e tag)
 
csubstr c4::yml::normalize_tag (csubstr tag)
 
csubstr c4::yml::normalize_tag_long (csubstr tag)
 
csubstr c4::yml::normalize_tag_long (csubstr tag, substr output)
 
bool c4::yml::is_custom_tag (csubstr tag)
 is a tag of the form !handle!tag? More...
 
bool c4::yml::is_valid_tag_handle (csubstr handle)
 
size_t c4::yml::transform_tag (substr output, csubstr handle, csubstr prefix, csubstr tag, Callbacks const &callbacks, Location const &ymlloc={}, bool with_brackets=true)
 returns the length of the transformed tag, or 0 to signal that the tag is local and cannot be resolved More...
 

Detailed Description

See also
sample::sample_tags

Macro Definition Documentation

◆ RYML_MAX_TAG_DIRECTIVES

#define RYML_MAX_TAG_DIRECTIVES   4

the maximum number of tag directives in a Tree

Definition at line 24 of file tag.hpp.

Typedef Documentation

◆ tag_bits

using c4::yml::tag_bits = typedef uint16_t

the integral type necessary to cover all the bits marking node tags

Definition at line 28 of file tag.hpp.

Enumeration Type Documentation

◆ YamlTag_e

a bit mask for marking tags for types

Enumerator
TAG_NONE 
TAG_MAP 

!!map Unordered set of key: value pairs without duplicates.

See also
https://yaml.org/type/map.html
TAG_OMAP 

!!omap Ordered sequence of key: value pairs without duplicates.

See also
https://yaml.org/type/omap.html
TAG_PAIRS 

!!pairs Ordered sequence of key: value pairs allowing duplicates.

See also
https://yaml.org/type/pairs.html
TAG_SET 

!!set Unordered set of non-equal values.

See also
https://yaml.org/type/set.html
TAG_SEQ 

!!seq Sequence of arbitrary values.

See also
https://yaml.org/type/seq.html
TAG_BINARY 

!!binary A sequence of zero or more octets (8 bit values).

See also
https://yaml.org/type/binary.html
TAG_BOOL 

!!bool Mathematical Booleans.

See also
https://yaml.org/type/bool.html
TAG_FLOAT 

!!float Floating-point approximation to real numbers.

https://yaml.org/type/float.html

TAG_INT 

!!float Mathematical integers.

https://yaml.org/type/int.html

TAG_MERGE 

!!merge Specify one or more mapping to be merged with the current one.

https://yaml.org/type/merge.html

TAG_NULL 

!!null Devoid of value.

https://yaml.org/type/null.html

TAG_STR 

!!str A sequence of zero or more Unicode characters.

https://yaml.org/type/str.html

TAG_TIMESTAMP 

!!timestamp A point in time https://yaml.org/type/timestamp.html

TAG_VALUE 

!!value Specify the default value of a mapping https://yaml.org/type/value.html

TAG_YAML 

!!yaml Specify the default value of a mapping https://yaml.org/type/yaml.html

Definition at line 31 of file tag.hpp.

31  : tag_bits { // NOLINT
32  TAG_NONE = 0,
33  // container types
34  TAG_MAP = 1, /**< !!map Unordered set of key: value pairs without duplicates. @see https://yaml.org/type/map.html */
35  TAG_OMAP = 2, /**< !!omap Ordered sequence of key: value pairs without duplicates. @see https://yaml.org/type/omap.html */
36  TAG_PAIRS = 3, /**< !!pairs Ordered sequence of key: value pairs allowing duplicates. @see https://yaml.org/type/pairs.html */
37  TAG_SET = 4, /**< !!set Unordered set of non-equal values. @see https://yaml.org/type/set.html */
38  TAG_SEQ = 5, /**< !!seq Sequence of arbitrary values. @see https://yaml.org/type/seq.html */
39  // scalar types
40  TAG_BINARY = 6, /**< !!binary A sequence of zero or more octets (8 bit values). @see https://yaml.org/type/binary.html */
41  TAG_BOOL = 7, /**< !!bool Mathematical Booleans. @see https://yaml.org/type/bool.html */
42  TAG_FLOAT = 8, /**< !!float Floating-point approximation to real numbers. https://yaml.org/type/float.html */
43  TAG_INT = 9, /**< !!float Mathematical integers. https://yaml.org/type/int.html */
44  TAG_MERGE = 10, /**< !!merge Specify one or more mapping to be merged with the current one. https://yaml.org/type/merge.html */
45  TAG_NULL = 11, /**< !!null Devoid of value. https://yaml.org/type/null.html */
46  TAG_STR = 12, /**< !!str A sequence of zero or more Unicode characters. https://yaml.org/type/str.html */
47  TAG_TIMESTAMP = 13, /**< !!timestamp A point in time https://yaml.org/type/timestamp.html */
48  TAG_VALUE = 14, /**< !!value Specify the default value of a mapping https://yaml.org/type/value.html */
49  TAG_YAML = 15, /**< !!yaml Specify the default value of a mapping https://yaml.org/type/yaml.html */
50 } YamlTag_e;
YamlTag_e
a bit mask for marking tags for types
Definition: tag.hpp:31
uint16_t tag_bits
the integral type necessary to cover all the bits marking node tags
Definition: tag.hpp:28
@ TAG_SET
!!set Unordered set of non-equal values.
Definition: tag.hpp:37
@ TAG_MERGE
!!merge Specify one or more mapping to be merged with the current one.
Definition: tag.hpp:44
@ TAG_INT
!!float Mathematical integers.
Definition: tag.hpp:43
@ TAG_SEQ
!!seq Sequence of arbitrary values.
Definition: tag.hpp:38
@ TAG_NULL
!!null Devoid of value.
Definition: tag.hpp:45
@ TAG_YAML
!!yaml Specify the default value of a mapping https://yaml.org/type/yaml.html
Definition: tag.hpp:49
@ TAG_TIMESTAMP
!!timestamp A point in time https://yaml.org/type/timestamp.html
Definition: tag.hpp:47
@ TAG_NONE
Definition: tag.hpp:32
@ TAG_STR
!!str A sequence of zero or more Unicode characters.
Definition: tag.hpp:46
@ TAG_BOOL
!!bool Mathematical Booleans.
Definition: tag.hpp:41
@ TAG_MAP
!!map Unordered set of key: value pairs without duplicates.
Definition: tag.hpp:34
@ TAG_BINARY
!!binary A sequence of zero or more octets (8 bit values).
Definition: tag.hpp:40
@ TAG_PAIRS
!!pairs Ordered sequence of key: value pairs allowing duplicates.
Definition: tag.hpp:36
@ TAG_VALUE
!!value Specify the default value of a mapping https://yaml.org/type/value.html
Definition: tag.hpp:48
@ TAG_OMAP
!!omap Ordered sequence of key: value pairs without duplicates.
Definition: tag.hpp:35
@ TAG_FLOAT
!!float Floating-point approximation to real numbers.
Definition: tag.hpp:42

Function Documentation

◆ to_tag()

YamlTag_e c4::yml::to_tag ( csubstr  tag)

Definition at line 68 of file tag.cpp.

69 {
70  if(tag.begins_with("!<"))
71  tag = tag.sub(1);
72  if(tag.begins_with("!!"))
73  {
74  tag = tag.sub(2);
75  }
76  else if(tag.begins_with('!'))
77  {
78  return TAG_NONE;
79  }
80  else
81  {
82  csubstr pfx = "<tag:yaml.org,2002:";
83  csubstr pfx2 = pfx.sub(1);
84  if(tag.begins_with(pfx2))
85  {
86  tag = tag.sub(pfx2.len);
87  }
88  else if(tag.begins_with(pfx))
89  {
90  tag = tag.sub(pfx.len);
91  if(!tag.len)
92  return TAG_NONE;
93  tag = tag.offs(0, 1);
94  }
95  }
96  if(tag == "map")
97  return TAG_MAP;
98  else if(tag == "omap")
99  return TAG_OMAP;
100  else if(tag == "pairs")
101  return TAG_PAIRS;
102  else if(tag == "set")
103  return TAG_SET;
104  else if(tag == "seq")
105  return TAG_SEQ;
106  else if(tag == "binary")
107  return TAG_BINARY;
108  else if(tag == "bool")
109  return TAG_BOOL;
110  else if(tag == "float")
111  return TAG_FLOAT;
112  else if(tag == "int")
113  return TAG_INT;
114  else if(tag == "merge")
115  return TAG_MERGE;
116  else if(tag == "null")
117  return TAG_NULL;
118  else if(tag == "str")
119  return TAG_STR;
120  else if(tag == "timestamp")
121  return TAG_TIMESTAMP;
122  else if(tag == "value")
123  return TAG_VALUE;
124  else if(tag == "yaml")
125  return TAG_YAML;
126 
127  return TAG_NONE;
128 }

References c4::yml::TAG_BINARY, c4::yml::TAG_BOOL, c4::yml::TAG_FLOAT, c4::yml::TAG_INT, c4::yml::TAG_MAP, c4::yml::TAG_MERGE, c4::yml::TAG_NONE, c4::yml::TAG_NULL, c4::yml::TAG_OMAP, c4::yml::TAG_PAIRS, c4::yml::TAG_SEQ, c4::yml::TAG_SET, c4::yml::TAG_STR, c4::yml::TAG_TIMESTAMP, c4::yml::TAG_VALUE, and c4::yml::TAG_YAML.

◆ from_tag()

csubstr c4::yml::from_tag ( YamlTag_e  tag)

Definition at line 170 of file tag.cpp.

171 {
172  switch(tag)
173  {
174  case TAG_MAP:
175  return {"!!map"};
176  case TAG_OMAP:
177  return {"!!omap"};
178  case TAG_PAIRS:
179  return {"!!pairs"};
180  case TAG_SET:
181  return {"!!set"};
182  case TAG_SEQ:
183  return {"!!seq"};
184  case TAG_BINARY:
185  return {"!!binary"};
186  case TAG_BOOL:
187  return {"!!bool"};
188  case TAG_FLOAT:
189  return {"!!float"};
190  case TAG_INT:
191  return {"!!int"};
192  case TAG_MERGE:
193  return {"!!merge"};
194  case TAG_NULL:
195  return {"!!null"};
196  case TAG_STR:
197  return {"!!str"};
198  case TAG_TIMESTAMP:
199  return {"!!timestamp"};
200  case TAG_VALUE:
201  return {"!!value"};
202  case TAG_YAML:
203  return {"!!yaml"};
204  case TAG_NONE:
205  default:
206  return {""};
207  }
208 }

References c4::yml::TAG_BINARY, c4::yml::TAG_BOOL, c4::yml::TAG_FLOAT, c4::yml::TAG_INT, c4::yml::TAG_MAP, c4::yml::TAG_MERGE, c4::yml::TAG_NONE, c4::yml::TAG_NULL, c4::yml::TAG_OMAP, c4::yml::TAG_PAIRS, c4::yml::TAG_SEQ, c4::yml::TAG_SET, c4::yml::TAG_STR, c4::yml::TAG_TIMESTAMP, c4::yml::TAG_VALUE, and c4::yml::TAG_YAML.

◆ from_tag_long()

csubstr c4::yml::from_tag_long ( YamlTag_e  tag)

Definition at line 130 of file tag.cpp.

131 {
132  switch(tag)
133  {
134  case TAG_MAP:
135  return {"<tag:yaml.org,2002:map>"};
136  case TAG_OMAP:
137  return {"<tag:yaml.org,2002:omap>"};
138  case TAG_PAIRS:
139  return {"<tag:yaml.org,2002:pairs>"};
140  case TAG_SET:
141  return {"<tag:yaml.org,2002:set>"};
142  case TAG_SEQ:
143  return {"<tag:yaml.org,2002:seq>"};
144  case TAG_BINARY:
145  return {"<tag:yaml.org,2002:binary>"};
146  case TAG_BOOL:
147  return {"<tag:yaml.org,2002:bool>"};
148  case TAG_FLOAT:
149  return {"<tag:yaml.org,2002:float>"};
150  case TAG_INT:
151  return {"<tag:yaml.org,2002:int>"};
152  case TAG_MERGE:
153  return {"<tag:yaml.org,2002:merge>"};
154  case TAG_NULL:
155  return {"<tag:yaml.org,2002:null>"};
156  case TAG_STR:
157  return {"<tag:yaml.org,2002:str>"};
158  case TAG_TIMESTAMP:
159  return {"<tag:yaml.org,2002:timestamp>"};
160  case TAG_VALUE:
161  return {"<tag:yaml.org,2002:value>"};
162  case TAG_YAML:
163  return {"<tag:yaml.org,2002:yaml>"};
164  case TAG_NONE:
165  default:
166  return {""};
167  }
168 }

References c4::yml::TAG_BINARY, c4::yml::TAG_BOOL, c4::yml::TAG_FLOAT, c4::yml::TAG_INT, c4::yml::TAG_MAP, c4::yml::TAG_MERGE, c4::yml::TAG_NONE, c4::yml::TAG_NULL, c4::yml::TAG_OMAP, c4::yml::TAG_PAIRS, c4::yml::TAG_SEQ, c4::yml::TAG_SET, c4::yml::TAG_STR, c4::yml::TAG_TIMESTAMP, c4::yml::TAG_VALUE, and c4::yml::TAG_YAML.

◆ normalize_tag()

csubstr c4::yml::normalize_tag ( csubstr  tag)

Definition at line 19 of file tag.cpp.

20 {
21  YamlTag_e t = to_tag(tag);
22  if(t != TAG_NONE)
23  return from_tag(t);
24  if(tag.begins_with("!<"))
25  tag = tag.sub(1);
26  if(tag.begins_with("<!"))
27  return tag;
28  return tag;
29 }
csubstr from_tag(YamlTag_e tag)
Definition: tag.cpp:170
YamlTag_e to_tag(csubstr tag)
Definition: tag.cpp:68

References c4::yml::from_tag(), c4::yml::TAG_NONE, and c4::yml::to_tag().

◆ normalize_tag_long() [1/2]

csubstr c4::yml::normalize_tag_long ( csubstr  tag)

Definition at line 31 of file tag.cpp.

32 {
33  YamlTag_e t = to_tag(tag);
34  if(t != TAG_NONE)
35  return from_tag_long(t);
36  if(tag.begins_with("!<"))
37  tag = tag.sub(1);
38  if(tag.begins_with("<!"))
39  return tag;
40  return tag;
41 }
csubstr from_tag_long(YamlTag_e tag)
Definition: tag.cpp:130

References c4::yml::from_tag_long(), c4::yml::TAG_NONE, and c4::yml::to_tag().

◆ normalize_tag_long() [2/2]

csubstr c4::yml::normalize_tag_long ( csubstr  tag,
substr  output 
)

Definition at line 43 of file tag.cpp.

44 {
45  csubstr result = normalize_tag_long(tag);
46  if(result.begins_with("!!"))
47  {
48  _RYML_CHECK_BASIC(!output.overlaps(tag));
49  tag = tag.sub(2);
50  const csubstr pfx = "<tag:yaml.org,2002:";
51  const size_t len = pfx.len + tag.len + 1;
52  if(len <= output.len)
53  {
54  memcpy(output.str , pfx.str, pfx.len);
55  memcpy(output.str + pfx.len, tag.str, tag.len);
56  output[pfx.len + tag.len] = '>';
57  result = output.first(len);
58  }
59  else
60  {
61  result.str = nullptr;
62  result.len = len;
63  }
64  }
65  return result;
66 }
csubstr normalize_tag_long(csubstr tag, substr output)
Definition: tag.cpp:43

References c4::yml::normalize_tag_long().

◆ is_custom_tag()

bool c4::yml::is_custom_tag ( csubstr  tag)

is a tag of the form !handle!tag?

Definition at line 9 of file tag.cpp.

10 {
11  if((tag.len > 2) && (tag.str[0] == '!'))
12  {
13  size_t pos = tag.find('!', 1);
14  return pos != npos && pos > 1 && tag.str[1] != '<';
15  }
16  return false;
17 }
@ npos
a null string position
Definition: common.hpp:258

References c4::yml::npos.

◆ is_valid_tag_handle()

bool c4::yml::is_valid_tag_handle ( csubstr  handle)

Definition at line 210 of file tag.cpp.

211 {
212  if(handle.begins_with('!') && handle.ends_with('!'))
213  {
214  _c4dbgpf("handle={}", _prs(handle, true));
215  csubstr trimmed = handle.sub(1);
216  if(trimmed.ends_with('!'))
217  trimmed = trimmed.offs(0, 1);
218  _c4dbgpf("handle_trimmed={}", _prs(trimmed, true));
219  // https://yaml.org/spec/1.2.2/#rule-ns-word-char
220  for(char c : trimmed)
221  {
222  bool ok = (c >= '0' && c <= '9')
223  || (c >= 'a' && c <= 'z')
224  || (c >= 'A' && c <= 'Z')
225  || c == '-';
226  if(!ok)
227  {
228  _c4dbgpf("invalid handle character: '{}'", _c4prc(c));
229  return false;
230  }
231  }
232  return true;
233  }
234  return false;
235 }

◆ transform_tag()

size_t c4::yml::transform_tag ( substr  output,
csubstr  handle,
csubstr  prefix,
csubstr  tag,
Callbacks const &  callbacks,
Location const &  ymlloc,
bool  with_brackets 
)

returns the length of the transformed tag, or 0 to signal that the tag is local and cannot be resolved

Definition at line 287 of file tag.cpp.

290 {
291  _RYML_ASSERT_BASIC_(callbacks, tag.len >= handle.len);
292  _RYML_ASSERT_BASIC_(callbacks, !output.overlaps(tag));
293  _RYML_ASSERT_BASIC_(callbacks, prefix.len > 0);
294  csubstr rest = tag.sub(handle.len);
295  _c4dbgpf("%TAG: rest={}", _prs(rest));
296  size_t rpos = 0, wpos = 0;
297  auto appendstr = [&](csubstr s) {
298  if(s.len && wpos + s.len <= output.len)
299  memcpy(output.str + wpos, s.str, s.len);
300  wpos += s.len;
301  };
302  auto appendchar = [&](char c) {
303  if(wpos < output.len)
304  output.str[wpos] = c;
305  ++wpos;
306  };
307  if(with_brackets)
308  appendchar('<');
309  appendstr(prefix);
310  const char *errmsg = nullptr;
311  for(size_t pos = 0; pos < rest.len; ++pos)
312  {
313  char c = rest.str[pos];
314  if(C4_LIKELY(is_valid_tag_char(c)))
315  {
316  if(c != '%')
317  continue;
318  else if(read_hex_char(rest, pos, &c))
319  {
320  appendstr(rest.range(rpos, pos));
321  appendchar(c);
322  pos += 2;
323  rpos = pos + 1;
324  continue;
325  }
326  }
327  errmsg = "invalid tag";
328  goto err; // NOLINT
329  }
330  appendstr(rest.sub(rpos));
331  if(with_brackets)
332  appendchar('>');
333  return wpos;
334 err:
335  if(ymlloc)
336  {
337  _RYML_ERR_PARSE_(callbacks, ymlloc, errmsg);
338  }
339  else
340  {
341  _RYML_ERR_BASIC_(callbacks, errmsg);
342  }
343 }