sstream 29 KB

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