istream 47 KB

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