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