From patchwork Thu Jul 27 15:03:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 127002 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp1164012vqo; Thu, 27 Jul 2023 08:05:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlHHSguxN51V12KsdPYE2bt0ix9UdPqXyBV0RmxRbUwK0ozK/G+DPcqIbqM3ERGGIZMo6gtk X-Received: by 2002:aa7:c706:0:b0:522:2111:1063 with SMTP id i6-20020aa7c706000000b0052221111063mr1965305edq.18.1690470318020; Thu, 27 Jul 2023 08:05:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690470318; cv=none; d=google.com; s=arc-20160816; b=aGvv2bLcVhLMfZAYBh5Ybp1WFYx3meO0ZH7FYVrMolkUIYPPVpQopwkSZma2VAign3 flMfJ7t9AarE0uBqlafbAWAWiQLdl/XkfmEO65KJwn1ICa0T2DiJ7v88wFabTD0LgCNx C9EuLXDKiE6ZQQM+TaRfcTbTWCZ9sIqz5QiVOgAWQJoR9sBhAmOwY2fpR5tgNFNmCV75 1Az5JSafItj7bMt6HMkUnW/epVyLMNPTDzY0Aem9zByLt4iGG0gdPBowBHDW0F8Peclo N2m9Yec7/ZOo5SmRMuWAkw6oDbyl53Cj002R0JxqzSoa3vUriDD/14dctKRdqlDIJCrp LeoQ== 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=DED4KXRGZlIXVaajnzLSxtehS1I211CS79dQGg4duHg=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=SHplSlL4fLEMh7dpEkEsM17aI/sNDLpyi7EwiG+dozQPgv4us6DSUbQ/1Z/DeGAnXA ZRV8NaYM5sSBbaVLBTtChGA9S4qPQKGZvJE31bxmazUltDfsgR9uwVobk/y4RBowVANO Ejse7fu/hXxhvz7lba0wCz5FQyW9Tk7hPq2xoYbl77mlBZj+2SQEtwo2X8U2/usz6WfR 5uT2+Y7XYcMGrnsQtnynKxlkIuI7zuH1GKPzDmjz1EsHfXhnT5CA4UNYx7P/YnzVpVeG 2nt9x6KQF7itFG9J4KWxxe0rsVEWheyGDPx84XcRUU9kByCtg9HvPMzAiSH6eyPt1J8o c/PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ta8w4jIb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l21-20020a056402345500b005222bdf55e9si1054413edc.205.2023.07.27.08.05.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 08:05:18 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ta8w4jIb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 3D4E03853D1D for ; Thu, 27 Jul 2023 15:04:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D4E03853D1D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690470289; bh=DED4KXRGZlIXVaajnzLSxtehS1I211CS79dQGg4duHg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ta8w4jIbTB7Qm6+KKLTr+boKEQaJN7WbQjP0chul/uuD9LPFFqZYEIZCr9fnmLoSo L2tzhGQxwHZ49NIPU8X2KfsQFEIPRsrrS9Z8i5ZbT3zUZds2O3FLcLCqJI51u44LlV eQt032GapDKzgNNYFFOlEv4nXjBRY3J5qmtsQfIQ= 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 C9E933858D39 for ; Thu, 27 Jul 2023 15:03:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9E933858D39 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-378-5biQl7oLPTWtmcTJTZk1Ow-1; Thu, 27 Jul 2023 11:03:49 -0400 X-MC-Unique: 5biQl7oLPTWtmcTJTZk1Ow-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 579982815E2C; Thu, 27 Jul 2023 15:03:49 +0000 (UTC) Received: from localhost (unknown [10.42.28.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id 824E01121330; Thu, 27 Jul 2023 15:03:48 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::format alternate form for floating-point [PR108046] Date: Thu, 27 Jul 2023 16:03:11 +0100 Message-ID: <20230727150345.303590-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, T_SCC_BODY_TEXT_LINE 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: 1772586604018012697 X-GMAIL-MSGID: 1772586604018012697 Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 to follow. -- >8 -- A decimal point was being added to the end of the string for {:#.0} because the __expc character was not being set, for the _Pres_none presentation type, so __s.find(__expc) didn't the 'e' in "1e+01" and so we created "1e+01." by appending the radix char to the end. This can be fixed by ensuring that __expc='e' is set for the _Pres_none case. I realized we can also set __expc='P' and __expc='E' when needed, to save a call to std::toupper later. For the {:#.0g} format, __expc='e' was being set and so the 'e' was found in "1e+10" but then __z = __prec - __sigfigs would wraparound to SIZE_MAX. That meant we would decide not to add a radix char because the number of extra characters to insert would be 1+SIZE_MAX i.e. zero. This can be fixed by using __z == 0 when __prec == 0. libstdc++-v3/ChangeLog: PR libstdc++/108046 * include/std/format (__formatter_fp::format): Ensure __expc is always set for all presentation types. Set __z correctly for zero precision. * testsuite/std/format/functions/format.cc: Check problem cases. --- libstdc++-v3/include/std/format | 17 +++++++++-------- .../testsuite/std/format/functions/format.cc | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 0c6069b2681..1e0ef612ddd 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1430,22 +1430,24 @@ namespace __format chars_format __fmt{}; bool __upper = false; bool __trailing_zeros = false; - char __expc = 0; + char __expc = 'e'; switch (_M_spec._M_type) { case _Pres_A: __upper = true; + __expc = 'P'; [[fallthrough]]; case _Pres_a: - __expc = 'p'; + if (_M_spec._M_type != _Pres_A) + __expc = 'p'; __fmt = chars_format::hex; break; case _Pres_E: __upper = true; + __expc = 'E'; [[fallthrough]]; case _Pres_e: - __expc = 'e'; __use_prec = true; __fmt = chars_format::scientific; break; @@ -1455,10 +1457,10 @@ namespace __format break; case _Pres_G: __upper = true; + __expc = 'E'; [[fallthrough]]; case _Pres_g: __trailing_zeros = true; - __expc = 'e'; __use_prec = true; __fmt = chars_format::general; break; @@ -1511,7 +1513,6 @@ namespace __format { for (char* __p = __start; __p != __res.ptr; ++__p) *__p = std::toupper(*__p); - __expc = std::toupper(__expc); } // Add sign for non-negative values. @@ -1545,15 +1546,15 @@ namespace __format __p = __s.find(__expc); if (__p == __s.npos) __p = __s.size(); - __d = __p; + __d = __p; // Position where '.' should be inserted. __sigfigs = __d; } - if (__trailing_zeros) + if (__trailing_zeros && __prec != 0) { if (!__format::__is_xdigit(__s[0])) --__sigfigs; - __z = __prec - __sigfigs; + __z = __prec - __sigfigs; // Number of zeros to insert. } if (size_t __extras = int(__d == __p) + __z) diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 3485535e3cb..bd914df6d7c 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -152,6 +152,10 @@ test_alternate_forms() s = std::format("{:#.2g}", -0.0); VERIFY( s == "-0.0" ); + + // PR libstdc++/108046 + s = std::format("{0:#.0} {0:#.1} {0:#.0g}", 10.0); + VERIFY( s == "1.e+01 1.e+01 1.e+01" ); } struct euro_punc : std::numpunct