From patchwork Thu Aug 17 20:31:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 135946 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp964574vqi; Thu, 17 Aug 2023 13:39:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtQTIRWhfuBBjhRJB8Uu2K0EoEGdSlEjyGXirhuF80Xn4TUDzD0vLeEQQcZ/dhNh6e0v32 X-Received: by 2002:aa7:db47:0:b0:523:3e23:15ab with SMTP id n7-20020aa7db47000000b005233e2315abmr534352edt.26.1692304765572; Thu, 17 Aug 2023 13:39:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692304765; cv=none; d=google.com; s=arc-20160816; b=yY6W415Mjq9dVu+Iaj5+XevvJ59kbTC+5xXGGhV6m4j/Z7k9WCbvrV4SeZ2sU0KB3D Jtp1UTpBDDzWxyOcEHiU24RtKJ1dpgZN+SdappWlGDroEPv2aSCTObBboRLX8EtcQyHE +Xyh5/NfNWjV2+uUPnhsprCbnOEJDfhsseKNGh1bj9WeJvwl7FPygY59zxilRKBBWRS5 wwFfwcBnpaPh8eRxqQ6PqGl7/u/o38d2pZ6LDrZAFvCjtc84HlBlpGCKATrtitGjdxM+ 1MMsW+kQKoW+HfDbmj/GFFLleI9XpDIspNTt31NTnAxSLzB+AlcqMZizjQ7H4PRLQ6Yf p2YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=3SrLT02d7TqqLunV/lnU0UiEFVz6+F2mfHQfusPHJgY=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=wd84Ol9lHcBH1vu/pEeN4FghPmfi0rWcmPvUn8NQyXVR+AQ7a+tVyWVfR72jePqy1W 3y5UXQmO4ZIjiihfQ8kUVHKzNYAmg2/6imz0MQJ5THqNbRgaOrfNs8VMwiTPGdHMTtOK FPACdr2OyIF+W+i/SD8j4J0PUA1MCYyDACU2XHjMF1i2SX9NF1HfOrizOhjabAkKi7TH sd0CNhXIWpM2kW1vp31aUnsavnuy4pk0X2ey/uttpt0fwoTwCAiTrwxv3sv0D9aE/E2Q sgABSq5Y4MK+EVxAor+rgVSt5vsNzsUn3853ZuHRayDipq11M+KBHOP7pTdhNI0VnEL+ ekUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JPeCtdMx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j1-20020aa7ca41000000b005222c07f612si225377edt.496.2023.08.17.13.39.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 13:39:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JPeCtdMx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C0F42385086B for ; Thu, 17 Aug 2023 20:34:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0F42385086B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692304455; bh=3SrLT02d7TqqLunV/lnU0UiEFVz6+F2mfHQfusPHJgY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JPeCtdMxNjj/svJpAy6q4MRr491sioPN41w2BphnjK6ZR2AkNN7RVnAat8HX3OXL9 hT5AcSfXKacSXp0Gp6yELZ7qmT4axWwe/HUG8xYGCMUQG15HiudcNavl+7FHfptcbM HbwCKVhwFtYEVx078qKtTPhWD7Q6iCoEj7QL1bbc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 F0D953860766 for ; Thu, 17 Aug 2023 20:31:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0D953860766 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) 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 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774510161806100776 X-GMAIL-MSGID: 1774510161806100776 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