blob: 0b614a0b095625bcfa613f0ff6d81312c9b32c46 [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
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +010028 // 27.8.1.1 [stringbuf.cons], constructors:
29 explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out); // before C++20
30 basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {} // C++20
31 explicit basic_stringbuf(ios_base::openmode which); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
33 ios_base::openmode which = ios_base::in | ios_base::out);
34 basic_stringbuf(basic_stringbuf&& rhs);
35
36 // 27.8.1.2 Assign and swap:
37 basic_stringbuf& operator=(basic_stringbuf&& rhs);
38 void swap(basic_stringbuf& rhs);
39
40 // 27.8.1.3 Get and set:
41 basic_string<char_type, traits_type, allocator_type> str() const;
42 void str(const basic_string<char_type, traits_type, allocator_type>& s);
43
44protected:
45 // 27.8.1.4 Overridden virtual functions:
46 virtual int_type underflow();
47 virtual int_type pbackfail(int_type c = traits_type::eof());
48 virtual int_type overflow (int_type c = traits_type::eof());
49 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
50 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
51 ios_base::openmode which = ios_base::in | ios_base::out);
52 virtual pos_type seekpos(pos_type sp,
53 ios_base::openmode which = ios_base::in | ios_base::out);
54};
55
56template <class charT, class traits, class Allocator>
57 void swap(basic_stringbuf<charT, traits, Allocator>& x,
58 basic_stringbuf<charT, traits, Allocator>& y);
59
60typedef basic_stringbuf<char> stringbuf;
61typedef basic_stringbuf<wchar_t> wstringbuf;
62
63template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
64class basic_istringstream
65 : public basic_istream<charT, traits>
66{
67public:
68 typedef charT char_type;
69 typedef traits traits_type;
70 typedef typename traits_type::int_type int_type;
71 typedef typename traits_type::pos_type pos_type;
72 typedef typename traits_type::off_type off_type;
73 typedef Allocator allocator_type;
74
75 // 27.8.2.1 Constructors:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +010076 explicit basic_istringstream(ios_base::openmode which = ios_base::in); // before C++20
77 basic_istringstream() : basic_istringstream(ios_base::in) {} // C++20
78 explicit basic_istringstream(ios_base::openmode which); // C++20
79
Howard Hinnantc51e1022010-05-11 19:42:16 +000080 explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
81 ios_base::openmode which = ios_base::in);
82 basic_istringstream(basic_istringstream&& rhs);
83
84 // 27.8.2.2 Assign and swap:
85 basic_istringstream& operator=(basic_istringstream&& rhs);
86 void swap(basic_istringstream& rhs);
87
88 // 27.8.2.3 Members:
89 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
90 basic_string<char_type, traits_type, allocator_type> str() const;
91 void str(const basic_string<char_type, traits_type, allocator_type>& s);
92};
93
94template <class charT, class traits, class Allocator>
95 void swap(basic_istringstream<charT, traits, Allocator>& x,
96 basic_istringstream<charT, traits, Allocator>& y);
97
98typedef basic_istringstream<char> istringstream;
99typedef basic_istringstream<wchar_t> wistringstream;
100
101template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
102class basic_ostringstream
103 : public basic_ostream<charT, traits>
104{
105public:
106 // types:
107 typedef charT char_type;
108 typedef traits traits_type;
109 typedef typename traits_type::int_type int_type;
110 typedef typename traits_type::pos_type pos_type;
111 typedef typename traits_type::off_type off_type;
112 typedef Allocator allocator_type;
113
114 // 27.8.3.1 Constructors/destructor:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100115 explicit basic_ostringstream(ios_base::openmode which = ios_base::out); // before C++20
116 basic_ostringstream() : basic_ostringstream(ios_base::out) {} // C++20
117 explicit basic_ostringstream(ios_base::openmode which); // C++20
118
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
120 ios_base::openmode which = ios_base::out);
121 basic_ostringstream(basic_ostringstream&& rhs);
122
123 // 27.8.3.2 Assign/swap:
124 basic_ostringstream& operator=(basic_ostringstream&& rhs);
125 void swap(basic_ostringstream& rhs);
126
127 // 27.8.3.3 Members:
128 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
129 basic_string<char_type, traits_type, allocator_type> str() const;
130 void str(const basic_string<char_type, traits_type, allocator_type>& s);
131};
132
133template <class charT, class traits, class Allocator>
134 void swap(basic_ostringstream<charT, traits, Allocator>& x,
135 basic_ostringstream<charT, traits, Allocator>& y);
136
137typedef basic_ostringstream<char> ostringstream;
138typedef basic_ostringstream<wchar_t> wostringstream;
139
140template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
141class basic_stringstream
142 : public basic_iostream<charT, traits>
143{
144public:
145 // types:
146 typedef charT char_type;
147 typedef traits traits_type;
148 typedef typename traits_type::int_type int_type;
149 typedef typename traits_type::pos_type pos_type;
150 typedef typename traits_type::off_type off_type;
151 typedef Allocator allocator_type;
152
153 // constructors/destructor
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100154 explicit basic_stringstream(ios_base::openmode which = ios_base::out | ios_base::in); // before C++20
155 basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {} // C++20
156 explicit basic_stringstream(ios_base::openmode which); // C++20
157
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
159 ios_base::openmode which = ios_base::out|ios_base::in);
160 basic_stringstream(basic_stringstream&& rhs);
161
162 // 27.8.5.1 Assign/swap:
163 basic_stringstream& operator=(basic_stringstream&& rhs);
164 void swap(basic_stringstream& rhs);
165
166 // Members:
167 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
168 basic_string<char_type, traits_type, allocator_type> str() const;
169 void str(const basic_string<char_type, traits_type, allocator_type>& str);
170};
171
172template <class charT, class traits, class Allocator>
173 void swap(basic_stringstream<charT, traits, Allocator>& x,
174 basic_stringstream<charT, traits, Allocator>& y);
175
176typedef basic_stringstream<char> stringstream;
177typedef basic_stringstream<wchar_t> wstringstream;
178
179} // std
180
181*/
182
183#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184#include <istream>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400185#include <ostream>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186#include <string>
187
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000188#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000190#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Eric Fiselierf4433a32017-05-31 22:07:49 +0000192_LIBCPP_PUSH_MACROS
193#include <__undef_macros>
194
195
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196_LIBCPP_BEGIN_NAMESPACE_STD
197
198// basic_stringbuf
199
200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000201class _LIBCPP_TEMPLATE_VIS basic_stringbuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 : public basic_streambuf<_CharT, _Traits>
203{
204public:
205 typedef _CharT char_type;
206 typedef _Traits traits_type;
207 typedef typename traits_type::int_type int_type;
208 typedef typename traits_type::pos_type pos_type;
209 typedef typename traits_type::off_type off_type;
210 typedef _Allocator allocator_type;
211
212 typedef basic_string<char_type, traits_type, allocator_type> string_type;
213
214private:
215
216 string_type __str_;
217 mutable char_type* __hm_;
218 ios_base::openmode __mode_;
219
220public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100221 // 30.8.2.1 [stringbuf.cons], constructors
222#ifndef _LIBCPP_CXX03_LANG
Louis Dionne6b6073b2020-10-09 14:21:23 -0400223 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100224 basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {}
225
226 _LIBCPP_INLINE_VISIBILITY
227 explicit basic_stringbuf(ios_base::openmode __wch)
228 : __hm_(nullptr), __mode_(__wch) {}
229#else
230 _LIBCPP_INLINE_VISIBILITY
231 explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in |
232 ios_base::out)
233 : __hm_(nullptr), __mode_(__wch) {}
234#endif
Louis Dionne6b6073b2020-10-09 14:21:23 -0400235
236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 explicit basic_stringbuf(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400238 ios_base::openmode __wch = ios_base::in | ios_base::out)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500239 : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch)
Louis Dionne6b6073b2020-10-09 14:21:23 -0400240 {
241 str(__s);
242 }
243
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_stringbuf(basic_stringbuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
246 // 27.8.1.2 Assign and swap:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_stringbuf& operator=(basic_stringbuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 void swap(basic_stringbuf& __rhs);
249
250 // 27.8.1.3 Get and set:
251 string_type str() const;
252 void str(const string_type& __s);
253
254protected:
255 // 27.8.1.4 Overridden virtual functions:
256 virtual int_type underflow();
257 virtual int_type pbackfail(int_type __c = traits_type::eof());
258 virtual int_type overflow (int_type __c = traits_type::eof());
259 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
260 ios_base::openmode __wch = ios_base::in | ios_base::out);
Louis Dionne6b6073b2020-10-09 14:21:23 -0400261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 virtual pos_type seekpos(pos_type __sp,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400263 ios_base::openmode __wch = ios_base::in | ios_base::out) {
264 return seekoff(__sp, ios_base::beg, __wch);
265 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266};
267
268template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
270 : __mode_(__rhs.__mode_)
271{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000272 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
273 ptrdiff_t __binp = -1;
274 ptrdiff_t __ninp = -1;
275 ptrdiff_t __einp = -1;
276 if (__rhs.eback() != nullptr)
277 {
278 __binp = __rhs.eback() - __p;
279 __ninp = __rhs.gptr() - __p;
280 __einp = __rhs.egptr() - __p;
281 }
282 ptrdiff_t __bout = -1;
283 ptrdiff_t __nout = -1;
284 ptrdiff_t __eout = -1;
285 if (__rhs.pbase() != nullptr)
286 {
287 __bout = __rhs.pbase() - __p;
288 __nout = __rhs.pptr() - __p;
289 __eout = __rhs.epptr() - __p;
290 }
291 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000292 __str_ = _VSTD::move(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000293 __p = const_cast<char_type*>(__str_.data());
294 if (__binp != -1)
295 this->setg(__p + __binp, __p + __ninp, __p + __einp);
296 if (__bout != -1)
297 {
298 this->setp(__p + __bout, __p + __eout);
Marshall Clow33932622017-09-12 15:00:43 +0000299 this->__pbump(__nout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000300 }
301 __hm_ = __hm == -1 ? nullptr : __p + __hm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 __p = const_cast<char_type*>(__rhs.__str_.data());
303 __rhs.setg(__p, __p, __p);
304 __rhs.setp(__p, __p);
305 __rhs.__hm_ = __p;
306 this->pubimbue(__rhs.getloc());
307}
308
309template <class _CharT, class _Traits, class _Allocator>
310basic_stringbuf<_CharT, _Traits, _Allocator>&
311basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
312{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000313 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
314 ptrdiff_t __binp = -1;
315 ptrdiff_t __ninp = -1;
316 ptrdiff_t __einp = -1;
317 if (__rhs.eback() != nullptr)
318 {
319 __binp = __rhs.eback() - __p;
320 __ninp = __rhs.gptr() - __p;
321 __einp = __rhs.egptr() - __p;
322 }
323 ptrdiff_t __bout = -1;
324 ptrdiff_t __nout = -1;
325 ptrdiff_t __eout = -1;
326 if (__rhs.pbase() != nullptr)
327 {
328 __bout = __rhs.pbase() - __p;
329 __nout = __rhs.pptr() - __p;
330 __eout = __rhs.epptr() - __p;
331 }
332 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000333 __str_ = _VSTD::move(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000334 __p = const_cast<char_type*>(__str_.data());
335 if (__binp != -1)
336 this->setg(__p + __binp, __p + __ninp, __p + __einp);
Marshall Clow45e157b2014-09-16 18:57:52 +0000337 else
338 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000339 if (__bout != -1)
340 {
341 this->setp(__p + __bout, __p + __eout);
Marshall Clow33932622017-09-12 15:00:43 +0000342 this->__pbump(__nout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000343 }
Marshall Clow45e157b2014-09-16 18:57:52 +0000344 else
345 this->setp(nullptr, nullptr);
346
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000347 __hm_ = __hm == -1 ? nullptr : __p + __hm;
348 __mode_ = __rhs.__mode_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349 __p = const_cast<char_type*>(__rhs.__str_.data());
350 __rhs.setg(__p, __p, __p);
351 __rhs.setp(__p, __p);
352 __rhs.__hm_ = __p;
353 this->pubimbue(__rhs.getloc());
354 return *this;
355}
356
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357template <class _CharT, class _Traits, class _Allocator>
358void
359basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
360{
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000361 char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
362 ptrdiff_t __rbinp = -1;
363 ptrdiff_t __rninp = -1;
364 ptrdiff_t __reinp = -1;
365 if (__rhs.eback() != nullptr)
366 {
367 __rbinp = __rhs.eback() - __p;
368 __rninp = __rhs.gptr() - __p;
369 __reinp = __rhs.egptr() - __p;
370 }
371 ptrdiff_t __rbout = -1;
372 ptrdiff_t __rnout = -1;
373 ptrdiff_t __reout = -1;
374 if (__rhs.pbase() != nullptr)
375 {
376 __rbout = __rhs.pbase() - __p;
377 __rnout = __rhs.pptr() - __p;
378 __reout = __rhs.epptr() - __p;
379 }
380 ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
381 __p = const_cast<char_type*>(__str_.data());
382 ptrdiff_t __lbinp = -1;
383 ptrdiff_t __lninp = -1;
384 ptrdiff_t __leinp = -1;
385 if (this->eback() != nullptr)
386 {
387 __lbinp = this->eback() - __p;
388 __lninp = this->gptr() - __p;
389 __leinp = this->egptr() - __p;
390 }
391 ptrdiff_t __lbout = -1;
392 ptrdiff_t __lnout = -1;
393 ptrdiff_t __leout = -1;
394 if (this->pbase() != nullptr)
395 {
396 __lbout = this->pbase() - __p;
397 __lnout = this->pptr() - __p;
398 __leout = this->epptr() - __p;
399 }
400 ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000401 _VSTD::swap(__mode_, __rhs.__mode_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402 __str_.swap(__rhs.__str_);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000403 __p = const_cast<char_type*>(__str_.data());
404 if (__rbinp != -1)
405 this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
406 else
407 this->setg(nullptr, nullptr, nullptr);
408 if (__rbout != -1)
409 {
410 this->setp(__p + __rbout, __p + __reout);
Marshall Clow33932622017-09-12 15:00:43 +0000411 this->__pbump(__rnout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000412 }
413 else
414 this->setp(nullptr, nullptr);
415 __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 __p = const_cast<char_type*>(__rhs.__str_.data());
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000417 if (__lbinp != -1)
418 __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
419 else
420 __rhs.setg(nullptr, nullptr, nullptr);
421 if (__lbout != -1)
422 {
423 __rhs.setp(__p + __lbout, __p + __leout);
Marshall Clow33932622017-09-12 15:00:43 +0000424 __rhs.__pbump(__lnout);
Howard Hinnantd6d6e102013-04-03 20:21:29 +0000425 }
426 else
427 __rhs.setp(nullptr, nullptr);
428 __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 locale __tl = __rhs.getloc();
430 __rhs.pubimbue(this->getloc());
431 this->pubimbue(__tl);
432}
433
434template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436void
437swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
438 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
439{
440 __x.swap(__y);
441}
442
443template <class _CharT, class _Traits, class _Allocator>
444basic_string<_CharT, _Traits, _Allocator>
445basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
446{
447 if (__mode_ & ios_base::out)
448 {
449 if (__hm_ < this->pptr())
450 __hm_ = this->pptr();
451 return string_type(this->pbase(), __hm_, __str_.get_allocator());
452 }
453 else if (__mode_ & ios_base::in)
454 return string_type(this->eback(), this->egptr(), __str_.get_allocator());
455 return string_type(__str_.get_allocator());
456}
457
458template <class _CharT, class _Traits, class _Allocator>
459void
460basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
461{
462 __str_ = __s;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500463 __hm_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 if (__mode_ & ios_base::in)
465 {
466 __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
467 this->setg(const_cast<char_type*>(__str_.data()),
468 const_cast<char_type*>(__str_.data()),
469 __hm_);
470 }
471 if (__mode_ & ios_base::out)
472 {
473 typename string_type::size_type __sz = __str_.size();
474 __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
475 __str_.resize(__str_.capacity());
476 this->setp(const_cast<char_type*>(__str_.data()),
477 const_cast<char_type*>(__str_.data()) + __str_.size());
478 if (__mode_ & (ios_base::app | ios_base::ate))
Marshall Clow33932622017-09-12 15:00:43 +0000479 {
480 while (__sz > INT_MAX)
481 {
Louis Dionne44bcff92018-08-03 22:36:53 +0000482 this->pbump(INT_MAX);
483 __sz -= INT_MAX;
Marshall Clow33932622017-09-12 15:00:43 +0000484 }
485 if (__sz > 0)
Louis Dionne44bcff92018-08-03 22:36:53 +0000486 this->pbump(__sz);
487 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 }
489}
490
491template <class _CharT, class _Traits, class _Allocator>
492typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
493basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
494{
495 if (__hm_ < this->pptr())
496 __hm_ = this->pptr();
497 if (__mode_ & ios_base::in)
498 {
499 if (this->egptr() < __hm_)
500 this->setg(this->eback(), this->gptr(), __hm_);
501 if (this->gptr() < this->egptr())
502 return traits_type::to_int_type(*this->gptr());
503 }
504 return traits_type::eof();
505}
506
507template <class _CharT, class _Traits, class _Allocator>
508typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
509basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
510{
511 if (__hm_ < this->pptr())
512 __hm_ = this->pptr();
513 if (this->eback() < this->gptr())
514 {
515 if (traits_type::eq_int_type(__c, traits_type::eof()))
516 {
517 this->setg(this->eback(), this->gptr()-1, __hm_);
518 return traits_type::not_eof(__c);
519 }
520 if ((__mode_ & ios_base::out) ||
521 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
522 {
523 this->setg(this->eback(), this->gptr()-1, __hm_);
524 *this->gptr() = traits_type::to_char_type(__c);
525 return __c;
526 }
527 }
528 return traits_type::eof();
529}
530
531template <class _CharT, class _Traits, class _Allocator>
532typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
533basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
534{
535 if (!traits_type::eq_int_type(__c, traits_type::eof()))
536 {
537 ptrdiff_t __ninp = this->gptr() - this->eback();
538 if (this->pptr() == this->epptr())
539 {
540 if (!(__mode_ & ios_base::out))
541 return traits_type::eof();
542#ifndef _LIBCPP_NO_EXCEPTIONS
543 try
544 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400545#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546 ptrdiff_t __nout = this->pptr() - this->pbase();
547 ptrdiff_t __hm = __hm_ - this->pbase();
548 __str_.push_back(char_type());
549 __str_.resize(__str_.capacity());
550 char_type* __p = const_cast<char_type*>(__str_.data());
551 this->setp(__p, __p + __str_.size());
Marshall Clow33932622017-09-12 15:00:43 +0000552 this->__pbump(__nout);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553 __hm_ = this->pbase() + __hm;
554#ifndef _LIBCPP_NO_EXCEPTIONS
555 }
556 catch (...)
557 {
558 return traits_type::eof();
559 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400560#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000562 __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 if (__mode_ & ios_base::in)
564 {
565 char_type* __p = const_cast<char_type*>(__str_.data());
566 this->setg(__p, __p + __ninp, __hm_);
567 }
Marshall Clow5ca73ad2019-02-01 21:59:27 +0000568 return this->sputc(traits_type::to_char_type(__c));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 }
570 return traits_type::not_eof(__c);
571}
572
573template <class _CharT, class _Traits, class _Allocator>
574typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
575basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
576 ios_base::seekdir __way,
577 ios_base::openmode __wch)
578{
579 if (__hm_ < this->pptr())
580 __hm_ = this->pptr();
581 if ((__wch & (ios_base::in | ios_base::out)) == 0)
582 return pos_type(-1);
583 if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
584 && __way == ios_base::cur)
585 return pos_type(-1);
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000586 const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 off_type __noff;
588 switch (__way)
589 {
590 case ios_base::beg:
591 __noff = 0;
592 break;
593 case ios_base::cur:
594 if (__wch & ios_base::in)
595 __noff = this->gptr() - this->eback();
596 else
597 __noff = this->pptr() - this->pbase();
598 break;
599 case ios_base::end:
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000600 __noff = __hm;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601 break;
602 default:
603 return pos_type(-1);
604 }
605 __noff += __off;
Peter Collingbourne28b5a562017-12-19 23:33:16 +0000606 if (__noff < 0 || __hm < __noff)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 return pos_type(-1);
608 if (__noff != 0)
609 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500610 if ((__wch & ios_base::in) && this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 return pos_type(-1);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500612 if ((__wch & ios_base::out) && this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613 return pos_type(-1);
614 }
615 if (__wch & ios_base::in)
616 this->setg(this->eback(), this->eback() + __noff, __hm_);
617 if (__wch & ios_base::out)
618 {
619 this->setp(this->pbase(), this->epptr());
620 this->pbump(__noff);
621 }
622 return pos_type(__noff);
623}
624
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625// basic_istringstream
626
627template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000628class _LIBCPP_TEMPLATE_VIS basic_istringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629 : public basic_istream<_CharT, _Traits>
630{
631public:
632 typedef _CharT char_type;
633 typedef _Traits traits_type;
634 typedef typename traits_type::int_type int_type;
635 typedef typename traits_type::pos_type pos_type;
636 typedef typename traits_type::off_type off_type;
637 typedef _Allocator allocator_type;
638
639 typedef basic_string<char_type, traits_type, allocator_type> string_type;
640
641private:
642 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
643
644public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100645 // 30.8.3.1 [istringstream.cons], constructors
646#ifndef _LIBCPP_CXX03_LANG
647 _LIBCPP_INLINE_VISIBILITY
648 basic_istringstream() : basic_istringstream(ios_base::in) {}
649
650 _LIBCPP_INLINE_VISIBILITY
651 explicit basic_istringstream(ios_base::openmode __wch)
652 : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {}
653#else
Louis Dionne6b6073b2020-10-09 14:21:23 -0400654 _LIBCPP_INLINE_VISIBILITY
655 explicit basic_istringstream(ios_base::openmode __wch = ios_base::in)
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100656 : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {}
657#endif
658
Louis Dionne6b6073b2020-10-09 14:21:23 -0400659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 explicit basic_istringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400661 ios_base::openmode __wch = ios_base::in)
662 : basic_istream<_CharT, _Traits>(&__sb_)
663 , __sb_(__s, __wch | ios_base::in)
664 { }
665
666 _LIBCPP_INLINE_VISIBILITY
667 basic_istringstream(basic_istringstream&& __rhs)
668 : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
669 , __sb_(_VSTD::move(__rhs.__sb_))
670 {
671 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
672 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
674 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400675 basic_istringstream& operator=(basic_istringstream&& __rhs) {
676 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
677 __sb_ = _VSTD::move(__rhs.__sb_);
678 return *this;
679 }
680 _LIBCPP_INLINE_VISIBILITY
681 void swap(basic_istringstream& __rhs) {
682 basic_istream<char_type, traits_type>::swap(__rhs);
683 __sb_.swap(__rhs.__sb_);
684 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685
686 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400687 _LIBCPP_INLINE_VISIBILITY
688 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
689 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
690 }
691 _LIBCPP_INLINE_VISIBILITY
692 string_type str() const {
693 return __sb_.str();
694 }
695 _LIBCPP_INLINE_VISIBILITY
696 void str(const string_type& __s) {
697 __sb_.str(__s);
698 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699};
700
701template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703void
704swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
705 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
706{
707 __x.swap(__y);
708}
709
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710// basic_ostringstream
711
712template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000713class _LIBCPP_TEMPLATE_VIS basic_ostringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 : public basic_ostream<_CharT, _Traits>
715{
716public:
717 typedef _CharT char_type;
718 typedef _Traits traits_type;
719 typedef typename traits_type::int_type int_type;
720 typedef typename traits_type::pos_type pos_type;
721 typedef typename traits_type::off_type off_type;
722 typedef _Allocator allocator_type;
723
724 typedef basic_string<char_type, traits_type, allocator_type> string_type;
725
726private:
727 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
728
729public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100730 // 30.8.4.1 [ostringstream.cons], constructors
731#ifndef _LIBCPP_CXX03_LANG
732 _LIBCPP_INLINE_VISIBILITY
733 basic_ostringstream() : basic_ostringstream(ios_base::out) {}
734
735 _LIBCPP_INLINE_VISIBILITY
736 explicit basic_ostringstream(ios_base::openmode __wch)
737 : basic_ostream<_CharT, _Traits>(&__sb_),
738 __sb_(__wch | ios_base::out) {}
739#else
Louis Dionne6b6073b2020-10-09 14:21:23 -0400740 _LIBCPP_INLINE_VISIBILITY
741 explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out)
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100742 : basic_ostream<_CharT, _Traits>(&__sb_),
743 __sb_(__wch | ios_base::out) {}
744#endif
Louis Dionne6b6073b2020-10-09 14:21:23 -0400745
746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 explicit basic_ostringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400748 ios_base::openmode __wch = ios_base::out)
749 : basic_ostream<_CharT, _Traits>(&__sb_)
750 , __sb_(__s, __wch | ios_base::out)
751 { }
752
753 _LIBCPP_INLINE_VISIBILITY
754 basic_ostringstream(basic_ostringstream&& __rhs)
755 : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs))
756 , __sb_(_VSTD::move(__rhs.__sb_))
757 {
758 basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
759 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760
761 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400762 basic_ostringstream& operator=(basic_ostringstream&& __rhs) {
763 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
764 __sb_ = _VSTD::move(__rhs.__sb_);
765 return *this;
766 }
767
768 _LIBCPP_INLINE_VISIBILITY
769 void swap(basic_ostringstream& __rhs) {
770 basic_ostream<char_type, traits_type>::swap(__rhs);
771 __sb_.swap(__rhs.__sb_);
772 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773
774 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400775 _LIBCPP_INLINE_VISIBILITY
776 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
777 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
778 }
779 _LIBCPP_INLINE_VISIBILITY
780 string_type str() const {
781 return __sb_.str();
782 }
783 _LIBCPP_INLINE_VISIBILITY
784 void str(const string_type& __s) {
785 __sb_.str(__s);
786 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787};
788
789template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000790inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791void
792swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
793 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
794{
795 __x.swap(__y);
796}
797
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798// basic_stringstream
799
800template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000801class _LIBCPP_TEMPLATE_VIS basic_stringstream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 : public basic_iostream<_CharT, _Traits>
803{
804public:
805 typedef _CharT char_type;
806 typedef _Traits traits_type;
807 typedef typename traits_type::int_type int_type;
808 typedef typename traits_type::pos_type pos_type;
809 typedef typename traits_type::off_type off_type;
810 typedef _Allocator allocator_type;
811
812 typedef basic_string<char_type, traits_type, allocator_type> string_type;
813
814private:
815 basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
816
817public:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100818 // 30.8.5.1 [stringstream.cons], constructors
819#ifndef _LIBCPP_CXX03_LANG
Louis Dionne6b6073b2020-10-09 14:21:23 -0400820 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100821 basic_stringstream() : basic_stringstream(ios_base::in | ios_base::out) {}
822
823 _LIBCPP_INLINE_VISIBILITY
824 explicit basic_stringstream(ios_base::openmode __wch)
825 : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {}
826#else
827 _LIBCPP_INLINE_VISIBILITY
828 explicit basic_stringstream(ios_base::openmode __wch = ios_base::in |
829 ios_base::out)
830 : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {}
831#endif
Louis Dionne6b6073b2020-10-09 14:21:23 -0400832
833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 explicit basic_stringstream(const string_type& __s,
Louis Dionne6b6073b2020-10-09 14:21:23 -0400835 ios_base::openmode __wch = ios_base::in | ios_base::out)
836 : basic_iostream<_CharT, _Traits>(&__sb_)
837 , __sb_(__s, __wch)
838 { }
839
840 _LIBCPP_INLINE_VISIBILITY
841 basic_stringstream(basic_stringstream&& __rhs)
842 : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs))
843 , __sb_(_VSTD::move(__rhs.__sb_))
844 {
845 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
846 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847
848 // 27.8.2.2 Assign and swap:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400849 basic_stringstream& operator=(basic_stringstream&& __rhs) {
850 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
851 __sb_ = _VSTD::move(__rhs.__sb_);
852 return *this;
853 }
854 _LIBCPP_INLINE_VISIBILITY
855 void swap(basic_stringstream& __rhs) {
856 basic_iostream<char_type, traits_type>::swap(__rhs);
857 __sb_.swap(__rhs.__sb_);
858 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859
860 // 27.8.2.3 Members:
Louis Dionne6b6073b2020-10-09 14:21:23 -0400861 _LIBCPP_INLINE_VISIBILITY
862 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const {
863 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
864 }
865 _LIBCPP_INLINE_VISIBILITY
866 string_type str() const {
867 return __sb_.str();
868 }
869 _LIBCPP_INLINE_VISIBILITY
870 void str(const string_type& __s) {
871 __sb_.str(__s);
872 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873};
874
875template <class _CharT, class _Traits, class _Allocator>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +0000876inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877void
878swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
879 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
880{
881 __x.swap(__y);
882}
883
Louis Dionne8a79be62020-10-21 11:11:45 -0400884#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
885_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>)
886_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>)
887_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>)
888_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>)
889#endif
890
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891_LIBCPP_END_NAMESPACE_STD
892
Eric Fiselierf4433a32017-05-31 22:07:49 +0000893_LIBCPP_POP_MACROS
894
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400895#endif // _LIBCPP_SSTREAM