From dad3237514e53b5e163b3bb9153f1edbc2db9738 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Thu, 2 Jan 2025 22:54:19 +0500 Subject: [PATCH] Fix a bug when copying the fill from basic_specs Signed-off-by: Vladislav Shchapov --- include/fmt/format.h | 14 ++++++++++++-- include/fmt/std.h | 4 +--- test/std-test.cc | 3 +++ test/xchar-test.cc | 3 ++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index d1b83d18..2fb85fef 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3654,6 +3654,17 @@ void vformat_to(buffer& buf, basic_string_view fmt, parse_format_string( fmt, format_handler{parse_context(fmt), {out, args, loc}}); } + +template +void basic_specs_copy_fill(basic_specs& dst, const basic_specs& src) { + if (src.fill_size() == 1 && const_check(!std::is_same::value)) { + Char fill = src.fill_unit(); + dst.set_fill(basic_string_view(&fill, 1)); + return; + } + dst.set_fill(basic_string_view(src.fill(), src.fill_size())); +} + } // namespace detail FMT_BEGIN_EXPORT @@ -3960,8 +3971,7 @@ template struct nested_formatter { write(basic_appender(buf)); auto specs = format_specs(); specs.width = width_; - specs.set_fill( - basic_string_view(specs_.fill(), specs_.fill_size())); + detail::basic_specs_copy_fill(specs, specs_); specs.set_align(specs_.align()); return detail::write( ctx.out(), basic_string_view(buf.data(), buf.size()), specs); diff --git a/include/fmt/std.h b/include/fmt/std.h index cae14fb6..bc277d64 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -696,9 +696,7 @@ template struct formatter, Char> { auto outer_specs = format_specs(); outer_specs.width = specs.width; - auto fill = specs.template fill(); - if (fill) - outer_specs.set_fill(basic_string_view(fill, specs.fill_size())); + detail::basic_specs_copy_fill(outer_specs, specs); outer_specs.set_align(specs.align()); specs.width = 0; diff --git a/test/std-test.cc b/test/std-test.cc index ab458ae8..2c57b3f6 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -91,6 +91,9 @@ TEST(std_test, complex) { EXPECT_EQ(fmt::format("{: }", std::complex(1, 2.2)), "( 1+2.2i)"); EXPECT_EQ(fmt::format("{: }", std::complex(1, -2.2)), "( 1-2.2i)"); + EXPECT_EQ(fmt::format("{:8}", std::complex(1, 2)), "(1+2i) "); + EXPECT_EQ(fmt::format("{:-<8}", std::complex(1, 2)), "(1+2i)--"); + EXPECT_EQ(fmt::format("{:>20.2f}", std::complex(1, 2.2)), " (1.00+2.20i)"); EXPECT_EQ(fmt::format("{:<20.2f}", std::complex(1, 2.2)), diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 9b02cd22..fd613e82 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -79,7 +79,7 @@ TEST(xchar_test, format) { EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error); EXPECT_EQ(fmt::format(L"{}", true), L"true"); EXPECT_EQ(fmt::format(L"{0}", L'a'), L"a"); - EXPECT_EQ(fmt::format(L"Letter {}", L'\x40e'), L"Letter \x40e"); // Ў + EXPECT_EQ(fmt::format(L"Letter {}", L'\x40e'), L"Letter \x40e"); // Ў if (sizeof(wchar_t) == 4) EXPECT_EQ(fmt::format(fmt::runtime(L"{:𓀨>3}"), 42), L"𓀨42"); EXPECT_EQ(fmt::format(L"{}c{}", L"ab", 1), L"abc1"); @@ -504,6 +504,7 @@ TEST(std_test_xchar, complex) { EXPECT_EQ(fmt::format(L"{:.2f}", std::complex(1, 2)), L"(1.00+2.00i)"); EXPECT_EQ(fmt::format(L"{:8}", std::complex(1, 2)), L"(1+2i) "); + EXPECT_EQ(fmt::format(L"{:-<8}", std::complex(1, 2)), L"(1+2i)--"); } TEST(std_test_xchar, optional) {