From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 2D5CD385E021 for ; Thu, 17 Aug 2023 20:31:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D5CD385E021 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692304280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3SrLT02d7TqqLunV/lnU0UiEFVz6+F2mfHQfusPHJgY=; b=Rr+Osjj3Erza105gyjK0KFgL+ZRbJWmVlhNMjNZM02knrslTSD0Rehd/xsmcDyaxqeEXgY 0zDDN321YfMJ6RhcNaKqhzatqmFvk7dfmD2qSaY0B4VEYxYMwXcJOMoJH0wax5rriDkwFO 3FzzBjPGRTWonpCtCt0jPUM92W8vdzk= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-627-JmIyJWX9PFmcOhR79-mvng-1; Thu, 17 Aug 2023 16:31:19 -0400 X-MC-Unique: JmIyJWX9PFmcOhR79-mvng-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EDBF51C07559; Thu, 17 Aug 2023 20:31:18 +0000 (UTC) Received: from localhost (unknown [10.42.28.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E20D140E950; Thu, 17 Aug 2023 20:31:18 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Rework std::format support for wchar_t Date: Thu, 17 Aug 2023 21:31:08 +0100 Message-ID: <20230817203118.1131359-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- This changes how std::format creates wide strings, by replacing uses of std::ctype::widen with the recently-added __to_wstring_numeric helper function. This removes the dependency on the locale, which should only be used for locale-specific formats such as {:Ld}. Also disable all the wide string formatting support if the _GLIBCXX_USE_WCHAR_T macro is not defined. This is consistent with other wchar_t support being disabled if the library is built without that macro defined. libstdc++-v3/ChangeLog: * include/std/format [_GLIBCXX_USE_WCHAR_T]: Guard all wide string formatters with this macro. (__formatter_int::_M_format_int, __formatter_fp::format) (formatter::format): Use __to_wstring_numeric instead of std::ctype::widen. (__formatter_fp::_M_localize): Use hardcoded wchar_t values instead of std::ctype::widen. * testsuite/std/format/functions/format.cc: Add more checks for wstring formatting of arithmetic types. --- libstdc++-v3/include/std/format | 108 ++++++++++++------ .../testsuite/std/format/functions/format.cc | 10 ++ 2 files changed, 82 insertions(+), 36 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 0d7d3d16420..79f810acce3 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -79,8 +79,10 @@ namespace __format using format_context = basic_format_context<__format::_Sink_iter, char>; +#ifdef _GLIBCXX_USE_WCHAR_T using wformat_context = basic_format_context<__format::_Sink_iter, wchar_t>; +#endif // [format.args], class template basic_format_args template class basic_format_args; @@ -118,9 +120,11 @@ namespace __format template using format_string = basic_format_string...>; +#ifdef _GLIBCXX_USE_WCHAR_T template using wformat_string = basic_format_string...>; +#endif // [format.formatter], formatter @@ -181,7 +185,9 @@ namespace __format // [format.parse.ctx], class template basic_format_parse_context template class basic_format_parse_context; using format_parse_context = basic_format_parse_context; +#ifdef _GLIBCXX_USE_WCHAR_T using wformat_parse_context = basic_format_parse_context; +#endif template class basic_format_parse_context @@ -745,8 +751,13 @@ namespace __format bool _M_hasval = false; }; +#ifdef _GLIBCXX_USE_WCHAR_T template concept __char = same_as<_CharT, char> || same_as<_CharT, wchar_t>; +#else + template + concept __char = same_as<_CharT, char>; +#endif template<__char _CharT> struct __formatter_str @@ -1125,26 +1136,20 @@ namespace __format { size_t __width = _M_spec._M_get_width(__fc); - _Optional_locale __loc; - basic_string_view<_CharT> __str; if constexpr (is_same_v) __str = __narrow_str; else { - __loc = __fc.locale(); - auto& __ct = use_facet>(__loc.value()); size_t __n = __narrow_str.size(); auto __p = (_CharT*)__builtin_alloca(__n * sizeof(_CharT)); - __ct.widen(__narrow_str.data(), __narrow_str.data() + __n, __p); + __to_wstring_numeric(__narrow_str.data(), __n, __p); __str = {__p, __n}; } if (_M_spec._M_localized) { - if constexpr (is_same_v) - __loc = __fc.locale(); - const auto& __l = __loc.value(); + const auto& __l = __fc.locale(); if (__l.name() != "C") { auto& __np = use_facet>(__l); @@ -1612,35 +1617,19 @@ namespace __format } } - // TODO move everything below to a new member function that - // doesn't depend on _Fp type. - - - _Optional_locale __loc; basic_string<_CharT> __wstr; basic_string_view<_CharT> __str; if constexpr (is_same_v<_CharT, char>) __str = __narrow_str; else { - __loc = __fc.locale(); - auto& __ct = use_facet>(__loc.value()); - const char* __data = __narrow_str.data(); - auto __overwrite = [&__data, &__ct](_CharT* __p, size_t __n) - { - __ct.widen(__data, __data + __n, __p); - return __n; - }; - _S_resize_and_overwrite(__wstr, __narrow_str.size(), __overwrite); + __wstr = std::__to_wstring_numeric(__narrow_str); __str = __wstr; } if (_M_spec._M_localized) { - if constexpr (is_same_v) - __wstr = _M_localize(__str, __expc, __fc.locale()); - else - __wstr = _M_localize(__str, __expc, __loc.value()); + __wstr = _M_localize(__str, __expc, __fc.locale()); if (!__wstr.empty()) __str = __wstr; } @@ -1697,9 +1686,24 @@ namespace __format } else { - const auto& __ct = use_facet>(__loc); - __dot = __ct.widen('.'); - __exp = __ct.widen(__expc); + __dot = L'.'; + switch (__expc) + { + case 'e': + __exp = L'e'; + break; + case 'E': + __exp = L'E'; + break; + case 'p': + __exp = L'p'; + break; + case 'P': + __exp = L'P'; + break; + default: + __builtin_unreachable(); + } } if (__grp.empty() && __point == __dot) @@ -1737,7 +1741,7 @@ namespace __format } // namespace __format /// @endcond - // Format a character. + /// Format a character. template<__format::__char _CharT> struct formatter<_CharT, _CharT> { @@ -1774,7 +1778,8 @@ namespace __format __format::__formatter_int<_CharT> _M_f; }; - // Format a char value for wide character output. +#ifdef _GLIBCXX_USE_WCHAR_T + /// Format a char value for wide character output. template<> struct formatter { @@ -1808,6 +1813,7 @@ namespace __format private: __format::__formatter_int _M_f; }; +#endif // USE_WCHAR_T /** Format a string. * @{ @@ -1903,6 +1909,7 @@ namespace __format __format::__formatter_str _M_f; }; +#ifdef _GLIBCXX_USE_WCHAR_T template struct formatter, wchar_t> { @@ -1926,6 +1933,7 @@ namespace __format private: __format::__formatter_str _M_f; }; +#endif // USE_WCHAR_T template struct formatter, char> @@ -1951,6 +1959,7 @@ namespace __format __format::__formatter_str _M_f; }; +#ifdef _GLIBCXX_USE_WCHAR_T template struct formatter, wchar_t> { @@ -1974,6 +1983,7 @@ namespace __format private: __format::__formatter_str _M_f; }; +#endif // USE_WCHAR_T /// @} /// Format an integer. @@ -2144,10 +2154,8 @@ namespace __format __str = string_view(__buf, __n); else { - const std::locale& __loc = __fc.locale(); - auto& __ct = use_facet>(__loc); auto __p = (_CharT*)__builtin_alloca(__n * sizeof(_CharT)); - __ct.widen(__buf, __buf + __n, __p); + __to_wstring_numeric(__buf, __n, __p); __str = wstring_view(__p, __n); } @@ -3698,25 +3706,33 @@ namespace __format vformat_to(_Out __out, string_view __fmt, format_args __args) { return __format::__do_vformat_to(std::move(__out), __fmt, __args); } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> [[__gnu__::__always_inline__]] inline _Out vformat_to(_Out __out, wstring_view __fmt, wformat_args __args) { return __format::__do_vformat_to(std::move(__out), __fmt, __args); } +#endif template requires output_iterator<_Out, const char&> [[__gnu__::__always_inline__]] inline _Out vformat_to(_Out __out, const locale& __loc, string_view __fmt, format_args __args) - { return __format::__do_vformat_to(std::move(__out), __fmt, __args, &__loc); } + { + return __format::__do_vformat_to(std::move(__out), __fmt, __args, &__loc); + } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> [[__gnu__::__always_inline__]] inline _Out vformat_to(_Out __out, const locale& __loc, wstring_view __fmt, wformat_args __args) - { return __format::__do_vformat_to(std::move(__out), __fmt, __args, &__loc); } + { + return __format::__do_vformat_to(std::move(__out), __fmt, __args, &__loc); + } +#endif [[nodiscard]] inline string @@ -3727,6 +3743,7 @@ namespace __format return std::move(__buf).get(); } +#ifdef _GLIBCXX_USE_WCHAR_T [[nodiscard]] inline wstring vformat(wstring_view __fmt, wformat_args __args) @@ -3735,6 +3752,7 @@ namespace __format std::vformat_to(__buf.out(), __fmt, __args); return std::move(__buf).get(); } +#endif [[nodiscard]] inline string @@ -3745,6 +3763,7 @@ namespace __format return std::move(__buf).get(); } +#ifdef _GLIBCXX_USE_WCHAR_T [[nodiscard]] inline wstring vformat(const locale& __loc, wstring_view __fmt, wformat_args __args) @@ -3753,6 +3772,7 @@ namespace __format std::vformat_to(__buf.out(), __loc, __fmt, __args); return std::move(__buf).get(); } +#endif template [[nodiscard]] @@ -3760,11 +3780,13 @@ namespace __format format(format_string<_Args...> __fmt, _Args&&... __args) { return std::vformat(__fmt.get(), std::make_format_args(__args...)); } +#ifdef _GLIBCXX_USE_WCHAR_T template [[nodiscard]] inline wstring format(wformat_string<_Args...> __fmt, _Args&&... __args) { return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); } +#endif template [[nodiscard]] @@ -3776,6 +3798,7 @@ namespace __format std::make_format_args(__args...)); } +#ifdef _GLIBCXX_USE_WCHAR_T template [[nodiscard]] inline wstring @@ -3785,6 +3808,7 @@ namespace __format return std::vformat(__loc, __fmt.get(), std::make_wformat_args(__args...)); } +#endif template requires output_iterator<_Out, const char&> @@ -3795,6 +3819,7 @@ namespace __format std::make_format_args(std::forward<_Args>(__args)...)); } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> inline _Out @@ -3803,6 +3828,7 @@ namespace __format return std::vformat_to(std::move(__out), __fmt.get(), std::make_wformat_args(std::forward<_Args>(__args)...)); } +#endif template requires output_iterator<_Out, const char&> @@ -3814,6 +3840,7 @@ namespace __format std::make_format_args(std::forward<_Args>(__args)...)); } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> inline _Out @@ -3823,6 +3850,7 @@ namespace __format return std::vformat_to(std::move(__out), __loc, __fmt.get(), std::make_wformat_args(std::forward<_Args>(__args)...)); } +#endif template requires output_iterator<_Out, const char&> @@ -3836,6 +3864,7 @@ namespace __format return std::move(__sink)._M_finish(); } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> inline format_to_n_result<_Out> @@ -3847,6 +3876,7 @@ namespace __format std::make_wformat_args(__args...)); return std::move(__sink)._M_finish(); } +#endif template requires output_iterator<_Out, const char&> @@ -3860,6 +3890,7 @@ namespace __format return std::move(__sink)._M_finish(); } +#ifdef _GLIBCXX_USE_WCHAR_T template requires output_iterator<_Out, const wchar_t&> inline format_to_n_result<_Out> @@ -3871,6 +3902,7 @@ namespace __format std::make_wformat_args(__args...)); return std::move(__sink)._M_finish(); } +#endif /// @cond undocumented namespace __format @@ -3927,6 +3959,7 @@ namespace __format return __buf.count(); } +#ifdef _GLIBCXX_USE_WCHAR_T template [[nodiscard]] inline size_t @@ -3937,6 +3970,7 @@ namespace __format std::make_wformat_args(std::forward<_Args>(__args)...)); return __buf.count(); } +#endif template [[nodiscard]] @@ -3950,6 +3984,7 @@ namespace __format return __buf.count(); } +#ifdef _GLIBCXX_USE_WCHAR_T template [[nodiscard]] inline size_t @@ -3961,6 +3996,7 @@ namespace __format std::make_wformat_args(std::forward<_Args>(__args)...)); return __buf.count(); } +#endif #if __cpp_lib_format_ranges // [format.range], formatting of ranges diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 59ed3be8baa..59d327fccee 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -274,6 +274,16 @@ test_wchar() VERIFY( s == L"0.0625" ); s = std::format(L"{}", 0.25); VERIFY( s == L"0.25" ); + s = std::format(L"{:+a} {:A}", 0x1.23p45, -0x1.abcdefp-15); + VERIFY( s == L"+1.23p+45 -1.ABCDEFP-15" ); + + double inf = std::numeric_limits::infinity(); + double nan = std::numeric_limits::quiet_NaN(); + s = std::format(L"{0} {0:F} {1} {1:E}", -inf, -nan); + VERIFY( s == L"-inf -INF -nan -NAN" ); + + s = std::format(L"{0:#b} {0:#B} {0:#x} {0:#X}", 99); + VERIFY( s == L"0b1100011 0B1100011 0x63 0X63" ); } void -- 2.41.0