1// String based streams -*- C++ -*-
3// Copyright (C) 1997-2024 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
36#pragma GCC system_header
38#include <bits/requires_hosted.h> // iostream
42#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
44#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
45# define _GLIBCXX_LVAL_REF_QUAL &
46# define _GLIBCXX_SSTREAM_ALWAYS_INLINE
48# define _GLIBCXX_LVAL_REF_QUAL
49// For symbols that are not exported from libstdc++.so for the COW string ABI.
50# define _GLIBCXX_SSTREAM_ALWAYS_INLINE [[__gnu__::__always_inline__]]
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
58_GLIBCXX_BEGIN_NAMESPACE_CXX11
60 // [27.7.1] template class basic_stringbuf
62 * @brief The actual work of input and output (for std::string).
65 * @tparam _CharT Type of character stream.
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
70 * This class associates either or both of its input and output sequences
71 * with a sequence of characters, which can be initialized from, or made
72 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
74 * For this class, open modes (of type @c ios_base::openmode) have
75 * @c in set if the input sequence can be read, and @c out set if the
76 * output sequence can be written.
78 template<typename _CharT, typename _Traits, typename _Alloc>
79 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
81 struct __xfer_bufptrs;
83#if __cplusplus >= 201103L
84 using allocator_traits = std::allocator_traits<_Alloc>;
86 = __or_<typename allocator_traits::propagate_on_container_swap,
87 typename allocator_traits::is_always_equal>;
92 typedef _CharT char_type;
93 typedef _Traits traits_type;
94 // _GLIBCXX_RESOLVE_LIB_DEFECTS
95 // 251. basic_stringbuf missing allocator_type
96 typedef _Alloc allocator_type;
97 typedef typename traits_type::int_type int_type;
98 typedef typename traits_type::pos_type pos_type;
99 typedef typename traits_type::off_type off_type;
101 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
102 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
103 typedef typename __string_type::size_type __size_type;
106 /// Place to stash in || out || in | out settings for current stringbuf.
107 ios_base::openmode _M_mode;
110 __string_type _M_string;
116 * @brief Starts with an empty string buffer.
118 * The default constructor initializes the parent class using its
122 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
126 * @brief Starts with an empty string buffer.
127 * @param __mode Whether the buffer can read, or write, or both.
129 * The default constructor initializes the parent class using its
133 basic_stringbuf(ios_base::openmode __mode)
134 : __streambuf_type(), _M_mode(__mode), _M_string()
138 * @brief Starts with an existing string buffer.
139 * @param __str A string to copy as a starting buffer.
140 * @param __mode Whether the buffer can read, or write, or both.
142 * This constructor initializes the parent class using its
146 basic_stringbuf(const __string_type& __str,
147 ios_base::openmode __mode = ios_base::in | ios_base::out)
148 : __streambuf_type(), _M_mode(),
149 _M_string(__str.data(), __str.size(), __str.get_allocator())
150 { _M_stringbuf_init(__mode); }
152#if __cplusplus >= 201103L
153 basic_stringbuf(const basic_stringbuf&) = delete;
155 basic_stringbuf(basic_stringbuf&& __rhs)
156 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
157 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
159#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
161 basic_stringbuf(const allocator_type& __a)
162 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
165 basic_stringbuf(ios_base::openmode __mode,
166 const allocator_type& __a)
167 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
171 basic_stringbuf(__string_type&& __s,
172 ios_base::openmode __mode = ios_base::in
174 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
175 { _M_stringbuf_init(__mode); }
177 template<typename _SAlloc>
178 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
179 const allocator_type& __a)
180 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a)
183 template<typename _SAlloc>
184 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
185 ios_base::openmode __mode,
186 const allocator_type& __a)
187 : __streambuf_type(), _M_mode(__mode),
188 _M_string(__s.data(), __s.size(), __a)
189 { _M_stringbuf_init(__mode); }
191 template<typename _SAlloc>
193 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
194 ios_base::openmode __mode = ios_base::in
196 : basic_stringbuf(__s, __mode, allocator_type{})
199 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
200 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
201 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
203 allocator_type get_allocator() const noexcept
204 { return _M_string.get_allocator(); }
207 // 27.8.2.2 Assign and swap:
210 operator=(const basic_stringbuf&) = delete;
213 operator=(basic_stringbuf&& __rhs)
215 __xfer_bufptrs __st{__rhs, this};
216 const __streambuf_type& __base = __rhs;
217 __streambuf_type::operator=(__base);
218 this->pubimbue(__rhs.getloc());
219 _M_mode = __rhs._M_mode;
220 _M_string = std::move(__rhs._M_string);
221 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
226 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
228 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
229 __xfer_bufptrs __r_st{__rhs, this};
230 __streambuf_type& __base = __rhs;
231 __streambuf_type::swap(__base);
232 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
233 std::swap(_M_mode, __rhs._M_mode);
234 std::swap(_M_string, __rhs._M_string); // XXX not exception safe
238 // Getters and setters:
241 * @brief Copying out the string buffer.
242 * @return A copy of one of the underlying sequences.
244 * <em>If the buffer is only created in input mode, the underlying
245 * character sequence is equal to the input sequence; otherwise, it
246 * is equal to the output sequence.</em> [27.7.1.2]/1
249 str() const _GLIBCXX_LVAL_REF_QUAL
251 __string_type __ret(_M_string.get_allocator());
252 if (char_type* __hi = _M_high_mark())
253 __ret.assign(this->pbase(), __hi);
259#if __cplusplus > 201703L
260#if _GLIBCXX_USE_CXX11_ABI
262 template<__allocator_like _SAlloc>
263 basic_string<_CharT, _Traits, _SAlloc>
264 str(const _SAlloc& __sa) const
267 return { __sv.data(), __sv.size(), __sa };
274 if (char_type* __hi = _M_high_mark())
276 if (_M_string.data() == this->pbase()) [[likely]]
277 // Set length to end of sequence and add null terminator.
278 _M_string._M_set_length(__hi - this->pbase());
280 _M_string.assign(this->pbase(), __hi);
282 auto __str = std::move(_M_string);
284 _M_sync(_M_string.data(), 0, 0);
289 _GLIBCXX_SSTREAM_ALWAYS_INLINE
290 basic_string_view<char_type, traits_type>
291 view() const noexcept
293 if (char_type* __hi = _M_high_mark())
294 return { this->pbase(), __hi };
301 * @brief Setting a new buffer.
302 * @param __s The string to use as a new sequence.
304 * Deallocates any previous stored sequence, then copies @a s to
308 str(const __string_type& __s)
310 // Cannot use _M_string = __s, since v3 strings are COW
311 // (not always true now but assign() always works).
312 _M_string.assign(__s.data(), __s.size());
313 _M_stringbuf_init(_M_mode);
316#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
318 template<__allocator_like _SAlloc>
319 requires (!is_same_v<_SAlloc, _Alloc>)
321 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
323 _M_string.assign(__s.data(), __s.size());
324 _M_stringbuf_init(_M_mode);
329 str(__string_type&& __s)
331 _M_string = std::move(__s);
332 _M_stringbuf_init(_M_mode);
337 // Common initialization code goes here.
339 _M_stringbuf_init(ios_base::openmode __mode)
342 __size_type __len = 0;
343 if (_M_mode & (ios_base::ate | ios_base::app))
344 __len = _M_string.size();
345 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
351 streamsize __ret = -1;
352 if (_M_mode & ios_base::in)
355 __ret = this->egptr() - this->gptr();
364 pbackfail(int_type __c = traits_type::eof());
367 overflow(int_type __c = traits_type::eof());
370 * @brief Manipulates the buffer.
371 * @param __s Pointer to a buffer area.
372 * @param __n Size of @a __s.
375 * If no buffer has already been created, and both @a __s and @a __n are
376 * non-zero, then @c __s is used as a buffer; see
377 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
380 virtual __streambuf_type*
381 setbuf(char_type* __s, streamsize __n)
385 // This is implementation-defined behavior, and assumes
386 // that an external char_type array of length __n exists
387 // and has been pre-allocated. If this is not the case,
388 // things will quickly blow up.
390 // Step 1: Destroy the current internal array.
393 // Step 2: Use the external array.
394 _M_sync(__s, __n, 0);
400 seekoff(off_type __off, ios_base::seekdir __way,
401 ios_base::openmode __mode = ios_base::in | ios_base::out);
404 seekpos(pos_type __sp,
405 ios_base::openmode __mode = ios_base::in | ios_base::out);
407 // Internal function for correctly updating the internal buffer
408 // for a particular _M_string, due to initialization or re-sizing
409 // of an existing _M_string.
411 _M_sync(char_type* __base, __size_type __i, __size_type __o);
413 // Internal function for correctly updating egptr() to the actual
418 if (char_type* __pptr = this->pptr())
420 char_type* __egptr = this->egptr();
421 if (!__egptr || __pptr > __egptr)
423 if (_M_mode & ios_base::in)
424 this->setg(this->eback(), this->gptr(), __pptr);
426 this->setg(__pptr, __pptr, __pptr);
431 // Works around the issue with pbump, part of the protected
432 // interface of basic_streambuf, taking just an int.
434 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
437 // Return a pointer to the end of the underlying character sequence.
438 // This might not be the same character as _M_string.end() because
439 // basic_stringbuf::overflow might have written to unused capacity
440 // in _M_string without updating its length.
441 __attribute__((__always_inline__))
443 _M_high_mark() const _GLIBCXX_NOEXCEPT
445 if (char_type* __pptr = this->pptr())
447 char_type* __egptr = this->egptr();
448 if (!__egptr || __pptr > __egptr)
449 return __pptr; // Underlying sequence is [pbase, pptr).
451 return __egptr; // Underlying sequence is [pbase, egptr).
453 return 0; // Underlying character sequence is just _M_string.
456#if __cplusplus >= 201103L
457#if _GLIBCXX_USE_CXX11_ABI
458 // This type captures the state of the gptr / pptr pointers as offsets
459 // so they can be restored in another object after moving the string.
460 struct __xfer_bufptrs
462 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
463 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
465 const _CharT* const __str = __from._M_string.data();
466 const _CharT* __end = nullptr;
469 _M_goff[0] = __from.eback() - __str;
470 _M_goff[1] = __from.gptr() - __str;
471 _M_goff[2] = __from.egptr() - __str;
472 __end = __from.egptr();
476 _M_poff[0] = __from.pbase() - __str;
477 _M_poff[1] = __from.pptr() - __from.pbase();
478 _M_poff[2] = __from.epptr() - __str;
479 if (!__end || __from.pptr() > __end)
480 __end = __from.pptr();
483 // Set _M_string length to the greater of the get and put areas.
486 // The const_cast avoids changing this constructor's signature,
487 // because it is exported from the dynamic library.
488 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
489 __mut_from._M_string._M_length(__end - __str);
495 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
496 if (_M_goff[0] != -1)
497 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
498 if (_M_poff[0] != -1)
499 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
502 basic_stringbuf* _M_to;
507 // This type does nothing when using Copy-On-Write strings.
508 struct __xfer_bufptrs
510 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
514 // The move constructor initializes an __xfer_bufptrs temporary then
515 // delegates to this constructor to performs moves during its lifetime.
516 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
517 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
518 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
521#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
522 // The move constructor initializes an __xfer_bufptrs temporary then
523 // delegates to this constructor to performs moves during its lifetime.
524 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
526 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
527 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
534 // [27.7.2] Template class basic_istringstream
536 * @brief Controlling input for std::string.
539 * @tparam _CharT Type of character stream.
540 * @tparam _Traits Traits for character type, defaults to
541 * char_traits<_CharT>.
542 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
544 * This class supports reading from objects of type std::basic_string,
545 * using the inherited functions from std::basic_istream. To control
546 * the associated sequence, an instance of std::basic_stringbuf is used,
547 * which this page refers to as @c sb.
549 template<typename _CharT, typename _Traits, typename _Alloc>
550 class basic_istringstream : public basic_istream<_CharT, _Traits>
554 typedef _CharT char_type;
555 typedef _Traits traits_type;
556 // _GLIBCXX_RESOLVE_LIB_DEFECTS
557 // 251. basic_stringbuf missing allocator_type
558 typedef _Alloc allocator_type;
559 typedef typename traits_type::int_type int_type;
560 typedef typename traits_type::pos_type pos_type;
561 typedef typename traits_type::off_type off_type;
563 // Non-standard types:
564 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
565 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
566 typedef basic_istream<char_type, traits_type> __istream_type;
569 __stringbuf_type _M_stringbuf;
575 * @brief Default constructor starts with an empty string buffer.
577 * Initializes @c sb using @c in, and passes @c &sb to the base
578 * class initializer. Does not allocate any buffer.
580 * That's a lie. We initialize the base class with NULL, because the
581 * string class does its own memory management.
583 basic_istringstream()
584 : __istream_type(), _M_stringbuf(ios_base::in)
585 { this->init(&_M_stringbuf); }
588 * @brief Starts with an empty string buffer.
589 * @param __mode Whether the buffer can read, or write, or both.
591 * @c ios_base::in is automatically included in @a __mode.
593 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
594 * class initializer. Does not allocate any buffer.
596 * That's a lie. We initialize the base class with NULL, because the
597 * string class does its own memory management.
600 basic_istringstream(ios_base::openmode __mode)
601 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
602 { this->init(&_M_stringbuf); }
605 * @brief Starts with an existing string buffer.
606 * @param __str A string to copy as a starting buffer.
607 * @param __mode Whether the buffer can read, or write, or both.
609 * @c ios_base::in is automatically included in @a mode.
611 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
612 * to the base class initializer.
614 * That's a lie. We initialize the base class with NULL, because the
615 * string class does its own memory management.
618 basic_istringstream(const __string_type& __str,
619 ios_base::openmode __mode = ios_base::in)
620 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
621 { this->init(&_M_stringbuf); }
624 * @brief The destructor does nothing.
626 * The buffer is deallocated by the stringbuf object, not the
629 ~basic_istringstream()
632#if __cplusplus >= 201103L
633 basic_istringstream(const basic_istringstream&) = delete;
635 basic_istringstream(basic_istringstream&& __rhs)
636 : __istream_type(std::move(__rhs)),
637 _M_stringbuf(std::move(__rhs._M_stringbuf))
638 { __istream_type::set_rdbuf(&_M_stringbuf); }
640#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
641 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
642 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
643 { this->init(std::__addressof(_M_stringbuf)); }
646 basic_istringstream(__string_type&& __str,
647 ios_base::openmode __mode = ios_base::in)
648 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
649 { this->init(std::__addressof(_M_stringbuf)); }
651 template<typename _SAlloc>
652 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
653 const allocator_type& __a)
654 : basic_istringstream(__str, ios_base::in, __a)
657 template<typename _SAlloc>
658 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
659 ios_base::openmode __mode,
660 const allocator_type& __a)
661 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
662 { this->init(std::__addressof(_M_stringbuf)); }
664 template<typename _SAlloc>
666 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
667 ios_base::openmode __mode = ios_base::in)
668 : basic_istringstream(__str, __mode, allocator_type())
672 // 27.8.3.2 Assign and swap:
675 operator=(const basic_istringstream&) = delete;
678 operator=(basic_istringstream&& __rhs)
680 __istream_type::operator=(std::move(__rhs));
681 _M_stringbuf = std::move(__rhs._M_stringbuf);
686 swap(basic_istringstream& __rhs)
688 __istream_type::swap(__rhs);
689 _M_stringbuf.swap(__rhs._M_stringbuf);
695 * @brief Accessing the underlying buffer.
696 * @return The current basic_stringbuf buffer.
698 * This hides both signatures of std::basic_ios::rdbuf().
702 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
705 * @brief Copying out the string buffer.
706 * @return @c rdbuf()->str()
709 str() const _GLIBCXX_LVAL_REF_QUAL
710 { return _M_stringbuf.str(); }
712#if __cplusplus > 201703L
713#if _GLIBCXX_USE_CXX11_ABI
715 template<__allocator_like _SAlloc>
716 basic_string<_CharT, _Traits, _SAlloc>
717 str(const _SAlloc& __sa) const
718 { return _M_stringbuf.str(__sa); }
723 { return std::move(_M_stringbuf).str(); }
726 _GLIBCXX_SSTREAM_ALWAYS_INLINE
727 basic_string_view<char_type, traits_type>
728 view() const noexcept
729 { return _M_stringbuf.view(); }
733 * @brief Setting a new buffer.
734 * @param __s The string to use as a new sequence.
736 * Calls @c rdbuf()->str(s).
739 str(const __string_type& __s)
740 { _M_stringbuf.str(__s); }
742#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
744 template<__allocator_like _SAlloc>
745 requires (!is_same_v<_SAlloc, _Alloc>)
747 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
748 { _M_stringbuf.str(__s); }
752 str(__string_type&& __s)
753 { _M_stringbuf.str(std::move(__s)); }
758 // [27.7.3] Template class basic_ostringstream
760 * @brief Controlling output for std::string.
763 * @tparam _CharT Type of character stream.
764 * @tparam _Traits Traits for character type, defaults to
765 * char_traits<_CharT>.
766 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
768 * This class supports writing to objects of type std::basic_string,
769 * using the inherited functions from std::basic_ostream. To control
770 * the associated sequence, an instance of std::basic_stringbuf is used,
771 * which this page refers to as @c sb.
773 template <typename _CharT, typename _Traits, typename _Alloc>
774 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
778 typedef _CharT char_type;
779 typedef _Traits traits_type;
780 // _GLIBCXX_RESOLVE_LIB_DEFECTS
781 // 251. basic_stringbuf missing allocator_type
782 typedef _Alloc allocator_type;
783 typedef typename traits_type::int_type int_type;
784 typedef typename traits_type::pos_type pos_type;
785 typedef typename traits_type::off_type off_type;
787 // Non-standard types:
788 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
789 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
790 typedef basic_ostream<char_type, traits_type> __ostream_type;
793 __stringbuf_type _M_stringbuf;
796 // Constructors/destructor:
799 * @brief Default constructor starts with an empty string buffer.
801 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
802 * class initializer. Does not allocate any buffer.
804 * That's a lie. We initialize the base class with NULL, because the
805 * string class does its own memory management.
807 basic_ostringstream()
808 : __ostream_type(), _M_stringbuf(ios_base::out)
809 { this->init(&_M_stringbuf); }
812 * @brief Starts with an empty string buffer.
813 * @param __mode Whether the buffer can read, or write, or both.
815 * @c ios_base::out is automatically included in @a mode.
817 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
818 * class initializer. Does not allocate any buffer.
820 * That's a lie. We initialize the base class with NULL, because the
821 * string class does its own memory management.
824 basic_ostringstream(ios_base::openmode __mode)
825 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
826 { this->init(&_M_stringbuf); }
829 * @brief Starts with an existing string buffer.
830 * @param __str A string to copy as a starting buffer.
831 * @param __mode Whether the buffer can read, or write, or both.
833 * @c ios_base::out is automatically included in @a mode.
835 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
836 * to the base class initializer.
838 * That's a lie. We initialize the base class with NULL, because the
839 * string class does its own memory management.
842 basic_ostringstream(const __string_type& __str,
843 ios_base::openmode __mode = ios_base::out)
844 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
845 { this->init(&_M_stringbuf); }
848 * @brief The destructor does nothing.
850 * The buffer is deallocated by the stringbuf object, not the
853 ~basic_ostringstream()
856#if __cplusplus >= 201103L
857 basic_ostringstream(const basic_ostringstream&) = delete;
859 basic_ostringstream(basic_ostringstream&& __rhs)
860 : __ostream_type(std::move(__rhs)),
861 _M_stringbuf(std::move(__rhs._M_stringbuf))
862 { __ostream_type::set_rdbuf(&_M_stringbuf); }
864#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
865 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
866 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
867 { this->init(std::__addressof(_M_stringbuf)); }
870 basic_ostringstream(__string_type&& __str,
871 ios_base::openmode __mode = ios_base::out)
872 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
873 { this->init(std::__addressof(_M_stringbuf)); }
875 template<typename _SAlloc>
876 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
877 const allocator_type& __a)
878 : basic_ostringstream(__str, ios_base::out, __a)
881 template<typename _SAlloc>
882 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
883 ios_base::openmode __mode,
884 const allocator_type& __a)
885 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
886 { this->init(std::__addressof(_M_stringbuf)); }
888 template<typename _SAlloc>
890 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
891 ios_base::openmode __mode = ios_base::out)
892 : basic_ostringstream(__str, __mode, allocator_type())
896 // 27.8.3.2 Assign and swap:
899 operator=(const basic_ostringstream&) = delete;
902 operator=(basic_ostringstream&& __rhs)
904 __ostream_type::operator=(std::move(__rhs));
905 _M_stringbuf = std::move(__rhs._M_stringbuf);
910 swap(basic_ostringstream& __rhs)
912 __ostream_type::swap(__rhs);
913 _M_stringbuf.swap(__rhs._M_stringbuf);
919 * @brief Accessing the underlying buffer.
920 * @return The current basic_stringbuf buffer.
922 * This hides both signatures of std::basic_ios::rdbuf().
926 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
929 * @brief Copying out the string buffer.
930 * @return @c rdbuf()->str()
933 str() const _GLIBCXX_LVAL_REF_QUAL
934 { return _M_stringbuf.str(); }
936#if __cplusplus > 201703L
937#if _GLIBCXX_USE_CXX11_ABI
939 template<__allocator_like _SAlloc>
940 basic_string<_CharT, _Traits, _SAlloc>
941 str(const _SAlloc& __sa) const
942 { return _M_stringbuf.str(__sa); }
947 { return std::move(_M_stringbuf).str(); }
950 _GLIBCXX_SSTREAM_ALWAYS_INLINE
951 basic_string_view<char_type, traits_type>
952 view() const noexcept
953 { return _M_stringbuf.view(); }
957 * @brief Setting a new buffer.
958 * @param __s The string to use as a new sequence.
960 * Calls @c rdbuf()->str(s).
963 str(const __string_type& __s)
964 { _M_stringbuf.str(__s); }
966#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
968 template<__allocator_like _SAlloc>
969 requires (!is_same_v<_SAlloc, _Alloc>)
971 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
972 { _M_stringbuf.str(__s); }
976 str(__string_type&& __s)
977 { _M_stringbuf.str(std::move(__s)); }
982 // [27.7.4] Template class basic_stringstream
984 * @brief Controlling input and output for std::string.
987 * @tparam _CharT Type of character stream.
988 * @tparam _Traits Traits for character type, defaults to
989 * char_traits<_CharT>.
990 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
992 * This class supports reading from and writing to objects of type
993 * std::basic_string, using the inherited functions from
994 * std::basic_iostream. To control the associated sequence, an instance
995 * of std::basic_stringbuf is used, which this page refers to as @c sb.
997 template <typename _CharT, typename _Traits, typename _Alloc>
998 class basic_stringstream : public basic_iostream<_CharT, _Traits>
1002 typedef _CharT char_type;
1003 typedef _Traits traits_type;
1004 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1005 // 251. basic_stringbuf missing allocator_type
1006 typedef _Alloc allocator_type;
1007 typedef typename traits_type::int_type int_type;
1008 typedef typename traits_type::pos_type pos_type;
1009 typedef typename traits_type::off_type off_type;
1011 // Non-standard Types:
1012 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1013 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
1014 typedef basic_iostream<char_type, traits_type> __iostream_type;
1017 __stringbuf_type _M_stringbuf;
1020 // Constructors/destructors
1023 * @brief Default constructor starts with an empty string buffer.
1025 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1026 * to the base class initializer. Does not allocate any buffer.
1028 * That's a lie. We initialize the base class with NULL, because the
1029 * string class does its own memory management.
1031 basic_stringstream()
1032 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1033 { this->init(&_M_stringbuf); }
1036 * @brief Starts with an empty string buffer.
1037 * @param __m Whether the buffer can read, or write, or both.
1039 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1040 * to the base class initializer. Does not allocate any buffer.
1042 * That's a lie. We initialize the base class with NULL, because the
1043 * string class does its own memory management.
1046 basic_stringstream(ios_base::openmode __m)
1047 : __iostream_type(), _M_stringbuf(__m)
1048 { this->init(&_M_stringbuf); }
1051 * @brief Starts with an existing string buffer.
1052 * @param __str A string to copy as a starting buffer.
1053 * @param __m Whether the buffer can read, or write, or both.
1055 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1056 * to the base class initializer.
1058 * That's a lie. We initialize the base class with NULL, because the
1059 * string class does its own memory management.
1062 basic_stringstream(const __string_type& __str,
1063 ios_base::openmode __m = ios_base::out | ios_base::in)
1064 : __iostream_type(), _M_stringbuf(__str, __m)
1065 { this->init(&_M_stringbuf); }
1068 * @brief The destructor does nothing.
1070 * The buffer is deallocated by the stringbuf object, not the
1071 * formatting stream.
1073 ~basic_stringstream()
1076#if __cplusplus >= 201103L
1077 basic_stringstream(const basic_stringstream&) = delete;
1079 basic_stringstream(basic_stringstream&& __rhs)
1080 : __iostream_type(std::move(__rhs)),
1081 _M_stringbuf(std::move(__rhs._M_stringbuf))
1082 { __iostream_type::set_rdbuf(&_M_stringbuf); }
1084#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1085 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1086 : __iostream_type(), _M_stringbuf(__mode, __a)
1087 { this->init(&_M_stringbuf); }
1090 basic_stringstream(__string_type&& __str,
1091 ios_base::openmode __mode = ios_base::in
1093 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1094 { this->init(std::__addressof(_M_stringbuf)); }
1096 template<typename _SAlloc>
1097 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1098 const allocator_type& __a)
1099 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1102 template<typename _SAlloc>
1103 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1104 ios_base::openmode __mode,
1105 const allocator_type& __a)
1106 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1107 { this->init(std::__addressof(_M_stringbuf)); }
1109 template<typename _SAlloc>
1111 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1112 ios_base::openmode __mode = ios_base::in
1114 : basic_stringstream(__str, __mode, allocator_type())
1118 // 27.8.3.2 Assign and swap:
1121 operator=(const basic_stringstream&) = delete;
1124 operator=(basic_stringstream&& __rhs)
1126 __iostream_type::operator=(std::move(__rhs));
1127 _M_stringbuf = std::move(__rhs._M_stringbuf);
1132 swap(basic_stringstream& __rhs)
1134 __iostream_type::swap(__rhs);
1135 _M_stringbuf.swap(__rhs._M_stringbuf);
1141 * @brief Accessing the underlying buffer.
1142 * @return The current basic_stringbuf buffer.
1144 * This hides both signatures of std::basic_ios::rdbuf().
1148 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1151 * @brief Copying out the string buffer.
1152 * @return @c rdbuf()->str()
1155 str() const _GLIBCXX_LVAL_REF_QUAL
1156 { return _M_stringbuf.str(); }
1158#if __cplusplus > 201703L
1159#if _GLIBCXX_USE_CXX11_ABI
1161 template<__allocator_like _SAlloc>
1162 basic_string<_CharT, _Traits, _SAlloc>
1163 str(const _SAlloc& __sa) const
1164 { return _M_stringbuf.str(__sa); }
1169 { return std::move(_M_stringbuf).str(); }
1172 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1173 basic_string_view<char_type, traits_type>
1174 view() const noexcept
1175 { return _M_stringbuf.view(); }
1179 * @brief Setting a new buffer.
1180 * @param __s The string to use as a new sequence.
1182 * Calls @c rdbuf()->str(s).
1185 str(const __string_type& __s)
1186 { _M_stringbuf.str(__s); }
1188#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1190 template<__allocator_like _SAlloc>
1191 requires (!is_same_v<_SAlloc, _Alloc>)
1193 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1194 { _M_stringbuf.str(__s); }
1198 str(__string_type&& __s)
1199 { _M_stringbuf.str(std::move(__s)); }
1203#if __cplusplus >= 201103L
1204 /// Swap specialization for stringbufs.
1205 template <class _CharT, class _Traits, class _Allocator>
1207 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1208 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1209 noexcept(noexcept(__x.swap(__y)))
1212 /// Swap specialization for istringstreams.
1213 template <class _CharT, class _Traits, class _Allocator>
1215 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1216 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1219 /// Swap specialization for ostringstreams.
1220 template <class _CharT, class _Traits, class _Allocator>
1222 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1223 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1226 /// Swap specialization for stringstreams.
1227 template <class _CharT, class _Traits, class _Allocator>
1229 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1230 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1234_GLIBCXX_END_NAMESPACE_CXX11
1235_GLIBCXX_END_NAMESPACE_VERSION
1238#undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1239#undef _GLIBCXX_LVAL_REF_QUAL
1241#include <bits/sstream.tcc>
1243#endif /* _GLIBCXX_SSTREAM */