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