From: "François Dumont" <frs.dumont@gmail.com>
To: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>,
gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Add std::copy_n overload for istreambuf_iterator
Date: Fri, 19 Jul 2019 21:38:00 -0000 [thread overview]
Message-ID: <fa336095-577a-e5f6-f47a-99713760541b@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 813 bytes --]
It sounds reasonable to overload std::copy_n for istreambuf_iterator.
   * include/bits/stl_algo.h (copy_n(istreambuf_iterator<>, _Size,
_OIte)):
   New declaration.
   * include/bits/streambuf_iterator.h (istreambuf_iterator<>): Declare
   std::copy_n for istreambuf_iterator of char types to be friend.
   (std::copy_n(istreambuf_iterator<>, _Size, _OIte)): New overload.
   * include/std/streambuf(basic_streambuf<>): Declare std::copy_n for
   istreambuf_iterator of char types to be friend.
   * testsuite/25_algorithms/copy_n/istreambuf_iterator.cc: New.
   * testsuite/25_algorithms/copy_n/istreambuf_iterator_neg.cc: New.
Tested under Linux x86_64, normal and debug modes.
Ok to commit ?
François
[-- Attachment #2: istreambuf_copy_n.patch --]
[-- Type: text/x-patch, Size: 7290 bytes --]
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h
index 478f012def8..ec651e2cc45 100644
--- a/libstdc++-v3/include/bits/stl_algo.h
+++ b/libstdc++-v3/include/bits/stl_algo.h
@@ -771,6 +771,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_OutputIterator __result, random_access_iterator_tag)
{ return std::copy(__first, __first + __n, __result); }
+ template<typename _CharT, typename _Size, typename _OutputIterator>
+ typename enable_if<__is_char<_CharT>::__value,
+ _OutputIterator>::type
+ copy_n(istreambuf_iterator<_CharT, char_traits<_CharT> >,
+ _Size __n, _OutputIterator __result);
+
/**
* @brief Copies the range [first,first+n) into [result,result+n).
* @ingroup mutating_algorithms
diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h
index 2f4ff494a3a..c682fa91bde 100644
--- a/libstdc++-v3/include/bits/streambuf_iterator.h
+++ b/libstdc++-v3/include/bits/streambuf_iterator.h
@@ -80,6 +80,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__copy_move_a2(istreambuf_iterator<_CharT2>,
istreambuf_iterator<_CharT2>, _CharT2*);
+#if __cplusplus >= 201103L
+ template<typename _CharT2, typename _Size, typename _OutputIterator>
+ friend typename enable_if<__is_char<_CharT2>::__value,
+ _OutputIterator>::type
+ copy_n(istreambuf_iterator<_CharT2>, _Size, _OutputIterator);
+#endif
+
template<typename _CharT2>
friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
istreambuf_iterator<_CharT2> >::__type
@@ -367,6 +374,50 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return __result;
}
+#if __cplusplus >= 201103L
+ template<typename _CharT, typename _Size, typename _OutputIterator>
+ typename enable_if<__is_char<_CharT>::__value, _OutputIterator>::type
+ copy_n(istreambuf_iterator<_CharT> __it, _Size __n,
+ _OutputIterator __result)
+ {
+ if (__n == 0)
+ return __result;
+
+ __glibcxx_assert(__n > 0);
+ __glibcxx_requires_cond(!__it._M_at_eof(),
+ _M_message(__gnu_debug::__msg_inc_istreambuf)
+ ._M_iterator(__it));
+
+ using traits_type = typename istreambuf_iterator<_CharT>::traits_type;
+ const auto __eof = traits_type::eof();
+
+ auto __sb = __it._M_sbuf;
+ while (__n > 0)
+ {
+ streamsize __size = __sb->egptr() - __sb->gptr();
+ if (__size == 0)
+ {
+ if (traits_type::eq_int_type(__sb->underflow(), __eof))
+ {
+ __glibcxx_requires_cond(__n == 0,
+ _M_message(__gnu_debug::__msg_inc_istreambuf)
+ ._M_iterator(__it));
+ break;
+ }
+
+ __size = __sb->egptr() - __sb->gptr();
+ }
+
+ streamsize __xsize = std::min<streamsize>(__size, __n);
+ __result = std::copy(__sb->gptr(), __sb->gptr() + __xsize, __result);
+ __sb->__safe_gbump(__xsize);
+ __n -= __xsize;
+ }
+
+ return __result;
+ }
+#endif
+
template<typename _CharT>
typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
istreambuf_iterator<_CharT> >::__type
diff --git a/libstdc++-v3/include/std/streambuf b/libstdc++-v3/include/std/streambuf
index d9ca981d704..4f62ebf4d95 100644
--- a/libstdc++-v3/include/std/streambuf
+++ b/libstdc++-v3/include/std/streambuf
@@ -155,6 +155,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__copy_move_a2(istreambuf_iterator<_CharT2>,
istreambuf_iterator<_CharT2>, _CharT2*);
+#if __cplusplus >= 201103L
+ template<typename _CharT2, typename _Size, typename _OutputIterator>
+ friend typename enable_if<__is_char<_CharT2>::__value,
+ _OutputIterator>::type
+ copy_n(istreambuf_iterator<_CharT2>, _Size, _OutputIterator);
+#endif
+
template<typename _CharT2>
friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
istreambuf_iterator<_CharT2> >::__type
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator.cc
new file mode 100644
index 00000000000..ebd769cf7c0
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator.cc
@@ -0,0 +1,59 @@
+// Copyright (C) 2019 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <algorithm>
+#include <sstream>
+#include <iterator>
+
+#include <testsuite_hooks.h>
+
+void test01()
+{
+ std::stringstream ss("12345");
+
+ std::string ostr(5, '0');
+ typedef std::istreambuf_iterator<char> istrb_ite;
+ std::copy_n(istrb_ite(ss), 0, ostr.begin());
+ VERIFY( ostr.front() == '0' );
+
+ std::copy_n(istrb_ite(ss), 2, ostr.begin());
+ VERIFY( ostr == "12000" );
+
+ std::copy_n(istrb_ite(ss), 3, ostr.begin() + 2);
+ VERIFY( ostr == "12345" );
+}
+
+void test02()
+{
+ std::stringstream ss("12345");
+
+ std::string ostr(5, '0');
+ typedef std::istreambuf_iterator<char> istrb_ite;
+
+ istrb_ite ibfit(ss);
+ std::copy_n(ibfit, 3, std::copy_n(ibfit, 2, ostr.begin()));
+ VERIFY( ostr == "12345" );
+}
+
+int main()
+{
+ test01();
+ test02();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator_neg.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator_neg.cc
new file mode 100644
index 00000000000..42bc5b64306
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator_neg.cc
@@ -0,0 +1,40 @@
+// Copyright (C) 2019 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <sstream>
+#include <iterator>
+
+#include <testsuite_hooks.h>
+
+void test01()
+{
+ std::stringstream ss("12345");
+
+ std::string ostr(10, '0');
+ typedef std::istreambuf_iterator<char> istrb_ite;
+ std::copy_n(istrb_ite(ss), 10, ostr.begin());
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
next reply other threads:[~2019-07-19 21:38 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-19 21:38 François Dumont [this message]
2019-09-27 11:01 ` Jonathan Wakely
2019-10-04 5:01 ` François Dumont
2019-10-04 11:06 ` Jonathan Wakely
2019-10-09 20:18 ` Christophe Lyon
2019-10-09 20:29 ` François Dumont
2019-10-10 19:57 ` François Dumont
2019-10-14 13:06 ` Christophe Lyon
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=fa336095-577a-e5f6-f47a-99713760541b@gmail.com \
--to=frs.dumont@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@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: link
Be 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).