1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ISTREAM
11#define _LIBCPP_ISTREAM
12
13/*
14 istream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_istream
18 : virtual public basic_ios<charT,traits>
19{
20public:
21 // types (inherited from basic_ios (27.5.4)):
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
27
28 // 27.7.1.1.1 Constructor/destructor:
29 explicit basic_istream(basic_streambuf<char_type, traits_type>* sb);
30 basic_istream(basic_istream&& rhs);
31 virtual ~basic_istream();
32
33 // 27.7.1.1.2 Assign/swap:
34 basic_istream& operator=(basic_istream&& rhs);
35 void swap(basic_istream& rhs);
36
37 // 27.7.1.1.3 Prefix/suffix:
38 class sentry;
39
40 // 27.7.1.2 Formatted input:
41 basic_istream& operator>>(basic_istream& (*pf)(basic_istream&));
42 basic_istream& operator>>(basic_ios<char_type, traits_type>&
43 (*pf)(basic_ios<char_type, traits_type>&));
44 basic_istream& operator>>(ios_base& (*pf)(ios_base&));
45 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb);
46 basic_istream& operator>>(bool& n);
47 basic_istream& operator>>(short& n);
48 basic_istream& operator>>(unsigned short& n);
49 basic_istream& operator>>(int& n);
50 basic_istream& operator>>(unsigned int& n);
51 basic_istream& operator>>(long& n);
52 basic_istream& operator>>(unsigned long& n);
53 basic_istream& operator>>(long long& n);
54 basic_istream& operator>>(unsigned long long& n);
55 basic_istream& operator>>(float& f);
56 basic_istream& operator>>(double& f);
57 basic_istream& operator>>(long double& f);
58 basic_istream& operator>>(void*& p);
59
60 // 27.7.1.3 Unformatted input:
61 streamsize gcount() const;
62 int_type get();
63 basic_istream& get(char_type& c);
64 basic_istream& get(char_type* s, streamsize n);
65 basic_istream& get(char_type* s, streamsize n, char_type delim);
66 basic_istream& get(basic_streambuf<char_type,traits_type>& sb);
67 basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim);
68
69 basic_istream& getline(char_type* s, streamsize n);
70 basic_istream& getline(char_type* s, streamsize n, char_type delim);
71
72 basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
73 int_type peek();
74 basic_istream& read (char_type* s, streamsize n);
75 streamsize readsome(char_type* s, streamsize n);
76
77 basic_istream& putback(char_type c);
78 basic_istream& unget();
79 int sync();
80
81 pos_type tellg();
82 basic_istream& seekg(pos_type);
83 basic_istream& seekg(off_type, ios_base::seekdir);
84protected:
85 basic_istream(const basic_istream& rhs) = delete;
86 basic_istream(basic_istream&& rhs);
87 // 27.7.2.1.2 Assign/swap:
88 basic_istream& operator=(const basic_istream& rhs) = delete;
89 basic_istream& operator=(basic_istream&& rhs);
90 void swap(basic_istream& rhs);
91};
92
93// 27.7.1.2.3 character extraction templates:
94template<class charT, class traits>
95 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
96
97template<class traits>
98 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
99
100template<class traits>
101 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
102
103template<class charT, class traits>
104 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
105
106template<class traits>
107 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
108
109template<class traits>
110 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
111
112template <class charT, class traits>
113 void
114 swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
115
116typedef basic_istream<char> istream;
117typedef basic_istream<wchar_t> wistream;
118
119template <class charT, class traits = char_traits<charT> >
120class basic_iostream :
121 public basic_istream<charT,traits>,
122 public basic_ostream<charT,traits>
123{
124public:
125 // types:
126 typedef charT char_type;
127 typedef traits traits_type;
128 typedef typename traits_type::int_type int_type;
129 typedef typename traits_type::pos_type pos_type;
130 typedef typename traits_type::off_type off_type;
131
132 // constructor/destructor
133 explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
134 basic_iostream(basic_iostream&& rhs);
135 virtual ~basic_iostream();
136
137 // assign/swap
138 basic_iostream& operator=(basic_iostream&& rhs);
139 void swap(basic_iostream& rhs);
140};
141
142template <class charT, class traits>
143 void
144 swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
145
146typedef basic_iostream<char> iostream;
147typedef basic_iostream<wchar_t> wiostream;
148
149template <class charT, class traits>
150 basic_istream<charT,traits>&
151 ws(basic_istream<charT,traits>& is);
152
153// rvalue stream extraction
154template <class Stream, class T>
155 Stream&& operator>>(Stream&& is, T&& x);
156
157} // std
158
159*/
160
161#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
162# include <__cxx03/istream>
163#else
164# include <__config>
165
166# if _LIBCPP_HAS_LOCALIZATION
167
168# include <__fwd/istream.h>
169# include <__iterator/istreambuf_iterator.h>
170# include <__ostream/basic_ostream.h>
171# include <__type_traits/conjunction.h>
172# include <__type_traits/enable_if.h>
173# include <__type_traits/is_base_of.h>
174# include <__type_traits/make_unsigned.h>
175# include <__utility/declval.h>
176# include <__utility/forward.h>
177# include <bitset>
178# include <ios>
179# include <locale>
180# include <version>
181
182# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
183# pragma GCC system_header
184# endif
185
186_LIBCPP_PUSH_MACROS
187# include <__undef_macros>
188
189_LIBCPP_BEGIN_NAMESPACE_STD
190
191template <class _CharT, class _Traits>
192class basic_istream : virtual public basic_ios<_CharT, _Traits> {
193 streamsize __gc_;
194
195 _LIBCPP_HIDE_FROM_ABI void __inc_gcount() {
196 if (__gc_ < numeric_limits<streamsize>::max())
197 ++__gc_;
198 }
199
200public:
201 // types (inherited from basic_ios (27.5.4)):
202 typedef _CharT char_type;
203 typedef _Traits traits_type;
204 typedef typename traits_type::int_type int_type;
205 typedef typename traits_type::pos_type pos_type;
206 typedef typename traits_type::off_type off_type;
207
208 // 27.7.1.1.1 Constructor/destructor:
209 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb)
210 : __gc_(0) {
211 this->init(__sb);
212 }
213 ~basic_istream() override;
214
215protected:
216 inline _LIBCPP_HIDE_FROM_ABI basic_istream(basic_istream&& __rhs);
217
218 // 27.7.1.1.2 Assign/swap:
219 inline _LIBCPP_HIDE_FROM_ABI basic_istream& operator=(basic_istream&& __rhs);
220
221 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_istream& __rhs) {
222 std::swap(__gc_, __rhs.__gc_);
223 basic_ios<char_type, traits_type>::swap(__rhs);
224 }
225
226public:
227 basic_istream(const basic_istream& __rhs) = delete;
228 basic_istream& operator=(const basic_istream& __rhs) = delete;
229
230 // 27.7.1.1.3 Prefix/suffix:
231 class sentry;
232
233 // 27.7.1.2 Formatted input:
234 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) {
235 return __pf(*this);
236 }
237
238 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream&
239 operator>>(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
240 __pf(*this);
241 return *this;
242 }
243
244 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(ios_base& (*__pf)(ios_base&)) {
245 __pf(*this);
246 return *this;
247 }
248
249 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
250 basic_istream& operator>>(bool& __n);
251 basic_istream& operator>>(short& __n);
252 basic_istream& operator>>(unsigned short& __n);
253 basic_istream& operator>>(int& __n);
254 basic_istream& operator>>(unsigned int& __n);
255 basic_istream& operator>>(long& __n);
256 basic_istream& operator>>(unsigned long& __n);
257 basic_istream& operator>>(long long& __n);
258 basic_istream& operator>>(unsigned long long& __n);
259 basic_istream& operator>>(float& __f);
260 basic_istream& operator>>(double& __f);
261 basic_istream& operator>>(long double& __f);
262 basic_istream& operator>>(void*& __p);
263
264 // 27.7.1.3 Unformatted input:
265 _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
266 int_type get();
267
268 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
269 int_type __ch = get();
270 if (__ch != traits_type::eof())
271 __c = traits_type::to_char_type(__ch);
272 return *this;
273 }
274
275 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type* __s, streamsize __n) {
276 return get(__s, __n, this->widen('\n'));
277 }
278
279 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
280
281 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb) {
282 return get(__sb, this->widen('\n'));
283 }
284
285 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
286
287 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& getline(char_type* __s, streamsize __n) {
288 return getline(__s, __n, this->widen('\n'));
289 }
290
291 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
292
293 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
294 int_type peek();
295 basic_istream& read(char_type* __s, streamsize __n);
296 streamsize readsome(char_type* __s, streamsize __n);
297
298 basic_istream& putback(char_type __c);
299 basic_istream& unget();
300 int sync();
301
302 pos_type tellg();
303 basic_istream& seekg(pos_type __pos);
304 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
305};
306
307template <class _CharT, class _Traits>
308class basic_istream<_CharT, _Traits>::sentry {
309 bool __ok_;
310
311public:
312 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
313 // ~sentry() = default;
314
315 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
316
317 sentry(const sentry&) = delete;
318 sentry& operator=(const sentry&) = delete;
319};
320
321template <class _CharT, class _Traits>
322basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) {
323 if (__is.good()) {
324 if (__is.tie())
325 __is.tie()->flush();
326 if (!__noskipws && (__is.flags() & ios_base::skipws)) {
327 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
328 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
329 _Ip __i(__is);
330 _Ip __eof;
331 for (; __i != __eof; ++__i)
332 if (!__ct.is(__ct.space, *__i))
333 break;
334 if (__i == __eof)
335 __is.setstate(ios_base::failbit | ios_base::eofbit);
336 }
337 __ok_ = __is.good();
338 } else
339 __is.setstate(ios_base::failbit);
340}
341
342template <class _CharT, class _Traits>
343basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) {
344 __rhs.__gc_ = 0;
345 this->move(__rhs);
346}
347
348template <class _CharT, class _Traits>
349basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs) {
350 swap(__rhs);
351 return *this;
352}
353
354template <class _CharT, class _Traits>
355basic_istream<_CharT, _Traits>::~basic_istream() {}
356
357template <class _Tp, class _CharT, class _Traits>
358_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
359__input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
360 ios_base::iostate __state = ios_base::goodbit;
361 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
362 if (__s) {
363# if _LIBCPP_HAS_EXCEPTIONS
364 try {
365# endif // _LIBCPP_HAS_EXCEPTIONS
366 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
367 typedef num_get<_CharT, _Ip> _Fp;
368 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
369# if _LIBCPP_HAS_EXCEPTIONS
370 } catch (...) {
371 __state |= ios_base::badbit;
372 __is.__setstate_nothrow(__state);
373 if (__is.exceptions() & ios_base::badbit) {
374 throw;
375 }
376 }
377# endif
378 __is.setstate(__state);
379 }
380 return __is;
381}
382
383template <class _CharT, class _Traits>
384basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n) {
385 return std::__input_arithmetic<unsigned short>(*this, __n);
386}
387
388template <class _CharT, class _Traits>
389basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n) {
390 return std::__input_arithmetic<unsigned int>(*this, __n);
391}
392
393template <class _CharT, class _Traits>
394basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long& __n) {
395 return std::__input_arithmetic<long>(*this, __n);
396}
397
398template <class _CharT, class _Traits>
399basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n) {
400 return std::__input_arithmetic<unsigned long>(*this, __n);
401}
402
403template <class _CharT, class _Traits>
404basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long long& __n) {
405 return std::__input_arithmetic<long long>(*this, __n);
406}
407
408template <class _CharT, class _Traits>
409basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n) {
410 return std::__input_arithmetic<unsigned long long>(*this, __n);
411}
412
413template <class _CharT, class _Traits>
414basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(float& __n) {
415 return std::__input_arithmetic<float>(*this, __n);
416}
417
418template <class _CharT, class _Traits>
419basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(double& __n) {
420 return std::__input_arithmetic<double>(*this, __n);
421}
422
423template <class _CharT, class _Traits>
424basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long double& __n) {
425 return std::__input_arithmetic<long double>(*this, __n);
426}
427
428template <class _CharT, class _Traits>
429basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(bool& __n) {
430 return std::__input_arithmetic<bool>(*this, __n);
431}
432
433template <class _CharT, class _Traits>
434basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(void*& __n) {
435 return std::__input_arithmetic<void*>(*this, __n);
436}
437
438template <class _Tp, class _CharT, class _Traits>
439_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
440__input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
441 ios_base::iostate __state = ios_base::goodbit;
442 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
443 if (__s) {
444# if _LIBCPP_HAS_EXCEPTIONS
445 try {
446# endif // _LIBCPP_HAS_EXCEPTIONS
447 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
448 typedef num_get<_CharT, _Ip> _Fp;
449 long __temp;
450 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
451 if (__temp < numeric_limits<_Tp>::min()) {
452 __state |= ios_base::failbit;
453 __n = numeric_limits<_Tp>::min();
454 } else if (__temp > numeric_limits<_Tp>::max()) {
455 __state |= ios_base::failbit;
456 __n = numeric_limits<_Tp>::max();
457 } else {
458 __n = static_cast<_Tp>(__temp);
459 }
460# if _LIBCPP_HAS_EXCEPTIONS
461 } catch (...) {
462 __state |= ios_base::badbit;
463 __is.__setstate_nothrow(__state);
464 if (__is.exceptions() & ios_base::badbit) {
465 throw;
466 }
467 }
468# endif // _LIBCPP_HAS_EXCEPTIONS
469 __is.setstate(__state);
470 }
471 return __is;
472}
473
474template <class _CharT, class _Traits>
475basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(short& __n) {
476 return std::__input_arithmetic_with_numeric_limits<short>(*this, __n);
477}
478
479template <class _CharT, class _Traits>
480basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(int& __n) {
481 return std::__input_arithmetic_with_numeric_limits<int>(*this, __n);
482}
483
484template <class _CharT, class _Traits>
485_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
486__input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) {
487 ios_base::iostate __state = ios_base::goodbit;
488 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
489 if (__sen) {
490# if _LIBCPP_HAS_EXCEPTIONS
491 try {
492# endif
493 _CharT* __s = __p;
494 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
495 while (__s != __p + (__n - 1)) {
496 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
497 if (_Traits::eq_int_type(__i, _Traits::eof())) {
498 __state |= ios_base::eofbit;
499 break;
500 }
501 _CharT __ch = _Traits::to_char_type(__i);
502 if (__ct.is(__ct.space, __ch))
503 break;
504 *__s++ = __ch;
505 __is.rdbuf()->sbumpc();
506 }
507 *__s = _CharT();
508 __is.width(0);
509 if (__s == __p)
510 __state |= ios_base::failbit;
511# if _LIBCPP_HAS_EXCEPTIONS
512 } catch (...) {
513 __state |= ios_base::badbit;
514 __is.__setstate_nothrow(__state);
515 if (__is.exceptions() & ios_base::badbit) {
516 throw;
517 }
518 }
519# endif
520 __is.setstate(__state);
521 }
522 return __is;
523}
524
525# if _LIBCPP_STD_VER >= 20
526
527template <class _CharT, class _Traits, size_t _Np>
528inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
529operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) {
530 size_t __n = _Np;
531 if (__is.width() > 0)
532 __n = std::min(a: size_t(__is.width()), b: _Np);
533 return std::__input_c_string(__is, __buf, __n);
534}
535
536template <class _Traits, size_t _Np>
537inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
538operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) {
539 return __is >> (char(&)[_Np])__buf;
540}
541
542template <class _Traits, size_t _Np>
543inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
544operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) {
545 return __is >> (char(&)[_Np])__buf;
546}
547
548# else
549
550template <class _CharT, class _Traits>
551inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
552operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) {
553 streamsize __n = __is.width();
554 if (__n <= 0)
555 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
556 return std::__input_c_string(__is, __s, size_t(__n));
557}
558
559template <class _Traits>
560inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
561operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) {
562 return __is >> (char*)__s;
563}
564
565template <class _Traits>
566inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
567operator>>(basic_istream<char, _Traits>& __is, signed char* __s) {
568 return __is >> (char*)__s;
569}
570
571# endif // _LIBCPP_STD_VER >= 20
572
573template <class _CharT, class _Traits>
574_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) {
575 ios_base::iostate __state = ios_base::goodbit;
576 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
577 if (__sen) {
578# if _LIBCPP_HAS_EXCEPTIONS
579 try {
580# endif
581 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
582 if (_Traits::eq_int_type(__i, _Traits::eof()))
583 __state |= ios_base::eofbit | ios_base::failbit;
584 else
585 __c = _Traits::to_char_type(__i);
586# if _LIBCPP_HAS_EXCEPTIONS
587 } catch (...) {
588 __state |= ios_base::badbit;
589 __is.__setstate_nothrow(__state);
590 if (__is.exceptions() & ios_base::badbit) {
591 throw;
592 }
593 }
594# endif
595 __is.setstate(__state);
596 }
597 return __is;
598}
599
600template <class _Traits>
601inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
602operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c) {
603 return __is >> (char&)__c;
604}
605
606template <class _Traits>
607inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
608operator>>(basic_istream<char, _Traits>& __is, signed char& __c) {
609 return __is >> (char&)__c;
610}
611
612template <class _CharT, class _Traits>
613basic_istream<_CharT, _Traits>&
614basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb) {
615 ios_base::iostate __state = ios_base::goodbit;
616 __gc_ = 0;
617 sentry __s(*this, true);
618 if (__s) {
619 if (__sb) {
620# if _LIBCPP_HAS_EXCEPTIONS
621 try {
622# endif // _LIBCPP_HAS_EXCEPTIONS
623 while (true) {
624 typename traits_type::int_type __i = this->rdbuf()->sgetc();
625 if (traits_type::eq_int_type(__i, _Traits::eof())) {
626 __state |= ios_base::eofbit;
627 break;
628 }
629 if (traits_type::eq_int_type(__sb->sputc(traits_type::to_char_type(__i)), traits_type::eof()))
630 break;
631 __inc_gcount();
632 this->rdbuf()->sbumpc();
633 }
634 if (__gc_ == 0)
635 __state |= ios_base::failbit;
636# if _LIBCPP_HAS_EXCEPTIONS
637 } catch (...) {
638 __state |= ios_base::badbit;
639 if (__gc_ == 0)
640 __state |= ios_base::failbit;
641
642 this->__setstate_nothrow(__state);
643 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit) {
644 throw;
645 }
646 }
647# endif // _LIBCPP_HAS_EXCEPTIONS
648 } else {
649 __state |= ios_base::failbit;
650 }
651 this->setstate(__state);
652 }
653 return *this;
654}
655
656template <class _CharT, class _Traits>
657typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::get() {
658 ios_base::iostate __state = ios_base::goodbit;
659 __gc_ = 0;
660 int_type __r = traits_type::eof();
661 sentry __s(*this, true);
662 if (__s) {
663# if _LIBCPP_HAS_EXCEPTIONS
664 try {
665# endif
666 __r = this->rdbuf()->sbumpc();
667 if (traits_type::eq_int_type(__r, traits_type::eof()))
668 __state |= ios_base::failbit | ios_base::eofbit;
669 else
670 __gc_ = 1;
671# if _LIBCPP_HAS_EXCEPTIONS
672 } catch (...) {
673 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
674 if (this->exceptions() & ios_base::badbit) {
675 throw;
676 }
677 }
678# endif
679 this->setstate(__state);
680 }
681 return __r;
682}
683
684template <class _CharT, class _Traits>
685basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm) {
686 ios_base::iostate __state = ios_base::goodbit;
687 __gc_ = 0;
688 sentry __sen(*this, true);
689 if (__sen) {
690 if (__n > 0) {
691# if _LIBCPP_HAS_EXCEPTIONS
692 try {
693# endif
694 while (__gc_ < __n - 1) {
695 int_type __i = this->rdbuf()->sgetc();
696 if (traits_type::eq_int_type(__i, traits_type::eof())) {
697 __state |= ios_base::eofbit;
698 break;
699 }
700 char_type __ch = traits_type::to_char_type(__i);
701 if (traits_type::eq(__ch, __dlm))
702 break;
703 *__s++ = __ch;
704 __inc_gcount();
705 this->rdbuf()->sbumpc();
706 }
707 if (__gc_ == 0)
708 __state |= ios_base::failbit;
709# if _LIBCPP_HAS_EXCEPTIONS
710 } catch (...) {
711 __state |= ios_base::badbit;
712 this->__setstate_nothrow(__state);
713 if (this->exceptions() & ios_base::badbit) {
714 if (__n > 0)
715 *__s = char_type();
716 throw;
717 }
718 }
719# endif
720 } else {
721 __state |= ios_base::failbit;
722 }
723
724 if (__n > 0)
725 *__s = char_type();
726 this->setstate(__state);
727 }
728 if (__n > 0)
729 *__s = char_type();
730 return *this;
731}
732
733template <class _CharT, class _Traits>
734basic_istream<_CharT, _Traits>&
735basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm) {
736 ios_base::iostate __state = ios_base::goodbit;
737 __gc_ = 0;
738 sentry __sen(*this, true);
739 if (__sen) {
740# if _LIBCPP_HAS_EXCEPTIONS
741 try {
742# endif // _LIBCPP_HAS_EXCEPTIONS
743 while (true) {
744 typename traits_type::int_type __i = this->rdbuf()->sgetc();
745 if (traits_type::eq_int_type(__i, traits_type::eof())) {
746 __state |= ios_base::eofbit;
747 break;
748 }
749 char_type __ch = traits_type::to_char_type(__i);
750 if (traits_type::eq(__ch, __dlm))
751 break;
752 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
753 break;
754 __inc_gcount();
755 this->rdbuf()->sbumpc();
756 }
757# if _LIBCPP_HAS_EXCEPTIONS
758 } catch (...) {
759 __state |= ios_base::badbit;
760 // according to the spec, exceptions here are caught but not rethrown
761 }
762# endif // _LIBCPP_HAS_EXCEPTIONS
763 if (__gc_ == 0)
764 __state |= ios_base::failbit;
765 this->setstate(__state);
766 }
767 return *this;
768}
769
770template <class _CharT, class _Traits>
771basic_istream<_CharT, _Traits>&
772basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm) {
773 ios_base::iostate __state = ios_base::goodbit;
774 __gc_ = 0;
775 sentry __sen(*this, true);
776 if (__sen) {
777# if _LIBCPP_HAS_EXCEPTIONS
778 try {
779# endif // _LIBCPP_HAS_EXCEPTIONS
780 while (true) {
781 typename traits_type::int_type __i = this->rdbuf()->sgetc();
782 if (traits_type::eq_int_type(__i, traits_type::eof())) {
783 __state |= ios_base::eofbit;
784 break;
785 }
786 char_type __ch = traits_type::to_char_type(__i);
787 if (traits_type::eq(__ch, __dlm)) {
788 this->rdbuf()->sbumpc();
789 __inc_gcount();
790 break;
791 }
792 if (__gc_ >= __n - 1) {
793 __state |= ios_base::failbit;
794 break;
795 }
796 *__s++ = __ch;
797 this->rdbuf()->sbumpc();
798 __inc_gcount();
799 }
800# if _LIBCPP_HAS_EXCEPTIONS
801 } catch (...) {
802 __state |= ios_base::badbit;
803 this->__setstate_nothrow(__state);
804 if (this->exceptions() & ios_base::badbit) {
805 if (__n > 0)
806 *__s = char_type();
807 if (__gc_ == 0)
808 __state |= ios_base::failbit;
809 throw;
810 }
811 }
812# endif // _LIBCPP_HAS_EXCEPTIONS
813 }
814 if (__n > 0)
815 *__s = char_type();
816 if (__gc_ == 0)
817 __state |= ios_base::failbit;
818 this->setstate(__state);
819 return *this;
820}
821
822template <class _CharT, class _Traits>
823basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm) {
824 ios_base::iostate __state = ios_base::goodbit;
825 __gc_ = 0;
826 sentry __sen(*this, true);
827 if (__sen) {
828# if _LIBCPP_HAS_EXCEPTIONS
829 try {
830# endif // _LIBCPP_HAS_EXCEPTIONS
831 if (__n == numeric_limits<streamsize>::max()) {
832 while (true) {
833 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
834 if (traits_type::eq_int_type(__i, traits_type::eof())) {
835 __state |= ios_base::eofbit;
836 break;
837 }
838 __inc_gcount();
839 if (traits_type::eq_int_type(__i, __dlm))
840 break;
841 }
842 } else {
843 while (__gc_ < __n) {
844 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
845 if (traits_type::eq_int_type(__i, traits_type::eof())) {
846 __state |= ios_base::eofbit;
847 break;
848 }
849 __inc_gcount();
850 if (traits_type::eq_int_type(__i, __dlm))
851 break;
852 }
853 }
854# if _LIBCPP_HAS_EXCEPTIONS
855 } catch (...) {
856 __state |= ios_base::badbit;
857 this->__setstate_nothrow(__state);
858 if (this->exceptions() & ios_base::badbit) {
859 throw;
860 }
861 }
862# endif // _LIBCPP_HAS_EXCEPTIONS
863 this->setstate(__state);
864 }
865 return *this;
866}
867
868template <class _CharT, class _Traits>
869typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::peek() {
870 ios_base::iostate __state = ios_base::goodbit;
871 __gc_ = 0;
872 int_type __r = traits_type::eof();
873 sentry __sen(*this, true);
874 if (__sen) {
875# if _LIBCPP_HAS_EXCEPTIONS
876 try {
877# endif // _LIBCPP_HAS_EXCEPTIONS
878 __r = this->rdbuf()->sgetc();
879 if (traits_type::eq_int_type(__r, traits_type::eof()))
880 __state |= ios_base::eofbit;
881# if _LIBCPP_HAS_EXCEPTIONS
882 } catch (...) {
883 __state |= ios_base::badbit;
884 this->__setstate_nothrow(__state);
885 if (this->exceptions() & ios_base::badbit) {
886 throw;
887 }
888 }
889# endif // _LIBCPP_HAS_EXCEPTIONS
890 this->setstate(__state);
891 }
892 return __r;
893}
894
895template <class _CharT, class _Traits>
896basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
897 ios_base::iostate __state = ios_base::goodbit;
898 __gc_ = 0;
899 sentry __sen(*this, true);
900 if (__sen) {
901# if _LIBCPP_HAS_EXCEPTIONS
902 try {
903# endif // _LIBCPP_HAS_EXCEPTIONS
904 __gc_ = this->rdbuf()->sgetn(__s, __n);
905 if (__gc_ != __n)
906 __state |= ios_base::failbit | ios_base::eofbit;
907# if _LIBCPP_HAS_EXCEPTIONS
908 } catch (...) {
909 __state |= ios_base::badbit;
910 this->__setstate_nothrow(__state);
911 if (this->exceptions() & ios_base::badbit) {
912 throw;
913 }
914 }
915# endif // _LIBCPP_HAS_EXCEPTIONS
916 } else {
917 __state |= ios_base::failbit;
918 }
919 this->setstate(__state);
920 return *this;
921}
922
923template <class _CharT, class _Traits>
924streamsize basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n) {
925 ios_base::iostate __state = ios_base::goodbit;
926 __gc_ = 0;
927 sentry __sen(*this, true);
928 if (__sen) {
929# if _LIBCPP_HAS_EXCEPTIONS
930 try {
931# endif // _LIBCPP_HAS_EXCEPTIONS
932 streamsize __c = this->rdbuf()->in_avail();
933 switch (__c) {
934 case -1:
935 __state |= ios_base::eofbit;
936 break;
937 case 0:
938 break;
939 default:
940 __n = std::min(a: __c, b: __n);
941 __gc_ = this->rdbuf()->sgetn(__s, __n);
942 if (__gc_ != __n)
943 __state |= ios_base::failbit | ios_base::eofbit;
944 break;
945 }
946# if _LIBCPP_HAS_EXCEPTIONS
947 } catch (...) {
948 __state |= ios_base::badbit;
949 this->__setstate_nothrow(__state);
950 if (this->exceptions() & ios_base::badbit) {
951 throw;
952 }
953 }
954# endif // _LIBCPP_HAS_EXCEPTIONS
955 } else {
956 __state |= ios_base::failbit;
957 }
958 this->setstate(__state);
959 return __gc_;
960}
961
962template <class _CharT, class _Traits>
963basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::putback(char_type __c) {
964 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
965 __gc_ = 0;
966 this->clear(__state);
967 sentry __sen(*this, true);
968 if (__sen) {
969# if _LIBCPP_HAS_EXCEPTIONS
970 try {
971# endif // _LIBCPP_HAS_EXCEPTIONS
972 if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
973 __state |= ios_base::badbit;
974# if _LIBCPP_HAS_EXCEPTIONS
975 } catch (...) {
976 __state |= ios_base::badbit;
977 this->__setstate_nothrow(__state);
978 if (this->exceptions() & ios_base::badbit) {
979 throw;
980 }
981 }
982# endif // _LIBCPP_HAS_EXCEPTIONS
983 } else {
984 __state |= ios_base::failbit;
985 }
986 this->setstate(__state);
987 return *this;
988}
989
990template <class _CharT, class _Traits>
991basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
992 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
993 __gc_ = 0;
994 this->clear(__state);
995 sentry __sen(*this, true);
996 if (__sen) {
997# if _LIBCPP_HAS_EXCEPTIONS
998 try {
999# endif // _LIBCPP_HAS_EXCEPTIONS
1000 if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
1001 __state |= ios_base::badbit;
1002# if _LIBCPP_HAS_EXCEPTIONS
1003 } catch (...) {
1004 __state |= ios_base::badbit;
1005 this->__setstate_nothrow(__state);
1006 if (this->exceptions() & ios_base::badbit) {
1007 throw;
1008 }
1009 }
1010# endif // _LIBCPP_HAS_EXCEPTIONS
1011 } else {
1012 __state |= ios_base::failbit;
1013 }
1014 this->setstate(__state);
1015 return *this;
1016}
1017
1018template <class _CharT, class _Traits>
1019int basic_istream<_CharT, _Traits>::sync() {
1020 ios_base::iostate __state = ios_base::goodbit;
1021 sentry __sen(*this, true);
1022 if (this->rdbuf() == nullptr)
1023 return -1;
1024
1025 int __r = 0;
1026 if (__sen) {
1027# if _LIBCPP_HAS_EXCEPTIONS
1028 try {
1029# endif // _LIBCPP_HAS_EXCEPTIONS
1030 if (this->rdbuf()->pubsync() == -1) {
1031 __state |= ios_base::badbit;
1032 __r = -1;
1033 }
1034# if _LIBCPP_HAS_EXCEPTIONS
1035 } catch (...) {
1036 __state |= ios_base::badbit;
1037 this->__setstate_nothrow(__state);
1038 if (this->exceptions() & ios_base::badbit) {
1039 throw;
1040 }
1041 }
1042# endif // _LIBCPP_HAS_EXCEPTIONS
1043 this->setstate(__state);
1044 }
1045 return __r;
1046}
1047
1048template <class _CharT, class _Traits>
1049typename basic_istream<_CharT, _Traits>::pos_type basic_istream<_CharT, _Traits>::tellg() {
1050 ios_base::iostate __state = ios_base::goodbit;
1051 pos_type __r(-1);
1052 sentry __sen(*this, true);
1053 if (__sen) {
1054# if _LIBCPP_HAS_EXCEPTIONS
1055 try {
1056# endif // _LIBCPP_HAS_EXCEPTIONS
1057 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1058# if _LIBCPP_HAS_EXCEPTIONS
1059 } catch (...) {
1060 __state |= ios_base::badbit;
1061 this->__setstate_nothrow(__state);
1062 if (this->exceptions() & ios_base::badbit) {
1063 throw;
1064 }
1065 }
1066# endif // _LIBCPP_HAS_EXCEPTIONS
1067 this->setstate(__state);
1068 }
1069 return __r;
1070}
1071
1072template <class _CharT, class _Traits>
1073basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
1074 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1075 this->clear(__state);
1076 sentry __sen(*this, true);
1077 if (__sen) {
1078# if _LIBCPP_HAS_EXCEPTIONS
1079 try {
1080# endif // _LIBCPP_HAS_EXCEPTIONS
1081 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1082 __state |= ios_base::failbit;
1083# if _LIBCPP_HAS_EXCEPTIONS
1084 } catch (...) {
1085 __state |= ios_base::badbit;
1086 this->__setstate_nothrow(__state);
1087 if (this->exceptions() & ios_base::badbit) {
1088 throw;
1089 }
1090 }
1091# endif // _LIBCPP_HAS_EXCEPTIONS
1092 this->setstate(__state);
1093 }
1094 return *this;
1095}
1096
1097template <class _CharT, class _Traits>
1098basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
1099 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1100 this->clear(__state);
1101 sentry __sen(*this, true);
1102 if (__sen) {
1103# if _LIBCPP_HAS_EXCEPTIONS
1104 try {
1105# endif // _LIBCPP_HAS_EXCEPTIONS
1106 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1107 __state |= ios_base::failbit;
1108# if _LIBCPP_HAS_EXCEPTIONS
1109 } catch (...) {
1110 __state |= ios_base::badbit;
1111 this->__setstate_nothrow(__state);
1112 if (this->exceptions() & ios_base::badbit) {
1113 throw;
1114 }
1115 }
1116# endif // _LIBCPP_HAS_EXCEPTIONS
1117 this->setstate(__state);
1118 }
1119 return *this;
1120}
1121
1122template <class _CharT, class _Traits>
1123_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& ws(basic_istream<_CharT, _Traits>& __is) {
1124 ios_base::iostate __state = ios_base::goodbit;
1125 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1126 if (__sen) {
1127# if _LIBCPP_HAS_EXCEPTIONS
1128 try {
1129# endif // _LIBCPP_HAS_EXCEPTIONS
1130 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1131 while (true) {
1132 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1133 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1134 __state |= ios_base::eofbit;
1135 break;
1136 }
1137 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1138 break;
1139 __is.rdbuf()->sbumpc();
1140 }
1141# if _LIBCPP_HAS_EXCEPTIONS
1142 } catch (...) {
1143 __state |= ios_base::badbit;
1144 __is.__setstate_nothrow(__state);
1145 if (__is.exceptions() & ios_base::badbit) {
1146 throw;
1147 }
1148 }
1149# endif // _LIBCPP_HAS_EXCEPTIONS
1150 __is.setstate(__state);
1151 }
1152 return __is;
1153}
1154
1155template <class _Stream, class _Tp, class = void>
1156struct __is_istreamable : false_type {};
1157
1158template <class _Stream, class _Tp>
1159struct __is_istreamable<_Stream, _Tp, decltype(std::declval<_Stream>() >> std::declval<_Tp>(), void())> : true_type {};
1160
1161template <class _Stream,
1162 class _Tp,
1163 __enable_if_t< _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0>
1164_LIBCPP_HIDE_FROM_ABI _Stream&& operator>>(_Stream&& __is, _Tp&& __x) {
1165 __is >> std::forward<_Tp>(__x);
1166 return std::move(__is);
1167}
1168
1169template <class _CharT, class _Traits>
1170class basic_iostream : public basic_istream<_CharT, _Traits>, public basic_ostream<_CharT, _Traits> {
1171public:
1172 // types:
1173 typedef _CharT char_type;
1174 typedef _Traits traits_type;
1175 typedef typename traits_type::int_type int_type;
1176 typedef typename traits_type::pos_type pos_type;
1177 typedef typename traits_type::off_type off_type;
1178
1179 // constructor/destructor
1180 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1181 : basic_istream<_CharT, _Traits>(__sb) {}
1182
1183 ~basic_iostream() override;
1184
1185protected:
1186 inline _LIBCPP_HIDE_FROM_ABI basic_iostream(basic_iostream&& __rhs);
1187
1188 // assign/swap
1189 inline _LIBCPP_HIDE_FROM_ABI basic_iostream& operator=(basic_iostream&& __rhs);
1190
1191 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_iostream& __rhs) {
1192 basic_istream<char_type, traits_type>::swap(__rhs);
1193 }
1194};
1195
1196template <class _CharT, class _Traits>
1197basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1198 : basic_istream<_CharT, _Traits>(std::move(__rhs)) {}
1199
1200template <class _CharT, class _Traits>
1201basic_iostream<_CharT, _Traits>& basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs) {
1202 swap(__rhs);
1203 return *this;
1204}
1205
1206template <class _CharT, class _Traits>
1207basic_iostream<_CharT, _Traits>::~basic_iostream() {}
1208
1209template <class _CharT, class _Traits, class _Allocator>
1210_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1211operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1212 ios_base::iostate __state = ios_base::goodbit;
1213 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1214 if (__sen) {
1215# if _LIBCPP_HAS_EXCEPTIONS
1216 try {
1217# endif
1218 __str.clear();
1219 using _Size = typename basic_string<_CharT, _Traits, _Allocator>::size_type;
1220 streamsize const __width = __is.width();
1221 _Size const __max_size = __str.max_size();
1222 _Size __n;
1223 if (__width <= 0) {
1224 __n = __max_size;
1225 } else {
1226 __n = std::__to_unsigned_like(x: __width) < __max_size ? static_cast<_Size>(__width) : __max_size;
1227 }
1228
1229 _Size __c = 0;
1230 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1231 while (__c < __n) {
1232 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1233 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1234 __state |= ios_base::eofbit;
1235 break;
1236 }
1237 _CharT __ch = _Traits::to_char_type(__i);
1238 if (__ct.is(__ct.space, __ch))
1239 break;
1240 __str.push_back(__ch);
1241 ++__c;
1242 __is.rdbuf()->sbumpc();
1243 }
1244 __is.width(0);
1245 if (__c == 0)
1246 __state |= ios_base::failbit;
1247# if _LIBCPP_HAS_EXCEPTIONS
1248 } catch (...) {
1249 __state |= ios_base::badbit;
1250 __is.__setstate_nothrow(__state);
1251 if (__is.exceptions() & ios_base::badbit) {
1252 throw;
1253 }
1254 }
1255# endif
1256 __is.setstate(__state);
1257 }
1258 return __is;
1259}
1260
1261template <class _CharT, class _Traits, class _Allocator>
1262_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1263getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1264 ios_base::iostate __state = ios_base::goodbit;
1265 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1266 if (!__sen)
1267 return __is;
1268# if _LIBCPP_HAS_EXCEPTIONS
1269 try {
1270# endif
1271 __str.clear();
1272
1273 auto& __buffer = *__is.rdbuf();
1274
1275 auto __next = __buffer.sgetc();
1276 for (; !_Traits::eq_int_type(__next, _Traits::eof()); __next = __buffer.sgetc()) {
1277 const auto* __first = __buffer.gptr();
1278 const auto* __last = __buffer.egptr();
1279 _CharT __1buf;
1280
1281 if (__first == __last) {
1282 __1buf = __next;
1283 __first = std::addressof(__1buf);
1284 __last = std::addressof(__1buf) + 1;
1285 }
1286
1287 auto __bump_stream = [&](ptrdiff_t __diff) {
1288 if (__first == std::addressof(__1buf)) {
1289 _LIBCPP_ASSERT_INTERNAL(__diff == 0 || __diff == 1, "trying to bump stream further than buffer size");
1290 if (__diff != 0)
1291 __buffer.sbumpc();
1292 } else {
1293 __buffer.__gbump_ptrdiff(__diff);
1294 }
1295 };
1296
1297 const auto* const __match = _Traits::find(__first, __last - __first, __dlm);
1298 if (__match)
1299 __last = __match;
1300
1301 if (auto __cap = __str.max_size() - __str.size(); __cap > static_cast<size_t>(__last - __first)) {
1302 __str.append(__first, __last);
1303 __bump_stream(__last - __first);
1304
1305 if (__match) {
1306 __bump_stream(1); // Remove the matched character
1307 break;
1308 }
1309 } else {
1310 __str.append(__first, __cap);
1311 __bump_stream(__cap);
1312 __state |= ios_base::failbit;
1313 break;
1314 }
1315 }
1316
1317 if (_Traits::eq_int_type(__next, _Traits::eof()))
1318 __state |= ios_base::eofbit | (__str.empty() ? ios_base::failbit : ios_base::goodbit);
1319
1320# if _LIBCPP_HAS_EXCEPTIONS
1321 } catch (...) {
1322 __state |= ios_base::badbit;
1323 __is.__setstate_nothrow(__state);
1324 if (__is.exceptions() & ios_base::badbit) {
1325 throw;
1326 }
1327 }
1328# endif
1329 __is.setstate(__state);
1330 return __is;
1331}
1332
1333template <class _CharT, class _Traits, class _Allocator>
1334inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1335getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1336 return std::getline(__is, __str, __is.widen('\n'));
1337}
1338
1339template <class _CharT, class _Traits, class _Allocator>
1340inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1341getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1342 return std::getline(__is, __str, __dlm);
1343}
1344
1345template <class _CharT, class _Traits, class _Allocator>
1346inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1347getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1348 return std::getline(__is, __str, __is.widen('\n'));
1349}
1350
1351template <class _CharT, class _Traits, size_t _Size>
1352_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1353operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x) {
1354 ios_base::iostate __state = ios_base::goodbit;
1355 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1356 if (__sen) {
1357# if _LIBCPP_HAS_EXCEPTIONS
1358 try {
1359# endif
1360 basic_string<_CharT, _Traits> __str;
1361 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1362 size_t __c = 0;
1363 _CharT __zero = __ct.widen('0');
1364 _CharT __one = __ct.widen('1');
1365 while (__c != _Size) {
1366 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1367 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1368 __state |= ios_base::eofbit;
1369 break;
1370 }
1371 _CharT __ch = _Traits::to_char_type(__i);
1372 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1373 break;
1374 __str.push_back(__ch);
1375 ++__c;
1376 __is.rdbuf()->sbumpc();
1377 }
1378 __x = bitset<_Size>(__str);
1379 if (_Size > 0 && __c == 0)
1380 __state |= ios_base::failbit;
1381# if _LIBCPP_HAS_EXCEPTIONS
1382 } catch (...) {
1383 __state |= ios_base::badbit;
1384 __is.__setstate_nothrow(__state);
1385 if (__is.exceptions() & ios_base::badbit) {
1386 throw;
1387 }
1388 }
1389# endif
1390 __is.setstate(__state);
1391 }
1392 return __is;
1393}
1394
1395extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1396# if _LIBCPP_HAS_WIDE_CHARACTERS
1397extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1398# endif
1399extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1400
1401_LIBCPP_END_NAMESPACE_STD
1402
1403_LIBCPP_POP_MACROS
1404
1405# endif // _LIBCPP_HAS_LOCALIZATION
1406
1407# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1408# include <concepts>
1409# include <iosfwd>
1410# include <ostream>
1411# include <type_traits>
1412# endif
1413
1414#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1415
1416#endif // _LIBCPP_ISTREAM
1417