From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 75CF93858D39; Thu, 27 Jul 2023 15:03:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75CF93858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690470191; bh=sGQPKoDboT00jHUFeB17g12gLIXyWgQDTlo9j2dDgeE=; h=From:To:Subject:Date:From; b=fjTrJ4N0+yeQK+ZN/ROwss2C8k9vbfPYQb/RHgiDEerGy86PR1+64oVc5jZVIgMGM VlYdCUqIog5OS7sFRqbGIC1mnpg92iGV+3EVaZe/0hiHTmusXS1olSyuvwPDJEOO0+ N3tas6xeN8fdm7PmGObMMW21viFyZM7OEAKcB6+Q= 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 r14-2825] libstdc++: Fix std::format alternate form for floating-point [PR108046] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 9bd1ee6d330b0441c165d889b6b25a5c85caf427 X-Git-Newrev: 50bc490c090cc95175e6068ed7438788d7fd7040 Message-Id: <20230727150311.75CF93858D39@sourceware.org> Date: Thu, 27 Jul 2023 15:03:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:50bc490c090cc95175e6068ed7438788d7fd7040 commit r14-2825-g50bc490c090cc95175e6068ed7438788d7fd7040 Author: Jonathan Wakely Date: Thu Jul 27 14:07:09 2023 +0100 libstdc++: Fix std::format alternate form for floating-point [PR108046] 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. Diff: --- libstdc++-v3/include/std/format | 17 +++++++++-------- libstdc++-v3/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