Add std::copy_n istreambuf_iterator specialization
authorFrançois Dumont <fdumont@gcc.gnu.org>
Sun, 6 Oct 2019 15:29:04 +0000 (15:29 +0000)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Sun, 6 Oct 2019 15:29:04 +0000 (15:29 +0000)
* include/bits/stl_algo.h
(__copy_n_a(_IIte, _Size, _OIte)): New.
(__copy_n_a(istreambuf_iterator<>, _Size, _CharT*)): New declaration.
(__copy_n(_IIte, _Size, _OIte, input_iterator_tag)): Adapt to use
latter.
* include/bits/streambuf_iterator.h (istreambuf_iterator<>): Declare
std::__copy_n_a friend.
(__copy_n_a(istreambuf_iterator<>, _Size, _CharT*)): New.
* testsuite/25_algorithms/copy_n/istreambuf_iterator/1.cc: New.
* testsuite/25_algorithms/copy_n/istreambuf_iterator/1_neg.cc: New.
* testsuite/25_algorithms/copy_n/istreambuf_iterator/2_neg.cc: New.

From-SVN: r276638

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_algo.h
libstdc++-v3/include/bits/streambuf_iterator.h
libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/2_neg.cc [new file with mode: 0644]

index 40d9081a3c80810ffae72db2514ac8f4dfaaac31..e0d3fd5f3a9e8462d404e3389da0b6ef10ec40b2 100644 (file)
@@ -1,5 +1,17 @@
 2019-10-06  François Dumont  <fdumont@gcc.gnu.org>
 
+       * include/bits/stl_algo.h
+       (__copy_n_a(_IIte, _Size, _OIte)): New.
+       (__copy_n_a(istreambuf_iterator<>, _Size, _CharT*)): New declaration.
+       (__copy_n(_IIte, _Size, _OIte, input_iterator_tag)): Adapt to use
+       latter.
+       * include/bits/streambuf_iterator.h (istreambuf_iterator<>): Declare
+       std::__copy_n_a friend.
+       (__copy_n_a(istreambuf_iterator<>, _Size, _CharT*)): New.
+       * testsuite/25_algorithms/copy_n/istreambuf_iterator/1.cc: New.
+       * testsuite/25_algorithms/copy_n/istreambuf_iterator/1_neg.cc: New.
+       * testsuite/25_algorithms/copy_n/istreambuf_iterator/2_neg.cc: New.
+
        * include/bits/stl_iterator_base_types.h (__iterator_category_t): Define
        for C++11.
        (_RequireInputIte): Likewise and use __enable_if_t.
index 6dd01e1943cc2da8f16148ffcd5f6138c3bd6a4e..187c732d30487580b5109547d089e08cb8e451a6 100644 (file)
@@ -880,7 +880,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
     }
-#endif
+#endif // C++11
 
   template<typename _ForwardIterator, typename _Predicate>
     _GLIBCXX20_CONSTEXPR
index 2f4ff494a3ad81370e352ff8524c776834dbd4aa..e3e8736e7682f1b900423296354a3828152063ac 100644 (file)
@@ -80,6 +80,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        __copy_move_a2(istreambuf_iterator<_CharT2>,
                       istreambuf_iterator<_CharT2>, _CharT2*);
 
+#if __cplusplus >= 201103L
+      template<typename _CharT2, typename _Size>
+       friend __enable_if_t<__is_char<_CharT2>::__value, _CharT2*>
+       __copy_n_a(istreambuf_iterator<_CharT2>, _Size, _CharT2*);
+#endif
+
       template<typename _CharT2>
        friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
                                    istreambuf_iterator<_CharT2> >::__type
@@ -367,6 +373,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __result;
     }
 
+#if __cplusplus >= 201103L
+  template<typename _CharT, typename _Size>
+    __enable_if_t<__is_char<_CharT>::__value, _CharT*>
+    __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result)
+    {
+      if (__n == 0)
+       return __result;
+
+      __glibcxx_requires_cond(__it._M_sbuf,
+                             _M_message(__gnu_debug::__msg_inc_istreambuf)
+                             ._M_iterator(__it));
+      _CharT* __beg = __result;
+      __result += __it._M_sbuf->sgetn(__beg, __n);
+      __glibcxx_requires_cond(__result - __beg == __n,
+                             _M_message(__gnu_debug::__msg_inc_istreambuf)
+                             ._M_iterator(__it));
+      return __result;
+    }
+#endif // C++11
+
   template<typename _CharT>
     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
                                    istreambuf_iterator<_CharT> >::__type
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1.cc
new file mode 100644 (file)
index 0000000..87d37b6
--- /dev/null
@@ -0,0 +1,73 @@
+// 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;
+  auto res = std::copy_n(istrb_ite(ss), 0, ostr.begin());
+  VERIFY( res == ostr.begin() );
+  VERIFY( ostr.front() == '0' );
+
+  res = std::copy_n(istrb_ite(ss), 2, ostr.begin());
+  VERIFY( res == ostr.begin() + 2 );
+  VERIFY( ostr == "12000" );
+
+  res = std::copy_n(istrb_ite(ss), 3, ostr.begin() + 2);
+  VERIFY( res == ostr.begin() + 5 );
+  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);
+  auto res = std::copy_n(ibfit, 3, std::copy_n(ibfit, 2, ostr.begin()));
+  VERIFY( res == ostr.begin() + 5 );
+  VERIFY( ostr == "12345" );
+}
+
+void test03()
+{
+  std::string ostr(5, '0');
+  typedef std::istreambuf_iterator<char> istrb_ite;
+
+  auto res = std::copy_n(istrb_ite(), 0, ostr.begin());
+  VERIFY( res == ostr.begin() );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1_neg.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/1_neg.cc
new file mode 100644 (file)
index 0000000..42bc5b6
--- /dev/null
@@ -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;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/2_neg.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/istreambuf_iterator/2_neg.cc
new file mode 100644 (file)
index 0000000..cede27a
--- /dev/null
@@ -0,0 +1,38 @@
+// 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::string ostr(10, '0');
+  typedef std::istreambuf_iterator<char> istrb_ite;
+  std::copy_n(istrb_ite(), 10, ostr.begin());
+}
+
+int main()
+{
+  test01();
+  return 0;
+}