From patchwork Mon Aug 7 21:14:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 132391 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1716277vqr; Mon, 7 Aug 2023 14:18:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGrV7MmTzGXqpMiVSXUwNOnREKmFlO7FPI0dGZL5x6b1t9cyGWE7utEYP+SspXsLW/Ae6AA X-Received: by 2002:aa7:ce04:0:b0:523:df1:ba1b with SMTP id d4-20020aa7ce04000000b005230df1ba1bmr8335343edv.37.1691443097028; Mon, 07 Aug 2023 14:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691443097; cv=none; d=google.com; s=arc-20160816; b=cBx2BLI5eh/o21B+X/QGwrbK5TMEp4ILp9VbAbaca8EooScTFsrQHmgmeEcQguf4/q wgla8Yh/h2oEircjLCSrl3IeD5Sj50+OMr/arsG6wat+/xDzkBJj6GIPeLcieXhMdSVc V51GSvCwU92sE1gw7+OJRfAUEQ/rxtOo1bEtDtS9gtPGfKVwLcdWZWDFOHWp/R2bd1uM wXKXzlJKsluhtk5pJkcBnKiQKsqmHCaLkBVyb3MDEc9UXUaBtoJKy3F4uxxZH6ZmvqjW FaS+F0QBte4vh1SFfke6xrCQLu9D6JxeRPVxIPqaOy8BCgM1bCvT5HArQ+y0JKOHV+HN OPeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hccDSP+WebWzkrK2UbN4Yk8eguO4T8MXC9za3CnwSMU=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=iVR3SAyxUd/xUPtnzPWCfbEwYs/OLNRNA/6dAT42Z/9uNCsQoz9d2MHoRS70V1MbFF 5+yYAGwbejtzcL7GeaREcs9i2MwtdeHLQpJIUe4zeUeSC81To21QqmpUDWqM9Hasyl3T f6mP+3dPidsFNGUm0W/D2We6CFpAu2jVKzcvS2U3qZphVUgdh+wxzc4XJbbjrGyGgiZ4 lP/m0air6wUq40TyGfYjZjzF8lKSmamiQlMjouTiYsBMkABqaeQtRWgBlpEtgJ5vvQfW VPgIdnfJBMs77UdEM5J3GICJFM7ZaXoJfgqsHUy8dRakHTRYlz+9HZvQzMRydRmO1JfR 0K9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FT20Xf2X; 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=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ay8-20020a056402202800b0052257d9655bsi5908703edb.304.2023.08.07.14.18.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 14:18:17 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=FT20Xf2X; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5F573857C55 for ; Mon, 7 Aug 2023 21:16:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5F573857C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691442973; bh=hccDSP+WebWzkrK2UbN4Yk8eguO4T8MXC9za3CnwSMU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=FT20Xf2XtFWPr3TkjlqfmfmTchYKsAJD53E2vTq+JFjmmO8mrbk4JpeLupRcR2Zoa SuPOlM/ZXV5n5pZeJzso14eRaMjiIA9Hlen2+HmW9qIfHhzzkQlmiejnxh+mo5/5T3 k56ftpVxZJoLuiwZs+XvMCXx+uQV/m5wRZ8703mA= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id EFC163857838 for ; Mon, 7 Aug 2023 21:14:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EFC163857838 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-468-2DdmvXU5PJ6spnh9wSZKLQ-1; Mon, 07 Aug 2023 17:14:22 -0400 X-MC-Unique: 2DdmvXU5PJ6spnh9wSZKLQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4D33685C710; Mon, 7 Aug 2023 21:14:22 +0000 (UTC) Received: from localhost (unknown [10.42.28.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 14535492C13; Mon, 7 Aug 2023 21:14:21 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Constrain __format::_Iter_sink for contiguous iterators [PR110917] Date: Mon, 7 Aug 2023 22:14:01 +0100 Message-ID: <20230807211421.701784-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773606636595558095 X-GMAIL-MSGID: 1773606636595558095 Tested x86_64-linux. Pushed to trunk. gcc-13 backport to follow. -- >8 -- We can't write to a span<_CharT> if the contiguous iterator has a value type that isn't _CharT. libstdc++-v3/ChangeLog: PR libstdc++/110917 * include/std/format (__format::_Iter_sink): Constrain partial specialization for contiguous iterators to require the value type to be CharT. * testsuite/std/format/functions/format_to.cc: New test. --- libstdc++-v3/include/std/format | 1 + .../std/format/functions/format_to.cc | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/format/functions/format_to.cc diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 0debbae5ba8..60e53642b11 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -2536,6 +2536,7 @@ namespace __format // not introduce any invalid pointer arithmetic or overflows that would not // have happened anyway. template + requires same_as, _CharT> class _Iter_sink<_CharT, _OutIter> : public _Sink<_CharT> { using uint64_t = __UINTPTR_TYPE__; diff --git a/libstdc++-v3/testsuite/std/format/functions/format_to.cc b/libstdc++-v3/testsuite/std/format/functions/format_to.cc new file mode 100644 index 00000000000..a35568954e2 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/functions/format_to.cc @@ -0,0 +1,100 @@ +// { dg-options "-std=gnu++20" } +// { dg-do run { target c++20 } } + +#include +#include +#include +#include +#include + +struct punct : std::numpunct +{ + std::string do_grouping() const override { return "\2"; } +}; + +void +test() +{ + char buf[32] = { }; + auto out = std::format_to(buf, "test"); + VERIFY( out == buf+4 ); + + std::locale loc({}, new punct); + auto out2 = std::format_to(buf, loc, "{:Ld}", 12345); + VERIFY( out2 == buf+7 ); + VERIFY( std::string_view(buf, out2 - buf) == "1,23,45" ); +} + +struct wpunct : std::numpunct +{ + std::string do_grouping() const override { return "\2"; } +}; + +void +test_wchar() +{ + wchar_t buf[32] = { }; + auto out = std::format_to(buf, L"123 + 456 = {}", 579); + VERIFY( out == buf+15 ); + + std::locale loc({}, new wpunct); + auto out2 = std::format_to(buf, loc, L"{:Ld}", 12345); + VERIFY( out2 == buf+7 ); + VERIFY( std::wstring_view(buf, out2 - buf) == L"1,23,45" ); +} + +template +struct move_only_iterator +{ + using iterator = I; + using value_type = iterator::value_type; + using difference_type = iterator::difference_type; + using iterator_category = std::output_iterator_tag; + + move_only_iterator(iterator b) : base_(b) { } + move_only_iterator(move_only_iterator&&) = default; + move_only_iterator& operator=(move_only_iterator&&) = default; + + move_only_iterator& operator++() { ++base_; return *this; } + move_only_iterator operator++(int) { auto tmp = *this; ++base_; return tmp; } + + decltype(auto) operator*() { return *base_; } + +private: + iterator base_; +}; + +void +test_move_only() +{ + std::string str; + move_only_iterator mo(std::back_inserter(str)); + auto res = std::format_to(std::move(mo), "for{:.3} that{:c}", + "matte", (int)'!'); + VERIFY( str == "format that!" ); + + std::vector vec; + move_only_iterator wmo(std::back_inserter(vec)); + auto wres = std::format_to(std::move(wmo), L"for{:.3} hat{:c}", + L"matte", (long)L'!'); + VERIFY( std::wstring_view(vec.data(), vec.size()) == L"format hat!" ); +} + +void +test_pr110917() +{ + // PR libstdc++/110917 + // std::format_to(int*, ...) fails to compile because of _S_make_span + unsigned char buf[7]; + auto res = std::format_to(buf, "{} {}", "abc", 123); + VERIFY( res == buf + 7 ); + VERIFY( ! std::memcmp(buf, "abc 123", 7) ); +} + +int main() +{ + test(); + test_wchar(); + test_move_only(); + test_pr110917(); +}