rapidyaml  0.16.0
parseandemitYAML,anddoitfast
Scalar style helpers

These functions are used by the emitter to choose a scalar style when a scalar does not have it set. More...

Functions

bool c4::yml::scalar_style_query_squo (csubstr scalar) noexcept
 query whether a scalar can be encoded using single quotes. More...
 
bool c4::yml::scalar_style_query_plain_flow (csubstr scalar) noexcept
 query whether a scalar can be encoded using plain style while in flow mode. More...
 
bool c4::yml::scalar_style_query_plain_block (csubstr scalar) noexcept
 query whether a scalar can be encoded using plain style while in block mode. More...
 
NodeType c4::yml::scalar_style_choose_flow (csubstr scalar) noexcept
 choose a YAML scalar style based on the scalar's contents, while in flow mode. More...
 
NodeType c4::yml::scalar_style_choose_block (csubstr scalar) noexcept
 choose a YAML scalar style based on the scalar's contents, while in block mode. More...
 
NodeType c4::yml::scalar_style_choose_json (csubstr scalar) noexcept
 choose a json scalar style based on the scalar's contents More...
 

Detailed Description

These functions are used by the emitter to choose a scalar style when a scalar does not have it set.

Function Documentation

bool c4::yml::scalar_style_query_squo ( csubstr  scalar)
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 15 of file scalar_style.cpp.

Referenced by c4::yml::scalar_style_choose_block(), and c4::yml::scalar_style_choose_flow().

16 {
17  // see https://www.yaml.info/learn/quote.html#noplain
18  // cannot have leading whitespace after a newline
19  for(size_t i = 0; i < scalar.len; ++i)
20  {
21  if(scalar.str[i] == '\n' && i + 1 < scalar.len)
22  {
23  char next = scalar.str[i + 1];
24  if(next == ' ' || next == '\t')
25  return false;
26  }
27  }
28  return true;
29 }
bool c4::yml::scalar_style_query_plain_flow ( csubstr  scalar)
noexcept

query whether a scalar can be encoded using plain style while in flow mode.

Plain scalars [have more constraints in flow mode than in block mode](https://www.yaml.info/learn/quote.html#noplain). scalar_style_query_plain_block() is the block mode analogous function.

Definition at line 51 of file scalar_style.cpp.

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

52 {
53  // see https://www.yaml.info/learn/quote.html#noplain
54  if(!scalar.len)
55  return !scalar.str;
56  // first
57  switch(scalar.str[0])
58  {
59  case ' ': case '\n': case '\t': case '\r':
60  case '!': case '&': case '*': case ',':
61  case '"': case '\'': case '|': case '>':
62  case '{': case '}': case '[': case ']':
63  case '#': case '`': case '%': case '@':
64  return false;
65  case '-': case ':': case '?':
66  if(scalar.len == 1 || (scalar.str[1] == ' ' || scalar.str[1] == '\t'))
67  return false;
68  break;
69  }
70  // bulk
71  for(size_t i = 1; i + 1 < scalar.len; ++i)
72  {
73  switch(scalar.str[i])
74  {
75  case ',': case '{': case '}': case '[': case ']':
76  return false;
77  case ':': case '#':
78  if(!is_valid_bulk_(scalar, i))
79  return false;
80  break;
81  }
82  }
83  // last
84  if(scalar.len > 1)
85  {
86  switch(scalar.back())
87  {
88  case ' ': case '\n': case '\t': case '\r':
89  case ',':
90  case '{': case '}':
91  case '[': case ']':
92  case '#':
93  case ':':
94  return false;
95  }
96  }
97  return true;
98 }
bool c4::yml::scalar_style_query_plain_block ( csubstr  scalar)
noexcept

query whether a scalar can be encoded using plain style while in block mode.

Plain scalars [have more constraints in flow mode than in block mode](https://www.yaml.info/learn/quote.html#noplain). scalar_style_query_plain_flow() is the flow mode analogous function.

Definition at line 101 of file scalar_style.cpp.

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

102 {
103  // see https://www.yaml.info/learn/quote.html#noplain
104  if(!scalar.len)
105  return !scalar.str;
106  // first
107  switch(scalar.str[0])
108  {
109  case ' ': case '\n': case '\t': case '\r':
110  case '!': case '&': case '*': case ',':
111  case '"': case '\'': case '|': case '>':
112  case '{': case '}': case '[': case ']':
113  case '#': case '`': case '%': case '@':
114  return false;
115  case '-': case ':': case '?':
116  if (scalar.len == 1 || (scalar.str[1] == ' ' || scalar.str[1] == '\t'))
117  return false;
118  break;
119  }
120  // bulk
121  for(size_t i = 1; i + 1 < scalar.len; ++i)
122  {
123  switch(scalar.str[i])
124  {
125  case ':': case '#':
126  if(!is_valid_bulk_(scalar, i))
127  return false;
128  break;
129  }
130  }
131  // last
132  if(scalar.len > 1)
133  {
134  switch(scalar.back())
135  {
136  case ' ': case '\n': case '\t': case '\r':
137  case '#':
138  case ':':
139  return false;
140  }
141  }
142  return true;
143 }
NodeType c4::yml::scalar_style_choose_flow ( csubstr  scalar)
inlinenoexcept

choose a YAML scalar style based on the scalar's contents, while in flow mode.

Plain scalars [have more constraints in flow mode than in block mode](https://www.yaml.info/learn/quote.html#noplain). scalar_style_choose_block() is the block mode analogous function.

Definition at line 46 of file scalar_style.hpp.

47 {
48  if(scalar.len)
49  {
51  return SCALAR_PLAIN;
52  else if(scalar_style_query_squo(scalar))
53  return SCALAR_SQUO;
54  return SCALAR_DQUO;
55  }
56  return scalar.str ? SCALAR_SQUO : SCALAR_PLAIN;
57 }
bool scalar_style_query_squo(csubstr scalar) noexcept
query whether a scalar can be encoded using single quotes.
mask of KEY_SQUO|VAL_SQUO,
Definition: node_type.hpp:171
mask of KEY_PLAIN|VAL_PLAIN,
Definition: node_type.hpp:173
mask of KEY_DQUO|VAL_DQUO,
Definition: node_type.hpp:172
bool scalar_style_query_plain_flow(csubstr scalar) noexcept
query whether a scalar can be encoded using plain style while in flow mode.
NodeType c4::yml::scalar_style_choose_block ( csubstr  scalar)
noexcept

choose a YAML scalar style based on the scalar's contents, while in block mode.

Plain scalars [have more constraints in flow mode than in block mode](https://www.yaml.info/learn/quote.html#noplain). scalar_style_choose_block() is the flow mode analogous function.

Definition at line 146 of file scalar_style.cpp.

147 {
148  if(scalar.len)
149  {
151  return SCALAR_PLAIN;
152  RYML_ASSERT_BASIC_(scalar_style_query_squo(scalar)
153  && "if this assertion fires, please submit an issue!");
154  return SCALAR_SQUO;
155  }
156  return scalar.str ? SCALAR_SQUO : SCALAR_PLAIN;
157 }
bool scalar_style_query_plain_block(csubstr scalar) noexcept
query whether a scalar can be encoded using plain style while in block mode.
bool scalar_style_query_squo(csubstr scalar) noexcept
query whether a scalar can be encoded using single quotes.
mask of KEY_SQUO|VAL_SQUO,
Definition: node_type.hpp:171
mask of KEY_PLAIN|VAL_PLAIN,
Definition: node_type.hpp:173
NodeType c4::yml::scalar_style_choose_json ( csubstr  scalar)
noexcept

choose a json scalar style based on the scalar's contents

Definition at line 160 of file scalar_style.cpp.

161 {
162  // do not quote numbers or special scalars
163  return scalar_is_plain_number_json(scalar)
165 }
bool scalar_is_plain_number_json(csubstr s) noexcept
JSON-sense query of plain number.
mask of KEY_PLAIN|VAL_PLAIN,
Definition: node_type.hpp:173
bool scalar_is_special_json(csubstr s) noexcept
Query if a scalar is plain, eg, true, false, null, +-.inf or .nan.
mask of KEY_DQUO|VAL_DQUO,
Definition: node_type.hpp:172