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