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