public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix std::format for localized floats [PR110968]
Date: Thu, 10 Aug 2023 23:33:45 +0100	[thread overview]
Message-ID: <20230810223353.1242664-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 to follow.

-- >8 --

The __formatter_fp::_M_localize function just returns an empty string if
the formatting locale is the C locale, as there is nothing to do. But
the caller was assuming that the returned string contains the localized
string. The caller should use the original string if _M_localize returns
an empty string.

libstdc++-v3/ChangeLog:

	PR libstdc++/110968
	* include/std/format (__formatter_fp::format): Check return
	value of _M_localize.
	* testsuite/std/format/functions/format.cc: Check classic
	locale.
---
 libstdc++-v3/include/std/format                       | 5 +++--
 libstdc++-v3/testsuite/std/format/functions/format.cc | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 96eb4cd742e..5d7af53fc94 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1610,8 +1610,8 @@ namespace __format
 
 
 	  _Optional_locale __loc;
-	  basic_string_view<_CharT> __str;
 	  basic_string<_CharT> __wstr;
+	  basic_string_view<_CharT> __str;
 	  if constexpr (is_same_v<_CharT, char>)
 	    __str = __narrow_str;
 	  else
@@ -1634,7 +1634,8 @@ namespace __format
 		__wstr = _M_localize(__str, __expc, __fc.locale());
 	      else
 		__wstr = _M_localize(__str, __expc, __loc.value());
-	      __str = __wstr;
+	      if (!__wstr.empty())
+		__str = __wstr;
 	    }
 
 	  size_t __width = _M_spec._M_get_width(__fc);
diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc
index bd914df6d7c..471cffb2b36 100644
--- a/libstdc++-v3/testsuite/std/format/functions/format.cc
+++ b/libstdc++-v3/testsuite/std/format/functions/format.cc
@@ -197,6 +197,9 @@ test_locale()
   s = std::format(eloc, "{0:#Lg} {0:+#.3Lg} {0:#08.4Lg}", -1234.);
   VERIFY( s == "-1.234,00 -1,23e+03 -01.234," );
 
+  s = std::format(cloc, "{:05L}", -1.0); // PR libstdc++/110968
+  VERIFY( s == "-0001" );
+
   // Restore
   std::locale::global(cloc);
 }
-- 
2.41.0


                 reply	other threads:[~2023-08-10 22:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230810223353.1242664-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).