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