]> 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>
Fri, 26 Apr 2024 14:57:38 +0000 (15:57 +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.

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

index 22dcb5f24bd70cdb61914c39b310b6141d482189..48deba2bcb2dcfa2ac76ffc5aac978c4ce418d02 100644 (file)
@@ -1734,7 +1734,7 @@ namespace __format
            }
 #endif
 
-         if (_M_spec._M_localized)
+         if (_M_spec._M_localized && __builtin_isfinite(__v))
            {
              __wstr = _M_localize(__str, __expc, __fc.locale());
              if (!__wstr.empty())
index 4499397aaf9744f5c9c40620e08bbfadc707a629..78cc1ab482ad7c2c3c74c0ae4ce81a12852e9062 100644 (file)
@@ -248,6 +248,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);
 }