| 1 | // String based streams -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 1997-2026 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file include/sstream |
| 26 | * This is a Standard C++ Library header. |
| 27 | */ |
| 28 | |
| 29 | // |
| 30 | // ISO C++ 14882: 27.7 String-based streams |
| 31 | // |
| 32 | |
| 33 | #ifndef _GLIBCXX_SSTREAM |
| 34 | #define _GLIBCXX_SSTREAM 1 |
| 35 | |
| 36 | #ifdef _GLIBCXX_SYSHDR |
| 37 | #pragma GCC system_header |
| 38 | #endif |
| 39 | |
| 40 | #include <bits/requires_hosted.h> // iostream |
| 41 | |
| 42 | #include <istream> |
| 43 | #include <ostream> |
| 44 | |
| 45 | #include <bits/alloc_traits.h> // allocator_traits, __allocator_like |
| 46 | |
| 47 | #define __glibcxx_want_sstream_from_string_view |
| 48 | #include <bits/version.h> |
| 49 | |
| 50 | #ifdef __cpp_lib_sstream_from_string_view |
| 51 | # include <string_view> |
| 52 | #endif |
| 53 | |
| 54 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 55 | # define _GLIBCXX_LVAL_REF_QUAL & |
| 56 | # define _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 57 | #else |
| 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__]] |
| 61 | #endif |
| 62 | |
| 63 | namespace std _GLIBCXX_VISIBILITY(default) |
| 64 | { |
| 65 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 66 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
| 67 | |
| 68 | // [27.7.1] template class basic_stringbuf |
| 69 | /** |
| 70 | * @brief The actual work of input and output (for std::string). |
| 71 | * @ingroup io |
| 72 | * |
| 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>. |
| 77 | * |
| 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.) |
| 81 | * |
| 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. |
| 85 | */ |
| 86 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 87 | class basic_stringbuf : public basic_streambuf<_CharT, _Traits> |
| 88 | { |
| 89 | struct __xfer_bufptrs; |
| 90 | |
| 91 | #if __cplusplus >= 201103L |
| 92 | using allocator_traits = std::allocator_traits<_Alloc>; |
| 93 | using _Noexcept_swap |
| 94 | = __or_<typename allocator_traits::propagate_on_container_swap, |
| 95 | typename allocator_traits::is_always_equal>; |
| 96 | #endif |
| 97 | |
| 98 | public: |
| 99 | // Types: |
| 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; |
| 108 | |
| 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; |
| 112 | |
| 113 | protected: |
| 114 | /// Place to stash in || out || in | out settings for current stringbuf. |
| 115 | ios_base::openmode _M_mode; |
| 116 | |
| 117 | // Data Members: |
| 118 | __string_type _M_string; |
| 119 | |
| 120 | public: |
| 121 | // Constructors: |
| 122 | |
| 123 | /** |
| 124 | * @brief Starts with an empty string buffer. |
| 125 | * |
| 126 | * The default constructor initializes the parent class using its |
| 127 | * own default ctor. |
| 128 | */ |
| 129 | basic_stringbuf() |
| 130 | : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string() |
| 131 | { } |
| 132 | |
| 133 | /** |
| 134 | * @brief Starts with an empty string buffer. |
| 135 | * @param __mode Whether the buffer can read, or write, or both. |
| 136 | * |
| 137 | * The default constructor initializes the parent class using its |
| 138 | * own default ctor. |
| 139 | */ |
| 140 | explicit |
| 141 | basic_stringbuf(ios_base::openmode __mode) |
| 142 | : __streambuf_type(), _M_mode(__mode), _M_string() |
| 143 | { } |
| 144 | |
| 145 | /** |
| 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. |
| 149 | * |
| 150 | * This constructor initializes the parent class using its |
| 151 | * own default ctor. |
| 152 | */ |
| 153 | explicit |
| 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); } |
| 159 | |
| 160 | #if __cplusplus >= 201103L |
| 161 | basic_stringbuf(const basic_stringbuf&) = delete; |
| 162 | |
| 163 | basic_stringbuf(basic_stringbuf&& __rhs) |
| 164 | : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this)) |
| 165 | { __rhs._M_sync(base: const_cast<char_type*>(__rhs._M_string.data()), i: 0, o: 0); } |
| 166 | |
| 167 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 168 | // P0408 Efficient access to basic_stringbuf buffer |
| 169 | explicit |
| 170 | basic_stringbuf(const allocator_type& __a) |
| 171 | : basic_stringbuf(ios_base::in | std::ios_base::out, __a) |
| 172 | { } |
| 173 | |
| 174 | basic_stringbuf(ios_base::openmode __mode, |
| 175 | const allocator_type& __a) |
| 176 | : __streambuf_type(), _M_mode(__mode), _M_string(__a) |
| 177 | { } |
| 178 | |
| 179 | explicit |
| 180 | basic_stringbuf(__string_type&& __s, |
| 181 | ios_base::openmode __mode = ios_base::in |
| 182 | | ios_base::out) |
| 183 | : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s)) |
| 184 | { _M_stringbuf_init(__mode); } |
| 185 | |
| 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) |
| 190 | { } |
| 191 | |
| 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); } |
| 199 | |
| 200 | template<typename _SAlloc> |
| 201 | explicit |
| 202 | basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s, |
| 203 | ios_base::openmode __mode = ios_base::in |
| 204 | | ios_base::out) |
| 205 | : basic_stringbuf(__s, __mode, allocator_type{}) |
| 206 | { } |
| 207 | #endif |
| 208 | |
| 209 | #ifdef __cpp_lib_sstream_from_string_view |
| 210 | template<typename _Tp> |
| 211 | explicit |
| 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{}) |
| 217 | { } |
| 218 | |
| 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) |
| 224 | { } |
| 225 | |
| 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); } |
| 233 | #endif // C++26 |
| 234 | |
| 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(base: const_cast<char_type*>(__rhs._M_string.data()), i: 0, o: 0); } |
| 240 | |
| 241 | allocator_type get_allocator() const noexcept |
| 242 | { return _M_string.get_allocator(); } |
| 243 | #endif // C++20 |
| 244 | |
| 245 | // 27.8.2.2 Assign and swap: |
| 246 | |
| 247 | basic_stringbuf& |
| 248 | operator=(const basic_stringbuf&) = delete; |
| 249 | |
| 250 | basic_stringbuf& |
| 251 | operator=(basic_stringbuf&& __rhs) |
| 252 | { |
| 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(base: const_cast<char_type*>(__rhs._M_string.data()), i: 0, o: 0); |
| 260 | return *this; |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value) |
| 265 | { |
| 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(a&: _M_mode, b&: __rhs._M_mode); |
| 272 | std::swap(_M_string, __rhs._M_string); // XXX not exception safe |
| 273 | } |
| 274 | #endif // C++11 |
| 275 | |
| 276 | // Getters and setters: |
| 277 | |
| 278 | /** |
| 279 | * @brief Copying out the string buffer. |
| 280 | * @return A copy of one of the underlying sequences. |
| 281 | * |
| 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 |
| 285 | */ |
| 286 | _GLIBCXX_NODISCARD |
| 287 | __string_type |
| 288 | str() const _GLIBCXX_LVAL_REF_QUAL |
| 289 | { |
| 290 | __string_type __ret(_M_string.get_allocator()); |
| 291 | if (char_type* __hi = _M_high_mark()) |
| 292 | __ret.assign(this->pbase(), __hi); |
| 293 | else |
| 294 | __ret = _M_string; |
| 295 | return __ret; |
| 296 | } |
| 297 | |
| 298 | #if __cplusplus > 201703L |
| 299 | #if _GLIBCXX_USE_CXX11_ABI |
| 300 | #if __cpp_concepts |
| 301 | // P0407 Allocator-aware basic_streambuf |
| 302 | template<__allocator_like _SAlloc> |
| 303 | _GLIBCXX_NODISCARD |
| 304 | basic_string<_CharT, _Traits, _SAlloc> |
| 305 | str(const _SAlloc& __sa) const |
| 306 | { |
| 307 | auto __sv = view(); |
| 308 | return { __sv.data(), __sv.size(), __sa }; |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | _GLIBCXX_NODISCARD |
| 313 | __string_type |
| 314 | str() && |
| 315 | { |
| 316 | if (char_type* __hi = _M_high_mark()) |
| 317 | { |
| 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()); |
| 321 | else |
| 322 | _M_string.assign(this->pbase(), __hi); |
| 323 | } |
| 324 | auto __str = std::move(_M_string); |
| 325 | _M_string.clear(); |
| 326 | _M_sync(base: _M_string.data(), i: 0, o: 0); |
| 327 | return __str; |
| 328 | } |
| 329 | #endif // cxx11 ABI |
| 330 | |
| 331 | _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 332 | basic_string_view<char_type, traits_type> |
| 333 | view() const noexcept |
| 334 | { |
| 335 | if (char_type* __hi = _M_high_mark()) |
| 336 | return { this->pbase(), __hi }; |
| 337 | else |
| 338 | return _M_string; |
| 339 | } |
| 340 | #endif // C++20 |
| 341 | |
| 342 | /** |
| 343 | * @brief Setting a new buffer. |
| 344 | * @param __s The string to use as a new sequence. |
| 345 | * |
| 346 | * Deallocates any previous stored sequence, then copies @a s to |
| 347 | * use as a new one. |
| 348 | */ |
| 349 | void |
| 350 | str(const __string_type& __s) |
| 351 | { |
| 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(mode: _M_mode); |
| 356 | } |
| 357 | |
| 358 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 359 | #if __cpp_concepts |
| 360 | // P0407 Allocator-aware basic_streambuf |
| 361 | template<__allocator_like _SAlloc> |
| 362 | requires (!is_same_v<_SAlloc, _Alloc>) |
| 363 | void |
| 364 | str(const basic_string<_CharT, _Traits, _SAlloc>& __s) |
| 365 | { |
| 366 | _M_string.assign(__s.data(), __s.size()); |
| 367 | _M_stringbuf_init(mode: _M_mode); |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | void |
| 372 | str(__string_type&& __s) |
| 373 | { |
| 374 | _M_string = std::move(__s); |
| 375 | _M_stringbuf_init(mode: _M_mode); |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | #ifdef __cpp_lib_sstream_from_string_view |
| 380 | template <typename _Tp> |
| 381 | void |
| 382 | str(const _Tp& __t) |
| 383 | requires (is_convertible_v<const _Tp&, |
| 384 | basic_string_view<_CharT, _Traits>>) |
| 385 | { |
| 386 | basic_string_view<_CharT, _Traits> __sv{__t}; |
| 387 | _M_string = __sv; |
| 388 | _M_stringbuf_init(_M_mode); |
| 389 | } |
| 390 | #endif // C++26 |
| 391 | |
| 392 | protected: |
| 393 | // Common initialization code goes here. |
| 394 | void |
| 395 | _M_stringbuf_init(ios_base::openmode __mode) |
| 396 | { |
| 397 | _M_mode = __mode; |
| 398 | __size_type __len = 0; |
| 399 | if (_M_mode & (ios_base::ate | ios_base::app)) |
| 400 | __len = _M_string.size(); |
| 401 | _M_sync(base: const_cast<char_type*>(_M_string.data()), i: 0, o: __len); |
| 402 | } |
| 403 | |
| 404 | virtual streamsize |
| 405 | showmanyc() |
| 406 | { |
| 407 | streamsize __ret = -1; |
| 408 | if (_M_mode & ios_base::in) |
| 409 | { |
| 410 | _M_update_egptr(); |
| 411 | __ret = this->egptr() - this->gptr(); |
| 412 | } |
| 413 | return __ret; |
| 414 | } |
| 415 | |
| 416 | virtual int_type |
| 417 | underflow(); |
| 418 | |
| 419 | virtual int_type |
| 420 | pbackfail(int_type __c = traits_type::eof()); |
| 421 | |
| 422 | virtual int_type |
| 423 | overflow(int_type __c = traits_type::eof()); |
| 424 | |
| 425 | /** |
| 426 | * @brief Manipulates the buffer. |
| 427 | * @param __s Pointer to a buffer area. |
| 428 | * @param __n Size of @a __s. |
| 429 | * @return @c this |
| 430 | * |
| 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 |
| 434 | * for more. |
| 435 | */ |
| 436 | virtual __streambuf_type* |
| 437 | setbuf(char_type* __s, streamsize __n) |
| 438 | { |
| 439 | if (__s && __n >= 0) |
| 440 | { |
| 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. |
| 445 | |
| 446 | // Step 1: Destroy the current internal array. |
| 447 | _M_string.clear(); |
| 448 | |
| 449 | // Step 2: Use the external array. |
| 450 | _M_sync(base: __s, i: __n, o: 0); |
| 451 | } |
| 452 | return this; |
| 453 | } |
| 454 | |
| 455 | virtual pos_type |
| 456 | seekoff(off_type __off, ios_base::seekdir __way, |
| 457 | ios_base::openmode __mode = ios_base::in | ios_base::out); |
| 458 | |
| 459 | virtual pos_type |
| 460 | seekpos(pos_type __sp, |
| 461 | ios_base::openmode __mode = ios_base::in | ios_base::out); |
| 462 | |
| 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. |
| 466 | void |
| 467 | _M_sync(char_type* __base, __size_type __i, __size_type __o); |
| 468 | |
| 469 | // Internal function for correctly updating egptr() to the actual |
| 470 | // string end. |
| 471 | void |
| 472 | _M_update_egptr() |
| 473 | { |
| 474 | if (char_type* __pptr = this->pptr()) |
| 475 | { |
| 476 | char_type* __egptr = this->egptr(); |
| 477 | if (!__egptr || __pptr > __egptr) |
| 478 | { |
| 479 | if (_M_mode & ios_base::in) |
| 480 | this->setg(this->eback(), this->gptr(), __pptr); |
| 481 | else |
| 482 | this->setg(__pptr, __pptr, __pptr); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Works around the issue with pbump, part of the protected |
| 488 | // interface of basic_streambuf, taking just an int. |
| 489 | void |
| 490 | _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off); |
| 491 | |
| 492 | private: |
| 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__)) |
| 498 | char_type* |
| 499 | _M_high_mark() const _GLIBCXX_NOEXCEPT |
| 500 | { |
| 501 | if (char_type* __pptr = this->pptr()) |
| 502 | { |
| 503 | char_type* __egptr = this->egptr(); |
| 504 | if (!__egptr || __pptr > __egptr) |
| 505 | return __pptr; // Underlying sequence is [pbase, pptr). |
| 506 | else |
| 507 | return __egptr; // Underlying sequence is [pbase, egptr). |
| 508 | } |
| 509 | return 0; // Underlying character sequence is just _M_string. |
| 510 | } |
| 511 | |
| 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 |
| 517 | { |
| 518 | __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to) |
| 519 | : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1} |
| 520 | { |
| 521 | const _CharT* const __str = __from._M_string.data(); |
| 522 | const _CharT* __end = nullptr; |
| 523 | if (__from.eback()) |
| 524 | { |
| 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(); |
| 529 | } |
| 530 | if (__from.pbase()) |
| 531 | { |
| 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(); |
| 537 | } |
| 538 | |
| 539 | // Set _M_string length to the greater of the get and put areas. |
| 540 | if (__end) |
| 541 | { |
| 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); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | ~__xfer_bufptrs() |
| 550 | { |
| 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(pbeg: __str+_M_poff[0], pend: __str+_M_poff[2], off: _M_poff[1]); |
| 556 | } |
| 557 | |
| 558 | basic_stringbuf* _M_to; |
| 559 | off_type _M_goff[3]; |
| 560 | off_type _M_poff[3]; |
| 561 | }; |
| 562 | #else |
| 563 | // This type does nothing when using Copy-On-Write strings. |
| 564 | struct __xfer_bufptrs |
| 565 | { |
| 566 | __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { } |
| 567 | }; |
| 568 | #endif |
| 569 | |
| 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)) |
| 575 | { } |
| 576 | |
| 577 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 578 | // P0408 Efficient access to basic_stringbuf buffer |
| 579 | |
| 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, |
| 583 | __xfer_bufptrs&&) |
| 584 | : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)), |
| 585 | _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a) |
| 586 | { } |
| 587 | #endif |
| 588 | #endif // C++11 |
| 589 | }; |
| 590 | |
| 591 | |
| 592 | // [27.7.2] Template class basic_istringstream |
| 593 | /** |
| 594 | * @brief Controlling input for std::string. |
| 595 | * @ingroup io |
| 596 | * |
| 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>. |
| 601 | * |
| 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. |
| 606 | */ |
| 607 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 608 | class basic_istringstream : public basic_istream<_CharT, _Traits> |
| 609 | { |
| 610 | public: |
| 611 | // Types: |
| 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; |
| 620 | |
| 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; |
| 625 | |
| 626 | private: |
| 627 | __stringbuf_type _M_stringbuf; |
| 628 | |
| 629 | public: |
| 630 | // Constructors: |
| 631 | |
| 632 | /** |
| 633 | * @brief Default constructor starts with an empty string buffer. |
| 634 | * |
| 635 | * Initializes @c sb using @c in, and passes @c &sb to the base |
| 636 | * class initializer. Does not allocate any buffer. |
| 637 | * |
| 638 | * That's a lie. We initialize the base class with NULL, because the |
| 639 | * string class does its own memory management. |
| 640 | */ |
| 641 | basic_istringstream() |
| 642 | : __istream_type(), _M_stringbuf(ios_base::in) |
| 643 | { this->init(std::__addressof(_M_stringbuf)); } |
| 644 | |
| 645 | /** |
| 646 | * @brief Starts with an empty string buffer. |
| 647 | * @param __mode Whether the buffer can read, or write, or both. |
| 648 | * |
| 649 | * @c ios_base::in is automatically included in @a __mode. |
| 650 | * |
| 651 | * Initializes @c sb using @c __mode|in, and passes @c &sb to the base |
| 652 | * class initializer. Does not allocate any buffer. |
| 653 | * |
| 654 | * That's a lie. We initialize the base class with NULL, because the |
| 655 | * string class does its own memory management. |
| 656 | */ |
| 657 | explicit |
| 658 | basic_istringstream(ios_base::openmode __mode) |
| 659 | : __istream_type(), _M_stringbuf(__mode | ios_base::in) |
| 660 | { this->init(std::__addressof(_M_stringbuf)); } |
| 661 | |
| 662 | /** |
| 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. |
| 666 | * |
| 667 | * @c ios_base::in is automatically included in @a mode. |
| 668 | * |
| 669 | * Initializes @c sb using @a str and @c mode|in, and passes @c &sb |
| 670 | * to the base class initializer. |
| 671 | * |
| 672 | * That's a lie. We initialize the base class with NULL, because the |
| 673 | * string class does its own memory management. |
| 674 | */ |
| 675 | explicit |
| 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)); } |
| 680 | |
| 681 | /** |
| 682 | * @brief The destructor does nothing. |
| 683 | * |
| 684 | * The buffer is deallocated by the stringbuf object, not the |
| 685 | * formatting stream. |
| 686 | */ |
| 687 | ~basic_istringstream() |
| 688 | { } |
| 689 | |
| 690 | #if __cplusplus >= 201103L |
| 691 | basic_istringstream(const basic_istringstream&) = delete; |
| 692 | |
| 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)); } |
| 697 | |
| 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)); } |
| 703 | |
| 704 | explicit |
| 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)); } |
| 709 | |
| 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) |
| 714 | { } |
| 715 | |
| 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)); } |
| 722 | |
| 723 | template<typename _SAlloc> |
| 724 | explicit |
| 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()) |
| 728 | { } |
| 729 | #endif // C++20 |
| 730 | |
| 731 | #ifdef __cpp_lib_sstream_from_string_view |
| 732 | template <typename _Tp> |
| 733 | explicit |
| 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{}) |
| 739 | { } |
| 740 | |
| 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) |
| 746 | { } |
| 747 | |
| 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)); } |
| 755 | #endif // C++26 |
| 756 | |
| 757 | // 27.8.3.2 Assign and swap: |
| 758 | |
| 759 | basic_istringstream& |
| 760 | operator=(const basic_istringstream&) = delete; |
| 761 | |
| 762 | basic_istringstream& |
| 763 | operator=(basic_istringstream&& __rhs) |
| 764 | { |
| 765 | __istream_type::operator=(std::move(__rhs)); |
| 766 | _M_stringbuf = std::move(__rhs._M_stringbuf); |
| 767 | return *this; |
| 768 | } |
| 769 | |
| 770 | void |
| 771 | swap(basic_istringstream& __rhs) |
| 772 | { |
| 773 | __istream_type::swap(__rhs); |
| 774 | _M_stringbuf.swap(__rhs._M_stringbuf); |
| 775 | } |
| 776 | #endif // C++11 |
| 777 | |
| 778 | // Members: |
| 779 | /** |
| 780 | * @brief Accessing the underlying buffer. |
| 781 | * @return The current basic_stringbuf buffer. |
| 782 | * |
| 783 | * This hides both signatures of std::basic_ios::rdbuf(). |
| 784 | */ |
| 785 | _GLIBCXX_NODISCARD |
| 786 | __stringbuf_type* |
| 787 | rdbuf() const |
| 788 | { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); } |
| 789 | |
| 790 | /** |
| 791 | * @brief Copying out the string buffer. |
| 792 | * @return @c rdbuf()->str() |
| 793 | */ |
| 794 | _GLIBCXX_NODISCARD |
| 795 | __string_type |
| 796 | str() const _GLIBCXX_LVAL_REF_QUAL |
| 797 | { return _M_stringbuf.str(); } |
| 798 | |
| 799 | #if __cplusplus > 201703L |
| 800 | #if _GLIBCXX_USE_CXX11_ABI |
| 801 | #if __cpp_concepts |
| 802 | // P0407 Allocator-aware basic_streambuf |
| 803 | template<__allocator_like _SAlloc> |
| 804 | _GLIBCXX_NODISCARD |
| 805 | basic_string<_CharT, _Traits, _SAlloc> |
| 806 | str(const _SAlloc& __sa) const |
| 807 | { return _M_stringbuf.str(__sa); } |
| 808 | #endif |
| 809 | |
| 810 | _GLIBCXX_NODISCARD |
| 811 | __string_type |
| 812 | str() && |
| 813 | { return std::move(_M_stringbuf).str(); } |
| 814 | #endif // cxx11 ABI |
| 815 | |
| 816 | _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 817 | basic_string_view<char_type, traits_type> |
| 818 | view() const noexcept |
| 819 | { return _M_stringbuf.view(); } |
| 820 | #endif // C++20 |
| 821 | |
| 822 | /** |
| 823 | * @brief Setting a new buffer. |
| 824 | * @param __s The string to use as a new sequence. |
| 825 | * |
| 826 | * Calls @c rdbuf()->str(s). |
| 827 | */ |
| 828 | void |
| 829 | str(const __string_type& __s) |
| 830 | { _M_stringbuf.str(__s); } |
| 831 | |
| 832 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 833 | #if __cpp_concepts |
| 834 | // P0407 Allocator-aware basic_streambuf |
| 835 | template<__allocator_like _SAlloc> |
| 836 | requires (!is_same_v<_SAlloc, _Alloc>) |
| 837 | void |
| 838 | str(const basic_string<_CharT, _Traits, _SAlloc>& __s) |
| 839 | { _M_stringbuf.str(__s); } |
| 840 | #endif |
| 841 | |
| 842 | void |
| 843 | str(__string_type&& __s) |
| 844 | { _M_stringbuf.str(std::move(__s)); } |
| 845 | #endif |
| 846 | |
| 847 | #ifdef __cpp_lib_sstream_from_string_view |
| 848 | template<typename _Tp> |
| 849 | void |
| 850 | str(const _Tp& __t) |
| 851 | requires (is_convertible_v<const _Tp&, |
| 852 | basic_string_view<_CharT, _Traits>>) |
| 853 | { _M_stringbuf.str(__t); } |
| 854 | #endif // C++26 |
| 855 | }; |
| 856 | |
| 857 | |
| 858 | // [27.7.3] Template class basic_ostringstream |
| 859 | /** |
| 860 | * @brief Controlling output for std::string. |
| 861 | * @ingroup io |
| 862 | * |
| 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>. |
| 867 | * |
| 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. |
| 872 | */ |
| 873 | template <typename _CharT, typename _Traits, typename _Alloc> |
| 874 | class basic_ostringstream : public basic_ostream<_CharT, _Traits> |
| 875 | { |
| 876 | public: |
| 877 | // Types: |
| 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; |
| 886 | |
| 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; |
| 891 | |
| 892 | private: |
| 893 | __stringbuf_type _M_stringbuf; |
| 894 | |
| 895 | public: |
| 896 | // Constructors/destructor: |
| 897 | |
| 898 | /** |
| 899 | * @brief Default constructor starts with an empty string buffer. |
| 900 | * |
| 901 | * Initializes @c sb using @c mode|out, and passes @c &sb to the base |
| 902 | * class initializer. Does not allocate any buffer. |
| 903 | * |
| 904 | * That's a lie. We initialize the base class with NULL, because the |
| 905 | * string class does its own memory management. |
| 906 | */ |
| 907 | basic_ostringstream() |
| 908 | : __ostream_type(), _M_stringbuf(ios_base::out) |
| 909 | { this->init(std::__addressof(_M_stringbuf)); } |
| 910 | |
| 911 | /** |
| 912 | * @brief Starts with an empty string buffer. |
| 913 | * @param __mode Whether the buffer can read, or write, or both. |
| 914 | * |
| 915 | * @c ios_base::out is automatically included in @a mode. |
| 916 | * |
| 917 | * Initializes @c sb using @c mode|out, and passes @c &sb to the base |
| 918 | * class initializer. Does not allocate any buffer. |
| 919 | * |
| 920 | * That's a lie. We initialize the base class with NULL, because the |
| 921 | * string class does its own memory management. |
| 922 | */ |
| 923 | explicit |
| 924 | basic_ostringstream(ios_base::openmode __mode) |
| 925 | : __ostream_type(), _M_stringbuf(__mode | ios_base::out) |
| 926 | { this->init(std::__addressof(_M_stringbuf)); } |
| 927 | |
| 928 | /** |
| 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. |
| 932 | * |
| 933 | * @c ios_base::out is automatically included in @a mode. |
| 934 | * |
| 935 | * Initializes @c sb using @a str and @c mode|out, and passes @c &sb |
| 936 | * to the base class initializer. |
| 937 | * |
| 938 | * That's a lie. We initialize the base class with NULL, because the |
| 939 | * string class does its own memory management. |
| 940 | */ |
| 941 | explicit |
| 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)); } |
| 946 | |
| 947 | /** |
| 948 | * @brief The destructor does nothing. |
| 949 | * |
| 950 | * The buffer is deallocated by the stringbuf object, not the |
| 951 | * formatting stream. |
| 952 | */ |
| 953 | ~basic_ostringstream() |
| 954 | { } |
| 955 | |
| 956 | #if __cplusplus >= 201103L |
| 957 | basic_ostringstream(const basic_ostringstream&) = delete; |
| 958 | |
| 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)); } |
| 963 | |
| 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)); } |
| 969 | |
| 970 | explicit |
| 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)); } |
| 975 | |
| 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) |
| 980 | { } |
| 981 | |
| 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)); } |
| 988 | |
| 989 | template<typename _SAlloc> |
| 990 | explicit |
| 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()) |
| 994 | { } |
| 995 | #endif // C++20 |
| 996 | |
| 997 | #ifdef __cpp_lib_sstream_from_string_view |
| 998 | template <typename _Tp> |
| 999 | explicit |
| 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{}) |
| 1005 | { } |
| 1006 | |
| 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) |
| 1012 | { } |
| 1013 | |
| 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)); } |
| 1021 | #endif // C++26 |
| 1022 | |
| 1023 | // 27.8.3.2 Assign and swap: |
| 1024 | |
| 1025 | basic_ostringstream& |
| 1026 | operator=(const basic_ostringstream&) = delete; |
| 1027 | |
| 1028 | basic_ostringstream& |
| 1029 | operator=(basic_ostringstream&& __rhs) |
| 1030 | { |
| 1031 | __ostream_type::operator=(std::move(__rhs)); |
| 1032 | _M_stringbuf = std::move(__rhs._M_stringbuf); |
| 1033 | return *this; |
| 1034 | } |
| 1035 | |
| 1036 | void |
| 1037 | swap(basic_ostringstream& __rhs) |
| 1038 | { |
| 1039 | __ostream_type::swap(__rhs); |
| 1040 | _M_stringbuf.swap(__rhs._M_stringbuf); |
| 1041 | } |
| 1042 | #endif // C++11 |
| 1043 | |
| 1044 | // Members: |
| 1045 | /** |
| 1046 | * @brief Accessing the underlying buffer. |
| 1047 | * @return The current basic_stringbuf buffer. |
| 1048 | * |
| 1049 | * This hides both signatures of std::basic_ios::rdbuf(). |
| 1050 | */ |
| 1051 | _GLIBCXX_NODISCARD |
| 1052 | __stringbuf_type* |
| 1053 | rdbuf() const |
| 1054 | { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); } |
| 1055 | |
| 1056 | /** |
| 1057 | * @brief Copying out the string buffer. |
| 1058 | * @return @c rdbuf()->str() |
| 1059 | */ |
| 1060 | _GLIBCXX_NODISCARD |
| 1061 | __string_type |
| 1062 | str() const _GLIBCXX_LVAL_REF_QUAL |
| 1063 | { return _M_stringbuf.str(); } |
| 1064 | |
| 1065 | #if __cplusplus > 201703L |
| 1066 | #if _GLIBCXX_USE_CXX11_ABI |
| 1067 | #if __cpp_concepts |
| 1068 | // P0407 Allocator-aware basic_streambuf |
| 1069 | template<__allocator_like _SAlloc> |
| 1070 | _GLIBCXX_NODISCARD |
| 1071 | basic_string<_CharT, _Traits, _SAlloc> |
| 1072 | str(const _SAlloc& __sa) const |
| 1073 | { return _M_stringbuf.str(__sa); } |
| 1074 | #endif |
| 1075 | |
| 1076 | _GLIBCXX_NODISCARD |
| 1077 | __string_type |
| 1078 | str() && |
| 1079 | { return std::move(_M_stringbuf).str(); } |
| 1080 | #endif // cxx11 ABI |
| 1081 | |
| 1082 | _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 1083 | basic_string_view<char_type, traits_type> |
| 1084 | view() const noexcept |
| 1085 | { return _M_stringbuf.view(); } |
| 1086 | #endif // C++20 |
| 1087 | |
| 1088 | /** |
| 1089 | * @brief Setting a new buffer. |
| 1090 | * @param __s The string to use as a new sequence. |
| 1091 | * |
| 1092 | * Calls @c rdbuf()->str(s). |
| 1093 | */ |
| 1094 | void |
| 1095 | str(const __string_type& __s) |
| 1096 | { _M_stringbuf.str(__s); } |
| 1097 | |
| 1098 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 1099 | #if __cpp_concepts |
| 1100 | // P0407 Allocator-aware basic_streambuf |
| 1101 | template<__allocator_like _SAlloc> |
| 1102 | requires (!is_same_v<_SAlloc, _Alloc>) |
| 1103 | void |
| 1104 | str(const basic_string<_CharT, _Traits, _SAlloc>& __s) |
| 1105 | { _M_stringbuf.str(__s); } |
| 1106 | #endif |
| 1107 | |
| 1108 | void |
| 1109 | str(__string_type&& __s) |
| 1110 | { _M_stringbuf.str(std::move(__s)); } |
| 1111 | #endif |
| 1112 | |
| 1113 | #ifdef __cpp_lib_sstream_from_string_view |
| 1114 | template<typename _Tp> |
| 1115 | void |
| 1116 | str(const _Tp& __t) |
| 1117 | requires (is_convertible_v<const _Tp&, |
| 1118 | basic_string_view<_CharT, _Traits>>) |
| 1119 | { _M_stringbuf.str(__t); } |
| 1120 | #endif // C++26 |
| 1121 | }; |
| 1122 | |
| 1123 | |
| 1124 | // [27.7.4] Template class basic_stringstream |
| 1125 | /** |
| 1126 | * @brief Controlling input and output for std::string. |
| 1127 | * @ingroup io |
| 1128 | * |
| 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>. |
| 1133 | * |
| 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. |
| 1138 | */ |
| 1139 | template <typename _CharT, typename _Traits, typename _Alloc> |
| 1140 | class basic_stringstream : public basic_iostream<_CharT, _Traits> |
| 1141 | { |
| 1142 | public: |
| 1143 | // Types: |
| 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; |
| 1152 | |
| 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; |
| 1157 | |
| 1158 | private: |
| 1159 | __stringbuf_type _M_stringbuf; |
| 1160 | |
| 1161 | public: |
| 1162 | // Constructors/destructors |
| 1163 | |
| 1164 | /** |
| 1165 | * @brief Default constructor starts with an empty string buffer. |
| 1166 | * |
| 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. |
| 1169 | * |
| 1170 | * That's a lie. We initialize the base class with NULL, because the |
| 1171 | * string class does its own memory management. |
| 1172 | */ |
| 1173 | basic_stringstream() |
| 1174 | : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in) |
| 1175 | { this->init(std::__addressof(_M_stringbuf)); } |
| 1176 | |
| 1177 | /** |
| 1178 | * @brief Starts with an empty string buffer. |
| 1179 | * @param __m Whether the buffer can read, or write, or both. |
| 1180 | * |
| 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. |
| 1183 | * |
| 1184 | * That's a lie. We initialize the base class with NULL, because the |
| 1185 | * string class does its own memory management. |
| 1186 | */ |
| 1187 | explicit |
| 1188 | basic_stringstream(ios_base::openmode __m) |
| 1189 | : __iostream_type(), _M_stringbuf(__m) |
| 1190 | { this->init(std::__addressof(_M_stringbuf)); } |
| 1191 | |
| 1192 | /** |
| 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. |
| 1196 | * |
| 1197 | * Initializes @c sb using @a __str and @c __m, and passes @c &sb |
| 1198 | * to the base class initializer. |
| 1199 | * |
| 1200 | * That's a lie. We initialize the base class with NULL, because the |
| 1201 | * string class does its own memory management. |
| 1202 | */ |
| 1203 | explicit |
| 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)); } |
| 1208 | |
| 1209 | /** |
| 1210 | * @brief The destructor does nothing. |
| 1211 | * |
| 1212 | * The buffer is deallocated by the stringbuf object, not the |
| 1213 | * formatting stream. |
| 1214 | */ |
| 1215 | ~basic_stringstream() |
| 1216 | { } |
| 1217 | |
| 1218 | #if __cplusplus >= 201103L |
| 1219 | basic_stringstream(const basic_stringstream&) = delete; |
| 1220 | |
| 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)); } |
| 1225 | |
| 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)); } |
| 1231 | |
| 1232 | explicit |
| 1233 | basic_stringstream(__string_type&& __str, |
| 1234 | ios_base::openmode __mode = ios_base::in |
| 1235 | | ios_base::out) |
| 1236 | : __iostream_type(), _M_stringbuf(std::move(__str), __mode) |
| 1237 | { this->init(std::__addressof(_M_stringbuf)); } |
| 1238 | |
| 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) |
| 1243 | { } |
| 1244 | |
| 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)); } |
| 1251 | |
| 1252 | template<typename _SAlloc> |
| 1253 | explicit |
| 1254 | basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, |
| 1255 | ios_base::openmode __mode = ios_base::in |
| 1256 | | ios_base::out) |
| 1257 | : basic_stringstream(__str, __mode, allocator_type()) |
| 1258 | { } |
| 1259 | #endif // C++20 |
| 1260 | |
| 1261 | #ifdef __cpp_lib_sstream_from_string_view |
| 1262 | template <typename _Tp> |
| 1263 | explicit |
| 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{}) |
| 1269 | { } |
| 1270 | |
| 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) |
| 1276 | { } |
| 1277 | |
| 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)); } |
| 1284 | #endif // C++26 |
| 1285 | |
| 1286 | // 27.8.3.2 Assign and swap: |
| 1287 | |
| 1288 | basic_stringstream& |
| 1289 | operator=(const basic_stringstream&) = delete; |
| 1290 | |
| 1291 | basic_stringstream& |
| 1292 | operator=(basic_stringstream&& __rhs) |
| 1293 | { |
| 1294 | __iostream_type::operator=(std::move(__rhs)); |
| 1295 | _M_stringbuf = std::move(__rhs._M_stringbuf); |
| 1296 | return *this; |
| 1297 | } |
| 1298 | |
| 1299 | void |
| 1300 | swap(basic_stringstream& __rhs) |
| 1301 | { |
| 1302 | __iostream_type::swap(__rhs); |
| 1303 | _M_stringbuf.swap(__rhs._M_stringbuf); |
| 1304 | } |
| 1305 | #endif // C++11 |
| 1306 | |
| 1307 | // Members: |
| 1308 | /** |
| 1309 | * @brief Accessing the underlying buffer. |
| 1310 | * @return The current basic_stringbuf buffer. |
| 1311 | * |
| 1312 | * This hides both signatures of std::basic_ios::rdbuf(). |
| 1313 | */ |
| 1314 | _GLIBCXX_NODISCARD |
| 1315 | __stringbuf_type* |
| 1316 | rdbuf() const |
| 1317 | { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); } |
| 1318 | |
| 1319 | /** |
| 1320 | * @brief Copying out the string buffer. |
| 1321 | * @return @c rdbuf()->str() |
| 1322 | */ |
| 1323 | _GLIBCXX_NODISCARD |
| 1324 | __string_type |
| 1325 | str() const _GLIBCXX_LVAL_REF_QUAL |
| 1326 | { return _M_stringbuf.str(); } |
| 1327 | |
| 1328 | #if __cplusplus > 201703L |
| 1329 | #if _GLIBCXX_USE_CXX11_ABI |
| 1330 | #if __cpp_concepts |
| 1331 | // P0407 Allocator-aware basic_streambuf |
| 1332 | template<__allocator_like _SAlloc> |
| 1333 | _GLIBCXX_NODISCARD |
| 1334 | basic_string<_CharT, _Traits, _SAlloc> |
| 1335 | str(const _SAlloc& __sa) const |
| 1336 | { return _M_stringbuf.str(__sa); } |
| 1337 | #endif |
| 1338 | |
| 1339 | _GLIBCXX_NODISCARD |
| 1340 | __string_type |
| 1341 | str() && |
| 1342 | { return std::move(_M_stringbuf).str(); } |
| 1343 | #endif // cxx11 ABI |
| 1344 | |
| 1345 | _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 1346 | basic_string_view<char_type, traits_type> |
| 1347 | view() const noexcept |
| 1348 | { return _M_stringbuf.view(); } |
| 1349 | #endif // C++20 |
| 1350 | |
| 1351 | /** |
| 1352 | * @brief Setting a new buffer. |
| 1353 | * @param __s The string to use as a new sequence. |
| 1354 | * |
| 1355 | * Calls @c rdbuf()->str(s). |
| 1356 | */ |
| 1357 | void |
| 1358 | str(const __string_type& __s) |
| 1359 | { _M_stringbuf.str(__s); } |
| 1360 | |
| 1361 | #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI |
| 1362 | #if __cpp_concepts |
| 1363 | // P0407 Allocator-aware basic_streambuf |
| 1364 | template<__allocator_like _SAlloc> |
| 1365 | requires (!is_same_v<_SAlloc, _Alloc>) |
| 1366 | void |
| 1367 | str(const basic_string<_CharT, _Traits, _SAlloc>& __s) |
| 1368 | { _M_stringbuf.str(__s); } |
| 1369 | #endif |
| 1370 | |
| 1371 | void |
| 1372 | str(__string_type&& __s) |
| 1373 | { _M_stringbuf.str(std::move(__s)); } |
| 1374 | #endif |
| 1375 | |
| 1376 | #ifdef __cpp_lib_sstream_from_string_view |
| 1377 | template<typename _Tp> |
| 1378 | void |
| 1379 | str(const _Tp& __t) |
| 1380 | requires (is_convertible_v<const _Tp&, |
| 1381 | basic_string_view<_CharT, _Traits>>) |
| 1382 | { _M_stringbuf.str(__t); } |
| 1383 | #endif // C++26 |
| 1384 | }; |
| 1385 | |
| 1386 | #if __cplusplus >= 201103L |
| 1387 | /// Swap specialization for stringbufs. |
| 1388 | template <class _CharT, class _Traits, class _Allocator> |
| 1389 | inline void |
| 1390 | swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x, |
| 1391 | basic_stringbuf<_CharT, _Traits, _Allocator>& __y) |
| 1392 | noexcept(noexcept(__x.swap(__y))) |
| 1393 | { __x.swap(__y); } |
| 1394 | |
| 1395 | /// Swap specialization for istringstreams. |
| 1396 | template <class _CharT, class _Traits, class _Allocator> |
| 1397 | inline void |
| 1398 | swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x, |
| 1399 | basic_istringstream<_CharT, _Traits, _Allocator>& __y) |
| 1400 | { __x.swap(__y); } |
| 1401 | |
| 1402 | /// Swap specialization for ostringstreams. |
| 1403 | template <class _CharT, class _Traits, class _Allocator> |
| 1404 | inline void |
| 1405 | swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x, |
| 1406 | basic_ostringstream<_CharT, _Traits, _Allocator>& __y) |
| 1407 | { __x.swap(__y); } |
| 1408 | |
| 1409 | /// Swap specialization for stringstreams. |
| 1410 | template <class _CharT, class _Traits, class _Allocator> |
| 1411 | inline void |
| 1412 | swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x, |
| 1413 | basic_stringstream<_CharT, _Traits, _Allocator>& __y) |
| 1414 | { __x.swap(__y); } |
| 1415 | #endif // C++11 |
| 1416 | |
| 1417 | _GLIBCXX_END_NAMESPACE_CXX11 |
| 1418 | _GLIBCXX_END_NAMESPACE_VERSION |
| 1419 | } // namespace |
| 1420 | |
| 1421 | #undef _GLIBCXX_SSTREAM_ALWAYS_INLINE |
| 1422 | #undef _GLIBCXX_LVAL_REF_QUAL |
| 1423 | |
| 1424 | #include <bits/sstream.tcc> |
| 1425 | |
| 1426 | #endif /* _GLIBCXX_SSTREAM */ |
| 1427 | |