mirror of
https://github.com/fmtlib/fmt.git
synced 2025-03-30 13:20:18 +00:00
Escape Unicode
This commit is contained in:
parent
6397095ca4
commit
371d8e2ee0
@ -304,9 +304,15 @@ auto write_range_entry(OutputIt out, basic_string_view<Char> str) -> OutputIt {
|
|||||||
*out++ = '\\';
|
*out++ = '\\';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (is_utf8() && escape.cp > 0xffff) {
|
||||||
|
out = format_to(out, "\\U{:08x}", escape.cp);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
for (Char escape_char : basic_string_view<Char>(
|
for (Char escape_char : basic_string_view<Char>(
|
||||||
escape.begin, to_unsigned(escape.end - escape.begin))) {
|
escape.begin, to_unsigned(escape.end - escape.begin))) {
|
||||||
out = format_to(out, "\\x{:02x}", escape_char);
|
out = format_to(
|
||||||
|
out, "\\x{:02x}",
|
||||||
|
static_cast<typename std::make_unsigned<Char>::type>(escape_char));
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -268,4 +268,10 @@ TEST(ranges_test, escape_string) {
|
|||||||
EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]");
|
EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]");
|
||||||
EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]");
|
EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]");
|
||||||
EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
|
EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
|
||||||
|
|
||||||
|
// Unassigned Unicode code points.
|
||||||
|
if (fmt::detail::is_utf8()) {
|
||||||
|
EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]");
|
||||||
|
EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xbf"}), "[\"\\U0010ffff\"]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user