sstream 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 <__assert> // all public C++ headers provide the assertion handler
  151. #include <__config>
  152. #include <__utility/swap.h>
  153. #include <istream>
  154. #include <ostream>
  155. #include <string>
  156. #include <version>
  157. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  158. # pragma GCC system_header
  159. #endif
  160. _LIBCPP_PUSH_MACROS
  161. #include <__undef_macros>
  162. _LIBCPP_BEGIN_NAMESPACE_STD
  163. // basic_stringbuf
  164. template <class _CharT, class _Traits, class _Allocator>
  165. class _LIBCPP_TEMPLATE_VIS basic_stringbuf
  166. : public basic_streambuf<_CharT, _Traits>
  167. {
  168. public:
  169. typedef _CharT char_type;
  170. typedef _Traits traits_type;
  171. typedef typename traits_type::int_type int_type;
  172. typedef typename traits_type::pos_type pos_type;
  173. typedef typename traits_type::off_type off_type;
  174. typedef _Allocator allocator_type;
  175. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  176. private:
  177. string_type __str_;
  178. mutable char_type* __hm_;
  179. ios_base::openmode __mode_;
  180. public:
  181. // 30.8.2.1 [stringbuf.cons], constructors
  182. _LIBCPP_INLINE_VISIBILITY
  183. basic_stringbuf()
  184. : __hm_(nullptr), __mode_(ios_base::in | ios_base::out) {}
  185. _LIBCPP_INLINE_VISIBILITY
  186. explicit basic_stringbuf(ios_base::openmode __wch)
  187. : __hm_(nullptr), __mode_(__wch) {}
  188. _LIBCPP_INLINE_VISIBILITY
  189. explicit basic_stringbuf(const string_type& __s,
  190. ios_base::openmode __wch = ios_base::in | ios_base::out)
  191. : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch)
  192. {
  193. str(__s);
  194. }
  195. basic_stringbuf(basic_stringbuf&& __rhs);
  196. // 27.8.1.2 Assign and swap:
  197. basic_stringbuf& operator=(basic_stringbuf&& __rhs);
  198. void swap(basic_stringbuf& __rhs);
  199. // 27.8.1.3 Get and set:
  200. string_type str() const;
  201. void str(const string_type& __s);
  202. protected:
  203. // 27.8.1.4 Overridden virtual functions:
  204. virtual int_type underflow();
  205. virtual int_type pbackfail(int_type __c = traits_type::eof());
  206. virtual int_type overflow (int_type __c = traits_type::eof());
  207. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  208. ios_base::openmode __wch = ios_base::in | ios_base::out);
  209. _LIBCPP_INLINE_VISIBILITY
  210. virtual pos_type seekpos(pos_type __sp,
  211. ios_base::openmode __wch = ios_base::in | ios_base::out) {
  212. return seekoff(__sp, ios_base::beg, __wch);
  213. }
  214. };
  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. template <class _CharT, class _Traits, class _Allocator>
  302. void
  303. basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
  304. {
  305. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  306. ptrdiff_t __rbinp = -1;
  307. ptrdiff_t __rninp = -1;
  308. ptrdiff_t __reinp = -1;
  309. if (__rhs.eback() != nullptr)
  310. {
  311. __rbinp = __rhs.eback() - __p;
  312. __rninp = __rhs.gptr() - __p;
  313. __reinp = __rhs.egptr() - __p;
  314. }
  315. ptrdiff_t __rbout = -1;
  316. ptrdiff_t __rnout = -1;
  317. ptrdiff_t __reout = -1;
  318. if (__rhs.pbase() != nullptr)
  319. {
  320. __rbout = __rhs.pbase() - __p;
  321. __rnout = __rhs.pptr() - __p;
  322. __reout = __rhs.epptr() - __p;
  323. }
  324. ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  325. __p = const_cast<char_type*>(__str_.data());
  326. ptrdiff_t __lbinp = -1;
  327. ptrdiff_t __lninp = -1;
  328. ptrdiff_t __leinp = -1;
  329. if (this->eback() != nullptr)
  330. {
  331. __lbinp = this->eback() - __p;
  332. __lninp = this->gptr() - __p;
  333. __leinp = this->egptr() - __p;
  334. }
  335. ptrdiff_t __lbout = -1;
  336. ptrdiff_t __lnout = -1;
  337. ptrdiff_t __leout = -1;
  338. if (this->pbase() != nullptr)
  339. {
  340. __lbout = this->pbase() - __p;
  341. __lnout = this->pptr() - __p;
  342. __leout = this->epptr() - __p;
  343. }
  344. ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
  345. _VSTD::swap(__mode_, __rhs.__mode_);
  346. __str_.swap(__rhs.__str_);
  347. __p = const_cast<char_type*>(__str_.data());
  348. if (__rbinp != -1)
  349. this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
  350. else
  351. this->setg(nullptr, nullptr, nullptr);
  352. if (__rbout != -1)
  353. {
  354. this->setp(__p + __rbout, __p + __reout);
  355. this->__pbump(__rnout);
  356. }
  357. else
  358. this->setp(nullptr, nullptr);
  359. __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
  360. __p = const_cast<char_type*>(__rhs.__str_.data());
  361. if (__lbinp != -1)
  362. __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
  363. else
  364. __rhs.setg(nullptr, nullptr, nullptr);
  365. if (__lbout != -1)
  366. {
  367. __rhs.setp(__p + __lbout, __p + __leout);
  368. __rhs.__pbump(__lnout);
  369. }
  370. else
  371. __rhs.setp(nullptr, nullptr);
  372. __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
  373. locale __tl = __rhs.getloc();
  374. __rhs.pubimbue(this->getloc());
  375. this->pubimbue(__tl);
  376. }
  377. template <class _CharT, class _Traits, class _Allocator>
  378. inline _LIBCPP_INLINE_VISIBILITY
  379. void
  380. swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
  381. basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
  382. {
  383. __x.swap(__y);
  384. }
  385. template <class _CharT, class _Traits, class _Allocator>
  386. basic_string<_CharT, _Traits, _Allocator>
  387. basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
  388. {
  389. if (__mode_ & ios_base::out)
  390. {
  391. if (__hm_ < this->pptr())
  392. __hm_ = this->pptr();
  393. return string_type(this->pbase(), __hm_, __str_.get_allocator());
  394. }
  395. else if (__mode_ & ios_base::in)
  396. return string_type(this->eback(), this->egptr(), __str_.get_allocator());
  397. return string_type(__str_.get_allocator());
  398. }
  399. template <class _CharT, class _Traits, class _Allocator>
  400. void
  401. basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
  402. {
  403. __str_ = __s;
  404. __hm_ = nullptr;
  405. if (__mode_ & ios_base::in)
  406. {
  407. __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
  408. this->setg(const_cast<char_type*>(__str_.data()),
  409. const_cast<char_type*>(__str_.data()),
  410. __hm_);
  411. }
  412. if (__mode_ & ios_base::out)
  413. {
  414. typename string_type::size_type __sz = __str_.size();
  415. __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
  416. __str_.resize(__str_.capacity());
  417. this->setp(const_cast<char_type*>(__str_.data()),
  418. const_cast<char_type*>(__str_.data()) + __str_.size());
  419. if (__mode_ & (ios_base::app | ios_base::ate))
  420. {
  421. while (__sz > INT_MAX)
  422. {
  423. this->pbump(INT_MAX);
  424. __sz -= INT_MAX;
  425. }
  426. if (__sz > 0)
  427. this->pbump(__sz);
  428. }
  429. }
  430. }
  431. template <class _CharT, class _Traits, class _Allocator>
  432. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  433. basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
  434. {
  435. if (__hm_ < this->pptr())
  436. __hm_ = this->pptr();
  437. if (__mode_ & ios_base::in)
  438. {
  439. if (this->egptr() < __hm_)
  440. this->setg(this->eback(), this->gptr(), __hm_);
  441. if (this->gptr() < this->egptr())
  442. return traits_type::to_int_type(*this->gptr());
  443. }
  444. return traits_type::eof();
  445. }
  446. template <class _CharT, class _Traits, class _Allocator>
  447. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  448. basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
  449. {
  450. if (__hm_ < this->pptr())
  451. __hm_ = this->pptr();
  452. if (this->eback() < this->gptr())
  453. {
  454. if (traits_type::eq_int_type(__c, traits_type::eof()))
  455. {
  456. this->setg(this->eback(), this->gptr()-1, __hm_);
  457. return traits_type::not_eof(__c);
  458. }
  459. if ((__mode_ & ios_base::out) ||
  460. traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
  461. {
  462. this->setg(this->eback(), this->gptr()-1, __hm_);
  463. *this->gptr() = traits_type::to_char_type(__c);
  464. return __c;
  465. }
  466. }
  467. return traits_type::eof();
  468. }
  469. template <class _CharT, class _Traits, class _Allocator>
  470. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  471. basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
  472. {
  473. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  474. {
  475. ptrdiff_t __ninp = this->gptr() - this->eback();
  476. if (this->pptr() == this->epptr())
  477. {
  478. if (!(__mode_ & ios_base::out))
  479. return traits_type::eof();
  480. #ifndef _LIBCPP_NO_EXCEPTIONS
  481. try
  482. {
  483. #endif // _LIBCPP_NO_EXCEPTIONS
  484. ptrdiff_t __nout = this->pptr() - this->pbase();
  485. ptrdiff_t __hm = __hm_ - this->pbase();
  486. __str_.push_back(char_type());
  487. __str_.resize(__str_.capacity());
  488. char_type* __p = const_cast<char_type*>(__str_.data());
  489. this->setp(__p, __p + __str_.size());
  490. this->__pbump(__nout);
  491. __hm_ = this->pbase() + __hm;
  492. #ifndef _LIBCPP_NO_EXCEPTIONS
  493. }
  494. catch (...)
  495. {
  496. return traits_type::eof();
  497. }
  498. #endif // _LIBCPP_NO_EXCEPTIONS
  499. }
  500. __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
  501. if (__mode_ & ios_base::in)
  502. {
  503. char_type* __p = const_cast<char_type*>(__str_.data());
  504. this->setg(__p, __p + __ninp, __hm_);
  505. }
  506. return this->sputc(traits_type::to_char_type(__c));
  507. }
  508. return traits_type::not_eof(__c);
  509. }
  510. template <class _CharT, class _Traits, class _Allocator>
  511. typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
  512. basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
  513. ios_base::seekdir __way,
  514. ios_base::openmode __wch)
  515. {
  516. if (__hm_ < this->pptr())
  517. __hm_ = this->pptr();
  518. if ((__wch & (ios_base::in | ios_base::out)) == 0)
  519. return pos_type(-1);
  520. if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
  521. && __way == ios_base::cur)
  522. return pos_type(-1);
  523. const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
  524. off_type __noff;
  525. switch (__way)
  526. {
  527. case ios_base::beg:
  528. __noff = 0;
  529. break;
  530. case ios_base::cur:
  531. if (__wch & ios_base::in)
  532. __noff = this->gptr() - this->eback();
  533. else
  534. __noff = this->pptr() - this->pbase();
  535. break;
  536. case ios_base::end:
  537. __noff = __hm;
  538. break;
  539. default:
  540. return pos_type(-1);
  541. }
  542. __noff += __off;
  543. if (__noff < 0 || __hm < __noff)
  544. return pos_type(-1);
  545. if (__noff != 0)
  546. {
  547. if ((__wch & ios_base::in) && this->gptr() == nullptr)
  548. return pos_type(-1);
  549. if ((__wch & ios_base::out) && this->pptr() == nullptr)
  550. return pos_type(-1);
  551. }
  552. if (__wch & ios_base::in)
  553. this->setg(this->eback(), this->eback() + __noff, __hm_);
  554. if (__wch & ios_base::out)
  555. {
  556. this->setp(this->pbase(), this->epptr());
  557. this->pbump(__noff);
  558. }
  559. return pos_type(__noff);
  560. }
  561. // basic_istringstream
  562. template <class _CharT, class _Traits, class _Allocator>
  563. class _LIBCPP_TEMPLATE_VIS 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. // 30.8.3.1 [istringstream.cons], constructors
  578. _LIBCPP_INLINE_VISIBILITY
  579. basic_istringstream()
  580. : basic_istream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in) {}
  581. _LIBCPP_INLINE_VISIBILITY
  582. explicit basic_istringstream(ios_base::openmode __wch)
  583. : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {}
  584. _LIBCPP_INLINE_VISIBILITY
  585. explicit basic_istringstream(const string_type& __s,
  586. ios_base::openmode __wch = ios_base::in)
  587. : basic_istream<_CharT, _Traits>(&__sb_)
  588. , __sb_(__s, __wch | ios_base::in)
  589. { }
  590. _LIBCPP_INLINE_VISIBILITY
  591. basic_istringstream(basic_istringstream&& __rhs)
  592. : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
  593. , __sb_(_VSTD::move(__rhs.__sb_))
  594. {
  595. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  596. }
  597. // 27.8.2.2 Assign and swap:
  598. basic_istringstream& operator=(basic_istringstream&& __rhs) {
  599. basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  600. __sb_ = _VSTD::move(__rhs.__sb_);
  601. return *this;
  602. }
  603. _LIBCPP_INLINE_VISIBILITY
  604. void swap(basic_istringstream& __rhs) {
  605. basic_istream<char_type, traits_type>::swap(__rhs);
  606. __sb_.swap(__rhs.__sb_);
  607. }
  608. // 27.8.2.3 Members:
  609. _LIBCPP_INLINE_VISIBILITY
  610. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  611. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  612. }
  613. _LIBCPP_INLINE_VISIBILITY
  614. string_type str() const {
  615. return __sb_.str();
  616. }
  617. _LIBCPP_INLINE_VISIBILITY
  618. void str(const string_type& __s) {
  619. __sb_.str(__s);
  620. }
  621. };
  622. template <class _CharT, class _Traits, class _Allocator>
  623. inline _LIBCPP_INLINE_VISIBILITY
  624. void
  625. swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
  626. basic_istringstream<_CharT, _Traits, _Allocator>& __y)
  627. {
  628. __x.swap(__y);
  629. }
  630. // basic_ostringstream
  631. template <class _CharT, class _Traits, class _Allocator>
  632. class _LIBCPP_TEMPLATE_VIS basic_ostringstream
  633. : public basic_ostream<_CharT, _Traits>
  634. {
  635. public:
  636. typedef _CharT char_type;
  637. typedef _Traits traits_type;
  638. typedef typename traits_type::int_type int_type;
  639. typedef typename traits_type::pos_type pos_type;
  640. typedef typename traits_type::off_type off_type;
  641. typedef _Allocator allocator_type;
  642. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  643. private:
  644. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  645. public:
  646. // 30.8.4.1 [ostringstream.cons], constructors
  647. _LIBCPP_INLINE_VISIBILITY
  648. basic_ostringstream()
  649. : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::out) {}
  650. _LIBCPP_INLINE_VISIBILITY
  651. explicit basic_ostringstream(ios_base::openmode __wch)
  652. : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::out) {}
  653. _LIBCPP_INLINE_VISIBILITY
  654. explicit basic_ostringstream(const string_type& __s,
  655. ios_base::openmode __wch = ios_base::out)
  656. : basic_ostream<_CharT, _Traits>(&__sb_)
  657. , __sb_(__s, __wch | ios_base::out)
  658. { }
  659. _LIBCPP_INLINE_VISIBILITY
  660. basic_ostringstream(basic_ostringstream&& __rhs)
  661. : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs))
  662. , __sb_(_VSTD::move(__rhs.__sb_))
  663. {
  664. basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
  665. }
  666. // 27.8.2.2 Assign and swap:
  667. basic_ostringstream& operator=(basic_ostringstream&& __rhs) {
  668. basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  669. __sb_ = _VSTD::move(__rhs.__sb_);
  670. return *this;
  671. }
  672. _LIBCPP_INLINE_VISIBILITY
  673. void swap(basic_ostringstream& __rhs) {
  674. basic_ostream<char_type, traits_type>::swap(__rhs);
  675. __sb_.swap(__rhs.__sb_);
  676. }
  677. // 27.8.2.3 Members:
  678. _LIBCPP_INLINE_VISIBILITY
  679. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  680. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  681. }
  682. _LIBCPP_INLINE_VISIBILITY
  683. string_type str() const {
  684. return __sb_.str();
  685. }
  686. _LIBCPP_INLINE_VISIBILITY
  687. void str(const string_type& __s) {
  688. __sb_.str(__s);
  689. }
  690. };
  691. template <class _CharT, class _Traits, class _Allocator>
  692. inline _LIBCPP_INLINE_VISIBILITY
  693. void
  694. swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
  695. basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
  696. {
  697. __x.swap(__y);
  698. }
  699. // basic_stringstream
  700. template <class _CharT, class _Traits, class _Allocator>
  701. class _LIBCPP_TEMPLATE_VIS basic_stringstream
  702. : public basic_iostream<_CharT, _Traits>
  703. {
  704. public:
  705. typedef _CharT char_type;
  706. typedef _Traits traits_type;
  707. typedef typename traits_type::int_type int_type;
  708. typedef typename traits_type::pos_type pos_type;
  709. typedef typename traits_type::off_type off_type;
  710. typedef _Allocator allocator_type;
  711. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  712. private:
  713. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  714. public:
  715. // 30.8.5.1 [stringstream.cons], constructors
  716. _LIBCPP_INLINE_VISIBILITY
  717. basic_stringstream()
  718. : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in | ios_base::out) {}
  719. _LIBCPP_INLINE_VISIBILITY
  720. explicit basic_stringstream(ios_base::openmode __wch)
  721. : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {}
  722. _LIBCPP_INLINE_VISIBILITY
  723. explicit basic_stringstream(const string_type& __s,
  724. ios_base::openmode __wch = ios_base::in | ios_base::out)
  725. : basic_iostream<_CharT, _Traits>(&__sb_)
  726. , __sb_(__s, __wch)
  727. { }
  728. _LIBCPP_INLINE_VISIBILITY
  729. basic_stringstream(basic_stringstream&& __rhs)
  730. : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs))
  731. , __sb_(_VSTD::move(__rhs.__sb_))
  732. {
  733. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  734. }
  735. // 27.8.2.2 Assign and swap:
  736. basic_stringstream& operator=(basic_stringstream&& __rhs) {
  737. basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  738. __sb_ = _VSTD::move(__rhs.__sb_);
  739. return *this;
  740. }
  741. _LIBCPP_INLINE_VISIBILITY
  742. void swap(basic_stringstream& __rhs) {
  743. basic_iostream<char_type, traits_type>::swap(__rhs);
  744. __sb_.swap(__rhs.__sb_);
  745. }
  746. // 27.8.2.3 Members:
  747. _LIBCPP_INLINE_VISIBILITY
  748. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  749. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  750. }
  751. _LIBCPP_INLINE_VISIBILITY
  752. string_type str() const {
  753. return __sb_.str();
  754. }
  755. _LIBCPP_INLINE_VISIBILITY
  756. void str(const string_type& __s) {
  757. __sb_.str(__s);
  758. }
  759. };
  760. template <class _CharT, class _Traits, class _Allocator>
  761. inline _LIBCPP_INLINE_VISIBILITY
  762. void
  763. swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
  764. basic_stringstream<_CharT, _Traits, _Allocator>& __y)
  765. {
  766. __x.swap(__y);
  767. }
  768. #if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
  769. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>;
  770. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>;
  771. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>;
  772. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>;
  773. #endif
  774. _LIBCPP_END_NAMESPACE_STD
  775. _LIBCPP_POP_MACROS
  776. #endif // _LIBCPP_SSTREAM