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