Skip to content

Commit ee0c335

Browse files
authored
Fix format_to + FMT_STRING for wide character type (#3931)
Added overload that takes a wformat_string. Fixes issue #3925.
1 parent 9973576 commit ee0c335

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Diff for: include/fmt/xchar.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
141141
return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
142142
}
143143

144+
template <typename OutputIt, typename... T>
145+
auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
146+
-> OutputIt {
147+
return vformat_to(out, fmt::wstring_view(fmt),
148+
fmt::make_wformat_args(args...));
149+
}
150+
144151
// Pass char_t as a default template parameter instead of using
145152
// std::basic_string<char_t<S>> to reduce the symbol size.
146153
template <typename S, typename... T,
@@ -186,8 +193,9 @@ auto vformat_to(OutputIt out, const S& format_str,
186193

187194
template <typename OutputIt, typename S, typename... T,
188195
typename Char = detail::format_string_char_t<S>,
189-
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
190-
detail::is_exotic_char<Char>::value)>
196+
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value &&
197+
!std::is_same<Char, char>::value &&
198+
!std::is_same<Char, wchar_t>::value)>
191199
inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
192200
return vformat_to(out, detail::to_string_view(fmt),
193201
fmt::make_format_args<buffered_context<Char>>(args...));

Diff for: test/xchar-test.cc

+6
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ TEST(xchar_test, format_to) {
195195
EXPECT_STREQ(buf.data(), L"42");
196196
}
197197

198+
TEST(xchar_test, compile_time_string_format_to) {
199+
std::wstring ws;
200+
fmt::format_to(std::back_inserter(ws), FMT_STRING(L"{}"), 42);
201+
EXPECT_EQ(L"42", ws);
202+
}
203+
198204
TEST(xchar_test, vformat_to) {
199205
int n = 42;
200206
auto args = fmt::make_wformat_args(n);

0 commit comments

Comments
 (0)