libstdc++
sstream
Go to the documentation of this file.
1// String based streams -*- C++ -*-
2
3// Copyright (C) 1997-2024 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/sstream
26 * This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 27.7 String-based streams
31//
32
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
35
36#pragma GCC system_header
37
38#include <bits/requires_hosted.h> // iostream
39
40#include <istream>
41#include <ostream>
42#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
43
44#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
45# define _GLIBCXX_LVAL_REF_QUAL &
46# define _GLIBCXX_SSTREAM_ALWAYS_INLINE
47#else
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__]]
51#endif
52
53
54
55namespace std _GLIBCXX_VISIBILITY(default)
56{
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
58_GLIBCXX_BEGIN_NAMESPACE_CXX11
59
60 // [27.7.1] template class basic_stringbuf
61 /**
62 * @brief The actual work of input and output (for std::string).
63 * @ingroup io
64 *
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>.
69 *
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.)
73 *
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.
77 */
78 template<typename _CharT, typename _Traits, typename _Alloc>
79 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
80 {
81 struct __xfer_bufptrs;
82
83#if __cplusplus >= 201103L
84 using allocator_traits = std::allocator_traits<_Alloc>;
85 using _Noexcept_swap
88#endif
89
90 public:
91 // Types:
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;
100
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;
104
105 protected:
106 /// Place to stash in || out || in | out settings for current stringbuf.
108
109 // Data Members:
110 __string_type _M_string;
111
112 public:
113 // Constructors:
114
115 /**
116 * @brief Starts with an empty string buffer.
117 *
118 * The default constructor initializes the parent class using its
119 * own default ctor.
120 */
122 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
123 { }
124
125 /**
126 * @brief Starts with an empty string buffer.
127 * @param __mode Whether the buffer can read, or write, or both.
128 *
129 * The default constructor initializes the parent class using its
130 * own default ctor.
131 */
132 explicit
134 : __streambuf_type(), _M_mode(__mode), _M_string()
135 { }
136
137 /**
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.
141 *
142 * This constructor initializes the parent class using its
143 * own default ctor.
144 */
145 explicit
146 basic_stringbuf(const __string_type& __str,
148 : __streambuf_type(), _M_mode(),
149 _M_string(__str.data(), __str.size(), __str.get_allocator())
150 { _M_stringbuf_init(__mode); }
151
152#if __cplusplus >= 201103L
153 basic_stringbuf(const basic_stringbuf&) = delete;
154
156 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
157 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
158
159#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
160 explicit
161 basic_stringbuf(const allocator_type& __a)
162 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
163 { }
164
166 const allocator_type& __a)
167 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
168 { }
169
170 explicit
171 basic_stringbuf(__string_type&& __s,
174 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
175 { _M_stringbuf_init(__mode); }
176
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)
181 { }
182
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); }
190
191 template<typename _SAlloc>
192 explicit
193 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
196 : basic_stringbuf(__s, __mode, allocator_type{})
197 { }
198
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); }
202
203 allocator_type get_allocator() const noexcept
204 { return _M_string.get_allocator(); }
205#endif // C++20
206
207 // 27.8.2.2 Assign and swap:
208
210 operator=(const basic_stringbuf&) = delete;
211
213 operator=(basic_stringbuf&& __rhs)
214 {
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);
222 return *this;
223 }
224
225 void
226 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
227 {
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
235 }
236#endif // C++11
237
238 // Getters and setters:
239
240 /**
241 * @brief Copying out the string buffer.
242 * @return A copy of one of the underlying sequences.
243 *
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
247 */
248 __string_type
249 str() const _GLIBCXX_LVAL_REF_QUAL
250 {
251 __string_type __ret(_M_string.get_allocator());
252 if (char_type* __hi = _M_high_mark())
253 __ret.assign(this->pbase(), __hi);
254 else
255 __ret = _M_string;
256 return __ret;
257 }
258
259#if __cplusplus > 201703L
260#if _GLIBCXX_USE_CXX11_ABI
261#if __cpp_concepts
262 template<__allocator_like _SAlloc>
264 str(const _SAlloc& __sa) const
265 {
266 auto __sv = view();
267 return { __sv.data(), __sv.size(), __sa };
268 }
269#endif
270
271 __string_type
272 str() &&
273 {
274 if (char_type* __hi = _M_high_mark())
275 {
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());
279 else
280 _M_string.assign(this->pbase(), __hi);
281 }
282 auto __str = std::move(_M_string);
283 _M_string.clear();
284 _M_sync(_M_string.data(), 0, 0);
285 return __str;
286 }
287#endif // cxx11 ABI
288
289 _GLIBCXX_SSTREAM_ALWAYS_INLINE
290 basic_string_view<char_type, traits_type>
291 view() const noexcept
292 {
293 if (char_type* __hi = _M_high_mark())
294 return { this->pbase(), __hi };
295 else
296 return _M_string;
297 }
298#endif // C++20
299
300 /**
301 * @brief Setting a new buffer.
302 * @param __s The string to use as a new sequence.
303 *
304 * Deallocates any previous stored sequence, then copies @a s to
305 * use as a new one.
306 */
307 void
308 str(const __string_type& __s)
309 {
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);
314 }
315
316#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
317#if __cpp_concepts
318 template<__allocator_like _SAlloc>
319 requires (!is_same_v<_SAlloc, _Alloc>)
320 void
322 {
323 _M_string.assign(__s.data(), __s.size());
324 _M_stringbuf_init(_M_mode);
325 }
326#endif
327
328 void
329 str(__string_type&& __s)
330 {
331 _M_string = std::move(__s);
332 _M_stringbuf_init(_M_mode);
333 }
334#endif
335
336 protected:
337 // Common initialization code goes here.
338 void
339 _M_stringbuf_init(ios_base::openmode __mode)
340 {
341 _M_mode = __mode;
342 __size_type __len = 0;
344 __len = _M_string.size();
345 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
346 }
347
349 showmanyc()
350 {
351 streamsize __ret = -1;
352 if (_M_mode & ios_base::in)
353 {
354 _M_update_egptr();
355 __ret = this->egptr() - this->gptr();
356 }
357 return __ret;
358 }
359
360 virtual int_type
361 underflow();
362
363 virtual int_type
364 pbackfail(int_type __c = traits_type::eof());
365
366 virtual int_type
367 overflow(int_type __c = traits_type::eof());
368
369 /**
370 * @brief Manipulates the buffer.
371 * @param __s Pointer to a buffer area.
372 * @param __n Size of @a __s.
373 * @return @c this
374 *
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
378 * for more.
379 */
380 virtual __streambuf_type*
381 setbuf(char_type* __s, streamsize __n)
382 {
383 if (__s && __n >= 0)
384 {
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.
389
390 // Step 1: Destroy the current internal array.
391 _M_string.clear();
392
393 // Step 2: Use the external array.
394 _M_sync(__s, __n, 0);
395 }
396 return this;
397 }
398
399 virtual pos_type
400 seekoff(off_type __off, ios_base::seekdir __way,
402
403 virtual pos_type
404 seekpos(pos_type __sp,
406
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.
410 void
411 _M_sync(char_type* __base, __size_type __i, __size_type __o);
412
413 // Internal function for correctly updating egptr() to the actual
414 // string end.
415 void
416 _M_update_egptr()
417 {
418 if (char_type* __pptr = this->pptr())
419 {
420 char_type* __egptr = this->egptr();
421 if (!__egptr || __pptr > __egptr)
422 {
423 if (_M_mode & ios_base::in)
424 this->setg(this->eback(), this->gptr(), __pptr);
425 else
426 this->setg(__pptr, __pptr, __pptr);
427 }
428 }
429 }
430
431 // Works around the issue with pbump, part of the protected
432 // interface of basic_streambuf, taking just an int.
433 void
434 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
435
436 private:
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__))
442 char_type*
443 _M_high_mark() const _GLIBCXX_NOEXCEPT
444 {
445 if (char_type* __pptr = this->pptr())
446 {
447 char_type* __egptr = this->egptr();
448 if (!__egptr || __pptr > __egptr)
449 return __pptr; // Underlying sequence is [pbase, pptr).
450 else
451 return __egptr; // Underlying sequence is [pbase, egptr).
452 }
453 return 0; // Underlying character sequence is just _M_string.
454 }
455
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
461 {
462 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
463 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
464 {
465 const _CharT* const __str = __from._M_string.data();
466 const _CharT* __end = nullptr;
467 if (__from.eback())
468 {
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();
473 }
474 if (__from.pbase())
475 {
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();
481 }
482
483 // Set _M_string length to the greater of the get and put areas.
484 if (__end)
485 {
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);
490 }
491 }
492
493 ~__xfer_bufptrs()
494 {
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]);
500 }
501
502 basic_stringbuf* _M_to;
503 off_type _M_goff[3];
504 off_type _M_poff[3];
505 };
506#else
507 // This type does nothing when using Copy-On-Write strings.
508 struct __xfer_bufptrs
509 {
510 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
511 };
512#endif
513
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))
519 { }
520
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,
525 __xfer_bufptrs&&)
526 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
527 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
528 { }
529#endif
530#endif // C++11
531 };
532
533
534 // [27.7.2] Template class basic_istringstream
535 /**
536 * @brief Controlling input for std::string.
537 * @ingroup io
538 *
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>.
543 *
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.
548 */
549 template<typename _CharT, typename _Traits, typename _Alloc>
550 class basic_istringstream : public basic_istream<_CharT, _Traits>
551 {
552 public:
553 // Types:
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;
562
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;
567
568 private:
569 __stringbuf_type _M_stringbuf;
570
571 public:
572 // Constructors:
573
574 /**
575 * @brief Default constructor starts with an empty string buffer.
576 *
577 * Initializes @c sb using @c in, and passes @c &sb to the base
578 * class initializer. Does not allocate any buffer.
579 *
580 * That's a lie. We initialize the base class with NULL, because the
581 * string class does its own memory management.
582 */
584 : __istream_type(), _M_stringbuf(ios_base::in)
585 { this->init(&_M_stringbuf); }
586
587 /**
588 * @brief Starts with an empty string buffer.
589 * @param __mode Whether the buffer can read, or write, or both.
590 *
591 * @c ios_base::in is automatically included in @a __mode.
592 *
593 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
594 * class initializer. Does not allocate any buffer.
595 *
596 * That's a lie. We initialize the base class with NULL, because the
597 * string class does its own memory management.
598 */
599 explicit
601 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
602 { this->init(&_M_stringbuf); }
603
604 /**
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.
608 *
609 * @c ios_base::in is automatically included in @a mode.
610 *
611 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
612 * to the base class initializer.
613 *
614 * That's a lie. We initialize the base class with NULL, because the
615 * string class does its own memory management.
616 */
617 explicit
618 basic_istringstream(const __string_type& __str,
620 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
621 { this->init(&_M_stringbuf); }
622
623 /**
624 * @brief The destructor does nothing.
625 *
626 * The buffer is deallocated by the stringbuf object, not the
627 * formatting stream.
628 */
631
632#if __cplusplus >= 201103L
634
636 : __istream_type(std::move(__rhs)),
637 _M_stringbuf(std::move(__rhs._M_stringbuf))
638 { __istream_type::set_rdbuf(&_M_stringbuf); }
639
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)); }
644
645 explicit
646 basic_istringstream(__string_type&& __str,
648 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
649 { this->init(std::__addressof(_M_stringbuf)); }
650
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)
655 { }
656
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)); }
663
664 template<typename _SAlloc>
665 explicit
666 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
668 : basic_istringstream(__str, __mode, allocator_type())
669 { }
670#endif // C++20
671
672 // 27.8.3.2 Assign and swap:
673
675 operator=(const basic_istringstream&) = delete;
676
678 operator=(basic_istringstream&& __rhs)
679 {
680 __istream_type::operator=(std::move(__rhs));
681 _M_stringbuf = std::move(__rhs._M_stringbuf);
682 return *this;
683 }
684
685 void
686 swap(basic_istringstream& __rhs)
687 {
688 __istream_type::swap(__rhs);
689 _M_stringbuf.swap(__rhs._M_stringbuf);
690 }
691#endif // C++11
692
693 // Members:
694 /**
695 * @brief Accessing the underlying buffer.
696 * @return The current basic_stringbuf buffer.
697 *
698 * This hides both signatures of std::basic_ios::rdbuf().
699 */
700 __stringbuf_type*
701 rdbuf() const
702 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
703
704 /**
705 * @brief Copying out the string buffer.
706 * @return @c rdbuf()->str()
707 */
708 __string_type
709 str() const _GLIBCXX_LVAL_REF_QUAL
710 { return _M_stringbuf.str(); }
711
712#if __cplusplus > 201703L
713#if _GLIBCXX_USE_CXX11_ABI
714#if __cpp_concepts
715 template<__allocator_like _SAlloc>
717 str(const _SAlloc& __sa) const
718 { return _M_stringbuf.str(__sa); }
719#endif
720
721 __string_type
722 str() &&
723 { return std::move(_M_stringbuf).str(); }
724#endif // cxx11 ABI
725
726 _GLIBCXX_SSTREAM_ALWAYS_INLINE
727 basic_string_view<char_type, traits_type>
728 view() const noexcept
729 { return _M_stringbuf.view(); }
730#endif // C++20
731
732 /**
733 * @brief Setting a new buffer.
734 * @param __s The string to use as a new sequence.
735 *
736 * Calls @c rdbuf()->str(s).
737 */
738 void
739 str(const __string_type& __s)
740 { _M_stringbuf.str(__s); }
741
742#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
743#if __cpp_concepts
744 template<__allocator_like _SAlloc>
745 requires (!is_same_v<_SAlloc, _Alloc>)
746 void
748 { _M_stringbuf.str(__s); }
749#endif
750
751 void
752 str(__string_type&& __s)
753 { _M_stringbuf.str(std::move(__s)); }
754#endif
755 };
756
757
758 // [27.7.3] Template class basic_ostringstream
759 /**
760 * @brief Controlling output for std::string.
761 * @ingroup io
762 *
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>.
767 *
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.
772 */
773 template <typename _CharT, typename _Traits, typename _Alloc>
774 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
775 {
776 public:
777 // Types:
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;
786
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;
791
792 private:
793 __stringbuf_type _M_stringbuf;
794
795 public:
796 // Constructors/destructor:
797
798 /**
799 * @brief Default constructor starts with an empty string buffer.
800 *
801 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
802 * class initializer. Does not allocate any buffer.
803 *
804 * That's a lie. We initialize the base class with NULL, because the
805 * string class does its own memory management.
806 */
808 : __ostream_type(), _M_stringbuf(ios_base::out)
809 { this->init(&_M_stringbuf); }
810
811 /**
812 * @brief Starts with an empty string buffer.
813 * @param __mode Whether the buffer can read, or write, or both.
814 *
815 * @c ios_base::out is automatically included in @a mode.
816 *
817 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
818 * class initializer. Does not allocate any buffer.
819 *
820 * That's a lie. We initialize the base class with NULL, because the
821 * string class does its own memory management.
822 */
823 explicit
825 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
826 { this->init(&_M_stringbuf); }
827
828 /**
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.
832 *
833 * @c ios_base::out is automatically included in @a mode.
834 *
835 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
836 * to the base class initializer.
837 *
838 * That's a lie. We initialize the base class with NULL, because the
839 * string class does its own memory management.
840 */
841 explicit
842 basic_ostringstream(const __string_type& __str,
844 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
845 { this->init(&_M_stringbuf); }
846
847 /**
848 * @brief The destructor does nothing.
849 *
850 * The buffer is deallocated by the stringbuf object, not the
851 * formatting stream.
852 */
855
856#if __cplusplus >= 201103L
858
860 : __ostream_type(std::move(__rhs)),
861 _M_stringbuf(std::move(__rhs._M_stringbuf))
862 { __ostream_type::set_rdbuf(&_M_stringbuf); }
863
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)); }
868
869 explicit
870 basic_ostringstream(__string_type&& __str,
872 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
873 { this->init(std::__addressof(_M_stringbuf)); }
874
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)
879 { }
880
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)); }
887
888 template<typename _SAlloc>
889 explicit
890 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
892 : basic_ostringstream(__str, __mode, allocator_type())
893 { }
894#endif // C++20
895
896 // 27.8.3.2 Assign and swap:
897
899 operator=(const basic_ostringstream&) = delete;
900
902 operator=(basic_ostringstream&& __rhs)
903 {
904 __ostream_type::operator=(std::move(__rhs));
905 _M_stringbuf = std::move(__rhs._M_stringbuf);
906 return *this;
907 }
908
909 void
910 swap(basic_ostringstream& __rhs)
911 {
912 __ostream_type::swap(__rhs);
913 _M_stringbuf.swap(__rhs._M_stringbuf);
914 }
915#endif // C++11
916
917 // Members:
918 /**
919 * @brief Accessing the underlying buffer.
920 * @return The current basic_stringbuf buffer.
921 *
922 * This hides both signatures of std::basic_ios::rdbuf().
923 */
924 __stringbuf_type*
925 rdbuf() const
926 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
927
928 /**
929 * @brief Copying out the string buffer.
930 * @return @c rdbuf()->str()
931 */
932 __string_type
933 str() const _GLIBCXX_LVAL_REF_QUAL
934 { return _M_stringbuf.str(); }
935
936#if __cplusplus > 201703L
937#if _GLIBCXX_USE_CXX11_ABI
938#if __cpp_concepts
939 template<__allocator_like _SAlloc>
941 str(const _SAlloc& __sa) const
942 { return _M_stringbuf.str(__sa); }
943#endif
944
945 __string_type
946 str() &&
947 { return std::move(_M_stringbuf).str(); }
948#endif // cxx11 ABI
949
950 _GLIBCXX_SSTREAM_ALWAYS_INLINE
951 basic_string_view<char_type, traits_type>
952 view() const noexcept
953 { return _M_stringbuf.view(); }
954#endif // C++20
955
956 /**
957 * @brief Setting a new buffer.
958 * @param __s The string to use as a new sequence.
959 *
960 * Calls @c rdbuf()->str(s).
961 */
962 void
963 str(const __string_type& __s)
964 { _M_stringbuf.str(__s); }
965
966#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
967#if __cpp_concepts
968 template<__allocator_like _SAlloc>
969 requires (!is_same_v<_SAlloc, _Alloc>)
970 void
972 { _M_stringbuf.str(__s); }
973#endif
974
975 void
976 str(__string_type&& __s)
977 { _M_stringbuf.str(std::move(__s)); }
978#endif
979 };
980
981
982 // [27.7.4] Template class basic_stringstream
983 /**
984 * @brief Controlling input and output for std::string.
985 * @ingroup io
986 *
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>.
991 *
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.
996 */
997 template <typename _CharT, typename _Traits, typename _Alloc>
998 class basic_stringstream : public basic_iostream<_CharT, _Traits>
999 {
1000 public:
1001 // Types:
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;
1010
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;
1015
1016 private:
1017 __stringbuf_type _M_stringbuf;
1018
1019 public:
1020 // Constructors/destructors
1021
1022 /**
1023 * @brief Default constructor starts with an empty string buffer.
1024 *
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.
1027 *
1028 * That's a lie. We initialize the base class with NULL, because the
1029 * string class does its own memory management.
1030 */
1032 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1033 { this->init(&_M_stringbuf); }
1034
1035 /**
1036 * @brief Starts with an empty string buffer.
1037 * @param __m Whether the buffer can read, or write, or both.
1038 *
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.
1041 *
1042 * That's a lie. We initialize the base class with NULL, because the
1043 * string class does its own memory management.
1044 */
1045 explicit
1047 : __iostream_type(), _M_stringbuf(__m)
1048 { this->init(&_M_stringbuf); }
1049
1050 /**
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.
1054 *
1055 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1056 * to the base class initializer.
1057 *
1058 * That's a lie. We initialize the base class with NULL, because the
1059 * string class does its own memory management.
1060 */
1061 explicit
1062 basic_stringstream(const __string_type& __str,
1064 : __iostream_type(), _M_stringbuf(__str, __m)
1065 { this->init(&_M_stringbuf); }
1066
1067 /**
1068 * @brief The destructor does nothing.
1069 *
1070 * The buffer is deallocated by the stringbuf object, not the
1071 * formatting stream.
1072 */
1075
1076#if __cplusplus >= 201103L
1077 basic_stringstream(const basic_stringstream&) = delete;
1078
1080 : __iostream_type(std::move(__rhs)),
1081 _M_stringbuf(std::move(__rhs._M_stringbuf))
1082 { __iostream_type::set_rdbuf(&_M_stringbuf); }
1083
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); }
1088
1089 explicit
1090 basic_stringstream(__string_type&& __str,
1092 | ios_base::out)
1093 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1094 { this->init(std::__addressof(_M_stringbuf)); }
1095
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)
1100 { }
1101
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)); }
1108
1109 template<typename _SAlloc>
1110 explicit
1111 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1113 | ios_base::out)
1114 : basic_stringstream(__str, __mode, allocator_type())
1115 { }
1116#endif // C++20
1117
1118 // 27.8.3.2 Assign and swap:
1119
1121 operator=(const basic_stringstream&) = delete;
1122
1124 operator=(basic_stringstream&& __rhs)
1125 {
1126 __iostream_type::operator=(std::move(__rhs));
1127 _M_stringbuf = std::move(__rhs._M_stringbuf);
1128 return *this;
1129 }
1130
1131 void
1132 swap(basic_stringstream& __rhs)
1133 {
1134 __iostream_type::swap(__rhs);
1135 _M_stringbuf.swap(__rhs._M_stringbuf);
1136 }
1137#endif // C++11
1138
1139 // Members:
1140 /**
1141 * @brief Accessing the underlying buffer.
1142 * @return The current basic_stringbuf buffer.
1143 *
1144 * This hides both signatures of std::basic_ios::rdbuf().
1145 */
1146 __stringbuf_type*
1147 rdbuf() const
1148 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1149
1150 /**
1151 * @brief Copying out the string buffer.
1152 * @return @c rdbuf()->str()
1153 */
1154 __string_type
1155 str() const _GLIBCXX_LVAL_REF_QUAL
1156 { return _M_stringbuf.str(); }
1157
1158#if __cplusplus > 201703L
1159#if _GLIBCXX_USE_CXX11_ABI
1160#if __cpp_concepts
1161 template<__allocator_like _SAlloc>
1163 str(const _SAlloc& __sa) const
1164 { return _M_stringbuf.str(__sa); }
1165#endif
1166
1167 __string_type
1168 str() &&
1169 { return std::move(_M_stringbuf).str(); }
1170#endif // cxx11 ABI
1171
1172 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1173 basic_string_view<char_type, traits_type>
1174 view() const noexcept
1175 { return _M_stringbuf.view(); }
1176#endif // C++20
1177
1178 /**
1179 * @brief Setting a new buffer.
1180 * @param __s The string to use as a new sequence.
1181 *
1182 * Calls @c rdbuf()->str(s).
1183 */
1184 void
1185 str(const __string_type& __s)
1186 { _M_stringbuf.str(__s); }
1187
1188#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1189#if __cpp_concepts
1190 template<__allocator_like _SAlloc>
1191 requires (!is_same_v<_SAlloc, _Alloc>)
1192 void
1194 { _M_stringbuf.str(__s); }
1195#endif
1196
1197 void
1198 str(__string_type&& __s)
1199 { _M_stringbuf.str(std::move(__s)); }
1200#endif
1201 };
1202
1203#if __cplusplus >= 201103L
1204 /// Swap specialization for stringbufs.
1205 template <class _CharT, class _Traits, class _Allocator>
1206 inline void
1209 noexcept(noexcept(__x.swap(__y)))
1210 { __x.swap(__y); }
1211
1212 /// Swap specialization for istringstreams.
1213 template <class _CharT, class _Traits, class _Allocator>
1214 inline void
1218
1219 /// Swap specialization for ostringstreams.
1220 template <class _CharT, class _Traits, class _Allocator>
1221 inline void
1225
1226 /// Swap specialization for stringstreams.
1227 template <class _CharT, class _Traits, class _Allocator>
1228 inline void
1232#endif // C++11
1233
1234_GLIBCXX_END_NAMESPACE_CXX11
1235_GLIBCXX_END_NAMESPACE_VERSION
1236} // namespace
1237
1238#undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1239#undef _GLIBCXX_LVAL_REF_QUAL
1240
1241#include <bits/sstream.tcc>
1242
1243#endif /* _GLIBCXX_SSTREAM */
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:137
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:68
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
constexpr _Iterator __base(_Iterator __it)
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
char_type * pptr() const
Access to the put area.
Definition streambuf:544
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
Definition streambuf:521
char_type * eback() const
Access to the get area.
Definition streambuf:489
char_type * egptr() const
Access to the get area.
Definition streambuf:499
char_type * gptr() const
Access to the get area.
Definition streambuf:496
locale pubimbue(const locale &__loc)
Entry point for imbue().
Definition streambuf:216
char_type * pbase() const
Access to the put area.
Definition streambuf:541
traits_type::off_type off_type
Definition streambuf:137
basic_streambuf()
Base constructor.
Definition streambuf:470
basic_istream(__streambuf_type *__sb)
Base constructor.
Definition istream:95
basic_ostream(__streambuf_type *__sb)
Base constructor.
Definition ostream:92
basic_iostream(basic_streambuf< _CharT, _Traits > *__sb)
Constructor does nothing.
Definition istream:1008
The actual work of input and output (for std::string).
Definition sstream:80
virtual streamsize showmanyc()
Investigating the data available.
Definition sstream:348
virtual int_type underflow()
Fetches more data from the controlled sequence.
Definition sstream.tcc:150
virtual pos_type seekpos(pos_type __sp, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:216
virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:168
virtual int_type overflow(int_type __c=traits_type::eof())
Consumes data from the buffer; writes to the controlled sequence.
Definition sstream.tcc:80
basic_stringbuf()
Starts with an empty string buffer.
Definition sstream:120
virtual int_type pbackfail(int_type __c=traits_type::eof())
Tries to back up the input sequence.
Definition sstream.tcc:46
__string_type str() const &
Copying out the string buffer.
Definition sstream:248
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
Definition sstream:380
ios_base::openmode _M_mode
Definition sstream:106
Controlling input for std::string.
Definition sstream:551
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:739
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:701
~basic_istringstream()
The destructor does nothing.
Definition sstream:629
basic_istringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::in)
Starts with an existing string buffer.
Definition sstream:618
__string_type str() const &
Copying out the string buffer.
Definition sstream:709
basic_istringstream()
Default constructor starts with an empty string buffer.
Definition sstream:583
basic_istringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:600
Controlling output for std::string.
Definition sstream:775
~basic_ostringstream()
The destructor does nothing.
Definition sstream:853
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:963
basic_ostringstream()
Default constructor starts with an empty string buffer.
Definition sstream:807
basic_ostringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::out)
Starts with an existing string buffer.
Definition sstream:842
__string_type str() const &
Copying out the string buffer.
Definition sstream:933
basic_ostringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:824
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:925
Controlling input and output for std::string.
Definition sstream:999
~basic_stringstream()
The destructor does nothing.
Definition sstream:1073
basic_stringstream(const __string_type &__str, ios_base::openmode __m=ios_base::out|ios_base::in)
Starts with an existing string buffer.
Definition sstream:1062
basic_stringstream()
Default constructor starts with an empty string buffer.
Definition sstream:1031
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:1185
__string_type str() const &
Copying out the string buffer.
Definition sstream:1155
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:1147
basic_stringstream(ios_base::openmode __m)
Starts with an empty string buffer.
Definition sstream:1046
Uniform interface to all allocator types.
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
Managing sequences of characters and character-like objects.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
The base of the I/O class hierarchy.
Definition ios_base.h:255
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:487
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:490
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:473
static const openmode app
Seek to end before each write.
Definition ios_base.h:476
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:512
static const openmode ate
Open and seek to end immediately after opening.
Definition ios_base.h:479