This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[PATCH] Implement std::advance for istreambuf_iterator using pubseekoff
- From: François Dumont <frs dot dumont at gmail dot com>
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 14 Oct 2019 22:12:19 +0200
- Subject: [PATCH] Implement std::advance for istreambuf_iterator using pubseekoff
The same way I proposed to review std::copy overload for
istreambuf_iterator we can implement std::advance using pubseekoff.
It is both a cleaner implementation and avoids yet another friend
declaration.
* include/std/streambuf
(advance(istreambuf_iterator<>&, _Distance)): Remove friend
declaration.
* include/bits/streambuf_iterator.h (__copy_move_a2): Re-implement
using
streambuf pubseekoff.
Tested under Linux x86_64.
Ok to commit ?
François
diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h
index 134b3486b9a..afe5c95f021 100644
--- a/libstdc++-v3/include/bits/streambuf_iterator.h
+++ b/libstdc++-v3/include/bits/streambuf_iterator.h
@@ -434,34 +434,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_message(__gnu_debug::__msg_inc_istreambuf)
._M_iterator(__i));
- typedef istreambuf_iterator<_CharT> __is_iterator_type;
- typedef typename __is_iterator_type::traits_type traits_type;
- typedef typename __is_iterator_type::streambuf_type streambuf_type;
- typedef typename traits_type::int_type int_type;
- const int_type __eof = traits_type::eof();
-
- streambuf_type* __sb = __i._M_sbuf;
- while (__n > 0)
- {
- streamsize __size = __sb->egptr() - __sb->gptr();
- if (__size > __n)
- {
- __sb->__safe_gbump(__n);
- break;
- }
-
- __sb->__safe_gbump(__size);
- __n -= __size;
- if (traits_type::eq_int_type(__sb->underflow(), __eof))
- {
- __glibcxx_requires_cond(__n == 0,
- _M_message(__gnu_debug::__msg_inc_istreambuf)
- ._M_iterator(__i));
- break;
- }
- }
+#ifdef _GLIBCXX_DEBUG
+ typedef basic_streambuf<_CharT> __streambuf_t;
+ typedef typename __streambuf_t::pos_type __pos_type;
+ __pos_type __cur_pos
+ = __i._M_sbuf->pubseekoff(0, ios_base::cur, ios_base::in);
+ __pos_type __new_pos =
+#endif
+ __i._M_sbuf->pubseekoff(__n, ios_base::cur, ios_base::in);
+ __i._M_c = char_traits<_CharT>::eof();
- __i._M_c = __eof;
+ __glibcxx_requires_cond(__new_pos - __cur_pos == __n,
+ _M_message(__gnu_debug::__msg_inc_istreambuf)
+ ._M_iterator(__i));
}
// @} group iterators
diff --git a/libstdc++-v3/include/std/streambuf b/libstdc++-v3/include/std/streambuf
index 3442f19bd78..ef03da39bc2 100644
--- a/libstdc++-v3/include/std/streambuf
+++ b/libstdc++-v3/include/std/streambuf
@@ -155,11 +155,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
const _CharT2&);
- template<typename _CharT2, typename _Distance>
- friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
- void>::__type
- advance(istreambuf_iterator<_CharT2>&, _Distance);
-
template<typename _CharT2, typename _Traits2>
friend basic_istream<_CharT2, _Traits2>&
operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);