From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7984B3858005 for ; Thu, 10 Aug 2023 22:33:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7984B3858005 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691706837; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3MYLsjQhNtJqWgBmH26MKUXK9kaaG0XN9QYSTOFL8Oo=; b=TJlvZROmtAIkuiNGq+Uz7qnhbvacTB0yA78t1evel1vdCUACtu196AhHP0f3QZ4ow6dg25 fqp55iXlfJljquS5uK2MYG36FGpUfF1AuPRJRvQXczdyHTXjtCS76zlD3wxms6oxv91M60 6U/4rAJ2R6mOm8TTAr56V51GLmAq3jo= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-608-4weNmYW8P8m9bm09_reLRw-1; Thu, 10 Aug 2023 18:33:54 -0400 X-MC-Unique: 4weNmYW8P8m9bm09_reLRw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D17A62A59562; Thu, 10 Aug 2023 22:33:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.188]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9554C1121314; Thu, 10 Aug 2023 22:33:53 +0000 (UTC) From: Jonathan Wakely 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 Message-ID: <20230810223353.1242664-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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