From patchwork Tue Dec 12 22:46:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 177716 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8088511vqy; Tue, 12 Dec 2023 16:23:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCVwjZExYbgrD28udAwEoDceDXXYwk0AWl2TkRKpTWvAjIWdVEsHk5i46kwL696uAGUn/O X-Received: by 2002:a05:6122:200a:b0:4b2:c554:e9b8 with SMTP id l10-20020a056122200a00b004b2c554e9b8mr6507187vkd.26.1702426994330; Tue, 12 Dec 2023 16:23:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702426994; cv=pass; d=google.com; s=arc-20160816; b=e8eA5h3yLdeqt8Nt1K0ZRGqcG91QCteB/FJtlXpkEL5uteYnb1Rp+RsAtfDQIV3QUs ay5vFBqmfjDX8wqMQKddRXQ+cffRCVoCz2JA4gHwHo0Ex1XzzF8mb8Kfpnzwg6Fm4z2q FYxBmsHeYGlqX89bx8SIppBEb4TvmTGoOGl0o07aU/4ondv/SyVkOvDgKin2lefVmQnP KgCN5BpaoV7L7FLOE7iYU2FZb/TbzOJrcXqGRrZpr661rKfVdobXd/f/jngr++6IAv8p wkTS/VwzKTJQ0WxsjXeNvc5OgydlqM5lNB5osQfguADuBKarWoQdWE+aoMSvJPS/4tWd l4Kg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Tnkple+k9SsuN8t0394HjNeENEAzVi0F5Tpef216MHg=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=1KL4+EkXWTUUOLSRDa0erGB+/Lpvjy8BnV4g0B+2sB+LlYD0+j9qxOf6iBINsRSR3k LWPZ2rOiqwjMe8FClc1MMjOyRpIcMo4uXNSSBtqLVybOvCiye2en7z5766xnHVjJPZwX ZkUPSKFqOH7zxqUxx/+Y0mL5O9xRfirz+dCmi0l8rjy38+0GZDcFae99A5l6jnaVG8/u Y8HBk/rtt1Be/JMC16ZlGonsztXT8nLcQUWvJ71IppGPD8tl62uRMKgGEothoC4hdLb3 YeilvrWFZw4LDKsaWZUMPDn4QuCCa1MGBbBrplkyshSprM8aw4dCLGCCuVZjKXreg1r6 3vNA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IzOryyOA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w14-20020a05620a424e00b0076da9a05a45si12146419qko.134.2023.12.12.16.23.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:23:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IzOryyOA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 973553861822 for ; Tue, 12 Dec 2023 22:47:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6F684385AE4E for ; Tue, 12 Dec 2023 22:47:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F684385AE4E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6F684385AE4E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702421226; cv=none; b=LjpcsaLuGKgN0HRCYz2YXX2xa4wBU/KHKrjNvuhAjrB8H+xQlP5s+JsS2iiJL5S3MEgKU6panvFnF8Dk+dkZRrCwhKTuTzmpweQAlDs9jj/HGKjM+rg3yeN/cypUZiH/2kj18dGAeIGAPJPkhSOou/vvSjbsPQDp59FasBtpVhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702421226; c=relaxed/simple; bh=13bpjIZgyboL1bWP6c9Y9hNf0c6DXMpyuBLdODPs9ck=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WISdgjPLHDI3iilwn6TD3abDeJbNC+jJqWubBuMGxGCw23HD9bgjgWzW8VChaatR6If5Il9Wzbce0OG9aoQntaY6uOjzCm2cf6jH+bn45B4PDxIWXEtSyok0RP76IxgF8Z5h1MA71oYwRqkPHmHVhsXzi+S6LLYFmpMGS9IeXDM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702421225; 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=Tnkple+k9SsuN8t0394HjNeENEAzVi0F5Tpef216MHg=; b=IzOryyOAyLc7BX7lehLr1XujWXrL3iHNyhItE2WQUW5YK8Zl1kYrSMfNDxjLf+xpCjocPq BkmlFIO2WiUS8MYEr1CqvwcienucyE2s3UiKxBW3xbFZJ2f3skExP2AlRRc3pCw0ViJEyB jfCpHtd+yfMPzAht9AB8GFFzwmch+XA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-599-pBtUU9eiNMe_1FghoUIgDw-1; Tue, 12 Dec 2023 17:47:03 -0500 X-MC-Unique: pBtUU9eiNMe_1FghoUIgDw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6C6921C294A1; Tue, 12 Dec 2023 22:47:03 +0000 (UTC) Received: from localhost (unknown [10.42.28.145]) by smtp.corp.redhat.com (Postfix) with ESMTP id 218CE1121306; Tue, 12 Dec 2023 22:47:02 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::format("{}", 'c') Date: Tue, 12 Dec 2023 22:46:55 +0000 Message-ID: <20231212224702.1518352-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785124088296145793 X-GMAIL-MSGID: 1785124088296145793 Tested x86_64-linux. Pushed to trunk. -- >8-- When I added a fast path for std::format("{}", x) in r14-5587-g41a5ea4cab2c59 I forgot to handle char separately from other integral types. That caused std::format("{}", 'c') to return "99" instead of "c". libstdc++-v3/ChangeLog: * include/std/format (__do_vformat_to): Handle char separately from other integral types. * testsuite/std/format/functions/format.cc: Check for expected output for char and bool arguments. * testsuite/std/format/string.cc: Check that 0 filling is rejected for character and string formats. --- libstdc++-v3/include/std/format | 9 +++ .../testsuite/std/format/functions/format.cc | 56 +++++++++++++++++++ libstdc++-v3/testsuite/std/format/string.cc | 3 + 3 files changed, 68 insertions(+) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 04d03e0ceb7..1f8cd5c06be 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3968,6 +3968,15 @@ namespace __format __done = true; } } + else if constexpr (is_same_v<_Tp, char>) + { + if (auto __res = __sink_out._M_reserve(1)) + { + *__res.get() = __arg; + __res._M_bump(1); + __done = true; + } + } else if constexpr (is_integral_v<_Tp>) { make_unsigned_t<_Tp> __uval; diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index 9328dec8875..b3b4f0647bc 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -256,12 +256,42 @@ test_width() } } +void +test_char() +{ + std::string s; + + s = std::format("{}", 'a'); + VERIFY( s == "a" ); + + s = std::format("{:c} {:d} {:o}", 'b', '\x17', '\x3f'); + VERIFY( s == "b 23 77" ); + + s = std::format("{:#d} {:#o}", '\x17', '\x3f'); + VERIFY( s == "23 077" ); + + s = std::format("{:04d} {:04o}", '\x17', '\x3f'); + VERIFY( s == "0023 0077" ); + + s = std::format("{:b} {:B} {:#b} {:#B}", '\xff', '\xa0', '\x17', '\x3f'); + if constexpr (std::is_unsigned_v) + VERIFY( s == "11111111 10100000 0b10111 0B111111" ); + else + VERIFY( s == "-1 -1100000 0b10111 0B111111" ); + + s = std::format("{:x} {:#x} {:#X}", '\x12', '\x34', '\x45'); + VERIFY( s == "12 0x34 0X45" ); +} + void test_wchar() { using namespace std::literals; std::wstring s; + s = std::format(L"{}", L'a'); + VERIFY( s == L"a" ); + s = std::format(L"{} {} {} {} {} {}", L'0', 1, 2LL, 3.4, L"five", L"six"s); VERIFY( s == L"0 1 2 3.4 five six" ); @@ -353,6 +383,9 @@ test_pointer() const void* pc = p; std::string s, str_int; + s = std::format("{}", p); + VERIFY( s == "0x0" ); + s = std::format("{} {} {}", p, pc, nullptr); VERIFY( s == "0x0 0x0 0x0" ); s = std::format("{:p} {:p} {:p}", p, pc, nullptr); @@ -385,6 +418,27 @@ test_pointer() #endif } +void +test_bool() +{ + std::string s; + + s = std::format("{}", true); + VERIFY( s == "true" ); + s = std::format("{:} {:s}", true, false); + VERIFY( s == "true false" ); + s = std::format("{:b} {:#b}", true, false); + VERIFY( s == "1 0b0" ); + s = std::format("{:B} {:#B}", false, true); + VERIFY( s == "0 0B1" ); + s = std::format("{:d} {:#d}", false, true); + VERIFY( s == "0 1" ); + s = std::format("{:o} {:#o} {:#o}", false, true, false); + VERIFY( s == "0 01 0" ); + s = std::format("{:x} {:#x} {:#X}", false, true, false); + VERIFY( s == "0 0x1 0X0" ); +} + int main() { test_no_args(); @@ -393,8 +447,10 @@ int main() test_alternate_forms(); test_locale(); test_width(); + test_char(); test_wchar(); test_minmax(); test_p1652r1(); test_pointer(); + test_bool(); } diff --git a/libstdc++-v3/testsuite/std/format/string.cc b/libstdc++-v3/testsuite/std/format/string.cc index 5d338644c62..40aaebae04e 100644 --- a/libstdc++-v3/testsuite/std/format/string.cc +++ b/libstdc++-v3/testsuite/std/format/string.cc @@ -109,6 +109,9 @@ test_format_spec() VERIFY( ! is_format_string_for("{:#?}", "str") ); VERIFY( ! is_format_string_for("{:#?}", 'c') ); + VERIFY( ! is_format_string_for("{:0c}", 'c') ); + VERIFY( ! is_format_string_for("{:0s}", true) ); + // Precision only valid for string and floating-point types. VERIFY( ! is_format_string_for("{:.3d}", 1) ); VERIFY( ! is_format_string_for("{:3.3d}", 1) );