public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7698] libstdc++: Constrain __format::_Iter_sink for contiguous iterators [PR110917] Date: Tue, 8 Aug 2023 16:13:24 +0000 (GMT) [thread overview] Message-ID: <20230808161324.277A43857729@sourceware.org> (raw) https://gcc.gnu.org/g:0f0152a93d15b24ebc7f6c7455baaded6a63fb2e commit r13-7698-g0f0152a93d15b24ebc7f6c7455baaded6a63fb2e Author: Jonathan Wakely <jwakely@redhat.com> Date: Mon Aug 7 14:37:25 2023 +0100 libstdc++: Constrain __format::_Iter_sink for contiguous iterators [PR110917] 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<CharT, OutIter>): Constrain partial specialization for contiguous iterators to require the value type to be CharT. * testsuite/std/format/functions/format_to.cc: New test. (cherry picked from commit c5ea5aecac323e9094e4dc967f54090cb244bc6a) Diff: --- libstdc++-v3/include/std/format | 1 + .../testsuite/std/format/functions/format_to.cc | 100 +++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index ebf8bbaa15f..e624b9fe353 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -2535,6 +2535,7 @@ namespace __format // not introduce any invalid pointer arithmetic or overflows that would not // have happened anyway. template<typename _CharT, contiguous_iterator _OutIter> + requires same_as<iter_value_t<_OutIter>, _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 <format> +#include <locale> +#include <vector> +#include <cstring> +#include <testsuite_hooks.h> + +struct punct : std::numpunct<char> +{ + 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<wchar_t> +{ + 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<typename I> +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<wchar_t> 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(); +}
reply other threads:[~2023-08-08 16:13 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=20230808161324.277A43857729@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).