sstream 30 KB

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