Home | History | Annotate | Download | only in v1
      1 // -*- C++ -*-
      2 //===--------------------------- sstream ----------------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      8 //
      9 //===----------------------------------------------------------------------===//
     10 
     11 #ifndef _LIBCPP_SSTREAM
     12 #define _LIBCPP_SSTREAM
     13 
     14 /*
     15     sstream synopsis
     16 
     17 template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
     18 class basic_stringbuf
     19     : public basic_streambuf<charT, traits>
     20 {
     21 public:
     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     // 27.8.1.1 Constructors:
     30     explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out);
     31     explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
     32                              ios_base::openmode which = ios_base::in | ios_base::out);
     33     basic_stringbuf(basic_stringbuf&& rhs);
     34 
     35     // 27.8.1.2 Assign and swap:
     36     basic_stringbuf& operator=(basic_stringbuf&& rhs);
     37     void swap(basic_stringbuf& rhs);
     38 
     39     // 27.8.1.3 Get and set:
     40     basic_string<char_type, traits_type, allocator_type> str() const;
     41     void str(const basic_string<char_type, traits_type, allocator_type>& s);
     42 
     43 protected:
     44     // 27.8.1.4 Overridden virtual functions:
     45     virtual int_type underflow();
     46     virtual int_type pbackfail(int_type c = traits_type::eof());
     47     virtual int_type overflow (int_type c = traits_type::eof());
     48     virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
     49     virtual pos_type seekoff(off_type off, ios_base::seekdir way,
     50                              ios_base::openmode which = ios_base::in | ios_base::out);
     51     virtual pos_type seekpos(pos_type sp,
     52                              ios_base::openmode which = ios_base::in | ios_base::out);
     53 };
     54 
     55 template <class charT, class traits, class Allocator>
     56   void swap(basic_stringbuf<charT, traits, Allocator>& x,
     57             basic_stringbuf<charT, traits, Allocator>& y);
     58 
     59 typedef basic_stringbuf<char>    stringbuf;
     60 typedef basic_stringbuf<wchar_t> wstringbuf;
     61 
     62 template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
     63 class basic_istringstream
     64     : public basic_istream<charT, traits>
     65 {
     66 public:
     67     typedef charT                          char_type;
     68     typedef traits                         traits_type;
     69     typedef typename traits_type::int_type int_type;
     70     typedef typename traits_type::pos_type pos_type;
     71     typedef typename traits_type::off_type off_type;
     72     typedef Allocator                      allocator_type;
     73 
     74     // 27.8.2.1 Constructors:
     75     explicit basic_istringstream(ios_base::openmode which = ios_base::in);
     76     explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
     77                                  ios_base::openmode which = ios_base::in);
     78     basic_istringstream(basic_istringstream&& rhs);
     79 
     80     // 27.8.2.2 Assign and swap:
     81     basic_istringstream& operator=(basic_istringstream&& rhs);
     82     void swap(basic_istringstream& rhs);
     83 
     84     // 27.8.2.3 Members:
     85     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
     86     basic_string<char_type, traits_type, allocator_type> str() const;
     87     void str(const basic_string<char_type, traits_type, allocator_type>& s);
     88 };
     89 
     90 template <class charT, class traits, class Allocator>
     91   void swap(basic_istringstream<charT, traits, Allocator>& x,
     92             basic_istringstream<charT, traits, Allocator>& y);
     93 
     94 typedef basic_istringstream<char>    istringstream;
     95 typedef basic_istringstream<wchar_t> wistringstream;
     96 
     97 template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
     98 class basic_ostringstream
     99     : public basic_ostream<charT, traits>
    100 {
    101 public:
    102     // types:
    103     typedef charT                          char_type;
    104     typedef traits                         traits_type;
    105     typedef typename traits_type::int_type int_type;
    106     typedef typename traits_type::pos_type pos_type;
    107     typedef typename traits_type::off_type off_type;
    108     typedef Allocator                      allocator_type;
    109 
    110     // 27.8.3.1 Constructors/destructor:
    111     explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
    112     explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
    113                                  ios_base::openmode which = ios_base::out);
    114     basic_ostringstream(basic_ostringstream&& rhs);
    115 
    116     // 27.8.3.2 Assign/swap:
    117     basic_ostringstream& operator=(basic_ostringstream&& rhs);
    118     void swap(basic_ostringstream& rhs);
    119 
    120     // 27.8.3.3 Members:
    121     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
    122     basic_string<char_type, traits_type, allocator_type> str() const;
    123     void str(const basic_string<char_type, traits_type, allocator_type>& s);
    124 };
    125 
    126 template <class charT, class traits, class Allocator>
    127   void swap(basic_ostringstream<charT, traits, Allocator>& x,
    128             basic_ostringstream<charT, traits, Allocator>& y);
    129 
    130 typedef basic_ostringstream<char>    ostringstream;
    131 typedef basic_ostringstream<wchar_t> wostringstream;
    132 
    133 template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
    134 class basic_stringstream
    135     : public basic_iostream<charT, traits>
    136 {
    137 public:
    138     // types:
    139     typedef charT                          char_type;
    140     typedef traits                         traits_type;
    141     typedef typename traits_type::int_type int_type;
    142     typedef typename traits_type::pos_type pos_type;
    143     typedef typename traits_type::off_type off_type;
    144     typedef Allocator                      allocator_type;
    145 
    146     // constructors/destructor
    147     explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in);
    148     explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
    149                                 ios_base::openmode which = ios_base::out|ios_base::in);
    150     basic_stringstream(basic_stringstream&& rhs);
    151 
    152     // 27.8.5.1 Assign/swap:
    153     basic_stringstream& operator=(basic_stringstream&& rhs);
    154     void swap(basic_stringstream& rhs);
    155 
    156     // Members:
    157     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
    158     basic_string<char_type, traits_type, allocator_type> str() const;
    159     void str(const basic_string<char_type, traits_type, allocator_type>& str);
    160 };
    161 
    162 template <class charT, class traits, class Allocator>
    163   void swap(basic_stringstream<charT, traits, Allocator>& x,
    164             basic_stringstream<charT, traits, Allocator>& y);
    165 
    166 typedef basic_stringstream<char>    stringstream;
    167 typedef basic_stringstream<wchar_t> wstringstream;
    168 
    169 }  // std
    170 
    171 */
    172 
    173 #include <__config>
    174 #include <ostream>
    175 #include <istream>
    176 #include <string>
    177 
    178 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    179 #pragma GCC system_header
    180 #endif
    181 
    182 _LIBCPP_PUSH_MACROS
    183 #include <__undef_macros>
    184 
    185 
    186 _LIBCPP_BEGIN_NAMESPACE_STD
    187 
    188 // basic_stringbuf
    189 
    190 template <class _CharT, class _Traits, class _Allocator>
    191 class _LIBCPP_TEMPLATE_VIS basic_stringbuf
    192     : public basic_streambuf<_CharT, _Traits>
    193 {
    194 public:
    195     typedef _CharT                         char_type;
    196     typedef _Traits                        traits_type;
    197     typedef typename traits_type::int_type int_type;
    198     typedef typename traits_type::pos_type pos_type;
    199     typedef typename traits_type::off_type off_type;
    200     typedef _Allocator                     allocator_type;
    201 
    202     typedef basic_string<char_type, traits_type, allocator_type> string_type;
    203 
    204 private:
    205 
    206     string_type __str_;
    207     mutable char_type* __hm_;
    208     ios_base::openmode __mode_;
    209 
    210 public:
    211     // 27.8.1.1 Constructors:
    212     inline _LIBCPP_INLINE_VISIBILITY
    213     explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out);
    214     inline _LIBCPP_INLINE_VISIBILITY
    215     explicit basic_stringbuf(const string_type& __s,
    216                              ios_base::openmode __wch = ios_base::in | ios_base::out);
    217 #ifndef _LIBCPP_CXX03_LANG
    218     basic_stringbuf(basic_stringbuf&& __rhs);
    219 
    220     // 27.8.1.2 Assign and swap:
    221     basic_stringbuf& operator=(basic_stringbuf&& __rhs);
    222 #endif
    223     void swap(basic_stringbuf& __rhs);
    224 
    225     // 27.8.1.3 Get and set:
    226     string_type str() const;
    227     void str(const string_type& __s);
    228 
    229 protected:
    230     // 27.8.1.4 Overridden virtual functions:
    231     virtual int_type underflow();
    232     virtual int_type pbackfail(int_type __c = traits_type::eof());
    233     virtual int_type overflow (int_type __c = traits_type::eof());
    234     virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
    235                              ios_base::openmode __wch = ios_base::in | ios_base::out);
    236     inline _LIBCPP_INLINE_VISIBILITY
    237     virtual pos_type seekpos(pos_type __sp,
    238                              ios_base::openmode __wch = ios_base::in | ios_base::out);
    239 };
    240 
    241 template <class _CharT, class _Traits, class _Allocator>
    242 basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(ios_base::openmode __wch)
    243     : __hm_(0),
    244       __mode_(__wch)
    245 {
    246     str(string_type());
    247 }
    248 
    249 template <class _CharT, class _Traits, class _Allocator>
    250 basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(const string_type& __s,
    251                              ios_base::openmode __wch)
    252     : __hm_(0),
    253       __mode_(__wch)
    254 {
    255     str(__s);
    256 }
    257 
    258 #ifndef _LIBCPP_CXX03_LANG
    259 
    260 template <class _CharT, class _Traits, class _Allocator>
    261 basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
    262     : __mode_(__rhs.__mode_)
    263 {
    264     char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
    265     ptrdiff_t __binp = -1;
    266     ptrdiff_t __ninp = -1;
    267     ptrdiff_t __einp = -1;
    268     if (__rhs.eback() != nullptr)
    269     {
    270         __binp = __rhs.eback() - __p;
    271         __ninp = __rhs.gptr() - __p;
    272         __einp = __rhs.egptr() - __p;
    273     }
    274     ptrdiff_t __bout = -1;
    275     ptrdiff_t __nout = -1;
    276     ptrdiff_t __eout = -1;
    277     if (__rhs.pbase() != nullptr)
    278     {
    279         __bout = __rhs.pbase() - __p;
    280         __nout = __rhs.pptr() - __p;
    281         __eout = __rhs.epptr() - __p;
    282     }
    283     ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
    284     __str_ = _VSTD::move(__rhs.__str_);
    285     __p = const_cast<char_type*>(__str_.data());
    286     if (__binp != -1)
    287         this->setg(__p + __binp, __p + __ninp, __p + __einp);
    288     if (__bout != -1)
    289     {
    290         this->setp(__p + __bout, __p + __eout);
    291         this->pbump(__nout);
    292     }
    293     __hm_ = __hm == -1 ? nullptr : __p + __hm;
    294     __p = const_cast<char_type*>(__rhs.__str_.data());
    295     __rhs.setg(__p, __p, __p);
    296     __rhs.setp(__p, __p);
    297     __rhs.__hm_ = __p;
    298     this->pubimbue(__rhs.getloc());
    299 }
    300 
    301 template <class _CharT, class _Traits, class _Allocator>
    302 basic_stringbuf<_CharT, _Traits, _Allocator>&
    303 basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
    304 {
    305     char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
    306     ptrdiff_t __binp = -1;
    307     ptrdiff_t __ninp = -1;
    308     ptrdiff_t __einp = -1;
    309     if (__rhs.eback() != nullptr)
    310     {
    311         __binp = __rhs.eback() - __p;
    312         __ninp = __rhs.gptr() - __p;
    313         __einp = __rhs.egptr() - __p;
    314     }
    315     ptrdiff_t __bout = -1;
    316     ptrdiff_t __nout = -1;
    317     ptrdiff_t __eout = -1;
    318     if (__rhs.pbase() != nullptr)
    319     {
    320         __bout = __rhs.pbase() - __p;
    321         __nout = __rhs.pptr() - __p;
    322         __eout = __rhs.epptr() - __p;
    323     }
    324     ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
    325     __str_ = _VSTD::move(__rhs.__str_);
    326     __p = const_cast<char_type*>(__str_.data());
    327     if (__binp != -1)
    328         this->setg(__p + __binp, __p + __ninp, __p + __einp);
    329     else
    330         this->setg(nullptr, nullptr, nullptr);
    331     if (__bout != -1)
    332     {
    333         this->setp(__p + __bout, __p + __eout);
    334         this->pbump(__nout);
    335     }
    336     else
    337         this->setp(nullptr, nullptr);
    338 
    339     __hm_ = __hm == -1 ? nullptr : __p + __hm;
    340     __mode_ = __rhs.__mode_;
    341     __p = const_cast<char_type*>(__rhs.__str_.data());
    342     __rhs.setg(__p, __p, __p);
    343     __rhs.setp(__p, __p);
    344     __rhs.__hm_ = __p;
    345     this->pubimbue(__rhs.getloc());
    346     return *this;
    347 }
    348 
    349 #endif  // _LIBCPP_CXX03_LANG
    350 
    351 template <class _CharT, class _Traits, class _Allocator>
    352 void
    353 basic_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 
    428 template <class _CharT, class _Traits, class _Allocator>
    429 inline _LIBCPP_INLINE_VISIBILITY
    430 void
    431 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
    432      basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
    433 {
    434     __x.swap(__y);
    435 }
    436 
    437 template <class _CharT, class _Traits, class _Allocator>
    438 basic_string<_CharT, _Traits, _Allocator>
    439 basic_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 
    452 template <class _CharT, class _Traits, class _Allocator>
    453 void
    454 basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
    455 {
    456     __str_ = __s;
    457     __hm_ = 0;
    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             this->pbump(__sz);
    474     }
    475 }
    476 
    477 template <class _CharT, class _Traits, class _Allocator>
    478 typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
    479 basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
    480 {
    481     if (__hm_ < this->pptr())
    482         __hm_ = this->pptr();
    483     if (__mode_ & ios_base::in)
    484     {
    485         if (this->egptr() < __hm_)
    486             this->setg(this->eback(), this->gptr(), __hm_);
    487         if (this->gptr() < this->egptr())
    488             return traits_type::to_int_type(*this->gptr());
    489     }
    490     return traits_type::eof();
    491 }
    492 
    493 template <class _CharT, class _Traits, class _Allocator>
    494 typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
    495 basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
    496 {
    497     if (__hm_ < this->pptr())
    498         __hm_ = this->pptr();
    499     if (this->eback() < this->gptr())
    500     {
    501         if (traits_type::eq_int_type(__c, traits_type::eof()))
    502         {
    503             this->setg(this->eback(), this->gptr()-1, __hm_);
    504             return traits_type::not_eof(__c);
    505         }
    506         if ((__mode_ & ios_base::out) ||
    507             traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
    508         {
    509             this->setg(this->eback(), this->gptr()-1, __hm_);
    510             *this->gptr() = traits_type::to_char_type(__c);
    511             return __c;
    512         }
    513     }
    514     return traits_type::eof();
    515 }
    516 
    517 template <class _CharT, class _Traits, class _Allocator>
    518 typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
    519 basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
    520 {
    521     if (!traits_type::eq_int_type(__c, traits_type::eof()))
    522     {
    523         ptrdiff_t __ninp = this->gptr()  - this->eback();
    524         if (this->pptr() == this->epptr())
    525         {
    526             if (!(__mode_ & ios_base::out))
    527                 return traits_type::eof();
    528 #ifndef _LIBCPP_NO_EXCEPTIONS
    529             try
    530             {
    531 #endif  // _LIBCPP_NO_EXCEPTIONS
    532                 ptrdiff_t __nout = this->pptr()  - this->pbase();
    533                 ptrdiff_t __hm = __hm_ - this->pbase();
    534                 __str_.push_back(char_type());
    535                 __str_.resize(__str_.capacity());
    536                 char_type* __p = const_cast<char_type*>(__str_.data());
    537                 this->setp(__p, __p + __str_.size());
    538                 this->pbump(__nout);
    539                 __hm_ = this->pbase() + __hm;
    540 #ifndef _LIBCPP_NO_EXCEPTIONS
    541             }
    542             catch (...)
    543             {
    544                 return traits_type::eof();
    545             }
    546 #endif  // _LIBCPP_NO_EXCEPTIONS
    547         }
    548         __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
    549         if (__mode_ & ios_base::in)
    550         {
    551             char_type* __p = const_cast<char_type*>(__str_.data());
    552             this->setg(__p, __p + __ninp, __hm_);
    553         }
    554         return this->sputc(__c);
    555     }
    556     return traits_type::not_eof(__c);
    557 }
    558 
    559 template <class _CharT, class _Traits, class _Allocator>
    560 typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
    561 basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
    562                                                       ios_base::seekdir __way,
    563                                                       ios_base::openmode __wch)
    564 {
    565     if (__hm_ < this->pptr())
    566         __hm_ = this->pptr();
    567     if ((__wch & (ios_base::in | ios_base::out)) == 0)
    568         return pos_type(-1);
    569     if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
    570         && __way == ios_base::cur)
    571         return pos_type(-1);
    572     off_type __noff;
    573     switch (__way)
    574     {
    575     case ios_base::beg:
    576         __noff = 0;
    577         break;
    578     case ios_base::cur:
    579         if (__wch & ios_base::in)
    580             __noff = this->gptr() - this->eback();
    581         else
    582             __noff = this->pptr() - this->pbase();
    583         break;
    584     case ios_base::end:
    585         __noff = __hm_ - __str_.data();
    586         break;
    587     default:
    588         return pos_type(-1);
    589     }
    590     __noff += __off;
    591     if (__noff < 0 || __hm_ - __str_.data() < __noff)
    592         return pos_type(-1);
    593     if (__noff != 0)
    594     {
    595         if ((__wch & ios_base::in) && this->gptr() == 0)
    596             return pos_type(-1);
    597         if ((__wch & ios_base::out) && this->pptr() == 0)
    598             return pos_type(-1);
    599     }
    600     if (__wch & ios_base::in)
    601         this->setg(this->eback(), this->eback() + __noff, __hm_);
    602     if (__wch & ios_base::out)
    603     {
    604         this->setp(this->pbase(), this->epptr());
    605         this->pbump(__noff);
    606     }
    607     return pos_type(__noff);
    608 }
    609 
    610 template <class _CharT, class _Traits, class _Allocator>
    611 typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
    612 basic_stringbuf<_CharT, _Traits, _Allocator>::seekpos(pos_type __sp,
    613                                                       ios_base::openmode __wch)
    614 {
    615     return seekoff(__sp, ios_base::beg, __wch);
    616 }
    617 
    618 // basic_istringstream
    619 
    620 template <class _CharT, class _Traits, class _Allocator>
    621 class _LIBCPP_TEMPLATE_VIS basic_istringstream
    622     : public basic_istream<_CharT, _Traits>
    623 {
    624 public:
    625     typedef _CharT                         char_type;
    626     typedef _Traits                        traits_type;
    627     typedef typename traits_type::int_type int_type;
    628     typedef typename traits_type::pos_type pos_type;
    629     typedef typename traits_type::off_type off_type;
    630     typedef _Allocator                     allocator_type;
    631 
    632     typedef basic_string<char_type, traits_type, allocator_type> string_type;
    633 
    634 private:
    635     basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
    636 
    637 public:
    638     // 27.8.2.1 Constructors:
    639     inline _LIBCPP_INLINE_VISIBILITY
    640     explicit basic_istringstream(ios_base::openmode __wch = ios_base::in);
    641     inline _LIBCPP_INLINE_VISIBILITY
    642     explicit basic_istringstream(const string_type& __s,
    643                                  ios_base::openmode __wch = ios_base::in);
    644 #ifndef _LIBCPP_CXX03_LANG
    645     inline _LIBCPP_INLINE_VISIBILITY
    646     basic_istringstream(basic_istringstream&& __rhs);
    647 
    648     // 27.8.2.2 Assign and swap:
    649     basic_istringstream& operator=(basic_istringstream&& __rhs);
    650 #endif  // _LIBCPP_CXX03_LANG
    651     inline _LIBCPP_INLINE_VISIBILITY
    652     void swap(basic_istringstream& __rhs);
    653 
    654     // 27.8.2.3 Members:
    655     inline _LIBCPP_INLINE_VISIBILITY
    656     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
    657     inline _LIBCPP_INLINE_VISIBILITY
    658     string_type str() const;
    659     inline _LIBCPP_INLINE_VISIBILITY
    660     void str(const string_type& __s);
    661 };
    662 
    663 template <class _CharT, class _Traits, class _Allocator>
    664 basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(ios_base::openmode __wch)
    665     : basic_istream<_CharT, _Traits>(&__sb_),
    666       __sb_(__wch | ios_base::in)
    667 {
    668 }
    669 
    670 template <class _CharT, class _Traits, class _Allocator>
    671 basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(const string_type& __s,
    672                                                                       ios_base::openmode __wch)
    673     : basic_istream<_CharT, _Traits>(&__sb_),
    674       __sb_(__s, __wch | ios_base::in)
    675 {
    676 }
    677 
    678 #ifndef _LIBCPP_CXX03_LANG
    679 
    680 template <class _CharT, class _Traits, class _Allocator>
    681 basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(basic_istringstream&& __rhs)
    682     : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs)),
    683       __sb_(_VSTD::move(__rhs.__sb_))
    684 {
    685     basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
    686 }
    687 
    688 template <class _CharT, class _Traits, class _Allocator>
    689 basic_istringstream<_CharT, _Traits, _Allocator>&
    690 basic_istringstream<_CharT, _Traits, _Allocator>::operator=(basic_istringstream&& __rhs)
    691 {
    692     basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
    693     __sb_ = _VSTD::move(__rhs.__sb_);
    694     return *this;
    695 }
    696 
    697 #endif  // _LIBCPP_CXX03_LANG
    698 
    699 template <class _CharT, class _Traits, class _Allocator>
    700 void basic_istringstream<_CharT, _Traits, _Allocator>::swap(basic_istringstream& __rhs)
    701 {
    702     basic_istream<char_type, traits_type>::swap(__rhs);
    703     __sb_.swap(__rhs.__sb_);
    704 }
    705 
    706 template <class _CharT, class _Traits, class _Allocator>
    707 inline _LIBCPP_INLINE_VISIBILITY
    708 void
    709 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
    710      basic_istringstream<_CharT, _Traits, _Allocator>& __y)
    711 {
    712     __x.swap(__y);
    713 }
    714 
    715 template <class _CharT, class _Traits, class _Allocator>
    716 basic_stringbuf<_CharT, _Traits, _Allocator>*
    717 basic_istringstream<_CharT, _Traits, _Allocator>::rdbuf() const
    718 {
    719     return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
    720 }
    721 
    722 template <class _CharT, class _Traits, class _Allocator>
    723 basic_string<_CharT, _Traits, _Allocator>
    724 basic_istringstream<_CharT, _Traits, _Allocator>::str() const
    725 {
    726     return __sb_.str();
    727 }
    728 
    729 template <class _CharT, class _Traits, class _Allocator>
    730 void basic_istringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
    731 {
    732     __sb_.str(__s);
    733 }
    734 
    735 // basic_ostringstream
    736 
    737 template <class _CharT, class _Traits, class _Allocator>
    738 class _LIBCPP_TEMPLATE_VIS basic_ostringstream
    739     : public basic_ostream<_CharT, _Traits>
    740 {
    741 public:
    742     typedef _CharT                         char_type;
    743     typedef _Traits                        traits_type;
    744     typedef typename traits_type::int_type int_type;
    745     typedef typename traits_type::pos_type pos_type;
    746     typedef typename traits_type::off_type off_type;
    747     typedef _Allocator                     allocator_type;
    748 
    749     typedef basic_string<char_type, traits_type, allocator_type> string_type;
    750 
    751 private:
    752     basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
    753 
    754 public:
    755     // 27.8.2.1 Constructors:
    756     inline _LIBCPP_INLINE_VISIBILITY
    757     explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out);
    758     inline _LIBCPP_INLINE_VISIBILITY
    759     explicit basic_ostringstream(const string_type& __s,
    760                                  ios_base::openmode __wch = ios_base::out);
    761 #ifndef _LIBCPP_CXX03_LANG
    762     inline _LIBCPP_INLINE_VISIBILITY
    763     basic_ostringstream(basic_ostringstream&& __rhs);
    764 
    765     // 27.8.2.2 Assign and swap:
    766     basic_ostringstream& operator=(basic_ostringstream&& __rhs);
    767 #endif  // _LIBCPP_CXX03_LANG
    768     inline _LIBCPP_INLINE_VISIBILITY
    769     void swap(basic_ostringstream& __rhs);
    770 
    771     // 27.8.2.3 Members:
    772     inline _LIBCPP_INLINE_VISIBILITY
    773     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
    774     inline _LIBCPP_INLINE_VISIBILITY
    775     string_type str() const;
    776     inline _LIBCPP_INLINE_VISIBILITY
    777     void str(const string_type& __s);
    778 };
    779 
    780 template <class _CharT, class _Traits, class _Allocator>
    781 basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(ios_base::openmode __wch)
    782     : basic_ostream<_CharT, _Traits>(&__sb_),
    783       __sb_(__wch | ios_base::out)
    784 {
    785 }
    786 
    787 template <class _CharT, class _Traits, class _Allocator>
    788 basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(const string_type& __s,
    789                                                                       ios_base::openmode __wch)
    790     : basic_ostream<_CharT, _Traits>(&__sb_),
    791       __sb_(__s, __wch | ios_base::out)
    792 {
    793 }
    794 
    795 #ifndef _LIBCPP_CXX03_LANG
    796 
    797 template <class _CharT, class _Traits, class _Allocator>
    798 basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(basic_ostringstream&& __rhs)
    799     : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs)),
    800       __sb_(_VSTD::move(__rhs.__sb_))
    801 {
    802     basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
    803 }
    804 
    805 template <class _CharT, class _Traits, class _Allocator>
    806 basic_ostringstream<_CharT, _Traits, _Allocator>&
    807 basic_ostringstream<_CharT, _Traits, _Allocator>::operator=(basic_ostringstream&& __rhs)
    808 {
    809     basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
    810     __sb_ = _VSTD::move(__rhs.__sb_);
    811     return *this;
    812 }
    813 
    814 #endif  // _LIBCPP_CXX03_LANG
    815 
    816 template <class _CharT, class _Traits, class _Allocator>
    817 void
    818 basic_ostringstream<_CharT, _Traits, _Allocator>::swap(basic_ostringstream& __rhs)
    819 {
    820     basic_ostream<char_type, traits_type>::swap(__rhs);
    821     __sb_.swap(__rhs.__sb_);
    822 }
    823 
    824 template <class _CharT, class _Traits, class _Allocator>
    825 inline _LIBCPP_INLINE_VISIBILITY
    826 void
    827 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
    828      basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
    829 {
    830     __x.swap(__y);
    831 }
    832 
    833 template <class _CharT, class _Traits, class _Allocator>
    834 basic_stringbuf<_CharT, _Traits, _Allocator>*
    835 basic_ostringstream<_CharT, _Traits, _Allocator>::rdbuf() const
    836 {
    837     return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
    838 }
    839 
    840 template <class _CharT, class _Traits, class _Allocator>
    841 basic_string<_CharT, _Traits, _Allocator>
    842 basic_ostringstream<_CharT, _Traits, _Allocator>::str() const
    843 {
    844     return __sb_.str();
    845 }
    846 
    847 template <class _CharT, class _Traits, class _Allocator>
    848 void
    849 basic_ostringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
    850 {
    851     __sb_.str(__s);
    852 }
    853 
    854 // basic_stringstream
    855 
    856 template <class _CharT, class _Traits, class _Allocator>
    857 class _LIBCPP_TEMPLATE_VIS basic_stringstream
    858     : public basic_iostream<_CharT, _Traits>
    859 {
    860 public:
    861     typedef _CharT                         char_type;
    862     typedef _Traits                        traits_type;
    863     typedef typename traits_type::int_type int_type;
    864     typedef typename traits_type::pos_type pos_type;
    865     typedef typename traits_type::off_type off_type;
    866     typedef _Allocator                     allocator_type;
    867 
    868     typedef basic_string<char_type, traits_type, allocator_type> string_type;
    869 
    870 private:
    871     basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
    872 
    873 public:
    874     // 27.8.2.1 Constructors:
    875     inline _LIBCPP_INLINE_VISIBILITY
    876     explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out);
    877     inline _LIBCPP_INLINE_VISIBILITY
    878     explicit basic_stringstream(const string_type& __s,
    879                                 ios_base::openmode __wch = ios_base::in | ios_base::out);
    880 #ifndef _LIBCPP_CXX03_LANG
    881     inline _LIBCPP_INLINE_VISIBILITY
    882     basic_stringstream(basic_stringstream&& __rhs);
    883 
    884     // 27.8.2.2 Assign and swap:
    885     basic_stringstream& operator=(basic_stringstream&& __rhs);
    886 #endif  // _LIBCPP_CXX03_LANG
    887     inline _LIBCPP_INLINE_VISIBILITY
    888     void swap(basic_stringstream& __rhs);
    889 
    890     // 27.8.2.3 Members:
    891     inline _LIBCPP_INLINE_VISIBILITY
    892     basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
    893     inline _LIBCPP_INLINE_VISIBILITY
    894     string_type str() const;
    895     inline _LIBCPP_INLINE_VISIBILITY
    896     void str(const string_type& __s);
    897 };
    898 
    899 template <class _CharT, class _Traits, class _Allocator>
    900 basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(ios_base::openmode __wch)
    901     : basic_iostream<_CharT, _Traits>(&__sb_),
    902       __sb_(__wch)
    903 {
    904 }
    905 
    906 template <class _CharT, class _Traits, class _Allocator>
    907 basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(const string_type& __s,
    908                                                                     ios_base::openmode __wch)
    909     : basic_iostream<_CharT, _Traits>(&__sb_),
    910       __sb_(__s, __wch)
    911 {
    912 }
    913 
    914 #ifndef _LIBCPP_CXX03_LANG
    915 
    916 template <class _CharT, class _Traits, class _Allocator>
    917 basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(basic_stringstream&& __rhs)
    918     : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs)),
    919       __sb_(_VSTD::move(__rhs.__sb_))
    920 {
    921     basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
    922 }
    923 
    924 template <class _CharT, class _Traits, class _Allocator>
    925 basic_stringstream<_CharT, _Traits, _Allocator>&
    926 basic_stringstream<_CharT, _Traits, _Allocator>::operator=(basic_stringstream&& __rhs)
    927 {
    928     basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
    929     __sb_ = _VSTD::move(__rhs.__sb_);
    930     return *this;
    931 }
    932 
    933 #endif  // _LIBCPP_CXX03_LANG
    934 
    935 template <class _CharT, class _Traits, class _Allocator>
    936 void
    937 basic_stringstream<_CharT, _Traits, _Allocator>::swap(basic_stringstream& __rhs)
    938 {
    939     basic_iostream<char_type, traits_type>::swap(__rhs);
    940     __sb_.swap(__rhs.__sb_);
    941 }
    942 
    943 template <class _CharT, class _Traits, class _Allocator>
    944 inline _LIBCPP_INLINE_VISIBILITY
    945 void
    946 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
    947      basic_stringstream<_CharT, _Traits, _Allocator>& __y)
    948 {
    949     __x.swap(__y);
    950 }
    951 
    952 template <class _CharT, class _Traits, class _Allocator>
    953 basic_stringbuf<_CharT, _Traits, _Allocator>*
    954 basic_stringstream<_CharT, _Traits, _Allocator>::rdbuf() const
    955 {
    956     return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
    957 }
    958 
    959 template <class _CharT, class _Traits, class _Allocator>
    960 basic_string<_CharT, _Traits, _Allocator>
    961 basic_stringstream<_CharT, _Traits, _Allocator>::str() const
    962 {
    963     return __sb_.str();
    964 }
    965 
    966 template <class _CharT, class _Traits, class _Allocator>
    967 void
    968 basic_stringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
    969 {
    970     __sb_.str(__s);
    971 }
    972 
    973 _LIBCPP_END_NAMESPACE_STD
    974 
    975 _LIBCPP_POP_MACROS
    976 
    977 #endif  // _LIBCPP_SSTREAM
    978