From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5018F3858020; Fri, 11 Aug 2023 00:03:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5018F3858020 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691712238; bh=tIrHvv3SSwtN2teNLvf6GVo09pSK/USNEk7MV7hnwq0=; h=From:To:Subject:Date:From; b=mi0SDq1samzlYRMX/ZCVaV7o4Rso5GlabKMOeKW4Yr0sVuLptGIxIQXubS/d9QnNJ IwqLp6A5dr93q/CeDzFRTSiTwNfce2y9YKRGOTYsx6qrSz4Qv7ycG6pgLGIeouAxTN +JE4diGkss9oxcIyTPLrjuIw3bV0O+QhHCPucVtE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7706] libstdc++: Fix std::format for localized floats [PR110968] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 97672bd599e32ec6d488a7532b4ad15311810a46 X-Git-Newrev: 15b3fc6c1c5296662d0ffd7d3cfb8b669421e5ea Message-Id: <20230811000358.5018F3858020@sourceware.org> Date: Fri, 11 Aug 2023 00:03:58 +0000 (GMT) List-Id: https://gcc.gnu.org/g:15b3fc6c1c5296662d0ffd7d3cfb8b669421e5ea commit r13-7706-g15b3fc6c1c5296662d0ffd7d3cfb8b669421e5ea Author: Jonathan Wakely Date: Thu Aug 10 14:33:44 2023 +0100 libstdc++: Fix std::format for localized floats [PR110968] The __formatter_fp::_M_localize function just returns an empty string if the formatting locale is the C locale, as there is nothing to do. But the caller was assuming that the returned string contains the localized string. The caller should use the original string if _M_localize returns an empty string. libstdc++-v3/ChangeLog: PR libstdc++/110968 * include/std/format (__formatter_fp::format): Check return value of _M_localize. * testsuite/std/format/functions/format.cc: Check classic locale. (cherry picked from commit f48a5423964f72e2e1ba0ad6a14d9d1464a78bed) Diff: --- libstdc++-v3/include/std/format | 5 +++-- libstdc++-v3/testsuite/std/format/functions/format.cc | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 96bf17e2aa17..8d6df7452fd7 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1608,8 +1608,8 @@ namespace __format _Optional_locale __loc; - basic_string_view<_CharT> __str; basic_string<_CharT> __wstr; + basic_string_view<_CharT> __str; if constexpr (is_same_v<_CharT, char>) __str = __narrow_str; else @@ -1632,7 +1632,8 @@ namespace __format __wstr = _M_localize(__str, __expc, __fc.locale()); else __wstr = _M_localize(__str, __expc, __loc.value()); - __str = __wstr; + if (!__wstr.empty()) + __str = __wstr; } size_t __width = _M_spec._M_get_width(__fc); diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 3485535e3cba..ba9403be79e3 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -193,6 +193,9 @@ test_locale() s = std::format(eloc, "{0:#Lg} {0:+#.3Lg} {0:#08.4Lg}", -1234.); VERIFY( s == "-1.234,00 -1,23e+03 -01.234," ); + s = std::format(cloc, "{:05L}", -1.0); // PR libstdc++/110968 + VERIFY( s == "-0001" ); + // Restore std::locale::global(cloc); }