Skip to main content

anonymous{expression-formatter.cpp} Namespace

Local helpers of the expression formatter. More...

Definition​

namespace micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp} { ... }

Functions Index​

voidappend_hex (std::string &buffer, char32_t code, std::string_view escape, std::size_t count)

Appends an escape and a fixed number of hexadecimal digits. More...

voidappend_plain (std::string &buffer, std::string_view sv)

Appends a text in the plain mode, with the control characters escaped. More...

voidappend_quoted (std::string &buffer, std::string_view sv)

Appends a string, as a quoted literal. More...

voidappend_utf8 (std::string &text, char32_t code)

Appends a code point to a text, as UTF-8. More...

template <class Char_T>
voidappend_wide_text (std::string &buffer, std::basic_string_view< Char_T > sv)

Appends the text of a string of a wide character type, converted to UTF-8, as the char text is appended. More...

template <class Char_T>
auto bounded_view (const Char_T *s, std::size_t capacity) noexcept -> std::basic_string_view< Char_T >

Returns the text of a C string held in a buffer of a known capacity. More...

constexpr boolis_escaped_in_plain (char c) noexcept

Tells whether a character is escaped in the plain mode. More...

template <class Char_T>
std::stringutf16_to_utf8 (std::basic_string_view< Char_T > sv)

Converts a UTF-16 text to UTF-8. More...

template <class Char_T>
std::stringutf32_to_utf8 (std::basic_string_view< Char_T > sv)

Converts a UTF-32 text to UTF-8. More...

Description​

Local helpers of the expression formatter.

The unnamed namespace gives the helpers internal linkage, so that they are visible only in this translation unit, and cannot conflict with the names of the application or of the other source files of the framework. They are not declared in any header.

It contains append_hex(), which writes the hexadecimal escapes of the characters that cannot be printed as they are, and append_quoted(), which writes a string between double quotes.

Functions​

append_hex()​

void micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::append_hex (std::string & buffer, char32_t code, std::string_view escape, std::size_t count)

Appends an escape and a fixed number of hexadecimal digits.

Used by expression_formatter::append_character(); the digits are lowercase, the most significant first, and the leading zeros are kept, so that the number of digits shows the size of the type.

Parameters
buffer

The text to which the escape is appended.

code

The value to write.

escape

The escape before the digits ("\\x", "\\u", or "\\U").

count

The number of digits.

Returns

Nothing.

Definition at line 99 of file expression-formatter.cpp.

99 append_hex (std::string& buffer, char32_t code, std::string_view escape,
100 std::size_t count)
101 {
102 constexpr std::string_view digits{ "0123456789abcdef" };
103 buffer.append (escape);
104 for (std::size_t i = count; i > 0; --i)
105 {
106 // The promoted type of `code` is `std::size_t` on some
107 // platforms, so a cast would be reported as useless.
108 const auto nibble = (code >> ((i - 1) * 4)) & 0x0Fu;
109 buffer.push_back (digits[nibble]);
110 }
111 }

Reference append_hex.

Referenced by append_hex and append_quoted.

append_plain()​

void micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::append_plain (std::string & buffer, std::string_view sv)

Appends a text in the plain mode, with the control characters escaped.

Parameters
buffer

The buffer to append to.

sv

The text.

The characters selected by is_escaped_in_plain() are written by append_control_escape(); all the others, including the tab, the line feed, and the bytes of the UTF-8 sequences, unchanged.

Definition at line 322 of file expression-formatter.cpp.

322 append_plain (std::string& buffer, std::string_view sv)
323 {
324 for (const char c : sv)
325 {
326 if (is_escaped_in_plain (c))
327 {
328 append_control_escape (buffer, c);
329 }
330 else
331 {
332 buffer.push_back (c);
333 }
334 }
335 }

References micro_os_plus::micro_test_plus::detail::append_control_escape, append_plain and is_escaped_in_plain.

Referenced by append_plain.

append_quoted()​

void micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::append_quoted (std::string & buffer, std::string_view sv)

Appends a string, as a quoted literal.

Used by the string operands, so that the empty strings and the leading or trailing spaces are visible, and the control characters do not reach the console. The escapes are those of the character literals, with the double quote escaped instead of the single quote. The bytes above 0x7F are written unchanged, so that the UTF-8 text remains readable.

Parameters
buffer

The text to which the literal is appended.

sv

The string to write; it may contain '\\0'.

Returns

Nothing.

Definition at line 130 of file expression-formatter.cpp.

130 append_quoted (std::string& buffer, std::string_view sv)
131 {
132 buffer.push_back ('"');
133 for (const char c : sv)
134 {
135 switch (c)
136 {
137 case '\0':
138 buffer.append ("\\0");
139 break;
140 case '\a':
141 buffer.append ("\\a");
142 break;
143 case '\b':
144 buffer.append ("\\b");
145 break;
146 case '\t':
147 buffer.append ("\\t");
148 break;
149 case '\n':
150 buffer.append ("\\n");
151 break;
152 case '\v':
153 buffer.append ("\\v");
154 break;
155 case '\f':
156 buffer.append ("\\f");
157 break;
158 case '\r':
159 buffer.append ("\\r");
160 break;
161 case '"':
162 buffer.append ("\\\"");
163 break;
164 case '\\':
165 buffer.append ("\\\\");
166 break;
167 default:
168 if (static_cast<unsigned char> (c) < 0x20 || c == 0x7F)
169 {
170 append_hex (buffer, static_cast<unsigned char> (c), "\\x",
171 2);
172 }
173 else
174 {
175 buffer.push_back (c);
176 }
177 break;
178 }
179 }
180 buffer.push_back ('"');
181 }

References append_hex and append_quoted.

Referenced by append_quoted.

append_utf8()​

void micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::append_utf8 (std::string & text, char32_t code)

Appends a code point to a text, as UTF-8.

A value that is not a Unicode scalar value (a surrogate, or a value above U+10FFFF) is written as the replacement character U+FFFD.

Parameters
text

The text to append to.

code

The code point.

Definition at line 194 of file expression-formatter.cpp.

194 append_utf8 (std::string& text, char32_t code)
195 {
196 if ((code >= 0xD800 && code <= 0xDFFF) || code > 0x10FFFF)
197 {
198 code = 0xFFFD;
199 }
200
201 if (code < 0x80)
202 {
203 text.push_back (static_cast<char> (code));
204 }
205 else if (code < 0x800)
206 {
207 text.push_back (static_cast<char> (0xC0 | (code >> 6)));
208 text.push_back (static_cast<char> (0x80 | (code & 0x3F)));
209 }
210 else if (code < 0x10000)
211 {
212 text.push_back (static_cast<char> (0xE0 | (code >> 12)));
213 text.push_back (static_cast<char> (0x80 | ((code >> 6) & 0x3F)));
214 text.push_back (static_cast<char> (0x80 | (code & 0x3F)));
215 }
216 else
217 {
218 text.push_back (static_cast<char> (0xF0 | (code >> 18)));
219 text.push_back (static_cast<char> (0x80 | ((code >> 12) & 0x3F)));
220 text.push_back (static_cast<char> (0x80 | ((code >> 6) & 0x3F)));
221 text.push_back (static_cast<char> (0x80 | (code & 0x3F)));
222 }
223 }

Reference append_utf8.

Referenced by append_utf8, utf16_to_utf8 and utf32_to_utf8.

append_wide_text()​

template <class Char_T>
void micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::append_wide_text (std::string & buffer, std::basic_string_view< Char_T > sv)

Appends the text of a string of a wide character type, converted to UTF-8, as the char text is appended.

The text is written by a formatter in the plain mode, so without quotes, with the control characters other than the tab and the line feed, and DEL, escaped, whatever the mode of the caller.

Template Parameters
Char_T

The character type.

Parameters
[inout] buffer

The buffer to append to.

sv

The text.

Returns

Nothing.

Definition at line 385 of file expression-formatter.cpp.

385 append_wide_text (std::string& buffer, std::basic_string_view<Char_T> sv)
386 {
387 std::string text;
388 plain_text::append (text, sv);
389 buffer.append (text);
390 }

References micro_os_plus::micro_test_plus::detail::plain_text::append and append_wide_text.

Referenced by append_wide_text.

bounded_view()​

template <class Char_T>
std::basic_string_view< Char_T > micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::bounded_view (const Char_T * s, std::size_t capacity)
noexcept

Returns the text of a C string held in a buffer of a known capacity.

With npos, the length is computed by the constructor of the view, which reads up to the terminator; otherwise only the capacity characters are searched for the terminator, so that an array without one is not read beyond its end.

Template Parameters
Char_T

The character type.

Parameters
s

The text; it must not be null.

capacity

The number of characters that may be read, or npos if the text is null-terminated.

Returns

A view of the characters before the first null character, or of the capacity characters if there is none.

Definition at line 356 of file expression-formatter.cpp.

356 bounded_view (const Char_T* s, std::size_t capacity) noexcept
357 {
358 if (capacity == std::basic_string_view<Char_T>::npos)
359 {
360 return std::basic_string_view<Char_T>{ s };
361 }
362
363 const std::basic_string_view<Char_T> buffer{ s, capacity };
364 return buffer.substr (0, buffer.find (Char_T{}));
365 }

Reference bounded_view.

Referenced by bounded_view.

is_escaped_in_plain()​

bool micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::is_escaped_in_plain (char c)
constexpr noexcept

Tells whether a character is escaped in the plain mode.

The control characters other than the tab and the line feed, and the DEL character, which can change the state of a terminal, and truncate a text.

Parameters
c

The character.

Return Values
true

The character is escaped.

false

The character is written unchanged.

Definition at line 303 of file expression-formatter.cpp.

303 is_escaped_in_plain (char c) noexcept
304 {
305 const auto unit = static_cast<unsigned char> (c);
306 return (unit < 0x20 && c != '\t' && c != '\n') || unit == 0x7F;
307 }

Reference is_escaped_in_plain.

Referenced by append_plain and is_escaped_in_plain.

utf16_to_utf8()​

template <class Char_T>
std::string micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::utf16_to_utf8 (std::basic_string_view< Char_T > sv)

Converts a UTF-16 text to UTF-8.

The surrogate pairs are combined; an unpaired surrogate is written as the replacement character.

Template Parameters
Char_T

A character type of at least 16 bits, holding UTF-16 code units.

Parameters
sv

The text to convert.

Returns

The UTF-8 text.

Definition at line 240 of file expression-formatter.cpp.

240 utf16_to_utf8 (std::basic_string_view<Char_T> sv)
241 {
242 std::string text;
243 text.reserve (sv.size ());
244 for (std::size_t i = 0; i < sv.size (); ++i)
245 {
246 const auto unit = static_cast<char32_t> (sv[i]) & 0xFFFFu;
247 if (unit >= 0xD800 && unit <= 0xDBFF && i + 1 < sv.size ())
248 {
249 const auto next = static_cast<char32_t> (sv[i + 1]) & 0xFFFFu;
250 if (next >= 0xDC00 && next <= 0xDFFF)
251 {
252 append_utf8 (text, 0x10000 + ((unit - 0xD800) << 10)
253 + (next - 0xDC00));
254 ++i;
255 continue;
256 }
257 }
258 append_utf8 (text, unit);
259 }
260 return text;
261 }

References append_utf8 and utf16_to_utf8.

Referenced by utf16_to_utf8.

utf32_to_utf8()​

template <class Char_T>
std::string micro_os_plus::micro_test_plus::detail::anonymous{expression-formatter.cpp}::utf32_to_utf8 (std::basic_string_view< Char_T > sv)

Converts a UTF-32 text to UTF-8.

Template Parameters
Char_T

A character type of at least 32 bits, holding UTF-32 code units.

Parameters
sv

The text to convert.

Returns

The UTF-8 text.

Unlike UTF-16, UTF-32 needs no decoding: each code unit is encoded by append_utf8(), which writes the values that are not Unicode scalar values as U+FFFD.

Definition at line 279 of file expression-formatter.cpp.

279 utf32_to_utf8 (std::basic_string_view<Char_T> sv)
280 {
281 std::string text;
282 text.reserve (sv.size ());
283 for (const Char_T unit : sv)
284 {
285 append_utf8 (text, static_cast<char32_t> (unit));
286 }
287 return text;
288 }

References append_utf8 and utf32_to_utf8.

Referenced by utf32_to_utf8.


The documentation for this namespace was generated from the following file:


Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.