30 #ifndef _STREAMBUF_ITERATOR_H
31 #define _STREAMBUF_ITERATOR_H 1
33 #pragma GCC system_header
38 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 template<
typename _CharT,
typename _Traits>
50 class istreambuf_iterator
51 :
public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
58 #if __cplusplus < 201103L
60 #elif __cplusplus > 201703L
67 typedef _Traits traits_type;
68 typedef typename _Traits::int_type int_type;
73 template<
typename _CharT2>
74 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
79 template<
bool _IsMove,
typename _CharT2>
80 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
85 #if __cplusplus >= 201103L
86 template<
typename _CharT2,
typename _Size>
87 friend __enable_if_t<__is_char<_CharT2>::__value, _CharT2*>
91 template<
typename _CharT2>
92 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
97 template<
typename _CharT2,
typename _Distance>
98 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
116 : _M_sbuf(0), _M_c(traits_type::eof()) { }
118 #if __cplusplus >= 201103L
126 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
130 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
132 #if __cplusplus >= 201103L
143 int_type __c = _M_get();
145 #ifdef _GLIBCXX_DEBUG_PEDANTIC
148 __glibcxx_requires_cond(!_S_is_eof(__c),
149 _M_message(__gnu_debug::__msg_deref_istreambuf)
150 ._M_iterator(*
this));
152 return traits_type::to_char_type(__c);
159 __glibcxx_requires_cond(_M_sbuf &&
160 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->
sgetc())),
161 _M_message(__gnu_debug::__msg_inc_istreambuf)
162 ._M_iterator(*
this));
165 _M_c = traits_type::eof();
173 __glibcxx_requires_cond(_M_sbuf &&
174 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->
sgetc())),
175 _M_message(__gnu_debug::__msg_inc_istreambuf)
176 ._M_iterator(*
this));
179 __old._M_c = _M_sbuf->
sbumpc();
180 _M_c = traits_type::eof();
190 {
return _M_at_eof() == __b._M_at_eof(); }
196 int_type __ret = _M_c;
197 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->
sgetc()))
204 {
return _S_is_eof(_M_get()); }
207 _S_is_eof(int_type __c)
209 const int_type __eof = traits_type::eof();
210 return traits_type::eq_int_type(__c, __eof);
214 template<
typename _CharT,
typename _Traits>
216 operator==(
const istreambuf_iterator<_CharT, _Traits>& __a,
217 const istreambuf_iterator<_CharT, _Traits>& __b)
218 {
return __a.equal(__b); }
220 template<
typename _CharT,
typename _Traits>
222 operator!=(
const istreambuf_iterator<_CharT, _Traits>& __a,
223 const istreambuf_iterator<_CharT, _Traits>& __b)
224 {
return !__a.equal(__b); }
227 template<
typename _CharT,
typename _Traits>
228 class ostreambuf_iterator
229 :
public iterator<output_iterator_tag, void, void, void, void>
235 #if __cplusplus > 201703L
239 typedef _Traits traits_type;
244 template<
typename _CharT2>
245 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
256 #if __cplusplus > 201703L
259 : _M_sbuf(
nullptr), _M_failed(true) { }
264 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
268 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
275 _Traits::eq_int_type(_M_sbuf->
sputc(__c), _Traits::eof()))
298 {
return _M_failed; }
303 if (__builtin_expect(!_M_failed,
true)
304 && __builtin_expect(this->_M_sbuf->
sputn(__ws, __len) != __len,
312 template<
typename _CharT>
313 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
314 ostreambuf_iterator<_CharT> >::__type
315 copy(istreambuf_iterator<_CharT> __first,
316 istreambuf_iterator<_CharT> __last,
317 ostreambuf_iterator<_CharT> __result)
319 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
322 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
324 __result._M_failed =
true;
329 template<
bool _IsMove,
typename _CharT>
330 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
331 ostreambuf_iterator<_CharT> >::__type
332 __copy_move_a2(_CharT* __first, _CharT* __last,
333 ostreambuf_iterator<_CharT> __result)
337 __result._M_put(__first, __num);
341 template<
bool _IsMove,
typename _CharT>
342 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
343 ostreambuf_iterator<_CharT> >::__type
344 __copy_move_a2(
const _CharT* __first,
const _CharT* __last,
345 ostreambuf_iterator<_CharT> __result)
349 __result._M_put(__first, __num);
353 template<
bool _IsMove,
typename _CharT>
354 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
356 __copy_move_a2(istreambuf_iterator<_CharT> __first,
357 istreambuf_iterator<_CharT> __last, _CharT* __result)
359 typedef istreambuf_iterator<_CharT> __is_iterator_type;
360 typedef typename __is_iterator_type::traits_type traits_type;
361 typedef typename __is_iterator_type::streambuf_type streambuf_type;
362 typedef typename traits_type::int_type int_type;
364 if (__first._M_sbuf && !__last._M_sbuf)
366 streambuf_type* __sb = __first._M_sbuf;
367 int_type __c = __sb->sgetc();
368 while (!traits_type::eq_int_type(__c, traits_type::eof()))
370 const streamsize __n = __sb->egptr() - __sb->gptr();
373 traits_type::copy(__result, __sb->gptr(), __n);
374 __sb->__safe_gbump(__n);
376 __c = __sb->underflow();
380 *__result++ = traits_type::to_char_type(__c);
381 __c = __sb->snextc();
388 #if __cplusplus >= 201103L
389 template<
typename _CharT,
typename _Size>
390 __enable_if_t<__is_char<_CharT>::__value, _CharT*>
391 __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result)
396 __glibcxx_requires_cond(__it._M_sbuf,
397 _M_message(__gnu_debug::__msg_inc_istreambuf)
399 _CharT* __beg = __result;
400 __result += __it._M_sbuf->sgetn(__beg, __n);
401 __glibcxx_requires_cond(__result - __beg == __n,
402 _M_message(__gnu_debug::__msg_inc_istreambuf)
408 template<
typename _CharT>
409 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
410 istreambuf_iterator<_CharT> >::__type
411 find(istreambuf_iterator<_CharT> __first,
412 istreambuf_iterator<_CharT> __last,
const _CharT& __val)
414 typedef istreambuf_iterator<_CharT> __is_iterator_type;
415 typedef typename __is_iterator_type::traits_type traits_type;
416 typedef typename __is_iterator_type::streambuf_type streambuf_type;
417 typedef typename traits_type::int_type int_type;
418 const int_type __eof = traits_type::eof();
420 if (__first._M_sbuf && !__last._M_sbuf)
422 const int_type __ival = traits_type::to_int_type(__val);
423 streambuf_type* __sb = __first._M_sbuf;
424 int_type __c = __sb->sgetc();
425 while (!traits_type::eq_int_type(__c, __eof)
426 && !traits_type::eq_int_type(__c, __ival))
428 streamsize __n = __sb->egptr() - __sb->gptr();
431 const _CharT* __p = traits_type::find(__sb->gptr(),
434 __n = __p - __sb->gptr();
435 __sb->__safe_gbump(__n);
439 __c = __sb->snextc();
442 __first._M_c = __eof;
448 template<
typename _CharT,
typename _Distance>
449 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
451 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
456 __glibcxx_assert(__n > 0);
457 __glibcxx_requires_cond(!__i._M_at_eof(),
458 _M_message(__gnu_debug::__msg_inc_istreambuf)
461 typedef istreambuf_iterator<_CharT> __is_iterator_type;
462 typedef typename __is_iterator_type::traits_type traits_type;
463 typedef typename __is_iterator_type::streambuf_type streambuf_type;
464 typedef typename traits_type::int_type int_type;
465 const int_type __eof = traits_type::eof();
467 streambuf_type* __sb = __i._M_sbuf;
470 streamsize __size = __sb->egptr() - __sb->gptr();
473 __sb->__safe_gbump(__n);
477 __sb->__safe_gbump(__size);
479 if (traits_type::eq_int_type(__sb->underflow(), __eof))
481 __glibcxx_requires_cond(__n == 0,
482 _M_message(__gnu_debug::__msg_inc_istreambuf)
493 _GLIBCXX_END_NAMESPACE_VERSION