1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_SSTREAM 11#define _LIBCPP_SSTREAM 12 13// clang-format off 14 15/* 16 sstream synopsis [sstream.syn] 17 18// Class template basic_stringbuf [stringbuf] 19template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > 20class basic_stringbuf 21 : public basic_streambuf<charT, traits> 22{ 23public: 24 typedef charT char_type; 25 typedef traits traits_type; 26 typedef typename traits_type::int_type int_type; 27 typedef typename traits_type::pos_type pos_type; 28 typedef typename traits_type::off_type off_type; 29 typedef Allocator allocator_type; 30 31 // [stringbuf.cons] constructors: 32 explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out); // before C++20 33 basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {} // C++20 34 explicit basic_stringbuf(ios_base::openmode which); // C++20 35 explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& s, 36 ios_base::openmode which = ios_base::in | ios_base::out); 37 explicit basic_stringbuf(const allocator_type& a) 38 : basic_stringbuf(ios_base::in | ios_base::out, a) {} // C++20 39 basic_stringbuf(ios_base::openmode which, const allocator_type& a); // C++20 40 explicit basic_stringbuf(basic_string<char_type, traits_type, allocator_type>&& s, 41 ios_base::openmode which = ios_base::in | ios_base::out); // C++20 42 template <class SAlloc> 43 basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a) 44 : basic_stringbuf(s, ios_base::in | ios_base::out, a) {} // C++20 45 template <class SAlloc> 46 basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s, 47 ios_base::openmode which, const allocator_type& a); // C++20 48 template <class SAlloc> 49 explicit basic_stringbuf(const basic_string<char_type, traits_type, SAlloc>& s, 50 ios_base::openmode which = ios_base::in | ios_base::out); // C++20 51 template<class T> 52 explicit basic_stringbuf(const T& t, 53 ios_base::openmode which = ios_base::in | ios_base::out); // Since C++26 54 template<class T> 55 basic_stringbuf(const T& t, const Allocator& a); // Since C++26 56 template<class T> 57 basic_stringbuf(const T& t, ios_base::openmode which, const Allocator& a); // Since C++26 58 basic_stringbuf(const basic_stringbuf&) = delete; 59 basic_stringbuf(basic_stringbuf&& rhs); 60 basic_stringbuf(basic_stringbuf&& rhs, const allocator_type& a); // C++20 61 62 // [stringbuf.assign] Assign and swap: 63 basic_stringbuf& operator=(const basic_stringbuf&) = delete; 64 basic_stringbuf& operator=(basic_stringbuf&& rhs); 65 void swap(basic_stringbuf& rhs) noexcept(see below); // conditionally noexcept since C++20 66 67 // [stringbuf.members] Member functions: 68 allocator_type get_allocator() const noexcept; // C++20 69 basic_string<char_type, traits_type, allocator_type> str() const; // before C++20 70 basic_string<char_type, traits_type, allocator_type> str() const &; // C++20 71 template <class SAlloc> 72 basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20 73 basic_string<char_type, traits_type, allocator_type> str() &&; // C++20 74 basic_string_view<char_type, traits_type> view() const noexcept; // C++20 75 void str(const basic_string<char_type, traits_type, allocator_type>& s); 76 template <class SAlloc> 77 void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20 78 void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20 79 template<class T> 80 void str(const T& t); // Since C++26 81 82protected: 83 // [stringbuf.virtuals] Overridden virtual functions: 84 virtual int_type underflow(); 85 virtual int_type pbackfail(int_type c = traits_type::eof()); 86 virtual int_type overflow (int_type c = traits_type::eof()); 87 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize); 88 virtual pos_type seekoff(off_type off, ios_base::seekdir way, 89 ios_base::openmode which = ios_base::in | ios_base::out); 90 virtual pos_type seekpos(pos_type sp, 91 ios_base::openmode which = ios_base::in | ios_base::out); 92}; 93 94// [stringbuf.assign] non member swap 95template <class charT, class traits, class Allocator> 96void swap(basic_stringbuf<charT, traits, Allocator>& x, 97 basic_stringbuf<charT, traits, Allocator>& y); // conditionally noexcept since C++20 98 99typedef basic_stringbuf<char> stringbuf; 100typedef basic_stringbuf<wchar_t> wstringbuf; 101 102// Class template basic_istringstream [istringstream] 103template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > 104class basic_istringstream 105 : public basic_istream<charT, traits> 106{ 107public: 108 typedef charT char_type; 109 typedef traits traits_type; 110 typedef typename traits_type::int_type int_type; 111 typedef typename traits_type::pos_type pos_type; 112 typedef typename traits_type::off_type off_type; 113 typedef Allocator allocator_type; 114 115 // [istringstream.cons] Constructors: 116 explicit basic_istringstream(ios_base::openmode which = ios_base::in); // before C++20 117 basic_istringstream() : basic_istringstream(ios_base::in) {} // C++20 118 explicit basic_istringstream(ios_base::openmode which); // C++20 119 explicit basic_istringstream(const basic_string<char_type, traits_type, allocator_type>& s, 120 ios_base::openmode which = ios_base::in); 121 basic_istringstream(ios_base::openmode which, const allocator_type& a); // C++20 122 explicit basic_istringstream(basic_string<char_type, traits_type, allocator_type>&& s, 123 ios_base::openmode which = ios_base::in); // C++20 124 template <class SAlloc> 125 basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a) 126 : basic_istringstream(s, ios_base::in, a) {} // C++20 127 template <class SAlloc> 128 basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s, 129 ios_base::openmode which, const allocator_type& a); // C++20 130 template <class SAlloc> 131 explicit basic_istringstream(const basic_string<char_type, traits_type, SAlloc>& s, 132 ios_base::openmode which = ios_base::in); // C++20 133 template<class T> 134 explicit basic_istringstream(const T& t, ios_base::openmode which = ios_base::in); // Since C++26 135 template<class T> 136 basic_istringstream(const T& t, const Allocator& a); // Since C++26 137 template<class T> 138 basic_istringstream(const T& t, ios_base::openmode which, const Allocator& a); // Since C++26 139 basic_istringstream(const basic_istringstream&) = delete; 140 basic_istringstream(basic_istringstream&& rhs); 141 142 // [istringstream.assign] Assign and swap: 143 basic_istringstream& operator=(const basic_istringstream&) = delete; 144 basic_istringstream& operator=(basic_istringstream&& rhs); 145 void swap(basic_istringstream& rhs); 146 147 // [istringstream.members] Member functions: 148 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; 149 basic_string<char_type, traits_type, allocator_type> str() const; // before C++20 150 basic_string<char_type, traits_type, allocator_type> str() const &; // C++20 151 template <class SAlloc> 152 basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20 153 basic_string<char_type, traits_type, allocator_type> str() &&; // C++20 154 basic_string_view<char_type, traits_type> view() const noexcept; // C++20 155 void str(const basic_string<char_type, traits_type, allocator_type>& s); 156 template <class SAlloc> 157 void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20 158 void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20 159 template<class T> 160 void str(const T& t); // Since C++26 161}; 162 163template <class charT, class traits, class Allocator> 164void swap(basic_istringstream<charT, traits, Allocator>& x, 165 basic_istringstream<charT, traits, Allocator>& y); 166 167typedef basic_istringstream<char> istringstream; 168typedef basic_istringstream<wchar_t> wistringstream; 169 170// Class template basic_ostringstream [ostringstream] 171template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > 172class basic_ostringstream 173 : public basic_ostream<charT, traits> 174{ 175public: 176 // types: 177 typedef charT char_type; 178 typedef traits traits_type; 179 typedef typename traits_type::int_type int_type; 180 typedef typename traits_type::pos_type pos_type; 181 typedef typename traits_type::off_type off_type; 182 typedef Allocator allocator_type; 183 184 // [ostringstream.cons] Constructors: 185 explicit basic_ostringstream(ios_base::openmode which = ios_base::out); // before C++20 186 basic_ostringstream() : basic_ostringstream(ios_base::out) {} // C++20 187 explicit basic_ostringstream(ios_base::openmode which); // C++20 188 explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& s, 189 ios_base::openmode which = ios_base::out); 190 basic_ostringstream(ios_base::openmode which, const allocator_type& a); // C++20 191 explicit basic_ostringstream(basic_string<char_type, traits_type, allocator_type>&& s, 192 ios_base::openmode which = ios_base::out); // C++20 193 template <class SAlloc> 194 basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a) 195 : basic_ostringstream(s, ios_base::out, a) {} // C++20 196 template <class SAlloc> 197 basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s, 198 ios_base::openmode which, const allocator_type& a); // C++20 199 template <class SAlloc> 200 explicit basic_ostringstream(const basic_string<char_type, traits_type, SAlloc>& s, 201 ios_base::openmode which = ios_base::out); // C++20 202 template<class T> 203 explicit basic_ostringstream(const T& t, ios_base::openmode which = ios_base::out); // Since C++26 204 template<class T> 205 basic_ostringstream(const T& t, const Allocator& a); // Since C++26 206 template<class T> 207 basic_ostringstream(const T& t, ios_base::openmode which, const Allocator& a); // Since C++26 208 basic_ostringstream(const basic_ostringstream&) = delete; 209 basic_ostringstream(basic_ostringstream&& rhs); 210 211 // [ostringstream.assign] Assign and swap: 212 basic_ostringstream& operator=(const basic_ostringstream&) = delete; 213 basic_ostringstream& operator=(basic_ostringstream&& rhs); 214 void swap(basic_ostringstream& rhs); 215 216 // [ostringstream.members] Member functions: 217 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; 218 basic_string<char_type, traits_type, allocator_type> str() const; // before C++20 219 basic_string<char_type, traits_type, allocator_type> str() const &; // C++20 220 template <class SAlloc> 221 basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20 222 basic_string<char_type, traits_type, allocator_type> str() &&; // C++20 223 basic_string_view<char_type, traits_type> view() const noexcept; // C++20 224 void str(const basic_string<char_type, traits_type, allocator_type>& s); 225 template <class SAlloc> 226 void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20 227 void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20 228 template<class T> 229 void str(const T& t); // Since C++26 230}; 231 232template <class charT, class traits, class Allocator> 233void swap(basic_ostringstream<charT, traits, Allocator>& x, 234 basic_ostringstream<charT, traits, Allocator>& y); 235 236typedef basic_ostringstream<char> ostringstream; 237typedef basic_ostringstream<wchar_t> wostringstream; 238 239// Class template basic_stringstream [stringstream] 240template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > 241class basic_stringstream 242 : public basic_iostream<charT, traits> 243{ 244public: 245 // types: 246 typedef charT char_type; 247 typedef traits traits_type; 248 typedef typename traits_type::int_type int_type; 249 typedef typename traits_type::pos_type pos_type; 250 typedef typename traits_type::off_type off_type; 251 typedef Allocator allocator_type; 252 253 // [stringstream.cons] constructors 254 explicit basic_stringstream(ios_base::openmode which = ios_base::out | ios_base::in); // before C++20 255 basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {} // C++20 256 explicit basic_stringstream(ios_base::openmode which); // C++20 257 explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& s, 258 ios_base::openmode which = ios_base::out | ios_base::in); 259 basic_stringstream(ios_base::openmode which, const allocator_type& a); // C++20 260 explicit basic_stringstream(basic_string<char_type, traits_type, allocator_type>&& s, 261 ios_base::openmode which = ios_base::out | ios_base::in); // C++20 262 template <class SAlloc> 263 basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s, const allocator_type& a) 264 : basic_stringstream(s, ios_base::out | ios_base::in, a) {} // C++20 265 template <class SAlloc> 266 basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s, 267 ios_base::openmode which, const allocator_type& a); // C++20 268 template <class SAlloc> 269 explicit basic_stringstream(const basic_string<char_type, traits_type, SAlloc>& s, 270 ios_base::openmode which = ios_base::out | ios_base::in); // C++20 271 template<class T> 272 explicit basic_stringstream(const T& t, 273 ios_base::openmode which = ios_base::out | ios_base::in); // Since C++26 274 template<class T> 275 basic_stringstream(const T& t, const Allocator& a); // Since C++26 276 template<class T> 277 basic_stringstream(const T& t, ios_base::openmode which, const Allocator& a); // Since C++26 278 basic_stringstream(const basic_stringstream&) = delete; 279 basic_stringstream(basic_stringstream&& rhs); 280 281 // [stringstream.assign] Assign and swap: 282 basic_stringstream& operator=(const basic_stringstream&) = delete; 283 basic_stringstream& operator=(basic_stringstream&& rhs); 284 void swap(basic_stringstream& rhs); 285 286 // [stringstream.members] Member functions: 287 basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; 288 basic_string<char_type, traits_type, allocator_type> str() const; // before C++20 289 basic_string<char_type, traits_type, allocator_type> str() const &; // C++20 290 template <class SAlloc> 291 basic_string<char_type, traits_type, SAlloc> str(const SAlloc& sa) const; // C++20 292 basic_string<char_type, traits_type, allocator_type> str() &&; // C++20 293 basic_string_view<char_type, traits_type> view() const noexcept; // C++20 294 void str(const basic_string<char_type, traits_type, allocator_type>& s); 295 template <class SAlloc> 296 void str(const basic_string<char_type, traits_type, SAlloc>& s); // C++20 297 void str(basic_string<char_type, traits_type, allocator_type>&& s); // C++20 298 template<class T> 299 void str(const T& t); // Since C++26 300}; 301 302template <class charT, class traits, class Allocator> 303void swap(basic_stringstream<charT, traits, Allocator>& x, 304 basic_stringstream<charT, traits, Allocator>& y); 305 306typedef basic_stringstream<char> stringstream; 307typedef basic_stringstream<wchar_t> wstringstream; 308 309} // std 310 311*/ 312 313// clang-format on 314 315#include <__availability> 316#include <__config> 317#include <__fwd/sstream.h> 318#include <__type_traits/is_convertible.h> 319#include <__utility/swap.h> 320#include <istream> 321#include <ostream> 322#include <string> 323#include <string_view> 324#include <version> 325 326#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 327# pragma GCC system_header 328#endif 329 330_LIBCPP_PUSH_MACROS 331#include <__undef_macros> 332 333// TODO(LLVM-19): Remove this once we drop support for Clang 16, 334// which had this bug: https://github.com/llvm/llvm-project/issues/40363 335#ifdef _WIN32 336# define _LIBCPP_HIDE_FROM_ABI_SSTREAM _LIBCPP_ALWAYS_INLINE 337#else 338# define _LIBCPP_HIDE_FROM_ABI_SSTREAM _LIBCPP_HIDE_FROM_ABI 339#endif 340 341_LIBCPP_BEGIN_NAMESPACE_STD 342 343// Class template basic_stringbuf [stringbuf] 344 345template <class _CharT, class _Traits, class _Allocator> 346class _LIBCPP_TEMPLATE_VIS basic_stringbuf : public basic_streambuf<_CharT, _Traits> { 347public: 348 typedef _CharT char_type; 349 typedef _Traits traits_type; 350 typedef typename traits_type::int_type int_type; 351 typedef typename traits_type::pos_type pos_type; 352 typedef typename traits_type::off_type off_type; 353 typedef _Allocator allocator_type; 354 355 typedef basic_string<char_type, traits_type, allocator_type> string_type; 356 357private: 358 string_type __str_; 359 mutable char_type* __hm_; 360 ios_base::openmode __mode_; 361 _LIBCPP_HIDE_FROM_ABI void __init_buf_ptrs(); 362 _LIBCPP_HIDE_FROM_ABI void __move_init(basic_stringbuf&& __rhs); 363 364public: 365 // [stringbuf.cons] constructors: 366 _LIBCPP_HIDE_FROM_ABI basic_stringbuf() : __hm_(nullptr), __mode_(ios_base::in | ios_base::out) {} 367 368 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(ios_base::openmode __wch) : __hm_(nullptr), __mode_(__wch) {} 369 370 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const string_type& __s, 371 ios_base::openmode __wch = ios_base::in | ios_base::out) 372 : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch) { 373 str(__s); 374 } 375 376#if _LIBCPP_STD_VER >= 20 377 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const allocator_type& __a) 378 : basic_stringbuf(ios_base::in | ios_base::out, __a) {} 379 380 _LIBCPP_HIDE_FROM_ABI basic_stringbuf(ios_base::openmode __wch, const allocator_type& __a) 381 : __str_(__a), __hm_(nullptr), __mode_(__wch) {} 382 383 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(string_type&& __s, 384 ios_base::openmode __wch = ios_base::in | ios_base::out) 385 : __str_(std::move(__s)), __hm_(nullptr), __mode_(__wch) { 386 __init_buf_ptrs(); 387 } 388 389 template <class _SAlloc> 390 _LIBCPP_HIDE_FROM_ABI 391 basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s, const allocator_type& __a) 392 : basic_stringbuf(__s, ios_base::in | ios_base::out, __a) {} 393 394 template <class _SAlloc> 395 _LIBCPP_HIDE_FROM_ABI basic_stringbuf( 396 const basic_string<char_type, traits_type, _SAlloc>& __s, ios_base::openmode __wch, const allocator_type& __a) 397 : __str_(__s, __a), __hm_(nullptr), __mode_(__wch) { 398 __init_buf_ptrs(); 399 } 400 401 template <class _SAlloc> 402 requires(!is_same_v<_SAlloc, allocator_type>) 403 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s, 404 ios_base::openmode __wch = ios_base::in | ios_base::out) 405 : __str_(__s), __hm_(nullptr), __mode_(__wch) { 406 __init_buf_ptrs(); 407 } 408#endif // _LIBCPP_STD_VER >= 20 409 410#if _LIBCPP_STD_VER >= 26 411 412 template <class _Tp> 413 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 414 _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const _Tp& __t, 415 ios_base::openmode __which = ios_base::in | ios_base::out) 416 : basic_stringbuf(__t, __which, _Allocator()) {} 417 418 template <class _Tp> 419 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 420 _LIBCPP_HIDE_FROM_ABI basic_stringbuf(const _Tp& __t, const _Allocator& __a) 421 : basic_stringbuf(__t, ios_base::in | ios_base::out, __a) {} 422 423 template <class _Tp> 424 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 425 _LIBCPP_HIDE_FROM_ABI basic_stringbuf(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) 426 : __hm_(nullptr), __mode_(__which) { 427 basic_string_view<_CharT, _Traits> __sv = __t; 428 __str_ = string_type(__sv, __a); 429 __init_buf_ptrs(); 430 } 431 432#endif // _LIBCPP_STD_VER >= 26 433 434 basic_stringbuf(const basic_stringbuf&) = delete; 435 basic_stringbuf(basic_stringbuf&& __rhs) : __mode_(__rhs.__mode_) { __move_init(std::move(__rhs)); } 436 437#if _LIBCPP_STD_VER >= 20 438 _LIBCPP_HIDE_FROM_ABI basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a) 439 : basic_stringbuf(__rhs.__mode_, __a) { 440 __move_init(std::move(__rhs)); 441 } 442#endif 443 444 // [stringbuf.assign] Assign and swap: 445 basic_stringbuf& operator=(const basic_stringbuf&) = delete; 446 basic_stringbuf& operator=(basic_stringbuf&& __rhs); 447 void swap(basic_stringbuf& __rhs) 448#if _LIBCPP_STD_VER >= 20 449 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value || 450 allocator_traits<allocator_type>::is_always_equal::value) 451#endif 452 ; 453 454 // [stringbuf.members] Member functions: 455 456#if _LIBCPP_STD_VER >= 20 457 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const noexcept { return __str_.get_allocator(); } 458#endif 459 460#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 461 string_type str() const; 462#else 463 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const& { return str(__str_.get_allocator()); } 464 465 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { 466 const basic_string_view<_CharT, _Traits> __view = view(); 467 typename string_type::size_type __pos = __view.empty() ? 0 : __view.data() - __str_.data(); 468 // In C++23, this is just string_type(std::move(__str_), __pos, __view.size(), __str_.get_allocator()); 469 // But we need something that works in C++20 also. 470 string_type __result(std::move(__str_), __str_.get_allocator()); 471 __result.resize(__pos + __view.size()); 472 __result.erase(0, __pos); 473 __init_buf_ptrs(); 474 return __result; 475 } 476#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 477 478#if _LIBCPP_STD_VER >= 20 479 template <class _SAlloc> 480 requires __is_allocator<_SAlloc>::value 481 _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { 482 return basic_string<_CharT, _Traits, _SAlloc>(view(), __sa); 483 } 484 485 _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept; 486#endif // _LIBCPP_STD_VER >= 20 487 488 void str(const string_type& __s) { 489 __str_ = __s; 490 __init_buf_ptrs(); 491 } 492 493#if _LIBCPP_STD_VER >= 20 494 template <class _SAlloc> 495 requires(!is_same_v<_SAlloc, allocator_type>) 496 _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { 497 __str_ = __s; 498 __init_buf_ptrs(); 499 } 500 501 _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { 502 __str_ = std::move(__s); 503 __init_buf_ptrs(); 504 } 505#endif // _LIBCPP_STD_VER >= 20 506 507#if _LIBCPP_STD_VER >= 26 508 509 template <class _Tp> 510 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 511 _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { 512 basic_string_view<_CharT, _Traits> __sv = __t; 513 __str_ = __sv; 514 __init_buf_ptrs(); 515 } 516 517#endif // _LIBCPP_STD_VER >= 26 518 519protected: 520 // [stringbuf.virtuals] Overridden virtual functions: 521 int_type underflow() override; 522 int_type pbackfail(int_type __c = traits_type::eof()) override; 523 int_type overflow(int_type __c = traits_type::eof()) override; 524 pos_type 525 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override; 526 _LIBCPP_HIDE_FROM_ABI_VIRTUAL 527 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override { 528 return seekoff(__sp, ios_base::beg, __wch); 529 } 530}; 531 532template <class _CharT, class _Traits, class _Allocator> 533_LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__move_init(basic_stringbuf&& __rhs) { 534 char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); 535 ptrdiff_t __binp = -1; 536 ptrdiff_t __ninp = -1; 537 ptrdiff_t __einp = -1; 538 if (__rhs.eback() != nullptr) { 539 __binp = __rhs.eback() - __p; 540 __ninp = __rhs.gptr() - __p; 541 __einp = __rhs.egptr() - __p; 542 } 543 ptrdiff_t __bout = -1; 544 ptrdiff_t __nout = -1; 545 ptrdiff_t __eout = -1; 546 if (__rhs.pbase() != nullptr) { 547 __bout = __rhs.pbase() - __p; 548 __nout = __rhs.pptr() - __p; 549 __eout = __rhs.epptr() - __p; 550 } 551 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; 552 __str_ = std::move(__rhs.__str_); 553 __p = const_cast<char_type*>(__str_.data()); 554 if (__binp != -1) 555 this->setg(__p + __binp, __p + __ninp, __p + __einp); 556 if (__bout != -1) { 557 this->setp(__p + __bout, __p + __eout); 558 this->__pbump(__nout); 559 } 560 __hm_ = __hm == -1 ? nullptr : __p + __hm; 561 __p = const_cast<char_type*>(__rhs.__str_.data()); 562 __rhs.setg(__p, __p, __p); 563 __rhs.setp(__p, __p); 564 __rhs.__hm_ = __p; 565 this->pubimbue(__rhs.getloc()); 566} 567 568template <class _CharT, class _Traits, class _Allocator> 569basic_stringbuf<_CharT, _Traits, _Allocator>& 570basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs) { 571 char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); 572 ptrdiff_t __binp = -1; 573 ptrdiff_t __ninp = -1; 574 ptrdiff_t __einp = -1; 575 if (__rhs.eback() != nullptr) { 576 __binp = __rhs.eback() - __p; 577 __ninp = __rhs.gptr() - __p; 578 __einp = __rhs.egptr() - __p; 579 } 580 ptrdiff_t __bout = -1; 581 ptrdiff_t __nout = -1; 582 ptrdiff_t __eout = -1; 583 if (__rhs.pbase() != nullptr) { 584 __bout = __rhs.pbase() - __p; 585 __nout = __rhs.pptr() - __p; 586 __eout = __rhs.epptr() - __p; 587 } 588 ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; 589 __str_ = std::move(__rhs.__str_); 590 __p = const_cast<char_type*>(__str_.data()); 591 if (__binp != -1) 592 this->setg(__p + __binp, __p + __ninp, __p + __einp); 593 else 594 this->setg(nullptr, nullptr, nullptr); 595 if (__bout != -1) { 596 this->setp(__p + __bout, __p + __eout); 597 this->__pbump(__nout); 598 } else 599 this->setp(nullptr, nullptr); 600 601 __hm_ = __hm == -1 ? nullptr : __p + __hm; 602 __mode_ = __rhs.__mode_; 603 __p = const_cast<char_type*>(__rhs.__str_.data()); 604 __rhs.setg(__p, __p, __p); 605 __rhs.setp(__p, __p); 606 __rhs.__hm_ = __p; 607 this->pubimbue(__rhs.getloc()); 608 return *this; 609} 610 611template <class _CharT, class _Traits, class _Allocator> 612void basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs) 613#if _LIBCPP_STD_VER >= 20 614 noexcept(allocator_traits<_Allocator>::propagate_on_container_swap::value || 615 allocator_traits<_Allocator>::is_always_equal::value) 616#endif 617{ 618 char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); 619 ptrdiff_t __rbinp = -1; 620 ptrdiff_t __rninp = -1; 621 ptrdiff_t __reinp = -1; 622 if (__rhs.eback() != nullptr) { 623 __rbinp = __rhs.eback() - __p; 624 __rninp = __rhs.gptr() - __p; 625 __reinp = __rhs.egptr() - __p; 626 } 627 ptrdiff_t __rbout = -1; 628 ptrdiff_t __rnout = -1; 629 ptrdiff_t __reout = -1; 630 if (__rhs.pbase() != nullptr) { 631 __rbout = __rhs.pbase() - __p; 632 __rnout = __rhs.pptr() - __p; 633 __reout = __rhs.epptr() - __p; 634 } 635 ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; 636 __p = const_cast<char_type*>(__str_.data()); 637 ptrdiff_t __lbinp = -1; 638 ptrdiff_t __lninp = -1; 639 ptrdiff_t __leinp = -1; 640 if (this->eback() != nullptr) { 641 __lbinp = this->eback() - __p; 642 __lninp = this->gptr() - __p; 643 __leinp = this->egptr() - __p; 644 } 645 ptrdiff_t __lbout = -1; 646 ptrdiff_t __lnout = -1; 647 ptrdiff_t __leout = -1; 648 if (this->pbase() != nullptr) { 649 __lbout = this->pbase() - __p; 650 __lnout = this->pptr() - __p; 651 __leout = this->epptr() - __p; 652 } 653 ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p; 654 std::swap(__mode_, __rhs.__mode_); 655 __str_.swap(__rhs.__str_); 656 __p = const_cast<char_type*>(__str_.data()); 657 if (__rbinp != -1) 658 this->setg(__p + __rbinp, __p + __rninp, __p + __reinp); 659 else 660 this->setg(nullptr, nullptr, nullptr); 661 if (__rbout != -1) { 662 this->setp(__p + __rbout, __p + __reout); 663 this->__pbump(__rnout); 664 } else 665 this->setp(nullptr, nullptr); 666 __hm_ = __rhm == -1 ? nullptr : __p + __rhm; 667 __p = const_cast<char_type*>(__rhs.__str_.data()); 668 if (__lbinp != -1) 669 __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp); 670 else 671 __rhs.setg(nullptr, nullptr, nullptr); 672 if (__lbout != -1) { 673 __rhs.setp(__p + __lbout, __p + __leout); 674 __rhs.__pbump(__lnout); 675 } else 676 __rhs.setp(nullptr, nullptr); 677 __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm; 678 locale __tl = __rhs.getloc(); 679 __rhs.pubimbue(this->getloc()); 680 this->pubimbue(__tl); 681} 682 683template <class _CharT, class _Traits, class _Allocator> 684inline _LIBCPP_HIDE_FROM_ABI void 685swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x, basic_stringbuf<_CharT, _Traits, _Allocator>& __y) 686#if _LIBCPP_STD_VER >= 20 687 noexcept(noexcept(__x.swap(__y))) 688#endif 689{ 690 __x.swap(__y); 691} 692 693#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 694template <class _CharT, class _Traits, class _Allocator> 695basic_string<_CharT, _Traits, _Allocator> basic_stringbuf<_CharT, _Traits, _Allocator>::str() const { 696 if (__mode_ & ios_base::out) { 697 if (__hm_ < this->pptr()) 698 __hm_ = this->pptr(); 699 return string_type(this->pbase(), __hm_, __str_.get_allocator()); 700 } else if (__mode_ & ios_base::in) 701 return string_type(this->eback(), this->egptr(), __str_.get_allocator()); 702 return string_type(__str_.get_allocator()); 703} 704#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 705 706template <class _CharT, class _Traits, class _Allocator> 707_LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__init_buf_ptrs() { 708 __hm_ = nullptr; 709 char_type* __data = const_cast<char_type*>(__str_.data()); 710 typename string_type::size_type __sz = __str_.size(); 711 if (__mode_ & ios_base::in) { 712 __hm_ = __data + __sz; 713 this->setg(__data, __data, __hm_); 714 } 715 if (__mode_ & ios_base::out) { 716 __hm_ = __data + __sz; 717 __str_.resize(__str_.capacity()); 718 this->setp(__data, __data + __str_.size()); 719 if (__mode_ & (ios_base::app | ios_base::ate)) { 720 while (__sz > INT_MAX) { 721 this->pbump(INT_MAX); 722 __sz -= INT_MAX; 723 } 724 if (__sz > 0) 725 this->pbump(__sz); 726 } 727 } 728} 729 730#if _LIBCPP_STD_VER >= 20 731template <class _CharT, class _Traits, class _Allocator> 732_LIBCPP_HIDE_FROM_ABI basic_string_view<_CharT, _Traits> 733basic_stringbuf<_CharT, _Traits, _Allocator>::view() const noexcept { 734 if (__mode_ & ios_base::out) { 735 if (__hm_ < this->pptr()) 736 __hm_ = this->pptr(); 737 return basic_string_view<_CharT, _Traits>(this->pbase(), __hm_); 738 } else if (__mode_ & ios_base::in) 739 return basic_string_view<_CharT, _Traits>(this->eback(), this->egptr()); 740 return basic_string_view<_CharT, _Traits>(); 741} 742#endif // _LIBCPP_STD_VER >= 20 743 744template <class _CharT, class _Traits, class _Allocator> 745typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type 746basic_stringbuf<_CharT, _Traits, _Allocator>::underflow() { 747 if (__hm_ < this->pptr()) 748 __hm_ = this->pptr(); 749 if (__mode_ & ios_base::in) { 750 if (this->egptr() < __hm_) 751 this->setg(this->eback(), this->gptr(), __hm_); 752 if (this->gptr() < this->egptr()) 753 return traits_type::to_int_type(*this->gptr()); 754 } 755 return traits_type::eof(); 756} 757 758template <class _CharT, class _Traits, class _Allocator> 759typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type 760basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c) { 761 if (__hm_ < this->pptr()) 762 __hm_ = this->pptr(); 763 if (this->eback() < this->gptr()) { 764 if (traits_type::eq_int_type(__c, traits_type::eof())) { 765 this->setg(this->eback(), this->gptr() - 1, __hm_); 766 return traits_type::not_eof(__c); 767 } 768 if ((__mode_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) { 769 this->setg(this->eback(), this->gptr() - 1, __hm_); 770 *this->gptr() = traits_type::to_char_type(__c); 771 return __c; 772 } 773 } 774 return traits_type::eof(); 775} 776 777template <class _CharT, class _Traits, class _Allocator> 778typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type 779basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c) { 780 if (!traits_type::eq_int_type(__c, traits_type::eof())) { 781 ptrdiff_t __ninp = this->gptr() - this->eback(); 782 if (this->pptr() == this->epptr()) { 783 if (!(__mode_ & ios_base::out)) 784 return traits_type::eof(); 785#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 786 try { 787#endif // _LIBCPP_HAS_NO_EXCEPTIONS 788 ptrdiff_t __nout = this->pptr() - this->pbase(); 789 ptrdiff_t __hm = __hm_ - this->pbase(); 790 __str_.push_back(char_type()); 791 __str_.resize(__str_.capacity()); 792 char_type* __p = const_cast<char_type*>(__str_.data()); 793 this->setp(__p, __p + __str_.size()); 794 this->__pbump(__nout); 795 __hm_ = this->pbase() + __hm; 796#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 797 } catch (...) { 798 return traits_type::eof(); 799 } 800#endif // _LIBCPP_HAS_NO_EXCEPTIONS 801 } 802 __hm_ = std::max(this->pptr() + 1, __hm_); 803 if (__mode_ & ios_base::in) { 804 char_type* __p = const_cast<char_type*>(__str_.data()); 805 this->setg(__p, __p + __ninp, __hm_); 806 } 807 return this->sputc(traits_type::to_char_type(__c)); 808 } 809 return traits_type::not_eof(__c); 810} 811 812template <class _CharT, class _Traits, class _Allocator> 813typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff( 814 off_type __off, ios_base::seekdir __way, ios_base::openmode __wch) { 815 if (__hm_ < this->pptr()) 816 __hm_ = this->pptr(); 817 if ((__wch & (ios_base::in | ios_base::out)) == 0) 818 return pos_type(-1); 819 if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out) && __way == ios_base::cur) 820 return pos_type(-1); 821 const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data(); 822 off_type __noff; 823 switch (__way) { 824 case ios_base::beg: 825 __noff = 0; 826 break; 827 case ios_base::cur: 828 if (__wch & ios_base::in) 829 __noff = this->gptr() - this->eback(); 830 else 831 __noff = this->pptr() - this->pbase(); 832 break; 833 case ios_base::end: 834 __noff = __hm; 835 break; 836 default: 837 return pos_type(-1); 838 } 839 __noff += __off; 840 if (__noff < 0 || __hm < __noff) 841 return pos_type(-1); 842 if (__noff != 0) { 843 if ((__wch & ios_base::in) && this->gptr() == nullptr) 844 return pos_type(-1); 845 if ((__wch & ios_base::out) && this->pptr() == nullptr) 846 return pos_type(-1); 847 } 848 if (__wch & ios_base::in) 849 this->setg(this->eback(), this->eback() + __noff, __hm_); 850 if (__wch & ios_base::out) { 851 this->setp(this->pbase(), this->epptr()); 852 this->__pbump(__noff); 853 } 854 return pos_type(__noff); 855} 856 857// Class template basic_istringstream [istringstream] 858 859template <class _CharT, class _Traits, class _Allocator> 860class _LIBCPP_TEMPLATE_VIS basic_istringstream : public basic_istream<_CharT, _Traits> { 861public: 862 typedef _CharT char_type; 863 typedef _Traits traits_type; 864 typedef typename traits_type::int_type int_type; 865 typedef typename traits_type::pos_type pos_type; 866 typedef typename traits_type::off_type off_type; 867 typedef _Allocator allocator_type; 868 869 typedef basic_string<char_type, traits_type, allocator_type> string_type; 870 871private: 872 basic_stringbuf<char_type, traits_type, allocator_type> __sb_; 873 874public: 875 // [istringstream.cons] Constructors: 876 _LIBCPP_HIDE_FROM_ABI basic_istringstream() : basic_istream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in) {} 877 878 _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(ios_base::openmode __wch) 879 : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {} 880 881 _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const string_type& __s, ios_base::openmode __wch = ios_base::in) 882 : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch | ios_base::in) {} 883 884#if _LIBCPP_STD_VER >= 20 885 _LIBCPP_HIDE_FROM_ABI basic_istringstream(ios_base::openmode __wch, const _Allocator& __a) 886 : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::in, __a) {} 887 888 _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(string_type&& __s, ios_base::openmode __wch = ios_base::in) 889 : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::in) {} 890 891 template <class _SAlloc> 892 _LIBCPP_HIDE_FROM_ABI basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) 893 : basic_istringstream(__s, ios_base::in, __a) {} 894 895 template <class _SAlloc> 896 _LIBCPP_HIDE_FROM_ABI basic_istringstream( 897 const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) 898 : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in, __a) {} 899 900 template <class _SAlloc> 901 _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, 902 ios_base::openmode __wch = ios_base::in) 903 : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in) {} 904#endif // _LIBCPP_STD_VER >= 20 905 906#if _LIBCPP_STD_VER >= 26 907 908 template <class _Tp> 909 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 910 _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const _Tp& __t, ios_base::openmode __which = ios_base::in) 911 : basic_istringstream(__t, __which, _Allocator()) {} 912 913 template <class _Tp> 914 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 915 _LIBCPP_HIDE_FROM_ABI basic_istringstream(const _Tp& __t, const _Allocator& __a) 916 : basic_istringstream(__t, ios_base::in, __a) {} 917 918 template <class _Tp> 919 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 920 _LIBCPP_HIDE_FROM_ABI basic_istringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) 921 : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which | ios_base::in, __a) {} 922 923#endif // _LIBCPP_STD_VER >= 26 924 925 basic_istringstream(const basic_istringstream&) = delete; 926 _LIBCPP_HIDE_FROM_ABI basic_istringstream(basic_istringstream&& __rhs) 927 : basic_istream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 928 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); 929 } 930 931 // [istringstream.assign] Assign and swap: 932 basic_istringstream& operator=(const basic_istringstream&) = delete; 933 basic_istringstream& operator=(basic_istringstream&& __rhs) { 934 basic_istream<char_type, traits_type>::operator=(std::move(__rhs)); 935 __sb_ = std::move(__rhs.__sb_); 936 return *this; 937 } 938 _LIBCPP_HIDE_FROM_ABI void swap(basic_istringstream& __rhs) { 939 basic_istream<char_type, traits_type>::swap(__rhs); 940 __sb_.swap(__rhs.__sb_); 941 } 942 943 // [istringstream.members] Member functions: 944 _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { 945 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); 946 } 947 948#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 949 _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } 950#else 951 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const& { return __sb_.str(); } 952 953 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); } 954#endif 955 956#if _LIBCPP_STD_VER >= 20 957 template <class _SAlloc> 958 requires __is_allocator<_SAlloc>::value 959 _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { 960 return __sb_.str(__sa); 961 } 962 963 _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } 964#endif // _LIBCPP_STD_VER >= 20 965 966 _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } 967 968#if _LIBCPP_STD_VER >= 20 969 template <class _SAlloc> 970 _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { 971 __sb_.str(__s); 972 } 973 974 _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } 975#endif // _LIBCPP_STD_VER >= 20 976 977#if _LIBCPP_STD_VER >= 26 978 template <class _Tp> 979 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 980 _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { 981 rdbuf()->str(__t); 982 } 983#endif // _LIBCPP_STD_VER >= 26 984}; 985 986template <class _CharT, class _Traits, class _Allocator> 987inline _LIBCPP_HIDE_FROM_ABI void 988swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x, basic_istringstream<_CharT, _Traits, _Allocator>& __y) { 989 __x.swap(__y); 990} 991 992// Class template basic_ostringstream [ostringstream] 993 994template <class _CharT, class _Traits, class _Allocator> 995class _LIBCPP_TEMPLATE_VIS basic_ostringstream : public basic_ostream<_CharT, _Traits> { 996public: 997 typedef _CharT char_type; 998 typedef _Traits traits_type; 999 typedef typename traits_type::int_type int_type; 1000 typedef typename traits_type::pos_type pos_type; 1001 typedef typename traits_type::off_type off_type; 1002 typedef _Allocator allocator_type; 1003 1004 typedef basic_string<char_type, traits_type, allocator_type> string_type; 1005 1006private: 1007 basic_stringbuf<char_type, traits_type, allocator_type> __sb_; 1008 1009public: 1010 // [ostringstream.cons] Constructors: 1011 _LIBCPP_HIDE_FROM_ABI basic_ostringstream() : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::out) {} 1012 1013 _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(ios_base::openmode __wch) 1014 : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::out) {} 1015 1016 _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const string_type& __s, ios_base::openmode __wch = ios_base::out) 1017 : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch | ios_base::out) {} 1018 1019#if _LIBCPP_STD_VER >= 20 1020 _LIBCPP_HIDE_FROM_ABI basic_ostringstream(ios_base::openmode __wch, const _Allocator& __a) 1021 : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::out, __a) {} 1022 1023 _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(string_type&& __s, ios_base::openmode __wch = ios_base::out) 1024 : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::out) {} 1025 1026 template <class _SAlloc> 1027 _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) 1028 : basic_ostringstream(__s, ios_base::out, __a) {} 1029 1030 template <class _SAlloc> 1031 _LIBCPP_HIDE_FROM_ABI basic_ostringstream( 1032 const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) 1033 : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out, __a) {} 1034 1035 template <class _SAlloc> 1036 requires(!is_same_v<_SAlloc, allocator_type>) 1037 _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, 1038 ios_base::openmode __wch = ios_base::out) 1039 : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out) {} 1040#endif // _LIBCPP_STD_VER >= 20 1041 1042#if _LIBCPP_STD_VER >= 26 1043 1044 template <class _Tp> 1045 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1046 _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const _Tp& __t, ios_base::openmode __which = ios_base::out) 1047 : basic_ostringstream(__t, __which | ios_base::out, _Allocator()) {} 1048 1049 template <class _Tp> 1050 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1051 _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const _Tp& __t, const _Allocator& __a) 1052 : basic_ostringstream(__t, ios_base::out, __a) {} 1053 1054 template <class _Tp> 1055 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1056 _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) 1057 : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which | ios_base::out, __a) {} 1058 1059#endif // _LIBCPP_STD_VER >= 26 1060 1061 basic_ostringstream(const basic_ostringstream&) = delete; 1062 _LIBCPP_HIDE_FROM_ABI basic_ostringstream(basic_ostringstream&& __rhs) 1063 : basic_ostream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 1064 basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_); 1065 } 1066 1067 // [ostringstream.assign] Assign and swap: 1068 basic_ostringstream& operator=(const basic_ostringstream&) = delete; 1069 basic_ostringstream& operator=(basic_ostringstream&& __rhs) { 1070 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs)); 1071 __sb_ = std::move(__rhs.__sb_); 1072 return *this; 1073 } 1074 1075 _LIBCPP_HIDE_FROM_ABI void swap(basic_ostringstream& __rhs) { 1076 basic_ostream<char_type, traits_type>::swap(__rhs); 1077 __sb_.swap(__rhs.__sb_); 1078 } 1079 1080 // [ostringstream.members] Member functions: 1081 _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { 1082 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); 1083 } 1084 1085#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 1086 _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } 1087#else 1088 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const& { return __sb_.str(); } 1089 1090 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); } 1091#endif 1092 1093#if _LIBCPP_STD_VER >= 20 1094 template <class _SAlloc> 1095 requires __is_allocator<_SAlloc>::value 1096 _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { 1097 return __sb_.str(__sa); 1098 } 1099 1100 _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } 1101#endif // _LIBCPP_STD_VER >= 20 1102 1103 _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } 1104 1105#if _LIBCPP_STD_VER >= 20 1106 template <class _SAlloc> 1107 _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { 1108 __sb_.str(__s); 1109 } 1110 1111 _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } 1112#endif // _LIBCPP_STD_VER >= 20 1113 1114#if _LIBCPP_STD_VER >= 26 1115 template <class _Tp> 1116 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1117 _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { 1118 rdbuf()->str(__t); 1119 } 1120#endif // _LIBCPP_STD_VER >= 26 1121}; 1122 1123template <class _CharT, class _Traits, class _Allocator> 1124inline _LIBCPP_HIDE_FROM_ABI void 1125swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x, basic_ostringstream<_CharT, _Traits, _Allocator>& __y) { 1126 __x.swap(__y); 1127} 1128 1129// Class template basic_stringstream [stringstream] 1130 1131template <class _CharT, class _Traits, class _Allocator> 1132class _LIBCPP_TEMPLATE_VIS basic_stringstream : public basic_iostream<_CharT, _Traits> { 1133public: 1134 typedef _CharT char_type; 1135 typedef _Traits traits_type; 1136 typedef typename traits_type::int_type int_type; 1137 typedef typename traits_type::pos_type pos_type; 1138 typedef typename traits_type::off_type off_type; 1139 typedef _Allocator allocator_type; 1140 1141 typedef basic_string<char_type, traits_type, allocator_type> string_type; 1142 1143private: 1144 basic_stringbuf<char_type, traits_type, allocator_type> __sb_; 1145 1146public: 1147 // [stringstream.cons] constructors 1148 _LIBCPP_HIDE_FROM_ABI basic_stringstream() 1149 : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in | ios_base::out) {} 1150 1151 _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(ios_base::openmode __wch) 1152 : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {} 1153 1154 _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const string_type& __s, 1155 ios_base::openmode __wch = ios_base::in | ios_base::out) 1156 : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch) {} 1157 1158#if _LIBCPP_STD_VER >= 20 1159 _LIBCPP_HIDE_FROM_ABI basic_stringstream(ios_base::openmode __wch, const _Allocator& __a) 1160 : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch, __a) {} 1161 1162 _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(string_type&& __s, 1163 ios_base::openmode __wch = ios_base::out | ios_base::in) 1164 : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch) {} 1165 1166 template <class _SAlloc> 1167 _LIBCPP_HIDE_FROM_ABI basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) 1168 : basic_stringstream(__s, ios_base::out | ios_base::in, __a) {} 1169 1170 template <class _SAlloc> 1171 _LIBCPP_HIDE_FROM_ABI 1172 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) 1173 : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch, __a) {} 1174 1175 template <class _SAlloc> 1176 requires(!is_same_v<_SAlloc, allocator_type>) 1177 _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, 1178 ios_base::openmode __wch = ios_base::out | ios_base::in) 1179 : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch) {} 1180#endif // _LIBCPP_STD_VER >= 20 1181 1182#if _LIBCPP_STD_VER >= 26 1183 1184 template <class _Tp> 1185 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1186 _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const _Tp& __t, 1187 ios_base::openmode __which = ios_base::out | ios_base::in) 1188 : basic_stringstream(__t, __which, _Allocator()) {} 1189 1190 template <class _Tp> 1191 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1192 _LIBCPP_HIDE_FROM_ABI basic_stringstream(const _Tp& __t, const _Allocator& __a) 1193 : basic_stringstream(__t, ios_base::out | ios_base::in, __a) {} 1194 1195 template <class _Tp> 1196 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1197 _LIBCPP_HIDE_FROM_ABI basic_stringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) 1198 : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which, __a) {} 1199 1200#endif // _LIBCPP_STD_VER >= 26 1201 1202 basic_stringstream(const basic_stringstream&) = delete; 1203 _LIBCPP_HIDE_FROM_ABI basic_stringstream(basic_stringstream&& __rhs) 1204 : basic_iostream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 1205 basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); 1206 } 1207 1208 // [stringstream.assign] Assign and swap: 1209 basic_stringstream& operator=(const basic_stringstream&) = delete; 1210 basic_stringstream& operator=(basic_stringstream&& __rhs) { 1211 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs)); 1212 __sb_ = std::move(__rhs.__sb_); 1213 return *this; 1214 } 1215 _LIBCPP_HIDE_FROM_ABI void swap(basic_stringstream& __rhs) { 1216 basic_iostream<char_type, traits_type>::swap(__rhs); 1217 __sb_.swap(__rhs.__sb_); 1218 } 1219 1220 // [stringstream.members] Member functions: 1221 _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { 1222 return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); 1223 } 1224 1225#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) 1226 _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } 1227#else 1228 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() const& { return __sb_.str(); } 1229 1230 _LIBCPP_HIDE_FROM_ABI_SSTREAM string_type str() && { return std::move(__sb_).str(); } 1231#endif 1232 1233#if _LIBCPP_STD_VER >= 20 1234 template <class _SAlloc> 1235 requires __is_allocator<_SAlloc>::value 1236 _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { 1237 return __sb_.str(__sa); 1238 } 1239 1240 _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } 1241#endif // _LIBCPP_STD_VER >= 20 1242 1243 _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } 1244 1245#if _LIBCPP_STD_VER >= 20 1246 template <class _SAlloc> 1247 _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { 1248 __sb_.str(__s); 1249 } 1250 1251 _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } 1252#endif // _LIBCPP_STD_VER >= 20 1253 1254#if _LIBCPP_STD_VER >= 26 1255 template <class _Tp> 1256 requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> 1257 _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { 1258 rdbuf()->str(__t); 1259 } 1260#endif // _LIBCPP_STD_VER >= 26 1261}; 1262 1263template <class _CharT, class _Traits, class _Allocator> 1264inline _LIBCPP_HIDE_FROM_ABI void 1265swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x, basic_stringstream<_CharT, _Traits, _Allocator>& __y) { 1266 __x.swap(__y); 1267} 1268 1269#if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 1270extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>; 1271extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>; 1272extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>; 1273extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>; 1274#endif 1275 1276_LIBCPP_END_NAMESPACE_STD 1277 1278_LIBCPP_POP_MACROS 1279 1280#if _LIBCPP_STD_VER <= 20 && !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) 1281# include <type_traits> 1282#endif 1283 1284#endif // _LIBCPP_SSTREAM 1285