rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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.
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.
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.
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.
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.
NodeType c4::yml::scalar_style_choose_json (csubstr scalar) noexcept
 choose a json scalar style based on the scalar's contents

Detailed Description

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

Function Documentation

◆ scalar_style_query_squo()

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.

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}
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

Referenced by scalar_style_choose_block(), and scalar_style_choose_flow().

◆ scalar_style_query_plain_flow()

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.

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}
C & back() noexcept
Definition substr.hpp:375

Referenced by scalar_style_choose_flow().

◆ scalar_style_query_plain_block()

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.

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}

Referenced by scalar_style_choose_block().

◆ scalar_style_choose_flow()

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}
@ SCALAR_SQUO
mask of KEY_SQUO|VAL_SQUO,
@ SCALAR_DQUO
mask of KEY_DQUO|VAL_DQUO,
@ SCALAR_PLAIN
mask of KEY_PLAIN|VAL_PLAIN,
bool scalar_style_query_squo(csubstr scalar) noexcept
query whether a scalar can be encoded using single quotes.
bool scalar_style_query_plain_flow(csubstr scalar) noexcept
query whether a scalar can be encoded using plain style while in flow mode.

◆ scalar_style_choose_block()

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.

◆ scalar_style_choose_json()

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_special_json(csubstr s) noexcept
Query if a scalar is plain, eg, true, false, null, +-.inf or .nan.
bool scalar_is_plain_number_json(csubstr s) noexcept
JSON-sense query of plain number.