public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-8198] libstdc++: Implement P2909R4 ("Dude, where's my char?") for C++20
@ 2024-01-08 17:18 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2024-01-08 17:18 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:c5ef02e5629f8c5d9b1eaa852eea02169ede2e50

commit r13-8198-gc5ef02e5629f8c5d9b1eaa852eea02169ede2e50
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jan 3 15:35:50 2024 +0000

    libstdc++: Implement P2909R4 ("Dude, where's my char?") for C++20
    
    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)

Diff:
---
 libstdc++-v3/include/std/format                    | 15 ++++++++----
 libstdc++-v3/include/std/version                   |  1 +
 .../testsuite/std/format/functions/format.cc       | 27 ++++++++++++++++++----
 3 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index f454c49e12d..d7feca0f6aa 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -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));
 	}
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index 31e79bc6f40..bd1bee0190d 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -295,6 +295,7 @@
 #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
diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc
index d3bac2dbd25..8ce4ea72597 100644
--- a/libstdc++-v3/testsuite/std/format/functions/format.cc
+++ b/libstdc++-v3/testsuite/std/format/functions/format.cc
@@ -10,6 +10,12 @@
 # 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
@@ -18,6 +24,12 @@
 # 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-08 17:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-08 17:18 [gcc r13-8198] libstdc++: Implement P2909R4 ("Dude, where's my char?") for C++20 Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).