_M_spec);
}
+ [[__gnu__::__always_inline__]]
+ static size_t
+ _S_character_width(_CharT __c)
+ {
+ // N.B. single byte cannot encode charcter of width greater than 1
+ if constexpr (sizeof(_CharT) > 1u &&
+ __unicode::__literal_encoding_is_unicode<_CharT>())
+ return __unicode::__field_width(__c);
+ else
+ return 1u;
+ }
+
template<typename _Out>
typename basic_format_context<_Out, _CharT>::iterator
_M_format_character(_CharT __c,
basic_format_context<_Out, _CharT>& __fc) const
{
- return __format::__write_padded_as_spec({&__c, 1u}, 1, __fc, _M_spec);
+ return __format::__write_padded_as_spec({&__c, 1u},
+ _S_character_width(__c),
+ __fc, _M_spec);
}
template<typename _Int>
{
// Similar to sC example in test_std_examples, but not from the standard.
// Verify that the character "🤡" has estimated field width 2,
- // rather than estimated field width equal to strlen("🤡"), which would be 4.
+ // rather than estimated field width equal to strlen("🤡"), which would be 4,
+ // or just width 1 for single character.
std::string sC = std::format("{:*<3}", "🤡");
VERIFY( sC == "🤡*" );
+ std::wstring wsC = std::format(L"{:*<3}", L"🤡");
+ VERIFY( wsC == L"🤡*" );
+ wsC = std::format(L"{:*<3}", L'🤡');
+ VERIFY( wsC == L"🤡*" );
// Verify that "£" has estimated field width 1, not strlen("£") == 2.
std::string sL = std::format("{:*<3}", "£");
std::string sP = std::format("{:1.1} {:*<1.1}", "£", "🤡");
VERIFY( sP == "£ *" );
sP = std::format("{:*<2.1} {:*<2.1}", "£", "🤡");
- VERIFY( sP == "£* **" );
// Verify field width handling for extended grapheme clusters,
// and that a cluster gets output as a single item, not truncated.