sstream 49 KB

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