3 #include <fmt/format.h>
8 std::string
esc(std::string_view str
, esc_line_option line_option
)
10 auto nesc
{std::count_if(begin(str
), end(str
), [](unsigned char c
) {
11 return (!std::isprint(c
)) || (c
== '\\');
14 return std::string(str
);
15 if (line_option
== esc_line_option::multi
)
16 nesc
+= std::count(begin(str
), end(str
), '\n');
18 ret
.reserve(str
.length() + nesc
);
21 case '\a': ret
+= "\\a"; break;
22 case '\b': ret
+= "\\b"; break;
23 case '\f': ret
+= "\\f"; break;
26 if (line_option
== esc_line_option::multi
)
29 case '\r': ret
+= "\\r"; break;
30 case '\t': ret
+= "\\t"; break;
31 case '\v': ret
+= "\\v"; break;
32 case '\\': ret
+= "\\\\"; break;
34 if (isprint(static_cast<unsigned char>(c
))) {
38 ret
+= fmt::format("\\x{:02x}", static_cast<unsigned char>(c
));
42 if (line_option
== esc_line_option::multi
) {
43 auto length
= ret
.length();
44 if (length
&& ('\n' == ret
.at(length
- 1))) {
45 ret
.erase(length
- 1, 1);