From ea85b81ccdb28dda636351e80a1888b1d279f325 Mon Sep 17 00:00:00 2001 From: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Date: Sun, 22 Mar 2026 08:10:17 +0800 Subject: [PATCH] Support fmt::runtime with wchar_t in fmt::format_to_n (#4715) --- include/fmt/xchar.h | 12 ++++++++++-- test/xchar-test.cc | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h index 31706f96..6d95a737 100644 --- a/include/fmt/xchar.h +++ b/include/fmt/xchar.h @@ -279,10 +279,18 @@ inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view fmt, return {buf.out(), buf.count()}; } +template ::value)> +FMT_INLINE auto format_to_n(OutputIt out, size_t n, wformat_string fmt, + T&&... args) -> format_to_n_result { + return vformat_to_n(out, n, fmt.get(), fmt::make_wformat_args(args...)); +} + template , - FMT_ENABLE_IF(detail::is_output_iterator::value&& - detail::is_exotic_char::value)> + FMT_ENABLE_IF(detail::is_output_iterator::value && + !std::is_same::value && + !std::is_same::value)> inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args) -> format_to_n_result { return vformat_to_n(out, n, fmt::basic_string_view(fmt), diff --git a/test/xchar-test.cc b/test/xchar-test.cc index cfe8389d..f6620ddf 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -146,6 +146,26 @@ TEST(format_test, wide_format_to_n) { EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4)); } +TEST(format_test, wide_format_to_n_runtime) { + wchar_t buffer[4]; + buffer[3] = L'x'; + auto result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}"), 12345); + EXPECT_EQ(5u, result.size); + EXPECT_EQ(buffer + 3, result.out); + EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4)); + buffer[0] = L'x'; + buffer[1] = L'x'; + buffer[2] = L'x'; + result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}"), L'A'); + EXPECT_EQ(1u, result.size); + EXPECT_EQ(buffer + 1, result.out); + EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4)); + result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}{} "), L'B', L'C'); + EXPECT_EQ(3u, result.size); + EXPECT_EQ(buffer + 3, result.out); + EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4)); +} + TEST(xchar_test, named_arg_udl) { using namespace fmt::literals; auto udl_a =