File tree Expand file tree Collapse file tree 1 file changed +18
-9
lines changed Expand file tree Collapse file tree 1 file changed +18
-9
lines changed Original file line number Diff line number Diff line change 8
8
9
9
#include " examples.hpp"
10
10
#include < toml++/toml.hpp>
11
+ #include < cctype> // For iscntrl.
12
+ #include < iomanip> // For setfill, hex, etc.
13
+ #include < optional>
11
14
12
15
using namespace std ::string_view_literals;
13
16
@@ -110,23 +113,29 @@ namespace
110
113
{
111
114
for (char c : str)
112
115
{
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>{})
114
125
{
115
- std::cout << toml::impl::control_char_escapes[ static_cast <std:: size_t >(c)] ;
126
+ std::cout << *escape_sequence ;
116
127
}
117
128
else
118
129
{
119
- if (c == ' \x7F ' )
130
+ if (c >= 0 && std::iscntrl (c) )
120
131
{
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 ();
123
136
}
124
137
else
125
138
{
126
- if (c == ' \\ ' )
127
- {
128
- std::cout << ' \\ ' ;
129
- }
130
139
std::cout << c;
131
140
}
132
141
}
You can’t perform that action at this time.
0 commit comments