Fix debug string formatting for range formatters (#4936)

Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
cui fliter 2026-09-11 23:43:46 +08:00 committed by GitHub
parent 1da57c78d4
commit 6aa4a5f6f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 11 deletions

View File

@ -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;
}
};

View File

@ -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