mirror of
https://github.com/fmtlib/fmt.git
synced 2026-09-13 14:22:26 +08:00
Fix debug string formatting for range formatters (#4936)
Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
parent
1da57c78d4
commit
6aa4a5f6f8
@ -644,23 +644,18 @@ struct formatter<
|
||||
|
||||
public:
|
||||
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
||||
return underlying_.parse(ctx);
|
||||
auto it = underlying_.parse(ctx);
|
||||
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value ==
|
||||
range_format::debug_string)
|
||||
underlying_.set_debug_format();
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename FormatContext>
|
||||
auto format(range_type& range, FormatContext& ctx) const
|
||||
-> decltype(ctx.out()) {
|
||||
auto out = ctx.out();
|
||||
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value ==
|
||||
range_format::debug_string) {
|
||||
*out++ = '"';
|
||||
}
|
||||
out = underlying_.format(
|
||||
return underlying_.format(
|
||||
string_type{detail::range_begin(range), detail::range_end(range)}, ctx);
|
||||
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value ==
|
||||
range_format::debug_string)
|
||||
*out++ = '"';
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -348,6 +348,10 @@ TEST(ranges_test, range_format_string) {
|
||||
TEST(ranges_test, range_format_debug_string) {
|
||||
const vector_debug_string v{'f', 'o', 'o'};
|
||||
EXPECT_EQ(fmt::format("{}", v), "\"foo\"");
|
||||
EXPECT_EQ(fmt::format("{}", vector_debug_string{'\n'}), "\"\\n\"");
|
||||
EXPECT_EQ(fmt::format("{}", vector_debug_string{'\\'}), "\"\\\\\"");
|
||||
EXPECT_EQ(fmt::format("{}", vector_debug_string{'\"'}), "\"\\\"\"");
|
||||
EXPECT_EQ(fmt::format("{:8}", vector_debug_string{'a'}), "\"a\" ");
|
||||
}
|
||||
|
||||
// A range that provides non-const only begin()/end() to test fmt::join
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user