Skip to content

Commit ac1644a

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 ac1644a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

examples/error_printer.cpp

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include "examples.hpp"
1010
#include <toml++/toml.hpp>
11+
#include <cctype> // For iscntrl.
12+
#include <iomanip> // For setfill, hex, etc.
13+
#include <optional>
1114

1215
using namespace std::string_view_literals;
1316

@@ -110,23 +113,29 @@ namespace
110113
{
111114
for (char c : str)
112115
{
113-
if (c >= 0 && static_cast<std::size_t>(c) < std::size(toml::impl::control_char_escapes))
116+
if (std::optional<std::string_view> escape_sequence = (c == '\\') ? "\\\\"sv
117+
: (c == '\a') ? "\\a"sv
118+
: (c == '\b') ? "\\b"sv
119+
: (c == '\f') ? "\\f"sv
120+
: (c == '\n') ? "\\n"sv
121+
: (c == '\r') ? "\\r"sv
122+
: (c == '\t') ? "\\t"sv
123+
: (c == '\v') ? "\\v"sv
124+
: std::optional<std::string_view>{})
114125
{
115-
std::cout << toml::impl::control_char_escapes[static_cast<std::size_t>(c)];
126+
std::cout << *escape_sequence;
116127
}
117128
else
118129
{
119-
if (c == '\x7F')
130+
if (c >= 0 && std::iscntrl(c))
120131
{
121-
// DEL character.
122-
std::cout << "\\u007F"sv;
132+
// Print a string, just like `toml::impl::control_char_escapes[c]`.
133+
std::ostringstream stream;
134+
stream << "\\u00"sv << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << int{ c };
135+
std::cout << stream.str();
123136
}
124137
else
125138
{
126-
if (c == '\\')
127-
{
128-
std::cout << '\\';
129-
}
130139
std::cout << c;
131140
}
132141
}

0 commit comments

Comments
 (0)