]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Implement P2909R4 ("Dude, where's my char?") for C++20
authorJonathan Wakely <jwakely@redhat.com>
Wed, 3 Jan 2024 15:35:50 +0000 (15:35 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 8 Jan 2024 15:28:34 +0000 (15:28 +0000)
This change ensures that char and wchar_t arguments are formatted
consistently when using integer presentation types. This avoids
non-portable std::format output that depends on whether char and wchar_t
happen to be signed or unsigned on the target. Formatting '\xff' as an
integer will now always format 255 and not sometimes -1. This was
approved in Kona 2023 as a DR for C++20 so the change is implemented
unconditionally.

Also make character formatters check for _Pres_c explicitly and call
_M_format_character directly. This avoid the overhead of calling format
and _S_to_character and then calling _M_format_character anyway.

libstdc++-v3/ChangeLog:

* include/std/format (formatter<C, C>::format): Check for
_Pres_c and call _M_format_character directly. Cast C to its
unsigned equivalent for formatting as an integer.
(formatter<char, wchar_t>::format): Likewise.
(basic_format_arg(T&)): Store char arguments as unsigned char
for formatting to a wide string.
(__cpp_lib_format_uchar): Define.
* include/std/version (__cpp_lib_format_uchar): Define.
* testsuite/std/format/functions/format.cc: Adjust test. Check
formatting of characters using all integer presentation types.

(cherry picked from commit 74a0dab18292bef54f316eb086112332befbc6a7)

libstdc++-v3/include/std/format
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/std/format/functions/format.cc

index f454c49e12d69bdcb31d77255593279edfe07255..d7feca0f6aa72e6a26db448ac707444fa3602beb 100644 (file)
@@ -66,6 +66,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 // 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
@@ -1775,7 +1777,8 @@ namespace __format
        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)
            {
@@ -1783,7 +1786,7 @@ namespace __format
              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
@@ -1812,7 +1815,8 @@ namespace __format
        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)
            {
@@ -1820,7 +1824,7 @@ namespace __format
              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
@@ -3086,6 +3090,9 @@ namespace __format
          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));
        }
index 31e79bc6f40eccafdf7d87d2e58940035c5d686a..bd1bee0190d49b96565b40628afd60e32195fc17 100644 (file)
 #define __cpp_lib_constexpr_vector 201907L
 #define __cpp_lib_erase_if 202002L
 #define __cpp_lib_format 202110L
+#define __cpp_lib_format_uchar 202311L
 #define __cpp_lib_generic_unordered_lookup 201811L
 #ifdef _GLIBCXX_HAS_GTHREADS
 # define __cpp_lib_jthread 201911L
index d3bac2dbd254a7b207c2b2bd5aad805461d8125c..8ce4ea725976512fe1d51fde4e1f484b6c30d81f 100644 (file)
 # 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>
@@ -275,13 +287,16 @@ test_char()
   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
@@ -304,6 +319,10 @@ test_wchar()
   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