1// String based streams -*- C++ -*-
3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/sstream
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.7 String-based streams
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
37#pragma GCC system_header
40#include <bits/requires_hosted.h> // iostream
44#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
46#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
47# define _GLIBCXX_LVAL_REF_QUAL &
48# define _GLIBCXX_SSTREAM_ALWAYS_INLINE
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__]]
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
60_GLIBCXX_BEGIN_NAMESPACE_CXX11
62 // [27.7.1] template class basic_stringbuf
64 * @brief The actual work of input and output (for std::string).
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>.
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.)
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.
80 template<typename _CharT, typename _Traits, typename _Alloc>
81 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
83 struct __xfer_bufptrs;
85#if __cplusplus >= 201103L
86 using allocator_traits = std::allocator_traits<_Alloc>;
88 = __or_<typename allocator_traits::propagate_on_container_swap,
89 typename allocator_traits::is_always_equal>;
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;
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;
108 /// Place to stash in || out || in | out settings for current stringbuf.
109 ios_base::openmode _M_mode;
112 __string_type _M_string;
118 * @brief Starts with an empty string buffer.
120 * The default constructor initializes the parent class using its
124 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
128 * @brief Starts with an empty string buffer.
129 * @param __mode Whether the buffer can read, or write, or both.
131 * The default constructor initializes the parent class using its
135 basic_stringbuf(ios_base::openmode __mode)
136 : __streambuf_type(), _M_mode(__mode), _M_string()
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.
144 * This constructor initializes the parent class using its
148 basic_stringbuf(const __string_type& __str,
149 ios_base::openmode __mode = ios_base::in | ios_base::out)
150 : __streambuf_type(), _M_mode(),
151 _M_string(__str.data(), __str.size(), __str.get_allocator())
152 { _M_stringbuf_init(__mode); }
154#if __cplusplus >= 201103L
155 basic_stringbuf(const basic_stringbuf&) = delete;
157 basic_stringbuf(basic_stringbuf&& __rhs)
158 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
159 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
161#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
163 basic_stringbuf(const allocator_type& __a)
164 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
167 basic_stringbuf(ios_base::openmode __mode,
168 const allocator_type& __a)
169 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
173 basic_stringbuf(__string_type&& __s,
174 ios_base::openmode __mode = ios_base::in
176 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
177 { _M_stringbuf_init(__mode); }
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)
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); }
193 template<typename _SAlloc>
195 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
196 ios_base::openmode __mode = ios_base::in
198 : basic_stringbuf(__s, __mode, allocator_type{})
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); }
205 allocator_type get_allocator() const noexcept
206 { return _M_string.get_allocator(); }
209 // 27.8.2.2 Assign and swap:
212 operator=(const basic_stringbuf&) = delete;
215 operator=(basic_stringbuf&& __rhs)
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);
228 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
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
240 // Getters and setters:
243 * @brief Copying out the string buffer.
244 * @return A copy of one of the underlying sequences.
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
252 str() const _GLIBCXX_LVAL_REF_QUAL
254 __string_type __ret(_M_string.get_allocator());
255 if (char_type* __hi = _M_high_mark())
256 __ret.assign(this->pbase(), __hi);
262#if __cplusplus > 201703L
263#if _GLIBCXX_USE_CXX11_ABI
265 template<__allocator_like _SAlloc>
267 basic_string<_CharT, _Traits, _SAlloc>
268 str(const _SAlloc& __sa) const
271 return { __sv.data(), __sv.size(), __sa };
279 if (char_type* __hi = _M_high_mark())
281 // Set length to end of character sequence and add null terminator.
282 _M_string._M_set_length(_M_high_mark() - this->pbase());
284 auto __str = std::move(_M_string);
286 _M_sync(_M_string.data(), 0, 0);
291 _GLIBCXX_SSTREAM_ALWAYS_INLINE
292 basic_string_view<char_type, traits_type>
293 view() const noexcept
295 if (char_type* __hi = _M_high_mark())
296 return { this->pbase(), __hi };
303 * @brief Setting a new buffer.
304 * @param __s The string to use as a new sequence.
306 * Deallocates any previous stored sequence, then copies @a s to
310 str(const __string_type& __s)
312 // Cannot use _M_string = __s, since v3 strings are COW
313 // (not always true now but assign() always works).
314 _M_string.assign(__s.data(), __s.size());
315 _M_stringbuf_init(_M_mode);
318#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
320 template<__allocator_like _SAlloc>
321 requires (!is_same_v<_SAlloc, _Alloc>)
323 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
325 _M_string.assign(__s.data(), __s.size());
326 _M_stringbuf_init(_M_mode);
331 str(__string_type&& __s)
333 _M_string = std::move(__s);
334 _M_stringbuf_init(_M_mode);
339 // Common initialization code goes here.
341 _M_stringbuf_init(ios_base::openmode __mode)
344 __size_type __len = 0;
345 if (_M_mode & (ios_base::ate | ios_base::app))
346 __len = _M_string.size();
347 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
353 streamsize __ret = -1;
354 if (_M_mode & ios_base::in)
357 __ret = this->egptr() - this->gptr();
366 pbackfail(int_type __c = traits_type::eof());
369 overflow(int_type __c = traits_type::eof());
372 * @brief Manipulates the buffer.
373 * @param __s Pointer to a buffer area.
374 * @param __n Size of @a __s.
377 * If no buffer has already been created, and both @a __s and @a __n are
378 * non-zero, then @c __s is used as a buffer; see
379 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
382 virtual __streambuf_type*
383 setbuf(char_type* __s, streamsize __n)
387 // This is implementation-defined behavior, and assumes
388 // that an external char_type array of length __n exists
389 // and has been pre-allocated. If this is not the case,
390 // things will quickly blow up.
392 // Step 1: Destroy the current internal array.
395 // Step 2: Use the external array.
396 _M_sync(__s, __n, 0);
402 seekoff(off_type __off, ios_base::seekdir __way,
403 ios_base::openmode __mode = ios_base::in | ios_base::out);
406 seekpos(pos_type __sp,
407 ios_base::openmode __mode = ios_base::in | ios_base::out);
409 // Internal function for correctly updating the internal buffer
410 // for a particular _M_string, due to initialization or re-sizing
411 // of an existing _M_string.
413 _M_sync(char_type* __base, __size_type __i, __size_type __o);
415 // Internal function for correctly updating egptr() to the actual
420 if (char_type* __pptr = this->pptr())
422 char_type* __egptr = this->egptr();
423 if (!__egptr || __pptr > __egptr)
425 if (_M_mode & ios_base::in)
426 this->setg(this->eback(), this->gptr(), __pptr);
428 this->setg(__pptr, __pptr, __pptr);
433 // Works around the issue with pbump, part of the protected
434 // interface of basic_streambuf, taking just an int.
436 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
439 // Return a pointer to the end of the underlying character sequence.
440 // This might not be the same character as _M_string.end() because
441 // basic_stringbuf::overflow might have written to unused capacity
442 // in _M_string without updating its length.
443 __attribute__((__always_inline__))
445 _M_high_mark() const _GLIBCXX_NOEXCEPT
447 if (char_type* __pptr = this->pptr())
449 char_type* __egptr = this->egptr();
450 if (!__egptr || __pptr > __egptr)
451 return __pptr; // Underlying sequence is [pbase, pptr).
453 return __egptr; // Underlying sequence is [pbase, egptr).
455 return 0; // Underlying character sequence is just _M_string.
458#if __cplusplus >= 201103L
459#if _GLIBCXX_USE_CXX11_ABI
460 // This type captures the state of the gptr / pptr pointers as offsets
461 // so they can be restored in another object after moving the string.
462 struct __xfer_bufptrs
464 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
465 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
467 const _CharT* const __str = __from._M_string.data();
468 const _CharT* __end = nullptr;
471 _M_goff[0] = __from.eback() - __str;
472 _M_goff[1] = __from.gptr() - __str;
473 _M_goff[2] = __from.egptr() - __str;
474 __end = __from.egptr();
478 _M_poff[0] = __from.pbase() - __str;
479 _M_poff[1] = __from.pptr() - __from.pbase();
480 _M_poff[2] = __from.epptr() - __str;
481 if (!__end || __from.pptr() > __end)
482 __end = __from.pptr();
485 // Set _M_string length to the greater of the get and put areas.
488 // The const_cast avoids changing this constructor's signature,
489 // because it is exported from the dynamic library.
490 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
491 __mut_from._M_string._M_length(__end - __str);
497 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
498 if (_M_goff[0] != -1)
499 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
500 if (_M_poff[0] != -1)
501 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
504 basic_stringbuf* _M_to;
509 // This type does nothing when using Copy-On-Write strings.
510 struct __xfer_bufptrs
512 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
516 // The move constructor initializes an __xfer_bufptrs temporary then
517 // delegates to this constructor to performs moves during its lifetime.
518 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
519 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
520 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
523#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
524 // The move constructor initializes an __xfer_bufptrs temporary then
525 // delegates to this constructor to performs moves during its lifetime.
526 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
528 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
529 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
536 // [27.7.2] Template class basic_istringstream
538 * @brief Controlling input for std::string.
541 * @tparam _CharT Type of character stream.
542 * @tparam _Traits Traits for character type, defaults to
543 * char_traits<_CharT>.
544 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
546 * This class supports reading from objects of type std::basic_string,
547 * using the inherited functions from std::basic_istream. To control
548 * the associated sequence, an instance of std::basic_stringbuf is used,
549 * which this page refers to as @c sb.
551 template<typename _CharT, typename _Traits, typename _Alloc>
552 class basic_istringstream : public basic_istream<_CharT, _Traits>
556 typedef _CharT char_type;
557 typedef _Traits traits_type;
558 // _GLIBCXX_RESOLVE_LIB_DEFECTS
559 // 251. basic_stringbuf missing allocator_type
560 typedef _Alloc allocator_type;
561 typedef typename traits_type::int_type int_type;
562 typedef typename traits_type::pos_type pos_type;
563 typedef typename traits_type::off_type off_type;
565 // Non-standard types:
566 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
567 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
568 typedef basic_istream<char_type, traits_type> __istream_type;
571 __stringbuf_type _M_stringbuf;
577 * @brief Default constructor starts with an empty string buffer.
579 * Initializes @c sb using @c in, and passes @c &sb to the base
580 * class initializer. Does not allocate any buffer.
582 * That's a lie. We initialize the base class with NULL, because the
583 * string class does its own memory management.
585 basic_istringstream()
586 : __istream_type(), _M_stringbuf(ios_base::in)
587 { this->init(&_M_stringbuf); }
590 * @brief Starts with an empty string buffer.
591 * @param __mode Whether the buffer can read, or write, or both.
593 * @c ios_base::in is automatically included in @a __mode.
595 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
596 * class initializer. Does not allocate any buffer.
598 * That's a lie. We initialize the base class with NULL, because the
599 * string class does its own memory management.
602 basic_istringstream(ios_base::openmode __mode)
603 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
604 { this->init(&_M_stringbuf); }
607 * @brief Starts with an existing string buffer.
608 * @param __str A string to copy as a starting buffer.
609 * @param __mode Whether the buffer can read, or write, or both.
611 * @c ios_base::in is automatically included in @a mode.
613 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
614 * to the base class initializer.
616 * That's a lie. We initialize the base class with NULL, because the
617 * string class does its own memory management.
620 basic_istringstream(const __string_type& __str,
621 ios_base::openmode __mode = ios_base::in)
622 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
623 { this->init(&_M_stringbuf); }
626 * @brief The destructor does nothing.
628 * The buffer is deallocated by the stringbuf object, not the
631 ~basic_istringstream()
634#if __cplusplus >= 201103L
635 basic_istringstream(const basic_istringstream&) = delete;
637 basic_istringstream(basic_istringstream&& __rhs)
638 : __istream_type(std::move(__rhs)),
639 _M_stringbuf(std::move(__rhs._M_stringbuf))
640 { __istream_type::set_rdbuf(&_M_stringbuf); }
642#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
643 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
644 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
645 { this->init(std::__addressof(_M_stringbuf)); }
648 basic_istringstream(__string_type&& __str,
649 ios_base::openmode __mode = ios_base::in)
650 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
651 { this->init(std::__addressof(_M_stringbuf)); }
653 template<typename _SAlloc>
654 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
655 const allocator_type& __a)
656 : basic_istringstream(__str, ios_base::in, __a)
659 template<typename _SAlloc>
660 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
661 ios_base::openmode __mode,
662 const allocator_type& __a)
663 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
664 { this->init(std::__addressof(_M_stringbuf)); }
666 template<typename _SAlloc>
668 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
669 ios_base::openmode __mode = ios_base::in)
670 : basic_istringstream(__str, __mode, allocator_type())
674 // 27.8.3.2 Assign and swap:
677 operator=(const basic_istringstream&) = delete;
680 operator=(basic_istringstream&& __rhs)
682 __istream_type::operator=(std::move(__rhs));
683 _M_stringbuf = std::move(__rhs._M_stringbuf);
688 swap(basic_istringstream& __rhs)
690 __istream_type::swap(__rhs);
691 _M_stringbuf.swap(__rhs._M_stringbuf);
697 * @brief Accessing the underlying buffer.
698 * @return The current basic_stringbuf buffer.
700 * This hides both signatures of std::basic_ios::rdbuf().
705 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
708 * @brief Copying out the string buffer.
709 * @return @c rdbuf()->str()
713 str() const _GLIBCXX_LVAL_REF_QUAL
714 { return _M_stringbuf.str(); }
716#if __cplusplus > 201703L
717#if _GLIBCXX_USE_CXX11_ABI
719 template<__allocator_like _SAlloc>
721 basic_string<_CharT, _Traits, _SAlloc>
722 str(const _SAlloc& __sa) const
723 { return _M_stringbuf.str(__sa); }
729 { return std::move(_M_stringbuf).str(); }
732 _GLIBCXX_SSTREAM_ALWAYS_INLINE
733 basic_string_view<char_type, traits_type>
734 view() const noexcept
735 { return _M_stringbuf.view(); }
739 * @brief Setting a new buffer.
740 * @param __s The string to use as a new sequence.
742 * Calls @c rdbuf()->str(s).
745 str(const __string_type& __s)
746 { _M_stringbuf.str(__s); }
748#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
750 template<__allocator_like _SAlloc>
751 requires (!is_same_v<_SAlloc, _Alloc>)
753 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
754 { _M_stringbuf.str(__s); }
758 str(__string_type&& __s)
759 { _M_stringbuf.str(std::move(__s)); }
764 // [27.7.3] Template class basic_ostringstream
766 * @brief Controlling output for std::string.
769 * @tparam _CharT Type of character stream.
770 * @tparam _Traits Traits for character type, defaults to
771 * char_traits<_CharT>.
772 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
774 * This class supports writing to objects of type std::basic_string,
775 * using the inherited functions from std::basic_ostream. To control
776 * the associated sequence, an instance of std::basic_stringbuf is used,
777 * which this page refers to as @c sb.
779 template <typename _CharT, typename _Traits, typename _Alloc>
780 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
784 typedef _CharT char_type;
785 typedef _Traits traits_type;
786 // _GLIBCXX_RESOLVE_LIB_DEFECTS
787 // 251. basic_stringbuf missing allocator_type
788 typedef _Alloc allocator_type;
789 typedef typename traits_type::int_type int_type;
790 typedef typename traits_type::pos_type pos_type;
791 typedef typename traits_type::off_type off_type;
793 // Non-standard types:
794 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
795 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
796 typedef basic_ostream<char_type, traits_type> __ostream_type;
799 __stringbuf_type _M_stringbuf;
802 // Constructors/destructor:
805 * @brief Default constructor starts with an empty string buffer.
807 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
808 * class initializer. Does not allocate any buffer.
810 * That's a lie. We initialize the base class with NULL, because the
811 * string class does its own memory management.
813 basic_ostringstream()
814 : __ostream_type(), _M_stringbuf(ios_base::out)
815 { this->init(&_M_stringbuf); }
818 * @brief Starts with an empty string buffer.
819 * @param __mode Whether the buffer can read, or write, or both.
821 * @c ios_base::out is automatically included in @a mode.
823 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
824 * class initializer. Does not allocate any buffer.
826 * That's a lie. We initialize the base class with NULL, because the
827 * string class does its own memory management.
830 basic_ostringstream(ios_base::openmode __mode)
831 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
832 { this->init(&_M_stringbuf); }
835 * @brief Starts with an existing string buffer.
836 * @param __str A string to copy as a starting buffer.
837 * @param __mode Whether the buffer can read, or write, or both.
839 * @c ios_base::out is automatically included in @a mode.
841 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
842 * to the base class initializer.
844 * That's a lie. We initialize the base class with NULL, because the
845 * string class does its own memory management.
848 basic_ostringstream(const __string_type& __str,
849 ios_base::openmode __mode = ios_base::out)
850 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
851 { this->init(&_M_stringbuf); }
854 * @brief The destructor does nothing.
856 * The buffer is deallocated by the stringbuf object, not the
859 ~basic_ostringstream()
862#if __cplusplus >= 201103L
863 basic_ostringstream(const basic_ostringstream&) = delete;
865 basic_ostringstream(basic_ostringstream&& __rhs)
866 : __ostream_type(std::move(__rhs)),
867 _M_stringbuf(std::move(__rhs._M_stringbuf))
868 { __ostream_type::set_rdbuf(&_M_stringbuf); }
870#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
871 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
872 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
873 { this->init(std::__addressof(_M_stringbuf)); }
876 basic_ostringstream(__string_type&& __str,
877 ios_base::openmode __mode = ios_base::out)
878 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
879 { this->init(std::__addressof(_M_stringbuf)); }
881 template<typename _SAlloc>
882 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
883 const allocator_type& __a)
884 : basic_ostringstream(__str, ios_base::out, __a)
887 template<typename _SAlloc>
888 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
889 ios_base::openmode __mode,
890 const allocator_type& __a)
891 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
892 { this->init(std::__addressof(_M_stringbuf)); }
894 template<typename _SAlloc>
896 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
897 ios_base::openmode __mode = ios_base::out)
898 : basic_ostringstream(__str, __mode, allocator_type())
902 // 27.8.3.2 Assign and swap:
905 operator=(const basic_ostringstream&) = delete;
908 operator=(basic_ostringstream&& __rhs)
910 __ostream_type::operator=(std::move(__rhs));
911 _M_stringbuf = std::move(__rhs._M_stringbuf);
916 swap(basic_ostringstream& __rhs)
918 __ostream_type::swap(__rhs);
919 _M_stringbuf.swap(__rhs._M_stringbuf);
925 * @brief Accessing the underlying buffer.
926 * @return The current basic_stringbuf buffer.
928 * This hides both signatures of std::basic_ios::rdbuf().
933 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
936 * @brief Copying out the string buffer.
937 * @return @c rdbuf()->str()
941 str() const _GLIBCXX_LVAL_REF_QUAL
942 { return _M_stringbuf.str(); }
944#if __cplusplus > 201703L
945#if _GLIBCXX_USE_CXX11_ABI
947 template<__allocator_like _SAlloc>
949 basic_string<_CharT, _Traits, _SAlloc>
950 str(const _SAlloc& __sa) const
951 { return _M_stringbuf.str(__sa); }
957 { return std::move(_M_stringbuf).str(); }
960 _GLIBCXX_SSTREAM_ALWAYS_INLINE
961 basic_string_view<char_type, traits_type>
962 view() const noexcept
963 { return _M_stringbuf.view(); }
967 * @brief Setting a new buffer.
968 * @param __s The string to use as a new sequence.
970 * Calls @c rdbuf()->str(s).
973 str(const __string_type& __s)
974 { _M_stringbuf.str(__s); }
976#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
978 template<__allocator_like _SAlloc>
979 requires (!is_same_v<_SAlloc, _Alloc>)
981 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
982 { _M_stringbuf.str(__s); }
986 str(__string_type&& __s)
987 { _M_stringbuf.str(std::move(__s)); }
992 // [27.7.4] Template class basic_stringstream
994 * @brief Controlling input and output for std::string.
997 * @tparam _CharT Type of character stream.
998 * @tparam _Traits Traits for character type, defaults to
999 * char_traits<_CharT>.
1000 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
1002 * This class supports reading from and writing to objects of type
1003 * std::basic_string, using the inherited functions from
1004 * std::basic_iostream. To control the associated sequence, an instance
1005 * of std::basic_stringbuf is used, which this page refers to as @c sb.
1007 template <typename _CharT, typename _Traits, typename _Alloc>
1008 class basic_stringstream : public basic_iostream<_CharT, _Traits>
1012 typedef _CharT char_type;
1013 typedef _Traits traits_type;
1014 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1015 // 251. basic_stringbuf missing allocator_type
1016 typedef _Alloc allocator_type;
1017 typedef typename traits_type::int_type int_type;
1018 typedef typename traits_type::pos_type pos_type;
1019 typedef typename traits_type::off_type off_type;
1021 // Non-standard Types:
1022 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1023 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
1024 typedef basic_iostream<char_type, traits_type> __iostream_type;
1027 __stringbuf_type _M_stringbuf;
1030 // Constructors/destructors
1033 * @brief Default constructor starts with an empty string buffer.
1035 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1036 * to the base class initializer. Does not allocate any buffer.
1038 * That's a lie. We initialize the base class with NULL, because the
1039 * string class does its own memory management.
1041 basic_stringstream()
1042 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1043 { this->init(&_M_stringbuf); }
1046 * @brief Starts with an empty string buffer.
1047 * @param __m Whether the buffer can read, or write, or both.
1049 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1050 * to the base class initializer. Does not allocate any buffer.
1052 * That's a lie. We initialize the base class with NULL, because the
1053 * string class does its own memory management.
1056 basic_stringstream(ios_base::openmode __m)
1057 : __iostream_type(), _M_stringbuf(__m)
1058 { this->init(&_M_stringbuf); }
1061 * @brief Starts with an existing string buffer.
1062 * @param __str A string to copy as a starting buffer.
1063 * @param __m Whether the buffer can read, or write, or both.
1065 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1066 * to the base class initializer.
1068 * That's a lie. We initialize the base class with NULL, because the
1069 * string class does its own memory management.
1072 basic_stringstream(const __string_type& __str,
1073 ios_base::openmode __m = ios_base::out | ios_base::in)
1074 : __iostream_type(), _M_stringbuf(__str, __m)
1075 { this->init(&_M_stringbuf); }
1078 * @brief The destructor does nothing.
1080 * The buffer is deallocated by the stringbuf object, not the
1081 * formatting stream.
1083 ~basic_stringstream()
1086#if __cplusplus >= 201103L
1087 basic_stringstream(const basic_stringstream&) = delete;
1089 basic_stringstream(basic_stringstream&& __rhs)
1090 : __iostream_type(std::move(__rhs)),
1091 _M_stringbuf(std::move(__rhs._M_stringbuf))
1092 { __iostream_type::set_rdbuf(&_M_stringbuf); }
1094#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1095 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1096 : __iostream_type(), _M_stringbuf(__mode, __a)
1097 { this->init(&_M_stringbuf); }
1100 basic_stringstream(__string_type&& __str,
1101 ios_base::openmode __mode = ios_base::in
1103 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1104 { this->init(std::__addressof(_M_stringbuf)); }
1106 template<typename _SAlloc>
1107 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1108 const allocator_type& __a)
1109 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1112 template<typename _SAlloc>
1113 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1114 ios_base::openmode __mode,
1115 const allocator_type& __a)
1116 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1117 { this->init(std::__addressof(_M_stringbuf)); }
1119 template<typename _SAlloc>
1121 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1122 ios_base::openmode __mode = ios_base::in
1124 : basic_stringstream(__str, __mode, allocator_type())
1128 // 27.8.3.2 Assign and swap:
1131 operator=(const basic_stringstream&) = delete;
1134 operator=(basic_stringstream&& __rhs)
1136 __iostream_type::operator=(std::move(__rhs));
1137 _M_stringbuf = std::move(__rhs._M_stringbuf);
1142 swap(basic_stringstream& __rhs)
1144 __iostream_type::swap(__rhs);
1145 _M_stringbuf.swap(__rhs._M_stringbuf);
1151 * @brief Accessing the underlying buffer.
1152 * @return The current basic_stringbuf buffer.
1154 * This hides both signatures of std::basic_ios::rdbuf().
1159 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1162 * @brief Copying out the string buffer.
1163 * @return @c rdbuf()->str()
1167 str() const _GLIBCXX_LVAL_REF_QUAL
1168 { return _M_stringbuf.str(); }
1170#if __cplusplus > 201703L
1171#if _GLIBCXX_USE_CXX11_ABI
1173 template<__allocator_like _SAlloc>
1175 basic_string<_CharT, _Traits, _SAlloc>
1176 str(const _SAlloc& __sa) const
1177 { return _M_stringbuf.str(__sa); }
1183 { return std::move(_M_stringbuf).str(); }
1186 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1187 basic_string_view<char_type, traits_type>
1188 view() const noexcept
1189 { return _M_stringbuf.view(); }
1193 * @brief Setting a new buffer.
1194 * @param __s The string to use as a new sequence.
1196 * Calls @c rdbuf()->str(s).
1199 str(const __string_type& __s)
1200 { _M_stringbuf.str(__s); }
1202#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1204 template<__allocator_like _SAlloc>
1205 requires (!is_same_v<_SAlloc, _Alloc>)
1207 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1208 { _M_stringbuf.str(__s); }
1212 str(__string_type&& __s)
1213 { _M_stringbuf.str(std::move(__s)); }
1217#if __cplusplus >= 201103L
1218 /// Swap specialization for stringbufs.
1219 template <class _CharT, class _Traits, class _Allocator>
1221 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1222 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1223 noexcept(noexcept(__x.swap(__y)))
1226 /// Swap specialization for istringstreams.
1227 template <class _CharT, class _Traits, class _Allocator>
1229 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1230 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1233 /// Swap specialization for ostringstreams.
1234 template <class _CharT, class _Traits, class _Allocator>
1236 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1237 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1240 /// Swap specialization for stringstreams.
1241 template <class _CharT, class _Traits, class _Allocator>
1243 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1244 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1248_GLIBCXX_END_NAMESPACE_CXX11
1249_GLIBCXX_END_NAMESPACE_VERSION
1252#undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1253#undef _GLIBCXX_LVAL_REF_QUAL
1255#include <bits/sstream.tcc>
1257#endif /* _GLIBCXX_SSTREAM */