istream 42 KB

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