From patchwork Thu Aug 10 22:33:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 134242 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp726866vqi; Thu, 10 Aug 2023 15:35:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFbq8aqruIPv2lbO5MdAwBIvOYxFcryJYXzkIJl61V5N8Dkr3/0yA2eV9U7Z7hPRtVW5tmO X-Received: by 2002:a17:906:5307:b0:98f:c9b:24ed with SMTP id h7-20020a170906530700b0098f0c9b24edmr186422ejo.67.1691706924136; Thu, 10 Aug 2023 15:35:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691706924; cv=none; d=google.com; s=arc-20160816; b=Wi6yLM6nf2HVo05RZXEwxMAKwQIjGysM9ITyfZAmBJE6esRM7OIgc1esv9CKP+LO12 UEHGMCgpEFSsYJaxgfLFHEiYanurkzcTRiUqpgHn1Vug0CNek9Oq6CDhjCmyfbWvPyax 9i5e4s3HZ3kq0Dk5S38KdH2cEnPKztXEQXGmRkj5DxwETiM9ffRslLBAUD9Lo4FVrEEx JSecTeWZT+aSwgPBUtmmFIg0j+vmXf/VCxpPpPzJjJFozti2Bm3/t5J9IFQnC8bzIngV 8OE8Ah3U7JxfgjIcIuEYo2KCpAXpxfxqHgzkLND6mXcZw6tc7+GfQr/CxYjz72OqN4wR 1ljg== 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=3MYLsjQhNtJqWgBmH26MKUXK9kaaG0XN9QYSTOFL8Oo=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=NOg1uA3O6mL5Vgg4GjyNpCzmBWAvw4t5+zdWQpTL89Ensgo27EA3ZijaagGw4Pzr5S nahJJCEycd5OgoRWaxWZCd+eY4+07f02712hXBXyxqx7LI8TL0IADrYmY+XSKohP+j0g Fp0nBZSfAt35Z5tzxP6POtnUfKQRLuPyV3B6UCml3rpE+4g+cN0InztOGw9Q+tzRdRbE rDdSJm9sybMeU4nPz8bQjedFr8wMDHJLtcQuXrcBlOl7IPf1oYtZFxHRUCoXhGTgTSGK oOdHmLCoqiXyDcV2/AtUaJc3uwH3+R5U/HsgsXRQZ8mJ52r6bF4wfPoV06dIrqqzJaGk J92Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=sSLM2eIq; 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 c8-20020a17090654c800b0098dfb10f3a6si2260826ejp.107.2023.08.10.15.35.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 15:35:24 -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=sSLM2eIq; 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 707743857028 for ; Thu, 10 Aug 2023 22:34:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 707743857028 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691706881; bh=3MYLsjQhNtJqWgBmH26MKUXK9kaaG0XN9QYSTOFL8Oo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sSLM2eIq3XsGVQAesLvWiJVRW311WQEJZrWovgw3dBmMtac+dzFo3OjgyWUXaZHDV 98cDwAdXqZ9Sc5WgCet64+T7UjuaVFc6AdJf52m0tMyOJ7vb8SLoQNYHALWt03hIcW DG8f62FDCiHkENNRy9cO/tcMG+tQOC28jLSEkIkc= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id E94463858288 for ; Thu, 10 Aug 2023 22:33:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E94463858288 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-608-4weNmYW8P8m9bm09_reLRw-1; Thu, 10 Aug 2023 18:33:54 -0400 X-MC-Unique: 4weNmYW8P8m9bm09_reLRw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D17A62A59562; Thu, 10 Aug 2023 22:33:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.188]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9554C1121314; Thu, 10 Aug 2023 22:33:53 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::format for localized floats [PR110968] Date: Thu, 10 Aug 2023 23:33:45 +0100 Message-ID: <20230810223353.1242664-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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 autolearn=ham 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: 1773883279694755346 X-GMAIL-MSGID: 1773883279694755346 Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 to follow. -- >8 -- 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. --- 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 96eb4cd742e..5d7af53fc94 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1610,8 +1610,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 @@ -1634,7 +1634,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 bd914df6d7c..471cffb2b36 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -197,6 +197,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); }