blob: 22cc0c3503844bbd8fe7d8ebadb1d7293381d7ae [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- sstream ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SSTREAM
11#define _LIBCPP_SSTREAM
12
13/*
14 sstream synopsis
15
16template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
17class basic_stringbuf
18 : public basic_streambuf<charT, traits>
19{
20public:
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
26 typedef Allocator allocator_type;
27
28 // 27.8.1.1 Constructors:
29 explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out);
30 explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
31 ios_base::openmode which = ios_base::in | ios_base::out);
32 basic_stringbuf(basic_stringbuf&& rhs);
33
34 // 27.8.1.2 Assign and swap:
35 basic_stringbuf& operator=(basic_stringbuf&& rhs);
36 void swap(basic_stringbuf& rhs);
37
38 // 27.8.1.3 Get and set:
39 basic_string<char_type, traits_type, allocator_type> str() const;
40 void str(const basic_string<char_type, traits_type, allocator_type>& s);
41
42protected:
43 // 27.8.1.4 Overridden virtual functions:
44 virtual int_type underflow();
45 virtual int_type pbackfail(int_type c = traits_type::eof());
46 virtual int_type overflow (int_type c = traits_type::eof());
47 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
48 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
49 ios_base::openmode which = ios_base::in | ios_base::out);
50 virtual pos_type seekpos(pos_type sp,
51 ios_base::openmode which = ios_base::in | ios_base::out);
52};
53
54template <class charT, class traits, class Allocator>
55 void swap(basic_stringbuf<charT, traits, Allocator>& x,
56 basic_stringbuf<charT, traits, Allocator>& y);
57
58typedef basic_stringbuf<char> stringbuf;
59typedef basic_stringbuf<wchar_t> wstringbuf;
60
61template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
62class basic_istringstream
63 : public basic_istream<charT, traits>
64{
65public:
66 typedef charT char_type;
67 typedef traits traits_type;
68 typedef typename traits_type::int_type int_type;
69 typedef typename traits_type::pos_type pos_type;
70 typedef typename traits_type::off_type off_type;
71 typedef Allocator allocator_type;
72
73 // 27.8.2.1 Constructors:
74 explicit basic_istringstream(ios_base::openmode which = ios_base::in);
75 explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
76 ios_base::openmode which = ios_base::in);
77 basic_istringstream(basic_istringstream&& rhs);
78
79 // 27.8.2.2 Assign and swap:
80 basic_istringstream& operator=(basic_istringstream&& rhs);
81 void swap(basic_istringstream& rhs);
82
83 // 27.8.2.3 Members:
84 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
85 basic_string<char_type, traits_type, allocator_type> str() const;
86 void str(const basic_string<char_type, traits_type, allocator_type>& s);
87};
88
89template <class charT, class traits, class Allocator>
90 void swap(basic_istringstream<charT, traits, Allocator>& x,
91 basic_istringstream<charT, traits, Allocator>& y);
92
93typedef basic_istringstream<char> istringstream;
94typedef basic_istringstream<wchar_t> wistringstream;
95
96template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
97class basic_ostringstream
98 : public basic_ostream<charT, traits>
99{
100public:
101 // types:
102 typedef charT char_type;
103 typedef traits traits_type;
104 typedef typename traits_type::int_type int_type;
105 typedef typename traits_type::pos_type pos_type;
106 typedef typename traits_type::off_type off_type;
107 typedef Allocator allocator_type;
108
109 // 27.8.3.1 Constructors/destructor:
110 explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
111 explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
112 ios_base::openmode which = ios_base::out);
113 basic_ostringstream(basic_ostringstream&& rhs);
114
115 // 27.8.3.2 Assign/swap:
116 basic_ostringstream& operator=(basic_ostringstream&& rhs);
117 void swap(basic_ostringstream& rhs);
118
119 // 27.8.3.3 Members:
120 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
121 basic_string<char_type, traits_type, allocator_type> str() const;
122 void str(const basic_string<char_type, traits_type, allocator_type>& s);
123};
124
125template <class charT, class traits, class Allocator>
126 void swap(basic_ostringstream<charT, traits, Allocator>& x,
127 basic_ostringstream<charT, traits, Allocator>& y);
128
129typedef basic_ostringstream<char> ostringstream;
130typedef basic_ostringstream<wchar_t> wostringstream;
131
132template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
133class basic_stringstream
134 : public basic_iostream<charT, traits>
135{
136public:
137 // types:
138 typedef charT char_type;
139 typedef traits traits_type;
140 typedef typename traits_type::int_type int_type;
141 typedef typename traits_type::pos_type pos_type;
142 typedef typename traits_type::off_type off_type;
143 typedef Allocator allocator_type;
144
145 // constructors/destructor
146 explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in);
147 explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
148 ios_base::openmode which = ios_base::out|ios_base::in);
149 basic_stringstream(basic_stringstream&& rhs);
150
151 // 27.8.5.1 Assign/swap:
152 basic_stringstream& operator=(basic_stringstream&& rhs);
153 void swap(basic_stringstream& rhs);
154
155 // Members:
156 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
157 basic_string<char_type, traits_type, allocator_type> str() const;
158 void str(const basic_string<char_type, traits_type, allocator_type>& str);
159};
160
161template <class charT, class traits, class Allocator>
162 void swap(basic_stringstream<charT, traits, Allocator>& x,
163 basic_stringstream<charT, traits, Allocator>& y);
164
165typedef basic_stringstream<char> stringstream;
166typedef basic_stringstream<wchar_t> wstringstream;
167
168} // std
169
170*/
171
172#include <__config>
173#include <ostream>
174#include <istream>
175#include <string>
176
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000177#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180
Eric Fiselierf4433a32017-05-31 22:07:49 +0000181_LIBCPP_PUSH_MACROS
182#include <__undef_macros>
183
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185_LIBCPP_BEGIN_NAMESPACE_STD
186
187// basic_stringbuf
188
189template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000190class _LIBCPP_TEMPLATE_VIS basic_stringbuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 : public basic_streambuf<_CharT, _Traits>
192{
193public:
194 typedef _CharT char_type;
195 typedef _Traits traits_type;
196 typedef typename traits_type::int_type int_type;
197 typedef typename traits_type::pos_type pos_type;
198 typedef typename traits_type::off_type off_type;
199 typedef _Allocator allocator_type;
200
201 typedef basic_string<char_type, traits_type, allocator_type> string_type;
202
203private:
204
205 string_type __str_;
206 mutable char_type* __hm_;
207 ios_base::openmode __mode_;
208
209public:
210 // 27.8.1.1 Constructors:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400211 _LIBCPP_INLINE_VISIBILITY
212 explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500213 : __hm_(nullptr), __mode_(__wch)
Louis Dionne6b6073b2020-10-09 14:21:23 -0400214 { }
215
216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 explicit basic_stringbuf(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400218 ios_base::openmode __wch = ios_base::in | ios_base::out)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500219 : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch)
Louis Dionne6b6073b2020-10-09 14:21:23 -0400220 {
221 str(__s);
222 }
223
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 basic_stringbuf(basic_stringbuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
226 // 27.8.1.2 Assign and swap:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 basic_stringbuf& operator=(basic_stringbuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 void swap(basic_stringbuf& __rhs);
229
230 // 27.8.1.3 Get and set:
231 string_type str() const;
232 void str(const string_type& __s);
233
234protected:
235 // 27.8.1.4 Overridden virtual functions:
236 virtual int_type underflow();
237 virtual int_type pbackfail(int_type __c = traits_type::eof());
238 virtual int_type overflow (int_type __c = traits_type::eof());
239 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
240 ios_base::openmode __wch = ios_base::in | ios_base::out);
Louis Dionne6b6073b2020-10-09 14:21:23 -0400241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 virtual pos_type seekpos(pos_type __sp,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400243 ios_base::openmode __wch = ios_base::in | ios_base::out) {
244 return seekoff(__sp, ios_base::beg, __wch);
245 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246};
247
248template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
250 : __mode_(__rhs.__mode_)
251{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000252 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
253 ptrdiff_t __binp = -1;
254 ptrdiff_t __ninp = -1;
255 ptrdiff_t __einp = -1;
256 if (__rhs.eback() != nullptr)
257 {
258 __binp = __rhs.eback() - __p;
259 __ninp = __rhs.gptr() - __p;
260 __einp = __rhs.egptr() - __p;
261 }
262 ptrdiff_t __bout = -1;
263 ptrdiff_t __nout = -1;
264 ptrdiff_t __eout = -1;
265 if (__rhs.pbase() != nullptr)
266 {
267 __bout = __rhs.pbase() - __p;
268 __nout = __rhs.pptr() - __p;
269 __eout = __rhs.epptr() - __p;
270 }
271 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000272 __str_ = _VSTD::move(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000273 __p = const_cast<char_type*>(__str_.data());
274 if (__binp != -1)
275 this->setg(__p + __binp, __p + __ninp, __p + __einp);
276 if (__bout != -1)
277 {
278 this->setp(__p + __bout, __p + __eout);
Marshall Clow33932622017-09-12 15:00:43 +0000279 this->__pbump(__nout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000280 }
281 __hm_ = __hm == -1 ? nullptr : __p + __hm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282 __p = const_cast<char_type*>(__rhs.__str_.data());
283 __rhs.setg(__p, __p, __p);
284 __rhs.setp(__p, __p);
285 __rhs.__hm_ = __p;
286 this->pubimbue(__rhs.getloc());
287}
288
289template <class _CharT, class _Traits, class _Allocator>
290basic_stringbuf<_CharT, _Traits, _Allocator>&
291basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
292{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000293 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
294 ptrdiff_t __binp = -1;
295 ptrdiff_t __ninp = -1;
296 ptrdiff_t __einp = -1;
297 if (__rhs.eback() != nullptr)
298 {
299 __binp = __rhs.eback() - __p;
300 __ninp = __rhs.gptr() - __p;
301 __einp = __rhs.egptr() - __p;
302 }
303 ptrdiff_t __bout = -1;
304 ptrdiff_t __nout = -1;
305 ptrdiff_t __eout = -1;
306 if (__rhs.pbase() != nullptr)
307 {
308 __bout = __rhs.pbase() - __p;
309 __nout = __rhs.pptr() - __p;
310 __eout = __rhs.epptr() - __p;
311 }
312 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000313 __str_ = _VSTD::move(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000314 __p = const_cast<char_type*>(__str_.data());
315 if (__binp != -1)
316 this->setg(__p + __binp, __p + __ninp, __p + __einp);
Marshall Clow45e157b2014-09-16 18:57:52 +0000317 else
318 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000319 if (__bout != -1)
320 {
321 this->setp(__p + __bout, __p + __eout);
Marshall Clow33932622017-09-12 15:00:43 +0000322 this->__pbump(__nout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000323 }
Marshall Clow45e157b2014-09-16 18:57:52 +0000324 else
325 this->setp(nullptr, nullptr);
326
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000327 __hm_ = __hm == -1 ? nullptr : __p + __hm;
328 __mode_ = __rhs.__mode_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 __p = const_cast<char_type*>(__rhs.__str_.data());
330 __rhs.setg(__p, __p, __p);
331 __rhs.setp(__p, __p);
332 __rhs.__hm_ = __p;
333 this->pubimbue(__rhs.getloc());
334 return *this;
335}
336
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337template <class _CharT, class _Traits, class _Allocator>
338void
339basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
340{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000341 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
342 ptrdiff_t __rbinp = -1;
343 ptrdiff_t __rninp = -1;
344 ptrdiff_t __reinp = -1;
345 if (__rhs.eback() != nullptr)
346 {
347 __rbinp = __rhs.eback() - __p;
348 __rninp = __rhs.gptr() - __p;
349 __reinp = __rhs.egptr() - __p;
350 }
351 ptrdiff_t __rbout = -1;
352 ptrdiff_t __rnout = -1;
353 ptrdiff_t __reout = -1;
354 if (__rhs.pbase() != nullptr)
355 {
356 __rbout = __rhs.pbase() - __p;
357 __rnout = __rhs.pptr() - __p;
358 __reout = __rhs.epptr() - __p;
359 }
360 ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
361 __p = const_cast<char_type*>(__str_.data());
362 ptrdiff_t __lbinp = -1;
363 ptrdiff_t __lninp = -1;
364 ptrdiff_t __leinp = -1;
365 if (this->eback() != nullptr)
366 {
367 __lbinp = this->eback() - __p;
368 __lninp = this->gptr() - __p;
369 __leinp = this->egptr() - __p;
370 }
371 ptrdiff_t __lbout = -1;
372 ptrdiff_t __lnout = -1;
373 ptrdiff_t __leout = -1;
374 if (this->pbase() != nullptr)
375 {
376 __lbout = this->pbase() - __p;
377 __lnout = this->pptr() - __p;
378 __leout = this->epptr() - __p;
379 }
380 ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000381 _VSTD::swap(__mode_, __rhs.__mode_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 __str_.swap(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000383 __p = const_cast<char_type*>(__str_.data());
384 if (__rbinp != -1)
385 this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
386 else
387 this->setg(nullptr, nullptr, nullptr);
388 if (__rbout != -1)
389 {
390 this->setp(__p + __rbout, __p + __reout);
Marshall Clow33932622017-09-12 15:00:43 +0000391 this->__pbump(__rnout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000392 }
393 else
394 this->setp(nullptr, nullptr);
395 __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 __p = const_cast<char_type*>(__rhs.__str_.data());
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000397 if (__lbinp != -1)
398 __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
399 else
400 __rhs.setg(nullptr, nullptr, nullptr);
401 if (__lbout != -1)
402 {
403 __rhs.setp(__p + __lbout, __p + __leout);
Marshall Clow33932622017-09-12 15:00:43 +0000404 __rhs.__pbump(__lnout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000405 }
406 else
407 __rhs.setp(nullptr, nullptr);
408 __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 locale __tl = __rhs.getloc();
410 __rhs.pubimbue(this->getloc());
411 this->pubimbue(__tl);
412}
413
414template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000415inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416void
417swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
418 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
419{
420 __x.swap(__y);
421}
422
423template <class _CharT, class _Traits, class _Allocator>
424basic_string<_CharT, _Traits, _Allocator>
425basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
426{
427 if (__mode_ & ios_base::out)
428 {
429 if (__hm_ < this->pptr())
430 __hm_ = this->pptr();
431 return string_type(this->pbase(), __hm_, __str_.get_allocator());
432 }
433 else if (__mode_ & ios_base::in)
434 return string_type(this->eback(), this->egptr(), __str_.get_allocator());
435 return string_type(__str_.get_allocator());
436}
437
438template <class _CharT, class _Traits, class _Allocator>
439void
440basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
441{
442 __str_ = __s;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500443 __hm_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 if (__mode_ & ios_base::in)
445 {
446 __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
447 this->setg(const_cast<char_type*>(__str_.data()),
448 const_cast<char_type*>(__str_.data()),
449 __hm_);
450 }
451 if (__mode_ & ios_base::out)
452 {
453 typename string_type::size_type __sz = __str_.size();
454 __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
455 __str_.resize(__str_.capacity());
456 this->setp(const_cast<char_type*>(__str_.data()),
457 const_cast<char_type*>(__str_.data()) + __str_.size());
458 if (__mode_ & (ios_base::app | ios_base::ate))
Marshall Clow33932622017-09-12 15:00:43 +0000459 {
460 while (__sz > INT_MAX)
461 {
Louis Dionne44bcff92018-08-03 22:36:53 +0000462 this->pbump(INT_MAX);
463 __sz -= INT_MAX;
Marshall Clow33932622017-09-12 15:00:43 +0000464 }
465 if (__sz > 0)
Louis Dionne44bcff92018-08-03 22:36:53 +0000466 this->pbump(__sz);
467 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 }
469}
470
471template <class _CharT, class _Traits, class _Allocator>
472typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
473basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
474{
475 if (__hm_ < this->pptr())
476 __hm_ = this->pptr();
477 if (__mode_ & ios_base::in)
478 {
479 if (this->egptr() < __hm_)
480 this->setg(this->eback(), this->gptr(), __hm_);
481 if (this->gptr() < this->egptr())
482 return traits_type::to_int_type(*this->gptr());
483 }
484 return traits_type::eof();
485}
486
487template <class _CharT, class _Traits, class _Allocator>
488typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
489basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
490{
491 if (__hm_ < this->pptr())
492 __hm_ = this->pptr();
493 if (this->eback() < this->gptr())
494 {
495 if (traits_type::eq_int_type(__c, traits_type::eof()))
496 {
497 this->setg(this->eback(), this->gptr()-1, __hm_);
498 return traits_type::not_eof(__c);
499 }
500 if ((__mode_ & ios_base::out) ||
501 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
502 {
503 this->setg(this->eback(), this->gptr()-1, __hm_);
504 *this->gptr() = traits_type::to_char_type(__c);
505 return __c;
506 }
507 }
508 return traits_type::eof();
509}
510
511template <class _CharT, class _Traits, class _Allocator>
512typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
513basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
514{
515 if (!traits_type::eq_int_type(__c, traits_type::eof()))
516 {
517 ptrdiff_t __ninp = this->gptr() - this->eback();
518 if (this->pptr() == this->epptr())
519 {
520 if (!(__mode_ & ios_base::out))
521 return traits_type::eof();
522#ifndef _LIBCPP_NO_EXCEPTIONS
523 try
524 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000525#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526 ptrdiff_t __nout = this->pptr() - this->pbase();
527 ptrdiff_t __hm = __hm_ - this->pbase();
528 __str_.push_back(char_type());
529 __str_.resize(__str_.capacity());
530 char_type* __p = const_cast<char_type*>(__str_.data());
531 this->setp(__p, __p + __str_.size());
Marshall Clow33932622017-09-12 15:00:43 +0000532 this->__pbump(__nout);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533 __hm_ = this->pbase() + __hm;
534#ifndef _LIBCPP_NO_EXCEPTIONS
535 }
536 catch (...)
537 {
538 return traits_type::eof();
539 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000540#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000542 __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 if (__mode_ & ios_base::in)
544 {
545 char_type* __p = const_cast<char_type*>(__str_.data());
546 this->setg(__p, __p + __ninp, __hm_);
547 }
Marshall Clow5ca73ad2019-02-01 21:59:27 +0000548 return this->sputc(traits_type::to_char_type(__c));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 }
550 return traits_type::not_eof(__c);
551}
552
553template <class _CharT, class _Traits, class _Allocator>
554typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
555basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
556 ios_base::seekdir __way,
557 ios_base::openmode __wch)
558{
559 if (__hm_ < this->pptr())
560 __hm_ = this->pptr();
561 if ((__wch & (ios_base::in | ios_base::out)) == 0)
562 return pos_type(-1);
563 if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
564 && __way == ios_base::cur)
565 return pos_type(-1);
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000566 const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567 off_type __noff;
568 switch (__way)
569 {
570 case ios_base::beg:
571 __noff = 0;
572 break;
573 case ios_base::cur:
574 if (__wch & ios_base::in)
575 __noff = this->gptr() - this->eback();
576 else
577 __noff = this->pptr() - this->pbase();
578 break;
579 case ios_base::end:
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000580 __noff = __hm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 break;
582 default:
583 return pos_type(-1);
584 }
585 __noff += __off;
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000586 if (__noff < 0 || __hm < __noff)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 return pos_type(-1);
588 if (__noff != 0)
589 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500590 if ((__wch & ios_base::in) && this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 return pos_type(-1);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500592 if ((__wch & ios_base::out) && this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 return pos_type(-1);
594 }
595 if (__wch & ios_base::in)
596 this->setg(this->eback(), this->eback() + __noff, __hm_);
597 if (__wch & ios_base::out)
598 {
599 this->setp(this->pbase(), this->epptr());
600 this->pbump(__noff);
601 }
602 return pos_type(__noff);
603}
604
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605// basic_istringstream
606
607template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000608class _LIBCPP_TEMPLATE_VIS basic_istringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609 : public basic_istream<_CharT, _Traits>
610{
611public:
612 typedef _CharT char_type;
613 typedef _Traits traits_type;
614 typedef typename traits_type::int_type int_type;
615 typedef typename traits_type::pos_type pos_type;
616 typedef typename traits_type::off_type off_type;
617 typedef _Allocator allocator_type;
618
619 typedef basic_string<char_type, traits_type, allocator_type> string_type;
620
621private:
622 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
623
624public:
625 // 27.8.2.1 Constructors:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400626 _LIBCPP_INLINE_VISIBILITY
627 explicit basic_istringstream(ios_base::openmode __wch = ios_base::in)
628 : basic_istream<_CharT, _Traits>(&__sb_)
629 , __sb_(__wch | ios_base::in)
630 { }
631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 explicit basic_istringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400633 ios_base::openmode __wch = ios_base::in)
634 : basic_istream<_CharT, _Traits>(&__sb_)
635 , __sb_(__s, __wch | ios_base::in)
636 { }
637
638 _LIBCPP_INLINE_VISIBILITY
639 basic_istringstream(basic_istringstream&& __rhs)
640 : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
641 , __sb_(_VSTD::move(__rhs.__sb_))
642 {
643 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
644 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645
646 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400647 basic_istringstream& operator=(basic_istringstream&& __rhs) {
648 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
649 __sb_ = _VSTD::move(__rhs.__sb_);
650 return *this;
651 }
652 _LIBCPP_INLINE_VISIBILITY
653 void swap(basic_istringstream& __rhs) {
654 basic_istream<char_type, traits_type>::swap(__rhs);
655 __sb_.swap(__rhs.__sb_);
656 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657
658 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400659 _LIBCPP_INLINE_VISIBILITY
660 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
661 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
662 }
663 _LIBCPP_INLINE_VISIBILITY
664 string_type str() const {
665 return __sb_.str();
666 }
667 _LIBCPP_INLINE_VISIBILITY
668 void str(const string_type& __s) {
669 __sb_.str(__s);
670 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671};
672
673template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675void
676swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
677 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
678{
679 __x.swap(__y);
680}
681
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682// basic_ostringstream
683
684template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000685class _LIBCPP_TEMPLATE_VIS basic_ostringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 : public basic_ostream<_CharT, _Traits>
687{
688public:
689 typedef _CharT char_type;
690 typedef _Traits traits_type;
691 typedef typename traits_type::int_type int_type;
692 typedef typename traits_type::pos_type pos_type;
693 typedef typename traits_type::off_type off_type;
694 typedef _Allocator allocator_type;
695
696 typedef basic_string<char_type, traits_type, allocator_type> string_type;
697
698private:
699 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
700
701public:
702 // 27.8.2.1 Constructors:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400703 _LIBCPP_INLINE_VISIBILITY
704 explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out)
705 : basic_ostream<_CharT, _Traits>(&__sb_)
706 , __sb_(__wch | ios_base::out)
707 { }
708
709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710 explicit basic_ostringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400711 ios_base::openmode __wch = ios_base::out)
712 : basic_ostream<_CharT, _Traits>(&__sb_)
713 , __sb_(__s, __wch | ios_base::out)
714 { }
715
716 _LIBCPP_INLINE_VISIBILITY
717 basic_ostringstream(basic_ostringstream&& __rhs)
718 : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs))
719 , __sb_(_VSTD::move(__rhs.__sb_))
720 {
721 basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
722 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723
724 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400725 basic_ostringstream& operator=(basic_ostringstream&& __rhs) {
726 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
727 __sb_ = _VSTD::move(__rhs.__sb_);
728 return *this;
729 }
730
731 _LIBCPP_INLINE_VISIBILITY
732 void swap(basic_ostringstream& __rhs) {
733 basic_ostream<char_type, traits_type>::swap(__rhs);
734 __sb_.swap(__rhs.__sb_);
735 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736
737 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400738 _LIBCPP_INLINE_VISIBILITY
739 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
740 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
741 }
742 _LIBCPP_INLINE_VISIBILITY
743 string_type str() const {
744 return __sb_.str();
745 }
746 _LIBCPP_INLINE_VISIBILITY
747 void str(const string_type& __s) {
748 __sb_.str(__s);
749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750};
751
752template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000753inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754void
755swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
756 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
757{
758 __x.swap(__y);
759}
760
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761// basic_stringstream
762
763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000764class _LIBCPP_TEMPLATE_VIS basic_stringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 : public basic_iostream<_CharT, _Traits>
766{
767public:
768 typedef _CharT char_type;
769 typedef _Traits traits_type;
770 typedef typename traits_type::int_type int_type;
771 typedef typename traits_type::pos_type pos_type;
772 typedef typename traits_type::off_type off_type;
773 typedef _Allocator allocator_type;
774
775 typedef basic_string<char_type, traits_type, allocator_type> string_type;
776
777private:
778 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
779
780public:
781 // 27.8.2.1 Constructors:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400782 _LIBCPP_INLINE_VISIBILITY
783 explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out)
784 : basic_iostream<_CharT, _Traits>(&__sb_)
785 , __sb_(__wch)
786 { }
787
788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 explicit basic_stringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400790 ios_base::openmode __wch = ios_base::in | ios_base::out)
791 : basic_iostream<_CharT, _Traits>(&__sb_)
792 , __sb_(__s, __wch)
793 { }
794
795 _LIBCPP_INLINE_VISIBILITY
796 basic_stringstream(basic_stringstream&& __rhs)
797 : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs))
798 , __sb_(_VSTD::move(__rhs.__sb_))
799 {
800 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
801 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802
803 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400804 basic_stringstream& operator=(basic_stringstream&& __rhs) {
805 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
806 __sb_ = _VSTD::move(__rhs.__sb_);
807 return *this;
808 }
809 _LIBCPP_INLINE_VISIBILITY
810 void swap(basic_stringstream& __rhs) {
811 basic_iostream<char_type, traits_type>::swap(__rhs);
812 __sb_.swap(__rhs.__sb_);
813 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814
815 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400816 _LIBCPP_INLINE_VISIBILITY
817 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
818 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
819 }
820 _LIBCPP_INLINE_VISIBILITY
821 string_type str() const {
822 return __sb_.str();
823 }
824 _LIBCPP_INLINE_VISIBILITY
825 void str(const string_type& __s) {
826 __sb_.str(__s);
827 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828};
829
830template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832void
833swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
834 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
835{
836 __x.swap(__y);
837}
838
Louis Dionne8a79be62020-10-21 11:11:45 -0400839#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
840_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>)
841_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>)
842_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>)
843_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>)
844#endif
845
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846_LIBCPP_END_NAMESPACE_STD
847
Eric Fiselierf4433a32017-05-31 22:07:49 +0000848_LIBCPP_POP_MACROS
849
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850#endif // _LIBCPP_SSTREAM