Skip to content

Commit 9ecb963

Browse files
committed
avoid using impl in error_printer example
Replaced access to `toml::impl::control_char_escapes` with manual handling of control characters, in `print_string`. Would ease a possible future replacement of `#include <toml++/toml.hpp>` with `import tomplplusplus` (because `toml::impl` is not exported).
1 parent fea1d90 commit 9ecb963

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

examples/error_printer.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,29 @@ namespace
110110
{
111111
for (char c : str)
112112
{
113-
if (c >= 0 && static_cast<std::size_t>(c) < std::size(toml::impl::control_char_escapes))
113+
if (std::optional<std::string_view> escape_sequence = (c == '\\') ? "\\\\"sv
114+
: (c == '\a') ? "\\a"sv
115+
: (c == '\b') ? "\\b"sv
116+
: (c == '\f') ? "\\f"sv
117+
: (c == '\n') ? "\\n"sv
118+
: (c == '\r') ? "\\r"sv
119+
: (c == '\t') ? "\\t"sv
120+
: (c == '\v') ? "\\v"sv
121+
: std::optional<std::string_view>{})
114122
{
115-
std::cout << toml::impl::control_char_escapes[static_cast<std::size_t>(c)];
123+
std::cout << *escape_sequence;
116124
}
117125
else
118126
{
119-
if (c == '\x7F')
127+
if (c >= 0 && std::iscntrl(c))
120128
{
121-
// DEL character.
122-
std::cout << "\\u007F"sv;
129+
// Print a string, just like `toml::impl::control_char_escapes[c]`.
130+
std::ostringstream stream;
131+
stream << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << int{ c };
132+
std::cout << "\\u00"sv << stream.str();
123133
}
124134
else
125135
{
126-
if (c == '\\')
127-
{
128-
std::cout << '\\';
129-
}
130136
std::cout << c;
131137
}
132138
}

0 commit comments

Comments
 (0)