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