sstream 54 KB

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