1// -*- C++ -*-
2//===--------------------------- sstream ----------------------------------===//
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
15
16template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
17class basic_stringbuf
18    : public basic_streambuf<charT, traits>
19{
20public:
21    typedef charT                          char_type;
22    typedef traits                         traits_type;
23    typedef typename traits_type::int_type int_type;
24    typedef typename traits_type::pos_type pos_type;
25    typedef typename traits_type::off_type off_type;
26    typedef Allocator                      allocator_type;
27
28    // 27.8.1.1 Constructors:
29    explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out);
30    explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
31                             ios_base::openmode which = ios_base::in | ios_base::out);
32    basic_stringbuf(basic_stringbuf&& rhs);
33
34    // 27.8.1.2 Assign and swap:
35    basic_stringbuf& operator=(basic_stringbuf&& rhs);
36    void swap(basic_stringbuf& rhs);
37
38    // 27.8.1.3 Get and set:
39    basic_string<char_type, traits_type, allocator_type> str() const;
40    void str(const basic_string<char_type, traits_type, allocator_type>& s);
41
42protected:
43    // 27.8.1.4 Overridden virtual functions:
44    virtual int_type underflow();
45    virtual int_type pbackfail(int_type c = traits_type::eof());
46    virtual int_type overflow (int_type c = traits_type::eof());
47    virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
48    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
49                             ios_base::openmode which = ios_base::in | ios_base::out);
50    virtual pos_type seekpos(pos_type sp,
51                             ios_base::openmode which = ios_base::in | ios_base::out);
52};
53
54template <class charT, class traits, class Allocator>
55  void swap(basic_stringbuf<charT, traits, Allocator>& x,
56            basic_stringbuf<charT, traits, Allocator>& y);
57
58typedef basic_stringbuf<char>    stringbuf;
59typedef basic_stringbuf<wchar_t> wstringbuf;
60
61template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
62class basic_istringstream
63    : public basic_istream<charT, traits>
64{
65public:
66    typedef charT                          char_type;
67    typedef traits                         traits_type;
68    typedef typename traits_type::int_type int_type;
69    typedef typename traits_type::pos_type pos_type;
70    typedef typename traits_type::off_type off_type;
71    typedef Allocator                      allocator_type;
72
73    // 27.8.2.1 Constructors:
74    explicit basic_istringstream(ios_base::openmode which = ios_base::in);
75    explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
76                                 ios_base::openmode which = ios_base::in);
77    basic_istringstream(basic_istringstream&& rhs);
78
79    // 27.8.2.2 Assign and swap:
80    basic_istringstream& operator=(basic_istringstream&& rhs);
81    void swap(basic_istringstream& rhs);
82
83    // 27.8.2.3 Members:
84    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
85    basic_string<char_type, traits_type, allocator_type> str() const;
86    void str(const basic_string<char_type, traits_type, allocator_type>& s);
87};
88
89template <class charT, class traits, class Allocator>
90  void swap(basic_istringstream<charT, traits, Allocator>& x,
91            basic_istringstream<charT, traits, Allocator>& y);
92
93typedef basic_istringstream<char>    istringstream;
94typedef basic_istringstream<wchar_t> wistringstream;
95
96template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
97class basic_ostringstream
98    : public basic_ostream<charT, traits>
99{
100public:
101    // types:
102    typedef charT                          char_type;
103    typedef traits                         traits_type;
104    typedef typename traits_type::int_type int_type;
105    typedef typename traits_type::pos_type pos_type;
106    typedef typename traits_type::off_type off_type;
107    typedef Allocator                      allocator_type;
108
109    // 27.8.3.1 Constructors/destructor:
110    explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
111    explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
112                                 ios_base::openmode which = ios_base::out);
113    basic_ostringstream(basic_ostringstream&& rhs);
114
115    // 27.8.3.2 Assign/swap:
116    basic_ostringstream& operator=(basic_ostringstream&& rhs);
117    void swap(basic_ostringstream& rhs);
118
119    // 27.8.3.3 Members:
120    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
121    basic_string<char_type, traits_type, allocator_type> str() const;
122    void str(const basic_string<char_type, traits_type, allocator_type>& s);
123};
124
125template <class charT, class traits, class Allocator>
126  void swap(basic_ostringstream<charT, traits, Allocator>& x,
127            basic_ostringstream<charT, traits, Allocator>& y);
128
129typedef basic_ostringstream<char>    ostringstream;
130typedef basic_ostringstream<wchar_t> wostringstream;
131
132template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
133class basic_stringstream
134    : public basic_iostream<charT, traits>
135{
136public:
137    // types:
138    typedef charT                          char_type;
139    typedef traits                         traits_type;
140    typedef typename traits_type::int_type int_type;
141    typedef typename traits_type::pos_type pos_type;
142    typedef typename traits_type::off_type off_type;
143    typedef Allocator                      allocator_type;
144
145    // constructors/destructor
146    explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in);
147    explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
148                                ios_base::openmode which = ios_base::out|ios_base::in);
149    basic_stringstream(basic_stringstream&& rhs);
150
151    // 27.8.5.1 Assign/swap:
152    basic_stringstream& operator=(basic_stringstream&& rhs);
153    void swap(basic_stringstream& rhs);
154
155    // Members:
156    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
157    basic_string<char_type, traits_type, allocator_type> str() const;
158    void str(const basic_string<char_type, traits_type, allocator_type>& str);
159};
160
161template <class charT, class traits, class Allocator>
162  void swap(basic_stringstream<charT, traits, Allocator>& x,
163            basic_stringstream<charT, traits, Allocator>& y);
164
165typedef basic_stringstream<char>    stringstream;
166typedef basic_stringstream<wchar_t> wstringstream;
167
168}  // std
169
170*/
171
172#include <__config>
173#include <ostream>
174#include <istream>
175#include <string>
176
177#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
178#pragma GCC system_header
179#endif
180
181_LIBCPP_PUSH_MACROS
182#include <__undef_macros>
183
184
185_LIBCPP_BEGIN_NAMESPACE_STD
186
187// basic_stringbuf
188
189template <class _CharT, class _Traits, class _Allocator>
190class _LIBCPP_TEMPLATE_VIS basic_stringbuf
191    : public basic_streambuf<_CharT, _Traits>
192{
193public:
194    typedef _CharT                         char_type;
195    typedef _Traits                        traits_type;
196    typedef typename traits_type::int_type int_type;
197    typedef typename traits_type::pos_type pos_type;
198    typedef typename traits_type::off_type off_type;
199    typedef _Allocator                     allocator_type;
200
201    typedef basic_string<char_type, traits_type, allocator_type> string_type;
202
203private:
204
205    string_type __str_;
206    mutable char_type* __hm_;
207    ios_base::openmode __mode_;
208
209public:
210    // 27.8.1.1 Constructors:
211    _LIBCPP_INLINE_VISIBILITY
212    explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out)
213        : __hm_(nullptr), __mode_(__wch)
214    { }
215
216    _LIBCPP_INLINE_VISIBILITY
217    explicit basic_stringbuf(const string_type& __s,
218                             ios_base::openmode __wch = ios_base::in | ios_base::out)
219        : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch)
220    {
221        str(__s);
222    }
223
224    basic_stringbuf(basic_stringbuf&& __rhs);
225
226    // 27.8.1.2 Assign and swap:
227    basic_stringbuf& operator=(basic_stringbuf&& __rhs);
228    void swap(basic_stringbuf& __rhs);
229
230    // 27.8.1.3 Get and set:
231    string_type str() const;
232    void str(const string_type& __s);
233
234protected:
235    // 27.8.1.4 Overridden virtual functions:
236    virtual int_type underflow();
237    virtual int_type pbackfail(int_type __c = traits_type::eof());
238    virtual int_type overflow (int_type __c = traits_type::eof());
239    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
240                             ios_base::openmode __wch = ios_base::in | ios_base::out);
241    _LIBCPP_INLINE_VISIBILITY
242    virtual pos_type seekpos(pos_type __sp,
243                             ios_base::openmode __wch = ios_base::in | ios_base::out) {
244        return seekoff(__sp, ios_base::beg, __wch);
245    }
246};
247
248template <class _CharT, class _Traits, class _Allocator>
249basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
250    : __mode_(__rhs.__mode_)
251{
252    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
253    ptrdiff_t __binp = -1;
254    ptrdiff_t __ninp = -1;
255    ptrdiff_t __einp = -1;
256    if (__rhs.eback() != nullptr)
257    {
258        __binp = __rhs.eback() - __p;
259        __ninp = __rhs.gptr() - __p;
260        __einp = __rhs.egptr() - __p;
261    }
262    ptrdiff_t __bout = -1;
263    ptrdiff_t __nout = -1;
264    ptrdiff_t __eout = -1;
265    if (__rhs.pbase() != nullptr)
266    {
267        __bout = __rhs.pbase() - __p;
268        __nout = __rhs.pptr() - __p;
269        __eout = __rhs.epptr() - __p;
270    }
271    ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
272    __str_ = _VSTD::move(__rhs.__str_);
273    __p = const_cast<char_type*>(__str_.data());
274    if (__binp != -1)
275        this->setg(__p + __binp, __p + __ninp, __p + __einp);
276    if (__bout != -1)
277    {
278        this->setp(__p + __bout, __p + __eout);
279        this->__pbump(__nout);
280    }
281    __hm_ = __hm == -1 ? nullptr : __p + __hm;
282    __p = const_cast<char_type*>(__rhs.__str_.data());
283    __rhs.setg(__p, __p, __p);
284    __rhs.setp(__p, __p);
285    __rhs.__hm_ = __p;
286    this->pubimbue(__rhs.getloc());
287}
288
289template <class _CharT, class _Traits, class _Allocator>
290basic_stringbuf<_CharT, _Traits, _Allocator>&
291basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
292{
293    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
294    ptrdiff_t __binp = -1;
295    ptrdiff_t __ninp = -1;
296    ptrdiff_t __einp = -1;
297    if (__rhs.eback() != nullptr)
298    {
299        __binp = __rhs.eback() - __p;
300        __ninp = __rhs.gptr() - __p;
301        __einp = __rhs.egptr() - __p;
302    }
303    ptrdiff_t __bout = -1;
304    ptrdiff_t __nout = -1;
305    ptrdiff_t __eout = -1;
306    if (__rhs.pbase() != nullptr)
307    {
308        __bout = __rhs.pbase() - __p;
309        __nout = __rhs.pptr() - __p;
310        __eout = __rhs.epptr() - __p;
311    }
312    ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
313    __str_ = _VSTD::move(__rhs.__str_);
314    __p = const_cast<char_type*>(__str_.data());
315    if (__binp != -1)
316        this->setg(__p + __binp, __p + __ninp, __p + __einp);
317    else
318        this->setg(nullptr, nullptr, nullptr);
319    if (__bout != -1)
320    {
321        this->setp(__p + __bout, __p + __eout);
322        this->__pbump(__nout);
323    }
324    else
325        this->setp(nullptr, nullptr);
326
327    __hm_ = __hm == -1 ? nullptr : __p + __hm;
328    __mode_ = __rhs.__mode_;
329    __p = const_cast<char_type*>(__rhs.__str_.data());
330    __rhs.setg(__p, __p, __p);
331    __rhs.setp(__p, __p);
332    __rhs.__hm_ = __p;
333    this->pubimbue(__rhs.getloc());
334    return *this;
335}
336
337template <class _CharT, class _Traits, class _Allocator>
338void
339basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
340{
341    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
342    ptrdiff_t __rbinp = -1;
343    ptrdiff_t __rninp = -1;
344    ptrdiff_t __reinp = -1;
345    if (__rhs.eback() != nullptr)
346    {
347        __rbinp = __rhs.eback() - __p;
348        __rninp = __rhs.gptr() - __p;
349        __reinp = __rhs.egptr() - __p;
350    }
351    ptrdiff_t __rbout = -1;
352    ptrdiff_t __rnout = -1;
353    ptrdiff_t __reout = -1;
354    if (__rhs.pbase() != nullptr)
355    {
356        __rbout = __rhs.pbase() - __p;
357        __rnout = __rhs.pptr() - __p;
358        __reout = __rhs.epptr() - __p;
359    }
360    ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
361    __p = const_cast<char_type*>(__str_.data());
362    ptrdiff_t __lbinp = -1;
363    ptrdiff_t __lninp = -1;
364    ptrdiff_t __leinp = -1;
365    if (this->eback() != nullptr)
366    {
367        __lbinp = this->eback() - __p;
368        __lninp = this->gptr() - __p;
369        __leinp = this->egptr() - __p;
370    }
371    ptrdiff_t __lbout = -1;
372    ptrdiff_t __lnout = -1;
373    ptrdiff_t __leout = -1;
374    if (this->pbase() != nullptr)
375    {
376        __lbout = this->pbase() - __p;
377        __lnout = this->pptr() - __p;
378        __leout = this->epptr() - __p;
379    }
380    ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
381    _VSTD::swap(__mode_, __rhs.__mode_);
382    __str_.swap(__rhs.__str_);
383    __p = const_cast<char_type*>(__str_.data());
384    if (__rbinp != -1)
385        this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
386    else
387        this->setg(nullptr, nullptr, nullptr);
388    if (__rbout != -1)
389    {
390        this->setp(__p + __rbout, __p + __reout);
391        this->__pbump(__rnout);
392    }
393    else
394        this->setp(nullptr, nullptr);
395    __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
396    __p = const_cast<char_type*>(__rhs.__str_.data());
397    if (__lbinp != -1)
398        __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
399    else
400        __rhs.setg(nullptr, nullptr, nullptr);
401    if (__lbout != -1)
402    {
403        __rhs.setp(__p + __lbout, __p + __leout);
404        __rhs.__pbump(__lnout);
405    }
406    else
407        __rhs.setp(nullptr, nullptr);
408    __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
409    locale __tl = __rhs.getloc();
410    __rhs.pubimbue(this->getloc());
411    this->pubimbue(__tl);
412}
413
414template <class _CharT, class _Traits, class _Allocator>
415inline _LIBCPP_INLINE_VISIBILITY
416void
417swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
418     basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
419{
420    __x.swap(__y);
421}
422
423template <class _CharT, class _Traits, class _Allocator>
424basic_string<_CharT, _Traits, _Allocator>
425basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
426{
427    if (__mode_ & ios_base::out)
428    {
429        if (__hm_ < this->pptr())
430            __hm_ = this->pptr();
431        return string_type(this->pbase(), __hm_, __str_.get_allocator());
432    }
433    else if (__mode_ & ios_base::in)
434        return string_type(this->eback(), this->egptr(), __str_.get_allocator());
435    return string_type(__str_.get_allocator());
436}
437
438template <class _CharT, class _Traits, class _Allocator>
439void
440basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
441{
442    __str_ = __s;
443    __hm_ = nullptr;
444    if (__mode_ & ios_base::in)
445    {
446        __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
447        this->setg(const_cast<char_type*>(__str_.data()),
448                   const_cast<char_type*>(__str_.data()),
449                   __hm_);
450    }
451    if (__mode_ & ios_base::out)
452    {
453        typename string_type::size_type __sz = __str_.size();
454        __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
455        __str_.resize(__str_.capacity());
456        this->setp(const_cast<char_type*>(__str_.data()),
457                   const_cast<char_type*>(__str_.data()) + __str_.size());
458        if (__mode_ & (ios_base::app | ios_base::ate))
459        {
460            while (__sz > INT_MAX)
461            {
462                this->pbump(INT_MAX);
463                __sz -= INT_MAX;
464            }
465            if (__sz > 0)
466                this->pbump(__sz);
467        }
468    }
469}
470
471template <class _CharT, class _Traits, class _Allocator>
472typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
473basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
474{
475    if (__hm_ < this->pptr())
476        __hm_ = this->pptr();
477    if (__mode_ & ios_base::in)
478    {
479        if (this->egptr() < __hm_)
480            this->setg(this->eback(), this->gptr(), __hm_);
481        if (this->gptr() < this->egptr())
482            return traits_type::to_int_type(*this->gptr());
483    }
484    return traits_type::eof();
485}
486
487template <class _CharT, class _Traits, class _Allocator>
488typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
489basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
490{
491    if (__hm_ < this->pptr())
492        __hm_ = this->pptr();
493    if (this->eback() < this->gptr())
494    {
495        if (traits_type::eq_int_type(__c, traits_type::eof()))
496        {
497            this->setg(this->eback(), this->gptr()-1, __hm_);
498            return traits_type::not_eof(__c);
499        }
500        if ((__mode_ & ios_base::out) ||
501            traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
502        {
503            this->setg(this->eback(), this->gptr()-1, __hm_);
504            *this->gptr() = traits_type::to_char_type(__c);
505            return __c;
506        }
507    }
508    return traits_type::eof();
509}
510
511template <class _CharT, class _Traits, class _Allocator>
512typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
513basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
514{
515    if (!traits_type::eq_int_type(__c, traits_type::eof()))
516    {
517        ptrdiff_t __ninp = this->gptr()  - this->eback();
518        if (this->pptr() == this->epptr())
519        {
520            if (!(__mode_ & ios_base::out))
521                return traits_type::eof();
522#ifndef _LIBCPP_NO_EXCEPTIONS
523            try
524            {
525#endif  // _LIBCPP_NO_EXCEPTIONS
526                ptrdiff_t __nout = this->pptr()  - this->pbase();
527                ptrdiff_t __hm = __hm_ - this->pbase();
528                __str_.push_back(char_type());
529                __str_.resize(__str_.capacity());
530                char_type* __p = const_cast<char_type*>(__str_.data());
531                this->setp(__p, __p + __str_.size());
532                this->__pbump(__nout);
533                __hm_ = this->pbase() + __hm;
534#ifndef _LIBCPP_NO_EXCEPTIONS
535            }
536            catch (...)
537            {
538                return traits_type::eof();
539            }
540#endif  // _LIBCPP_NO_EXCEPTIONS
541        }
542        __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
543        if (__mode_ & ios_base::in)
544        {
545            char_type* __p = const_cast<char_type*>(__str_.data());
546            this->setg(__p, __p + __ninp, __hm_);
547        }
548        return this->sputc(traits_type::to_char_type(__c));
549    }
550    return traits_type::not_eof(__c);
551}
552
553template <class _CharT, class _Traits, class _Allocator>
554typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
555basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
556                                                      ios_base::seekdir __way,
557                                                      ios_base::openmode __wch)
558{
559    if (__hm_ < this->pptr())
560        __hm_ = this->pptr();
561    if ((__wch & (ios_base::in | ios_base::out)) == 0)
562        return pos_type(-1);
563    if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
564        && __way == ios_base::cur)
565        return pos_type(-1);
566    const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
567    off_type __noff;
568    switch (__way)
569    {
570    case ios_base::beg:
571        __noff = 0;
572        break;
573    case ios_base::cur:
574        if (__wch & ios_base::in)
575            __noff = this->gptr() - this->eback();
576        else
577            __noff = this->pptr() - this->pbase();
578        break;
579    case ios_base::end:
580        __noff = __hm;
581        break;
582    default:
583        return pos_type(-1);
584    }
585    __noff += __off;
586    if (__noff < 0 || __hm < __noff)
587        return pos_type(-1);
588    if (__noff != 0)
589    {
590        if ((__wch & ios_base::in) && this->gptr() == nullptr)
591            return pos_type(-1);
592        if ((__wch & ios_base::out) && this->pptr() == nullptr)
593            return pos_type(-1);
594    }
595    if (__wch & ios_base::in)
596        this->setg(this->eback(), this->eback() + __noff, __hm_);
597    if (__wch & ios_base::out)
598    {
599        this->setp(this->pbase(), this->epptr());
600        this->pbump(__noff);
601    }
602    return pos_type(__noff);
603}
604
605// basic_istringstream
606
607template <class _CharT, class _Traits, class _Allocator>
608class _LIBCPP_TEMPLATE_VIS basic_istringstream
609    : public basic_istream<_CharT, _Traits>
610{
611public:
612    typedef _CharT                         char_type;
613    typedef _Traits                        traits_type;
614    typedef typename traits_type::int_type int_type;
615    typedef typename traits_type::pos_type pos_type;
616    typedef typename traits_type::off_type off_type;
617    typedef _Allocator                     allocator_type;
618
619    typedef basic_string<char_type, traits_type, allocator_type> string_type;
620
621private:
622    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
623
624public:
625    // 27.8.2.1 Constructors:
626    _LIBCPP_INLINE_VISIBILITY
627    explicit basic_istringstream(ios_base::openmode __wch = ios_base::in)
628        : basic_istream<_CharT, _Traits>(&__sb_)
629        , __sb_(__wch | ios_base::in)
630    { }
631    _LIBCPP_INLINE_VISIBILITY
632    explicit basic_istringstream(const string_type& __s,
633                                 ios_base::openmode __wch = ios_base::in)
634        : basic_istream<_CharT, _Traits>(&__sb_)
635        , __sb_(__s, __wch | ios_base::in)
636    { }
637
638    _LIBCPP_INLINE_VISIBILITY
639    basic_istringstream(basic_istringstream&& __rhs)
640        : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
641        , __sb_(_VSTD::move(__rhs.__sb_))
642    {
643        basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
644    }
645
646    // 27.8.2.2 Assign and swap:
647    basic_istringstream& operator=(basic_istringstream&& __rhs) {
648        basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
649        __sb_ = _VSTD::move(__rhs.__sb_);
650        return *this;
651    }
652    _LIBCPP_INLINE_VISIBILITY
653    void swap(basic_istringstream& __rhs) {
654        basic_istream<char_type, traits_type>::swap(__rhs);
655        __sb_.swap(__rhs.__sb_);
656    }
657
658    // 27.8.2.3 Members:
659    _LIBCPP_INLINE_VISIBILITY
660    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
661        return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
662    }
663    _LIBCPP_INLINE_VISIBILITY
664    string_type str() const {
665        return __sb_.str();
666    }
667    _LIBCPP_INLINE_VISIBILITY
668    void str(const string_type& __s) {
669        __sb_.str(__s);
670    }
671};
672
673template <class _CharT, class _Traits, class _Allocator>
674inline _LIBCPP_INLINE_VISIBILITY
675void
676swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
677     basic_istringstream<_CharT, _Traits, _Allocator>& __y)
678{
679    __x.swap(__y);
680}
681
682// basic_ostringstream
683
684template <class _CharT, class _Traits, class _Allocator>
685class _LIBCPP_TEMPLATE_VIS basic_ostringstream
686    : public basic_ostream<_CharT, _Traits>
687{
688public:
689    typedef _CharT                         char_type;
690    typedef _Traits                        traits_type;
691    typedef typename traits_type::int_type int_type;
692    typedef typename traits_type::pos_type pos_type;
693    typedef typename traits_type::off_type off_type;
694    typedef _Allocator                     allocator_type;
695
696    typedef basic_string<char_type, traits_type, allocator_type> string_type;
697
698private:
699    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
700
701public:
702    // 27.8.2.1 Constructors:
703    _LIBCPP_INLINE_VISIBILITY
704    explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out)
705        : basic_ostream<_CharT, _Traits>(&__sb_)
706        , __sb_(__wch | ios_base::out)
707    { }
708
709    _LIBCPP_INLINE_VISIBILITY
710    explicit basic_ostringstream(const string_type& __s,
711                                 ios_base::openmode __wch = ios_base::out)
712        : basic_ostream<_CharT, _Traits>(&__sb_)
713        , __sb_(__s, __wch | ios_base::out)
714    { }
715
716    _LIBCPP_INLINE_VISIBILITY
717    basic_ostringstream(basic_ostringstream&& __rhs)
718        : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs))
719        , __sb_(_VSTD::move(__rhs.__sb_))
720    {
721        basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
722    }
723
724    // 27.8.2.2 Assign and swap:
725    basic_ostringstream& operator=(basic_ostringstream&& __rhs) {
726        basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
727        __sb_ = _VSTD::move(__rhs.__sb_);
728        return *this;
729    }
730
731    _LIBCPP_INLINE_VISIBILITY
732    void swap(basic_ostringstream& __rhs) {
733        basic_ostream<char_type, traits_type>::swap(__rhs);
734        __sb_.swap(__rhs.__sb_);
735    }
736
737    // 27.8.2.3 Members:
738    _LIBCPP_INLINE_VISIBILITY
739    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
740        return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
741    }
742    _LIBCPP_INLINE_VISIBILITY
743    string_type str() const {
744        return __sb_.str();
745    }
746    _LIBCPP_INLINE_VISIBILITY
747    void str(const string_type& __s) {
748        __sb_.str(__s);
749    }
750};
751
752template <class _CharT, class _Traits, class _Allocator>
753inline _LIBCPP_INLINE_VISIBILITY
754void
755swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
756     basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
757{
758    __x.swap(__y);
759}
760
761// basic_stringstream
762
763template <class _CharT, class _Traits, class _Allocator>
764class _LIBCPP_TEMPLATE_VIS basic_stringstream
765    : public basic_iostream<_CharT, _Traits>
766{
767public:
768    typedef _CharT                         char_type;
769    typedef _Traits                        traits_type;
770    typedef typename traits_type::int_type int_type;
771    typedef typename traits_type::pos_type pos_type;
772    typedef typename traits_type::off_type off_type;
773    typedef _Allocator                     allocator_type;
774
775    typedef basic_string<char_type, traits_type, allocator_type> string_type;
776
777private:
778    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
779
780public:
781    // 27.8.2.1 Constructors:
782    _LIBCPP_INLINE_VISIBILITY
783    explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out)
784        : basic_iostream<_CharT, _Traits>(&__sb_)
785        , __sb_(__wch)
786    { }
787
788    _LIBCPP_INLINE_VISIBILITY
789    explicit basic_stringstream(const string_type& __s,
790                                ios_base::openmode __wch = ios_base::in | ios_base::out)
791        : basic_iostream<_CharT, _Traits>(&__sb_)
792        , __sb_(__s, __wch)
793    { }
794
795    _LIBCPP_INLINE_VISIBILITY
796    basic_stringstream(basic_stringstream&& __rhs)
797        : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs))
798        , __sb_(_VSTD::move(__rhs.__sb_))
799    {
800        basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
801    }
802
803    // 27.8.2.2 Assign and swap:
804    basic_stringstream& operator=(basic_stringstream&& __rhs) {
805        basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
806        __sb_ = _VSTD::move(__rhs.__sb_);
807        return *this;
808    }
809    _LIBCPP_INLINE_VISIBILITY
810    void swap(basic_stringstream& __rhs) {
811        basic_iostream<char_type, traits_type>::swap(__rhs);
812        __sb_.swap(__rhs.__sb_);
813    }
814
815    // 27.8.2.3 Members:
816    _LIBCPP_INLINE_VISIBILITY
817    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
818        return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
819    }
820    _LIBCPP_INLINE_VISIBILITY
821    string_type str() const {
822        return __sb_.str();
823    }
824    _LIBCPP_INLINE_VISIBILITY
825    void str(const string_type& __s) {
826        __sb_.str(__s);
827    }
828};
829
830template <class _CharT, class _Traits, class _Allocator>
831inline _LIBCPP_INLINE_VISIBILITY
832void
833swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
834     basic_stringstream<_CharT, _Traits, _Allocator>& __y)
835{
836    __x.swap(__y);
837}
838
839#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
840_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>)
841_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>)
842_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>)
843_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>)
844#endif
845
846_LIBCPP_END_NAMESPACE_STD
847
848_LIBCPP_POP_MACROS
849
850#endif  // _LIBCPP_SSTREAM
851