1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997-2026 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 #ifdef _GLIBCXX_SYSHDR
37 #pragma GCC system_header
40 #include <bits/requires_hosted.h> // iostream
45 #include <bits/alloc_traits.h> // allocator_traits, __allocator_like
47 #define __glibcxx_want_sstream_from_string_view
48 #include <bits/version.h>
50 #ifdef __cpp_lib_sstream_from_string_view
51 # include <string_view>
54 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
55 # define _GLIBCXX_LVAL_REF_QUAL &
56 # define _GLIBCXX_SSTREAM_ALWAYS_INLINE
58 # define _GLIBCXX_LVAL_REF_QUAL
59 // For symbols that are not exported from libstdc++.so for the COW string ABI.
60 # define _GLIBCXX_SSTREAM_ALWAYS_INLINE [[__gnu__::__always_inline__]]
63 namespace std _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
66 _GLIBCXX_BEGIN_NAMESPACE_CXX11
68 // [27.7.1] template class basic_stringbuf
70 * @brief The actual work of input and output (for std::string).
73 * @tparam _CharT Type of character stream.
74 * @tparam _Traits Traits for character type, defaults to
75 * char_traits<_CharT>.
76 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
78 * This class associates either or both of its input and output sequences
79 * with a sequence of characters, which can be initialized from, or made
80 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
82 * For this class, open modes (of type @c ios_base::openmode) have
83 * @c in set if the input sequence can be read, and @c out set if the
84 * output sequence can be written.
86 template<typename _CharT, typename _Traits, typename _Alloc>
87 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
89 struct __xfer_bufptrs;
91 #if __cplusplus >= 201103L
92 using allocator_traits = std::allocator_traits<_Alloc>;
94 = __or_<typename allocator_traits::propagate_on_container_swap,
95 typename allocator_traits::is_always_equal>;
100 typedef _CharT char_type;
101 typedef _Traits traits_type;
102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
103 // 251. basic_stringbuf missing allocator_type
104 typedef _Alloc allocator_type;
105 typedef typename traits_type::int_type int_type;
106 typedef typename traits_type::pos_type pos_type;
107 typedef typename traits_type::off_type off_type;
109 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
110 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
111 typedef typename __string_type::size_type __size_type;
114 /// Place to stash in || out || in | out settings for current stringbuf.
115 ios_base::openmode _M_mode;
118 __string_type _M_string;
124 * @brief Starts with an empty string buffer.
126 * The default constructor initializes the parent class using its
130 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
134 * @brief Starts with an empty string buffer.
135 * @param __mode Whether the buffer can read, or write, or both.
137 * The default constructor initializes the parent class using its
141 basic_stringbuf(ios_base::openmode __mode)
142 : __streambuf_type(), _M_mode(__mode), _M_string()
146 * @brief Starts with an existing string buffer.
147 * @param __str A string to copy as a starting buffer.
148 * @param __mode Whether the buffer can read, or write, or both.
150 * This constructor initializes the parent class using its
154 basic_stringbuf(const __string_type& __str,
155 ios_base::openmode __mode = ios_base::in | ios_base::out)
156 : __streambuf_type(), _M_mode(),
157 _M_string(__str.data(), __str.size(), __str.get_allocator())
158 { _M_stringbuf_init(__mode); }
160 #if __cplusplus >= 201103L
161 basic_stringbuf(const basic_stringbuf&) = delete;
163 basic_stringbuf(basic_stringbuf&& __rhs)
164 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
165 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
167 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
168 // P0408 Efficient access to basic_stringbuf buffer
170 basic_stringbuf(const allocator_type& __a)
171 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
174 basic_stringbuf(ios_base::openmode __mode,
175 const allocator_type& __a)
176 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
180 basic_stringbuf(__string_type&& __s,
181 ios_base::openmode __mode = ios_base::in
183 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
184 { _M_stringbuf_init(__mode); }
186 template<typename _SAlloc>
187 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
188 const allocator_type& __a)
189 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a)
192 template<typename _SAlloc>
193 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
194 ios_base::openmode __mode,
195 const allocator_type& __a)
196 : __streambuf_type(), _M_mode(__mode),
197 _M_string(__s.data(), __s.size(), __a)
198 { _M_stringbuf_init(__mode); }
200 template<typename _SAlloc>
202 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
203 ios_base::openmode __mode = ios_base::in
205 : basic_stringbuf(__s, __mode, allocator_type{})
209 #ifdef __cpp_lib_sstream_from_string_view
210 template<typename _Tp>
212 basic_stringbuf(const _Tp& __t,
213 ios_base::openmode __mode = ios_base::in | ios_base::out)
214 requires (is_convertible_v<const _Tp&,
215 basic_string_view<_CharT, _Traits>>)
216 : basic_stringbuf(__t, __mode, allocator_type{})
219 template<typename _Tp>
220 basic_stringbuf(const _Tp& __t, const allocator_type& __a)
221 requires (is_convertible_v<const _Tp&,
222 basic_string_view<_CharT, _Traits>>)
223 : basic_stringbuf(__t, ios_base::in | ios_base::out, __a)
226 template<typename _Tp>
227 basic_stringbuf(const _Tp& __t, ios_base::openmode __mode,
228 const allocator_type& __a)
229 requires (is_convertible_v<const _Tp&,
230 basic_string_view<_CharT, _Traits>>)
231 : _M_string(__t, __a)
232 { _M_stringbuf_init(__mode); }
235 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
236 // P0408 Efficient access to basic_stringbuf buffer
237 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
238 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
239 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
241 allocator_type get_allocator() const noexcept
242 { return _M_string.get_allocator(); }
245 // 27.8.2.2 Assign and swap:
248 operator=(const basic_stringbuf&) = delete;
251 operator=(basic_stringbuf&& __rhs)
253 __xfer_bufptrs __st{__rhs, this};
254 const __streambuf_type& __base = __rhs;
255 __streambuf_type::operator=(__base);
256 this->pubimbue(__rhs.getloc());
257 _M_mode = __rhs._M_mode;
258 _M_string = std::move(__rhs._M_string);
259 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
264 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
266 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
267 __xfer_bufptrs __r_st{__rhs, this};
268 __streambuf_type& __base = __rhs;
269 __streambuf_type::swap(__base);
270 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
271 std::swap(_M_mode, __rhs._M_mode);
272 std::swap(_M_string, __rhs._M_string); // XXX not exception safe
276 // Getters and setters:
279 * @brief Copying out the string buffer.
280 * @return A copy of one of the underlying sequences.
282 * <em>If the buffer is only created in input mode, the underlying
283 * character sequence is equal to the input sequence; otherwise, it
284 * is equal to the output sequence.</em> [27.7.1.2]/1
288 str() const _GLIBCXX_LVAL_REF_QUAL
290 __string_type __ret(_M_string.get_allocator());
291 if (char_type* __hi = _M_high_mark())
292 __ret.assign(this->pbase(), __hi);
298 #if __cplusplus > 201703L
299 #if _GLIBCXX_USE_CXX11_ABI
301 // P0407 Allocator-aware basic_streambuf
302 template<__allocator_like _SAlloc>
304 basic_string<_CharT, _Traits, _SAlloc>
305 str(const _SAlloc& __sa) const
308 return { __sv.data(), __sv.size(), __sa };
316 if (char_type* __hi = _M_high_mark())
318 if (_M_string.data() == this->pbase()) [[likely]]
319 // Set length to end of sequence and add null terminator.
320 _M_string._M_set_length(__hi - this->pbase());
322 _M_string.assign(this->pbase(), __hi);
324 auto __str = std::move(_M_string);
326 _M_sync(_M_string.data(), 0, 0);
331 _GLIBCXX_SSTREAM_ALWAYS_INLINE
332 basic_string_view<char_type, traits_type>
333 view() const noexcept
335 if (char_type* __hi = _M_high_mark())
336 return { this->pbase(), __hi };
343 * @brief Setting a new buffer.
344 * @param __s The string to use as a new sequence.
346 * Deallocates any previous stored sequence, then copies @a s to
350 str(const __string_type& __s)
352 // Cannot use _M_string = __s, since v3 strings are COW
353 // (not always true now but assign() always works).
354 _M_string.assign(__s.data(), __s.size());
355 _M_stringbuf_init(_M_mode);
358 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
360 // P0407 Allocator-aware basic_streambuf
361 template<__allocator_like _SAlloc>
362 requires (!is_same_v<_SAlloc, _Alloc>)
364 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
366 _M_string.assign(__s.data(), __s.size());
367 _M_stringbuf_init(_M_mode);
372 str(__string_type&& __s)
374 _M_string = std::move(__s);
375 _M_stringbuf_init(_M_mode);
379 #ifdef __cpp_lib_sstream_from_string_view
380 template <typename _Tp>
383 requires (is_convertible_v<const _Tp&,
384 basic_string_view<_CharT, _Traits>>)
386 basic_string_view<_CharT, _Traits> __sv{__t};
388 _M_stringbuf_init(_M_mode);
393 // Common initialization code goes here.
395 _M_stringbuf_init(ios_base::openmode __mode)
398 __size_type __len = 0;
399 if (_M_mode & (ios_base::ate | ios_base::app))
400 __len = _M_string.size();
401 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
407 streamsize __ret = -1;
408 if (_M_mode & ios_base::in)
411 __ret = this->egptr() - this->gptr();
420 pbackfail(int_type __c = traits_type::eof());
423 overflow(int_type __c = traits_type::eof());
426 * @brief Manipulates the buffer.
427 * @param __s Pointer to a buffer area.
428 * @param __n Size of @a __s.
431 * If no buffer has already been created, and both @a __s and @a __n are
432 * non-zero, then @c __s is used as a buffer; see
433 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
436 virtual __streambuf_type*
437 setbuf(char_type* __s, streamsize __n)
441 // This is implementation-defined behavior, and assumes
442 // that an external char_type array of length __n exists
443 // and has been pre-allocated. If this is not the case,
444 // things will quickly blow up.
446 // Step 1: Destroy the current internal array.
449 // Step 2: Use the external array.
450 _M_sync(__s, __n, 0);
456 seekoff(off_type __off, ios_base::seekdir __way,
457 ios_base::openmode __mode = ios_base::in | ios_base::out);
460 seekpos(pos_type __sp,
461 ios_base::openmode __mode = ios_base::in | ios_base::out);
463 // Internal function for correctly updating the internal buffer
464 // for a particular _M_string, due to initialization or re-sizing
465 // of an existing _M_string.
467 _M_sync(char_type* __base, __size_type __i, __size_type __o);
469 // Internal function for correctly updating egptr() to the actual
474 if (char_type* __pptr = this->pptr())
476 char_type* __egptr = this->egptr();
477 if (!__egptr || __pptr > __egptr)
479 if (_M_mode & ios_base::in)
480 this->setg(this->eback(), this->gptr(), __pptr);
482 this->setg(__pptr, __pptr, __pptr);
487 // Works around the issue with pbump, part of the protected
488 // interface of basic_streambuf, taking just an int.
490 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
493 // Return a pointer to the end of the underlying character sequence.
494 // This might not be the same character as _M_string.end() because
495 // basic_stringbuf::overflow might have written to unused capacity
496 // in _M_string without updating its length.
497 __attribute__((__always_inline__))
499 _M_high_mark() const _GLIBCXX_NOEXCEPT
501 if (char_type* __pptr = this->pptr())
503 char_type* __egptr = this->egptr();
504 if (!__egptr || __pptr > __egptr)
505 return __pptr; // Underlying sequence is [pbase, pptr).
507 return __egptr; // Underlying sequence is [pbase, egptr).
509 return 0; // Underlying character sequence is just _M_string.
512 #if __cplusplus >= 201103L
513 #if _GLIBCXX_USE_CXX11_ABI
514 // This type captures the state of the gptr / pptr pointers as offsets
515 // so they can be restored in another object after moving the string.
516 struct __xfer_bufptrs
518 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
519 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
521 const _CharT* const __str = __from._M_string.data();
522 const _CharT* __end = nullptr;
525 _M_goff[0] = __from.eback() - __str;
526 _M_goff[1] = __from.gptr() - __str;
527 _M_goff[2] = __from.egptr() - __str;
528 __end = __from.egptr();
532 _M_poff[0] = __from.pbase() - __str;
533 _M_poff[1] = __from.pptr() - __from.pbase();
534 _M_poff[2] = __from.epptr() - __str;
535 if (!__end || __from.pptr() > __end)
536 __end = __from.pptr();
539 // Set _M_string length to the greater of the get and put areas.
542 // The const_cast avoids changing this constructor's signature,
543 // because it is exported from the dynamic library.
544 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
545 __mut_from._M_string._M_length(__end - __str);
551 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
552 if (_M_goff[0] != -1)
553 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
554 if (_M_poff[0] != -1)
555 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
558 basic_stringbuf* _M_to;
563 // This type does nothing when using Copy-On-Write strings.
564 struct __xfer_bufptrs
566 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
570 // The move constructor initializes an __xfer_bufptrs temporary then
571 // delegates to this constructor to performs moves during its lifetime.
572 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
573 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
574 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
577 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
578 // P0408 Efficient access to basic_stringbuf buffer
580 // The move constructor initializes an __xfer_bufptrs temporary then
581 // delegates to this constructor to performs moves during its lifetime.
582 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
584 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
585 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
592 // [27.7.2] Template class basic_istringstream
594 * @brief Controlling input for std::string.
597 * @tparam _CharT Type of character stream.
598 * @tparam _Traits Traits for character type, defaults to
599 * char_traits<_CharT>.
600 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
602 * This class supports reading from objects of type std::basic_string,
603 * using the inherited functions from std::basic_istream. To control
604 * the associated sequence, an instance of std::basic_stringbuf is used,
605 * which this page refers to as @c sb.
607 template<typename _CharT, typename _Traits, typename _Alloc>
608 class basic_istringstream : public basic_istream<_CharT, _Traits>
612 typedef _CharT char_type;
613 typedef _Traits traits_type;
614 // _GLIBCXX_RESOLVE_LIB_DEFECTS
615 // 251. basic_stringbuf missing allocator_type
616 typedef _Alloc allocator_type;
617 typedef typename traits_type::int_type int_type;
618 typedef typename traits_type::pos_type pos_type;
619 typedef typename traits_type::off_type off_type;
621 // Non-standard types:
622 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
623 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
624 typedef basic_istream<char_type, traits_type> __istream_type;
627 __stringbuf_type _M_stringbuf;
633 * @brief Default constructor starts with an empty string buffer.
635 * Initializes @c sb using @c in, and passes @c &sb to the base
636 * class initializer. Does not allocate any buffer.
638 * That's a lie. We initialize the base class with NULL, because the
639 * string class does its own memory management.
641 basic_istringstream()
642 : __istream_type(), _M_stringbuf(ios_base::in)
643 { this->init(std::__addressof(_M_stringbuf)); }
646 * @brief Starts with an empty string buffer.
647 * @param __mode Whether the buffer can read, or write, or both.
649 * @c ios_base::in is automatically included in @a __mode.
651 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
652 * class initializer. Does not allocate any buffer.
654 * That's a lie. We initialize the base class with NULL, because the
655 * string class does its own memory management.
658 basic_istringstream(ios_base::openmode __mode)
659 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
660 { this->init(std::__addressof(_M_stringbuf)); }
663 * @brief Starts with an existing string buffer.
664 * @param __str A string to copy as a starting buffer.
665 * @param __mode Whether the buffer can read, or write, or both.
667 * @c ios_base::in is automatically included in @a mode.
669 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
670 * to the base class initializer.
672 * That's a lie. We initialize the base class with NULL, because the
673 * string class does its own memory management.
676 basic_istringstream(const __string_type& __str,
677 ios_base::openmode __mode = ios_base::in)
678 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
679 { this->init(std::__addressof(_M_stringbuf)); }
682 * @brief The destructor does nothing.
684 * The buffer is deallocated by the stringbuf object, not the
687 ~basic_istringstream()
690 #if __cplusplus >= 201103L
691 basic_istringstream(const basic_istringstream&) = delete;
693 basic_istringstream(basic_istringstream&& __rhs)
694 : __istream_type(std::move(__rhs)),
695 _M_stringbuf(std::move(__rhs._M_stringbuf))
696 { __istream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
698 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
699 // P0408 Efficient access to basic_stringbuf buffer
700 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
701 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
702 { this->init(std::__addressof(_M_stringbuf)); }
705 basic_istringstream(__string_type&& __str,
706 ios_base::openmode __mode = ios_base::in)
707 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
708 { this->init(std::__addressof(_M_stringbuf)); }
710 template<typename _SAlloc>
711 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
712 const allocator_type& __a)
713 : basic_istringstream(__str, ios_base::in, __a)
716 template<typename _SAlloc>
717 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
718 ios_base::openmode __mode,
719 const allocator_type& __a)
720 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
721 { this->init(std::__addressof(_M_stringbuf)); }
723 template<typename _SAlloc>
725 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
726 ios_base::openmode __mode = ios_base::in)
727 : basic_istringstream(__str, __mode, allocator_type())
731 #ifdef __cpp_lib_sstream_from_string_view
732 template <typename _Tp>
734 basic_istringstream(const _Tp& __t,
735 ios_base::openmode __mode = ios_base::in)
736 requires (is_convertible_v<const _Tp&,
737 basic_string_view<_CharT, _Traits>>)
738 : basic_istringstream(__t, __mode, allocator_type{})
741 template <typename _Tp>
742 basic_istringstream(const _Tp& __t, const allocator_type& __a)
743 requires (is_convertible_v<const _Tp&,
744 basic_string_view<_CharT, _Traits>>)
745 : basic_istringstream(__t, ios_base::in, __a)
748 template <typename _Tp>
749 basic_istringstream(const _Tp& __t, ios_base::openmode __mode,
750 const allocator_type& __a)
751 requires (is_convertible_v<const _Tp&,
752 basic_string_view<_CharT, _Traits>>)
753 : __istream_type(), _M_stringbuf(__t, __mode | ios_base::in, __a)
754 { this->init(std::__addressof(_M_stringbuf)); }
757 // 27.8.3.2 Assign and swap:
760 operator=(const basic_istringstream&) = delete;
763 operator=(basic_istringstream&& __rhs)
765 __istream_type::operator=(std::move(__rhs));
766 _M_stringbuf = std::move(__rhs._M_stringbuf);
771 swap(basic_istringstream& __rhs)
773 __istream_type::swap(__rhs);
774 _M_stringbuf.swap(__rhs._M_stringbuf);
780 * @brief Accessing the underlying buffer.
781 * @return The current basic_stringbuf buffer.
783 * This hides both signatures of std::basic_ios::rdbuf().
788 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
791 * @brief Copying out the string buffer.
792 * @return @c rdbuf()->str()
796 str() const _GLIBCXX_LVAL_REF_QUAL
797 { return _M_stringbuf.str(); }
799 #if __cplusplus > 201703L
800 #if _GLIBCXX_USE_CXX11_ABI
802 // P0407 Allocator-aware basic_streambuf
803 template<__allocator_like _SAlloc>
805 basic_string<_CharT, _Traits, _SAlloc>
806 str(const _SAlloc& __sa) const
807 { return _M_stringbuf.str(__sa); }
813 { return std::move(_M_stringbuf).str(); }
816 _GLIBCXX_SSTREAM_ALWAYS_INLINE
817 basic_string_view<char_type, traits_type>
818 view() const noexcept
819 { return _M_stringbuf.view(); }
823 * @brief Setting a new buffer.
824 * @param __s The string to use as a new sequence.
826 * Calls @c rdbuf()->str(s).
829 str(const __string_type& __s)
830 { _M_stringbuf.str(__s); }
832 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
834 // P0407 Allocator-aware basic_streambuf
835 template<__allocator_like _SAlloc>
836 requires (!is_same_v<_SAlloc, _Alloc>)
838 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
839 { _M_stringbuf.str(__s); }
843 str(__string_type&& __s)
844 { _M_stringbuf.str(std::move(__s)); }
847 #ifdef __cpp_lib_sstream_from_string_view
848 template<typename _Tp>
851 requires (is_convertible_v<const _Tp&,
852 basic_string_view<_CharT, _Traits>>)
853 { _M_stringbuf.str(__t); }
858 // [27.7.3] Template class basic_ostringstream
860 * @brief Controlling output for std::string.
863 * @tparam _CharT Type of character stream.
864 * @tparam _Traits Traits for character type, defaults to
865 * char_traits<_CharT>.
866 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
868 * This class supports writing to objects of type std::basic_string,
869 * using the inherited functions from std::basic_ostream. To control
870 * the associated sequence, an instance of std::basic_stringbuf is used,
871 * which this page refers to as @c sb.
873 template <typename _CharT, typename _Traits, typename _Alloc>
874 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
878 typedef _CharT char_type;
879 typedef _Traits traits_type;
880 // _GLIBCXX_RESOLVE_LIB_DEFECTS
881 // 251. basic_stringbuf missing allocator_type
882 typedef _Alloc allocator_type;
883 typedef typename traits_type::int_type int_type;
884 typedef typename traits_type::pos_type pos_type;
885 typedef typename traits_type::off_type off_type;
887 // Non-standard types:
888 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
889 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
890 typedef basic_ostream<char_type, traits_type> __ostream_type;
893 __stringbuf_type _M_stringbuf;
896 // Constructors/destructor:
899 * @brief Default constructor starts with an empty string buffer.
901 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
902 * class initializer. Does not allocate any buffer.
904 * That's a lie. We initialize the base class with NULL, because the
905 * string class does its own memory management.
907 basic_ostringstream()
908 : __ostream_type(), _M_stringbuf(ios_base::out)
909 { this->init(std::__addressof(_M_stringbuf)); }
912 * @brief Starts with an empty string buffer.
913 * @param __mode Whether the buffer can read, or write, or both.
915 * @c ios_base::out is automatically included in @a mode.
917 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
918 * class initializer. Does not allocate any buffer.
920 * That's a lie. We initialize the base class with NULL, because the
921 * string class does its own memory management.
924 basic_ostringstream(ios_base::openmode __mode)
925 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
926 { this->init(std::__addressof(_M_stringbuf)); }
929 * @brief Starts with an existing string buffer.
930 * @param __str A string to copy as a starting buffer.
931 * @param __mode Whether the buffer can read, or write, or both.
933 * @c ios_base::out is automatically included in @a mode.
935 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
936 * to the base class initializer.
938 * That's a lie. We initialize the base class with NULL, because the
939 * string class does its own memory management.
942 basic_ostringstream(const __string_type& __str,
943 ios_base::openmode __mode = ios_base::out)
944 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
945 { this->init(std::__addressof(_M_stringbuf)); }
948 * @brief The destructor does nothing.
950 * The buffer is deallocated by the stringbuf object, not the
953 ~basic_ostringstream()
956 #if __cplusplus >= 201103L
957 basic_ostringstream(const basic_ostringstream&) = delete;
959 basic_ostringstream(basic_ostringstream&& __rhs)
960 : __ostream_type(std::move(__rhs)),
961 _M_stringbuf(std::move(__rhs._M_stringbuf))
962 { __ostream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
964 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
965 // P0408 Efficient access to basic_stringbuf buffer
966 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
967 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
968 { this->init(std::__addressof(_M_stringbuf)); }
971 basic_ostringstream(__string_type&& __str,
972 ios_base::openmode __mode = ios_base::out)
973 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
974 { this->init(std::__addressof(_M_stringbuf)); }
976 template<typename _SAlloc>
977 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
978 const allocator_type& __a)
979 : basic_ostringstream(__str, ios_base::out, __a)
982 template<typename _SAlloc>
983 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
984 ios_base::openmode __mode,
985 const allocator_type& __a)
986 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
987 { this->init(std::__addressof(_M_stringbuf)); }
989 template<typename _SAlloc>
991 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
992 ios_base::openmode __mode = ios_base::out)
993 : basic_ostringstream(__str, __mode, allocator_type())
997 #ifdef __cpp_lib_sstream_from_string_view
998 template <typename _Tp>
1000 basic_ostringstream(
1001 const _Tp& __t, ios_base::openmode __mode = ios_base::out)
1002 requires (is_convertible_v<const _Tp&,
1003 basic_string_view<_CharT, _Traits>>)
1004 : basic_ostringstream(__t, __mode, allocator_type{})
1007 template <typename _Tp>
1008 basic_ostringstream(const _Tp& __t, const allocator_type& __a)
1009 requires (is_convertible_v<const _Tp&,
1010 basic_string_view<_CharT, _Traits>>)
1011 : basic_ostringstream(__t, ios_base::out, __a)
1014 template <typename _Tp>
1015 basic_ostringstream(const _Tp& __t, ios_base::openmode __mode,
1016 const allocator_type& __a)
1017 requires (is_convertible_v<const _Tp&,
1018 basic_string_view<_CharT, _Traits>>)
1019 : __ostream_type(), _M_stringbuf(__t, __mode | ios_base::out, __a)
1020 { this->init(std::__addressof(_M_stringbuf)); }
1023 // 27.8.3.2 Assign and swap:
1025 basic_ostringstream&
1026 operator=(const basic_ostringstream&) = delete;
1028 basic_ostringstream&
1029 operator=(basic_ostringstream&& __rhs)
1031 __ostream_type::operator=(std::move(__rhs));
1032 _M_stringbuf = std::move(__rhs._M_stringbuf);
1037 swap(basic_ostringstream& __rhs)
1039 __ostream_type::swap(__rhs);
1040 _M_stringbuf.swap(__rhs._M_stringbuf);
1046 * @brief Accessing the underlying buffer.
1047 * @return The current basic_stringbuf buffer.
1049 * This hides both signatures of std::basic_ios::rdbuf().
1054 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
1057 * @brief Copying out the string buffer.
1058 * @return @c rdbuf()->str()
1062 str() const _GLIBCXX_LVAL_REF_QUAL
1063 { return _M_stringbuf.str(); }
1065 #if __cplusplus > 201703L
1066 #if _GLIBCXX_USE_CXX11_ABI
1068 // P0407 Allocator-aware basic_streambuf
1069 template<__allocator_like _SAlloc>
1071 basic_string<_CharT, _Traits, _SAlloc>
1072 str(const _SAlloc& __sa) const
1073 { return _M_stringbuf.str(__sa); }
1079 { return std::move(_M_stringbuf).str(); }
1082 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1083 basic_string_view<char_type, traits_type>
1084 view() const noexcept
1085 { return _M_stringbuf.view(); }
1089 * @brief Setting a new buffer.
1090 * @param __s The string to use as a new sequence.
1092 * Calls @c rdbuf()->str(s).
1095 str(const __string_type& __s)
1096 { _M_stringbuf.str(__s); }
1098 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1100 // P0407 Allocator-aware basic_streambuf
1101 template<__allocator_like _SAlloc>
1102 requires (!is_same_v<_SAlloc, _Alloc>)
1104 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1105 { _M_stringbuf.str(__s); }
1109 str(__string_type&& __s)
1110 { _M_stringbuf.str(std::move(__s)); }
1113 #ifdef __cpp_lib_sstream_from_string_view
1114 template<typename _Tp>
1117 requires (is_convertible_v<const _Tp&,
1118 basic_string_view<_CharT, _Traits>>)
1119 { _M_stringbuf.str(__t); }
1124 // [27.7.4] Template class basic_stringstream
1126 * @brief Controlling input and output for std::string.
1129 * @tparam _CharT Type of character stream.
1130 * @tparam _Traits Traits for character type, defaults to
1131 * char_traits<_CharT>.
1132 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
1134 * This class supports reading from and writing to objects of type
1135 * std::basic_string, using the inherited functions from
1136 * std::basic_iostream. To control the associated sequence, an instance
1137 * of std::basic_stringbuf is used, which this page refers to as @c sb.
1139 template <typename _CharT, typename _Traits, typename _Alloc>
1140 class basic_stringstream : public basic_iostream<_CharT, _Traits>
1144 typedef _CharT char_type;
1145 typedef _Traits traits_type;
1146 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1147 // 251. basic_stringbuf missing allocator_type
1148 typedef _Alloc allocator_type;
1149 typedef typename traits_type::int_type int_type;
1150 typedef typename traits_type::pos_type pos_type;
1151 typedef typename traits_type::off_type off_type;
1153 // Non-standard Types:
1154 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1155 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
1156 typedef basic_iostream<char_type, traits_type> __iostream_type;
1159 __stringbuf_type _M_stringbuf;
1162 // Constructors/destructors
1165 * @brief Default constructor starts with an empty string buffer.
1167 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1168 * to the base class initializer. Does not allocate any buffer.
1170 * That's a lie. We initialize the base class with NULL, because the
1171 * string class does its own memory management.
1173 basic_stringstream()
1174 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1175 { this->init(std::__addressof(_M_stringbuf)); }
1178 * @brief Starts with an empty string buffer.
1179 * @param __m Whether the buffer can read, or write, or both.
1181 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1182 * to the base class initializer. Does not allocate any buffer.
1184 * That's a lie. We initialize the base class with NULL, because the
1185 * string class does its own memory management.
1188 basic_stringstream(ios_base::openmode __m)
1189 : __iostream_type(), _M_stringbuf(__m)
1190 { this->init(std::__addressof(_M_stringbuf)); }
1193 * @brief Starts with an existing string buffer.
1194 * @param __str A string to copy as a starting buffer.
1195 * @param __m Whether the buffer can read, or write, or both.
1197 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1198 * to the base class initializer.
1200 * That's a lie. We initialize the base class with NULL, because the
1201 * string class does its own memory management.
1204 basic_stringstream(const __string_type& __str,
1205 ios_base::openmode __m = ios_base::out | ios_base::in)
1206 : __iostream_type(), _M_stringbuf(__str, __m)
1207 { this->init(std::__addressof(_M_stringbuf)); }
1210 * @brief The destructor does nothing.
1212 * The buffer is deallocated by the stringbuf object, not the
1213 * formatting stream.
1215 ~basic_stringstream()
1218 #if __cplusplus >= 201103L
1219 basic_stringstream(const basic_stringstream&) = delete;
1221 basic_stringstream(basic_stringstream&& __rhs)
1222 : __iostream_type(std::move(__rhs)),
1223 _M_stringbuf(std::move(__rhs._M_stringbuf))
1224 { __iostream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
1226 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1227 // P0408 Efficient access to basic_stringbuf buffer
1228 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1229 : __iostream_type(), _M_stringbuf(__mode, __a)
1230 { this->init(std::__addressof(_M_stringbuf)); }
1233 basic_stringstream(__string_type&& __str,
1234 ios_base::openmode __mode = ios_base::in
1236 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1237 { this->init(std::__addressof(_M_stringbuf)); }
1239 template<typename _SAlloc>
1240 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1241 const allocator_type& __a)
1242 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1245 template<typename _SAlloc>
1246 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1247 ios_base::openmode __mode,
1248 const allocator_type& __a)
1249 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1250 { this->init(std::__addressof(_M_stringbuf)); }
1252 template<typename _SAlloc>
1254 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1255 ios_base::openmode __mode = ios_base::in
1257 : basic_stringstream(__str, __mode, allocator_type())
1261 #ifdef __cpp_lib_sstream_from_string_view
1262 template <typename _Tp>
1264 basic_stringstream(const _Tp& __t,
1265 ios_base::openmode __mode = ios_base::in | ios_base::out)
1266 requires (is_convertible_v<const _Tp&,
1267 basic_string_view<_CharT, _Traits>>)
1268 : basic_stringstream(__t, __mode, allocator_type{})
1271 template <typename _Tp>
1272 basic_stringstream(const _Tp& __t, const allocator_type& __a)
1273 requires (is_convertible_v<const _Tp&,
1274 basic_string_view<_CharT, _Traits>>)
1275 : basic_stringstream(__t, ios_base::in | ios_base::out, __a)
1278 template <typename _Tp>
1279 basic_stringstream(const _Tp& __t, ios_base::openmode __mode,
1280 const allocator_type& __a)
1281 requires (is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>>)
1282 : __iostream_type(), _M_stringbuf(__t, __mode, __a)
1283 { this->init(std::__addressof(_M_stringbuf)); }
1286 // 27.8.3.2 Assign and swap:
1289 operator=(const basic_stringstream&) = delete;
1292 operator=(basic_stringstream&& __rhs)
1294 __iostream_type::operator=(std::move(__rhs));
1295 _M_stringbuf = std::move(__rhs._M_stringbuf);
1300 swap(basic_stringstream& __rhs)
1302 __iostream_type::swap(__rhs);
1303 _M_stringbuf.swap(__rhs._M_stringbuf);
1309 * @brief Accessing the underlying buffer.
1310 * @return The current basic_stringbuf buffer.
1312 * This hides both signatures of std::basic_ios::rdbuf().
1317 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
1320 * @brief Copying out the string buffer.
1321 * @return @c rdbuf()->str()
1325 str() const _GLIBCXX_LVAL_REF_QUAL
1326 { return _M_stringbuf.str(); }
1328 #if __cplusplus > 201703L
1329 #if _GLIBCXX_USE_CXX11_ABI
1331 // P0407 Allocator-aware basic_streambuf
1332 template<__allocator_like _SAlloc>
1334 basic_string<_CharT, _Traits, _SAlloc>
1335 str(const _SAlloc& __sa) const
1336 { return _M_stringbuf.str(__sa); }
1342 { return std::move(_M_stringbuf).str(); }
1345 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1346 basic_string_view<char_type, traits_type>
1347 view() const noexcept
1348 { return _M_stringbuf.view(); }
1352 * @brief Setting a new buffer.
1353 * @param __s The string to use as a new sequence.
1355 * Calls @c rdbuf()->str(s).
1358 str(const __string_type& __s)
1359 { _M_stringbuf.str(__s); }
1361 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1363 // P0407 Allocator-aware basic_streambuf
1364 template<__allocator_like _SAlloc>
1365 requires (!is_same_v<_SAlloc, _Alloc>)
1367 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1368 { _M_stringbuf.str(__s); }
1372 str(__string_type&& __s)
1373 { _M_stringbuf.str(std::move(__s)); }
1376 #ifdef __cpp_lib_sstream_from_string_view
1377 template<typename _Tp>
1380 requires (is_convertible_v<const _Tp&,
1381 basic_string_view<_CharT, _Traits>>)
1382 { _M_stringbuf.str(__t); }
1386 #if __cplusplus >= 201103L
1387 /// Swap specialization for stringbufs.
1388 template <class _CharT, class _Traits, class _Allocator>
1390 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1391 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1392 noexcept(noexcept(__x.swap(__y)))
1395 /// Swap specialization for istringstreams.
1396 template <class _CharT, class _Traits, class _Allocator>
1398 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1399 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1402 /// Swap specialization for ostringstreams.
1403 template <class _CharT, class _Traits, class _Allocator>
1405 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1406 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1409 /// Swap specialization for stringstreams.
1410 template <class _CharT, class _Traits, class _Allocator>
1412 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1413 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1417 _GLIBCXX_END_NAMESPACE_CXX11
1418 _GLIBCXX_END_NAMESPACE_VERSION
1421 #undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1422 #undef _GLIBCXX_LVAL_REF_QUAL
1424 #include <bits/sstream.tcc>
1426 #endif /* _GLIBCXX_SSTREAM */