]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
83ffe9cd 3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
725dc051
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
725dc051 24
f910786b 25/** @file bits/streambuf_iterator.h
729e3d3f 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{iterator}
729e3d3f
PE
28 */
29
3d7c150e
BK
30#ifndef _STREAMBUF_ITERATOR_H
31#define _STREAMBUF_ITERATOR_H 1
725dc051 32
b0a85b86
GDR
33#pragma GCC system_header
34
2e2a38cd 35#include <streambuf>
d31e19e4 36#include <bits/stl_iterator_base_types.h>
285b36d6 37#include <debug/debug.h>
2e2a38cd 38
12ffa228
BK
39namespace std _GLIBCXX_VISIBILITY(default)
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
e324f9cb 42
8e32aa11
BK
43 /**
44 * @addtogroup iterators
45 * @{
46 */
47
de196e5d
JW
48// Ignore warnings about std::iterator.
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
725dc051 51 // 24.5.3 Template class istreambuf_iterator
ffcec5c8 52 /// Provides input iterator semantics for streambufs.
39003c99 53 template<typename _CharT, typename _Traits>
725dc051
BK
54 class istreambuf_iterator
55 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
7918cb93 56 _CharT*, _CharT>
725dc051
BK
57 {
58 public:
725dc051 59 // Types:
f0b88346 60 ///@{
ffcec5c8 61 /// Public typedefs
7918cb93
JW
62#if __cplusplus < 201103L
63 typedef _CharT& reference; // Changed to _CharT by LWG 445
64#elif __cplusplus > 201703L
9aeb3bef
JW
65 // _GLIBCXX_RESOLVE_LIB_DEFECTS
66 // 3188. istreambuf_iterator::pointer should not be unspecified
67 using pointer = void;
68#endif
7918cb93 69
ed6814f7
BI
70 typedef _CharT char_type;
71 typedef _Traits traits_type;
72 typedef typename _Traits::int_type int_type;
73 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
74 typedef basic_istream<_CharT, _Traits> istream_type;
f0b88346 75 ///@}
725dc051 76
0002d5d2 77 template<typename _CharT2>
105c6331 78 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 79 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
80 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
81 ostreambuf_iterator<_CharT2>);
82
f0112db9 83 template<bool _IsMove, typename _CharT2>
e324f9cb 84 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
105c6331 85 _CharT2*>::__type
f0112db9
PC
86 __copy_move_a2(istreambuf_iterator<_CharT2>,
87 istreambuf_iterator<_CharT2>, _CharT2*);
0002d5d2 88
8ab38f6c 89 template<typename _CharT2, typename _Size>
4e05c918
FD
90 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
91 _CharT2*>::__type
92 __copy_n_a(istreambuf_iterator<_CharT2>, _Size, _CharT2*, bool);
8ab38f6c 93
0002d5d2 94 template<typename _CharT2>
105c6331 95 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 96 istreambuf_iterator<_CharT2> >::__type
0002d5d2 97 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
976e25f4 98 const _CharT2&);
0002d5d2 99
e324f9cb
FD
100 template<typename _CharT2, typename _Distance>
101 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
102 void>::__type
103 advance(istreambuf_iterator<_CharT2>&, _Distance);
104
39003c99 105 private:
ed6814f7
BI
106 // 24.5.3 istreambuf_iterator
107 // p 1
39003c99
BK
108 // If the end of stream is reached (streambuf_type::sgetc()
109 // returns traits_type::eof()), the iterator becomes equal to
110 // the "end of stream" iterator value.
111 // NB: This implementation assumes the "end of stream" value
112 // is EOF, or -1.
ed6814f7 113 mutable streambuf_type* _M_sbuf;
4e914524 114 int_type _M_c;
39003c99
BK
115
116 public:
ffcec5c8 117 /// Construct end of input stream iterator.
23e4c4ee 118 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
f13a69ec 119 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 120
07522ae9 121#if __cplusplus > 201703L && __cpp_lib_concepts
120e8734
JW
122 constexpr istreambuf_iterator(default_sentinel_t) noexcept
123 : istreambuf_iterator() { }
124#endif
125
734f5023 126#if __cplusplus >= 201103L
23e4c4ee
PC
127 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
128
129 ~istreambuf_iterator() = default;
130#endif
131
ffcec5c8 132 /// Construct start of input stream iterator.
23e4c4ee 133 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 134 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 135
ffcec5c8 136 /// Construct start of streambuf iterator.
23e4c4ee 137 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 138 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 139
a1417556
JW
140#if __cplusplus >= 201103L
141 istreambuf_iterator&
142 operator=(const istreambuf_iterator&) noexcept = default;
143#endif
144
ffcec5c8
JQ
145 /// Return the current character pointed to by iterator. This returns
146 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
147 /// operator*() on an end of stream is undefined.
240b01b0 148 _GLIBCXX_NODISCARD
ed6814f7 149 char_type
725dc051 150 operator*() const
ed6814f7 151 {
4e914524
PO
152 int_type __c = _M_get();
153
285b36d6
BK
154#ifdef _GLIBCXX_DEBUG_PEDANTIC
155 // Dereferencing a past-the-end istreambuf_iterator is a
156 // libstdc++ extension
4e914524 157 __glibcxx_requires_cond(!_S_is_eof(__c),
285b36d6 158 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 159 ._M_iterator(*this));
285b36d6 160#endif
4e914524 161 return traits_type::to_char_type(__c);
285b36d6 162 }
ffcec5c8
JQ
163
164 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 165 istreambuf_iterator&
725dc051 166 operator++()
ed6814f7 167 {
4e914524
PO
168 __glibcxx_requires_cond(_M_sbuf &&
169 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 170 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 171 ._M_iterator(*this));
4e914524
PO
172
173 _M_sbuf->sbumpc();
174 _M_c = traits_type::eof();
ed6814f7 175 return *this;
725dc051
BK
176 }
177
ffcec5c8 178 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 179 istreambuf_iterator
725dc051
BK
180 operator++(int)
181 {
4e914524
PO
182 __glibcxx_requires_cond(_M_sbuf &&
183 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 184 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 185 ._M_iterator(*this));
285b36d6 186
b581eaf7 187 istreambuf_iterator __old = *this;
4e914524
PO
188 __old._M_c = _M_sbuf->sbumpc();
189 _M_c = traits_type::eof();
ed6814f7 190 return __old;
725dc051 191 }
725dc051 192
f5677b15 193 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 194 // 110 istreambuf_iterator::equal not const
0e7300bb 195 // NB: there is also number 111 (NAD) relevant to this function.
ffcec5c8 196 /// Return true both iterators are end or both are not end.
240b01b0 197 _GLIBCXX_NODISCARD
ed6814f7 198 bool
b581eaf7 199 equal(const istreambuf_iterator& __b) const
6bfcbf0d 200 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
201
202 private:
ed6814f7 203 int_type
9875ea05 204 _M_get() const
ed6814f7 205 {
4e914524
PO
206 int_type __ret = _M_c;
207 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
208 _M_sbuf = 0;
9875ea05
BK
209 return __ret;
210 }
285b36d6 211
ed6814f7 212 bool
285b36d6 213 _M_at_eof() const
4e914524
PO
214 { return _S_is_eof(_M_get()); }
215
216 static bool
217 _S_is_eof(int_type __c)
285b36d6
BK
218 {
219 const int_type __eof = traits_type::eof();
4e914524 220 return traits_type::eq_int_type(__c, __eof);
285b36d6 221 }
120e8734 222
07522ae9 223#if __cplusplus > 201703L && __cpp_lib_concepts
240b01b0 224 [[nodiscard]]
120e8734
JW
225 friend bool
226 operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
227 { return __i._M_at_eof(); }
228#endif
725dc051
BK
229 };
230
231 template<typename _CharT, typename _Traits>
240b01b0 232 _GLIBCXX_NODISCARD
ed6814f7 233 inline bool
725dc051
BK
234 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
235 const istreambuf_iterator<_CharT, _Traits>& __b)
236 { return __a.equal(__b); }
237
240b01b0 238#if __cpp_impl_three_way_comparison < 201907L
725dc051 239 template<typename _CharT, typename _Traits>
240b01b0 240 _GLIBCXX_NODISCARD
ed6814f7 241 inline bool
725dc051
BK
242 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
243 const istreambuf_iterator<_CharT, _Traits>& __b)
244 { return !__a.equal(__b); }
240b01b0 245#endif
5c352a47 246
ffcec5c8 247 /// Provides output iterator semantics for streambufs.
5c352a47
BK
248 template<typename _CharT, typename _Traits>
249 class ostreambuf_iterator
250 : public iterator<output_iterator_tag, void, void, void, void>
251 {
252 public:
253 // Types:
f0b88346 254 ///@{
ffcec5c8 255 /// Public typedefs
9aeb3bef
JW
256#if __cplusplus > 201703L
257 using difference_type = ptrdiff_t;
258#endif
e324f9cb
FD
259 typedef _CharT char_type;
260 typedef _Traits traits_type;
5c352a47
BK
261 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
262 typedef basic_ostream<_CharT, _Traits> ostream_type;
f0b88346 263 ///@}
5c352a47 264
0002d5d2 265 template<typename _CharT2>
105c6331 266 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 267 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
268 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
269 ostreambuf_iterator<_CharT2>);
270
5c352a47 271 private:
ed6814f7
BI
272 streambuf_type* _M_sbuf;
273 bool _M_failed;
5c352a47
BK
274
275 public:
9aeb3bef
JW
276
277#if __cplusplus > 201703L
278 constexpr
279 ostreambuf_iterator() noexcept
280 : _M_sbuf(nullptr), _M_failed(true) { }
281#endif
282
ffcec5c8 283 /// Construct output iterator from ostream.
23e4c4ee 284 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
5c352a47 285 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 286
ffcec5c8 287 /// Construct output iterator from streambuf.
23e4c4ee 288 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
5c352a47
BK
289 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
290
ffcec5c8 291 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 292 ostreambuf_iterator&
2e2a38cd
BK
293 operator=(_CharT __c)
294 {
ed6814f7 295 if (!_M_failed &&
2e2a38cd
BK
296 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
297 _M_failed = true;
298 return *this;
299 }
5c352a47 300
ffcec5c8 301 /// Return *this.
240b01b0 302 _GLIBCXX_NODISCARD
ed6814f7 303 ostreambuf_iterator&
e0ec69c9 304 operator*()
5c352a47
BK
305 { return *this; }
306
ffcec5c8 307 /// Return *this.
ed6814f7 308 ostreambuf_iterator&
e0ec69c9 309 operator++(int)
5c352a47
BK
310 { return *this; }
311
ffcec5c8 312 /// Return *this.
ed6814f7 313 ostreambuf_iterator&
e0ec69c9 314 operator++()
5c352a47
BK
315 { return *this; }
316
ffcec5c8 317 /// Return true if previous operator=() failed.
240b01b0 318 _GLIBCXX_NODISCARD
ed6814f7 319 bool
23e4c4ee 320 failed() const _GLIBCXX_USE_NOEXCEPT
5c352a47 321 { return _M_failed; }
5c352a47 322
ed6814f7 323 ostreambuf_iterator&
2e2a38cd
BK
324 _M_put(const _CharT* __ws, streamsize __len)
325 {
53279c10 326 if (__builtin_expect(!_M_failed, true)
15d72060
PC
327 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
328 false))
53279c10 329 _M_failed = true;
2e2a38cd
BK
330 return *this;
331 }
332 };
de196e5d 333#pragma GCC diagnostic pop
3cbc7af0 334
0002d5d2
PC
335 // Overloads for streambuf iterators.
336 template<typename _CharT>
105c6331 337 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
e324f9cb 338 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
339 copy(istreambuf_iterator<_CharT> __first,
340 istreambuf_iterator<_CharT> __last,
341 ostreambuf_iterator<_CharT> __result)
342 {
343 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
344 {
345 bool __ineof;
346 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
347 if (!__ineof)
348 __result._M_failed = true;
349 }
350 return __result;
351 }
352
f0112db9 353 template<bool _IsMove, typename _CharT>
e324f9cb
FD
354 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
355 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
356 __copy_move_a2(_CharT* __first, _CharT* __last,
357 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
358 {
359 const streamsize __num = __last - __first;
360 if (__num > 0)
361 __result._M_put(__first, __num);
362 return __result;
363 }
364
f0112db9 365 template<bool _IsMove, typename _CharT>
105c6331
BK
366 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
367 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
368 __copy_move_a2(const _CharT* __first, const _CharT* __last,
369 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
370 {
371 const streamsize __num = __last - __first;
372 if (__num > 0)
373 __result._M_put(__first, __num);
374 return __result;
375 }
376
f0112db9 377 template<bool _IsMove, typename _CharT>
e324f9cb
FD
378 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
379 _CharT*>::__type
f0112db9
PC
380 __copy_move_a2(istreambuf_iterator<_CharT> __first,
381 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2 382 {
e324f9cb
FD
383 typedef istreambuf_iterator<_CharT> __is_iterator_type;
384 typedef typename __is_iterator_type::traits_type traits_type;
0002d5d2 385 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 386 typedef typename traits_type::int_type int_type;
0002d5d2
PC
387
388 if (__first._M_sbuf && !__last._M_sbuf)
389 {
390 streambuf_type* __sb = __first._M_sbuf;
391 int_type __c = __sb->sgetc();
392 while (!traits_type::eq_int_type(__c, traits_type::eof()))
393 {
394 const streamsize __n = __sb->egptr() - __sb->gptr();
395 if (__n > 1)
396 {
397 traits_type::copy(__result, __sb->gptr(), __n);
1139a735 398 __sb->__safe_gbump(__n);
0002d5d2
PC
399 __result += __n;
400 __c = __sb->underflow();
401 }
402 else
403 {
404 *__result++ = traits_type::to_char_type(__c);
405 __c = __sb->snextc();
406 }
407 }
408 }
409 return __result;
410 }
411
8ab38f6c 412 template<typename _CharT, typename _Size>
4e05c918
FD
413 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
414 _CharT*>::__type
415 __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result,
416 bool __strict __attribute__((__unused__)))
8ab38f6c
FD
417 {
418 if (__n == 0)
419 return __result;
420
421 __glibcxx_requires_cond(__it._M_sbuf,
422 _M_message(__gnu_debug::__msg_inc_istreambuf)
423 ._M_iterator(__it));
424 _CharT* __beg = __result;
425 __result += __it._M_sbuf->sgetn(__beg, __n);
4e05c918 426 __glibcxx_requires_cond(!__strict || __result - __beg == __n,
8ab38f6c
FD
427 _M_message(__gnu_debug::__msg_inc_istreambuf)
428 ._M_iterator(__it));
429 return __result;
430 }
8ab38f6c 431
0002d5d2 432 template<typename _CharT>
105c6331
BK
433 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
434 istreambuf_iterator<_CharT> >::__type
0002d5d2 435 find(istreambuf_iterator<_CharT> __first,
976e25f4 436 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2 437 {
e324f9cb 438 typedef istreambuf_iterator<_CharT> __is_iterator_type;
0002d5d2
PC
439 typedef typename __is_iterator_type::traits_type traits_type;
440 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 441 typedef typename traits_type::int_type int_type;
4e914524 442 const int_type __eof = traits_type::eof();
0002d5d2
PC
443
444 if (__first._M_sbuf && !__last._M_sbuf)
445 {
446 const int_type __ival = traits_type::to_int_type(__val);
447 streambuf_type* __sb = __first._M_sbuf;
448 int_type __c = __sb->sgetc();
4e914524 449 while (!traits_type::eq_int_type(__c, __eof)
0002d5d2
PC
450 && !traits_type::eq_int_type(__c, __ival))
451 {
452 streamsize __n = __sb->egptr() - __sb->gptr();
453 if (__n > 1)
454 {
455 const _CharT* __p = traits_type::find(__sb->gptr(),
456 __n, __val);
457 if (__p)
458 __n = __p - __sb->gptr();
1139a735 459 __sb->__safe_gbump(__n);
0002d5d2
PC
460 __c = __sb->sgetc();
461 }
462 else
463 __c = __sb->snextc();
464 }
465
4e914524 466 __first._M_c = __eof;
0002d5d2 467 }
4e914524 468
0002d5d2
PC
469 return __first;
470 }
471
e324f9cb
FD
472 template<typename _CharT, typename _Distance>
473 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
474 void>::__type
475 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
476 {
477 if (__n == 0)
478 return;
479
480 __glibcxx_assert(__n > 0);
481 __glibcxx_requires_cond(!__i._M_at_eof(),
482 _M_message(__gnu_debug::__msg_inc_istreambuf)
483 ._M_iterator(__i));
484
485 typedef istreambuf_iterator<_CharT> __is_iterator_type;
486 typedef typename __is_iterator_type::traits_type traits_type;
487 typedef typename __is_iterator_type::streambuf_type streambuf_type;
488 typedef typename traits_type::int_type int_type;
489 const int_type __eof = traits_type::eof();
490
491 streambuf_type* __sb = __i._M_sbuf;
492 while (__n > 0)
493 {
494 streamsize __size = __sb->egptr() - __sb->gptr();
495 if (__size > __n)
496 {
497 __sb->__safe_gbump(__n);
498 break;
499 }
500
501 __sb->__safe_gbump(__size);
502 __n -= __size;
503 if (traits_type::eq_int_type(__sb->underflow(), __eof))
504 {
505 __glibcxx_requires_cond(__n == 0,
506 _M_message(__gnu_debug::__msg_inc_istreambuf)
507 ._M_iterator(__i));
508 break;
509 }
510 }
511
512 __i._M_c = __eof;
513 }
514
72ddd861 515/// @} group iterators
8e32aa11 516
12ffa228
BK
517_GLIBCXX_END_NAMESPACE_VERSION
518} // namespace
3cbc7af0 519
b85381b9 520#endif