libstdc++
sstream
Go to the documentation of this file.
1// String based streams -*- C++ -*-
2
3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/sstream
26 * This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 27.7 String-based streams
31//
32
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
35
36#ifdef _GLIBCXX_SYSHDR
37#pragma GCC system_header
38#endif
39
40#include <bits/requires_hosted.h> // iostream
41
42#include <istream>
43#include <ostream>
44#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
45
46#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
47# define _GLIBCXX_LVAL_REF_QUAL &
48# define _GLIBCXX_SSTREAM_ALWAYS_INLINE
49#else
50# define _GLIBCXX_LVAL_REF_QUAL
51// For symbols that are not exported from libstdc++.so for the COW string ABI.
52# define _GLIBCXX_SSTREAM_ALWAYS_INLINE [[__gnu__::__always_inline__]]
53#endif
54
55
56
57namespace std _GLIBCXX_VISIBILITY(default)
58{
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
60_GLIBCXX_BEGIN_NAMESPACE_CXX11
61
62 // [27.7.1] template class basic_stringbuf
63 /**
64 * @brief The actual work of input and output (for std::string).
65 * @ingroup io
66 *
67 * @tparam _CharT Type of character stream.
68 * @tparam _Traits Traits for character type, defaults to
69 * char_traits<_CharT>.
70 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
71 *
72 * This class associates either or both of its input and output sequences
73 * with a sequence of characters, which can be initialized from, or made
74 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
75 *
76 * For this class, open modes (of type @c ios_base::openmode) have
77 * @c in set if the input sequence can be read, and @c out set if the
78 * output sequence can be written.
79 */
80 template<typename _CharT, typename _Traits, typename _Alloc>
81 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
82 {
83 struct __xfer_bufptrs;
84
85#if __cplusplus >= 201103L
86 using allocator_traits = std::allocator_traits<_Alloc>;
87 using _Noexcept_swap
90#endif
91
92 public:
93 // Types:
94 typedef _CharT char_type;
95 typedef _Traits traits_type;
96 // _GLIBCXX_RESOLVE_LIB_DEFECTS
97 // 251. basic_stringbuf missing allocator_type
98 typedef _Alloc allocator_type;
99 typedef typename traits_type::int_type int_type;
100 typedef typename traits_type::pos_type pos_type;
101 typedef typename traits_type::off_type off_type;
102
103 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
104 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
105 typedef typename __string_type::size_type __size_type;
106
107 protected:
108 /// Place to stash in || out || in | out settings for current stringbuf.
110
111 // Data Members:
112 __string_type _M_string;
113
114 public:
115 // Constructors:
116
117 /**
118 * @brief Starts with an empty string buffer.
119 *
120 * The default constructor initializes the parent class using its
121 * own default ctor.
122 */
124 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
125 { }
126
127 /**
128 * @brief Starts with an empty string buffer.
129 * @param __mode Whether the buffer can read, or write, or both.
130 *
131 * The default constructor initializes the parent class using its
132 * own default ctor.
133 */
134 explicit
136 : __streambuf_type(), _M_mode(__mode), _M_string()
137 { }
138
139 /**
140 * @brief Starts with an existing string buffer.
141 * @param __str A string to copy as a starting buffer.
142 * @param __mode Whether the buffer can read, or write, or both.
143 *
144 * This constructor initializes the parent class using its
145 * own default ctor.
146 */
147 explicit
148 basic_stringbuf(const __string_type& __str,
150 : __streambuf_type(), _M_mode(),
151 _M_string(__str.data(), __str.size(), __str.get_allocator())
152 { _M_stringbuf_init(__mode); }
153
154#if __cplusplus >= 201103L
155 basic_stringbuf(const basic_stringbuf&) = delete;
156
158 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
159 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
160
161#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
162 explicit
163 basic_stringbuf(const allocator_type& __a)
164 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
165 { }
166
168 const allocator_type& __a)
169 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
170 { }
171
172 explicit
173 basic_stringbuf(__string_type&& __s,
176 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
177 { _M_stringbuf_init(__mode); }
178
179 template<typename _SAlloc>
180 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
181 const allocator_type& __a)
182 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a)
183 { }
184
185 template<typename _SAlloc>
186 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
187 ios_base::openmode __mode,
188 const allocator_type& __a)
189 : __streambuf_type(), _M_mode(__mode),
190 _M_string(__s.data(), __s.size(), __a)
191 { _M_stringbuf_init(__mode); }
192
193 template<typename _SAlloc>
194 explicit
195 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
198 : basic_stringbuf(__s, __mode, allocator_type{})
199 { }
200
201 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
202 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
203 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
204
205 allocator_type get_allocator() const noexcept
206 { return _M_string.get_allocator(); }
207#endif // C++20
208
209 // 27.8.2.2 Assign and swap:
210
212 operator=(const basic_stringbuf&) = delete;
213
215 operator=(basic_stringbuf&& __rhs)
216 {
217 __xfer_bufptrs __st{__rhs, this};
218 const __streambuf_type& __base = __rhs;
219 __streambuf_type::operator=(__base);
220 this->pubimbue(__rhs.getloc());
221 _M_mode = __rhs._M_mode;
222 _M_string = std::move(__rhs._M_string);
223 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
224 return *this;
225 }
226
227 void
228 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
229 {
230 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
231 __xfer_bufptrs __r_st{__rhs, this};
232 __streambuf_type& __base = __rhs;
233 __streambuf_type::swap(__base);
234 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
235 std::swap(_M_mode, __rhs._M_mode);
236 std::swap(_M_string, __rhs._M_string); // XXX not exception safe
237 }
238#endif // C++11
239
240 // Getters and setters:
241
242 /**
243 * @brief Copying out the string buffer.
244 * @return A copy of one of the underlying sequences.
245 *
246 * <em>If the buffer is only created in input mode, the underlying
247 * character sequence is equal to the input sequence; otherwise, it
248 * is equal to the output sequence.</em> [27.7.1.2]/1
249 */
250 _GLIBCXX_NODISCARD
251 __string_type
252 str() const _GLIBCXX_LVAL_REF_QUAL
253 {
254 __string_type __ret(_M_string.get_allocator());
255 if (char_type* __hi = _M_high_mark())
256 __ret.assign(this->pbase(), __hi);
257 else
258 __ret = _M_string;
259 return __ret;
260 }
261
262#if __cplusplus > 201703L
263#if _GLIBCXX_USE_CXX11_ABI
264#if __cpp_concepts
265 template<__allocator_like _SAlloc>
266 _GLIBCXX_NODISCARD
268 str(const _SAlloc& __sa) const
269 {
270 auto __sv = view();
271 return { __sv.data(), __sv.size(), __sa };
272 }
273#endif
274
275 _GLIBCXX_NODISCARD
276 __string_type
277 str() &&
278 {
279 if (char_type* __hi = _M_high_mark())
280 {
281 if (_M_string.data() == this->pbase()) [[likely]]
282 // Set length to end of sequence and add null terminator.
283 _M_string._M_set_length(__hi - this->pbase());
284 else
285 _M_string.assign(this->pbase(), __hi);
286 }
287 auto __str = std::move(_M_string);
288 _M_string.clear();
289 _M_sync(_M_string.data(), 0, 0);
290 return __str;
291 }
292#endif // cxx11 ABI
293
294 _GLIBCXX_SSTREAM_ALWAYS_INLINE
295 basic_string_view<char_type, traits_type>
296 view() const noexcept
297 {
298 if (char_type* __hi = _M_high_mark())
299 return { this->pbase(), __hi };
300 else
301 return _M_string;
302 }
303#endif // C++20
304
305 /**
306 * @brief Setting a new buffer.
307 * @param __s The string to use as a new sequence.
308 *
309 * Deallocates any previous stored sequence, then copies @a s to
310 * use as a new one.
311 */
312 void
313 str(const __string_type& __s)
314 {
315 // Cannot use _M_string = __s, since v3 strings are COW
316 // (not always true now but assign() always works).
317 _M_string.assign(__s.data(), __s.size());
318 _M_stringbuf_init(_M_mode);
319 }
320
321#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
322#if __cpp_concepts
323 template<__allocator_like _SAlloc>
324 requires (!is_same_v<_SAlloc, _Alloc>)
325 void
327 {
328 _M_string.assign(__s.data(), __s.size());
329 _M_stringbuf_init(_M_mode);
330 }
331#endif
332
333 void
334 str(__string_type&& __s)
335 {
336 _M_string = std::move(__s);
337 _M_stringbuf_init(_M_mode);
338 }
339#endif
340
341 protected:
342 // Common initialization code goes here.
343 void
344 _M_stringbuf_init(ios_base::openmode __mode)
345 {
346 _M_mode = __mode;
347 __size_type __len = 0;
349 __len = _M_string.size();
350 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
351 }
352
354 showmanyc()
355 {
356 streamsize __ret = -1;
357 if (_M_mode & ios_base::in)
358 {
359 _M_update_egptr();
360 __ret = this->egptr() - this->gptr();
361 }
362 return __ret;
363 }
364
365 virtual int_type
366 underflow();
367
368 virtual int_type
369 pbackfail(int_type __c = traits_type::eof());
370
371 virtual int_type
372 overflow(int_type __c = traits_type::eof());
373
374 /**
375 * @brief Manipulates the buffer.
376 * @param __s Pointer to a buffer area.
377 * @param __n Size of @a __s.
378 * @return @c this
379 *
380 * If no buffer has already been created, and both @a __s and @a __n are
381 * non-zero, then @c __s is used as a buffer; see
382 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
383 * for more.
384 */
385 virtual __streambuf_type*
386 setbuf(char_type* __s, streamsize __n)
387 {
388 if (__s && __n >= 0)
389 {
390 // This is implementation-defined behavior, and assumes
391 // that an external char_type array of length __n exists
392 // and has been pre-allocated. If this is not the case,
393 // things will quickly blow up.
394
395 // Step 1: Destroy the current internal array.
396 _M_string.clear();
397
398 // Step 2: Use the external array.
399 _M_sync(__s, __n, 0);
400 }
401 return this;
402 }
403
404 virtual pos_type
405 seekoff(off_type __off, ios_base::seekdir __way,
407
408 virtual pos_type
409 seekpos(pos_type __sp,
411
412 // Internal function for correctly updating the internal buffer
413 // for a particular _M_string, due to initialization or re-sizing
414 // of an existing _M_string.
415 void
416 _M_sync(char_type* __base, __size_type __i, __size_type __o);
417
418 // Internal function for correctly updating egptr() to the actual
419 // string end.
420 void
421 _M_update_egptr()
422 {
423 if (char_type* __pptr = this->pptr())
424 {
425 char_type* __egptr = this->egptr();
426 if (!__egptr || __pptr > __egptr)
427 {
428 if (_M_mode & ios_base::in)
429 this->setg(this->eback(), this->gptr(), __pptr);
430 else
431 this->setg(__pptr, __pptr, __pptr);
432 }
433 }
434 }
435
436 // Works around the issue with pbump, part of the protected
437 // interface of basic_streambuf, taking just an int.
438 void
439 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
440
441 private:
442 // Return a pointer to the end of the underlying character sequence.
443 // This might not be the same character as _M_string.end() because
444 // basic_stringbuf::overflow might have written to unused capacity
445 // in _M_string without updating its length.
446 __attribute__((__always_inline__))
447 char_type*
448 _M_high_mark() const _GLIBCXX_NOEXCEPT
449 {
450 if (char_type* __pptr = this->pptr())
451 {
452 char_type* __egptr = this->egptr();
453 if (!__egptr || __pptr > __egptr)
454 return __pptr; // Underlying sequence is [pbase, pptr).
455 else
456 return __egptr; // Underlying sequence is [pbase, egptr).
457 }
458 return 0; // Underlying character sequence is just _M_string.
459 }
460
461#if __cplusplus >= 201103L
462#if _GLIBCXX_USE_CXX11_ABI
463 // This type captures the state of the gptr / pptr pointers as offsets
464 // so they can be restored in another object after moving the string.
465 struct __xfer_bufptrs
466 {
467 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
468 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
469 {
470 const _CharT* const __str = __from._M_string.data();
471 const _CharT* __end = nullptr;
472 if (__from.eback())
473 {
474 _M_goff[0] = __from.eback() - __str;
475 _M_goff[1] = __from.gptr() - __str;
476 _M_goff[2] = __from.egptr() - __str;
477 __end = __from.egptr();
478 }
479 if (__from.pbase())
480 {
481 _M_poff[0] = __from.pbase() - __str;
482 _M_poff[1] = __from.pptr() - __from.pbase();
483 _M_poff[2] = __from.epptr() - __str;
484 if (!__end || __from.pptr() > __end)
485 __end = __from.pptr();
486 }
487
488 // Set _M_string length to the greater of the get and put areas.
489 if (__end)
490 {
491 // The const_cast avoids changing this constructor's signature,
492 // because it is exported from the dynamic library.
493 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
494 __mut_from._M_string._M_length(__end - __str);
495 }
496 }
497
498 ~__xfer_bufptrs()
499 {
500 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
501 if (_M_goff[0] != -1)
502 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
503 if (_M_poff[0] != -1)
504 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
505 }
506
507 basic_stringbuf* _M_to;
508 off_type _M_goff[3];
509 off_type _M_poff[3];
510 };
511#else
512 // This type does nothing when using Copy-On-Write strings.
513 struct __xfer_bufptrs
514 {
515 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
516 };
517#endif
518
519 // The move constructor initializes an __xfer_bufptrs temporary then
520 // delegates to this constructor to performs moves during its lifetime.
521 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
522 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
523 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
524 { }
525
526#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
527 // The move constructor initializes an __xfer_bufptrs temporary then
528 // delegates to this constructor to performs moves during its lifetime.
529 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
530 __xfer_bufptrs&&)
531 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
532 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
533 { }
534#endif
535#endif // C++11
536 };
537
538
539 // [27.7.2] Template class basic_istringstream
540 /**
541 * @brief Controlling input for std::string.
542 * @ingroup io
543 *
544 * @tparam _CharT Type of character stream.
545 * @tparam _Traits Traits for character type, defaults to
546 * char_traits<_CharT>.
547 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
548 *
549 * This class supports reading from objects of type std::basic_string,
550 * using the inherited functions from std::basic_istream. To control
551 * the associated sequence, an instance of std::basic_stringbuf is used,
552 * which this page refers to as @c sb.
553 */
554 template<typename _CharT, typename _Traits, typename _Alloc>
555 class basic_istringstream : public basic_istream<_CharT, _Traits>
556 {
557 public:
558 // Types:
559 typedef _CharT char_type;
560 typedef _Traits traits_type;
561 // _GLIBCXX_RESOLVE_LIB_DEFECTS
562 // 251. basic_stringbuf missing allocator_type
563 typedef _Alloc allocator_type;
564 typedef typename traits_type::int_type int_type;
565 typedef typename traits_type::pos_type pos_type;
566 typedef typename traits_type::off_type off_type;
567
568 // Non-standard types:
569 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
570 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
571 typedef basic_istream<char_type, traits_type> __istream_type;
572
573 private:
574 __stringbuf_type _M_stringbuf;
575
576 public:
577 // Constructors:
578
579 /**
580 * @brief Default constructor starts with an empty string buffer.
581 *
582 * Initializes @c sb using @c in, and passes @c &sb to the base
583 * class initializer. Does not allocate any buffer.
584 *
585 * That's a lie. We initialize the base class with NULL, because the
586 * string class does its own memory management.
587 */
589 : __istream_type(), _M_stringbuf(ios_base::in)
590 { this->init(&_M_stringbuf); }
591
592 /**
593 * @brief Starts with an empty string buffer.
594 * @param __mode Whether the buffer can read, or write, or both.
595 *
596 * @c ios_base::in is automatically included in @a __mode.
597 *
598 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
599 * class initializer. Does not allocate any buffer.
600 *
601 * That's a lie. We initialize the base class with NULL, because the
602 * string class does its own memory management.
603 */
604 explicit
606 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
607 { this->init(&_M_stringbuf); }
608
609 /**
610 * @brief Starts with an existing string buffer.
611 * @param __str A string to copy as a starting buffer.
612 * @param __mode Whether the buffer can read, or write, or both.
613 *
614 * @c ios_base::in is automatically included in @a mode.
615 *
616 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
617 * to the base class initializer.
618 *
619 * That's a lie. We initialize the base class with NULL, because the
620 * string class does its own memory management.
621 */
622 explicit
623 basic_istringstream(const __string_type& __str,
625 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
626 { this->init(&_M_stringbuf); }
627
628 /**
629 * @brief The destructor does nothing.
630 *
631 * The buffer is deallocated by the stringbuf object, not the
632 * formatting stream.
633 */
636
637#if __cplusplus >= 201103L
639
641 : __istream_type(std::move(__rhs)),
642 _M_stringbuf(std::move(__rhs._M_stringbuf))
643 { __istream_type::set_rdbuf(&_M_stringbuf); }
644
645#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
646 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
647 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
648 { this->init(std::__addressof(_M_stringbuf)); }
649
650 explicit
651 basic_istringstream(__string_type&& __str,
653 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
654 { this->init(std::__addressof(_M_stringbuf)); }
655
656 template<typename _SAlloc>
657 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
658 const allocator_type& __a)
659 : basic_istringstream(__str, ios_base::in, __a)
660 { }
661
662 template<typename _SAlloc>
663 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
664 ios_base::openmode __mode,
665 const allocator_type& __a)
666 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
667 { this->init(std::__addressof(_M_stringbuf)); }
668
669 template<typename _SAlloc>
670 explicit
671 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
673 : basic_istringstream(__str, __mode, allocator_type())
674 { }
675#endif // C++20
676
677 // 27.8.3.2 Assign and swap:
678
680 operator=(const basic_istringstream&) = delete;
681
683 operator=(basic_istringstream&& __rhs)
684 {
685 __istream_type::operator=(std::move(__rhs));
686 _M_stringbuf = std::move(__rhs._M_stringbuf);
687 return *this;
688 }
689
690 void
691 swap(basic_istringstream& __rhs)
692 {
693 __istream_type::swap(__rhs);
694 _M_stringbuf.swap(__rhs._M_stringbuf);
695 }
696#endif // C++11
697
698 // Members:
699 /**
700 * @brief Accessing the underlying buffer.
701 * @return The current basic_stringbuf buffer.
702 *
703 * This hides both signatures of std::basic_ios::rdbuf().
704 */
705 _GLIBCXX_NODISCARD
706 __stringbuf_type*
707 rdbuf() const
708 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
709
710 /**
711 * @brief Copying out the string buffer.
712 * @return @c rdbuf()->str()
713 */
714 _GLIBCXX_NODISCARD
715 __string_type
716 str() const _GLIBCXX_LVAL_REF_QUAL
717 { return _M_stringbuf.str(); }
718
719#if __cplusplus > 201703L
720#if _GLIBCXX_USE_CXX11_ABI
721#if __cpp_concepts
722 template<__allocator_like _SAlloc>
723 _GLIBCXX_NODISCARD
725 str(const _SAlloc& __sa) const
726 { return _M_stringbuf.str(__sa); }
727#endif
728
729 _GLIBCXX_NODISCARD
730 __string_type
731 str() &&
732 { return std::move(_M_stringbuf).str(); }
733#endif // cxx11 ABI
734
735 _GLIBCXX_SSTREAM_ALWAYS_INLINE
736 basic_string_view<char_type, traits_type>
737 view() const noexcept
738 { return _M_stringbuf.view(); }
739#endif // C++20
740
741 /**
742 * @brief Setting a new buffer.
743 * @param __s The string to use as a new sequence.
744 *
745 * Calls @c rdbuf()->str(s).
746 */
747 void
748 str(const __string_type& __s)
749 { _M_stringbuf.str(__s); }
750
751#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
752#if __cpp_concepts
753 template<__allocator_like _SAlloc>
754 requires (!is_same_v<_SAlloc, _Alloc>)
755 void
757 { _M_stringbuf.str(__s); }
758#endif
759
760 void
761 str(__string_type&& __s)
762 { _M_stringbuf.str(std::move(__s)); }
763#endif
764 };
765
766
767 // [27.7.3] Template class basic_ostringstream
768 /**
769 * @brief Controlling output for std::string.
770 * @ingroup io
771 *
772 * @tparam _CharT Type of character stream.
773 * @tparam _Traits Traits for character type, defaults to
774 * char_traits<_CharT>.
775 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
776 *
777 * This class supports writing to objects of type std::basic_string,
778 * using the inherited functions from std::basic_ostream. To control
779 * the associated sequence, an instance of std::basic_stringbuf is used,
780 * which this page refers to as @c sb.
781 */
782 template <typename _CharT, typename _Traits, typename _Alloc>
783 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
784 {
785 public:
786 // Types:
787 typedef _CharT char_type;
788 typedef _Traits traits_type;
789 // _GLIBCXX_RESOLVE_LIB_DEFECTS
790 // 251. basic_stringbuf missing allocator_type
791 typedef _Alloc allocator_type;
792 typedef typename traits_type::int_type int_type;
793 typedef typename traits_type::pos_type pos_type;
794 typedef typename traits_type::off_type off_type;
795
796 // Non-standard types:
797 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
798 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
799 typedef basic_ostream<char_type, traits_type> __ostream_type;
800
801 private:
802 __stringbuf_type _M_stringbuf;
803
804 public:
805 // Constructors/destructor:
806
807 /**
808 * @brief Default constructor starts with an empty string buffer.
809 *
810 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
811 * class initializer. Does not allocate any buffer.
812 *
813 * That's a lie. We initialize the base class with NULL, because the
814 * string class does its own memory management.
815 */
817 : __ostream_type(), _M_stringbuf(ios_base::out)
818 { this->init(&_M_stringbuf); }
819
820 /**
821 * @brief Starts with an empty string buffer.
822 * @param __mode Whether the buffer can read, or write, or both.
823 *
824 * @c ios_base::out is automatically included in @a mode.
825 *
826 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
827 * class initializer. Does not allocate any buffer.
828 *
829 * That's a lie. We initialize the base class with NULL, because the
830 * string class does its own memory management.
831 */
832 explicit
834 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
835 { this->init(&_M_stringbuf); }
836
837 /**
838 * @brief Starts with an existing string buffer.
839 * @param __str A string to copy as a starting buffer.
840 * @param __mode Whether the buffer can read, or write, or both.
841 *
842 * @c ios_base::out is automatically included in @a mode.
843 *
844 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
845 * to the base class initializer.
846 *
847 * That's a lie. We initialize the base class with NULL, because the
848 * string class does its own memory management.
849 */
850 explicit
851 basic_ostringstream(const __string_type& __str,
853 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
854 { this->init(&_M_stringbuf); }
855
856 /**
857 * @brief The destructor does nothing.
858 *
859 * The buffer is deallocated by the stringbuf object, not the
860 * formatting stream.
861 */
864
865#if __cplusplus >= 201103L
867
869 : __ostream_type(std::move(__rhs)),
870 _M_stringbuf(std::move(__rhs._M_stringbuf))
871 { __ostream_type::set_rdbuf(&_M_stringbuf); }
872
873#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
874 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
875 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
876 { this->init(std::__addressof(_M_stringbuf)); }
877
878 explicit
879 basic_ostringstream(__string_type&& __str,
881 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
882 { this->init(std::__addressof(_M_stringbuf)); }
883
884 template<typename _SAlloc>
885 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
886 const allocator_type& __a)
887 : basic_ostringstream(__str, ios_base::out, __a)
888 { }
889
890 template<typename _SAlloc>
891 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
892 ios_base::openmode __mode,
893 const allocator_type& __a)
894 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
895 { this->init(std::__addressof(_M_stringbuf)); }
896
897 template<typename _SAlloc>
898 explicit
899 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
901 : basic_ostringstream(__str, __mode, allocator_type())
902 { }
903#endif // C++20
904
905 // 27.8.3.2 Assign and swap:
906
908 operator=(const basic_ostringstream&) = delete;
909
911 operator=(basic_ostringstream&& __rhs)
912 {
913 __ostream_type::operator=(std::move(__rhs));
914 _M_stringbuf = std::move(__rhs._M_stringbuf);
915 return *this;
916 }
917
918 void
919 swap(basic_ostringstream& __rhs)
920 {
921 __ostream_type::swap(__rhs);
922 _M_stringbuf.swap(__rhs._M_stringbuf);
923 }
924#endif // C++11
925
926 // Members:
927 /**
928 * @brief Accessing the underlying buffer.
929 * @return The current basic_stringbuf buffer.
930 *
931 * This hides both signatures of std::basic_ios::rdbuf().
932 */
933 _GLIBCXX_NODISCARD
934 __stringbuf_type*
935 rdbuf() const
936 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
937
938 /**
939 * @brief Copying out the string buffer.
940 * @return @c rdbuf()->str()
941 */
942 _GLIBCXX_NODISCARD
943 __string_type
944 str() const _GLIBCXX_LVAL_REF_QUAL
945 { return _M_stringbuf.str(); }
946
947#if __cplusplus > 201703L
948#if _GLIBCXX_USE_CXX11_ABI
949#if __cpp_concepts
950 template<__allocator_like _SAlloc>
951 _GLIBCXX_NODISCARD
953 str(const _SAlloc& __sa) const
954 { return _M_stringbuf.str(__sa); }
955#endif
956
957 _GLIBCXX_NODISCARD
958 __string_type
959 str() &&
960 { return std::move(_M_stringbuf).str(); }
961#endif // cxx11 ABI
962
963 _GLIBCXX_SSTREAM_ALWAYS_INLINE
964 basic_string_view<char_type, traits_type>
965 view() const noexcept
966 { return _M_stringbuf.view(); }
967#endif // C++20
968
969 /**
970 * @brief Setting a new buffer.
971 * @param __s The string to use as a new sequence.
972 *
973 * Calls @c rdbuf()->str(s).
974 */
975 void
976 str(const __string_type& __s)
977 { _M_stringbuf.str(__s); }
978
979#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
980#if __cpp_concepts
981 template<__allocator_like _SAlloc>
982 requires (!is_same_v<_SAlloc, _Alloc>)
983 void
985 { _M_stringbuf.str(__s); }
986#endif
987
988 void
989 str(__string_type&& __s)
990 { _M_stringbuf.str(std::move(__s)); }
991#endif
992 };
993
994
995 // [27.7.4] Template class basic_stringstream
996 /**
997 * @brief Controlling input and output for std::string.
998 * @ingroup io
999 *
1000 * @tparam _CharT Type of character stream.
1001 * @tparam _Traits Traits for character type, defaults to
1002 * char_traits<_CharT>.
1003 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
1004 *
1005 * This class supports reading from and writing to objects of type
1006 * std::basic_string, using the inherited functions from
1007 * std::basic_iostream. To control the associated sequence, an instance
1008 * of std::basic_stringbuf is used, which this page refers to as @c sb.
1009 */
1010 template <typename _CharT, typename _Traits, typename _Alloc>
1011 class basic_stringstream : public basic_iostream<_CharT, _Traits>
1012 {
1013 public:
1014 // Types:
1015 typedef _CharT char_type;
1016 typedef _Traits traits_type;
1017 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1018 // 251. basic_stringbuf missing allocator_type
1019 typedef _Alloc allocator_type;
1020 typedef typename traits_type::int_type int_type;
1021 typedef typename traits_type::pos_type pos_type;
1022 typedef typename traits_type::off_type off_type;
1023
1024 // Non-standard Types:
1025 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1026 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
1027 typedef basic_iostream<char_type, traits_type> __iostream_type;
1028
1029 private:
1030 __stringbuf_type _M_stringbuf;
1031
1032 public:
1033 // Constructors/destructors
1034
1035 /**
1036 * @brief Default constructor starts with an empty string buffer.
1037 *
1038 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1039 * to the base class initializer. Does not allocate any buffer.
1040 *
1041 * That's a lie. We initialize the base class with NULL, because the
1042 * string class does its own memory management.
1043 */
1045 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1046 { this->init(&_M_stringbuf); }
1047
1048 /**
1049 * @brief Starts with an empty string buffer.
1050 * @param __m Whether the buffer can read, or write, or both.
1051 *
1052 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1053 * to the base class initializer. Does not allocate any buffer.
1054 *
1055 * That's a lie. We initialize the base class with NULL, because the
1056 * string class does its own memory management.
1057 */
1058 explicit
1060 : __iostream_type(), _M_stringbuf(__m)
1061 { this->init(&_M_stringbuf); }
1062
1063 /**
1064 * @brief Starts with an existing string buffer.
1065 * @param __str A string to copy as a starting buffer.
1066 * @param __m Whether the buffer can read, or write, or both.
1067 *
1068 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1069 * to the base class initializer.
1070 *
1071 * That's a lie. We initialize the base class with NULL, because the
1072 * string class does its own memory management.
1073 */
1074 explicit
1075 basic_stringstream(const __string_type& __str,
1077 : __iostream_type(), _M_stringbuf(__str, __m)
1078 { this->init(&_M_stringbuf); }
1079
1080 /**
1081 * @brief The destructor does nothing.
1082 *
1083 * The buffer is deallocated by the stringbuf object, not the
1084 * formatting stream.
1085 */
1088
1089#if __cplusplus >= 201103L
1090 basic_stringstream(const basic_stringstream&) = delete;
1091
1093 : __iostream_type(std::move(__rhs)),
1094 _M_stringbuf(std::move(__rhs._M_stringbuf))
1095 { __iostream_type::set_rdbuf(&_M_stringbuf); }
1096
1097#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1098 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1099 : __iostream_type(), _M_stringbuf(__mode, __a)
1100 { this->init(&_M_stringbuf); }
1101
1102 explicit
1103 basic_stringstream(__string_type&& __str,
1105 | ios_base::out)
1106 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1107 { this->init(std::__addressof(_M_stringbuf)); }
1108
1109 template<typename _SAlloc>
1110 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1111 const allocator_type& __a)
1112 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1113 { }
1114
1115 template<typename _SAlloc>
1116 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1117 ios_base::openmode __mode,
1118 const allocator_type& __a)
1119 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1120 { this->init(std::__addressof(_M_stringbuf)); }
1121
1122 template<typename _SAlloc>
1123 explicit
1124 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1126 | ios_base::out)
1127 : basic_stringstream(__str, __mode, allocator_type())
1128 { }
1129#endif // C++20
1130
1131 // 27.8.3.2 Assign and swap:
1132
1134 operator=(const basic_stringstream&) = delete;
1135
1137 operator=(basic_stringstream&& __rhs)
1138 {
1139 __iostream_type::operator=(std::move(__rhs));
1140 _M_stringbuf = std::move(__rhs._M_stringbuf);
1141 return *this;
1142 }
1143
1144 void
1145 swap(basic_stringstream& __rhs)
1146 {
1147 __iostream_type::swap(__rhs);
1148 _M_stringbuf.swap(__rhs._M_stringbuf);
1149 }
1150#endif // C++11
1151
1152 // Members:
1153 /**
1154 * @brief Accessing the underlying buffer.
1155 * @return The current basic_stringbuf buffer.
1156 *
1157 * This hides both signatures of std::basic_ios::rdbuf().
1158 */
1159 _GLIBCXX_NODISCARD
1160 __stringbuf_type*
1161 rdbuf() const
1162 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1163
1164 /**
1165 * @brief Copying out the string buffer.
1166 * @return @c rdbuf()->str()
1167 */
1168 _GLIBCXX_NODISCARD
1169 __string_type
1170 str() const _GLIBCXX_LVAL_REF_QUAL
1171 { return _M_stringbuf.str(); }
1172
1173#if __cplusplus > 201703L
1174#if _GLIBCXX_USE_CXX11_ABI
1175#if __cpp_concepts
1176 template<__allocator_like _SAlloc>
1177 _GLIBCXX_NODISCARD
1179 str(const _SAlloc& __sa) const
1180 { return _M_stringbuf.str(__sa); }
1181#endif
1182
1183 _GLIBCXX_NODISCARD
1184 __string_type
1185 str() &&
1186 { return std::move(_M_stringbuf).str(); }
1187#endif // cxx11 ABI
1188
1189 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1190 basic_string_view<char_type, traits_type>
1191 view() const noexcept
1192 { return _M_stringbuf.view(); }
1193#endif // C++20
1194
1195 /**
1196 * @brief Setting a new buffer.
1197 * @param __s The string to use as a new sequence.
1198 *
1199 * Calls @c rdbuf()->str(s).
1200 */
1201 void
1202 str(const __string_type& __s)
1203 { _M_stringbuf.str(__s); }
1204
1205#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1206#if __cpp_concepts
1207 template<__allocator_like _SAlloc>
1208 requires (!is_same_v<_SAlloc, _Alloc>)
1209 void
1211 { _M_stringbuf.str(__s); }
1212#endif
1213
1214 void
1215 str(__string_type&& __s)
1216 { _M_stringbuf.str(std::move(__s)); }
1217#endif
1218 };
1219
1220#if __cplusplus >= 201103L
1221 /// Swap specialization for stringbufs.
1222 template <class _CharT, class _Traits, class _Allocator>
1223 inline void
1226 noexcept(noexcept(__x.swap(__y)))
1227 { __x.swap(__y); }
1228
1229 /// Swap specialization for istringstreams.
1230 template <class _CharT, class _Traits, class _Allocator>
1231 inline void
1235
1236 /// Swap specialization for ostringstreams.
1237 template <class _CharT, class _Traits, class _Allocator>
1238 inline void
1242
1243 /// Swap specialization for stringstreams.
1244 template <class _CharT, class _Traits, class _Allocator>
1245 inline void
1249#endif // C++11
1250
1251_GLIBCXX_END_NAMESPACE_CXX11
1252_GLIBCXX_END_NAMESPACE_VERSION
1253} // namespace
1254
1255#undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1256#undef _GLIBCXX_LVAL_REF_QUAL
1257
1258#include <bits/sstream.tcc>
1259
1260#endif /* _GLIBCXX_SSTREAM */
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
constexpr _Iterator __base(_Iterator __it)
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
char_type * pptr() const
Access to the put area.
Definition streambuf:546
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
Definition streambuf:523
char_type * eback() const
Access to the get area.
Definition streambuf:491
char_type * egptr() const
Access to the get area.
Definition streambuf:501
char_type * gptr() const
Access to the get area.
Definition streambuf:498
locale pubimbue(const locale &__loc)
Entry point for imbue().
Definition streambuf:218
char_type * pbase() const
Access to the put area.
Definition streambuf:543
traits_type::off_type off_type
Definition streambuf:139
basic_streambuf()
Base constructor.
Definition streambuf:472
basic_istream(__streambuf_type *__sb)
Base constructor.
Definition istream:97
basic_ostream(__streambuf_type *__sb)
Base constructor.
Definition ostream.h:92
basic_iostream(basic_streambuf< _CharT, _Traits > *__sb)
Constructor does nothing.
Definition istream:1010
The actual work of input and output (for std::string).
Definition sstream:82
virtual streamsize showmanyc()
Investigating the data available.
Definition sstream:353
virtual int_type underflow()
Fetches more data from the controlled sequence.
Definition sstream.tcc:154
virtual pos_type seekpos(pos_type __sp, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:220
virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:172
virtual int_type overflow(int_type __c=traits_type::eof())
Consumes data from the buffer; writes to the controlled sequence.
Definition sstream.tcc:84
basic_stringbuf()
Starts with an empty string buffer.
Definition sstream:122
virtual int_type pbackfail(int_type __c=traits_type::eof())
Tries to back up the input sequence.
Definition sstream.tcc:50
__string_type str() const &
Copying out the string buffer.
Definition sstream:251
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
Definition sstream:385
ios_base::openmode _M_mode
Definition sstream:108
Controlling input for std::string.
Definition sstream:556
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:748
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:707
~basic_istringstream()
The destructor does nothing.
Definition sstream:634
basic_istringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::in)
Starts with an existing string buffer.
Definition sstream:623
__string_type str() const &
Copying out the string buffer.
Definition sstream:716
basic_istringstream()
Default constructor starts with an empty string buffer.
Definition sstream:588
basic_istringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:605
Controlling output for std::string.
Definition sstream:784
~basic_ostringstream()
The destructor does nothing.
Definition sstream:862
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:976
basic_ostringstream()
Default constructor starts with an empty string buffer.
Definition sstream:816
basic_ostringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::out)
Starts with an existing string buffer.
Definition sstream:851
__string_type str() const &
Copying out the string buffer.
Definition sstream:944
basic_ostringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:833
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:935
Controlling input and output for std::string.
Definition sstream:1012
~basic_stringstream()
The destructor does nothing.
Definition sstream:1086
basic_stringstream(const __string_type &__str, ios_base::openmode __m=ios_base::out|ios_base::in)
Starts with an existing string buffer.
Definition sstream:1075
basic_stringstream()
Default constructor starts with an empty string buffer.
Definition sstream:1044
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:1202
__string_type str() const &
Copying out the string buffer.
Definition sstream:1170
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:1161
basic_stringstream(ios_base::openmode __m)
Starts with an empty string buffer.
Definition sstream:1059
Uniform interface to all allocator types.
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
Managing sequences of characters and character-like objects.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
The base of the I/O class hierarchy.
Definition ios_base.h:266
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:498
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:501
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:484
static const openmode app
Seek to end before each write.
Definition ios_base.h:487
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:523
static const openmode ate
Open and seek to end immediately after opening.
Definition ios_base.h:490