sstream 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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>& s,
  30. ios_base::openmode which = ios_base::in | ios_base::out);
  31. explicit basic_stringbuf(const allocator_type& a)
  32. : basic_stringbuf(ios_base::in | ios_base::out, a) {} // C++20
  33. basic_stringbuf(ios_base::openmode which, const allocator_type& a); // C++20
  34. explicit basic_stringbuf(basic_string<char_type, traits_type, allocator_type>&& s,
  35. ios_base::openmode which = ios_base::in | ios_base::out); // C++20
  36. template <class SAlloc>
  37. basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a)
  38. : basic_stringbuf(s, ios_base::in | ios_base::out, a) {} // C++20
  39. template <class SAlloc>
  40. basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s,
  41. ios_base::openmode which, const allocator_type& a); // C++20
  42. template <class SAlloc>
  43. explicit basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s,
  44. ios_base::openmode which = ios_base::in | ios_base::out); // C++20
  45. basic_stringbuf(basic_stringbuf&& rhs);
  46. basic_stringbuf(basic_stringbuf&& rhs, const allocator_type& a); // C++20
  47. // [stringbuf.assign] Assign and swap:
  48. basic_stringbuf& operator=(basic_stringbuf&& rhs);
  49. void swap(basic_stringbuf& rhs) noexcept(see below); // conditionally noexcept since C++20
  50. // [stringbuf.members] Member functions:
  51. allocator_type get_allocator() const noexcept; // C++20
  52. basic_string<char_type, traits_type, allocator_type> str() const; // before C++20
  53. basic_string<char_type, traits_type, allocator_type> str() const &; // C++20
  54. template <class SAlloc>
  55. basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20
  56. basic_string<char_type, traits_type, allocator_type> str() &&; // C++20
  57. basic_string_view<char_type, traits_type> view() const noexcept; // C++20
  58. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  59. template <class SAlloc>
  60. void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20
  61. void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20
  62. protected:
  63. // [stringbuf.virtuals] Overridden virtual functions:
  64. virtual int_type underflow();
  65. virtual int_type pbackfail(int_type c = traits_type::eof());
  66. virtual int_type overflow (int_type c = traits_type::eof());
  67. virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
  68. virtual pos_type seekoff(off_type off, ios_base::seekdir way,
  69. ios_base::openmode which = ios_base::in | ios_base::out);
  70. virtual pos_type seekpos(pos_type sp,
  71. ios_base::openmode which = ios_base::in | ios_base::out);
  72. };
  73. // [stringbuf.assign] non member swap
  74. template <class charT, class traits, class Allocator>
  75. void swap(basic_stringbuf<charT, traits, Allocator>& x,
  76. basic_stringbuf<charT, traits, Allocator>& y); // conditionally noexcept since C++20
  77. typedef basic_stringbuf<char> stringbuf;
  78. typedef basic_stringbuf<wchar_t> wstringbuf;
  79. // Class template basic_istringstream [istringstream]
  80. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  81. class basic_istringstream
  82. : public basic_istream<charT, traits>
  83. {
  84. public:
  85. typedef charT char_type;
  86. typedef traits traits_type;
  87. typedef typename traits_type::int_type int_type;
  88. typedef typename traits_type::pos_type pos_type;
  89. typedef typename traits_type::off_type off_type;
  90. typedef Allocator allocator_type;
  91. // [istringstream.cons] Constructors:
  92. explicit basic_istringstream(ios_base::openmode which = ios_base::in); // before C++20
  93. basic_istringstream() : basic_istringstream(ios_base::in) {} // C++20
  94. explicit basic_istringstream(ios_base::openmode which); // C++20
  95. explicit basic_istringstream(const basic_string<char_type, traits_type, allocator_type>& s,
  96. ios_base::openmode which = ios_base::in);
  97. basic_istringstream(ios_base::openmode which, const allocator_type& a); // C++20
  98. explicit basic_istringstream(basic_string<char_type, traits_type, allocator_type>&& s,
  99. ios_base::openmode which = ios_base::in); // C++20
  100. template <class SAlloc>
  101. basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a)
  102. : basic_istringstream(s, ios_base::in, a) {} // C++20
  103. template <class SAlloc>
  104. basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  105. ios_base::openmode which, const allocator_type& a); // C++20
  106. template <class SAlloc>
  107. explicit basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  108. ios_base::openmode which = ios_base::in); // C++20
  109. basic_istringstream(basic_istringstream&& rhs);
  110. // [istringstream.assign] Assign and swap:
  111. basic_istringstream& operator=(basic_istringstream&& rhs);
  112. void swap(basic_istringstream& rhs);
  113. // [istringstream.members] Member functions:
  114. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  115. basic_string<char_type, traits_type, allocator_type> str() const; // before C++20
  116. basic_string<char_type, traits_type, allocator_type> str() const &; // C++20
  117. template <class SAlloc>
  118. basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20
  119. basic_string<char_type, traits_type, allocator_type> str() &&; // C++20
  120. basic_string_view<char_type, traits_type> view() const noexcept; // C++20
  121. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  122. template <class SAlloc>
  123. void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20
  124. void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20
  125. };
  126. template <class charT, class traits, class Allocator>
  127. void swap(basic_istringstream<charT, traits, Allocator>& x,
  128. basic_istringstream<charT, traits, Allocator>& y);
  129. typedef basic_istringstream<char> istringstream;
  130. typedef basic_istringstream<wchar_t> wistringstream;
  131. // Class template basic_ostringstream [ostringstream]
  132. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  133. class basic_ostringstream
  134. : public basic_ostream<charT, traits>
  135. {
  136. public:
  137. // types:
  138. typedef charT char_type;
  139. typedef traits traits_type;
  140. typedef typename traits_type::int_type int_type;
  141. typedef typename traits_type::pos_type pos_type;
  142. typedef typename traits_type::off_type off_type;
  143. typedef Allocator allocator_type;
  144. // [ostringstream.cons] Constructors:
  145. explicit basic_ostringstream(ios_base::openmode which = ios_base::out); // before C++20
  146. basic_ostringstream() : basic_ostringstream(ios_base::out) {} // C++20
  147. explicit basic_ostringstream(ios_base::openmode which); // C++20
  148. explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& s,
  149. ios_base::openmode which = ios_base::out);
  150. basic_ostringstream(ios_base::openmode which, const allocator_type& a); // C++20
  151. explicit basic_ostringstream(basic_string<char_type, traits_type, allocator_type>&& s,
  152. ios_base::openmode which = ios_base::out); // C++20
  153. template <class SAlloc>
  154. basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a)
  155. : basic_ostringstream(s, ios_base::out, a) {} // C++20
  156. template <class SAlloc>
  157. basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  158. ios_base::openmode which, const allocator_type& a); // C++20
  159. template <class SAlloc>
  160. explicit basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  161. ios_base::openmode which = ios_base::out); // C++20
  162. basic_ostringstream(basic_ostringstream&& rhs);
  163. // [ostringstream.assign] Assign and swap:
  164. basic_ostringstream& operator=(basic_ostringstream&& rhs);
  165. void swap(basic_ostringstream& rhs);
  166. // [ostringstream.members] Member functions:
  167. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  168. basic_string<char_type, traits_type, allocator_type> str() const; // before C++20
  169. basic_string<char_type, traits_type, allocator_type> str() const &; // C++20
  170. template <class SAlloc>
  171. basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20
  172. basic_string<char_type, traits_type, allocator_type> str() &&; // C++20
  173. basic_string_view<char_type, traits_type> view() const noexcept; // C++20
  174. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  175. template <class SAlloc>
  176. void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20
  177. void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20
  178. };
  179. template <class charT, class traits, class Allocator>
  180. void swap(basic_ostringstream<charT, traits, Allocator>& x,
  181. basic_ostringstream<charT, traits, Allocator>& y);
  182. typedef basic_ostringstream<char> ostringstream;
  183. typedef basic_ostringstream<wchar_t> wostringstream;
  184. // Class template basic_stringstream [stringstream]
  185. template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  186. class basic_stringstream
  187. : public basic_iostream<charT, traits>
  188. {
  189. public:
  190. // types:
  191. typedef charT char_type;
  192. typedef traits traits_type;
  193. typedef typename traits_type::int_type int_type;
  194. typedef typename traits_type::pos_type pos_type;
  195. typedef typename traits_type::off_type off_type;
  196. typedef Allocator allocator_type;
  197. // [stringstream.cons] constructors
  198. explicit basic_stringstream(ios_base::openmode which = ios_base::out | ios_base::in); // before C++20
  199. basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {} // C++20
  200. explicit basic_stringstream(ios_base::openmode which); // C++20
  201. explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& s,
  202. ios_base::openmode which = ios_base::out | ios_base::in);
  203. basic_stringstream(ios_base::openmode which, const allocator_type& a); // C++20
  204. explicit basic_stringstream(basic_string<char_type, traits_type, allocator_type>&& s,
  205. ios_base::openmode which = ios_base::out | ios_base::in); // C++20
  206. template <class SAlloc>
  207. basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a)
  208. : basic_stringstream(s, ios_base::out | ios_base::in, a) {} // C++20
  209. template <class SAlloc>
  210. basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  211. ios_base::openmode which, const allocator_type& a); // C++20
  212. template <class SAlloc>
  213. explicit basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s,
  214. ios_base::openmode which = ios_base::out | ios_base::in); // C++20
  215. basic_stringstream(basic_stringstream&& rhs);
  216. // [stringstream.assign] Assign and swap:
  217. basic_stringstream& operator=(basic_stringstream&& rhs);
  218. void swap(basic_stringstream& rhs);
  219. // [stringstream.members] Member functions:
  220. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
  221. basic_string<char_type, traits_type, allocator_type> str() const; // before C++20
  222. basic_string<char_type, traits_type, allocator_type> str() const &; // C++20
  223. template <class SAlloc>
  224. basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20
  225. basic_string<char_type, traits_type, allocator_type> str() &&; // C++20
  226. basic_string_view<char_type, traits_type> view() const noexcept; // C++20
  227. void str(const basic_string<char_type, traits_type, allocator_type>& s);
  228. template <class SAlloc>
  229. void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20
  230. void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20
  231. };
  232. template <class charT, class traits, class Allocator>
  233. void swap(basic_stringstream<charT, traits, Allocator>& x,
  234. basic_stringstream<charT, traits, Allocator>& y);
  235. typedef basic_stringstream<char> stringstream;
  236. typedef basic_stringstream<wchar_t> wstringstream;
  237. } // std
  238. */
  239. #include <__assert> // all public C++ headers provide the assertion handler
  240. #include <__config>
  241. #include <__fwd/sstream.h>
  242. #include <__utility/swap.h>
  243. #include <istream>
  244. #include <ostream>
  245. #include <string>
  246. #include <version>
  247. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  248. # pragma GCC system_header
  249. #endif
  250. _LIBCPP_PUSH_MACROS
  251. #include <__undef_macros>
  252. // TODO(LLVM-19): Remove this once we drop support for Clang 16,
  253. // which had this bug: https://github.com/llvm/llvm-project/issues/40363
  254. #ifdef _WIN32
  255. #define _LIBCPP_HIDE_FROM_ABI_SSTREAM _LIBCPP_ALWAYS_INLINE
  256. #else
  257. #define _LIBCPP_HIDE_FROM_ABI_SSTREAM _LIBCPP_HIDE_FROM_ABI
  258. #endif
  259. _LIBCPP_BEGIN_NAMESPACE_STD
  260. // Class template basic_stringbuf [stringbuf]
  261. template <class _CharT, class _Traits, class _Allocator>
  262. class _LIBCPP_TEMPLATE_VIS basic_stringbuf
  263. : public basic_streambuf<_CharT, _Traits>
  264. {
  265. public:
  266. typedef _CharT char_type;
  267. typedef _Traits traits_type;
  268. typedef typename traits_type::int_type int_type;
  269. typedef typename traits_type::pos_type pos_type;
  270. typedef typename traits_type::off_type off_type;
  271. typedef _Allocator allocator_type;
  272. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  273. private:
  274. string_type __str_;
  275. mutable char_type* __hm_;
  276. ios_base::openmode __mode_;
  277. _LIBCPP_HIDE_FROM_ABI void __init_buf_ptrs();
  278. _LIBCPP_HIDE_FROM_ABI void __move_init(basic_stringbuf&& __rhs);
  279. public:
  280. // [stringbuf.cons] constructors:
  281. _LIBCPP_INLINE_VISIBILITY
  282. basic_stringbuf()
  283. : __hm_(nullptr), __mode_(ios_base::in | ios_base::out) {}
  284. _LIBCPP_INLINE_VISIBILITY
  285. explicit basic_stringbuf(ios_base::openmode __wch)
  286. : __hm_(nullptr), __mode_(__wch) {}
  287. _LIBCPP_INLINE_VISIBILITY
  288. explicit basic_stringbuf(const string_type& __s,
  289. ios_base::openmode __wch = ios_base::in | ios_base::out)
  290. : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch)
  291. {
  292. str(__s);
  293. }
  294. #if _LIBCPP_STD_VER >= 20
  295. _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const allocator_type& __a)
  296. : basic_stringbuf(ios_base::in | ios_base::out, __a) {}
  297. _LIBCPP_HIDE_FROM_ABI basic_stringbuf(ios_base::openmode __wch, const allocator_type& __a)
  298. : __str_(__a), __hm_(nullptr), __mode_(__wch) {}
  299. _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(string_type&& __s,
  300. ios_base::openmode __wch = ios_base::in | ios_base::out)
  301. : __str_(std::move(__s)), __hm_(nullptr), __mode_(__wch) {
  302. __init_buf_ptrs();
  303. }
  304. template <class _SAlloc>
  305. _LIBCPP_HIDE_FROM_ABI
  306. basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s, const allocator_type& __a)
  307. : basic_stringbuf(__s, ios_base::in | ios_base::out, __a) {}
  308. template <class _SAlloc>
  309. _LIBCPP_HIDE_FROM_ABI basic_stringbuf(
  310. const basic_string<char_type, traits_type, _SAlloc>& __s, ios_base::openmode __wch, const allocator_type& __a)
  311. : __str_(__s, __a), __hm_(nullptr), __mode_(__wch) {
  312. __init_buf_ptrs();
  313. }
  314. template <class _SAlloc>
  315. requires (!is_same_v<_SAlloc, allocator_type>)
  316. _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s,
  317. ios_base::openmode __wch = ios_base::in | ios_base::out)
  318. : __str_(__s), __hm_(nullptr), __mode_(__wch) {
  319. __init_buf_ptrs();
  320. }
  321. #endif // _LIBCPP_STD_VER >= 20
  322. basic_stringbuf(basic_stringbuf&& __rhs) : __mode_(__rhs.__mode_) { __move_init(std::move(__rhs)); }
  323. #if _LIBCPP_STD_VER >= 20
  324. _LIBCPP_HIDE_FROM_ABI basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
  325. : basic_stringbuf(__rhs.__mode_, __a) {
  326. __move_init(std::move(__rhs));
  327. }
  328. #endif
  329. // [stringbuf.assign] Assign and swap:
  330. basic_stringbuf& operator=(basic_stringbuf&& __rhs);
  331. void swap(basic_stringbuf& __rhs)
  332. #if _LIBCPP_STD_VER >= 20
  333. noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
  334. allocator_traits<allocator_type>::is_always_equal::value)
  335. #endif
  336. ;
  337. // [stringbuf.members] Member functions:
  338. #if _LIBCPP_STD_VER >= 20
  339. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const noexcept { return __str_.get_allocator(); }
  340. #endif
  341. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  342. string_type str() const;
  343. #else
  344. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return str(__str_.get_allocator()); }
  345. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && {
  346. string_type __result;
  347. const basic_string_view<_CharT, _Traits> __view = view();
  348. if (!__view.empty()) {
  349. auto __pos = __view.data() - __str_.data();
  350. __result.assign(std::move(__str_), __pos, __view.size());
  351. }
  352. __str_.clear();
  353. __init_buf_ptrs();
  354. return __result;
  355. }
  356. #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  357. #if _LIBCPP_STD_VER >= 20
  358. template <class _SAlloc>
  359. requires __is_allocator<_SAlloc>::value
  360. _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const {
  361. return basic_string<_CharT, _Traits, _SAlloc>(view(), __sa);
  362. }
  363. _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept;
  364. #endif // _LIBCPP_STD_VER >= 20
  365. void str(const string_type& __s) {
  366. __str_ = __s;
  367. __init_buf_ptrs();
  368. }
  369. #if _LIBCPP_STD_VER >= 20
  370. template <class _SAlloc>
  371. requires (!is_same_v<_SAlloc, allocator_type>)
  372. _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) {
  373. __str_ = __s;
  374. __init_buf_ptrs();
  375. }
  376. _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) {
  377. __str_ = std::move(__s);
  378. __init_buf_ptrs();
  379. }
  380. #endif // _LIBCPP_STD_VER >= 20
  381. protected:
  382. // [stringbuf.virtuals] Overridden virtual functions:
  383. int_type underflow() override;
  384. int_type pbackfail(int_type __c = traits_type::eof()) override;
  385. int_type overflow (int_type __c = traits_type::eof()) override;
  386. pos_type seekoff(off_type __off, ios_base::seekdir __way,
  387. ios_base::openmode __wch = ios_base::in | ios_base::out) override;
  388. _LIBCPP_HIDE_FROM_ABI_VIRTUAL
  389. pos_type seekpos(pos_type __sp,
  390. ios_base::openmode __wch = ios_base::in | ios_base::out) override {
  391. return seekoff(__sp, ios_base::beg, __wch);
  392. }
  393. };
  394. template <class _CharT, class _Traits, class _Allocator>
  395. _LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__move_init(basic_stringbuf&& __rhs) {
  396. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  397. ptrdiff_t __binp = -1;
  398. ptrdiff_t __ninp = -1;
  399. ptrdiff_t __einp = -1;
  400. if (__rhs.eback() != nullptr)
  401. {
  402. __binp = __rhs.eback() - __p;
  403. __ninp = __rhs.gptr() - __p;
  404. __einp = __rhs.egptr() - __p;
  405. }
  406. ptrdiff_t __bout = -1;
  407. ptrdiff_t __nout = -1;
  408. ptrdiff_t __eout = -1;
  409. if (__rhs.pbase() != nullptr)
  410. {
  411. __bout = __rhs.pbase() - __p;
  412. __nout = __rhs.pptr() - __p;
  413. __eout = __rhs.epptr() - __p;
  414. }
  415. ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  416. __str_ = _VSTD::move(__rhs.__str_);
  417. __p = const_cast<char_type*>(__str_.data());
  418. if (__binp != -1)
  419. this->setg(__p + __binp, __p + __ninp, __p + __einp);
  420. if (__bout != -1)
  421. {
  422. this->setp(__p + __bout, __p + __eout);
  423. this->__pbump(__nout);
  424. }
  425. __hm_ = __hm == -1 ? nullptr : __p + __hm;
  426. __p = const_cast<char_type*>(__rhs.__str_.data());
  427. __rhs.setg(__p, __p, __p);
  428. __rhs.setp(__p, __p);
  429. __rhs.__hm_ = __p;
  430. this->pubimbue(__rhs.getloc());
  431. }
  432. template <class _CharT, class _Traits, class _Allocator>
  433. basic_stringbuf<_CharT, _Traits, _Allocator>&
  434. basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
  435. {
  436. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  437. ptrdiff_t __binp = -1;
  438. ptrdiff_t __ninp = -1;
  439. ptrdiff_t __einp = -1;
  440. if (__rhs.eback() != nullptr)
  441. {
  442. __binp = __rhs.eback() - __p;
  443. __ninp = __rhs.gptr() - __p;
  444. __einp = __rhs.egptr() - __p;
  445. }
  446. ptrdiff_t __bout = -1;
  447. ptrdiff_t __nout = -1;
  448. ptrdiff_t __eout = -1;
  449. if (__rhs.pbase() != nullptr)
  450. {
  451. __bout = __rhs.pbase() - __p;
  452. __nout = __rhs.pptr() - __p;
  453. __eout = __rhs.epptr() - __p;
  454. }
  455. ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  456. __str_ = _VSTD::move(__rhs.__str_);
  457. __p = const_cast<char_type*>(__str_.data());
  458. if (__binp != -1)
  459. this->setg(__p + __binp, __p + __ninp, __p + __einp);
  460. else
  461. this->setg(nullptr, nullptr, nullptr);
  462. if (__bout != -1)
  463. {
  464. this->setp(__p + __bout, __p + __eout);
  465. this->__pbump(__nout);
  466. }
  467. else
  468. this->setp(nullptr, nullptr);
  469. __hm_ = __hm == -1 ? nullptr : __p + __hm;
  470. __mode_ = __rhs.__mode_;
  471. __p = const_cast<char_type*>(__rhs.__str_.data());
  472. __rhs.setg(__p, __p, __p);
  473. __rhs.setp(__p, __p);
  474. __rhs.__hm_ = __p;
  475. this->pubimbue(__rhs.getloc());
  476. return *this;
  477. }
  478. template <class _CharT, class _Traits, class _Allocator>
  479. void
  480. basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
  481. #if _LIBCPP_STD_VER >= 20
  482. noexcept(allocator_traits<_Allocator>::propagate_on_container_swap::value ||
  483. allocator_traits<_Allocator>::is_always_equal::value)
  484. #endif
  485. {
  486. char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
  487. ptrdiff_t __rbinp = -1;
  488. ptrdiff_t __rninp = -1;
  489. ptrdiff_t __reinp = -1;
  490. if (__rhs.eback() != nullptr)
  491. {
  492. __rbinp = __rhs.eback() - __p;
  493. __rninp = __rhs.gptr() - __p;
  494. __reinp = __rhs.egptr() - __p;
  495. }
  496. ptrdiff_t __rbout = -1;
  497. ptrdiff_t __rnout = -1;
  498. ptrdiff_t __reout = -1;
  499. if (__rhs.pbase() != nullptr)
  500. {
  501. __rbout = __rhs.pbase() - __p;
  502. __rnout = __rhs.pptr() - __p;
  503. __reout = __rhs.epptr() - __p;
  504. }
  505. ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
  506. __p = const_cast<char_type*>(__str_.data());
  507. ptrdiff_t __lbinp = -1;
  508. ptrdiff_t __lninp = -1;
  509. ptrdiff_t __leinp = -1;
  510. if (this->eback() != nullptr)
  511. {
  512. __lbinp = this->eback() - __p;
  513. __lninp = this->gptr() - __p;
  514. __leinp = this->egptr() - __p;
  515. }
  516. ptrdiff_t __lbout = -1;
  517. ptrdiff_t __lnout = -1;
  518. ptrdiff_t __leout = -1;
  519. if (this->pbase() != nullptr)
  520. {
  521. __lbout = this->pbase() - __p;
  522. __lnout = this->pptr() - __p;
  523. __leout = this->epptr() - __p;
  524. }
  525. ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
  526. _VSTD::swap(__mode_, __rhs.__mode_);
  527. __str_.swap(__rhs.__str_);
  528. __p = const_cast<char_type*>(__str_.data());
  529. if (__rbinp != -1)
  530. this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
  531. else
  532. this->setg(nullptr, nullptr, nullptr);
  533. if (__rbout != -1)
  534. {
  535. this->setp(__p + __rbout, __p + __reout);
  536. this->__pbump(__rnout);
  537. }
  538. else
  539. this->setp(nullptr, nullptr);
  540. __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
  541. __p = const_cast<char_type*>(__rhs.__str_.data());
  542. if (__lbinp != -1)
  543. __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
  544. else
  545. __rhs.setg(nullptr, nullptr, nullptr);
  546. if (__lbout != -1)
  547. {
  548. __rhs.setp(__p + __lbout, __p + __leout);
  549. __rhs.__pbump(__lnout);
  550. }
  551. else
  552. __rhs.setp(nullptr, nullptr);
  553. __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
  554. locale __tl = __rhs.getloc();
  555. __rhs.pubimbue(this->getloc());
  556. this->pubimbue(__tl);
  557. }
  558. template <class _CharT, class _Traits, class _Allocator>
  559. inline _LIBCPP_INLINE_VISIBILITY
  560. void
  561. swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
  562. basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
  563. #if _LIBCPP_STD_VER >= 20
  564. noexcept(noexcept(__x.swap(__y)))
  565. #endif
  566. {
  567. __x.swap(__y);
  568. }
  569. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  570. template <class _CharT, class _Traits, class _Allocator>
  571. basic_string<_CharT, _Traits, _Allocator>
  572. basic_stringbuf<_CharT, _Traits, _Allocator>::str() const {
  573. if (__mode_ & ios_base::out) {
  574. if (__hm_ < this->pptr())
  575. __hm_ = this->pptr();
  576. return string_type(this->pbase(), __hm_, __str_.get_allocator());
  577. } else if (__mode_ & ios_base::in)
  578. return string_type(this->eback(), this->egptr(), __str_.get_allocator());
  579. return string_type(__str_.get_allocator());
  580. }
  581. #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  582. template <class _CharT, class _Traits, class _Allocator>
  583. _LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__init_buf_ptrs() {
  584. __hm_ = nullptr;
  585. char_type* __data = const_cast<char_type*>(__str_.data());
  586. typename string_type::size_type __sz = __str_.size();
  587. if (__mode_ & ios_base::in) {
  588. __hm_ = __data + __sz;
  589. this->setg(__data, __data, __hm_);
  590. }
  591. if (__mode_ & ios_base::out) {
  592. __hm_ = __data + __sz;
  593. __str_.resize(__str_.capacity());
  594. this->setp(__data, __data + __str_.size());
  595. if (__mode_ & (ios_base::app | ios_base::ate)) {
  596. while (__sz > INT_MAX) {
  597. this->pbump(INT_MAX);
  598. __sz -= INT_MAX;
  599. }
  600. if (__sz > 0)
  601. this->pbump(__sz);
  602. }
  603. }
  604. }
  605. #if _LIBCPP_STD_VER >= 20
  606. template <class _CharT, class _Traits, class _Allocator>
  607. _LIBCPP_HIDE_FROM_ABI basic_string_view<_CharT, _Traits>
  608. basic_stringbuf<_CharT, _Traits, _Allocator>::view() const noexcept {
  609. if (__mode_ & ios_base::out) {
  610. if (__hm_ < this->pptr())
  611. __hm_ = this->pptr();
  612. return basic_string_view<_CharT, _Traits>(this->pbase(), __hm_);
  613. } else if (__mode_ & ios_base::in)
  614. return basic_string_view<_CharT, _Traits>(this->eback(), this->egptr());
  615. return basic_string_view<_CharT, _Traits>();
  616. }
  617. #endif // _LIBCPP_STD_VER >= 20
  618. template <class _CharT, class _Traits, class _Allocator>
  619. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  620. basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
  621. {
  622. if (__hm_ < this->pptr())
  623. __hm_ = this->pptr();
  624. if (__mode_ & ios_base::in)
  625. {
  626. if (this->egptr() < __hm_)
  627. this->setg(this->eback(), this->gptr(), __hm_);
  628. if (this->gptr() < this->egptr())
  629. return traits_type::to_int_type(*this->gptr());
  630. }
  631. return traits_type::eof();
  632. }
  633. template <class _CharT, class _Traits, class _Allocator>
  634. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  635. basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
  636. {
  637. if (__hm_ < this->pptr())
  638. __hm_ = this->pptr();
  639. if (this->eback() < this->gptr())
  640. {
  641. if (traits_type::eq_int_type(__c, traits_type::eof()))
  642. {
  643. this->setg(this->eback(), this->gptr()-1, __hm_);
  644. return traits_type::not_eof(__c);
  645. }
  646. if ((__mode_ & ios_base::out) ||
  647. traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
  648. {
  649. this->setg(this->eback(), this->gptr()-1, __hm_);
  650. *this->gptr() = traits_type::to_char_type(__c);
  651. return __c;
  652. }
  653. }
  654. return traits_type::eof();
  655. }
  656. template <class _CharT, class _Traits, class _Allocator>
  657. typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
  658. basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
  659. {
  660. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  661. {
  662. ptrdiff_t __ninp = this->gptr() - this->eback();
  663. if (this->pptr() == this->epptr())
  664. {
  665. if (!(__mode_ & ios_base::out))
  666. return traits_type::eof();
  667. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  668. try
  669. {
  670. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  671. ptrdiff_t __nout = this->pptr() - this->pbase();
  672. ptrdiff_t __hm = __hm_ - this->pbase();
  673. __str_.push_back(char_type());
  674. __str_.resize(__str_.capacity());
  675. char_type* __p = const_cast<char_type*>(__str_.data());
  676. this->setp(__p, __p + __str_.size());
  677. this->__pbump(__nout);
  678. __hm_ = this->pbase() + __hm;
  679. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  680. }
  681. catch (...)
  682. {
  683. return traits_type::eof();
  684. }
  685. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  686. }
  687. __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
  688. if (__mode_ & ios_base::in)
  689. {
  690. char_type* __p = const_cast<char_type*>(__str_.data());
  691. this->setg(__p, __p + __ninp, __hm_);
  692. }
  693. return this->sputc(traits_type::to_char_type(__c));
  694. }
  695. return traits_type::not_eof(__c);
  696. }
  697. template <class _CharT, class _Traits, class _Allocator>
  698. typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
  699. basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
  700. ios_base::seekdir __way,
  701. ios_base::openmode __wch)
  702. {
  703. if (__hm_ < this->pptr())
  704. __hm_ = this->pptr();
  705. if ((__wch & (ios_base::in | ios_base::out)) == 0)
  706. return pos_type(-1);
  707. if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
  708. && __way == ios_base::cur)
  709. return pos_type(-1);
  710. const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
  711. off_type __noff;
  712. switch (__way)
  713. {
  714. case ios_base::beg:
  715. __noff = 0;
  716. break;
  717. case ios_base::cur:
  718. if (__wch & ios_base::in)
  719. __noff = this->gptr() - this->eback();
  720. else
  721. __noff = this->pptr() - this->pbase();
  722. break;
  723. case ios_base::end:
  724. __noff = __hm;
  725. break;
  726. default:
  727. return pos_type(-1);
  728. }
  729. __noff += __off;
  730. if (__noff < 0 || __hm < __noff)
  731. return pos_type(-1);
  732. if (__noff != 0)
  733. {
  734. if ((__wch & ios_base::in) && this->gptr() == nullptr)
  735. return pos_type(-1);
  736. if ((__wch & ios_base::out) && this->pptr() == nullptr)
  737. return pos_type(-1);
  738. }
  739. if (__wch & ios_base::in)
  740. this->setg(this->eback(), this->eback() + __noff, __hm_);
  741. if (__wch & ios_base::out)
  742. {
  743. this->setp(this->pbase(), this->epptr());
  744. this->__pbump(__noff);
  745. }
  746. return pos_type(__noff);
  747. }
  748. // Class template basic_istringstream [istringstream]
  749. template <class _CharT, class _Traits, class _Allocator>
  750. class _LIBCPP_TEMPLATE_VIS basic_istringstream
  751. : public basic_istream<_CharT, _Traits>
  752. {
  753. public:
  754. typedef _CharT char_type;
  755. typedef _Traits traits_type;
  756. typedef typename traits_type::int_type int_type;
  757. typedef typename traits_type::pos_type pos_type;
  758. typedef typename traits_type::off_type off_type;
  759. typedef _Allocator allocator_type;
  760. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  761. private:
  762. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  763. public:
  764. // [istringstream.cons] Constructors:
  765. _LIBCPP_INLINE_VISIBILITY
  766. basic_istringstream()
  767. : basic_istream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in) {}
  768. _LIBCPP_INLINE_VISIBILITY
  769. explicit basic_istringstream(ios_base::openmode __wch)
  770. : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {}
  771. _LIBCPP_INLINE_VISIBILITY
  772. explicit basic_istringstream(const string_type& __s,
  773. ios_base::openmode __wch = ios_base::in)
  774. : basic_istream<_CharT, _Traits>(&__sb_)
  775. , __sb_(__s, __wch | ios_base::in)
  776. { }
  777. #if _LIBCPP_STD_VER >= 20
  778. _LIBCPP_HIDE_FROM_ABI basic_istringstream(ios_base::openmode __wch, const _Allocator& __a)
  779. : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::in, __a) {}
  780. _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(string_type&& __s, ios_base::openmode __wch = ios_base::in)
  781. : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::in) {}
  782. template <class _SAlloc>
  783. _LIBCPP_HIDE_FROM_ABI basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a)
  784. : basic_istringstream(__s, ios_base::in, __a) {}
  785. template <class _SAlloc>
  786. _LIBCPP_HIDE_FROM_ABI basic_istringstream(
  787. const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a)
  788. : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in, __a) {}
  789. template <class _SAlloc>
  790. _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s,
  791. ios_base::openmode __wch = ios_base::in)
  792. : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in) {}
  793. #endif // _LIBCPP_STD_VER >= 20
  794. _LIBCPP_INLINE_VISIBILITY
  795. basic_istringstream(basic_istringstream&& __rhs)
  796. : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
  797. , __sb_(_VSTD::move(__rhs.__sb_))
  798. {
  799. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  800. }
  801. // [istringstream.assign] Assign and swap:
  802. basic_istringstream& operator=(basic_istringstream&& __rhs) {
  803. basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  804. __sb_ = _VSTD::move(__rhs.__sb_);
  805. return *this;
  806. }
  807. _LIBCPP_INLINE_VISIBILITY
  808. void swap(basic_istringstream& __rhs) {
  809. basic_istream<char_type, traits_type>::swap(__rhs);
  810. __sb_.swap(__rhs.__sb_);
  811. }
  812. // [istringstream.members] Member functions:
  813. _LIBCPP_INLINE_VISIBILITY
  814. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  815. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  816. }
  817. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  818. _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); }
  819. #else
  820. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return __sb_.str(); }
  821. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); }
  822. #endif
  823. #if _LIBCPP_STD_VER >= 20
  824. template <class _SAlloc>
  825. requires __is_allocator<_SAlloc>::value
  826. _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const {
  827. return __sb_.str(__sa);
  828. }
  829. _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); }
  830. #endif // _LIBCPP_STD_VER >= 20
  831. _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); }
  832. #if _LIBCPP_STD_VER >= 20
  833. template <class _SAlloc>
  834. _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) {
  835. __sb_.str(__s);
  836. }
  837. _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); }
  838. #endif // _LIBCPP_STD_VER >= 20
  839. };
  840. template <class _CharT, class _Traits, class _Allocator>
  841. inline _LIBCPP_INLINE_VISIBILITY
  842. void
  843. swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
  844. basic_istringstream<_CharT, _Traits, _Allocator>& __y)
  845. {
  846. __x.swap(__y);
  847. }
  848. // Class template basic_ostringstream [ostringstream]
  849. template <class _CharT, class _Traits, class _Allocator>
  850. class _LIBCPP_TEMPLATE_VIS basic_ostringstream
  851. : public basic_ostream<_CharT, _Traits>
  852. {
  853. public:
  854. typedef _CharT char_type;
  855. typedef _Traits traits_type;
  856. typedef typename traits_type::int_type int_type;
  857. typedef typename traits_type::pos_type pos_type;
  858. typedef typename traits_type::off_type off_type;
  859. typedef _Allocator allocator_type;
  860. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  861. private:
  862. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  863. public:
  864. // [ostringstream.cons] Constructors:
  865. _LIBCPP_INLINE_VISIBILITY
  866. basic_ostringstream()
  867. : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::out) {}
  868. _LIBCPP_INLINE_VISIBILITY
  869. explicit basic_ostringstream(ios_base::openmode __wch)
  870. : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::out) {}
  871. _LIBCPP_INLINE_VISIBILITY
  872. explicit basic_ostringstream(const string_type& __s,
  873. ios_base::openmode __wch = ios_base::out)
  874. : basic_ostream<_CharT, _Traits>(&__sb_)
  875. , __sb_(__s, __wch | ios_base::out)
  876. { }
  877. #if _LIBCPP_STD_VER >= 20
  878. _LIBCPP_HIDE_FROM_ABI basic_ostringstream(ios_base::openmode __wch, const _Allocator& __a)
  879. : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::out, __a) {}
  880. _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(string_type&& __s, ios_base::openmode __wch = ios_base::out)
  881. : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::out) {}
  882. template <class _SAlloc>
  883. _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a)
  884. : basic_ostringstream(__s, ios_base::out, __a) {}
  885. template <class _SAlloc>
  886. _LIBCPP_HIDE_FROM_ABI basic_ostringstream(
  887. const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a)
  888. : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out, __a) {}
  889. template <class _SAlloc>
  890. requires (!is_same_v<_SAlloc, allocator_type>)
  891. _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s,
  892. ios_base::openmode __wch = ios_base::out)
  893. : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out) {}
  894. #endif // _LIBCPP_STD_VER >= 20
  895. _LIBCPP_INLINE_VISIBILITY
  896. basic_ostringstream(basic_ostringstream&& __rhs)
  897. : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs))
  898. , __sb_(_VSTD::move(__rhs.__sb_))
  899. {
  900. basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
  901. }
  902. // [ostringstream.assign] Assign and swap:
  903. basic_ostringstream& operator=(basic_ostringstream&& __rhs) {
  904. basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  905. __sb_ = _VSTD::move(__rhs.__sb_);
  906. return *this;
  907. }
  908. _LIBCPP_INLINE_VISIBILITY
  909. void swap(basic_ostringstream& __rhs) {
  910. basic_ostream<char_type, traits_type>::swap(__rhs);
  911. __sb_.swap(__rhs.__sb_);
  912. }
  913. // [ostringstream.members] Member functions:
  914. _LIBCPP_INLINE_VISIBILITY
  915. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  916. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  917. }
  918. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  919. _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); }
  920. #else
  921. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return __sb_.str(); }
  922. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); }
  923. #endif
  924. #if _LIBCPP_STD_VER >= 20
  925. template <class _SAlloc>
  926. requires __is_allocator<_SAlloc>::value
  927. _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const {
  928. return __sb_.str(__sa);
  929. }
  930. _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); }
  931. #endif // _LIBCPP_STD_VER >= 20
  932. _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); }
  933. #if _LIBCPP_STD_VER >= 20
  934. template <class _SAlloc>
  935. _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) {
  936. __sb_.str(__s);
  937. }
  938. _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); }
  939. #endif // _LIBCPP_STD_VER >= 20
  940. };
  941. template <class _CharT, class _Traits, class _Allocator>
  942. inline _LIBCPP_INLINE_VISIBILITY
  943. void
  944. swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
  945. basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
  946. {
  947. __x.swap(__y);
  948. }
  949. // Class template basic_stringstream [stringstream]
  950. template <class _CharT, class _Traits, class _Allocator>
  951. class _LIBCPP_TEMPLATE_VIS basic_stringstream
  952. : public basic_iostream<_CharT, _Traits>
  953. {
  954. public:
  955. typedef _CharT char_type;
  956. typedef _Traits traits_type;
  957. typedef typename traits_type::int_type int_type;
  958. typedef typename traits_type::pos_type pos_type;
  959. typedef typename traits_type::off_type off_type;
  960. typedef _Allocator allocator_type;
  961. typedef basic_string<char_type, traits_type, allocator_type> string_type;
  962. private:
  963. basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
  964. public:
  965. // [stringstream.cons] constructors
  966. _LIBCPP_INLINE_VISIBILITY
  967. basic_stringstream()
  968. : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in | ios_base::out) {}
  969. _LIBCPP_INLINE_VISIBILITY
  970. explicit basic_stringstream(ios_base::openmode __wch)
  971. : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {}
  972. _LIBCPP_INLINE_VISIBILITY
  973. explicit basic_stringstream(const string_type& __s,
  974. ios_base::openmode __wch = ios_base::in | ios_base::out)
  975. : basic_iostream<_CharT, _Traits>(&__sb_)
  976. , __sb_(__s, __wch)
  977. { }
  978. #if _LIBCPP_STD_VER >= 20
  979. _LIBCPP_HIDE_FROM_ABI basic_stringstream(ios_base::openmode __wch, const _Allocator& __a)
  980. : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch, __a) {}
  981. _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(string_type&& __s, ios_base::openmode __wch = ios_base::out | ios_base::in)
  982. : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch) {}
  983. template <class _SAlloc>
  984. _LIBCPP_HIDE_FROM_ABI basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a)
  985. : basic_stringstream(__s, ios_base::out | ios_base::in, __a) {}
  986. template <class _SAlloc>
  987. _LIBCPP_HIDE_FROM_ABI basic_stringstream(
  988. const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a)
  989. : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch, __a) {}
  990. template <class _SAlloc>
  991. requires (!is_same_v<_SAlloc, allocator_type>)
  992. _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s,
  993. ios_base::openmode __wch = ios_base::out | ios_base::in)
  994. : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch) {}
  995. #endif // _LIBCPP_STD_VER >= 20
  996. _LIBCPP_INLINE_VISIBILITY
  997. basic_stringstream(basic_stringstream&& __rhs)
  998. : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs))
  999. , __sb_(_VSTD::move(__rhs.__sb_))
  1000. {
  1001. basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
  1002. }
  1003. // [stringstream.assign] Assign and swap:
  1004. basic_stringstream& operator=(basic_stringstream&& __rhs) {
  1005. basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  1006. __sb_ = _VSTD::move(__rhs.__sb_);
  1007. return *this;
  1008. }
  1009. _LIBCPP_INLINE_VISIBILITY
  1010. void swap(basic_stringstream& __rhs) {
  1011. basic_iostream<char_type, traits_type>::swap(__rhs);
  1012. __sb_.swap(__rhs.__sb_);
  1013. }
  1014. // [stringstream.members] Member functions:
  1015. _LIBCPP_INLINE_VISIBILITY
  1016. basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
  1017. return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
  1018. }
  1019. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY)
  1020. _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); }
  1021. #else
  1022. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const & { return __sb_.str(); }
  1023. _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); }
  1024. #endif
  1025. #if _LIBCPP_STD_VER >= 20
  1026. template <class _SAlloc>
  1027. requires __is_allocator<_SAlloc>::value
  1028. _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const {
  1029. return __sb_.str(__sa);
  1030. }
  1031. _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); }
  1032. #endif // _LIBCPP_STD_VER >= 20
  1033. _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); }
  1034. #if _LIBCPP_STD_VER >= 20
  1035. template <class _SAlloc>
  1036. _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) {
  1037. __sb_.str(__s);
  1038. }
  1039. _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); }
  1040. #endif // _LIBCPP_STD_VER >= 20
  1041. };
  1042. template <class _CharT, class _Traits, class _Allocator>
  1043. inline _LIBCPP_INLINE_VISIBILITY
  1044. void
  1045. swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
  1046. basic_stringstream<_CharT, _Traits, _Allocator>& __y)
  1047. {
  1048. __x.swap(__y);
  1049. }
  1050. #if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
  1051. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>;
  1052. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>;
  1053. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>;
  1054. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>;
  1055. #endif
  1056. _LIBCPP_END_NAMESPACE_STD
  1057. _LIBCPP_POP_MACROS
  1058. #if _LIBCPP_STD_VER <= 20 && !defined(_LIPCPP_REMOVE_TRANSITIVE_INCLUDES)
  1059. # include <type_traits>
  1060. #endif
  1061. #endif // _LIBCPP_SSTREAM