]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Do not apply localized formatting to NaN and inf [PR114863]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 26 Apr 2024 10:42:26 +0000 (11:42 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 30 Apr 2024 20:20:33 +0000 (21:20 +0100)
We don't want to add grouping to strings like "-inf", and there is no
radix character to replace either.

libstdc++-v3/ChangeLog:

PR libstdc++/114863
* include/std/format (__formatter_fp::format): Only use
_M_localized for finite values.
* testsuite/std/format/functions/format.cc: Check localized
formatting of NaN and initiny.

(cherry picked from commit 7501c0a397fcf609a1ff5f083746b6330b89ee11)

libstdc++-v3/include/std/format
libstdc++-v3/testsuite/std/format/functions/format.cc

index a938d65a7b99dc10f35e145f6781d8b89d02d0bd..d69703a283ea08bac8bed2498dfc9d0ea3df4e82 100644 (file)
@@ -1666,7 +1666,7 @@ namespace __format
              __str = __wstr;
            }
 
-         if (_M_spec._M_localized)
+         if (_M_spec._M_localized && __builtin_isfinite(__v))
            {
              if constexpr (is_same_v<char, _CharT>)
                __wstr = _M_localize(__str, __expc, __fc.locale());
index 1f1802e561ca350ceebd11585cce5a5d283b57e4..16f5cda39f166c7e6678dcec5df798cfc4d42a02 100644 (file)
@@ -231,6 +231,14 @@ test_locale()
   s = std::format(cloc, "{:05L}", -1.0); // PR libstdc++/110968
   VERIFY( s == "-0001" );
 
+  // PR libstdc++/114863 grouping applied to nan and inf
+  double inf = std::numeric_limits<double>::infinity();
+  s = std::format(eloc, "{0:Le} {0:Lf} {0:Lg}", -inf);
+  VERIFY( s == "-inf -inf -inf" );
+  double nan = std::numeric_limits<double>::quiet_NaN();
+  s = std::format(eloc, "{0:Le} {0:Lf} {0:Lg}", -nan);
+  VERIFY( s == "-nan -nan -nan" );
+
   // Restore
   std::locale::global(cloc);
 }