From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 2C3EC385141F; Wed, 9 Sep 2020 20:12:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2C3EC385141F Received: by mail-wm1-x32e.google.com with SMTP id w2so3499544wmi.1; Wed, 09 Sep 2020 13:12:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=w8Z7H9kkSU8NZjZwxVQIlAxee8hSf6JXr2CZlKUh5AI=; b=S+70+XptLMucpStSxfOmDjvLYCjmrPmdoVyTf4yUolizPmubfrAQR0W8FyWq0ms/+p H34BY9gVydn18KLy3a6exmKwrm9bt3Fu6y67X+RfLJQUsOts41wB9KxCURVrze3J+5jF kQvX8n6JgtqoQSuoHLA+/lHtlF413ZrZcTZ5bA8jDntpvThWDALrAcY2/2hV1RdjJBlv P+pmxn91wYzO/ixpc31X9cibjF0w+61ulFtwPoFOJzPLUdgFKYnD3iJrohR08S6hCnVh icOadshXthowzHkDmxS0vN4XeczYAr1Dq2szX1xBTb57YxKpGCNdeFC+rFSfqIUpeTsV 2JRw== X-Gm-Message-State: AOAM532nyEjYipAyY4eIRKLidp3pCcvt38p2bQKYalvEObHUDqAxBO7E SwkKAteY69I+kKJNbwOP9sKmvuxzIdeMvg== X-Google-Smtp-Source: ABdhPJx8NuEuW+WQrmefPbvZSx8CafcxkyW9WYS1wXWKaDHFtwlPevHJxkTVs7BIrPpvXZX4rMkucQ== X-Received: by 2002:a7b:c14d:: with SMTP id z13mr4986164wmi.19.1599682350898; Wed, 09 Sep 2020 13:12:30 -0700 (PDT) Received: from ?IPv6:2a01:e0a:1dc:b1c0:4c0f:5fef:2a38:cc19? ([2a01:e0a:1dc:b1c0:4c0f:5fef:2a38:cc19]) by smtp.googlemail.com with ESMTPSA id f1sm5707778wrt.20.2020.09.09.13.12.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Sep 2020 13:12:30 -0700 (PDT) From: =?UTF-8?Q?Fran=c3=a7ois_Dumont?= Subject: [PATH 3/3] libstdc++: Add std::advance ostreambuf_iterator overload To: "libstdc++@gcc.gnu.org" , gcc-patches Message-ID: <8bbbdd58-56bf-4022-0d40-d4e92afb3795@gmail.com> Date: Wed, 9 Sep 2020 22:12:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------008B6356B2643731C8526C32" Content-Language: fr X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Wed, 09 Sep 2020 20:12:34 -0000 This is a multi-part message in MIME format. --------------008B6356B2643731C8526C32 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit libstdc++: Add std::advance overload for ostreambuf_iterator Implement std::advance overload for ostreambuf_iterator using basic_streambuf pubseekof. libstdc++-v3/ChangeLog:         * include/bits/streambuf_iterator.h (ostreambuf_iterator): Add         std::advance friend declaration.         (advance(ostreambuf_iterator<>&, _Distance)): New.         * testsuite/25_algorithms/advance/ostreambuf_iterator/char/1.cc:         New test.         * testsuite/25_algorithms/advance/ostreambuf_iterator/char/1_neg.cc:         New test.         * testsuite/25_algorithms/advance/ostreambuf_iterator/char/2.cc:         New test.         * testsuite/25_algorithms/advance/ostreambuf_iterator/char/2_neg.cc:         New test. Tested under Linux x85_64. Ok to commit ? François --------------008B6356B2643731C8526C32 Content-Type: text/x-patch; charset=UTF-8; name="advance_ostreambuf_iterator.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="advance_ostreambuf_iterator.patch" diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h index afe967e5f03..1f6613e9ef6 100644 --- a/libstdc++-v3/include/bits/streambuf_iterator.h +++ b/libstdc++-v3/include/bits/streambuf_iterator.h @@ -257,6 +257,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, ostreambuf_iterator<_CharT2>); + template + friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, + void>::__type + advance(ostreambuf_iterator<_CharT2>&, _Distance); + private: streambuf_type* _M_sbuf; bool _M_failed; @@ -405,7 +410,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, - istreambuf_iterator<_CharT> >::__type + istreambuf_iterator<_CharT> >::__type find(istreambuf_iterator<_CharT> __first, istreambuf_iterator<_CharT> __last, const _CharT& __val) { @@ -475,6 +480,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } + template + typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, + void>::__type + advance(ostreambuf_iterator<_CharT>& __i, _Distance __n) + { + if (__n == 0) + return; + + __glibcxx_assert(__n > 0); + __glibcxx_requires_cond(!__i.failed(), + _M_message(__gnu_debug::__msg_advance_oob) + ._M_iterator(__i) + ._M_integer(__n)); + + typedef basic_streambuf<_CharT> __streambuf_t; + typedef typename __streambuf_t::pos_type __pos_t; + __pos_t __cur_pos + = __i._M_sbuf->pubseekoff(0, ios_base::cur, ios_base::out); + __pos_t __new_pos = + __i._M_sbuf->pubseekoff(__n, ios_base::cur, ios_base::out); + + if (__new_pos - __cur_pos != __n) + { + __i._M_failed = true; + __glibcxx_requires_cond(!__i.failed(), + _M_message(__gnu_debug::__msg_advance_oob) + ._M_iterator(__i) + ._M_integer(__n)); + } + } + // @} group iterators _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1.cc b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1.cc new file mode 100644 index 00000000000..dd70cc67c75 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1.cc @@ -0,0 +1,55 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +#include + +void test01() +{ + using namespace std; + + const char data1[] = "Drei Phantasien nach Friedrich Holderlin"; + string str1(data1); + str1[17] = 'i'; + + ostringstream oss1(str1); + ostreambuf_iterator beg1(oss1); + + std::advance(beg1, 17); + *beg1 = 'a'; + + VERIFY( !beg1.failed() ); + VERIFY( oss1.str() == data1 ); + str1 = oss1.str(); + + // -1 for the trailing '\0' + // -1 for the beg1 assignment. + std::advance(beg1, sizeof(data1) - 17 - 1 - 1); + *beg1 = '.'; + + str1 += '.'; + VERIFY( oss1.str() == str1 ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1_neg.cc b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1_neg.cc new file mode 100644 index 00000000000..8d266256ed3 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/1_neg.cc @@ -0,0 +1,40 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include +#include + +void test01() +{ + using namespace std; + + const char data1[] = "Drei Phantasien nach Friedrich Holderlin"; + ostringstream oss1(data1); + ostreambuf_iterator beg1(oss1); + + advance(beg1, -1); // Error. +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2.cc b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2.cc new file mode 100644 index 00000000000..147ff24b728 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2.cc @@ -0,0 +1,47 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// Debug mode would detect the invalid std::advance call. +// { dg-require-normal-mode "" } + +#include +#include +#include + +#include + +void test01() +{ + using namespace std; + + const char data1[] = "Drei Phantasien nach Friedrich Holderlin"; + + ostringstream oss1(data1); + ostreambuf_iterator beg1(oss1); + + VERIFY( !beg1.failed() ); + + std::advance(beg1, sizeof(data1)); + + VERIFY( beg1.failed() ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2_neg.cc b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2_neg.cc new file mode 100644 index 00000000000..ffb2238354a --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/advance/ostreambuf_iterator/char/2_neg.cc @@ -0,0 +1,40 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include +#include + +void test01() +{ + using namespace std; + + const char data1[] = "Drei Phantasien nach Friedrich Holderlin"; + ostringstream oss1(data1); + ostreambuf_iterator beg1(oss1); + + advance(beg1, sizeof(data1)); // Error. +} + +int main() +{ + test01(); + return 0; +} --------------008B6356B2643731C8526C32--