istream 47 KB

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