public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-7742] libstdc++: Fix std::format("{:F}", inf) to use uppercase
@ 2023-08-22 11:46 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-08-22 11:46 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:6c54fc2560cfb16734c0189f5e6399a78628b797

commit r13-7742-g6c54fc2560cfb16734c0189f5e6399a78628b797
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Aug 17 13:02:27 2023 +0100

    libstdc++: Fix std::format("{:F}", inf) to use uppercase
    
    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.
    
    (cherry picked from commit d07bce478f9d770de5acb0480a3f0cec2f8b72d8)

Diff:
---
 libstdc++-v3/include/std/format                       | 10 ++++++++--
 libstdc++-v3/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 7091685f1b89..a613f2c06eb6 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -319,7 +319,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.
@@ -1392,10 +1392,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;
@@ -1450,6 +1453,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 ba9403be79e3..33e1fe3bd095 100644
--- a/libstdc++-v3/testsuite/std/format/functions/format.cc
+++ b/libstdc++-v3/testsuite/std/format/functions/format.cc
@@ -154,6 +154,18 @@ test_alternate_forms()
   VERIFY( s == "-0.0" );
 }
 
+void
+test_infnan()
+{
+  double inf = std::numeric_limits<double>::infinity();
+  double nan = std::numeric_limits<double>::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<char>
 {
   std::string do_grouping() const override { return "\3\3"; }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-08-22 11:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-22 11:46 [gcc r13-7742] libstdc++: Fix std::format("{:F}", inf) to use uppercase Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).