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