1 1.1 mrg // String based streams -*- C++ -*- 2 1.1 mrg 3 1.1.1.15 mrg // Copyright (C) 1997-2024 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.1.1.2 mrg /** @file include/sstream 26 1.1 mrg * This is a Standard C++ Library header. 27 1.1 mrg */ 28 1.1 mrg 29 1.1 mrg // 30 1.1 mrg // ISO C++ 14882: 27.7 String-based streams 31 1.1 mrg // 32 1.1 mrg 33 1.1 mrg #ifndef _GLIBCXX_SSTREAM 34 1.1 mrg #define _GLIBCXX_SSTREAM 1 35 1.1 mrg 36 1.1 mrg #pragma GCC system_header 37 1.1 mrg 38 1.1.1.15 mrg #include <bits/requires_hosted.h> // iostream 39 1.1.1.15 mrg 40 1.1 mrg #include <istream> 41 1.1 mrg #include <ostream> 42 1.1.1.13 mrg #include <bits/alloc_traits.h> // allocator_traits, __allocator_like 43 1.1.1.13 mrg 44 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 45 1.1.1.13 mrg # define _GLIBCXX_LVAL_REF_QUAL & 46 1.1.1.14 mrg # define _GLIBCXX_SSTREAM_ALWAYS_INLINE 47 1.1.1.13 mrg #else 48 1.1.1.13 mrg # define _GLIBCXX_LVAL_REF_QUAL 49 1.1.1.14 mrg // For symbols that are not exported from libstdc++.so for the COW string ABI. 50 1.1.1.14 mrg # define _GLIBCXX_SSTREAM_ALWAYS_INLINE [[__gnu__::__always_inline__]] 51 1.1.1.13 mrg #endif 52 1.1 mrg 53 1.1.1.14 mrg 54 1.1.1.14 mrg 55 1.1.1.2 mrg namespace std _GLIBCXX_VISIBILITY(default) 56 1.1.1.2 mrg { 57 1.1.1.2 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 58 1.1.1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_CXX11 59 1.1 mrg 60 1.1 mrg // [27.7.1] template class basic_stringbuf 61 1.1 mrg /** 62 1.1 mrg * @brief The actual work of input and output (for std::string). 63 1.1 mrg * @ingroup io 64 1.1 mrg * 65 1.1.1.2 mrg * @tparam _CharT Type of character stream. 66 1.1.1.2 mrg * @tparam _Traits Traits for character type, defaults to 67 1.1.1.2 mrg * char_traits<_CharT>. 68 1.1.1.2 mrg * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 69 1.1.1.2 mrg * 70 1.1 mrg * This class associates either or both of its input and output sequences 71 1.1 mrg * with a sequence of characters, which can be initialized from, or made 72 1.1 mrg * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.) 73 1.1 mrg * 74 1.1 mrg * For this class, open modes (of type @c ios_base::openmode) have 75 1.1 mrg * @c in set if the input sequence can be read, and @c out set if the 76 1.1 mrg * output sequence can be written. 77 1.1 mrg */ 78 1.1 mrg template<typename _CharT, typename _Traits, typename _Alloc> 79 1.1 mrg class basic_stringbuf : public basic_streambuf<_CharT, _Traits> 80 1.1 mrg { 81 1.1.1.3 mrg struct __xfer_bufptrs; 82 1.1.1.13 mrg 83 1.1.1.13 mrg #if __cplusplus >= 201103L 84 1.1.1.13 mrg using allocator_traits = std::allocator_traits<_Alloc>; 85 1.1.1.13 mrg using _Noexcept_swap 86 1.1.1.13 mrg = __or_<typename allocator_traits::propagate_on_container_swap, 87 1.1.1.13 mrg typename allocator_traits::is_always_equal>; 88 1.1.1.13 mrg #endif 89 1.1.1.13 mrg 90 1.1 mrg public: 91 1.1 mrg // Types: 92 1.1 mrg typedef _CharT char_type; 93 1.1 mrg typedef _Traits traits_type; 94 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 95 1.1 mrg // 251. basic_stringbuf missing allocator_type 96 1.1 mrg typedef _Alloc allocator_type; 97 1.1 mrg typedef typename traits_type::int_type int_type; 98 1.1 mrg typedef typename traits_type::pos_type pos_type; 99 1.1 mrg typedef typename traits_type::off_type off_type; 100 1.1 mrg 101 1.1 mrg typedef basic_streambuf<char_type, traits_type> __streambuf_type; 102 1.1 mrg typedef basic_string<char_type, _Traits, _Alloc> __string_type; 103 1.1 mrg typedef typename __string_type::size_type __size_type; 104 1.1 mrg 105 1.1 mrg protected: 106 1.1 mrg /// Place to stash in || out || in | out settings for current stringbuf. 107 1.1 mrg ios_base::openmode _M_mode; 108 1.1 mrg 109 1.1 mrg // Data Members: 110 1.1 mrg __string_type _M_string; 111 1.1 mrg 112 1.1 mrg public: 113 1.1 mrg // Constructors: 114 1.1.1.11 mrg 115 1.1.1.11 mrg /** 116 1.1.1.11 mrg * @brief Starts with an empty string buffer. 117 1.1.1.11 mrg * 118 1.1.1.11 mrg * The default constructor initializes the parent class using its 119 1.1.1.11 mrg * own default ctor. 120 1.1.1.11 mrg */ 121 1.1.1.11 mrg basic_stringbuf() 122 1.1.1.11 mrg : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string() 123 1.1.1.11 mrg { } 124 1.1.1.11 mrg 125 1.1 mrg /** 126 1.1 mrg * @brief Starts with an empty string buffer. 127 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 128 1.1 mrg * 129 1.1 mrg * The default constructor initializes the parent class using its 130 1.1 mrg * own default ctor. 131 1.1 mrg */ 132 1.1 mrg explicit 133 1.1.1.11 mrg basic_stringbuf(ios_base::openmode __mode) 134 1.1 mrg : __streambuf_type(), _M_mode(__mode), _M_string() 135 1.1 mrg { } 136 1.1 mrg 137 1.1 mrg /** 138 1.1 mrg * @brief Starts with an existing string buffer. 139 1.1.1.2 mrg * @param __str A string to copy as a starting buffer. 140 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 141 1.1 mrg * 142 1.1 mrg * This constructor initializes the parent class using its 143 1.1 mrg * own default ctor. 144 1.1 mrg */ 145 1.1 mrg explicit 146 1.1 mrg basic_stringbuf(const __string_type& __str, 147 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 148 1.1.1.10 mrg : __streambuf_type(), _M_mode(), 149 1.1.1.10 mrg _M_string(__str.data(), __str.size(), __str.get_allocator()) 150 1.1 mrg { _M_stringbuf_init(__mode); } 151 1.1 mrg 152 1.1.1.3 mrg #if __cplusplus >= 201103L 153 1.1.1.3 mrg basic_stringbuf(const basic_stringbuf&) = delete; 154 1.1.1.3 mrg 155 1.1.1.3 mrg basic_stringbuf(basic_stringbuf&& __rhs) 156 1.1.1.3 mrg : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this)) 157 1.1.1.3 mrg { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); } 158 1.1.1.3 mrg 159 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 160 1.1.1.13 mrg explicit 161 1.1.1.13 mrg basic_stringbuf(const allocator_type& __a) 162 1.1.1.13 mrg : basic_stringbuf(ios_base::in | std::ios_base::out, __a) 163 1.1.1.13 mrg { } 164 1.1.1.13 mrg 165 1.1.1.13 mrg basic_stringbuf(ios_base::openmode __mode, 166 1.1.1.13 mrg const allocator_type& __a) 167 1.1.1.13 mrg : __streambuf_type(), _M_mode(__mode), _M_string(__a) 168 1.1.1.13 mrg { } 169 1.1.1.13 mrg 170 1.1.1.13 mrg explicit 171 1.1.1.13 mrg basic_stringbuf(__string_type&& __s, 172 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in 173 1.1.1.13 mrg | ios_base::out) 174 1.1.1.13 mrg : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s)) 175 1.1.1.13 mrg { _M_stringbuf_init(__mode); } 176 1.1.1.13 mrg 177 1.1.1.13 mrg template<typename _SAlloc> 178 1.1.1.13 mrg basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s, 179 1.1.1.13 mrg const allocator_type& __a) 180 1.1.1.13 mrg : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a) 181 1.1.1.13 mrg { } 182 1.1.1.13 mrg 183 1.1.1.13 mrg template<typename _SAlloc> 184 1.1.1.13 mrg basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s, 185 1.1.1.13 mrg ios_base::openmode __mode, 186 1.1.1.13 mrg const allocator_type& __a) 187 1.1.1.13 mrg : __streambuf_type(), _M_mode(__mode), 188 1.1.1.13 mrg _M_string(__s.data(), __s.size(), __a) 189 1.1.1.13 mrg { _M_stringbuf_init(__mode); } 190 1.1.1.13 mrg 191 1.1.1.13 mrg template<typename _SAlloc> 192 1.1.1.13 mrg explicit 193 1.1.1.13 mrg basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s, 194 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in 195 1.1.1.13 mrg | ios_base::out) 196 1.1.1.13 mrg : basic_stringbuf(__s, __mode, allocator_type{}) 197 1.1.1.13 mrg { } 198 1.1.1.13 mrg 199 1.1.1.13 mrg basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a) 200 1.1.1.13 mrg : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this)) 201 1.1.1.13 mrg { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); } 202 1.1.1.13 mrg 203 1.1.1.13 mrg allocator_type get_allocator() const noexcept 204 1.1.1.13 mrg { return _M_string.get_allocator(); } 205 1.1.1.13 mrg #endif // C++20 206 1.1.1.13 mrg 207 1.1.1.3 mrg // 27.8.2.2 Assign and swap: 208 1.1.1.3 mrg 209 1.1.1.3 mrg basic_stringbuf& 210 1.1.1.3 mrg operator=(const basic_stringbuf&) = delete; 211 1.1.1.3 mrg 212 1.1.1.3 mrg basic_stringbuf& 213 1.1.1.3 mrg operator=(basic_stringbuf&& __rhs) 214 1.1.1.3 mrg { 215 1.1.1.3 mrg __xfer_bufptrs __st{__rhs, this}; 216 1.1.1.3 mrg const __streambuf_type& __base = __rhs; 217 1.1.1.3 mrg __streambuf_type::operator=(__base); 218 1.1.1.3 mrg this->pubimbue(__rhs.getloc()); 219 1.1.1.3 mrg _M_mode = __rhs._M_mode; 220 1.1.1.3 mrg _M_string = std::move(__rhs._M_string); 221 1.1.1.3 mrg __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); 222 1.1.1.3 mrg return *this; 223 1.1.1.3 mrg } 224 1.1.1.3 mrg 225 1.1.1.3 mrg void 226 1.1.1.13 mrg swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value) 227 1.1.1.3 mrg { 228 1.1.1.3 mrg __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)}; 229 1.1.1.3 mrg __xfer_bufptrs __r_st{__rhs, this}; 230 1.1.1.3 mrg __streambuf_type& __base = __rhs; 231 1.1.1.3 mrg __streambuf_type::swap(__base); 232 1.1.1.3 mrg __rhs.pubimbue(this->pubimbue(__rhs.getloc())); 233 1.1.1.3 mrg std::swap(_M_mode, __rhs._M_mode); 234 1.1.1.13 mrg std::swap(_M_string, __rhs._M_string); // XXX not exception safe 235 1.1.1.3 mrg } 236 1.1.1.13 mrg #endif // C++11 237 1.1.1.13 mrg 238 1.1.1.13 mrg // Getters and setters: 239 1.1.1.3 mrg 240 1.1 mrg /** 241 1.1 mrg * @brief Copying out the string buffer. 242 1.1 mrg * @return A copy of one of the underlying sequences. 243 1.1 mrg * 244 1.1 mrg * <em>If the buffer is only created in input mode, the underlying 245 1.1 mrg * character sequence is equal to the input sequence; otherwise, it 246 1.1 mrg * is equal to the output sequence.</em> [27.7.1.2]/1 247 1.1 mrg */ 248 1.1 mrg __string_type 249 1.1.1.13 mrg str() const _GLIBCXX_LVAL_REF_QUAL 250 1.1 mrg { 251 1.1.1.10 mrg __string_type __ret(_M_string.get_allocator()); 252 1.1.1.13 mrg if (char_type* __hi = _M_high_mark()) 253 1.1.1.13 mrg __ret.assign(this->pbase(), __hi); 254 1.1 mrg else 255 1.1 mrg __ret = _M_string; 256 1.1 mrg return __ret; 257 1.1 mrg } 258 1.1 mrg 259 1.1.1.14 mrg #if __cplusplus > 201703L 260 1.1.1.14 mrg #if _GLIBCXX_USE_CXX11_ABI 261 1.1.1.13 mrg #if __cpp_concepts 262 1.1.1.13 mrg template<__allocator_like _SAlloc> 263 1.1.1.13 mrg basic_string<_CharT, _Traits, _SAlloc> 264 1.1.1.13 mrg str(const _SAlloc& __sa) const 265 1.1.1.13 mrg { 266 1.1.1.13 mrg auto __sv = view(); 267 1.1.1.13 mrg return { __sv.data(), __sv.size(), __sa }; 268 1.1.1.13 mrg } 269 1.1.1.13 mrg #endif 270 1.1.1.13 mrg 271 1.1.1.13 mrg __string_type 272 1.1.1.13 mrg str() && 273 1.1.1.13 mrg { 274 1.1.1.13 mrg if (char_type* __hi = _M_high_mark()) 275 1.1.1.13 mrg { 276 1.1.1.13 mrg // Set length to end of character sequence and add null terminator. 277 1.1.1.13 mrg _M_string._M_set_length(_M_high_mark() - this->pbase()); 278 1.1.1.13 mrg } 279 1.1.1.13 mrg auto __str = std::move(_M_string); 280 1.1.1.13 mrg _M_string.clear(); 281 1.1.1.13 mrg _M_sync(_M_string.data(), 0, 0); 282 1.1.1.13 mrg return __str; 283 1.1.1.13 mrg } 284 1.1.1.14 mrg #endif // cxx11 ABI 285 1.1.1.13 mrg 286 1.1.1.14 mrg _GLIBCXX_SSTREAM_ALWAYS_INLINE 287 1.1.1.13 mrg basic_string_view<char_type, traits_type> 288 1.1.1.13 mrg view() const noexcept 289 1.1.1.13 mrg { 290 1.1.1.13 mrg if (char_type* __hi = _M_high_mark()) 291 1.1.1.13 mrg return { this->pbase(), __hi }; 292 1.1.1.13 mrg else 293 1.1.1.13 mrg return _M_string; 294 1.1.1.13 mrg } 295 1.1.1.13 mrg #endif // C++20 296 1.1.1.13 mrg 297 1.1 mrg /** 298 1.1 mrg * @brief Setting a new buffer. 299 1.1.1.2 mrg * @param __s The string to use as a new sequence. 300 1.1 mrg * 301 1.1 mrg * Deallocates any previous stored sequence, then copies @a s to 302 1.1 mrg * use as a new one. 303 1.1 mrg */ 304 1.1 mrg void 305 1.1 mrg str(const __string_type& __s) 306 1.1 mrg { 307 1.1.1.3 mrg // Cannot use _M_string = __s, since v3 strings are COW 308 1.1.1.3 mrg // (not always true now but assign() always works). 309 1.1 mrg _M_string.assign(__s.data(), __s.size()); 310 1.1 mrg _M_stringbuf_init(_M_mode); 311 1.1 mrg } 312 1.1 mrg 313 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 314 1.1.1.13 mrg #if __cpp_concepts 315 1.1.1.13 mrg template<__allocator_like _SAlloc> 316 1.1.1.13 mrg requires (!is_same_v<_SAlloc, _Alloc>) 317 1.1.1.13 mrg void 318 1.1.1.13 mrg str(const basic_string<_CharT, _Traits, _SAlloc>& __s) 319 1.1.1.13 mrg { 320 1.1.1.13 mrg _M_string.assign(__s.data(), __s.size()); 321 1.1.1.13 mrg _M_stringbuf_init(_M_mode); 322 1.1.1.13 mrg } 323 1.1.1.13 mrg #endif 324 1.1.1.13 mrg 325 1.1.1.13 mrg void 326 1.1.1.13 mrg str(__string_type&& __s) 327 1.1.1.13 mrg { 328 1.1.1.13 mrg _M_string = std::move(__s); 329 1.1.1.13 mrg _M_stringbuf_init(_M_mode); 330 1.1.1.13 mrg } 331 1.1.1.13 mrg #endif 332 1.1.1.13 mrg 333 1.1 mrg protected: 334 1.1 mrg // Common initialization code goes here. 335 1.1 mrg void 336 1.1 mrg _M_stringbuf_init(ios_base::openmode __mode) 337 1.1 mrg { 338 1.1 mrg _M_mode = __mode; 339 1.1 mrg __size_type __len = 0; 340 1.1 mrg if (_M_mode & (ios_base::ate | ios_base::app)) 341 1.1 mrg __len = _M_string.size(); 342 1.1 mrg _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len); 343 1.1 mrg } 344 1.1 mrg 345 1.1 mrg virtual streamsize 346 1.1 mrg showmanyc() 347 1.1.1.7 mrg { 348 1.1 mrg streamsize __ret = -1; 349 1.1 mrg if (_M_mode & ios_base::in) 350 1.1 mrg { 351 1.1 mrg _M_update_egptr(); 352 1.1 mrg __ret = this->egptr() - this->gptr(); 353 1.1 mrg } 354 1.1 mrg return __ret; 355 1.1 mrg } 356 1.1 mrg 357 1.1 mrg virtual int_type 358 1.1 mrg underflow(); 359 1.1 mrg 360 1.1 mrg virtual int_type 361 1.1 mrg pbackfail(int_type __c = traits_type::eof()); 362 1.1 mrg 363 1.1 mrg virtual int_type 364 1.1 mrg overflow(int_type __c = traits_type::eof()); 365 1.1 mrg 366 1.1 mrg /** 367 1.1 mrg * @brief Manipulates the buffer. 368 1.1.1.2 mrg * @param __s Pointer to a buffer area. 369 1.1.1.2 mrg * @param __n Size of @a __s. 370 1.1 mrg * @return @c this 371 1.1 mrg * 372 1.1.1.2 mrg * If no buffer has already been created, and both @a __s and @a __n are 373 1.1.1.2 mrg * non-zero, then @c __s is used as a buffer; see 374 1.1.1.3 mrg * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering 375 1.1 mrg * for more. 376 1.1 mrg */ 377 1.1 mrg virtual __streambuf_type* 378 1.1 mrg setbuf(char_type* __s, streamsize __n) 379 1.1 mrg { 380 1.1 mrg if (__s && __n >= 0) 381 1.1 mrg { 382 1.1 mrg // This is implementation-defined behavior, and assumes 383 1.1 mrg // that an external char_type array of length __n exists 384 1.1 mrg // and has been pre-allocated. If this is not the case, 385 1.1 mrg // things will quickly blow up. 386 1.1.1.7 mrg 387 1.1 mrg // Step 1: Destroy the current internal array. 388 1.1 mrg _M_string.clear(); 389 1.1.1.7 mrg 390 1.1 mrg // Step 2: Use the external array. 391 1.1 mrg _M_sync(__s, __n, 0); 392 1.1 mrg } 393 1.1 mrg return this; 394 1.1 mrg } 395 1.1 mrg 396 1.1 mrg virtual pos_type 397 1.1 mrg seekoff(off_type __off, ios_base::seekdir __way, 398 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out); 399 1.1 mrg 400 1.1 mrg virtual pos_type 401 1.1 mrg seekpos(pos_type __sp, 402 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out); 403 1.1 mrg 404 1.1 mrg // Internal function for correctly updating the internal buffer 405 1.1 mrg // for a particular _M_string, due to initialization or re-sizing 406 1.1 mrg // of an existing _M_string. 407 1.1 mrg void 408 1.1 mrg _M_sync(char_type* __base, __size_type __i, __size_type __o); 409 1.1 mrg 410 1.1 mrg // Internal function for correctly updating egptr() to the actual 411 1.1 mrg // string end. 412 1.1 mrg void 413 1.1 mrg _M_update_egptr() 414 1.1 mrg { 415 1.1.1.13 mrg if (char_type* __pptr = this->pptr()) 416 1.1 mrg { 417 1.1.1.13 mrg char_type* __egptr = this->egptr(); 418 1.1.1.13 mrg if (!__egptr || __pptr > __egptr) 419 1.1.1.13 mrg { 420 1.1.1.13 mrg if (_M_mode & ios_base::in) 421 1.1.1.13 mrg this->setg(this->eback(), this->gptr(), __pptr); 422 1.1.1.13 mrg else 423 1.1.1.13 mrg this->setg(__pptr, __pptr, __pptr); 424 1.1.1.13 mrg } 425 1.1 mrg } 426 1.1 mrg } 427 1.1.1.2 mrg 428 1.1.1.2 mrg // Works around the issue with pbump, part of the protected 429 1.1.1.2 mrg // interface of basic_streambuf, taking just an int. 430 1.1.1.2 mrg void 431 1.1.1.2 mrg _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off); 432 1.1.1.3 mrg 433 1.1.1.3 mrg private: 434 1.1.1.13 mrg // Return a pointer to the end of the underlying character sequence. 435 1.1.1.13 mrg // This might not be the same character as _M_string.end() because 436 1.1.1.13 mrg // basic_stringbuf::overflow might have written to unused capacity 437 1.1.1.13 mrg // in _M_string without updating its length. 438 1.1.1.13 mrg __attribute__((__always_inline__)) 439 1.1.1.13 mrg char_type* 440 1.1.1.13 mrg _M_high_mark() const _GLIBCXX_NOEXCEPT 441 1.1.1.13 mrg { 442 1.1.1.13 mrg if (char_type* __pptr = this->pptr()) 443 1.1.1.13 mrg { 444 1.1.1.13 mrg char_type* __egptr = this->egptr(); 445 1.1.1.13 mrg if (!__egptr || __pptr > __egptr) 446 1.1.1.13 mrg return __pptr; // Underlying sequence is [pbase, pptr). 447 1.1.1.13 mrg else 448 1.1.1.13 mrg return __egptr; // Underlying sequence is [pbase, egptr). 449 1.1.1.13 mrg } 450 1.1.1.13 mrg return 0; // Underlying character sequence is just _M_string. 451 1.1.1.13 mrg } 452 1.1.1.13 mrg 453 1.1.1.3 mrg #if __cplusplus >= 201103L 454 1.1.1.3 mrg #if _GLIBCXX_USE_CXX11_ABI 455 1.1.1.3 mrg // This type captures the state of the gptr / pptr pointers as offsets 456 1.1.1.3 mrg // so they can be restored in another object after moving the string. 457 1.1.1.3 mrg struct __xfer_bufptrs 458 1.1.1.3 mrg { 459 1.1.1.3 mrg __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to) 460 1.1.1.3 mrg : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1} 461 1.1.1.3 mrg { 462 1.1.1.6 mrg const _CharT* const __str = __from._M_string.data(); 463 1.1.1.6 mrg const _CharT* __end = nullptr; 464 1.1.1.3 mrg if (__from.eback()) 465 1.1.1.3 mrg { 466 1.1.1.6 mrg _M_goff[0] = __from.eback() - __str; 467 1.1.1.6 mrg _M_goff[1] = __from.gptr() - __str; 468 1.1.1.6 mrg _M_goff[2] = __from.egptr() - __str; 469 1.1.1.6 mrg __end = __from.egptr(); 470 1.1.1.3 mrg } 471 1.1.1.3 mrg if (__from.pbase()) 472 1.1.1.3 mrg { 473 1.1.1.3 mrg _M_poff[0] = __from.pbase() - __str; 474 1.1.1.3 mrg _M_poff[1] = __from.pptr() - __from.pbase(); 475 1.1.1.3 mrg _M_poff[2] = __from.epptr() - __str; 476 1.1.1.13 mrg if (!__end || __from.pptr() > __end) 477 1.1.1.6 mrg __end = __from.pptr(); 478 1.1.1.6 mrg } 479 1.1.1.6 mrg 480 1.1.1.6 mrg // Set _M_string length to the greater of the get and put areas. 481 1.1.1.6 mrg if (__end) 482 1.1.1.6 mrg { 483 1.1.1.6 mrg // The const_cast avoids changing this constructor's signature, 484 1.1.1.6 mrg // because it is exported from the dynamic library. 485 1.1.1.6 mrg auto& __mut_from = const_cast<basic_stringbuf&>(__from); 486 1.1.1.6 mrg __mut_from._M_string._M_length(__end - __str); 487 1.1.1.3 mrg } 488 1.1.1.3 mrg } 489 1.1.1.3 mrg 490 1.1.1.3 mrg ~__xfer_bufptrs() 491 1.1.1.3 mrg { 492 1.1.1.3 mrg char_type* __str = const_cast<char_type*>(_M_to->_M_string.data()); 493 1.1.1.3 mrg if (_M_goff[0] != -1) 494 1.1.1.3 mrg _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]); 495 1.1.1.3 mrg if (_M_poff[0] != -1) 496 1.1.1.3 mrg _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]); 497 1.1.1.3 mrg } 498 1.1.1.3 mrg 499 1.1.1.3 mrg basic_stringbuf* _M_to; 500 1.1.1.3 mrg off_type _M_goff[3]; 501 1.1.1.3 mrg off_type _M_poff[3]; 502 1.1.1.3 mrg }; 503 1.1.1.3 mrg #else 504 1.1.1.3 mrg // This type does nothing when using Copy-On-Write strings. 505 1.1.1.3 mrg struct __xfer_bufptrs 506 1.1.1.3 mrg { 507 1.1.1.3 mrg __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { } 508 1.1.1.3 mrg }; 509 1.1.1.3 mrg #endif 510 1.1.1.3 mrg 511 1.1.1.3 mrg // The move constructor initializes an __xfer_bufptrs temporary then 512 1.1.1.3 mrg // delegates to this constructor to performs moves during its lifetime. 513 1.1.1.3 mrg basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&) 514 1.1.1.3 mrg : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)), 515 1.1.1.3 mrg _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string)) 516 1.1.1.3 mrg { } 517 1.1.1.13 mrg 518 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 519 1.1.1.13 mrg // The move constructor initializes an __xfer_bufptrs temporary then 520 1.1.1.13 mrg // delegates to this constructor to performs moves during its lifetime. 521 1.1.1.13 mrg basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a, 522 1.1.1.13 mrg __xfer_bufptrs&&) 523 1.1.1.13 mrg : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)), 524 1.1.1.13 mrg _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a) 525 1.1.1.13 mrg { } 526 1.1.1.3 mrg #endif 527 1.1.1.13 mrg #endif // C++11 528 1.1 mrg }; 529 1.1 mrg 530 1.1 mrg 531 1.1 mrg // [27.7.2] Template class basic_istringstream 532 1.1 mrg /** 533 1.1 mrg * @brief Controlling input for std::string. 534 1.1 mrg * @ingroup io 535 1.1 mrg * 536 1.1.1.2 mrg * @tparam _CharT Type of character stream. 537 1.1.1.2 mrg * @tparam _Traits Traits for character type, defaults to 538 1.1.1.2 mrg * char_traits<_CharT>. 539 1.1.1.2 mrg * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 540 1.1.1.2 mrg * 541 1.1 mrg * This class supports reading from objects of type std::basic_string, 542 1.1 mrg * using the inherited functions from std::basic_istream. To control 543 1.1 mrg * the associated sequence, an instance of std::basic_stringbuf is used, 544 1.1 mrg * which this page refers to as @c sb. 545 1.1 mrg */ 546 1.1 mrg template<typename _CharT, typename _Traits, typename _Alloc> 547 1.1 mrg class basic_istringstream : public basic_istream<_CharT, _Traits> 548 1.1 mrg { 549 1.1 mrg public: 550 1.1 mrg // Types: 551 1.1 mrg typedef _CharT char_type; 552 1.1 mrg typedef _Traits traits_type; 553 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 554 1.1 mrg // 251. basic_stringbuf missing allocator_type 555 1.1 mrg typedef _Alloc allocator_type; 556 1.1 mrg typedef typename traits_type::int_type int_type; 557 1.1 mrg typedef typename traits_type::pos_type pos_type; 558 1.1 mrg typedef typename traits_type::off_type off_type; 559 1.1 mrg 560 1.1 mrg // Non-standard types: 561 1.1 mrg typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 562 1.1 mrg typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 563 1.1 mrg typedef basic_istream<char_type, traits_type> __istream_type; 564 1.1 mrg 565 1.1 mrg private: 566 1.1 mrg __stringbuf_type _M_stringbuf; 567 1.1 mrg 568 1.1 mrg public: 569 1.1 mrg // Constructors: 570 1.1.1.11 mrg 571 1.1 mrg /** 572 1.1 mrg * @brief Default constructor starts with an empty string buffer. 573 1.1.1.11 mrg * 574 1.1.1.11 mrg * Initializes @c sb using @c in, and passes @c &sb to the base 575 1.1.1.11 mrg * class initializer. Does not allocate any buffer. 576 1.1.1.11 mrg * 577 1.1.1.11 mrg * That's a lie. We initialize the base class with NULL, because the 578 1.1.1.11 mrg * string class does its own memory management. 579 1.1.1.11 mrg */ 580 1.1.1.11 mrg basic_istringstream() 581 1.1.1.11 mrg : __istream_type(), _M_stringbuf(ios_base::in) 582 1.1.1.11 mrg { this->init(&_M_stringbuf); } 583 1.1.1.11 mrg 584 1.1.1.11 mrg /** 585 1.1.1.11 mrg * @brief Starts with an empty string buffer. 586 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 587 1.1 mrg * 588 1.1.1.2 mrg * @c ios_base::in is automatically included in @a __mode. 589 1.1 mrg * 590 1.1.1.2 mrg * Initializes @c sb using @c __mode|in, and passes @c &sb to the base 591 1.1 mrg * class initializer. Does not allocate any buffer. 592 1.1 mrg * 593 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 594 1.1 mrg * string class does its own memory management. 595 1.1 mrg */ 596 1.1 mrg explicit 597 1.1.1.11 mrg basic_istringstream(ios_base::openmode __mode) 598 1.1 mrg : __istream_type(), _M_stringbuf(__mode | ios_base::in) 599 1.1 mrg { this->init(&_M_stringbuf); } 600 1.1 mrg 601 1.1 mrg /** 602 1.1 mrg * @brief Starts with an existing string buffer. 603 1.1.1.2 mrg * @param __str A string to copy as a starting buffer. 604 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 605 1.1 mrg * 606 1.1 mrg * @c ios_base::in is automatically included in @a mode. 607 1.1 mrg * 608 1.1 mrg * Initializes @c sb using @a str and @c mode|in, and passes @c &sb 609 1.1 mrg * to the base class initializer. 610 1.1 mrg * 611 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 612 1.1 mrg * string class does its own memory management. 613 1.1 mrg */ 614 1.1 mrg explicit 615 1.1 mrg basic_istringstream(const __string_type& __str, 616 1.1 mrg ios_base::openmode __mode = ios_base::in) 617 1.1 mrg : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in) 618 1.1 mrg { this->init(&_M_stringbuf); } 619 1.1 mrg 620 1.1 mrg /** 621 1.1 mrg * @brief The destructor does nothing. 622 1.1 mrg * 623 1.1 mrg * The buffer is deallocated by the stringbuf object, not the 624 1.1 mrg * formatting stream. 625 1.1 mrg */ 626 1.1 mrg ~basic_istringstream() 627 1.1 mrg { } 628 1.1 mrg 629 1.1.1.3 mrg #if __cplusplus >= 201103L 630 1.1.1.3 mrg basic_istringstream(const basic_istringstream&) = delete; 631 1.1.1.3 mrg 632 1.1.1.3 mrg basic_istringstream(basic_istringstream&& __rhs) 633 1.1.1.3 mrg : __istream_type(std::move(__rhs)), 634 1.1.1.3 mrg _M_stringbuf(std::move(__rhs._M_stringbuf)) 635 1.1.1.3 mrg { __istream_type::set_rdbuf(&_M_stringbuf); } 636 1.1.1.3 mrg 637 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 638 1.1.1.13 mrg basic_istringstream(ios_base::openmode __mode, const allocator_type& __a) 639 1.1.1.13 mrg : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a) 640 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 641 1.1.1.13 mrg 642 1.1.1.13 mrg explicit 643 1.1.1.13 mrg basic_istringstream(__string_type&& __str, 644 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in) 645 1.1.1.13 mrg : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in) 646 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 647 1.1.1.13 mrg 648 1.1.1.13 mrg template<typename _SAlloc> 649 1.1.1.13 mrg basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 650 1.1.1.13 mrg const allocator_type& __a) 651 1.1.1.13 mrg : basic_istringstream(__str, ios_base::in, __a) 652 1.1.1.13 mrg { } 653 1.1.1.13 mrg 654 1.1.1.13 mrg template<typename _SAlloc> 655 1.1.1.13 mrg basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 656 1.1.1.13 mrg ios_base::openmode __mode, 657 1.1.1.13 mrg const allocator_type& __a) 658 1.1.1.13 mrg : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a) 659 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 660 1.1.1.13 mrg 661 1.1.1.13 mrg template<typename _SAlloc> 662 1.1.1.13 mrg explicit 663 1.1.1.13 mrg basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 664 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in) 665 1.1.1.13 mrg : basic_istringstream(__str, __mode, allocator_type()) 666 1.1.1.13 mrg { } 667 1.1.1.13 mrg #endif // C++20 668 1.1.1.13 mrg 669 1.1.1.3 mrg // 27.8.3.2 Assign and swap: 670 1.1.1.3 mrg 671 1.1.1.3 mrg basic_istringstream& 672 1.1.1.3 mrg operator=(const basic_istringstream&) = delete; 673 1.1.1.3 mrg 674 1.1.1.3 mrg basic_istringstream& 675 1.1.1.3 mrg operator=(basic_istringstream&& __rhs) 676 1.1.1.3 mrg { 677 1.1.1.3 mrg __istream_type::operator=(std::move(__rhs)); 678 1.1.1.3 mrg _M_stringbuf = std::move(__rhs._M_stringbuf); 679 1.1.1.3 mrg return *this; 680 1.1.1.3 mrg } 681 1.1.1.3 mrg 682 1.1.1.3 mrg void 683 1.1.1.3 mrg swap(basic_istringstream& __rhs) 684 1.1.1.3 mrg { 685 1.1.1.3 mrg __istream_type::swap(__rhs); 686 1.1.1.3 mrg _M_stringbuf.swap(__rhs._M_stringbuf); 687 1.1.1.3 mrg } 688 1.1.1.13 mrg #endif // C++11 689 1.1.1.3 mrg 690 1.1 mrg // Members: 691 1.1 mrg /** 692 1.1 mrg * @brief Accessing the underlying buffer. 693 1.1 mrg * @return The current basic_stringbuf buffer. 694 1.1 mrg * 695 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 696 1.1 mrg */ 697 1.1 mrg __stringbuf_type* 698 1.1 mrg rdbuf() const 699 1.1 mrg { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 700 1.1 mrg 701 1.1 mrg /** 702 1.1 mrg * @brief Copying out the string buffer. 703 1.1 mrg * @return @c rdbuf()->str() 704 1.1 mrg */ 705 1.1 mrg __string_type 706 1.1.1.13 mrg str() const _GLIBCXX_LVAL_REF_QUAL 707 1.1 mrg { return _M_stringbuf.str(); } 708 1.1 mrg 709 1.1.1.14 mrg #if __cplusplus > 201703L 710 1.1.1.14 mrg #if _GLIBCXX_USE_CXX11_ABI 711 1.1.1.13 mrg #if __cpp_concepts 712 1.1.1.13 mrg template<__allocator_like _SAlloc> 713 1.1.1.13 mrg basic_string<_CharT, _Traits, _SAlloc> 714 1.1.1.13 mrg str(const _SAlloc& __sa) const 715 1.1.1.13 mrg { return _M_stringbuf.str(__sa); } 716 1.1.1.13 mrg #endif 717 1.1.1.13 mrg 718 1.1.1.13 mrg __string_type 719 1.1.1.13 mrg str() && 720 1.1.1.13 mrg { return std::move(_M_stringbuf).str(); } 721 1.1.1.14 mrg #endif // cxx11 ABI 722 1.1.1.13 mrg 723 1.1.1.14 mrg _GLIBCXX_SSTREAM_ALWAYS_INLINE 724 1.1.1.13 mrg basic_string_view<char_type, traits_type> 725 1.1.1.13 mrg view() const noexcept 726 1.1.1.13 mrg { return _M_stringbuf.view(); } 727 1.1.1.14 mrg #endif // C++20 728 1.1.1.13 mrg 729 1.1 mrg /** 730 1.1 mrg * @brief Setting a new buffer. 731 1.1.1.2 mrg * @param __s The string to use as a new sequence. 732 1.1 mrg * 733 1.1 mrg * Calls @c rdbuf()->str(s). 734 1.1 mrg */ 735 1.1 mrg void 736 1.1 mrg str(const __string_type& __s) 737 1.1 mrg { _M_stringbuf.str(__s); } 738 1.1.1.13 mrg 739 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 740 1.1.1.13 mrg #if __cpp_concepts 741 1.1.1.13 mrg template<__allocator_like _SAlloc> 742 1.1.1.13 mrg requires (!is_same_v<_SAlloc, _Alloc>) 743 1.1.1.13 mrg void 744 1.1.1.13 mrg str(const basic_string<_CharT, _Traits, _SAlloc>& __s) 745 1.1.1.13 mrg { _M_stringbuf.str(__s); } 746 1.1.1.13 mrg #endif 747 1.1.1.13 mrg 748 1.1.1.13 mrg void 749 1.1.1.13 mrg str(__string_type&& __s) 750 1.1.1.13 mrg { _M_stringbuf.str(std::move(__s)); } 751 1.1.1.13 mrg #endif 752 1.1 mrg }; 753 1.1 mrg 754 1.1 mrg 755 1.1 mrg // [27.7.3] Template class basic_ostringstream 756 1.1 mrg /** 757 1.1 mrg * @brief Controlling output for std::string. 758 1.1 mrg * @ingroup io 759 1.1 mrg * 760 1.1.1.2 mrg * @tparam _CharT Type of character stream. 761 1.1.1.2 mrg * @tparam _Traits Traits for character type, defaults to 762 1.1.1.2 mrg * char_traits<_CharT>. 763 1.1.1.2 mrg * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 764 1.1.1.2 mrg * 765 1.1 mrg * This class supports writing to objects of type std::basic_string, 766 1.1 mrg * using the inherited functions from std::basic_ostream. To control 767 1.1 mrg * the associated sequence, an instance of std::basic_stringbuf is used, 768 1.1 mrg * which this page refers to as @c sb. 769 1.1 mrg */ 770 1.1 mrg template <typename _CharT, typename _Traits, typename _Alloc> 771 1.1 mrg class basic_ostringstream : public basic_ostream<_CharT, _Traits> 772 1.1 mrg { 773 1.1 mrg public: 774 1.1 mrg // Types: 775 1.1 mrg typedef _CharT char_type; 776 1.1 mrg typedef _Traits traits_type; 777 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 778 1.1 mrg // 251. basic_stringbuf missing allocator_type 779 1.1 mrg typedef _Alloc allocator_type; 780 1.1 mrg typedef typename traits_type::int_type int_type; 781 1.1 mrg typedef typename traits_type::pos_type pos_type; 782 1.1 mrg typedef typename traits_type::off_type off_type; 783 1.1 mrg 784 1.1 mrg // Non-standard types: 785 1.1 mrg typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 786 1.1 mrg typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 787 1.1 mrg typedef basic_ostream<char_type, traits_type> __ostream_type; 788 1.1 mrg 789 1.1 mrg private: 790 1.1 mrg __stringbuf_type _M_stringbuf; 791 1.1 mrg 792 1.1 mrg public: 793 1.1 mrg // Constructors/destructor: 794 1.1.1.11 mrg 795 1.1 mrg /** 796 1.1 mrg * @brief Default constructor starts with an empty string buffer. 797 1.1.1.11 mrg * 798 1.1.1.11 mrg * Initializes @c sb using @c mode|out, and passes @c &sb to the base 799 1.1.1.11 mrg * class initializer. Does not allocate any buffer. 800 1.1.1.11 mrg * 801 1.1.1.11 mrg * That's a lie. We initialize the base class with NULL, because the 802 1.1.1.11 mrg * string class does its own memory management. 803 1.1.1.11 mrg */ 804 1.1.1.11 mrg basic_ostringstream() 805 1.1.1.11 mrg : __ostream_type(), _M_stringbuf(ios_base::out) 806 1.1.1.11 mrg { this->init(&_M_stringbuf); } 807 1.1.1.11 mrg 808 1.1.1.11 mrg /** 809 1.1.1.11 mrg * @brief Starts with an empty string buffer. 810 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 811 1.1 mrg * 812 1.1 mrg * @c ios_base::out is automatically included in @a mode. 813 1.1 mrg * 814 1.1 mrg * Initializes @c sb using @c mode|out, and passes @c &sb to the base 815 1.1 mrg * class initializer. Does not allocate any buffer. 816 1.1 mrg * 817 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 818 1.1 mrg * string class does its own memory management. 819 1.1 mrg */ 820 1.1 mrg explicit 821 1.1.1.11 mrg basic_ostringstream(ios_base::openmode __mode) 822 1.1 mrg : __ostream_type(), _M_stringbuf(__mode | ios_base::out) 823 1.1 mrg { this->init(&_M_stringbuf); } 824 1.1 mrg 825 1.1 mrg /** 826 1.1 mrg * @brief Starts with an existing string buffer. 827 1.1.1.2 mrg * @param __str A string to copy as a starting buffer. 828 1.1.1.2 mrg * @param __mode Whether the buffer can read, or write, or both. 829 1.1 mrg * 830 1.1 mrg * @c ios_base::out is automatically included in @a mode. 831 1.1 mrg * 832 1.1 mrg * Initializes @c sb using @a str and @c mode|out, and passes @c &sb 833 1.1 mrg * to the base class initializer. 834 1.1 mrg * 835 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 836 1.1 mrg * string class does its own memory management. 837 1.1 mrg */ 838 1.1 mrg explicit 839 1.1 mrg basic_ostringstream(const __string_type& __str, 840 1.1 mrg ios_base::openmode __mode = ios_base::out) 841 1.1 mrg : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out) 842 1.1 mrg { this->init(&_M_stringbuf); } 843 1.1 mrg 844 1.1 mrg /** 845 1.1 mrg * @brief The destructor does nothing. 846 1.1 mrg * 847 1.1 mrg * The buffer is deallocated by the stringbuf object, not the 848 1.1 mrg * formatting stream. 849 1.1 mrg */ 850 1.1 mrg ~basic_ostringstream() 851 1.1 mrg { } 852 1.1 mrg 853 1.1.1.3 mrg #if __cplusplus >= 201103L 854 1.1.1.3 mrg basic_ostringstream(const basic_ostringstream&) = delete; 855 1.1.1.3 mrg 856 1.1.1.3 mrg basic_ostringstream(basic_ostringstream&& __rhs) 857 1.1.1.3 mrg : __ostream_type(std::move(__rhs)), 858 1.1.1.3 mrg _M_stringbuf(std::move(__rhs._M_stringbuf)) 859 1.1.1.3 mrg { __ostream_type::set_rdbuf(&_M_stringbuf); } 860 1.1.1.3 mrg 861 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 862 1.1.1.13 mrg basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a) 863 1.1.1.13 mrg : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a) 864 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 865 1.1.1.13 mrg 866 1.1.1.13 mrg explicit 867 1.1.1.13 mrg basic_ostringstream(__string_type&& __str, 868 1.1.1.13 mrg ios_base::openmode __mode = ios_base::out) 869 1.1.1.13 mrg : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out) 870 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 871 1.1.1.13 mrg 872 1.1.1.13 mrg template<typename _SAlloc> 873 1.1.1.13 mrg basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 874 1.1.1.13 mrg const allocator_type& __a) 875 1.1.1.13 mrg : basic_ostringstream(__str, ios_base::out, __a) 876 1.1.1.13 mrg { } 877 1.1.1.13 mrg 878 1.1.1.13 mrg template<typename _SAlloc> 879 1.1.1.13 mrg basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 880 1.1.1.13 mrg ios_base::openmode __mode, 881 1.1.1.13 mrg const allocator_type& __a) 882 1.1.1.13 mrg : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a) 883 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 884 1.1.1.13 mrg 885 1.1.1.13 mrg template<typename _SAlloc> 886 1.1.1.13 mrg explicit 887 1.1.1.13 mrg basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 888 1.1.1.13 mrg ios_base::openmode __mode = ios_base::out) 889 1.1.1.13 mrg : basic_ostringstream(__str, __mode, allocator_type()) 890 1.1.1.13 mrg { } 891 1.1.1.13 mrg #endif // C++20 892 1.1.1.13 mrg 893 1.1.1.3 mrg // 27.8.3.2 Assign and swap: 894 1.1.1.3 mrg 895 1.1.1.3 mrg basic_ostringstream& 896 1.1.1.3 mrg operator=(const basic_ostringstream&) = delete; 897 1.1.1.3 mrg 898 1.1.1.3 mrg basic_ostringstream& 899 1.1.1.3 mrg operator=(basic_ostringstream&& __rhs) 900 1.1.1.3 mrg { 901 1.1.1.3 mrg __ostream_type::operator=(std::move(__rhs)); 902 1.1.1.3 mrg _M_stringbuf = std::move(__rhs._M_stringbuf); 903 1.1.1.3 mrg return *this; 904 1.1.1.3 mrg } 905 1.1.1.3 mrg 906 1.1.1.3 mrg void 907 1.1.1.3 mrg swap(basic_ostringstream& __rhs) 908 1.1.1.3 mrg { 909 1.1.1.3 mrg __ostream_type::swap(__rhs); 910 1.1.1.3 mrg _M_stringbuf.swap(__rhs._M_stringbuf); 911 1.1.1.3 mrg } 912 1.1.1.13 mrg #endif // C++11 913 1.1.1.3 mrg 914 1.1 mrg // Members: 915 1.1 mrg /** 916 1.1 mrg * @brief Accessing the underlying buffer. 917 1.1 mrg * @return The current basic_stringbuf buffer. 918 1.1 mrg * 919 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 920 1.1 mrg */ 921 1.1 mrg __stringbuf_type* 922 1.1 mrg rdbuf() const 923 1.1 mrg { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 924 1.1 mrg 925 1.1 mrg /** 926 1.1 mrg * @brief Copying out the string buffer. 927 1.1 mrg * @return @c rdbuf()->str() 928 1.1 mrg */ 929 1.1 mrg __string_type 930 1.1.1.13 mrg str() const _GLIBCXX_LVAL_REF_QUAL 931 1.1 mrg { return _M_stringbuf.str(); } 932 1.1 mrg 933 1.1.1.14 mrg #if __cplusplus > 201703L 934 1.1.1.14 mrg #if _GLIBCXX_USE_CXX11_ABI 935 1.1.1.13 mrg #if __cpp_concepts 936 1.1.1.13 mrg template<__allocator_like _SAlloc> 937 1.1.1.13 mrg basic_string<_CharT, _Traits, _SAlloc> 938 1.1.1.13 mrg str(const _SAlloc& __sa) const 939 1.1.1.13 mrg { return _M_stringbuf.str(__sa); } 940 1.1.1.13 mrg #endif 941 1.1.1.13 mrg 942 1.1.1.13 mrg __string_type 943 1.1.1.13 mrg str() && 944 1.1.1.13 mrg { return std::move(_M_stringbuf).str(); } 945 1.1.1.14 mrg #endif // cxx11 ABI 946 1.1.1.13 mrg 947 1.1.1.14 mrg _GLIBCXX_SSTREAM_ALWAYS_INLINE 948 1.1.1.13 mrg basic_string_view<char_type, traits_type> 949 1.1.1.13 mrg view() const noexcept 950 1.1.1.13 mrg { return _M_stringbuf.view(); } 951 1.1.1.14 mrg #endif // C++20 952 1.1.1.13 mrg 953 1.1 mrg /** 954 1.1 mrg * @brief Setting a new buffer. 955 1.1.1.2 mrg * @param __s The string to use as a new sequence. 956 1.1 mrg * 957 1.1 mrg * Calls @c rdbuf()->str(s). 958 1.1 mrg */ 959 1.1 mrg void 960 1.1 mrg str(const __string_type& __s) 961 1.1 mrg { _M_stringbuf.str(__s); } 962 1.1.1.13 mrg 963 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 964 1.1.1.13 mrg #if __cpp_concepts 965 1.1.1.13 mrg template<__allocator_like _SAlloc> 966 1.1.1.13 mrg requires (!is_same_v<_SAlloc, _Alloc>) 967 1.1.1.13 mrg void 968 1.1.1.13 mrg str(const basic_string<_CharT, _Traits, _SAlloc>& __s) 969 1.1.1.13 mrg { _M_stringbuf.str(__s); } 970 1.1.1.13 mrg #endif 971 1.1.1.13 mrg 972 1.1.1.13 mrg void 973 1.1.1.13 mrg str(__string_type&& __s) 974 1.1.1.13 mrg { _M_stringbuf.str(std::move(__s)); } 975 1.1.1.13 mrg #endif 976 1.1 mrg }; 977 1.1 mrg 978 1.1 mrg 979 1.1 mrg // [27.7.4] Template class basic_stringstream 980 1.1 mrg /** 981 1.1 mrg * @brief Controlling input and output for std::string. 982 1.1 mrg * @ingroup io 983 1.1 mrg * 984 1.1.1.2 mrg * @tparam _CharT Type of character stream. 985 1.1.1.2 mrg * @tparam _Traits Traits for character type, defaults to 986 1.1.1.2 mrg * char_traits<_CharT>. 987 1.1.1.2 mrg * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 988 1.1.1.2 mrg * 989 1.1 mrg * This class supports reading from and writing to objects of type 990 1.1 mrg * std::basic_string, using the inherited functions from 991 1.1 mrg * std::basic_iostream. To control the associated sequence, an instance 992 1.1 mrg * of std::basic_stringbuf is used, which this page refers to as @c sb. 993 1.1 mrg */ 994 1.1 mrg template <typename _CharT, typename _Traits, typename _Alloc> 995 1.1 mrg class basic_stringstream : public basic_iostream<_CharT, _Traits> 996 1.1 mrg { 997 1.1 mrg public: 998 1.1 mrg // Types: 999 1.1 mrg typedef _CharT char_type; 1000 1.1 mrg typedef _Traits traits_type; 1001 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1002 1.1 mrg // 251. basic_stringbuf missing allocator_type 1003 1.1 mrg typedef _Alloc allocator_type; 1004 1.1 mrg typedef typename traits_type::int_type int_type; 1005 1.1 mrg typedef typename traits_type::pos_type pos_type; 1006 1.1 mrg typedef typename traits_type::off_type off_type; 1007 1.1 mrg 1008 1.1 mrg // Non-standard Types: 1009 1.1 mrg typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 1010 1.1 mrg typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 1011 1.1 mrg typedef basic_iostream<char_type, traits_type> __iostream_type; 1012 1.1 mrg 1013 1.1 mrg private: 1014 1.1 mrg __stringbuf_type _M_stringbuf; 1015 1.1 mrg 1016 1.1 mrg public: 1017 1.1 mrg // Constructors/destructors 1018 1.1.1.11 mrg 1019 1.1 mrg /** 1020 1.1 mrg * @brief Default constructor starts with an empty string buffer. 1021 1.1.1.11 mrg * 1022 1.1.1.11 mrg * Initializes @c sb using the mode @c in|out, and passes @c &sb 1023 1.1.1.11 mrg * to the base class initializer. Does not allocate any buffer. 1024 1.1.1.11 mrg * 1025 1.1.1.11 mrg * That's a lie. We initialize the base class with NULL, because the 1026 1.1.1.11 mrg * string class does its own memory management. 1027 1.1.1.11 mrg */ 1028 1.1.1.11 mrg basic_stringstream() 1029 1.1.1.11 mrg : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in) 1030 1.1.1.11 mrg { this->init(&_M_stringbuf); } 1031 1.1.1.11 mrg 1032 1.1.1.11 mrg /** 1033 1.1.1.11 mrg * @brief Starts with an empty string buffer. 1034 1.1.1.2 mrg * @param __m Whether the buffer can read, or write, or both. 1035 1.1 mrg * 1036 1.1.1.11 mrg * Initializes @c sb using the mode from @c __m, and passes @c &sb 1037 1.1.1.11 mrg * to the base class initializer. Does not allocate any buffer. 1038 1.1 mrg * 1039 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 1040 1.1 mrg * string class does its own memory management. 1041 1.1 mrg */ 1042 1.1 mrg explicit 1043 1.1.1.11 mrg basic_stringstream(ios_base::openmode __m) 1044 1.1 mrg : __iostream_type(), _M_stringbuf(__m) 1045 1.1 mrg { this->init(&_M_stringbuf); } 1046 1.1 mrg 1047 1.1 mrg /** 1048 1.1 mrg * @brief Starts with an existing string buffer. 1049 1.1.1.2 mrg * @param __str A string to copy as a starting buffer. 1050 1.1.1.2 mrg * @param __m Whether the buffer can read, or write, or both. 1051 1.1 mrg * 1052 1.1.1.2 mrg * Initializes @c sb using @a __str and @c __m, and passes @c &sb 1053 1.1 mrg * to the base class initializer. 1054 1.1 mrg * 1055 1.1 mrg * That's a lie. We initialize the base class with NULL, because the 1056 1.1 mrg * string class does its own memory management. 1057 1.1 mrg */ 1058 1.1 mrg explicit 1059 1.1 mrg basic_stringstream(const __string_type& __str, 1060 1.1 mrg ios_base::openmode __m = ios_base::out | ios_base::in) 1061 1.1 mrg : __iostream_type(), _M_stringbuf(__str, __m) 1062 1.1 mrg { this->init(&_M_stringbuf); } 1063 1.1 mrg 1064 1.1 mrg /** 1065 1.1 mrg * @brief The destructor does nothing. 1066 1.1 mrg * 1067 1.1 mrg * The buffer is deallocated by the stringbuf object, not the 1068 1.1 mrg * formatting stream. 1069 1.1 mrg */ 1070 1.1 mrg ~basic_stringstream() 1071 1.1 mrg { } 1072 1.1 mrg 1073 1.1.1.3 mrg #if __cplusplus >= 201103L 1074 1.1.1.3 mrg basic_stringstream(const basic_stringstream&) = delete; 1075 1.1.1.3 mrg 1076 1.1.1.3 mrg basic_stringstream(basic_stringstream&& __rhs) 1077 1.1.1.3 mrg : __iostream_type(std::move(__rhs)), 1078 1.1.1.3 mrg _M_stringbuf(std::move(__rhs._M_stringbuf)) 1079 1.1.1.3 mrg { __iostream_type::set_rdbuf(&_M_stringbuf); } 1080 1.1.1.3 mrg 1081 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 1082 1.1.1.13 mrg basic_stringstream(ios_base::openmode __mode, const allocator_type& __a) 1083 1.1.1.13 mrg : __iostream_type(), _M_stringbuf(__mode, __a) 1084 1.1.1.13 mrg { this->init(&_M_stringbuf); } 1085 1.1.1.13 mrg 1086 1.1.1.13 mrg explicit 1087 1.1.1.13 mrg basic_stringstream(__string_type&& __str, 1088 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in 1089 1.1.1.13 mrg | ios_base::out) 1090 1.1.1.13 mrg : __iostream_type(), _M_stringbuf(std::move(__str), __mode) 1091 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 1092 1.1.1.13 mrg 1093 1.1.1.13 mrg template<typename _SAlloc> 1094 1.1.1.13 mrg basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 1095 1.1.1.13 mrg const allocator_type& __a) 1096 1.1.1.13 mrg : basic_stringstream(__str, ios_base::in | ios_base::out, __a) 1097 1.1.1.13 mrg { } 1098 1.1.1.13 mrg 1099 1.1.1.13 mrg template<typename _SAlloc> 1100 1.1.1.13 mrg basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 1101 1.1.1.13 mrg ios_base::openmode __mode, 1102 1.1.1.13 mrg const allocator_type& __a) 1103 1.1.1.13 mrg : __iostream_type(), _M_stringbuf(__str, __mode, __a) 1104 1.1.1.13 mrg { this->init(std::__addressof(_M_stringbuf)); } 1105 1.1.1.13 mrg 1106 1.1.1.13 mrg template<typename _SAlloc> 1107 1.1.1.13 mrg explicit 1108 1.1.1.13 mrg basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str, 1109 1.1.1.13 mrg ios_base::openmode __mode = ios_base::in 1110 1.1.1.13 mrg | ios_base::out) 1111 1.1.1.13 mrg : basic_stringstream(__str, __mode, allocator_type()) 1112 1.1.1.13 mrg { } 1113 1.1.1.13 mrg #endif // C++20 1114 1.1.1.13 mrg 1115 1.1.1.3 mrg // 27.8.3.2 Assign and swap: 1116 1.1.1.3 mrg 1117 1.1.1.3 mrg basic_stringstream& 1118 1.1.1.3 mrg operator=(const basic_stringstream&) = delete; 1119 1.1.1.3 mrg 1120 1.1.1.3 mrg basic_stringstream& 1121 1.1.1.3 mrg operator=(basic_stringstream&& __rhs) 1122 1.1.1.3 mrg { 1123 1.1.1.3 mrg __iostream_type::operator=(std::move(__rhs)); 1124 1.1.1.3 mrg _M_stringbuf = std::move(__rhs._M_stringbuf); 1125 1.1.1.3 mrg return *this; 1126 1.1.1.3 mrg } 1127 1.1.1.3 mrg 1128 1.1.1.3 mrg void 1129 1.1.1.3 mrg swap(basic_stringstream& __rhs) 1130 1.1.1.3 mrg { 1131 1.1.1.3 mrg __iostream_type::swap(__rhs); 1132 1.1.1.3 mrg _M_stringbuf.swap(__rhs._M_stringbuf); 1133 1.1.1.3 mrg } 1134 1.1.1.13 mrg #endif // C++11 1135 1.1.1.3 mrg 1136 1.1 mrg // Members: 1137 1.1 mrg /** 1138 1.1 mrg * @brief Accessing the underlying buffer. 1139 1.1 mrg * @return The current basic_stringbuf buffer. 1140 1.1 mrg * 1141 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 1142 1.1 mrg */ 1143 1.1 mrg __stringbuf_type* 1144 1.1 mrg rdbuf() const 1145 1.1 mrg { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 1146 1.1 mrg 1147 1.1 mrg /** 1148 1.1 mrg * @brief Copying out the string buffer. 1149 1.1 mrg * @return @c rdbuf()->str() 1150 1.1 mrg */ 1151 1.1 mrg __string_type 1152 1.1.1.13 mrg str() const _GLIBCXX_LVAL_REF_QUAL 1153 1.1 mrg { return _M_stringbuf.str(); } 1154 1.1 mrg 1155 1.1.1.14 mrg #if __cplusplus > 201703L 1156 1.1.1.14 mrg #if _GLIBCXX_USE_CXX11_ABI 1157 1.1.1.13 mrg #if __cpp_concepts 1158 1.1.1.13 mrg template<__allocator_like _SAlloc> 1159 1.1.1.13 mrg basic_string<_CharT, _Traits, _SAlloc> 1160 1.1.1.13 mrg str(const _SAlloc& __sa) const 1161 1.1.1.13 mrg { return _M_stringbuf.str(__sa); } 1162 1.1.1.13 mrg #endif 1163 1.1.1.13 mrg 1164 1.1.1.13 mrg __string_type 1165 1.1.1.13 mrg str() && 1166 1.1.1.13 mrg { return std::move(_M_stringbuf).str(); } 1167 1.1.1.14 mrg #endif // cxx11 ABI 1168 1.1.1.13 mrg 1169 1.1.1.14 mrg _GLIBCXX_SSTREAM_ALWAYS_INLINE 1170 1.1.1.13 mrg basic_string_view<char_type, traits_type> 1171 1.1.1.13 mrg view() const noexcept 1172 1.1.1.13 mrg { return _M_stringbuf.view(); } 1173 1.1.1.14 mrg #endif // C++20 1174 1.1.1.13 mrg 1175 1.1 mrg /** 1176 1.1 mrg * @brief Setting a new buffer. 1177 1.1.1.2 mrg * @param __s The string to use as a new sequence. 1178 1.1 mrg * 1179 1.1 mrg * Calls @c rdbuf()->str(s). 1180 1.1 mrg */ 1181 1.1 mrg void 1182 1.1 mrg str(const __string_type& __s) 1183 1.1 mrg { _M_stringbuf.str(__s); } 1184 1.1.1.13 mrg 1185 1.1.1.13 mrg #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI 1186 1.1.1.13 mrg #if __cpp_concepts 1187 1.1.1.13 mrg template<__allocator_like _SAlloc> 1188 1.1.1.13 mrg requires (!is_same_v<_SAlloc, _Alloc>) 1189 1.1.1.13 mrg void 1190 1.1.1.13 mrg str(const basic_string<_CharT, _Traits, _SAlloc>& __s) 1191 1.1.1.13 mrg { _M_stringbuf.str(__s); } 1192 1.1.1.13 mrg #endif 1193 1.1.1.13 mrg 1194 1.1.1.13 mrg void 1195 1.1.1.13 mrg str(__string_type&& __s) 1196 1.1.1.13 mrg { _M_stringbuf.str(std::move(__s)); } 1197 1.1.1.13 mrg #endif 1198 1.1 mrg }; 1199 1.1 mrg 1200 1.1.1.3 mrg #if __cplusplus >= 201103L 1201 1.1.1.3 mrg /// Swap specialization for stringbufs. 1202 1.1.1.3 mrg template <class _CharT, class _Traits, class _Allocator> 1203 1.1.1.3 mrg inline void 1204 1.1.1.3 mrg swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x, 1205 1.1.1.3 mrg basic_stringbuf<_CharT, _Traits, _Allocator>& __y) 1206 1.1.1.13 mrg noexcept(noexcept(__x.swap(__y))) 1207 1.1.1.3 mrg { __x.swap(__y); } 1208 1.1.1.3 mrg 1209 1.1.1.3 mrg /// Swap specialization for istringstreams. 1210 1.1.1.3 mrg template <class _CharT, class _Traits, class _Allocator> 1211 1.1.1.3 mrg inline void 1212 1.1.1.3 mrg swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x, 1213 1.1.1.3 mrg basic_istringstream<_CharT, _Traits, _Allocator>& __y) 1214 1.1.1.3 mrg { __x.swap(__y); } 1215 1.1.1.3 mrg 1216 1.1.1.3 mrg /// Swap specialization for ostringstreams. 1217 1.1.1.3 mrg template <class _CharT, class _Traits, class _Allocator> 1218 1.1.1.3 mrg inline void 1219 1.1.1.3 mrg swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x, 1220 1.1.1.3 mrg basic_ostringstream<_CharT, _Traits, _Allocator>& __y) 1221 1.1.1.3 mrg { __x.swap(__y); } 1222 1.1.1.3 mrg 1223 1.1.1.3 mrg /// Swap specialization for stringstreams. 1224 1.1.1.3 mrg template <class _CharT, class _Traits, class _Allocator> 1225 1.1.1.3 mrg inline void 1226 1.1.1.3 mrg swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x, 1227 1.1.1.3 mrg basic_stringstream<_CharT, _Traits, _Allocator>& __y) 1228 1.1.1.3 mrg { __x.swap(__y); } 1229 1.1.1.13 mrg #endif // C++11 1230 1.1.1.3 mrg 1231 1.1.1.3 mrg _GLIBCXX_END_NAMESPACE_CXX11 1232 1.1.1.2 mrg _GLIBCXX_END_NAMESPACE_VERSION 1233 1.1.1.2 mrg } // namespace 1234 1.1 mrg 1235 1.1.1.14 mrg #undef _GLIBCXX_SSTREAM_ALWAYS_INLINE 1236 1.1.1.13 mrg #undef _GLIBCXX_LVAL_REF_QUAL 1237 1.1.1.13 mrg 1238 1.1.1.2 mrg #include <bits/sstream.tcc> 1239 1.1 mrg 1240 1.1 mrg #endif /* _GLIBCXX_SSTREAM */ 1241