sstream 29 KB

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