2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_ISTREAM
11 #define _LIBCPP_ISTREAM
16 template <class charT, class traits = char_traits<charT> >
18 : virtual public basic_ios<charT,traits>
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;
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();
33 // 27.7.1.1.2 Assign/swap:
34 basic_istream& operator=(basic_istream&& rhs);
35 void swap(basic_istream& rhs);
37 // 27.7.1.1.3 Prefix/suffix:
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);
60 // 27.7.1.3 Unformatted input:
61 streamsize gcount() const;
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);
69 basic_istream& getline(char_type* s, streamsize n);
70 basic_istream& getline(char_type* s, streamsize n, char_type delim);
72 basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
74 basic_istream& read (char_type* s, streamsize n);
75 streamsize readsome(char_type* s, streamsize n);
77 basic_istream& putback(char_type c);
78 basic_istream& unget();
82 basic_istream& seekg(pos_type);
83 basic_istream& seekg(off_type, ios_base::seekdir);
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);
93 // 27.7.1.2.3 character extraction templates:
94 template<class charT, class traits>
95 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
97 template<class traits>
98 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
100 template<class traits>
101 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
103 template<class charT, class traits>
104 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
106 template<class traits>
107 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
109 template<class traits>
110 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
112 template <class charT, class traits>
114 swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
116 typedef basic_istream<char> istream;
117 typedef basic_istream<wchar_t> wistream;
119 template <class charT, class traits = char_traits<charT> >
120 class basic_iostream :
121 public basic_istream<charT,traits>,
122 public basic_ostream<charT,traits>
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;
132 // constructor/destructor
133 explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
134 basic_iostream(basic_iostream&& rhs);
135 virtual ~basic_iostream();
138 basic_iostream& operator=(basic_iostream&& rhs);
139 void swap(basic_iostream& rhs);
142 template <class charT, class traits>
144 swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
146 typedef basic_iostream<char> iostream;
147 typedef basic_iostream<wchar_t> wiostream;
149 template <class charT, class traits>
150 basic_istream<charT,traits>&
151 ws(basic_istream<charT,traits>& is);
153 // rvalue stream extraction
154 template <class Stream, class T>
155 Stream&& operator>>(Stream&& is, T&& x);
163 #if _LIBCPP_HAS_LOCALIZATION
165 # include <__fwd/istream.h>
166 # include <__iterator/istreambuf_iterator.h>
167 # include <__ostream/basic_ostream.h>
168 # include <__type_traits/conjunction.h>
169 # include <__type_traits/enable_if.h>
170 # include <__type_traits/is_base_of.h>
171 # include <__type_traits/make_unsigned.h>
172 # include <__utility/declval.h>
173 # include <__utility/forward.h>
179 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
180 # pragma GCC system_header
184 # include <__undef_macros>
186 _LIBCPP_BEGIN_NAMESPACE_STD
188 template <class _CharT, class _Traits>
189 class _LIBCPP_TEMPLATE_VIS basic_istream : virtual public basic_ios<_CharT, _Traits> {
192 _LIBCPP_HIDE_FROM_ABI void __inc_gcount() {
193 if (__gc_ < numeric_limits<streamsize>::max())
198 // types (inherited from basic_ios (27.5.4)):
199 typedef _CharT char_type;
200 typedef _Traits traits_type;
201 typedef typename traits_type::int_type int_type;
202 typedef typename traits_type::pos_type pos_type;
203 typedef typename traits_type::off_type off_type;
205 // 27.7.1.1.1 Constructor/destructor:
206 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb)
210 ~basic_istream() override;
213 inline _LIBCPP_HIDE_FROM_ABI basic_istream(basic_istream&& __rhs);
215 // 27.7.1.1.2 Assign/swap:
216 inline _LIBCPP_HIDE_FROM_ABI basic_istream& operator=(basic_istream&& __rhs);
218 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_istream& __rhs) {
219 std::swap(__gc_, __rhs.__gc_);
220 basic_ios<char_type, traits_type>::swap(__rhs);
224 basic_istream(const basic_istream& __rhs) = delete;
225 basic_istream& operator=(const basic_istream& __rhs) = delete;
227 // 27.7.1.1.3 Prefix/suffix:
228 class _LIBCPP_TEMPLATE_VIS sentry;
230 // 27.7.1.2 Formatted input:
231 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) {
235 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream&
236 operator>>(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
241 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(ios_base& (*__pf)(ios_base&)) {
246 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
247 basic_istream& operator>>(bool& __n);
248 basic_istream& operator>>(short& __n);
249 basic_istream& operator>>(unsigned short& __n);
250 basic_istream& operator>>(int& __n);
251 basic_istream& operator>>(unsigned int& __n);
252 basic_istream& operator>>(long& __n);
253 basic_istream& operator>>(unsigned long& __n);
254 basic_istream& operator>>(long long& __n);
255 basic_istream& operator>>(unsigned long long& __n);
256 basic_istream& operator>>(float& __f);
257 basic_istream& operator>>(double& __f);
258 basic_istream& operator>>(long double& __f);
259 basic_istream& operator>>(void*& __p);
261 // 27.7.1.3 Unformatted input:
262 _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
265 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
266 int_type __ch = get();
267 if (__ch != traits_type::eof())
268 __c = traits_type::to_char_type(__ch);
272 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type* __s, streamsize __n) {
273 return get(__s, __n, this->widen('\n'));
276 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
278 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb) {
279 return get(__sb, this->widen('\n'));
282 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
284 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& getline(char_type* __s, streamsize __n) {
285 return getline(__s, __n, this->widen('\n'));
288 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
290 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
292 basic_istream& read(char_type* __s, streamsize __n);
293 streamsize readsome(char_type* __s, streamsize __n);
295 basic_istream& putback(char_type __c);
296 basic_istream& unget();
300 basic_istream& seekg(pos_type __pos);
301 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
304 template <class _CharT, class _Traits>
305 class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry {
309 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
310 // ~sentry() = default;
312 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
314 sentry(const sentry&) = delete;
315 sentry& operator=(const sentry&) = delete;
318 template <class _CharT, class _Traits>
319 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) {
323 if (!__noskipws && (__is.flags() & ios_base::skipws)) {
324 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
325 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
328 for (; __i != __eof; ++__i)
329 if (!__ct.is(__ct.space, *__i))
332 __is.setstate(ios_base::failbit | ios_base::eofbit);
336 __is.setstate(ios_base::failbit);
339 template <class _CharT, class _Traits>
340 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) {
345 template <class _CharT, class _Traits>
346 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs) {
351 template <class _CharT, class _Traits>
352 basic_istream<_CharT, _Traits>::~basic_istream() {}
354 template <class _Tp, class _CharT, class _Traits>
355 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
356 __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
357 ios_base::iostate __state = ios_base::goodbit;
358 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
360 # if _LIBCPP_HAS_EXCEPTIONS
362 # endif // _LIBCPP_HAS_EXCEPTIONS
363 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
364 typedef num_get<_CharT, _Ip> _Fp;
365 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
366 # if _LIBCPP_HAS_EXCEPTIONS
368 __state |= ios_base::badbit;
369 __is.__setstate_nothrow(__state);
370 if (__is.exceptions() & ios_base::badbit) {
375 __is.setstate(__state);
380 template <class _CharT, class _Traits>
381 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n) {
382 return std::__input_arithmetic<unsigned short>(*this, __n);
385 template <class _CharT, class _Traits>
386 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n) {
387 return std::__input_arithmetic<unsigned int>(*this, __n);
390 template <class _CharT, class _Traits>
391 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long& __n) {
392 return std::__input_arithmetic<long>(*this, __n);
395 template <class _CharT, class _Traits>
396 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n) {
397 return std::__input_arithmetic<unsigned long>(*this, __n);
400 template <class _CharT, class _Traits>
401 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long long& __n) {
402 return std::__input_arithmetic<long long>(*this, __n);
405 template <class _CharT, class _Traits>
406 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n) {
407 return std::__input_arithmetic<unsigned long long>(*this, __n);
410 template <class _CharT, class _Traits>
411 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(float& __n) {
412 return std::__input_arithmetic<float>(*this, __n);
415 template <class _CharT, class _Traits>
416 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(double& __n) {
417 return std::__input_arithmetic<double>(*this, __n);
420 template <class _CharT, class _Traits>
421 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long double& __n) {
422 return std::__input_arithmetic<long double>(*this, __n);
425 template <class _CharT, class _Traits>
426 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(bool& __n) {
427 return std::__input_arithmetic<bool>(*this, __n);
430 template <class _CharT, class _Traits>
431 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(void*& __n) {
432 return std::__input_arithmetic<void*>(*this, __n);
435 template <class _Tp, class _CharT, class _Traits>
436 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
437 __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
438 ios_base::iostate __state = ios_base::goodbit;
439 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
441 # if _LIBCPP_HAS_EXCEPTIONS
443 # endif // _LIBCPP_HAS_EXCEPTIONS
444 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
445 typedef num_get<_CharT, _Ip> _Fp;
447 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
448 if (__temp < numeric_limits<_Tp>::min()) {
449 __state |= ios_base::failbit;
450 __n = numeric_limits<_Tp>::min();
451 } else if (__temp > numeric_limits<_Tp>::max()) {
452 __state |= ios_base::failbit;
453 __n = numeric_limits<_Tp>::max();
455 __n = static_cast<_Tp>(__temp);
457 # if _LIBCPP_HAS_EXCEPTIONS
459 __state |= ios_base::badbit;
460 __is.__setstate_nothrow(__state);
461 if (__is.exceptions() & ios_base::badbit) {
465 # endif // _LIBCPP_HAS_EXCEPTIONS
466 __is.setstate(__state);
471 template <class _CharT, class _Traits>
472 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(short& __n) {
473 return std::__input_arithmetic_with_numeric_limits<short>(*this, __n);
476 template <class _CharT, class _Traits>
477 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(int& __n) {
478 return std::__input_arithmetic_with_numeric_limits<int>(*this, __n);
481 template <class _CharT, class _Traits>
482 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
483 __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) {
484 ios_base::iostate __state = ios_base::goodbit;
485 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
487 # if _LIBCPP_HAS_EXCEPTIONS
491 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
492 while (__s != __p + (__n - 1)) {
493 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
494 if (_Traits::eq_int_type(__i, _Traits::eof())) {
495 __state |= ios_base::eofbit;
498 _CharT __ch = _Traits::to_char_type(__i);
499 if (__ct.is(__ct.space, __ch))
502 __is.rdbuf()->sbumpc();
507 __state |= ios_base::failbit;
508 # if _LIBCPP_HAS_EXCEPTIONS
510 __state |= ios_base::badbit;
511 __is.__setstate_nothrow(__state);
512 if (__is.exceptions() & ios_base::badbit) {
517 __is.setstate(__state);
522 # if _LIBCPP_STD_VER >= 20
524 template <class _CharT, class _Traits, size_t _Np>
525 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
526 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) {
528 if (__is.width() > 0)
529 __n = std::min(size_t(__is.width()), _Np);
530 return std::__input_c_string(__is, __buf, __n);
533 template <class _Traits, size_t _Np>
534 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
535 operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) {
536 return __is >> (char(&)[_Np])__buf;
539 template <class _Traits, size_t _Np>
540 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
541 operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) {
542 return __is >> (char(&)[_Np])__buf;
547 template <class _CharT, class _Traits>
548 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
549 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) {
550 streamsize __n = __is.width();
552 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
553 return std::__input_c_string(__is, __s, size_t(__n));
556 template <class _Traits>
557 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
558 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) {
559 return __is >> (char*)__s;
562 template <class _Traits>
563 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
564 operator>>(basic_istream<char, _Traits>& __is, signed char* __s) {
565 return __is >> (char*)__s;
568 # endif // _LIBCPP_STD_VER >= 20
570 template <class _CharT, class _Traits>
571 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) {
572 ios_base::iostate __state = ios_base::goodbit;
573 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
575 # if _LIBCPP_HAS_EXCEPTIONS
578 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
579 if (_Traits::eq_int_type(__i, _Traits::eof()))
580 __state |= ios_base::eofbit | ios_base::failbit;
582 __c = _Traits::to_char_type(__i);
583 # if _LIBCPP_HAS_EXCEPTIONS
585 __state |= ios_base::badbit;
586 __is.__setstate_nothrow(__state);
587 if (__is.exceptions() & ios_base::badbit) {
592 __is.setstate(__state);
597 template <class _Traits>
598 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
599 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c) {
600 return __is >> (char&)__c;
603 template <class _Traits>
604 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
605 operator>>(basic_istream<char, _Traits>& __is, signed char& __c) {
606 return __is >> (char&)__c;
609 template <class _CharT, class _Traits>
610 basic_istream<_CharT, _Traits>&
611 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb) {
612 ios_base::iostate __state = ios_base::goodbit;
614 sentry __s(*this, true);
617 # if _LIBCPP_HAS_EXCEPTIONS
619 # endif // _LIBCPP_HAS_EXCEPTIONS
621 typename traits_type::int_type __i = this->rdbuf()->sgetc();
622 if (traits_type::eq_int_type(__i, _Traits::eof())) {
623 __state |= ios_base::eofbit;
626 if (traits_type::eq_int_type(__sb->sputc(traits_type::to_char_type(__i)), traits_type::eof()))
629 this->rdbuf()->sbumpc();
632 __state |= ios_base::failbit;
633 # if _LIBCPP_HAS_EXCEPTIONS
635 __state |= ios_base::badbit;
637 __state |= ios_base::failbit;
639 this->__setstate_nothrow(__state);
640 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit) {
644 # endif // _LIBCPP_HAS_EXCEPTIONS
646 __state |= ios_base::failbit;
648 this->setstate(__state);
653 template <class _CharT, class _Traits>
654 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::get() {
655 ios_base::iostate __state = ios_base::goodbit;
657 int_type __r = traits_type::eof();
658 sentry __s(*this, true);
660 # if _LIBCPP_HAS_EXCEPTIONS
663 __r = this->rdbuf()->sbumpc();
664 if (traits_type::eq_int_type(__r, traits_type::eof()))
665 __state |= ios_base::failbit | ios_base::eofbit;
668 # if _LIBCPP_HAS_EXCEPTIONS
670 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
671 if (this->exceptions() & ios_base::badbit) {
676 this->setstate(__state);
681 template <class _CharT, class _Traits>
682 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm) {
683 ios_base::iostate __state = ios_base::goodbit;
685 sentry __sen(*this, true);
688 # if _LIBCPP_HAS_EXCEPTIONS
691 while (__gc_ < __n - 1) {
692 int_type __i = this->rdbuf()->sgetc();
693 if (traits_type::eq_int_type(__i, traits_type::eof())) {
694 __state |= ios_base::eofbit;
697 char_type __ch = traits_type::to_char_type(__i);
698 if (traits_type::eq(__ch, __dlm))
702 this->rdbuf()->sbumpc();
705 __state |= ios_base::failbit;
706 # if _LIBCPP_HAS_EXCEPTIONS
708 __state |= ios_base::badbit;
709 this->__setstate_nothrow(__state);
710 if (this->exceptions() & ios_base::badbit) {
718 __state |= ios_base::failbit;
723 this->setstate(__state);
730 template <class _CharT, class _Traits>
731 basic_istream<_CharT, _Traits>&
732 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm) {
733 ios_base::iostate __state = ios_base::goodbit;
735 sentry __sen(*this, true);
737 # if _LIBCPP_HAS_EXCEPTIONS
739 # endif // _LIBCPP_HAS_EXCEPTIONS
741 typename traits_type::int_type __i = this->rdbuf()->sgetc();
742 if (traits_type::eq_int_type(__i, traits_type::eof())) {
743 __state |= ios_base::eofbit;
746 char_type __ch = traits_type::to_char_type(__i);
747 if (traits_type::eq(__ch, __dlm))
749 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
752 this->rdbuf()->sbumpc();
754 # if _LIBCPP_HAS_EXCEPTIONS
756 __state |= ios_base::badbit;
757 // according to the spec, exceptions here are caught but not rethrown
759 # endif // _LIBCPP_HAS_EXCEPTIONS
761 __state |= ios_base::failbit;
762 this->setstate(__state);
767 template <class _CharT, class _Traits>
768 basic_istream<_CharT, _Traits>&
769 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm) {
770 ios_base::iostate __state = ios_base::goodbit;
772 sentry __sen(*this, true);
774 # if _LIBCPP_HAS_EXCEPTIONS
776 # endif // _LIBCPP_HAS_EXCEPTIONS
778 typename traits_type::int_type __i = this->rdbuf()->sgetc();
779 if (traits_type::eq_int_type(__i, traits_type::eof())) {
780 __state |= ios_base::eofbit;
783 char_type __ch = traits_type::to_char_type(__i);
784 if (traits_type::eq(__ch, __dlm)) {
785 this->rdbuf()->sbumpc();
789 if (__gc_ >= __n - 1) {
790 __state |= ios_base::failbit;
794 this->rdbuf()->sbumpc();
797 # if _LIBCPP_HAS_EXCEPTIONS
799 __state |= ios_base::badbit;
800 this->__setstate_nothrow(__state);
801 if (this->exceptions() & ios_base::badbit) {
805 __state |= ios_base::failbit;
809 # endif // _LIBCPP_HAS_EXCEPTIONS
814 __state |= ios_base::failbit;
815 this->setstate(__state);
819 template <class _CharT, class _Traits>
820 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm) {
821 ios_base::iostate __state = ios_base::goodbit;
823 sentry __sen(*this, true);
825 # if _LIBCPP_HAS_EXCEPTIONS
827 # endif // _LIBCPP_HAS_EXCEPTIONS
828 if (__n == numeric_limits<streamsize>::max()) {
830 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
831 if (traits_type::eq_int_type(__i, traits_type::eof())) {
832 __state |= ios_base::eofbit;
836 if (traits_type::eq_int_type(__i, __dlm))
840 while (__gc_ < __n) {
841 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
842 if (traits_type::eq_int_type(__i, traits_type::eof())) {
843 __state |= ios_base::eofbit;
847 if (traits_type::eq_int_type(__i, __dlm))
851 # if _LIBCPP_HAS_EXCEPTIONS
853 __state |= ios_base::badbit;
854 this->__setstate_nothrow(__state);
855 if (this->exceptions() & ios_base::badbit) {
859 # endif // _LIBCPP_HAS_EXCEPTIONS
860 this->setstate(__state);
865 template <class _CharT, class _Traits>
866 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::peek() {
867 ios_base::iostate __state = ios_base::goodbit;
869 int_type __r = traits_type::eof();
870 sentry __sen(*this, true);
872 # if _LIBCPP_HAS_EXCEPTIONS
874 # endif // _LIBCPP_HAS_EXCEPTIONS
875 __r = this->rdbuf()->sgetc();
876 if (traits_type::eq_int_type(__r, traits_type::eof()))
877 __state |= ios_base::eofbit;
878 # if _LIBCPP_HAS_EXCEPTIONS
880 __state |= ios_base::badbit;
881 this->__setstate_nothrow(__state);
882 if (this->exceptions() & ios_base::badbit) {
886 # endif // _LIBCPP_HAS_EXCEPTIONS
887 this->setstate(__state);
892 template <class _CharT, class _Traits>
893 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
894 ios_base::iostate __state = ios_base::goodbit;
896 sentry __sen(*this, true);
898 # if _LIBCPP_HAS_EXCEPTIONS
900 # endif // _LIBCPP_HAS_EXCEPTIONS
901 __gc_ = this->rdbuf()->sgetn(__s, __n);
903 __state |= ios_base::failbit | ios_base::eofbit;
904 # if _LIBCPP_HAS_EXCEPTIONS
906 __state |= ios_base::badbit;
907 this->__setstate_nothrow(__state);
908 if (this->exceptions() & ios_base::badbit) {
912 # endif // _LIBCPP_HAS_EXCEPTIONS
914 __state |= ios_base::failbit;
916 this->setstate(__state);
920 template <class _CharT, class _Traits>
921 streamsize basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n) {
922 ios_base::iostate __state = ios_base::goodbit;
924 sentry __sen(*this, true);
926 # if _LIBCPP_HAS_EXCEPTIONS
928 # endif // _LIBCPP_HAS_EXCEPTIONS
929 streamsize __c = this->rdbuf()->in_avail();
932 __state |= ios_base::eofbit;
937 __n = std::min(__c, __n);
938 __gc_ = this->rdbuf()->sgetn(__s, __n);
940 __state |= ios_base::failbit | ios_base::eofbit;
943 # if _LIBCPP_HAS_EXCEPTIONS
945 __state |= ios_base::badbit;
946 this->__setstate_nothrow(__state);
947 if (this->exceptions() & ios_base::badbit) {
951 # endif // _LIBCPP_HAS_EXCEPTIONS
953 __state |= ios_base::failbit;
955 this->setstate(__state);
959 template <class _CharT, class _Traits>
960 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::putback(char_type __c) {
961 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
963 this->clear(__state);
964 sentry __sen(*this, true);
966 # if _LIBCPP_HAS_EXCEPTIONS
968 # endif // _LIBCPP_HAS_EXCEPTIONS
969 if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
970 __state |= ios_base::badbit;
971 # if _LIBCPP_HAS_EXCEPTIONS
973 __state |= ios_base::badbit;
974 this->__setstate_nothrow(__state);
975 if (this->exceptions() & ios_base::badbit) {
979 # endif // _LIBCPP_HAS_EXCEPTIONS
981 __state |= ios_base::failbit;
983 this->setstate(__state);
987 template <class _CharT, class _Traits>
988 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
989 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
991 this->clear(__state);
992 sentry __sen(*this, true);
994 # if _LIBCPP_HAS_EXCEPTIONS
996 # endif // _LIBCPP_HAS_EXCEPTIONS
997 if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
998 __state |= ios_base::badbit;
999 # if _LIBCPP_HAS_EXCEPTIONS
1001 __state |= ios_base::badbit;
1002 this->__setstate_nothrow(__state);
1003 if (this->exceptions() & ios_base::badbit) {
1007 # endif // _LIBCPP_HAS_EXCEPTIONS
1009 __state |= ios_base::failbit;
1011 this->setstate(__state);
1015 template <class _CharT, class _Traits>
1016 int basic_istream<_CharT, _Traits>::sync() {
1017 ios_base::iostate __state = ios_base::goodbit;
1018 sentry __sen(*this, true);
1019 if (this->rdbuf() == nullptr)
1024 # if _LIBCPP_HAS_EXCEPTIONS
1026 # endif // _LIBCPP_HAS_EXCEPTIONS
1027 if (this->rdbuf()->pubsync() == -1) {
1028 __state |= ios_base::badbit;
1031 # if _LIBCPP_HAS_EXCEPTIONS
1033 __state |= ios_base::badbit;
1034 this->__setstate_nothrow(__state);
1035 if (this->exceptions() & ios_base::badbit) {
1039 # endif // _LIBCPP_HAS_EXCEPTIONS
1040 this->setstate(__state);
1045 template <class _CharT, class _Traits>
1046 typename basic_istream<_CharT, _Traits>::pos_type basic_istream<_CharT, _Traits>::tellg() {
1047 ios_base::iostate __state = ios_base::goodbit;
1049 sentry __sen(*this, true);
1051 # if _LIBCPP_HAS_EXCEPTIONS
1053 # endif // _LIBCPP_HAS_EXCEPTIONS
1054 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1055 # if _LIBCPP_HAS_EXCEPTIONS
1057 __state |= ios_base::badbit;
1058 this->__setstate_nothrow(__state);
1059 if (this->exceptions() & ios_base::badbit) {
1063 # endif // _LIBCPP_HAS_EXCEPTIONS
1064 this->setstate(__state);
1069 template <class _CharT, class _Traits>
1070 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
1071 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1072 this->clear(__state);
1073 sentry __sen(*this, true);
1075 # if _LIBCPP_HAS_EXCEPTIONS
1077 # endif // _LIBCPP_HAS_EXCEPTIONS
1078 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1079 __state |= ios_base::failbit;
1080 # if _LIBCPP_HAS_EXCEPTIONS
1082 __state |= ios_base::badbit;
1083 this->__setstate_nothrow(__state);
1084 if (this->exceptions() & ios_base::badbit) {
1088 # endif // _LIBCPP_HAS_EXCEPTIONS
1089 this->setstate(__state);
1094 template <class _CharT, class _Traits>
1095 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
1096 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1097 this->clear(__state);
1098 sentry __sen(*this, true);
1100 # if _LIBCPP_HAS_EXCEPTIONS
1102 # endif // _LIBCPP_HAS_EXCEPTIONS
1103 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1104 __state |= ios_base::failbit;
1105 # if _LIBCPP_HAS_EXCEPTIONS
1107 __state |= ios_base::badbit;
1108 this->__setstate_nothrow(__state);
1109 if (this->exceptions() & ios_base::badbit) {
1113 # endif // _LIBCPP_HAS_EXCEPTIONS
1114 this->setstate(__state);
1119 template <class _CharT, class _Traits>
1120 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& ws(basic_istream<_CharT, _Traits>& __is) {
1121 ios_base::iostate __state = ios_base::goodbit;
1122 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1124 # if _LIBCPP_HAS_EXCEPTIONS
1126 # endif // _LIBCPP_HAS_EXCEPTIONS
1127 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1129 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1130 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1131 __state |= ios_base::eofbit;
1134 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1136 __is.rdbuf()->sbumpc();
1138 # if _LIBCPP_HAS_EXCEPTIONS
1140 __state |= ios_base::badbit;
1141 __is.__setstate_nothrow(__state);
1142 if (__is.exceptions() & ios_base::badbit) {
1146 # endif // _LIBCPP_HAS_EXCEPTIONS
1147 __is.setstate(__state);
1152 template <class _Stream, class _Tp, class = void>
1153 struct __is_istreamable : false_type {};
1155 template <class _Stream, class _Tp>
1156 struct __is_istreamable<_Stream, _Tp, decltype(std::declval<_Stream>() >> std::declval<_Tp>(), void())> : true_type {};
1158 template <class _Stream,
1160 __enable_if_t< _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0>
1161 _LIBCPP_HIDE_FROM_ABI _Stream&& operator>>(_Stream&& __is, _Tp&& __x) {
1162 __is >> std::forward<_Tp>(__x);
1163 return std::move(__is);
1166 template <class _CharT, class _Traits>
1167 class _LIBCPP_TEMPLATE_VIS basic_iostream
1168 : public basic_istream<_CharT, _Traits>,
1169 public basic_ostream<_CharT, _Traits> {
1172 typedef _CharT char_type;
1173 typedef _Traits traits_type;
1174 typedef typename traits_type::int_type int_type;
1175 typedef typename traits_type::pos_type pos_type;
1176 typedef typename traits_type::off_type off_type;
1178 // constructor/destructor
1179 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1180 : basic_istream<_CharT, _Traits>(__sb) {}
1182 ~basic_iostream() override;
1185 inline _LIBCPP_HIDE_FROM_ABI basic_iostream(basic_iostream&& __rhs);
1188 inline _LIBCPP_HIDE_FROM_ABI basic_iostream& operator=(basic_iostream&& __rhs);
1190 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_iostream& __rhs) {
1191 basic_istream<char_type, traits_type>::swap(__rhs);
1195 template <class _CharT, class _Traits>
1196 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1197 : basic_istream<_CharT, _Traits>(std::move(__rhs)) {}
1199 template <class _CharT, class _Traits>
1200 basic_iostream<_CharT, _Traits>& basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs) {
1205 template <class _CharT, class _Traits>
1206 basic_iostream<_CharT, _Traits>::~basic_iostream() {}
1208 template <class _CharT, class _Traits, class _Allocator>
1209 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1210 operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1211 ios_base::iostate __state = ios_base::goodbit;
1212 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1214 # if _LIBCPP_HAS_EXCEPTIONS
1218 using _Size = typename basic_string<_CharT, _Traits, _Allocator>::size_type;
1219 streamsize const __width = __is.width();
1220 _Size const __max_size = __str.max_size();
1225 __n = std::__to_unsigned_like(__width) < __max_size ? static_cast<_Size>(__width) : __max_size;
1229 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1231 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1232 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1233 __state |= ios_base::eofbit;
1236 _CharT __ch = _Traits::to_char_type(__i);
1237 if (__ct.is(__ct.space, __ch))
1239 __str.push_back(__ch);
1241 __is.rdbuf()->sbumpc();
1245 __state |= ios_base::failbit;
1246 # if _LIBCPP_HAS_EXCEPTIONS
1248 __state |= ios_base::badbit;
1249 __is.__setstate_nothrow(__state);
1250 if (__is.exceptions() & ios_base::badbit) {
1255 __is.setstate(__state);
1260 template <class _CharT, class _Traits, class _Allocator>
1261 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1262 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1263 ios_base::iostate __state = ios_base::goodbit;
1264 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1266 # if _LIBCPP_HAS_EXCEPTIONS
1270 streamsize __extr = 0;
1272 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1273 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1274 __state |= ios_base::eofbit;
1278 _CharT __ch = _Traits::to_char_type(__i);
1279 if (_Traits::eq(__ch, __dlm))
1281 __str.push_back(__ch);
1282 if (__str.size() == __str.max_size()) {
1283 __state |= ios_base::failbit;
1288 __state |= ios_base::failbit;
1289 # if _LIBCPP_HAS_EXCEPTIONS
1291 __state |= ios_base::badbit;
1292 __is.__setstate_nothrow(__state);
1293 if (__is.exceptions() & ios_base::badbit) {
1298 __is.setstate(__state);
1303 template <class _CharT, class _Traits, class _Allocator>
1304 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1305 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1306 return std::getline(__is, __str, __is.widen('\n'));
1309 template <class _CharT, class _Traits, class _Allocator>
1310 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1311 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1312 return std::getline(__is, __str, __dlm);
1315 template <class _CharT, class _Traits, class _Allocator>
1316 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1317 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1318 return std::getline(__is, __str, __is.widen('\n'));
1321 template <class _CharT, class _Traits, size_t _Size>
1322 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1323 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x) {
1324 ios_base::iostate __state = ios_base::goodbit;
1325 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1327 # if _LIBCPP_HAS_EXCEPTIONS
1330 basic_string<_CharT, _Traits> __str;
1331 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1333 _CharT __zero = __ct.widen('0');
1334 _CharT __one = __ct.widen('1');
1335 while (__c != _Size) {
1336 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1337 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1338 __state |= ios_base::eofbit;
1341 _CharT __ch = _Traits::to_char_type(__i);
1342 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1344 __str.push_back(__ch);
1346 __is.rdbuf()->sbumpc();
1348 __x = bitset<_Size>(__str);
1349 if (_Size > 0 && __c == 0)
1350 __state |= ios_base::failbit;
1351 # if _LIBCPP_HAS_EXCEPTIONS
1353 __state |= ios_base::badbit;
1354 __is.__setstate_nothrow(__state);
1355 if (__is.exceptions() & ios_base::badbit) {
1360 __is.setstate(__state);
1365 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1366 # if _LIBCPP_HAS_WIDE_CHARACTERS
1367 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1369 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1371 _LIBCPP_END_NAMESPACE_STD
1373 #endif // _LIBCPP_HAS_LOCALIZATION
1375 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1376 # include <concepts>
1379 # include <type_traits>
1384 #endif // _LIBCPP_ISTREAM