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