mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-19 20:18:49 +00:00
We should escape
This commit is contained in:
parent
b559cfd4c0
commit
11b07a56b2
@ -227,6 +227,10 @@ template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Char> inline bool is_printable_ascii(Char c) {
|
||||||
|
return c >= 0x20 && c < 0x7e;
|
||||||
|
}
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename Char, typename OutputIt, typename Arg,
|
typename Char, typename OutputIt, typename Arg,
|
||||||
FMT_ENABLE_IF(is_std_string_like<typename std::decay<Arg>::type>::value)>
|
FMT_ENABLE_IF(is_std_string_like<typename std::decay<Arg>::type>::value)>
|
||||||
@ -247,8 +251,14 @@ OutputIt write_range_entry(OutputIt out, const Arg& v) {
|
|||||||
c = 't';
|
c = 't';
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
|
FMT_FALLTHROUGH;
|
||||||
|
case '\\':
|
||||||
*out++ = '\\';
|
*out++ = '\\';
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
if (is_printable_ascii(c)) break;
|
||||||
|
out = format_to(out, "\\x{:02x}", c);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
*out++ = c;
|
*out++ = c;
|
||||||
}
|
}
|
||||||
|
@ -264,6 +264,7 @@ TEST(ranges_test, join_range) {
|
|||||||
#endif // FMT_RANGES_TEST_ENABLE_JOIN
|
#endif // FMT_RANGES_TEST_ENABLE_JOIN
|
||||||
|
|
||||||
TEST(ranges_test, escape_string) {
|
TEST(ranges_test, escape_string) {
|
||||||
auto v = std::vector<std::string>{"\n\r\t\""};
|
EXPECT_EQ(fmt::format("{}", std::vector<std::string>{"\n\r\t\"\\"}),
|
||||||
EXPECT_EQ(fmt::format("{}", v), "[\"\\n\\r\\t\\\"\"]");
|
"[\"\\n\\r\\t\\\"\\\\\"]");
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::vector<std::string>{"\x7"}), "[\"\\x07\"]");
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user