From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id C45B938582AA; Mon, 8 Jan 2024 17:18:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C45B938582AA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1704734280; bh=+tezhl4Jk/8WdgDx4fuPK9urxIlNJ7z+1ixPfun16Rk=; h=From:To:Subject:Date:From; b=aHxKw2D6OfK84ScZSq1T5ptW4lCVZa6ZIHZVkn5ljujrMOUp9QipEAIu+PiK7euI6 Wp8Y3HSomb0JzZ5sLsMoBrL+VWuR/eTAp+v1tJWWXWGAF+RvwZwMPdfXvnPoPJoL0Q znBHI+twYF5dH1KDWfpf/pyRs183h0vN9uh6z6m4= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-8198] libstdc++: Implement P2909R4 ("Dude, where's my char?") for C++20 X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 5d0d592c486c31bd9c26af0d1f0a7b6a3d68c22b X-Git-Newrev: c5ef02e5629f8c5d9b1eaa852eea02169ede2e50 Message-Id: <20240108171800.C45B938582AA@sourceware.org> Date: Mon, 8 Jan 2024 17:18:00 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c5ef02e5629f8c5d9b1eaa852eea02169ede2e50 commit r13-8198-gc5ef02e5629f8c5d9b1eaa852eea02169ede2e50 Author: Jonathan Wakely 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::format): Check for _Pres_c and call _M_format_character directly. Cast C to its unsigned equivalent for formatting as an integer. (formatter::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>(__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(__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, char> + && is_same_v<_CharT, wchar_t>) + _M_set(static_cast<_Td>(static_cast(__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 " #endif +#ifndef __cpp_lib_format_uchar +# error "Feature test macro for formatting chars as integers is missing in " +#elif __cpp_lib_format_uchar < 202311L +# error "Feature test macro for formatting chars as integers has wrong value in " +#endif + #undef __cpp_lib_format #include #ifndef __cpp_lib_format @@ -18,6 +24,12 @@ # error "Feature test macro for std::format has wrong value in " #endif +#ifndef __cpp_lib_format_uchar +# error "Feature test macro for formatting chars as integers is missing in " +#elif __cpp_lib_format_uchar < 202311L +# error "Feature test macro for formatting chars as integers has wrong value in " +#endif + #include #include #include @@ -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) - 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