From 3bbf923aa5b1b4cc3e4608814aa9c7087925b1fa Mon Sep 17 00:00:00 2001 From: "Hans-Martin B. Jensen" Date: Sun, 14 Apr 2024 21:05:33 +0200 Subject: [PATCH] Fix format_to + FMT_STRING for wide character type Added overload that takes a wformat_string. Fixes issue #3925. --- include/fmt/xchar.h | 22 +++++++++++++++------- test/xchar-test.cc | 6 ++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h index 557dbe1d3adb..1ccde3e78f4e 100644 --- a/include/fmt/xchar.h +++ b/include/fmt/xchar.h @@ -127,6 +127,18 @@ auto join(const std::tuple& tuple, basic_string_view sep) return {tuple, sep}; } +template +auto format(wformat_string fmt, T&&... args) -> std::wstring { + return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); +} + +template +auto format_to(OutputIt out, wformat_string fmt, T&&... args) + -> OutputIt { + return vformat_to(out, fmt::wstring_view(fmt), + fmt::make_wformat_args(args...)); +} + template ::value)> auto vformat(basic_string_view format_str, typename detail::vformat_args::type args) @@ -136,11 +148,6 @@ auto vformat(basic_string_view format_str, return to_string(buf); } -template -auto format(wformat_string fmt, T&&... args) -> std::wstring { - return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); -} - // Pass char_t as a default template parameter instead of using // std::basic_string> to reduce the symbol size. 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(OutputIt out, const S& fmt, T&&... args) -> OutputIt { return vformat_to(out, detail::to_string_view(fmt), fmt::make_format_args>(args...)); diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 8f15a3479766..cbc961d360a5 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -195,6 +195,12 @@ TEST(xchar_test, format_to) { EXPECT_STREQ(buf.data(), L"42"); } +TEST(xchar_test, compile_time_string_format_to) { + std::wstring ws; + fmt::format_to(std::back_inserter(ws), FMT_STRING(L"{}"), 42); + EXPECT_EQ(L"42", ws); +} + TEST(xchar_test, vformat_to) { int n = 42; auto args = fmt::make_wformat_args(n);