From patchwork Thu Aug 17 12:21:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1822332 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=qWNE++2d; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RRPKn0RzPz1xrk for ; Thu, 17 Aug 2023 22:23:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13ABA385DC11 for ; Thu, 17 Aug 2023 12:23:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 13ABA385DC11 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692274987; bh=bfTTPTmtiog8g7pR7UIhU3t8siNk1+8Sv6uK4Z3EAkc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qWNE++2dRz0N0ao2wyPV/P2aOmQkb0MUzzy4KuSlpzb0oZU7BRXE7bCXHfEkG1UUb L0OpPf6VCLZJ2bo3mggp0RguJmLbyDUqCFt4LL+u6yFOWJwHeINEXgIiW4QARbA4Ns M04Gn1FTzPtwSlekGZTTLuWWawN95Cxf5I/4jnNA= 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 1ACF73853D3C for ; Thu, 17 Aug 2023 12:22:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1ACF73853D3C 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-594-hx8p5eKhNkqm87W-nD2o3A-1; Thu, 17 Aug 2023 08:22:26 -0400 X-MC-Unique: hx8p5eKhNkqm87W-nD2o3A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 35F5F29AA3B0; Thu, 17 Aug 2023 12:22:26 +0000 (UTC) Received: from localhost (unknown [10.42.28.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id F41782026D68; Thu, 17 Aug 2023 12:22:25 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::format("{:F}", inf) to use uppercase Date: Thu, 17 Aug 2023 13:21:50 +0100 Message-ID: <20230817122226.966809-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 will follow. -- >8 -- std::format was treating {:f} and {:F} identically on the basis that for the fixed 1.234567 format there are no alphabetical characters that need to be in uppercase. But that's wrong for infinities and NaNs, which should be formatted as "INF" and "NAN" for {:F}. libstdc++-v3/ChangeLog: * include/std/format (__format::_Pres_type): Add _Pres_F. (__formatter_fp::parse): Use _Pres_F for 'F'. (__formatter_fp::format): Set __upper for _Pres_F. * testsuite/std/format/functions/format.cc: Check formatting of infinity and NaN for each presentation type. --- libstdc++-v3/include/std/format | 10 ++++++++-- .../testsuite/std/format/functions/format.cc | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index a8db10d6460..40c7d6128f6 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -309,7 +309,7 @@ namespace __format // Presentation types for integral types (including bool and charT). _Pres_d = 1, _Pres_b, _Pres_B, _Pres_o, _Pres_x, _Pres_X, _Pres_c, // Presentation types for floating-point types. - _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_g, _Pres_G, + _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F, _Pres_g, _Pres_G, _Pres_p = 0, _Pres_P, // For pointers. _Pres_s = 0, // For strings and bool. _Pres_esc = 0xf, // For strings and charT. @@ -1382,10 +1382,13 @@ namespace __format ++__first; break; case 'f': - case 'F': __spec._M_type = _Pres_f; ++__first; break; + case 'F': + __spec._M_type = _Pres_F; + ++__first; + break; case 'g': __spec._M_type = _Pres_g; ++__first; @@ -1442,6 +1445,9 @@ namespace __format __use_prec = true; __fmt = chars_format::scientific; break; + case _Pres_F: + __upper = true; + [[fallthrough]]; case _Pres_f: __use_prec = true; __fmt = chars_format::fixed; diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 4db5202815d..59ed3be8baa 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -159,6 +159,18 @@ test_alternate_forms() VERIFY( s == "1.e+01 1.e+01 1.e+01" ); } +void +test_infnan() +{ + double inf = std::numeric_limits::infinity(); + double nan = std::numeric_limits::quiet_NaN(); + std::string s; + s = std::format("{0} {0:e} {0:E} {0:f} {0:F} {0:g} {0:G} {0:a} {0:A}", inf); + VERIFY( s == "inf inf INF inf INF inf INF inf INF" ); + s = std::format("{0} {0:e} {0:E} {0:f} {0:F} {0:g} {0:G} {0:a} {0:A}", nan); + VERIFY( s == "nan nan NAN nan NAN nan NAN nan NAN" ); +} + struct euro_punc : std::numpunct { std::string do_grouping() const override { return "\3\3"; }