istream 48 KB

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