sstream 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. // -*- C++ -*-
  2. //===--------------------------- sstream ----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_SSTREAM
  11. #define _LIBCPP_SSTREAM
  12. /*
  13. sstream synopsis
  14. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  15. class basic_stringbuf
  16. : public basic_streambuf<charT, traits>
  17. {
  18. public:
  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. typedef Allocator allocator_type;
  25. // 27.8.1.1 Constructors:
  26. explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out);
  27. explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
  28. ios_base::openmode which = ios_base::in | ios_base::out);
  29. basic_stringbuf(basic_stringbuf&& rhs);
  30. // 27.8.1.2 Assign and swap:
  31. basic_stringbuf& operator=(basic_stringbuf&& rhs);
  32. void swap(basic_stringbuf& rhs);
  33. // 27.8.1.3 Get and set:
  34. basic_string<char_type, traits_type, allocator_type> str() const;
  35. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  36. protected:
  37. // 27.8.1.4 Overridden virtual functions:
  38. virtual int_type underflow();
  39. virtual int_type pbackfail(int_type c = traits_type::eof());
  40. virtual int_type overflow (int_type c = traits_type::eof());
  41. virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
  42. virtual pos_type seekoff(off_type off, ios_base::seekdir way,
  43. ios_base::openmode which = ios_base::in | ios_base::out);
  44. virtual pos_type seekpos(pos_type sp,
  45. ios_base::openmode which = ios_base::in | ios_base::out);
  46. };
  47. template <class charT, class traits, class Allocator>
  48. void swap(basic_stringbuf<charT, traits, Allocator>& x,
  49. basic_stringbuf<charT, traits, Allocator>& y);
  50. typedef basic_stringbuf<char> stringbuf;
  51. typedef basic_stringbuf<wchar_t> wstringbuf;
  52. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  53. class basic_istringstream
  54. : public basic_istream<charT, traits>
  55. {
  56. public:
  57. typedef charT char_type;
  58. typedef traits traits_type;
  59. typedef typename traits_type::int_type int_type;
  60. typedef typename traits_type::pos_type pos_type;
  61. typedef typename traits_type::off_type off_type;
  62. typedef Allocator allocator_type;
  63. // 27.8.2.1 Constructors:
  64. explicit basic_istringstream(ios_base::openmode which = ios_base::in);
  65. explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
  66. ios_base::openmode which = ios_base::in);
  67. basic_istringstream(basic_istringstream&& rhs);
  68. // 27.8.2.2 Assign and swap:
  69. basic_istringstream& operator=(basic_istringstream&& rhs);
  70. void swap(basic_istringstream& rhs);
  71. // 27.8.2.3 Members:
  72. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  73. basic_string<char_type, traits_type, allocator_type> str() const;
  74. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  75. };
  76. template <class charT, class traits, class Allocator>
  77. void swap(basic_istringstream<charT, traits, Allocator>& x,
  78. basic_istringstream<charT, traits, Allocator>& y);
  79. typedef basic_istringstream<char> istringstream;
  80. typedef basic_istringstream<wchar_t> wistringstream;
  81. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  82. class basic_ostringstream
  83. : public basic_ostream<charT, traits>
  84. {
  85. public:
  86. // types:
  87. typedef charT char_type;
  88. typedef traits traits_type;
  89. typedef typename traits_type::int_type int_type;
  90. typedef typename traits_type::pos_type pos_type;
  91. typedef typename traits_type::off_type off_type;
  92. typedef Allocator allocator_type;
  93. // 27.8.3.1 Constructors/destructor:
  94. explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
  95. explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
  96. ios_base::openmode which = ios_base::out);
  97. basic_ostringstream(basic_ostringstream&& rhs);
  98. // 27.8.3.2 Assign/swap:
  99. basic_ostringstream& operator=(basic_ostringstream&& rhs);
  100. void swap(basic_ostringstream& rhs);
  101. // 27.8.3.3 Members:
  102. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  103. basic_string<char_type, traits_type, allocator_type> str() const;
  104. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  105. };
  106. template <class charT, class traits, class Allocator>
  107. void swap(basic_ostringstream<charT, traits, Allocator>& x,
  108. basic_ostringstream<charT, traits, Allocator>& y);
  109. typedef basic_ostringstream<char> ostringstream;
  110. typedef basic_ostringstream<wchar_t> wostringstream;
  111. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  112. class basic_stringstream
  113. : public basic_iostream<charT, traits>
  114. {
  115. public:
  116. // types:
  117. typedef charT char_type;
  118. typedef traits traits_type;
  119. typedef typename traits_type::int_type int_type;
  120. typedef typename traits_type::pos_type pos_type;
  121. typedef typename traits_type::off_type off_type;
  122. typedef Allocator allocator_type;
  123. // constructors/destructor
  124. explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in);
  125. explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
  126. ios_base::openmode which = ios_base::out|ios_base::in);
  127. basic_stringstream(basic_stringstream&& rhs);
  128. // 27.8.5.1 Assign/swap:
  129. basic_stringstream& operator=(basic_stringstream&& rhs);
  130. void swap(basic_stringstream& rhs);
  131. // Members:
  132. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  133. basic_string<char_type, traits_type, allocator_type> str() const;
  134. void str(const basic_string<char_type, traits_type, allocator_type>& str);
  135. };
  136. template <class charT, class traits, class Allocator>
  137. void swap(basic_stringstream<charT, traits, Allocator>& x,
  138. basic_stringstream<charT, traits, Allocator>& y);
  139. typedef basic_stringstream<char> stringstream;
  140. typedef basic_stringstream<wchar_t> wstringstream;
  141. } // std
  142. */
  143. // Not supported in SGX.
  144. #include <__config>
  145. #if !defined(_LIBCPP_SGX_CONFIG)
  146. #include <ostream>
  147. #include <istream>
  148. #include <string>
  149. #include <__undef_min_max>
  150. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  151. #pragma GCC system_header
  152. #endif
  153. _LIBCPP_BEGIN_NAMESPACE_STD
  154. // basic_stringbuf
  155. template <class _CharT, class _Traits, class _Allocator>
  156. class _LIBCPP_TYPE_VIS_ONLY basic_stringbuf
  157. : public basic_streambuf<_CharT, _Traits>
  158. {
  159. public:
  160. typedef _CharT char_type;
  161. typedef _Traits traits_type;
  162. typedef typename traits_type::int_type int_type;
  163. typedef typename traits_type::pos_type pos_type;
  164. typedef typename traits_type::off_type off_type;
  165. typedef _Allocator allocator_type;
  166. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  167. private:
  168. string_type __str_;
  169. mutable char_type* __hm_;
  170. ios_base::openmode __mode_;
  171. public:
  172. // 27.8.1.1 Constructors:
  173. explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out);
  174. explicit basic_stringbuf(const string_type& __s,
  175. ios_base::openmode __wch = ios_base::in | ios_base::out);
  176. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  177. basic_stringbuf(basic_stringbuf&& __rhs);
  178. #endif
  179. // 27.8.1.2 Assign and swap:
  180. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  181. basic_stringbuf& operator=(basic_stringbuf&& __rhs);
  182. #endif
  183. void swap(basic_stringbuf& __rhs);
  184. // 27.8.1.3 Get and set:
  185. string_type str() const;
  186. void str(const string_type& __s);
  187. protected:
  188. // 27.8.1.4 Overridden virtual functions:
  189. virtual int_type underflow();
  190. virtual int_type pbackfail(int_type __c = traits_type::eof());
  191. virtual int_type overflow (int_type __c = traits_type::eof());
  192. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  193. ios_base::openmode __wch = ios_base::in | ios_base::out);
  194. virtual pos_type seekpos(pos_type __sp,
  195. ios_base::openmode __wch = ios_base::in | ios_base::out);
  196. };
  197. template <class _CharT, class _Traits, class _Allocator>
  198. inline _LIBCPP_INLINE_VISIBILITY
  199. basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(ios_base::openmode __wch)
  200. : __hm_(0),
  201. __mode_(__wch)
  202. {
  203. str(string_type());
  204. }
  205. template <class _CharT, class _Traits, class _Allocator>
  206. inline _LIBCPP_INLINE_VISIBILITY
  207. basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(const string_type& __s,
  208. ios_base::openmode __wch)
  209. : __hm_(0),
  210. __mode_(__wch)
  211. {
  212. str(__s);
  213. }
  214. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  215. template <class _CharT, class _Traits, class _Allocator>
  216. basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
  217. : __mode_(__rhs.__mode_)
  218. {
  219. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  220. ptrdiff_t __binp = -1;
  221. ptrdiff_t __ninp = -1;
  222. ptrdiff_t __einp = -1;
  223. if (__rhs.eback() != nullptr)
  224. {
  225. __binp = __rhs.eback() - __p;
  226. __ninp = __rhs.gptr() - __p;
  227. __einp = __rhs.egptr() - __p;
  228. }
  229. ptrdiff_t __bout = -1;
  230. ptrdiff_t __nout = -1;
  231. ptrdiff_t __eout = -1;
  232. if (__rhs.pbase() != nullptr)
  233. {
  234. __bout = __rhs.pbase() - __p;
  235. __nout = __rhs.pptr() - __p;
  236. __eout = __rhs.epptr() - __p;
  237. }
  238. ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  239. __str_ = _VSTD::move(__rhs.__str_);
  240. __p = const_cast<char_type*>(__str_.data());
  241. if (__binp != -1)
  242. this->setg(__p + __binp, __p + __ninp, __p + __einp);
  243. if (__bout != -1)
  244. {
  245. this->setp(__p + __bout, __p + __eout);
  246. this->pbump(__nout);
  247. }
  248. __hm_ = __hm == -1 ? nullptr : __p + __hm;
  249. __p = const_cast<char_type*>(__rhs.__str_.data());
  250. __rhs.setg(__p, __p, __p);
  251. __rhs.setp(__p, __p);
  252. __rhs.__hm_ = __p;
  253. this->pubimbue(__rhs.getloc());
  254. }
  255. template <class _CharT, class _Traits, class _Allocator>
  256. basic_stringbuf<_CharT, _Traits, _Allocator>&
  257. basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
  258. {
  259. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  260. ptrdiff_t __binp = -1;
  261. ptrdiff_t __ninp = -1;
  262. ptrdiff_t __einp = -1;
  263. if (__rhs.eback() != nullptr)
  264. {
  265. __binp = __rhs.eback() - __p;
  266. __ninp = __rhs.gptr() - __p;
  267. __einp = __rhs.egptr() - __p;
  268. }
  269. ptrdiff_t __bout = -1;
  270. ptrdiff_t __nout = -1;
  271. ptrdiff_t __eout = -1;
  272. if (__rhs.pbase() != nullptr)
  273. {
  274. __bout = __rhs.pbase() - __p;
  275. __nout = __rhs.pptr() - __p;
  276. __eout = __rhs.epptr() - __p;
  277. }
  278. ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  279. __str_ = _VSTD::move(__rhs.__str_);
  280. __p = const_cast<char_type*>(__str_.data());
  281. if (__binp != -1)
  282. this->setg(__p + __binp, __p + __ninp, __p + __einp);
  283. else
  284. this->setg(nullptr, nullptr, nullptr);
  285. if (__bout != -1)
  286. {
  287. this->setp(__p + __bout, __p + __eout);
  288. this->pbump(__nout);
  289. }
  290. else
  291. this->setp(nullptr, nullptr);
  292. __hm_ = __hm == -1 ? nullptr : __p + __hm;
  293. __mode_ = __rhs.__mode_;
  294. __p = const_cast<char_type*>(__rhs.__str_.data());
  295. __rhs.setg(__p, __p, __p);
  296. __rhs.setp(__p, __p);
  297. __rhs.__hm_ = __p;
  298. this->pubimbue(__rhs.getloc());
  299. return *this;
  300. }
  301. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  302. template <class _CharT, class _Traits, class _Allocator>
  303. void
  304. basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
  305. {
  306. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  307. ptrdiff_t __rbinp = -1;
  308. ptrdiff_t __rninp = -1;
  309. ptrdiff_t __reinp = -1;
  310. if (__rhs.eback() != nullptr)
  311. {
  312. __rbinp = __rhs.eback() - __p;
  313. __rninp = __rhs.gptr() - __p;
  314. __reinp = __rhs.egptr() - __p;
  315. }
  316. ptrdiff_t __rbout = -1;
  317. ptrdiff_t __rnout = -1;
  318. ptrdiff_t __reout = -1;
  319. if (__rhs.pbase() != nullptr)
  320. {
  321. __rbout = __rhs.pbase() - __p;
  322. __rnout = __rhs.pptr() - __p;
  323. __reout = __rhs.epptr() - __p;
  324. }
  325. ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  326. __p = const_cast<char_type*>(__str_.data());
  327. ptrdiff_t __lbinp = -1;
  328. ptrdiff_t __lninp = -1;
  329. ptrdiff_t __leinp = -1;
  330. if (this->eback() != nullptr)
  331. {
  332. __lbinp = this->eback() - __p;
  333. __lninp = this->gptr() - __p;
  334. __leinp = this->egptr() - __p;
  335. }
  336. ptrdiff_t __lbout = -1;
  337. ptrdiff_t __lnout = -1;
  338. ptrdiff_t __leout = -1;
  339. if (this->pbase() != nullptr)
  340. {
  341. __lbout = this->pbase() - __p;
  342. __lnout = this->pptr() - __p;
  343. __leout = this->epptr() - __p;
  344. }
  345. ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
  346. _VSTD::swap(__mode_, __rhs.__mode_);
  347. __str_.swap(__rhs.__str_);
  348. __p = const_cast<char_type*>(__str_.data());
  349. if (__rbinp != -1)
  350. this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
  351. else
  352. this->setg(nullptr, nullptr, nullptr);
  353. if (__rbout != -1)
  354. {
  355. this->setp(__p + __rbout, __p + __reout);
  356. this->pbump(__rnout);
  357. }
  358. else
  359. this->setp(nullptr, nullptr);
  360. __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
  361. __p = const_cast<char_type*>(__rhs.__str_.data());
  362. if (__lbinp != -1)
  363. __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
  364. else
  365. __rhs.setg(nullptr, nullptr, nullptr);
  366. if (__lbout != -1)
  367. {
  368. __rhs.setp(__p + __lbout, __p + __leout);
  369. __rhs.pbump(__lnout);
  370. }
  371. else
  372. __rhs.setp(nullptr, nullptr);
  373. __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
  374. locale __tl = __rhs.getloc();
  375. __rhs.pubimbue(this->getloc());
  376. this->pubimbue(__tl);
  377. }
  378. template <class _CharT, class _Traits, class _Allocator>
  379. inline _LIBCPP_INLINE_VISIBILITY
  380. void
  381. swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
  382. basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
  383. {
  384. __x.swap(__y);
  385. }
  386. template <class _CharT, class _Traits, class _Allocator>
  387. basic_string<_CharT, _Traits, _Allocator>
  388. basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
  389. {
  390. if (__mode_ & ios_base::out)
  391. {
  392. if (__hm_ < this->pptr())
  393. __hm_ = this->pptr();
  394. return string_type(this->pbase(), __hm_, __str_.get_allocator());
  395. }
  396. else if (__mode_ & ios_base::in)
  397. return string_type(this->eback(), this->egptr(), __str_.get_allocator());
  398. return string_type(__str_.get_allocator());
  399. }
  400. template <class _CharT, class _Traits, class _Allocator>
  401. void
  402. basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
  403. {
  404. __str_ = __s;
  405. __hm_ = 0;
  406. if (__mode_ & ios_base::in)
  407. {
  408. __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
  409. this->setg(const_cast<char_type*>(__str_.data()),
  410. const_cast<char_type*>(__str_.data()),
  411. __hm_);
  412. }
  413. if (__mode_ & ios_base::out)
  414. {
  415. typename string_type::size_type __sz = __str_.size();
  416. __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
  417. __str_.resize(__str_.capacity());
  418. this->setp(const_cast<char_type*>(__str_.data()),
  419. const_cast<char_type*>(__str_.data()) + __str_.size());
  420. if (__mode_ & (ios_base::app | ios_base::ate))
  421. this->pbump(__sz);
  422. }
  423. }
  424. template <class _CharT, class _Traits, class _Allocator>
  425. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  426. basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
  427. {
  428. if (__hm_ < this->pptr())
  429. __hm_ = this->pptr();
  430. if (__mode_ & ios_base::in)
  431. {
  432. if (this->egptr() < __hm_)
  433. this->setg(this->eback(), this->gptr(), __hm_);
  434. if (this->gptr() < this->egptr())
  435. return traits_type::to_int_type(*this->gptr());
  436. }
  437. return traits_type::eof();
  438. }
  439. template <class _CharT, class _Traits, class _Allocator>
  440. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  441. basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
  442. {
  443. if (__hm_ < this->pptr())
  444. __hm_ = this->pptr();
  445. if (this->eback() < this->gptr())
  446. {
  447. if (traits_type::eq_int_type(__c, traits_type::eof()))
  448. {
  449. this->setg(this->eback(), this->gptr()-1, __hm_);
  450. return traits_type::not_eof(__c);
  451. }
  452. if ((__mode_ & ios_base::out) ||
  453. traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
  454. {
  455. this->setg(this->eback(), this->gptr()-1, __hm_);
  456. *this->gptr() = traits_type::to_char_type(__c);
  457. return __c;
  458. }
  459. }
  460. return traits_type::eof();
  461. }
  462. template <class _CharT, class _Traits, class _Allocator>
  463. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  464. basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
  465. {
  466. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  467. {
  468. ptrdiff_t __ninp = this->gptr() - this->eback();
  469. if (this->pptr() == this->epptr())
  470. {
  471. if (!(__mode_ & ios_base::out))
  472. return traits_type::eof();
  473. #ifndef _LIBCPP_NO_EXCEPTIONS
  474. try
  475. {
  476. #endif // _LIBCPP_NO_EXCEPTIONS
  477. ptrdiff_t __nout = this->pptr() - this->pbase();
  478. ptrdiff_t __hm = __hm_ - this->pbase();
  479. __str_.push_back(char_type());
  480. __str_.resize(__str_.capacity());
  481. char_type* __p = const_cast<char_type*>(__str_.data());
  482. this->setp(__p, __p + __str_.size());
  483. this->pbump(__nout);
  484. __hm_ = this->pbase() + __hm;
  485. #ifndef _LIBCPP_NO_EXCEPTIONS
  486. }
  487. catch (...)
  488. {
  489. return traits_type::eof();
  490. }
  491. #endif // _LIBCPP_NO_EXCEPTIONS
  492. }
  493. __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
  494. if (__mode_ & ios_base::in)
  495. {
  496. char_type* __p = const_cast<char_type*>(__str_.data());
  497. this->setg(__p, __p + __ninp, __hm_);
  498. }
  499. return this->sputc(__c);
  500. }
  501. return traits_type::not_eof(__c);
  502. }
  503. template <class _CharT, class _Traits, class _Allocator>
  504. typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
  505. basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
  506. ios_base::seekdir __way,
  507. ios_base::openmode __wch)
  508. {
  509. if (__hm_ < this->pptr())
  510. __hm_ = this->pptr();
  511. if ((__wch & (ios_base::in | ios_base::out)) == 0)
  512. return pos_type(-1);
  513. if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
  514. && __way == ios_base::cur)
  515. return pos_type(-1);
  516. off_type __noff;
  517. switch (__way)
  518. {
  519. case ios_base::beg:
  520. __noff = 0;
  521. break;
  522. case ios_base::cur:
  523. if (__wch & ios_base::in)
  524. __noff = this->gptr() - this->eback();
  525. else
  526. __noff = this->pptr() - this->pbase();
  527. break;
  528. case ios_base::end:
  529. __noff = __hm_ - __str_.data();
  530. break;
  531. default:
  532. return pos_type(-1);
  533. }
  534. __noff += __off;
  535. if (__noff < 0 || __hm_ - __str_.data() < __noff)
  536. return pos_type(-1);
  537. if (__noff != 0)
  538. {
  539. if ((__wch & ios_base::in) && this->gptr() == 0)
  540. return pos_type(-1);
  541. if ((__wch & ios_base::out) && this->pptr() == 0)
  542. return pos_type(-1);
  543. }
  544. if (__wch & ios_base::in)
  545. this->setg(this->eback(), this->eback() + __noff, __hm_);
  546. if (__wch & ios_base::out)
  547. {
  548. this->setp(this->pbase(), this->epptr());
  549. this->pbump(__noff);
  550. }
  551. return pos_type(__noff);
  552. }
  553. template <class _CharT, class _Traits, class _Allocator>
  554. inline _LIBCPP_INLINE_VISIBILITY
  555. typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
  556. basic_stringbuf<_CharT, _Traits, _Allocator>::seekpos(pos_type __sp,
  557. ios_base::openmode __wch)
  558. {
  559. return seekoff(__sp, ios_base::beg, __wch);
  560. }
  561. // basic_istringstream
  562. template <class _CharT, class _Traits, class _Allocator>
  563. class _LIBCPP_TYPE_VIS_ONLY basic_istringstream
  564. : public basic_istream<_CharT, _Traits>
  565. {
  566. public:
  567. typedef _CharT char_type;
  568. typedef _Traits traits_type;
  569. typedef typename traits_type::int_type int_type;
  570. typedef typename traits_type::pos_type pos_type;
  571. typedef typename traits_type::off_type off_type;
  572. typedef _Allocator allocator_type;
  573. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  574. private:
  575. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  576. public:
  577. // 27.8.2.1 Constructors:
  578. explicit basic_istringstream(ios_base::openmode __wch = ios_base::in);
  579. explicit basic_istringstream(const string_type& __s,
  580. ios_base::openmode __wch = ios_base::in);
  581. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  582. basic_istringstream(basic_istringstream&& __rhs);
  583. // 27.8.2.2 Assign and swap:
  584. basic_istringstream& operator=(basic_istringstream&& __rhs);
  585. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  586. void swap(basic_istringstream& __rhs);
  587. // 27.8.2.3 Members:
  588. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  589. string_type str() const;
  590. void str(const string_type& __s);
  591. };
  592. template <class _CharT, class _Traits, class _Allocator>
  593. inline _LIBCPP_INLINE_VISIBILITY
  594. basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(ios_base::openmode __wch)
  595. : basic_istream<_CharT, _Traits>(&__sb_),
  596. __sb_(__wch | ios_base::in)
  597. {
  598. }
  599. template <class _CharT, class _Traits, class _Allocator>
  600. inline _LIBCPP_INLINE_VISIBILITY
  601. basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(const string_type& __s,
  602. ios_base::openmode __wch)
  603. : basic_istream<_CharT, _Traits>(&__sb_),
  604. __sb_(__s, __wch | ios_base::in)
  605. {
  606. }
  607. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  608. template <class _CharT, class _Traits, class _Allocator>
  609. inline _LIBCPP_INLINE_VISIBILITY
  610. basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(basic_istringstream&& __rhs)
  611. : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs)),
  612. __sb_(_VSTD::move(__rhs.__sb_))
  613. {
  614. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  615. }
  616. template <class _CharT, class _Traits, class _Allocator>
  617. basic_istringstream<_CharT, _Traits, _Allocator>&
  618. basic_istringstream<_CharT, _Traits, _Allocator>::operator=(basic_istringstream&& __rhs)
  619. {
  620. basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  621. __sb_ = _VSTD::move(__rhs.__sb_);
  622. return *this;
  623. }
  624. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  625. template <class _CharT, class _Traits, class _Allocator>
  626. inline _LIBCPP_INLINE_VISIBILITY
  627. void
  628. basic_istringstream<_CharT, _Traits, _Allocator>::swap(basic_istringstream& __rhs)
  629. {
  630. basic_istream<char_type, traits_type>::swap(__rhs);
  631. __sb_.swap(__rhs.__sb_);
  632. }
  633. template <class _CharT, class _Traits, class _Allocator>
  634. inline _LIBCPP_INLINE_VISIBILITY
  635. void
  636. swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
  637. basic_istringstream<_CharT, _Traits, _Allocator>& __y)
  638. {
  639. __x.swap(__y);
  640. }
  641. template <class _CharT, class _Traits, class _Allocator>
  642. inline _LIBCPP_INLINE_VISIBILITY
  643. basic_stringbuf<_CharT, _Traits, _Allocator>*
  644. basic_istringstream<_CharT, _Traits, _Allocator>::rdbuf() const
  645. {
  646. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  647. }
  648. template <class _CharT, class _Traits, class _Allocator>
  649. inline _LIBCPP_INLINE_VISIBILITY
  650. basic_string<_CharT, _Traits, _Allocator>
  651. basic_istringstream<_CharT, _Traits, _Allocator>::str() const
  652. {
  653. return __sb_.str();
  654. }
  655. template <class _CharT, class _Traits, class _Allocator>
  656. inline _LIBCPP_INLINE_VISIBILITY
  657. void
  658. basic_istringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
  659. {
  660. __sb_.str(__s);
  661. }
  662. // basic_ostringstream
  663. template <class _CharT, class _Traits, class _Allocator>
  664. class _LIBCPP_TYPE_VIS_ONLY basic_ostringstream
  665. : public basic_ostream<_CharT, _Traits>
  666. {
  667. public:
  668. typedef _CharT char_type;
  669. typedef _Traits traits_type;
  670. typedef typename traits_type::int_type int_type;
  671. typedef typename traits_type::pos_type pos_type;
  672. typedef typename traits_type::off_type off_type;
  673. typedef _Allocator allocator_type;
  674. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  675. private:
  676. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  677. public:
  678. // 27.8.2.1 Constructors:
  679. explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out);
  680. explicit basic_ostringstream(const string_type& __s,
  681. ios_base::openmode __wch = ios_base::out);
  682. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  683. basic_ostringstream(basic_ostringstream&& __rhs);
  684. // 27.8.2.2 Assign and swap:
  685. basic_ostringstream& operator=(basic_ostringstream&& __rhs);
  686. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  687. void swap(basic_ostringstream& __rhs);
  688. // 27.8.2.3 Members:
  689. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  690. string_type str() const;
  691. void str(const string_type& __s);
  692. };
  693. template <class _CharT, class _Traits, class _Allocator>
  694. inline _LIBCPP_INLINE_VISIBILITY
  695. basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(ios_base::openmode __wch)
  696. : basic_ostream<_CharT, _Traits>(&__sb_),
  697. __sb_(__wch | ios_base::out)
  698. {
  699. }
  700. template <class _CharT, class _Traits, class _Allocator>
  701. inline _LIBCPP_INLINE_VISIBILITY
  702. basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(const string_type& __s,
  703. ios_base::openmode __wch)
  704. : basic_ostream<_CharT, _Traits>(&__sb_),
  705. __sb_(__s, __wch | ios_base::out)
  706. {
  707. }
  708. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  709. template <class _CharT, class _Traits, class _Allocator>
  710. inline _LIBCPP_INLINE_VISIBILITY
  711. basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(basic_ostringstream&& __rhs)
  712. : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs)),
  713. __sb_(_VSTD::move(__rhs.__sb_))
  714. {
  715. basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
  716. }
  717. template <class _CharT, class _Traits, class _Allocator>
  718. basic_ostringstream<_CharT, _Traits, _Allocator>&
  719. basic_ostringstream<_CharT, _Traits, _Allocator>::operator=(basic_ostringstream&& __rhs)
  720. {
  721. basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  722. __sb_ = _VSTD::move(__rhs.__sb_);
  723. return *this;
  724. }
  725. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  726. template <class _CharT, class _Traits, class _Allocator>
  727. inline _LIBCPP_INLINE_VISIBILITY
  728. void
  729. basic_ostringstream<_CharT, _Traits, _Allocator>::swap(basic_ostringstream& __rhs)
  730. {
  731. basic_ostream<char_type, traits_type>::swap(__rhs);
  732. __sb_.swap(__rhs.__sb_);
  733. }
  734. template <class _CharT, class _Traits, class _Allocator>
  735. inline _LIBCPP_INLINE_VISIBILITY
  736. void
  737. swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
  738. basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
  739. {
  740. __x.swap(__y);
  741. }
  742. template <class _CharT, class _Traits, class _Allocator>
  743. inline _LIBCPP_INLINE_VISIBILITY
  744. basic_stringbuf<_CharT, _Traits, _Allocator>*
  745. basic_ostringstream<_CharT, _Traits, _Allocator>::rdbuf() const
  746. {
  747. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  748. }
  749. template <class _CharT, class _Traits, class _Allocator>
  750. inline _LIBCPP_INLINE_VISIBILITY
  751. basic_string<_CharT, _Traits, _Allocator>
  752. basic_ostringstream<_CharT, _Traits, _Allocator>::str() const
  753. {
  754. return __sb_.str();
  755. }
  756. template <class _CharT, class _Traits, class _Allocator>
  757. inline _LIBCPP_INLINE_VISIBILITY
  758. void
  759. basic_ostringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
  760. {
  761. __sb_.str(__s);
  762. }
  763. // basic_stringstream
  764. template <class _CharT, class _Traits, class _Allocator>
  765. class _LIBCPP_TYPE_VIS_ONLY basic_stringstream
  766. : public basic_iostream<_CharT, _Traits>
  767. {
  768. public:
  769. typedef _CharT char_type;
  770. typedef _Traits traits_type;
  771. typedef typename traits_type::int_type int_type;
  772. typedef typename traits_type::pos_type pos_type;
  773. typedef typename traits_type::off_type off_type;
  774. typedef _Allocator allocator_type;
  775. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  776. private:
  777. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  778. public:
  779. // 27.8.2.1 Constructors:
  780. explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out);
  781. explicit basic_stringstream(const string_type& __s,
  782. ios_base::openmode __wch = ios_base::in | ios_base::out);
  783. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  784. basic_stringstream(basic_stringstream&& __rhs);
  785. // 27.8.2.2 Assign and swap:
  786. basic_stringstream& operator=(basic_stringstream&& __rhs);
  787. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  788. void swap(basic_stringstream& __rhs);
  789. // 27.8.2.3 Members:
  790. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  791. string_type str() const;
  792. void str(const string_type& __s);
  793. };
  794. template <class _CharT, class _Traits, class _Allocator>
  795. inline _LIBCPP_INLINE_VISIBILITY
  796. basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(ios_base::openmode __wch)
  797. : basic_iostream<_CharT, _Traits>(&__sb_),
  798. __sb_(__wch)
  799. {
  800. }
  801. template <class _CharT, class _Traits, class _Allocator>
  802. inline _LIBCPP_INLINE_VISIBILITY
  803. basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(const string_type& __s,
  804. ios_base::openmode __wch)
  805. : basic_iostream<_CharT, _Traits>(&__sb_),
  806. __sb_(__s, __wch)
  807. {
  808. }
  809. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  810. template <class _CharT, class _Traits, class _Allocator>
  811. inline _LIBCPP_INLINE_VISIBILITY
  812. basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(basic_stringstream&& __rhs)
  813. : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs)),
  814. __sb_(_VSTD::move(__rhs.__sb_))
  815. {
  816. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  817. }
  818. template <class _CharT, class _Traits, class _Allocator>
  819. basic_stringstream<_CharT, _Traits, _Allocator>&
  820. basic_stringstream<_CharT, _Traits, _Allocator>::operator=(basic_stringstream&& __rhs)
  821. {
  822. basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  823. __sb_ = _VSTD::move(__rhs.__sb_);
  824. return *this;
  825. }
  826. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  827. template <class _CharT, class _Traits, class _Allocator>
  828. inline _LIBCPP_INLINE_VISIBILITY
  829. void
  830. basic_stringstream<_CharT, _Traits, _Allocator>::swap(basic_stringstream& __rhs)
  831. {
  832. basic_iostream<char_type, traits_type>::swap(__rhs);
  833. __sb_.swap(__rhs.__sb_);
  834. }
  835. template <class _CharT, class _Traits, class _Allocator>
  836. inline _LIBCPP_INLINE_VISIBILITY
  837. void
  838. swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
  839. basic_stringstream<_CharT, _Traits, _Allocator>& __y)
  840. {
  841. __x.swap(__y);
  842. }
  843. template <class _CharT, class _Traits, class _Allocator>
  844. inline _LIBCPP_INLINE_VISIBILITY
  845. basic_stringbuf<_CharT, _Traits, _Allocator>*
  846. basic_stringstream<_CharT, _Traits, _Allocator>::rdbuf() const
  847. {
  848. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  849. }
  850. template <class _CharT, class _Traits, class _Allocator>
  851. inline _LIBCPP_INLINE_VISIBILITY
  852. basic_string<_CharT, _Traits, _Allocator>
  853. basic_stringstream<_CharT, _Traits, _Allocator>::str() const
  854. {
  855. return __sb_.str();
  856. }
  857. template <class _CharT, class _Traits, class _Allocator>
  858. inline _LIBCPP_INLINE_VISIBILITY
  859. void
  860. basic_stringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
  861. {
  862. __sb_.str(__s);
  863. }
  864. _LIBCPP_END_NAMESPACE_STD
  865. #endif // !defined(_LIBCPP_SGX_CONFIG)
  866. #endif // _LIBCPP_SSTREAM