// 202207 Encodings in localized formatting of chrono, basic-format-string.
#define __cpp_lib_format 202110L
+#define __cpp_lib_format_uchar 202311L
+
#if __cplusplus > 202002L
// 202207 P2286R8 Formatting Ranges
// 202207 P2585R1 Improving default container formatting
typename basic_format_context<_Out, _CharT>::iterator
format(_CharT __u, basic_format_context<_Out, _CharT>& __fc) const
{
- if (_M_f._M_spec._M_type == __format::_Pres_none)
+ if (_M_f._M_spec._M_type == __format::_Pres_none
+ || _M_f._M_spec._M_type == __format::_Pres_c)
return _M_f._M_format_character(__u, __fc);
else if (_M_f._M_spec._M_type == __format::_Pres_esc)
{
return __fc.out();
}
else
- return _M_f.format(__u, __fc);
+ return _M_f.format(static_cast<make_unsigned_t<_CharT>>(__u), __fc);
}
#if __cpp_lib_format_ranges
typename basic_format_context<_Out, wchar_t>::iterator
format(char __u, basic_format_context<_Out, wchar_t>& __fc) const
{
- if (_M_f._M_spec._M_type == __format::_Pres_none)
+ if (_M_f._M_spec._M_type == __format::_Pres_none
+ || _M_f._M_spec._M_type == __format::_Pres_c)
return _M_f._M_format_character(__u, __fc);
else if (_M_f._M_spec._M_type == __format::_Pres_esc)
{
return __fc.out();
}
else
- return _M_f.format(__u, __fc);
+ return _M_f.format(static_cast<unsigned char>(__u), __fc);
}
#if __cpp_lib_format_ranges
using _Td = _Normalize<_Tp>;
if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
_M_set(_Td{__v.data(), __v.size()});
+ else if constexpr (is_same_v<remove_const_t<_Tp>, char>
+ && is_same_v<_CharT, wchar_t>)
+ _M_set(static_cast<_Td>(static_cast<unsigned char>(__v)));
else
_M_set(static_cast<_Td>(__v));
}
# error "Feature test macro for std::format has wrong value in <format>"
#endif
+#ifndef __cpp_lib_format_uchar
+# error "Feature test macro for formatting chars as integers is missing in <format>"
+#elif __cpp_lib_format_uchar < 202311L
+# error "Feature test macro for formatting chars as integers has wrong value in <format>"
+#endif
+
#undef __cpp_lib_format
#include <version>
#ifndef __cpp_lib_format
# error "Feature test macro for std::format has wrong value in <version>"
#endif
+#ifndef __cpp_lib_format_uchar
+# error "Feature test macro for formatting chars as integers is missing in <version>"
+#elif __cpp_lib_format_uchar < 202311L
+# error "Feature test macro for formatting chars as integers has wrong value in <version>"
+#endif
+
#include <string>
#include <limits>
#include <cstdint>
VERIFY( s == "0023 0077" );
s = std::format("{:b} {:B} {:#b} {:#B}", '\xff', '\xa0', '\x17', '\x3f');
- if constexpr (std::is_unsigned_v<char>)
- VERIFY( s == "11111111 10100000 0b10111 0B111111" );
- else
- VERIFY( s == "-1 -1100000 0b10111 0B111111" );
+ VERIFY( s == "11111111 10100000 0b10111 0B111111" );
s = std::format("{:x} {:#x} {:#X}", '\x12', '\x34', '\x45');
VERIFY( s == "12 0x34 0X45" );
+
+ // P2909R4 Fix formatting of code units as integers (Dude, where’s my char?)
+ // char and wchar_t should be converted to unsigned when formatting them
+ // with an integer presentation type.
+ s = std::format("{0:b} {0:B} {0:d} {0:o} {0:x} {0:X}", '\xf0');
+ VERIFY( s == "11110000 11110000 240 360 f0 F0" );
}
void
VERIFY( s == L"0.0625" );
s = std::format(L"{}", 0.25);
VERIFY( s == L"0.25" );
+
+ // P2909R4 Fix formatting of code units as integers (Dude, where’s my char?)
+ s = std::format(L"{:d} {:d}", wchar_t(-1), char(-1));
+ VERIFY( s.find('-') == std::wstring::npos );
}
void