Fix a bug when copying the fill from basic_specs

Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
This commit is contained in:
Vladislav Shchapov
2025-01-02 22:54:19 +05:00
committed by Victor Zverovich
parent 880e1494dc
commit dad3237514
4 changed files with 18 additions and 6 deletions

View File

@ -3654,6 +3654,17 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
parse_format_string( parse_format_string(
fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}}); fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
} }
template <typename Char>
void basic_specs_copy_fill(basic_specs& dst, const basic_specs& src) {
if (src.fill_size() == 1 && const_check(!std::is_same<Char, char>::value)) {
Char fill = src.fill_unit<Char>();
dst.set_fill(basic_string_view<Char>(&fill, 1));
return;
}
dst.set_fill(basic_string_view<char>(src.fill<char>(), src.fill_size()));
}
} // namespace detail } // namespace detail
FMT_BEGIN_EXPORT FMT_BEGIN_EXPORT
@ -3960,8 +3971,7 @@ template <typename T, typename Char = char> struct nested_formatter {
write(basic_appender<Char>(buf)); write(basic_appender<Char>(buf));
auto specs = format_specs(); auto specs = format_specs();
specs.width = width_; specs.width = width_;
specs.set_fill( detail::basic_specs_copy_fill<Char>(specs, specs_);
basic_string_view<Char>(specs_.fill<Char>(), specs_.fill_size()));
specs.set_align(specs_.align()); specs.set_align(specs_.align());
return detail::write<Char>( return detail::write<Char>(
ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs); ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);

View File

@ -696,9 +696,7 @@ template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
auto outer_specs = format_specs(); auto outer_specs = format_specs();
outer_specs.width = specs.width; outer_specs.width = specs.width;
auto fill = specs.template fill<Char>(); detail::basic_specs_copy_fill<Char>(outer_specs, specs);
if (fill)
outer_specs.set_fill(basic_string_view<Char>(fill, specs.fill_size()));
outer_specs.set_align(specs.align()); outer_specs.set_align(specs.align());
specs.width = 0; specs.width = 0;

View File

@ -91,6 +91,9 @@ TEST(std_test, complex) {
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)"); EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)"); EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
EXPECT_EQ(fmt::format("{:8}", std::complex<double>(1, 2)), "(1+2i) ");
EXPECT_EQ(fmt::format("{:-<8}", std::complex<double>(1, 2)), "(1+2i)--");
EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)), EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
" (1.00+2.20i)"); " (1.00+2.20i)");
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)), EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),

View File

@ -79,7 +79,7 @@ TEST(xchar_test, format) {
EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error); 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"{}", true), L"true");
EXPECT_EQ(fmt::format(L"{0}", L'a'), L"a"); 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) if (sizeof(wchar_t) == 4)
EXPECT_EQ(fmt::format(fmt::runtime(L"{:𓀨>3}"), 42), L"𓀨42"); EXPECT_EQ(fmt::format(fmt::runtime(L"{:𓀨>3}"), 42), L"𓀨42");
EXPECT_EQ(fmt::format(L"{}c{}", L"ab", 1), L"abc1"); 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<double>(1, 2)), EXPECT_EQ(fmt::format(L"{:.2f}", std::complex<double>(1, 2)),
L"(1.00+2.00i)"); L"(1.00+2.00i)");
EXPECT_EQ(fmt::format(L"{:8}", std::complex<double>(1, 2)), L"(1+2i) "); EXPECT_EQ(fmt::format(L"{:8}", std::complex<double>(1, 2)), L"(1+2i) ");
EXPECT_EQ(fmt::format(L"{:-<8}", std::complex<double>(1, 2)), L"(1+2i)--");
} }
TEST(std_test_xchar, optional) { TEST(std_test_xchar, optional) {