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] |
19 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
20 | class basic_stringbuf |
21 | : public basic_streambuf<charT, traits> |
22 | { |
23 | public: |
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 | |
82 | protected: |
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 |
95 | template <class charT, class traits, class Allocator> |
96 | void swap(basic_stringbuf<charT, traits, Allocator>& x, |
97 | basic_stringbuf<charT, traits, Allocator>& y); // conditionally noexcept since C++20 |
98 | |
99 | typedef basic_stringbuf<char> stringbuf; |
100 | typedef basic_stringbuf<wchar_t> wstringbuf; |
101 | |
102 | // Class template basic_istringstream [istringstream] |
103 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
104 | class basic_istringstream |
105 | : public basic_istream<charT, traits> |
106 | { |
107 | public: |
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 | |
163 | template <class charT, class traits, class Allocator> |
164 | void swap(basic_istringstream<charT, traits, Allocator>& x, |
165 | basic_istringstream<charT, traits, Allocator>& y); |
166 | |
167 | typedef basic_istringstream<char> istringstream; |
168 | typedef basic_istringstream<wchar_t> wistringstream; |
169 | |
170 | // Class template basic_ostringstream [ostringstream] |
171 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
172 | class basic_ostringstream |
173 | : public basic_ostream<charT, traits> |
174 | { |
175 | public: |
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 | |
232 | template <class charT, class traits, class Allocator> |
233 | void swap(basic_ostringstream<charT, traits, Allocator>& x, |
234 | basic_ostringstream<charT, traits, Allocator>& y); |
235 | |
236 | typedef basic_ostringstream<char> ostringstream; |
237 | typedef basic_ostringstream<wchar_t> wostringstream; |
238 | |
239 | // Class template basic_stringstream [stringstream] |
240 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
241 | class basic_stringstream |
242 | : public basic_iostream<charT, traits> |
243 | { |
244 | public: |
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 | |
302 | template <class charT, class traits, class Allocator> |
303 | void swap(basic_stringstream<charT, traits, Allocator>& x, |
304 | basic_stringstream<charT, traits, Allocator>& y); |
305 | |
306 | typedef basic_stringstream<char> stringstream; |
307 | typedef basic_stringstream<wchar_t> wstringstream; |
308 | |
309 | } // std |
310 | |
311 | */ |
312 | |
313 | // clang-format on |
314 | |
315 | #include <__config> |
316 | #include <__fwd/sstream.h> |
317 | #include <__ostream/basic_ostream.h> |
318 | #include <__type_traits/is_convertible.h> |
319 | #include <__utility/swap.h> |
320 | #include <istream> |
321 | #include <string> |
322 | #include <string_view> |
323 | #include <version> |
324 | |
325 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
326 | # pragma GCC system_header |
327 | #endif |
328 | |
329 | _LIBCPP_PUSH_MACROS |
330 | #include <__undef_macros> |
331 | |
332 | _LIBCPP_BEGIN_NAMESPACE_STD |
333 | |
334 | // Class template basic_stringbuf [stringbuf] |
335 | |
336 | template <class _CharT, class _Traits, class _Allocator> |
337 | class _LIBCPP_TEMPLATE_VIS basic_stringbuf : public basic_streambuf<_CharT, _Traits> { |
338 | public: |
339 | typedef _CharT char_type; |
340 | typedef _Traits traits_type; |
341 | typedef typename traits_type::int_type int_type; |
342 | typedef typename traits_type::pos_type pos_type; |
343 | typedef typename traits_type::off_type off_type; |
344 | typedef _Allocator allocator_type; |
345 | |
346 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
347 | |
348 | private: |
349 | string_type __str_; |
350 | mutable char_type* __hm_; |
351 | ios_base::openmode __mode_; |
352 | _LIBCPP_HIDE_FROM_ABI void __init_buf_ptrs(); |
353 | _LIBCPP_HIDE_FROM_ABI void __move_init(basic_stringbuf&& __rhs); |
354 | |
355 | public: |
356 | // [stringbuf.cons] constructors: |
357 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf() : __hm_(nullptr), __mode_(ios_base::in | ios_base::out) {} |
358 | |
359 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(ios_base::openmode __wch) : __hm_(nullptr), __mode_(__wch) {} |
360 | |
361 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const string_type& __s, |
362 | ios_base::openmode __wch = ios_base::in | ios_base::out) |
363 | : __str_(__s.get_allocator()), __hm_(nullptr), __mode_(__wch) { |
364 | str(__s); |
365 | } |
366 | |
367 | #if _LIBCPP_STD_VER >= 20 |
368 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const allocator_type& __a) |
369 | : basic_stringbuf(ios_base::in | ios_base::out, __a) {} |
370 | |
371 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf(ios_base::openmode __wch, const allocator_type& __a) |
372 | : __str_(__a), __hm_(nullptr), __mode_(__wch) {} |
373 | |
374 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(string_type&& __s, |
375 | ios_base::openmode __wch = ios_base::in | ios_base::out) |
376 | : __str_(std::move(__s)), __hm_(nullptr), __mode_(__wch) { |
377 | __init_buf_ptrs(); |
378 | } |
379 | |
380 | template <class _SAlloc> |
381 | _LIBCPP_HIDE_FROM_ABI |
382 | basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s, const allocator_type& __a) |
383 | : basic_stringbuf(__s, ios_base::in | ios_base::out, __a) {} |
384 | |
385 | template <class _SAlloc> |
386 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf( |
387 | const basic_string<char_type, traits_type, _SAlloc>& __s, ios_base::openmode __wch, const allocator_type& __a) |
388 | : __str_(__s, __a), __hm_(nullptr), __mode_(__wch) { |
389 | __init_buf_ptrs(); |
390 | } |
391 | |
392 | template <class _SAlloc> |
393 | requires(!is_same_v<_SAlloc, allocator_type>) |
394 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const basic_string<char_type, traits_type, _SAlloc>& __s, |
395 | ios_base::openmode __wch = ios_base::in | ios_base::out) |
396 | : __str_(__s), __hm_(nullptr), __mode_(__wch) { |
397 | __init_buf_ptrs(); |
398 | } |
399 | #endif // _LIBCPP_STD_VER >= 20 |
400 | |
401 | #if _LIBCPP_STD_VER >= 26 |
402 | |
403 | template <class _Tp> |
404 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
405 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringbuf(const _Tp& __t, |
406 | ios_base::openmode __which = ios_base::in | ios_base::out) |
407 | : basic_stringbuf(__t, __which, _Allocator()) {} |
408 | |
409 | template <class _Tp> |
410 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
411 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf(const _Tp& __t, const _Allocator& __a) |
412 | : basic_stringbuf(__t, ios_base::in | ios_base::out, __a) {} |
413 | |
414 | template <class _Tp> |
415 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
416 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) |
417 | : __hm_(nullptr), __mode_(__which) { |
418 | basic_string_view<_CharT, _Traits> __sv = __t; |
419 | __str_ = string_type(__sv, __a); |
420 | __init_buf_ptrs(); |
421 | } |
422 | |
423 | #endif // _LIBCPP_STD_VER >= 26 |
424 | |
425 | basic_stringbuf(const basic_stringbuf&) = delete; |
426 | basic_stringbuf(basic_stringbuf&& __rhs) : __mode_(__rhs.__mode_) { __move_init(std::move(__rhs)); } |
427 | |
428 | #if _LIBCPP_STD_VER >= 20 |
429 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a) |
430 | : basic_stringbuf(__rhs.__mode_, __a) { |
431 | __move_init(std::move(__rhs)); |
432 | } |
433 | #endif |
434 | |
435 | // [stringbuf.assign] Assign and swap: |
436 | basic_stringbuf& operator=(const basic_stringbuf&) = delete; |
437 | basic_stringbuf& operator=(basic_stringbuf&& __rhs); |
438 | void swap(basic_stringbuf& __rhs) |
439 | #if _LIBCPP_STD_VER >= 20 |
440 | noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value || |
441 | allocator_traits<allocator_type>::is_always_equal::value) |
442 | #endif |
443 | ; |
444 | |
445 | // [stringbuf.members] Member functions: |
446 | |
447 | #if _LIBCPP_STD_VER >= 20 |
448 | _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const noexcept { return __str_.get_allocator(); } |
449 | #endif |
450 | |
451 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
452 | string_type str() const; |
453 | #else |
454 | _LIBCPP_HIDE_FROM_ABI string_type str() const& { return str(__str_.get_allocator()); } |
455 | |
456 | _LIBCPP_HIDE_FROM_ABI string_type str() && { |
457 | const basic_string_view<_CharT, _Traits> __view = view(); |
458 | typename string_type::size_type __pos = __view.empty() ? 0 : __view.data() - __str_.data(); |
459 | // In C++23, this is just string_type(std::move(__str_), __pos, __view.size(), __str_.get_allocator()); |
460 | // But we need something that works in C++20 also. |
461 | string_type __result(std::move(__str_), __str_.get_allocator()); |
462 | __result.resize(__pos + __view.size()); |
463 | __result.erase(0, __pos); |
464 | __init_buf_ptrs(); |
465 | return __result; |
466 | } |
467 | #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
468 | |
469 | #if _LIBCPP_STD_VER >= 20 |
470 | template <class _SAlloc> |
471 | requires __is_allocator<_SAlloc>::value |
472 | _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { |
473 | return basic_string<_CharT, _Traits, _SAlloc>(view(), __sa); |
474 | } |
475 | |
476 | _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept; |
477 | #endif // _LIBCPP_STD_VER >= 20 |
478 | |
479 | void str(const string_type& __s) { |
480 | __str_ = __s; |
481 | __init_buf_ptrs(); |
482 | } |
483 | |
484 | #if _LIBCPP_STD_VER >= 20 |
485 | template <class _SAlloc> |
486 | requires(!is_same_v<_SAlloc, allocator_type>) |
487 | _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { |
488 | __str_ = __s; |
489 | __init_buf_ptrs(); |
490 | } |
491 | |
492 | _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { |
493 | __str_ = std::move(__s); |
494 | __init_buf_ptrs(); |
495 | } |
496 | #endif // _LIBCPP_STD_VER >= 20 |
497 | |
498 | #if _LIBCPP_STD_VER >= 26 |
499 | |
500 | template <class _Tp> |
501 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
502 | _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { |
503 | basic_string_view<_CharT, _Traits> __sv = __t; |
504 | __str_ = __sv; |
505 | __init_buf_ptrs(); |
506 | } |
507 | |
508 | #endif // _LIBCPP_STD_VER >= 26 |
509 | |
510 | protected: |
511 | // [stringbuf.virtuals] Overridden virtual functions: |
512 | int_type underflow() override; |
513 | int_type pbackfail(int_type __c = traits_type::eof()) override; |
514 | int_type overflow(int_type __c = traits_type::eof()) override; |
515 | pos_type |
516 | seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override; |
517 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL |
518 | pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override { |
519 | return seekoff(__sp, ios_base::beg, __wch); |
520 | } |
521 | }; |
522 | |
523 | template <class _CharT, class _Traits, class _Allocator> |
524 | _LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__move_init(basic_stringbuf&& __rhs) { |
525 | char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); |
526 | ptrdiff_t __binp = -1; |
527 | ptrdiff_t __ninp = -1; |
528 | ptrdiff_t __einp = -1; |
529 | if (__rhs.eback() != nullptr) { |
530 | __binp = __rhs.eback() - __p; |
531 | __ninp = __rhs.gptr() - __p; |
532 | __einp = __rhs.egptr() - __p; |
533 | } |
534 | ptrdiff_t __bout = -1; |
535 | ptrdiff_t __nout = -1; |
536 | ptrdiff_t __eout = -1; |
537 | if (__rhs.pbase() != nullptr) { |
538 | __bout = __rhs.pbase() - __p; |
539 | __nout = __rhs.pptr() - __p; |
540 | __eout = __rhs.epptr() - __p; |
541 | } |
542 | ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; |
543 | __str_ = std::move(__rhs.__str_); |
544 | __p = const_cast<char_type*>(__str_.data()); |
545 | if (__binp != -1) |
546 | this->setg(__p + __binp, __p + __ninp, __p + __einp); |
547 | if (__bout != -1) { |
548 | this->setp(__p + __bout, __p + __eout); |
549 | this->__pbump(__nout); |
550 | } |
551 | __hm_ = __hm == -1 ? nullptr : __p + __hm; |
552 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
553 | __rhs.setg(__p, __p, __p); |
554 | __rhs.setp(__p, __p); |
555 | __rhs.__hm_ = __p; |
556 | this->pubimbue(__rhs.getloc()); |
557 | } |
558 | |
559 | template <class _CharT, class _Traits, class _Allocator> |
560 | basic_stringbuf<_CharT, _Traits, _Allocator>& |
561 | basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs) { |
562 | char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); |
563 | ptrdiff_t __binp = -1; |
564 | ptrdiff_t __ninp = -1; |
565 | ptrdiff_t __einp = -1; |
566 | if (__rhs.eback() != nullptr) { |
567 | __binp = __rhs.eback() - __p; |
568 | __ninp = __rhs.gptr() - __p; |
569 | __einp = __rhs.egptr() - __p; |
570 | } |
571 | ptrdiff_t __bout = -1; |
572 | ptrdiff_t __nout = -1; |
573 | ptrdiff_t __eout = -1; |
574 | if (__rhs.pbase() != nullptr) { |
575 | __bout = __rhs.pbase() - __p; |
576 | __nout = __rhs.pptr() - __p; |
577 | __eout = __rhs.epptr() - __p; |
578 | } |
579 | ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; |
580 | __str_ = std::move(__rhs.__str_); |
581 | __p = const_cast<char_type*>(__str_.data()); |
582 | if (__binp != -1) |
583 | this->setg(__p + __binp, __p + __ninp, __p + __einp); |
584 | else |
585 | this->setg(nullptr, nullptr, nullptr); |
586 | if (__bout != -1) { |
587 | this->setp(__p + __bout, __p + __eout); |
588 | this->__pbump(__nout); |
589 | } else |
590 | this->setp(nullptr, nullptr); |
591 | |
592 | __hm_ = __hm == -1 ? nullptr : __p + __hm; |
593 | __mode_ = __rhs.__mode_; |
594 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
595 | __rhs.setg(__p, __p, __p); |
596 | __rhs.setp(__p, __p); |
597 | __rhs.__hm_ = __p; |
598 | this->pubimbue(__rhs.getloc()); |
599 | return *this; |
600 | } |
601 | |
602 | template <class _CharT, class _Traits, class _Allocator> |
603 | void basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs) |
604 | #if _LIBCPP_STD_VER >= 20 |
605 | noexcept(allocator_traits<_Allocator>::propagate_on_container_swap::value || |
606 | allocator_traits<_Allocator>::is_always_equal::value) |
607 | #endif |
608 | { |
609 | char_type* __p = const_cast<char_type*>(__rhs.__str_.data()); |
610 | ptrdiff_t __rbinp = -1; |
611 | ptrdiff_t __rninp = -1; |
612 | ptrdiff_t __reinp = -1; |
613 | if (__rhs.eback() != nullptr) { |
614 | __rbinp = __rhs.eback() - __p; |
615 | __rninp = __rhs.gptr() - __p; |
616 | __reinp = __rhs.egptr() - __p; |
617 | } |
618 | ptrdiff_t __rbout = -1; |
619 | ptrdiff_t __rnout = -1; |
620 | ptrdiff_t __reout = -1; |
621 | if (__rhs.pbase() != nullptr) { |
622 | __rbout = __rhs.pbase() - __p; |
623 | __rnout = __rhs.pptr() - __p; |
624 | __reout = __rhs.epptr() - __p; |
625 | } |
626 | ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p; |
627 | __p = const_cast<char_type*>(__str_.data()); |
628 | ptrdiff_t __lbinp = -1; |
629 | ptrdiff_t __lninp = -1; |
630 | ptrdiff_t __leinp = -1; |
631 | if (this->eback() != nullptr) { |
632 | __lbinp = this->eback() - __p; |
633 | __lninp = this->gptr() - __p; |
634 | __leinp = this->egptr() - __p; |
635 | } |
636 | ptrdiff_t __lbout = -1; |
637 | ptrdiff_t __lnout = -1; |
638 | ptrdiff_t __leout = -1; |
639 | if (this->pbase() != nullptr) { |
640 | __lbout = this->pbase() - __p; |
641 | __lnout = this->pptr() - __p; |
642 | __leout = this->epptr() - __p; |
643 | } |
644 | ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p; |
645 | std::swap(__mode_, __rhs.__mode_); |
646 | __str_.swap(__rhs.__str_); |
647 | __p = const_cast<char_type*>(__str_.data()); |
648 | if (__rbinp != -1) |
649 | this->setg(__p + __rbinp, __p + __rninp, __p + __reinp); |
650 | else |
651 | this->setg(nullptr, nullptr, nullptr); |
652 | if (__rbout != -1) { |
653 | this->setp(__p + __rbout, __p + __reout); |
654 | this->__pbump(__rnout); |
655 | } else |
656 | this->setp(nullptr, nullptr); |
657 | __hm_ = __rhm == -1 ? nullptr : __p + __rhm; |
658 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
659 | if (__lbinp != -1) |
660 | __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp); |
661 | else |
662 | __rhs.setg(nullptr, nullptr, nullptr); |
663 | if (__lbout != -1) { |
664 | __rhs.setp(__p + __lbout, __p + __leout); |
665 | __rhs.__pbump(__lnout); |
666 | } else |
667 | __rhs.setp(nullptr, nullptr); |
668 | __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm; |
669 | locale __tl = __rhs.getloc(); |
670 | __rhs.pubimbue(this->getloc()); |
671 | this->pubimbue(__tl); |
672 | } |
673 | |
674 | template <class _CharT, class _Traits, class _Allocator> |
675 | inline _LIBCPP_HIDE_FROM_ABI void |
676 | swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x, basic_stringbuf<_CharT, _Traits, _Allocator>& __y) |
677 | #if _LIBCPP_STD_VER >= 20 |
678 | noexcept(noexcept(__x.swap(__y))) |
679 | #endif |
680 | { |
681 | __x.swap(__y); |
682 | } |
683 | |
684 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
685 | template <class _CharT, class _Traits, class _Allocator> |
686 | basic_string<_CharT, _Traits, _Allocator> basic_stringbuf<_CharT, _Traits, _Allocator>::str() const { |
687 | if (__mode_ & ios_base::out) { |
688 | if (__hm_ < this->pptr()) |
689 | __hm_ = this->pptr(); |
690 | return string_type(this->pbase(), __hm_, __str_.get_allocator()); |
691 | } else if (__mode_ & ios_base::in) |
692 | return string_type(this->eback(), this->egptr(), __str_.get_allocator()); |
693 | return string_type(__str_.get_allocator()); |
694 | } |
695 | #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
696 | |
697 | template <class _CharT, class _Traits, class _Allocator> |
698 | _LIBCPP_HIDE_FROM_ABI void basic_stringbuf<_CharT, _Traits, _Allocator>::__init_buf_ptrs() { |
699 | __hm_ = nullptr; |
700 | char_type* __data = const_cast<char_type*>(__str_.data()); |
701 | typename string_type::size_type __sz = __str_.size(); |
702 | if (__mode_ & ios_base::in) { |
703 | __hm_ = __data + __sz; |
704 | this->setg(__data, __data, __hm_); |
705 | } |
706 | if (__mode_ & ios_base::out) { |
707 | __hm_ = __data + __sz; |
708 | __str_.resize(__str_.capacity()); |
709 | this->setp(__data, __data + __str_.size()); |
710 | if (__mode_ & (ios_base::app | ios_base::ate)) { |
711 | while (__sz > INT_MAX) { |
712 | this->pbump(INT_MAX); |
713 | __sz -= INT_MAX; |
714 | } |
715 | if (__sz > 0) |
716 | this->pbump(__sz); |
717 | } |
718 | } |
719 | } |
720 | |
721 | #if _LIBCPP_STD_VER >= 20 |
722 | template <class _CharT, class _Traits, class _Allocator> |
723 | _LIBCPP_HIDE_FROM_ABI basic_string_view<_CharT, _Traits> |
724 | basic_stringbuf<_CharT, _Traits, _Allocator>::view() const noexcept { |
725 | if (__mode_ & ios_base::out) { |
726 | if (__hm_ < this->pptr()) |
727 | __hm_ = this->pptr(); |
728 | return basic_string_view<_CharT, _Traits>(this->pbase(), __hm_); |
729 | } else if (__mode_ & ios_base::in) |
730 | return basic_string_view<_CharT, _Traits>(this->eback(), this->egptr()); |
731 | return basic_string_view<_CharT, _Traits>(); |
732 | } |
733 | #endif // _LIBCPP_STD_VER >= 20 |
734 | |
735 | template <class _CharT, class _Traits, class _Allocator> |
736 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
737 | basic_stringbuf<_CharT, _Traits, _Allocator>::underflow() { |
738 | if (__hm_ < this->pptr()) |
739 | __hm_ = this->pptr(); |
740 | if (__mode_ & ios_base::in) { |
741 | if (this->egptr() < __hm_) |
742 | this->setg(this->eback(), this->gptr(), __hm_); |
743 | if (this->gptr() < this->egptr()) |
744 | return traits_type::to_int_type(*this->gptr()); |
745 | } |
746 | return traits_type::eof(); |
747 | } |
748 | |
749 | template <class _CharT, class _Traits, class _Allocator> |
750 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
751 | basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c) { |
752 | if (__hm_ < this->pptr()) |
753 | __hm_ = this->pptr(); |
754 | if (this->eback() < this->gptr()) { |
755 | if (traits_type::eq_int_type(__c, traits_type::eof())) { |
756 | this->setg(this->eback(), this->gptr() - 1, __hm_); |
757 | return traits_type::not_eof(__c); |
758 | } |
759 | if ((__mode_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) { |
760 | this->setg(this->eback(), this->gptr() - 1, __hm_); |
761 | *this->gptr() = traits_type::to_char_type(__c); |
762 | return __c; |
763 | } |
764 | } |
765 | return traits_type::eof(); |
766 | } |
767 | |
768 | template <class _CharT, class _Traits, class _Allocator> |
769 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
770 | basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c) { |
771 | if (!traits_type::eq_int_type(__c, traits_type::eof())) { |
772 | ptrdiff_t __ninp = this->gptr() - this->eback(); |
773 | if (this->pptr() == this->epptr()) { |
774 | if (!(__mode_ & ios_base::out)) |
775 | return traits_type::eof(); |
776 | #ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
777 | try { |
778 | #endif // _LIBCPP_HAS_NO_EXCEPTIONS |
779 | ptrdiff_t __nout = this->pptr() - this->pbase(); |
780 | ptrdiff_t __hm = __hm_ - this->pbase(); |
781 | __str_.push_back(char_type()); |
782 | __str_.resize(__str_.capacity()); |
783 | char_type* __p = const_cast<char_type*>(__str_.data()); |
784 | this->setp(__p, __p + __str_.size()); |
785 | this->__pbump(__nout); |
786 | __hm_ = this->pbase() + __hm; |
787 | #ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
788 | } catch (...) { |
789 | return traits_type::eof(); |
790 | } |
791 | #endif // _LIBCPP_HAS_NO_EXCEPTIONS |
792 | } |
793 | __hm_ = std::max(this->pptr() + 1, __hm_); |
794 | if (__mode_ & ios_base::in) { |
795 | char_type* __p = const_cast<char_type*>(__str_.data()); |
796 | this->setg(__p, __p + __ninp, __hm_); |
797 | } |
798 | return this->sputc(traits_type::to_char_type(__c)); |
799 | } |
800 | return traits_type::not_eof(__c); |
801 | } |
802 | |
803 | template <class _CharT, class _Traits, class _Allocator> |
804 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff( |
805 | off_type __off, ios_base::seekdir __way, ios_base::openmode __wch) { |
806 | if (__hm_ < this->pptr()) |
807 | __hm_ = this->pptr(); |
808 | if ((__wch & (ios_base::in | ios_base::out)) == 0) |
809 | return pos_type(-1); |
810 | if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out) && __way == ios_base::cur) |
811 | return pos_type(-1); |
812 | const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data(); |
813 | off_type __noff; |
814 | switch (__way) { |
815 | case ios_base::beg: |
816 | __noff = 0; |
817 | break; |
818 | case ios_base::cur: |
819 | if (__wch & ios_base::in) |
820 | __noff = this->gptr() - this->eback(); |
821 | else |
822 | __noff = this->pptr() - this->pbase(); |
823 | break; |
824 | case ios_base::end: |
825 | __noff = __hm; |
826 | break; |
827 | default: |
828 | return pos_type(-1); |
829 | } |
830 | __noff += __off; |
831 | if (__noff < 0 || __hm < __noff) |
832 | return pos_type(-1); |
833 | if (__noff != 0) { |
834 | if ((__wch & ios_base::in) && this->gptr() == nullptr) |
835 | return pos_type(-1); |
836 | if ((__wch & ios_base::out) && this->pptr() == nullptr) |
837 | return pos_type(-1); |
838 | } |
839 | if (__wch & ios_base::in) |
840 | this->setg(this->eback(), this->eback() + __noff, __hm_); |
841 | if (__wch & ios_base::out) { |
842 | this->setp(this->pbase(), this->epptr()); |
843 | this->__pbump(__noff); |
844 | } |
845 | return pos_type(__noff); |
846 | } |
847 | |
848 | // Class template basic_istringstream [istringstream] |
849 | |
850 | template <class _CharT, class _Traits, class _Allocator> |
851 | class _LIBCPP_TEMPLATE_VIS basic_istringstream : public basic_istream<_CharT, _Traits> { |
852 | public: |
853 | typedef _CharT char_type; |
854 | typedef _Traits traits_type; |
855 | typedef typename traits_type::int_type int_type; |
856 | typedef typename traits_type::pos_type pos_type; |
857 | typedef typename traits_type::off_type off_type; |
858 | typedef _Allocator allocator_type; |
859 | |
860 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
861 | |
862 | private: |
863 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
864 | |
865 | public: |
866 | // [istringstream.cons] Constructors: |
867 | _LIBCPP_HIDE_FROM_ABI basic_istringstream() : basic_istream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in) {} |
868 | |
869 | _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(ios_base::openmode __wch) |
870 | : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::in) {} |
871 | |
872 | _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const string_type& __s, ios_base::openmode __wch = ios_base::in) |
873 | : basic_istream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch | ios_base::in) {} |
874 | |
875 | #if _LIBCPP_STD_VER >= 20 |
876 | _LIBCPP_HIDE_FROM_ABI basic_istringstream(ios_base::openmode __wch, const _Allocator& __a) |
877 | : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::in, __a) {} |
878 | |
879 | _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(string_type&& __s, ios_base::openmode __wch = ios_base::in) |
880 | : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::in) {} |
881 | |
882 | template <class _SAlloc> |
883 | _LIBCPP_HIDE_FROM_ABI basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) |
884 | : basic_istringstream(__s, ios_base::in, __a) {} |
885 | |
886 | template <class _SAlloc> |
887 | _LIBCPP_HIDE_FROM_ABI basic_istringstream( |
888 | const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) |
889 | : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in, __a) {} |
890 | |
891 | template <class _SAlloc> |
892 | _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, |
893 | ios_base::openmode __wch = ios_base::in) |
894 | : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::in) {} |
895 | #endif // _LIBCPP_STD_VER >= 20 |
896 | |
897 | #if _LIBCPP_STD_VER >= 26 |
898 | |
899 | template <class _Tp> |
900 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
901 | _LIBCPP_HIDE_FROM_ABI explicit basic_istringstream(const _Tp& __t, ios_base::openmode __which = ios_base::in) |
902 | : basic_istringstream(__t, __which, _Allocator()) {} |
903 | |
904 | template <class _Tp> |
905 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
906 | _LIBCPP_HIDE_FROM_ABI basic_istringstream(const _Tp& __t, const _Allocator& __a) |
907 | : basic_istringstream(__t, ios_base::in, __a) {} |
908 | |
909 | template <class _Tp> |
910 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
911 | _LIBCPP_HIDE_FROM_ABI basic_istringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) |
912 | : basic_istream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which | ios_base::in, __a) {} |
913 | |
914 | #endif // _LIBCPP_STD_VER >= 26 |
915 | |
916 | basic_istringstream(const basic_istringstream&) = delete; |
917 | _LIBCPP_HIDE_FROM_ABI basic_istringstream(basic_istringstream&& __rhs) |
918 | : basic_istream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { |
919 | basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); |
920 | } |
921 | |
922 | // [istringstream.assign] Assign and swap: |
923 | basic_istringstream& operator=(const basic_istringstream&) = delete; |
924 | basic_istringstream& operator=(basic_istringstream&& __rhs) { |
925 | basic_istream<char_type, traits_type>::operator=(std::move(__rhs)); |
926 | __sb_ = std::move(__rhs.__sb_); |
927 | return *this; |
928 | } |
929 | _LIBCPP_HIDE_FROM_ABI void swap(basic_istringstream& __rhs) { |
930 | basic_istream<char_type, traits_type>::swap(__rhs); |
931 | __sb_.swap(__rhs.__sb_); |
932 | } |
933 | |
934 | // [istringstream.members] Member functions: |
935 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { |
936 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
937 | } |
938 | |
939 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
940 | _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } |
941 | #else |
942 | _LIBCPP_HIDE_FROM_ABI string_type str() const& { return __sb_.str(); } |
943 | |
944 | _LIBCPP_HIDE_FROM_ABI string_type str() && { return std::move(__sb_).str(); } |
945 | #endif |
946 | |
947 | #if _LIBCPP_STD_VER >= 20 |
948 | template <class _SAlloc> |
949 | requires __is_allocator<_SAlloc>::value |
950 | _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { |
951 | return __sb_.str(__sa); |
952 | } |
953 | |
954 | _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } |
955 | #endif // _LIBCPP_STD_VER >= 20 |
956 | |
957 | _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } |
958 | |
959 | #if _LIBCPP_STD_VER >= 20 |
960 | template <class _SAlloc> |
961 | _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { |
962 | __sb_.str(__s); |
963 | } |
964 | |
965 | _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } |
966 | #endif // _LIBCPP_STD_VER >= 20 |
967 | |
968 | #if _LIBCPP_STD_VER >= 26 |
969 | template <class _Tp> |
970 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
971 | _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { |
972 | rdbuf()->str(__t); |
973 | } |
974 | #endif // _LIBCPP_STD_VER >= 26 |
975 | }; |
976 | |
977 | template <class _CharT, class _Traits, class _Allocator> |
978 | inline _LIBCPP_HIDE_FROM_ABI void |
979 | swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x, basic_istringstream<_CharT, _Traits, _Allocator>& __y) { |
980 | __x.swap(__y); |
981 | } |
982 | |
983 | // Class template basic_ostringstream [ostringstream] |
984 | |
985 | template <class _CharT, class _Traits, class _Allocator> |
986 | class _LIBCPP_TEMPLATE_VIS basic_ostringstream : public basic_ostream<_CharT, _Traits> { |
987 | public: |
988 | typedef _CharT char_type; |
989 | typedef _Traits traits_type; |
990 | typedef typename traits_type::int_type int_type; |
991 | typedef typename traits_type::pos_type pos_type; |
992 | typedef typename traits_type::off_type off_type; |
993 | typedef _Allocator allocator_type; |
994 | |
995 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
996 | |
997 | private: |
998 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
999 | |
1000 | public: |
1001 | // [ostringstream.cons] Constructors: |
1002 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream() : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::out) {} |
1003 | |
1004 | _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(ios_base::openmode __wch) |
1005 | : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__wch | ios_base::out) {} |
1006 | |
1007 | _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const string_type& __s, ios_base::openmode __wch = ios_base::out) |
1008 | : basic_ostream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch | ios_base::out) {} |
1009 | |
1010 | #if _LIBCPP_STD_VER >= 20 |
1011 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream(ios_base::openmode __wch, const _Allocator& __a) |
1012 | : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch | ios_base::out, __a) {} |
1013 | |
1014 | _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(string_type&& __s, ios_base::openmode __wch = ios_base::out) |
1015 | : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch | ios_base::out) {} |
1016 | |
1017 | template <class _SAlloc> |
1018 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) |
1019 | : basic_ostringstream(__s, ios_base::out, __a) {} |
1020 | |
1021 | template <class _SAlloc> |
1022 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream( |
1023 | const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) |
1024 | : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out, __a) {} |
1025 | |
1026 | template <class _SAlloc> |
1027 | requires(!is_same_v<_SAlloc, allocator_type>) |
1028 | _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, |
1029 | ios_base::openmode __wch = ios_base::out) |
1030 | : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch | ios_base::out) {} |
1031 | #endif // _LIBCPP_STD_VER >= 20 |
1032 | |
1033 | #if _LIBCPP_STD_VER >= 26 |
1034 | |
1035 | template <class _Tp> |
1036 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1037 | _LIBCPP_HIDE_FROM_ABI explicit basic_ostringstream(const _Tp& __t, ios_base::openmode __which = ios_base::out) |
1038 | : basic_ostringstream(__t, __which | ios_base::out, _Allocator()) {} |
1039 | |
1040 | template <class _Tp> |
1041 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1042 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const _Tp& __t, const _Allocator& __a) |
1043 | : basic_ostringstream(__t, ios_base::out, __a) {} |
1044 | |
1045 | template <class _Tp> |
1046 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1047 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) |
1048 | : basic_ostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which | ios_base::out, __a) {} |
1049 | |
1050 | #endif // _LIBCPP_STD_VER >= 26 |
1051 | |
1052 | basic_ostringstream(const basic_ostringstream&) = delete; |
1053 | _LIBCPP_HIDE_FROM_ABI basic_ostringstream(basic_ostringstream&& __rhs) |
1054 | : basic_ostream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { |
1055 | basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_); |
1056 | } |
1057 | |
1058 | // [ostringstream.assign] Assign and swap: |
1059 | basic_ostringstream& operator=(const basic_ostringstream&) = delete; |
1060 | basic_ostringstream& operator=(basic_ostringstream&& __rhs) { |
1061 | basic_ostream<char_type, traits_type>::operator=(std::move(__rhs)); |
1062 | __sb_ = std::move(__rhs.__sb_); |
1063 | return *this; |
1064 | } |
1065 | |
1066 | _LIBCPP_HIDE_FROM_ABI void swap(basic_ostringstream& __rhs) { |
1067 | basic_ostream<char_type, traits_type>::swap(__rhs); |
1068 | __sb_.swap(__rhs.__sb_); |
1069 | } |
1070 | |
1071 | // [ostringstream.members] Member functions: |
1072 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { |
1073 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
1074 | } |
1075 | |
1076 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
1077 | _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } |
1078 | #else |
1079 | _LIBCPP_HIDE_FROM_ABI string_type str() const& { return __sb_.str(); } |
1080 | |
1081 | _LIBCPP_HIDE_FROM_ABI string_type str() && { return std::move(__sb_).str(); } |
1082 | #endif |
1083 | |
1084 | #if _LIBCPP_STD_VER >= 20 |
1085 | template <class _SAlloc> |
1086 | requires __is_allocator<_SAlloc>::value |
1087 | _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { |
1088 | return __sb_.str(__sa); |
1089 | } |
1090 | |
1091 | _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } |
1092 | #endif // _LIBCPP_STD_VER >= 20 |
1093 | |
1094 | _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } |
1095 | |
1096 | #if _LIBCPP_STD_VER >= 20 |
1097 | template <class _SAlloc> |
1098 | _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { |
1099 | __sb_.str(__s); |
1100 | } |
1101 | |
1102 | _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } |
1103 | #endif // _LIBCPP_STD_VER >= 20 |
1104 | |
1105 | #if _LIBCPP_STD_VER >= 26 |
1106 | template <class _Tp> |
1107 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1108 | _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { |
1109 | rdbuf()->str(__t); |
1110 | } |
1111 | #endif // _LIBCPP_STD_VER >= 26 |
1112 | }; |
1113 | |
1114 | template <class _CharT, class _Traits, class _Allocator> |
1115 | inline _LIBCPP_HIDE_FROM_ABI void |
1116 | swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x, basic_ostringstream<_CharT, _Traits, _Allocator>& __y) { |
1117 | __x.swap(__y); |
1118 | } |
1119 | |
1120 | // Class template basic_stringstream [stringstream] |
1121 | |
1122 | template <class _CharT, class _Traits, class _Allocator> |
1123 | class _LIBCPP_TEMPLATE_VIS basic_stringstream : public basic_iostream<_CharT, _Traits> { |
1124 | public: |
1125 | typedef _CharT char_type; |
1126 | typedef _Traits traits_type; |
1127 | typedef typename traits_type::int_type int_type; |
1128 | typedef typename traits_type::pos_type pos_type; |
1129 | typedef typename traits_type::off_type off_type; |
1130 | typedef _Allocator allocator_type; |
1131 | |
1132 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
1133 | |
1134 | private: |
1135 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
1136 | |
1137 | public: |
1138 | // [stringstream.cons] constructors |
1139 | _LIBCPP_HIDE_FROM_ABI basic_stringstream() |
1140 | : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(ios_base::in | ios_base::out) {} |
1141 | |
1142 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(ios_base::openmode __wch) |
1143 | : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__wch) {} |
1144 | |
1145 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const string_type& __s, |
1146 | ios_base::openmode __wch = ios_base::in | ios_base::out) |
1147 | : basic_iostream<_CharT, _Traits>(&__sb_), __sb_(__s, __wch) {} |
1148 | |
1149 | #if _LIBCPP_STD_VER >= 20 |
1150 | _LIBCPP_HIDE_FROM_ABI basic_stringstream(ios_base::openmode __wch, const _Allocator& __a) |
1151 | : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__wch, __a) {} |
1152 | |
1153 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(string_type&& __s, |
1154 | ios_base::openmode __wch = ios_base::out | ios_base::in) |
1155 | : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(std::move(__s), __wch) {} |
1156 | |
1157 | template <class _SAlloc> |
1158 | _LIBCPP_HIDE_FROM_ABI basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, const _Allocator& __a) |
1159 | : basic_stringstream(__s, ios_base::out | ios_base::in, __a) {} |
1160 | |
1161 | template <class _SAlloc> |
1162 | _LIBCPP_HIDE_FROM_ABI |
1163 | basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, ios_base::openmode __wch, const _Allocator& __a) |
1164 | : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch, __a) {} |
1165 | |
1166 | template <class _SAlloc> |
1167 | requires(!is_same_v<_SAlloc, allocator_type>) |
1168 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __s, |
1169 | ios_base::openmode __wch = ios_base::out | ios_base::in) |
1170 | : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__s, __wch) {} |
1171 | #endif // _LIBCPP_STD_VER >= 20 |
1172 | |
1173 | #if _LIBCPP_STD_VER >= 26 |
1174 | |
1175 | template <class _Tp> |
1176 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1177 | _LIBCPP_HIDE_FROM_ABI explicit basic_stringstream(const _Tp& __t, |
1178 | ios_base::openmode __which = ios_base::out | ios_base::in) |
1179 | : basic_stringstream(__t, __which, _Allocator()) {} |
1180 | |
1181 | template <class _Tp> |
1182 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1183 | _LIBCPP_HIDE_FROM_ABI basic_stringstream(const _Tp& __t, const _Allocator& __a) |
1184 | : basic_stringstream(__t, ios_base::out | ios_base::in, __a) {} |
1185 | |
1186 | template <class _Tp> |
1187 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1188 | _LIBCPP_HIDE_FROM_ABI basic_stringstream(const _Tp& __t, ios_base::openmode __which, const _Allocator& __a) |
1189 | : basic_iostream<_CharT, _Traits>(std::addressof(__sb_)), __sb_(__t, __which, __a) {} |
1190 | |
1191 | #endif // _LIBCPP_STD_VER >= 26 |
1192 | |
1193 | basic_stringstream(const basic_stringstream&) = delete; |
1194 | _LIBCPP_HIDE_FROM_ABI basic_stringstream(basic_stringstream&& __rhs) |
1195 | : basic_iostream<_CharT, _Traits>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { |
1196 | basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); |
1197 | } |
1198 | |
1199 | // [stringstream.assign] Assign and swap: |
1200 | basic_stringstream& operator=(const basic_stringstream&) = delete; |
1201 | basic_stringstream& operator=(basic_stringstream&& __rhs) { |
1202 | basic_iostream<char_type, traits_type>::operator=(std::move(__rhs)); |
1203 | __sb_ = std::move(__rhs.__sb_); |
1204 | return *this; |
1205 | } |
1206 | _LIBCPP_HIDE_FROM_ABI void swap(basic_stringstream& __rhs) { |
1207 | basic_iostream<char_type, traits_type>::swap(__rhs); |
1208 | __sb_.swap(__rhs.__sb_); |
1209 | } |
1210 | |
1211 | // [stringstream.members] Member functions: |
1212 | _LIBCPP_HIDE_FROM_ABI basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const { |
1213 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
1214 | } |
1215 | |
1216 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_BUILDING_LIBRARY) |
1217 | _LIBCPP_HIDE_FROM_ABI string_type str() const { return __sb_.str(); } |
1218 | #else |
1219 | _LIBCPP_HIDE_FROM_ABI string_type str() const& { return __sb_.str(); } |
1220 | |
1221 | _LIBCPP_HIDE_FROM_ABI string_type str() && { return std::move(__sb_).str(); } |
1222 | #endif |
1223 | |
1224 | #if _LIBCPP_STD_VER >= 20 |
1225 | template <class _SAlloc> |
1226 | requires __is_allocator<_SAlloc>::value |
1227 | _LIBCPP_HIDE_FROM_ABI basic_string<char_type, traits_type, _SAlloc> str(const _SAlloc& __sa) const { |
1228 | return __sb_.str(__sa); |
1229 | } |
1230 | |
1231 | _LIBCPP_HIDE_FROM_ABI basic_string_view<char_type, traits_type> view() const noexcept { return __sb_.view(); } |
1232 | #endif // _LIBCPP_STD_VER >= 20 |
1233 | |
1234 | _LIBCPP_HIDE_FROM_ABI void str(const string_type& __s) { __sb_.str(__s); } |
1235 | |
1236 | #if _LIBCPP_STD_VER >= 20 |
1237 | template <class _SAlloc> |
1238 | _LIBCPP_HIDE_FROM_ABI void str(const basic_string<char_type, traits_type, _SAlloc>& __s) { |
1239 | __sb_.str(__s); |
1240 | } |
1241 | |
1242 | _LIBCPP_HIDE_FROM_ABI void str(string_type&& __s) { __sb_.str(std::move(__s)); } |
1243 | #endif // _LIBCPP_STD_VER >= 20 |
1244 | |
1245 | #if _LIBCPP_STD_VER >= 26 |
1246 | template <class _Tp> |
1247 | requires is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>> |
1248 | _LIBCPP_HIDE_FROM_ABI void str(const _Tp& __t) { |
1249 | rdbuf()->str(__t); |
1250 | } |
1251 | #endif // _LIBCPP_STD_VER >= 26 |
1252 | }; |
1253 | |
1254 | template <class _CharT, class _Traits, class _Allocator> |
1255 | inline _LIBCPP_HIDE_FROM_ABI void |
1256 | swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x, basic_stringstream<_CharT, _Traits, _Allocator>& __y) { |
1257 | __x.swap(__y); |
1258 | } |
1259 | |
1260 | #if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 |
1261 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringbuf<char>; |
1262 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_stringstream<char>; |
1263 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostringstream<char>; |
1264 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istringstream<char>; |
1265 | #endif |
1266 | |
1267 | _LIBCPP_END_NAMESPACE_STD |
1268 | |
1269 | _LIBCPP_POP_MACROS |
1270 | |
1271 | #if _LIBCPP_STD_VER <= 20 && !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) |
1272 | # include <ostream> |
1273 | # include <type_traits> |
1274 | #endif |
1275 | |
1276 | #endif // _LIBCPP_SSTREAM |
1277 | |