Skip to content

Commit 0518f10

Browse files
committed
Add output formatting to all character types
1 parent 040412c commit 0518f10

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

include/boost/decimal/format.hpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ struct formatter<T>
225225
template <typename FormatContext>
226226
auto format(const T &v, FormatContext &ctx) const
227227
{
228+
using CharType = FormatContext::char_type;
228229
using namespace boost::decimal;
229230
using namespace boost::decimal::detail;
230231

@@ -302,7 +303,46 @@ struct formatter<T>
302303
s.resize(initial_length + offset);
303304
}
304305

305-
return std::format_to(ctx.out(), "{}", s);
306+
if constexpr (std::is_same_v<CharType, char>)
307+
{
308+
return std::format_to(ctx.out(), "{}", s);
309+
}
310+
else if constexpr (std::is_same_v<CharType, wchar_t>)
311+
{
312+
std::wstring result;
313+
result.reserve(s.size());
314+
for (const char c : s)
315+
{
316+
result.push_back(static_cast<CharType>(static_cast<unsigned char>(c)));
317+
}
318+
319+
return std::format_to(ctx.out(), L"{}", result);
320+
}
321+
else
322+
{
323+
// For other character types (char16_t, char32_t, etc.)
324+
325+
std::basic_string<CharType> result;
326+
result.reserve(s.size());
327+
for (const char c : s)
328+
{
329+
result.push_back(static_cast<CharType>(static_cast<unsigned char>(c)));
330+
}
331+
332+
if constexpr (std::is_same_v<CharType, char16_t>)
333+
{
334+
return std::format_to(ctx.out(), u"{}", result);
335+
}
336+
else if constexpr (std::is_same_v<CharType, char32_t>)
337+
{
338+
return std::format_to(ctx.out(), U"{}", result);
339+
}
340+
else
341+
{
342+
static_assert(std::is_same_v<CharType, char8_t>, "Unsupported wide character type");
343+
return std::format_to(ctx.out(), u8"{}", result);
344+
}
345+
}
306346
}
307347
};
308348

0 commit comments

Comments
 (0)