]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Components for manipulating sequences of characters -*- C++ -*- |
2 | ||
83ffe9cd | 3 | // Copyright (C) 1997-2023 Free Software Foundation, Inc. |
725dc051 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
725dc051 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
725dc051 | 24 | |
f910786b | 25 | /** @file bits/basic_string.h |
729e3d3f | 26 | * This is an internal header file, included by other library headers. |
f910786b | 27 | * Do not attempt to use it directly. @headername{string} |
729e3d3f PE |
28 | */ |
29 | ||
143c27b0 BK |
30 | // |
31 | // ISO C++ 14882: 21 Strings library | |
32 | // | |
33 | ||
3d7c150e BK |
34 | #ifndef _BASIC_STRING_H |
35 | #define _BASIC_STRING_H 1 | |
725dc051 | 36 | |
b0a85b86 GDR |
37 | #pragma GCC system_header |
38 | ||
34a2b755 | 39 | #include <ext/alloc_traits.h> |
285b36d6 | 40 | #include <debug/debug.h> |
e25d2617 | 41 | |
734f5023 | 42 | #if __cplusplus >= 201103L |
988499f4 | 43 | #include <initializer_list> |
a7d5d7e2 | 44 | #endif |
725dc051 | 45 | |
1a289fa3 | 46 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
47 | # include <string_view> |
48 | #endif | |
49 | ||
7b527614 JW |
50 | #if ! _GLIBCXX_USE_CXX11_ABI |
51 | # include "cow_string.h" | |
52 | #else | |
12ffa228 BK |
53 | namespace std _GLIBCXX_VISIBILITY(default) |
54 | { | |
55 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
7b527614 | 56 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
3cbc7af0 | 57 | |
b376b1ef | 58 | #ifdef __cpp_lib_is_constant_evaluated |
b96e2ff9 ML |
59 | // Support P0980R1 in C++20. |
60 | # define __cpp_lib_constexpr_string 201907L | |
74d14778 | 61 | #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED |
3da80ed7 JW |
62 | // Support P0426R1 changes to char_traits in C++17. |
63 | # define __cpp_lib_constexpr_string 201611L | |
3da80ed7 JW |
64 | #endif |
65 | ||
34a2b755 JW |
66 | /** |
67 | * @class basic_string basic_string.h <string> | |
68 | * @brief Managing sequences of characters and character-like objects. | |
69 | * | |
70 | * @ingroup strings | |
71 | * @ingroup sequences | |
72 | * | |
73 | * @tparam _CharT Type of character | |
74 | * @tparam _Traits Traits for character type, defaults to | |
75 | * char_traits<_CharT>. | |
76 | * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. | |
77 | * | |
78 | * Meets the requirements of a <a href="tables.html#65">container</a>, a | |
79 | * <a href="tables.html#66">reversible container</a>, and a | |
80 | * <a href="tables.html#67">sequence</a>. Of the | |
81 | * <a href="tables.html#68">optional sequence requirements</a>, only | |
82 | * @c push_back, @c at, and @c %array access are supported. | |
83 | */ | |
84 | template<typename _CharT, typename _Traits, typename _Alloc> | |
85 | class basic_string | |
86 | { | |
87 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template | |
88 | rebind<_CharT>::other _Char_alloc_type; | |
2d76292b JW |
89 | |
90 | #if __cpp_lib_constexpr_string < 201907L | |
34a2b755 | 91 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; |
2d76292b JW |
92 | #else |
93 | template<typename _Traits2, typename _Dummy_for_PR85282> | |
94 | struct _Alloc_traits_impl : __gnu_cxx::__alloc_traits<_Char_alloc_type> | |
95 | { | |
96 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Base; | |
97 | ||
98 | [[__gnu__::__always_inline__]] | |
99 | static constexpr typename _Base::pointer | |
100 | allocate(_Char_alloc_type& __a, typename _Base::size_type __n) | |
101 | { | |
102 | pointer __p = _Base::allocate(__a, __n); | |
74d14778 | 103 | if (std::is_constant_evaluated()) |
2d76292b JW |
104 | // Begin the lifetime of characters in allocated storage. |
105 | for (size_type __i = 0; __i < __n; ++__i) | |
106 | std::construct_at(__builtin_addressof(__p[__i])); | |
107 | return __p; | |
108 | } | |
109 | }; | |
110 | ||
111 | template<typename _Dummy_for_PR85282> | |
112 | struct _Alloc_traits_impl<char_traits<_CharT>, _Dummy_for_PR85282> | |
113 | : __gnu_cxx::__alloc_traits<_Char_alloc_type> | |
114 | { | |
115 | // std::char_traits begins the lifetime of characters. | |
116 | }; | |
117 | ||
118 | using _Alloc_traits = _Alloc_traits_impl<_Traits, void>; | |
119 | #endif | |
34a2b755 JW |
120 | |
121 | // Types: | |
122 | public: | |
123 | typedef _Traits traits_type; | |
124 | typedef typename _Traits::char_type value_type; | |
125 | typedef _Char_alloc_type allocator_type; | |
126 | typedef typename _Alloc_traits::size_type size_type; | |
127 | typedef typename _Alloc_traits::difference_type difference_type; | |
128 | typedef typename _Alloc_traits::reference reference; | |
129 | typedef typename _Alloc_traits::const_reference const_reference; | |
130 | typedef typename _Alloc_traits::pointer pointer; | |
131 | typedef typename _Alloc_traits::const_pointer const_pointer; | |
132 | typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; | |
133 | typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> | |
134 | const_iterator; | |
135 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
136 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
137 | ||
138 | /// Value returned by various member functions when they fail. | |
139 | static const size_type npos = static_cast<size_type>(-1); | |
140 | ||
3eb1eda1 | 141 | protected: |
34a2b755 JW |
142 | // type used for positions in insert, erase etc. |
143 | #if __cplusplus < 201103L | |
144 | typedef iterator __const_iterator; | |
145 | #else | |
146 | typedef const_iterator __const_iterator; | |
147 | #endif | |
148 | ||
3eb1eda1 | 149 | private: |
1a289fa3 | 150 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
151 | // A helper type for avoiding boiler-plate. |
152 | typedef basic_string_view<_CharT, _Traits> __sv_type; | |
92daf2de JW |
153 | |
154 | template<typename _Tp, typename _Res> | |
155 | using _If_sv = enable_if_t< | |
156 | __and_<is_convertible<const _Tp&, __sv_type>, | |
4cf5930f | 157 | __not_<is_convertible<const _Tp*, const basic_string*>>, |
92daf2de JW |
158 | __not_<is_convertible<const _Tp&, const _CharT*>>>::value, |
159 | _Res>; | |
df66af3b DK |
160 | |
161 | // Allows an implicit conversion to __sv_type. | |
b96e2ff9 | 162 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
163 | static __sv_type |
164 | _S_to_string_view(__sv_type __svt) noexcept | |
165 | { return __svt; } | |
166 | ||
167 | // Wraps a string_view by explicit conversion and thus | |
168 | // allows to add an internal constructor that does not | |
169 | // participate in overload resolution when a string_view | |
170 | // is provided. | |
171 | struct __sv_wrapper | |
172 | { | |
b96e2ff9 ML |
173 | _GLIBCXX20_CONSTEXPR explicit |
174 | __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } | |
175 | ||
df66af3b DK |
176 | __sv_type _M_sv; |
177 | }; | |
1a289fa3 JW |
178 | |
179 | /** | |
180 | * @brief Only internally used: Construct string from a string view | |
181 | * wrapper. | |
182 | * @param __svw string view wrapper. | |
183 | * @param __a Allocator to use. | |
184 | */ | |
b96e2ff9 | 185 | _GLIBCXX20_CONSTEXPR |
1a289fa3 JW |
186 | explicit |
187 | basic_string(__sv_wrapper __svw, const _Alloc& __a) | |
188 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } | |
ca8f2cb1 VV |
189 | #endif |
190 | ||
34a2b755 JW |
191 | // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
192 | struct _Alloc_hider : allocator_type // TODO check __is_final | |
193 | { | |
8cab3d18 | 194 | #if __cplusplus < 201103L |
34a2b755 JW |
195 | _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) |
196 | : allocator_type(__a), _M_p(__dat) { } | |
8cab3d18 | 197 | #else |
b96e2ff9 | 198 | _GLIBCXX20_CONSTEXPR |
8cab3d18 JW |
199 | _Alloc_hider(pointer __dat, const _Alloc& __a) |
200 | : allocator_type(__a), _M_p(__dat) { } | |
201 | ||
b96e2ff9 | 202 | _GLIBCXX20_CONSTEXPR |
8cab3d18 JW |
203 | _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc()) |
204 | : allocator_type(std::move(__a)), _M_p(__dat) { } | |
205 | #endif | |
34a2b755 JW |
206 | |
207 | pointer _M_p; // The actual data. | |
208 | }; | |
209 | ||
210 | _Alloc_hider _M_dataplus; | |
211 | size_type _M_string_length; | |
212 | ||
59aa28e8 | 213 | enum { _S_local_capacity = 15 / sizeof(_CharT) }; |
34a2b755 JW |
214 | |
215 | union | |
216 | { | |
217 | _CharT _M_local_buf[_S_local_capacity + 1]; | |
218 | size_type _M_allocated_capacity; | |
219 | }; | |
220 | ||
b96e2ff9 | 221 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
222 | void |
223 | _M_data(pointer __p) | |
224 | { _M_dataplus._M_p = __p; } | |
225 | ||
b96e2ff9 | 226 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
227 | void |
228 | _M_length(size_type __length) | |
229 | { _M_string_length = __length; } | |
230 | ||
b96e2ff9 | 231 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
232 | pointer |
233 | _M_data() const | |
234 | { return _M_dataplus._M_p; } | |
235 | ||
b96e2ff9 | 236 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
237 | pointer |
238 | _M_local_data() | |
239 | { | |
240 | #if __cplusplus >= 201103L | |
241 | return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); | |
242 | #else | |
243 | return pointer(_M_local_buf); | |
244 | #endif | |
245 | } | |
246 | ||
b96e2ff9 | 247 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
248 | const_pointer |
249 | _M_local_data() const | |
250 | { | |
251 | #if __cplusplus >= 201103L | |
252 | return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); | |
253 | #else | |
254 | return const_pointer(_M_local_buf); | |
255 | #endif | |
256 | } | |
257 | ||
b96e2ff9 | 258 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
259 | void |
260 | _M_capacity(size_type __capacity) | |
261 | { _M_allocated_capacity = __capacity; } | |
262 | ||
b96e2ff9 | 263 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
264 | void |
265 | _M_set_length(size_type __n) | |
266 | { | |
267 | _M_length(__n); | |
268 | traits_type::assign(_M_data()[__n], _CharT()); | |
269 | } | |
270 | ||
b96e2ff9 | 271 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
272 | bool |
273 | _M_is_local() const | |
274 | { return _M_data() == _M_local_data(); } | |
275 | ||
276 | // Create & Destroy | |
b96e2ff9 | 277 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
278 | pointer |
279 | _M_create(size_type&, size_type); | |
280 | ||
b96e2ff9 | 281 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
282 | void |
283 | _M_dispose() | |
284 | { | |
285 | if (!_M_is_local()) | |
286 | _M_destroy(_M_allocated_capacity); | |
287 | } | |
288 | ||
b96e2ff9 | 289 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
290 | void |
291 | _M_destroy(size_type __size) throw() | |
292 | { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } | |
293 | ||
5a9572e4 | 294 | #if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
34a2b755 JW |
295 | // _M_construct_aux is used to implement the 21.3.1 para 15 which |
296 | // requires special behaviour if _InIterator is an integral type | |
297 | template<typename _InIterator> | |
298 | void | |
299 | _M_construct_aux(_InIterator __beg, _InIterator __end, | |
300 | std::__false_type) | |
301 | { | |
302 | typedef typename iterator_traits<_InIterator>::iterator_category _Tag; | |
303 | _M_construct(__beg, __end, _Tag()); | |
304 | } | |
305 | ||
306 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
307 | // 438. Ambiguity in the "do the right thing" clause | |
308 | template<typename _Integer> | |
309 | void | |
310 | _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) | |
311 | { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } | |
312 | ||
313 | void | |
314 | _M_construct_aux_2(size_type __req, _CharT __c) | |
315 | { _M_construct(__req, __c); } | |
5a9572e4 | 316 | #endif |
34a2b755 JW |
317 | |
318 | // For Input Iterators, used in istreambuf_iterators, etc. | |
319 | template<typename _InIterator> | |
b96e2ff9 | 320 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
321 | void |
322 | _M_construct(_InIterator __beg, _InIterator __end, | |
323 | std::input_iterator_tag); | |
324 | ||
325 | // For forward_iterators up to random_access_iterators, used for | |
326 | // string::iterator, _CharT*, etc. | |
327 | template<typename _FwdIterator> | |
b96e2ff9 | 328 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
329 | void |
330 | _M_construct(_FwdIterator __beg, _FwdIterator __end, | |
331 | std::forward_iterator_tag); | |
332 | ||
b96e2ff9 | 333 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
334 | void |
335 | _M_construct(size_type __req, _CharT __c); | |
336 | ||
b96e2ff9 | 337 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
338 | allocator_type& |
339 | _M_get_allocator() | |
340 | { return _M_dataplus; } | |
341 | ||
b96e2ff9 | 342 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
343 | const allocator_type& |
344 | _M_get_allocator() const | |
345 | { return _M_dataplus; } | |
346 | ||
6afa1083 JW |
347 | // Ensure that _M_local_buf is the active member of the union. |
348 | __attribute__((__always_inline__)) | |
349 | _GLIBCXX14_CONSTEXPR | |
350 | pointer | |
351 | _M_use_local_data() _GLIBCXX_NOEXCEPT | |
352 | { | |
353 | #if __cpp_lib_is_constant_evaluated | |
74d14778 | 354 | if (std::is_constant_evaluated()) |
52672be7 NS |
355 | for (size_type __i = 0; __i <= _S_local_capacity; ++__i) |
356 | _M_local_buf[__i] = _CharT(); | |
6afa1083 JW |
357 | #endif |
358 | return _M_local_data(); | |
359 | } | |
360 | ||
34a2b755 JW |
361 | private: |
362 | ||
363 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST | |
364 | // The explicit instantiations in misc-inst.cc require this due to | |
365 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 | |
366 | template<typename _Tp, bool _Requires = | |
367 | !__are_same<_Tp, _CharT*>::__value | |
368 | && !__are_same<_Tp, const _CharT*>::__value | |
369 | && !__are_same<_Tp, iterator>::__value | |
370 | && !__are_same<_Tp, const_iterator>::__value> | |
371 | struct __enable_if_not_native_iterator | |
372 | { typedef basic_string& __type; }; | |
373 | template<typename _Tp> | |
374 | struct __enable_if_not_native_iterator<_Tp, false> { }; | |
375 | #endif | |
376 | ||
b96e2ff9 | 377 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
378 | size_type |
379 | _M_check(size_type __pos, const char* __s) const | |
380 | { | |
381 | if (__pos > this->size()) | |
382 | __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " | |
383 | "this->size() (which is %zu)"), | |
384 | __s, __pos, this->size()); | |
385 | return __pos; | |
386 | } | |
387 | ||
b96e2ff9 | 388 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
389 | void |
390 | _M_check_length(size_type __n1, size_type __n2, const char* __s) const | |
391 | { | |
392 | if (this->max_size() - (this->size() - __n1) < __n2) | |
393 | __throw_length_error(__N(__s)); | |
394 | } | |
395 | ||
396 | ||
397 | // NB: _M_limit doesn't check for a bad __pos value. | |
b96e2ff9 | 398 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
399 | size_type |
400 | _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT | |
401 | { | |
402 | const bool __testoff = __off < this->size() - __pos; | |
403 | return __testoff ? __off : this->size() - __pos; | |
404 | } | |
405 | ||
406 | // True if _Rep and source do not overlap. | |
407 | bool | |
408 | _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT | |
409 | { | |
2d60da10 JW |
410 | return (less<const _CharT*>()(__s, _M_data()) |
411 | || less<const _CharT*>()(_M_data() + this->size(), __s)); | |
34a2b755 JW |
412 | } |
413 | ||
414 | // When __n = 1 way faster than the general multichar | |
415 | // traits_type::copy/move/assign. | |
b96e2ff9 | 416 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
417 | static void |
418 | _S_copy(_CharT* __d, const _CharT* __s, size_type __n) | |
419 | { | |
420 | if (__n == 1) | |
421 | traits_type::assign(*__d, *__s); | |
422 | else | |
423 | traits_type::copy(__d, __s, __n); | |
424 | } | |
425 | ||
b96e2ff9 | 426 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
427 | static void |
428 | _S_move(_CharT* __d, const _CharT* __s, size_type __n) | |
429 | { | |
430 | if (__n == 1) | |
431 | traits_type::assign(*__d, *__s); | |
432 | else | |
433 | traits_type::move(__d, __s, __n); | |
434 | } | |
435 | ||
b96e2ff9 | 436 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
437 | static void |
438 | _S_assign(_CharT* __d, size_type __n, _CharT __c) | |
439 | { | |
440 | if (__n == 1) | |
441 | traits_type::assign(*__d, __c); | |
442 | else | |
443 | traits_type::assign(__d, __n, __c); | |
444 | } | |
445 | ||
446 | // _S_copy_chars is a separate template to permit specialization | |
447 | // to optimize for the common case of pointers as iterators. | |
448 | template<class _Iterator> | |
b96e2ff9 | 449 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
450 | static void |
451 | _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) | |
34a2b755 | 452 | { |
f970a17d | 453 | for (; __k1 != __k2; ++__k1, (void)++__p) |
34a2b755 JW |
454 | traits_type::assign(*__p, *__k1); // These types are off. |
455 | } | |
456 | ||
b96e2ff9 | 457 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
458 | static void |
459 | _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT | |
460 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } | |
461 | ||
b96e2ff9 | 462 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
463 | static void |
464 | _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) | |
465 | _GLIBCXX_NOEXCEPT | |
466 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } | |
467 | ||
b96e2ff9 | 468 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
469 | static void |
470 | _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT | |
471 | { _S_copy(__p, __k1, __k2 - __k1); } | |
472 | ||
b96e2ff9 | 473 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
474 | static void |
475 | _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) | |
476 | _GLIBCXX_NOEXCEPT | |
477 | { _S_copy(__p, __k1, __k2 - __k1); } | |
478 | ||
b96e2ff9 | 479 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
480 | static int |
481 | _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT | |
482 | { | |
483 | const difference_type __d = difference_type(__n1 - __n2); | |
484 | ||
485 | if (__d > __gnu_cxx::__numeric_traits<int>::__max) | |
486 | return __gnu_cxx::__numeric_traits<int>::__max; | |
487 | else if (__d < __gnu_cxx::__numeric_traits<int>::__min) | |
488 | return __gnu_cxx::__numeric_traits<int>::__min; | |
489 | else | |
490 | return int(__d); | |
491 | } | |
492 | ||
b96e2ff9 | 493 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 494 | void |
a7d47f35 | 495 | _M_assign(const basic_string&); |
34a2b755 | 496 | |
b96e2ff9 | 497 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
498 | void |
499 | _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, | |
500 | size_type __len2); | |
501 | ||
b96e2ff9 | 502 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
503 | void |
504 | _M_erase(size_type __pos, size_type __n); | |
505 | ||
506 | public: | |
507 | // Construct/copy/destroy: | |
508 | // NB: We overload ctors in some cases instead of using default | |
509 | // arguments, per 17.4.4.4 para. 2 item 2. | |
510 | ||
511 | /** | |
512 | * @brief Default constructor creates an empty string. | |
513 | */ | |
b96e2ff9 | 514 | _GLIBCXX20_CONSTEXPR |
bcb896ab | 515 | basic_string() |
5caff414 | 516 | _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value) |
34a2b755 | 517 | : _M_dataplus(_M_local_data()) |
2d76292b JW |
518 | { |
519 | _M_use_local_data(); | |
520 | _M_set_length(0); | |
521 | } | |
34a2b755 JW |
522 | |
523 | /** | |
524 | * @brief Construct an empty string using allocator @a a. | |
525 | */ | |
b96e2ff9 | 526 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 527 | explicit |
5caff414 | 528 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT |
34a2b755 | 529 | : _M_dataplus(_M_local_data(), __a) |
2d76292b JW |
530 | { |
531 | _M_use_local_data(); | |
532 | _M_set_length(0); | |
533 | } | |
34a2b755 JW |
534 | |
535 | /** | |
536 | * @brief Construct string with copy of value of @a __str. | |
537 | * @param __str Source string. | |
538 | */ | |
b96e2ff9 | 539 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 540 | basic_string(const basic_string& __str) |
5caff414 JW |
541 | : _M_dataplus(_M_local_data(), |
542 | _Alloc_traits::_S_select_on_copy(__str._M_get_allocator())) | |
5a9572e4 JW |
543 | { |
544 | _M_construct(__str._M_data(), __str._M_data() + __str.length(), | |
545 | std::forward_iterator_tag()); | |
546 | } | |
34a2b755 | 547 | |
86bbf15b JW |
548 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
549 | // 2583. no way to supply an allocator for basic_string(str, pos) | |
34a2b755 JW |
550 | /** |
551 | * @brief Construct string as copy of a substring. | |
552 | * @param __str Source string. | |
553 | * @param __pos Index of first character to copy from. | |
86bbf15b | 554 | * @param __a Allocator to use. |
34a2b755 | 555 | */ |
b96e2ff9 | 556 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 557 | basic_string(const basic_string& __str, size_type __pos, |
86bbf15b JW |
558 | const _Alloc& __a = _Alloc()) |
559 | : _M_dataplus(_M_local_data(), __a) | |
560 | { | |
561 | const _CharT* __start = __str._M_data() | |
562 | + __str._M_check(__pos, "basic_string::basic_string"); | |
5a9572e4 JW |
563 | _M_construct(__start, __start + __str._M_limit(__pos, npos), |
564 | std::forward_iterator_tag()); | |
86bbf15b JW |
565 | } |
566 | ||
567 | /** | |
568 | * @brief Construct string as copy of a substring. | |
569 | * @param __str Source string. | |
570 | * @param __pos Index of first character to copy from. | |
571 | * @param __n Number of characters to copy. | |
572 | */ | |
b96e2ff9 | 573 | _GLIBCXX20_CONSTEXPR |
86bbf15b JW |
574 | basic_string(const basic_string& __str, size_type __pos, |
575 | size_type __n) | |
34a2b755 JW |
576 | : _M_dataplus(_M_local_data()) |
577 | { | |
2d60da10 | 578 | const _CharT* __start = __str._M_data() |
34a2b755 | 579 | + __str._M_check(__pos, "basic_string::basic_string"); |
5a9572e4 JW |
580 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
581 | std::forward_iterator_tag()); | |
34a2b755 JW |
582 | } |
583 | ||
584 | /** | |
585 | * @brief Construct string as copy of a substring. | |
586 | * @param __str Source string. | |
587 | * @param __pos Index of first character to copy from. | |
86bbf15b | 588 | * @param __n Number of characters to copy. |
34a2b755 JW |
589 | * @param __a Allocator to use. |
590 | */ | |
b96e2ff9 | 591 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
592 | basic_string(const basic_string& __str, size_type __pos, |
593 | size_type __n, const _Alloc& __a) | |
594 | : _M_dataplus(_M_local_data(), __a) | |
595 | { | |
2d60da10 JW |
596 | const _CharT* __start |
597 | = __str._M_data() + __str._M_check(__pos, "string::string"); | |
5a9572e4 JW |
598 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
599 | std::forward_iterator_tag()); | |
34a2b755 JW |
600 | } |
601 | ||
602 | /** | |
603 | * @brief Construct string initialized by a character %array. | |
604 | * @param __s Source character %array. | |
605 | * @param __n Number of characters to copy. | |
606 | * @param __a Allocator to use (default is default allocator). | |
607 | * | |
608 | * NB: @a __s must have at least @a __n characters, '\\0' | |
609 | * has no special meaning. | |
610 | */ | |
b96e2ff9 | 611 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
612 | basic_string(const _CharT* __s, size_type __n, |
613 | const _Alloc& __a = _Alloc()) | |
614 | : _M_dataplus(_M_local_data(), __a) | |
5a9572e4 JW |
615 | { |
616 | // NB: Not required, but considered best practice. | |
617 | if (__s == 0 && __n > 0) | |
618 | std::__throw_logic_error(__N("basic_string: " | |
619 | "construction from null is not valid")); | |
620 | _M_construct(__s, __s + __n, std::forward_iterator_tag()); | |
621 | } | |
34a2b755 JW |
622 | |
623 | /** | |
624 | * @brief Construct string as copy of a C string. | |
625 | * @param __s Source C string. | |
626 | * @param __a Allocator to use (default is default allocator). | |
627 | */ | |
5d84e6c5 JW |
628 | #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
629 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
630 | // 3076. basic_string CTAD ambiguity | |
631 | template<typename = _RequireAllocator<_Alloc>> | |
632 | #endif | |
b96e2ff9 | 633 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
634 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) |
635 | : _M_dataplus(_M_local_data(), __a) | |
789c57bc | 636 | { |
5a9572e4 JW |
637 | // NB: Not required, but considered best practice. |
638 | if (__s == 0) | |
639 | std::__throw_logic_error(__N("basic_string: " | |
640 | "construction from null is not valid")); | |
641 | const _CharT* __end = __s + traits_type::length(__s); | |
642 | _M_construct(__s, __end, forward_iterator_tag()); | |
789c57bc | 643 | } |
34a2b755 JW |
644 | |
645 | /** | |
646 | * @brief Construct string as multiple characters. | |
647 | * @param __n Number of characters. | |
648 | * @param __c Character to use. | |
649 | * @param __a Allocator to use (default is default allocator). | |
650 | */ | |
5d84e6c5 JW |
651 | #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
652 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
653 | // 3076. basic_string CTAD ambiguity | |
654 | template<typename = _RequireAllocator<_Alloc>> | |
655 | #endif | |
b96e2ff9 | 656 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
657 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) |
658 | : _M_dataplus(_M_local_data(), __a) | |
659 | { _M_construct(__n, __c); } | |
660 | ||
661 | #if __cplusplus >= 201103L | |
662 | /** | |
663 | * @brief Move construct string. | |
664 | * @param __str Source string. | |
665 | * | |
666 | * The newly-created string contains the exact contents of @a __str. | |
667 | * @a __str is a valid, but unspecified string. | |
252c9967 | 668 | */ |
b96e2ff9 | 669 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
670 | basic_string(basic_string&& __str) noexcept |
671 | : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) | |
672 | { | |
673 | if (__str._M_is_local()) | |
674 | { | |
faa7d78e | 675 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
b96e2ff9 | 676 | __str.length() + 1); |
34a2b755 JW |
677 | } |
678 | else | |
679 | { | |
680 | _M_data(__str._M_data()); | |
681 | _M_capacity(__str._M_allocated_capacity); | |
682 | } | |
683 | ||
684 | // Must use _M_length() here not _M_set_length() because | |
685 | // basic_stringbuf relies on writing into unallocated capacity so | |
686 | // we mess up the contents if we put a '\0' in the string. | |
687 | _M_length(__str.length()); | |
688 | __str._M_data(__str._M_local_data()); | |
689 | __str._M_set_length(0); | |
690 | } | |
691 | ||
692 | /** | |
693 | * @brief Construct string from an initializer %list. | |
694 | * @param __l std::initializer_list of characters. | |
695 | * @param __a Allocator to use (default is default allocator). | |
696 | */ | |
b96e2ff9 | 697 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
698 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) |
699 | : _M_dataplus(_M_local_data(), __a) | |
5a9572e4 | 700 | { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); } |
34a2b755 | 701 | |
b96e2ff9 | 702 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
703 | basic_string(const basic_string& __str, const _Alloc& __a) |
704 | : _M_dataplus(_M_local_data(), __a) | |
5a9572e4 | 705 | { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); } |
34a2b755 | 706 | |
b96e2ff9 | 707 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 708 | basic_string(basic_string&& __str, const _Alloc& __a) |
5caff414 | 709 | noexcept(_Alloc_traits::_S_always_equal()) |
34a2b755 JW |
710 | : _M_dataplus(_M_local_data(), __a) |
711 | { | |
5caff414 JW |
712 | if (__str._M_is_local()) |
713 | { | |
714 | traits_type::copy(_M_local_buf, __str._M_local_buf, | |
b96e2ff9 | 715 | __str.length() + 1); |
5caff414 JW |
716 | _M_length(__str.length()); |
717 | __str._M_set_length(0); | |
718 | } | |
719 | else if (_Alloc_traits::_S_always_equal() | |
720 | || __str.get_allocator() == __a) | |
721 | { | |
722 | _M_data(__str._M_data()); | |
723 | _M_length(__str.length()); | |
724 | _M_capacity(__str._M_allocated_capacity); | |
725 | __str._M_data(__str._M_local_buf); | |
726 | __str._M_set_length(0); | |
727 | } | |
34a2b755 | 728 | else |
5a9572e4 | 729 | _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); |
34a2b755 | 730 | } |
fe3ed885 | 731 | #endif // C++11 |
34a2b755 | 732 | |
fe3ed885 | 733 | #if __cplusplus >= 202100L |
cf876562 JW |
734 | basic_string(nullptr_t) = delete; |
735 | basic_string& operator=(nullptr_t) = delete; | |
fe3ed885 | 736 | #endif // C++23 |
34a2b755 JW |
737 | |
738 | /** | |
739 | * @brief Construct string as copy of a range. | |
740 | * @param __beg Start of range. | |
741 | * @param __end End of range. | |
742 | * @param __a Allocator to use (default is default allocator). | |
743 | */ | |
744 | #if __cplusplus >= 201103L | |
745 | template<typename _InputIterator, | |
746 | typename = std::_RequireInputIter<_InputIterator>> | |
747 | #else | |
748 | template<typename _InputIterator> | |
749 | #endif | |
b96e2ff9 | 750 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
751 | basic_string(_InputIterator __beg, _InputIterator __end, |
752 | const _Alloc& __a = _Alloc()) | |
753 | : _M_dataplus(_M_local_data(), __a) | |
5a9572e4 JW |
754 | { |
755 | #if __cplusplus >= 201103L | |
756 | _M_construct(__beg, __end, std::__iterator_category(__beg)); | |
757 | #else | |
758 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; | |
759 | _M_construct_aux(__beg, __end, _Integral()); | |
760 | #endif | |
761 | } | |
34a2b755 | 762 | |
1a289fa3 | 763 | #if __cplusplus >= 201703L |
bf56b0b8 JW |
764 | /** |
765 | * @brief Construct string from a substring of a string_view. | |
df66af3b | 766 | * @param __t Source object convertible to string view. |
bf56b0b8 JW |
767 | * @param __pos The index of the first character to copy from __t. |
768 | * @param __n The number of characters to copy from __t. | |
769 | * @param __a Allocator to use. | |
770 | */ | |
6aa0859a JW |
771 | template<typename _Tp, |
772 | typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> | |
b96e2ff9 | 773 | _GLIBCXX20_CONSTEXPR |
bf56b0b8 JW |
774 | basic_string(const _Tp& __t, size_type __pos, size_type __n, |
775 | const _Alloc& __a = _Alloc()) | |
df66af3b | 776 | : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |
bf56b0b8 | 777 | |
ca8f2cb1 VV |
778 | /** |
779 | * @brief Construct string from a string_view. | |
df66af3b | 780 | * @param __t Source object convertible to string view. |
ca8f2cb1 VV |
781 | * @param __a Allocator to use (default is default allocator). |
782 | */ | |
df66af3b | 783 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
b96e2ff9 | 784 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
785 | explicit |
786 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) | |
787 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } | |
ca8f2cb1 VV |
788 | #endif // C++17 |
789 | ||
34a2b755 JW |
790 | /** |
791 | * @brief Destroy the string instance. | |
792 | */ | |
b96e2ff9 | 793 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
794 | ~basic_string() |
795 | { _M_dispose(); } | |
796 | ||
797 | /** | |
798 | * @brief Assign the value of @a str to this string. | |
799 | * @param __str Source string. | |
800 | */ | |
b96e2ff9 | 801 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
802 | basic_string& |
803 | operator=(const basic_string& __str) | |
5caff414 | 804 | { |
11d10beb | 805 | return this->assign(__str); |
5caff414 | 806 | } |
34a2b755 JW |
807 | |
808 | /** | |
809 | * @brief Copy contents of @a s into this string. | |
810 | * @param __s Source null-terminated string. | |
811 | */ | |
b96e2ff9 | 812 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
813 | basic_string& |
814 | operator=(const _CharT* __s) | |
815 | { return this->assign(__s); } | |
816 | ||
817 | /** | |
818 | * @brief Set value to string of length 1. | |
819 | * @param __c Source character. | |
820 | * | |
821 | * Assigning to a character makes this string length 1 and | |
822 | * (*this)[0] == @a c. | |
823 | */ | |
b96e2ff9 | 824 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
825 | basic_string& |
826 | operator=(_CharT __c) | |
827 | { | |
828 | this->assign(1, __c); | |
829 | return *this; | |
830 | } | |
831 | ||
832 | #if __cplusplus >= 201103L | |
833 | /** | |
834 | * @brief Move assign the value of @a str to this string. | |
835 | * @param __str Source string. | |
836 | * | |
837 | * The contents of @a str are moved into this string (without copying). | |
838 | * @a str is a valid, but unspecified string. | |
252c9967 | 839 | */ |
34a2b755 JW |
840 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
841 | // 2063. Contradictory requirements for string move assignment | |
b96e2ff9 | 842 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
843 | basic_string& |
844 | operator=(basic_string&& __str) | |
5caff414 | 845 | noexcept(_Alloc_traits::_S_nothrow_move()) |
34a2b755 | 846 | { |
5caff414 JW |
847 | if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign() |
848 | && !_Alloc_traits::_S_always_equal() | |
849 | && _M_get_allocator() != __str._M_get_allocator()) | |
850 | { | |
851 | // Destroy existing storage before replacing allocator. | |
852 | _M_destroy(_M_allocated_capacity); | |
853 | _M_data(_M_local_data()); | |
854 | _M_set_length(0); | |
855 | } | |
856 | // Replace allocator if POCMA is true. | |
857 | std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator()); | |
858 | ||
71e09389 JW |
859 | if (__str._M_is_local()) |
860 | { | |
c2fb0a1a JW |
861 | // We've always got room for a short string, just copy it |
862 | // (unless this is a self-move, because that would violate the | |
863 | // char_traits::copy precondition that the ranges don't overlap). | |
864 | if (__builtin_expect(std::__addressof(__str) != this, true)) | |
865 | { | |
866 | if (__str.size()) | |
867 | this->_S_copy(_M_data(), __str._M_data(), __str.size()); | |
868 | _M_set_length(__str.size()); | |
869 | } | |
71e09389 JW |
870 | } |
871 | else if (_Alloc_traits::_S_propagate_on_move_assign() | |
872 | || _Alloc_traits::_S_always_equal() | |
873 | || _M_get_allocator() == __str._M_get_allocator()) | |
5caff414 | 874 | { |
71e09389 | 875 | // Just move the allocated pointer, our allocator can free it. |
5caff414 JW |
876 | pointer __data = nullptr; |
877 | size_type __capacity; | |
878 | if (!_M_is_local()) | |
879 | { | |
880 | if (_Alloc_traits::_S_always_equal()) | |
881 | { | |
71e09389 | 882 | // __str can reuse our existing storage. |
5caff414 JW |
883 | __data = _M_data(); |
884 | __capacity = _M_allocated_capacity; | |
885 | } | |
71e09389 | 886 | else // __str can't use it, so free it. |
5caff414 JW |
887 | _M_destroy(_M_allocated_capacity); |
888 | } | |
889 | ||
890 | _M_data(__str._M_data()); | |
891 | _M_length(__str.length()); | |
892 | _M_capacity(__str._M_allocated_capacity); | |
893 | if (__data) | |
894 | { | |
895 | __str._M_data(__data); | |
896 | __str._M_capacity(__capacity); | |
897 | } | |
898 | else | |
899 | __str._M_data(__str._M_local_buf); | |
900 | } | |
71e09389 JW |
901 | else // Need to do a deep copy |
902 | assign(__str); | |
5caff414 | 903 | __str.clear(); |
34a2b755 JW |
904 | return *this; |
905 | } | |
906 | ||
907 | /** | |
908 | * @brief Set value to string constructed from initializer %list. | |
909 | * @param __l std::initializer_list. | |
910 | */ | |
b96e2ff9 | 911 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
912 | basic_string& |
913 | operator=(initializer_list<_CharT> __l) | |
914 | { | |
915 | this->assign(__l.begin(), __l.size()); | |
916 | return *this; | |
917 | } | |
918 | #endif // C++11 | |
919 | ||
1a289fa3 | 920 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
921 | /** |
922 | * @brief Set value to string constructed from a string_view. | |
df66af3b | 923 | * @param __svt An object convertible to string_view. |
ca8f2cb1 | 924 | */ |
df66af3b | 925 | template<typename _Tp> |
b96e2ff9 | 926 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
927 | _If_sv<_Tp, basic_string&> |
928 | operator=(const _Tp& __svt) | |
929 | { return this->assign(__svt); } | |
ca8f2cb1 VV |
930 | |
931 | /** | |
932 | * @brief Convert to a string_view. | |
933 | * @return A string_view. | |
934 | */ | |
b96e2ff9 | 935 | _GLIBCXX20_CONSTEXPR |
ca8f2cb1 | 936 | operator __sv_type() const noexcept |
df66af3b | 937 | { return __sv_type(data(), size()); } |
ca8f2cb1 VV |
938 | #endif // C++17 |
939 | ||
34a2b755 JW |
940 | // Iterators: |
941 | /** | |
942 | * Returns a read/write iterator that points to the first character in | |
943 | * the %string. | |
944 | */ | |
b911ca42 | 945 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
946 | iterator |
947 | begin() _GLIBCXX_NOEXCEPT | |
948 | { return iterator(_M_data()); } | |
949 | ||
950 | /** | |
951 | * Returns a read-only (constant) iterator that points to the first | |
952 | * character in the %string. | |
953 | */ | |
b911ca42 | 954 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
955 | const_iterator |
956 | begin() const _GLIBCXX_NOEXCEPT | |
957 | { return const_iterator(_M_data()); } | |
958 | ||
959 | /** | |
960 | * Returns a read/write iterator that points one past the last | |
961 | * character in the %string. | |
962 | */ | |
b911ca42 | 963 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
964 | iterator |
965 | end() _GLIBCXX_NOEXCEPT | |
966 | { return iterator(_M_data() + this->size()); } | |
967 | ||
968 | /** | |
969 | * Returns a read-only (constant) iterator that points one past the | |
970 | * last character in the %string. | |
971 | */ | |
b911ca42 | 972 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
973 | const_iterator |
974 | end() const _GLIBCXX_NOEXCEPT | |
975 | { return const_iterator(_M_data() + this->size()); } | |
976 | ||
977 | /** | |
978 | * Returns a read/write reverse iterator that points to the last | |
979 | * character in the %string. Iteration is done in reverse element | |
980 | * order. | |
981 | */ | |
b911ca42 | 982 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
983 | reverse_iterator |
984 | rbegin() _GLIBCXX_NOEXCEPT | |
985 | { return reverse_iterator(this->end()); } | |
986 | ||
987 | /** | |
988 | * Returns a read-only (constant) reverse iterator that points | |
989 | * to the last character in the %string. Iteration is done in | |
990 | * reverse element order. | |
991 | */ | |
b911ca42 | 992 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
993 | const_reverse_iterator |
994 | rbegin() const _GLIBCXX_NOEXCEPT | |
995 | { return const_reverse_iterator(this->end()); } | |
996 | ||
997 | /** | |
998 | * Returns a read/write reverse iterator that points to one before the | |
999 | * first character in the %string. Iteration is done in reverse | |
1000 | * element order. | |
1001 | */ | |
b911ca42 | 1002 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1003 | reverse_iterator |
1004 | rend() _GLIBCXX_NOEXCEPT | |
1005 | { return reverse_iterator(this->begin()); } | |
1006 | ||
1007 | /** | |
1008 | * Returns a read-only (constant) reverse iterator that points | |
1009 | * to one before the first character in the %string. Iteration | |
1010 | * is done in reverse element order. | |
1011 | */ | |
b911ca42 | 1012 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1013 | const_reverse_iterator |
1014 | rend() const _GLIBCXX_NOEXCEPT | |
1015 | { return const_reverse_iterator(this->begin()); } | |
1016 | ||
1017 | #if __cplusplus >= 201103L | |
1018 | /** | |
1019 | * Returns a read-only (constant) iterator that points to the first | |
1020 | * character in the %string. | |
1021 | */ | |
b911ca42 | 1022 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1023 | const_iterator |
1024 | cbegin() const noexcept | |
1025 | { return const_iterator(this->_M_data()); } | |
1026 | ||
1027 | /** | |
1028 | * Returns a read-only (constant) iterator that points one past the | |
1029 | * last character in the %string. | |
1030 | */ | |
b911ca42 | 1031 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1032 | const_iterator |
1033 | cend() const noexcept | |
1034 | { return const_iterator(this->_M_data() + this->size()); } | |
1035 | ||
1036 | /** | |
1037 | * Returns a read-only (constant) reverse iterator that points | |
1038 | * to the last character in the %string. Iteration is done in | |
1039 | * reverse element order. | |
1040 | */ | |
b911ca42 | 1041 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1042 | const_reverse_iterator |
1043 | crbegin() const noexcept | |
1044 | { return const_reverse_iterator(this->end()); } | |
1045 | ||
1046 | /** | |
1047 | * Returns a read-only (constant) reverse iterator that points | |
1048 | * to one before the first character in the %string. Iteration | |
1049 | * is done in reverse element order. | |
1050 | */ | |
b911ca42 | 1051 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1052 | const_reverse_iterator |
1053 | crend() const noexcept | |
1054 | { return const_reverse_iterator(this->begin()); } | |
1055 | #endif | |
1056 | ||
1057 | public: | |
1058 | // Capacity: | |
1059 | /// Returns the number of characters in the string, not including any | |
1060 | /// null-termination. | |
b911ca42 | 1061 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1062 | size_type |
1063 | size() const _GLIBCXX_NOEXCEPT | |
1064 | { return _M_string_length; } | |
1065 | ||
1066 | /// Returns the number of characters in the string, not including any | |
1067 | /// null-termination. | |
b911ca42 | 1068 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1069 | size_type |
1070 | length() const _GLIBCXX_NOEXCEPT | |
1071 | { return _M_string_length; } | |
1072 | ||
1073 | /// Returns the size() of the largest possible %string. | |
b911ca42 | 1074 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1075 | size_type |
1076 | max_size() const _GLIBCXX_NOEXCEPT | |
1077 | { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; } | |
1078 | ||
1079 | /** | |
1080 | * @brief Resizes the %string to the specified number of characters. | |
1081 | * @param __n Number of characters the %string should contain. | |
1082 | * @param __c Character to fill any new elements. | |
1083 | * | |
1084 | * This function will %resize the %string to the specified | |
1085 | * number of characters. If the number is smaller than the | |
1086 | * %string's current size the %string is truncated, otherwise | |
1087 | * the %string is extended and new elements are %set to @a __c. | |
1088 | */ | |
b96e2ff9 | 1089 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1090 | void |
1091 | resize(size_type __n, _CharT __c); | |
1092 | ||
1093 | /** | |
1094 | * @brief Resizes the %string to the specified number of characters. | |
1095 | * @param __n Number of characters the %string should contain. | |
1096 | * | |
1097 | * This function will resize the %string to the specified length. If | |
1098 | * the new size is smaller than the %string's current size the %string | |
1099 | * is truncated, otherwise the %string is extended and new characters | |
1100 | * are default-constructed. For basic types such as char, this means | |
1101 | * setting them to 0. | |
1102 | */ | |
b96e2ff9 | 1103 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1104 | void |
1105 | resize(size_type __n) | |
1106 | { this->resize(__n, _CharT()); } | |
1107 | ||
1108 | #if __cplusplus >= 201103L | |
140cf935 AL |
1109 | #pragma GCC diagnostic push |
1110 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
34a2b755 | 1111 | /// A non-binding request to reduce capacity() to size(). |
b96e2ff9 | 1112 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1113 | void |
1114 | shrink_to_fit() noexcept | |
140cf935 AL |
1115 | { reserve(); } |
1116 | #pragma GCC diagnostic pop | |
34a2b755 JW |
1117 | #endif |
1118 | ||
929abc7f JW |
1119 | #if __cplusplus > 202002L |
1120 | #define __cpp_lib_string_resize_and_overwrite 202110L | |
1121 | template<typename _Operation> | |
1122 | constexpr void | |
1123 | resize_and_overwrite(size_type __n, _Operation __op); | |
1124 | #endif | |
1125 | ||
34a2b755 JW |
1126 | /** |
1127 | * Returns the total number of characters that the %string can hold | |
1128 | * before needing to allocate more memory. | |
1129 | */ | |
b911ca42 | 1130 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1131 | size_type |
1132 | capacity() const _GLIBCXX_NOEXCEPT | |
1133 | { | |
1134 | return _M_is_local() ? size_type(_S_local_capacity) | |
1135 | : _M_allocated_capacity; | |
1136 | } | |
1137 | ||
1138 | /** | |
1139 | * @brief Attempt to preallocate enough memory for specified number of | |
1140 | * characters. | |
1141 | * @param __res_arg Number of characters required. | |
1142 | * @throw std::length_error If @a __res_arg exceeds @c max_size(). | |
1143 | * | |
1144 | * This function attempts to reserve enough memory for the | |
1145 | * %string to hold the specified number of characters. If the | |
1146 | * number requested is more than max_size(), length_error is | |
1147 | * thrown. | |
1148 | * | |
1149 | * The advantage of this function is that if optimal code is a | |
1150 | * necessity and the user can determine the string length that will be | |
1151 | * required, the user can reserve the memory in %advance, and thus | |
1152 | * prevent a possible reallocation of memory and copying of %string | |
1153 | * data. | |
1154 | */ | |
b96e2ff9 | 1155 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 1156 | void |
140cf935 AL |
1157 | reserve(size_type __res_arg); |
1158 | ||
1159 | /** | |
1160 | * Equivalent to shrink_to_fit(). | |
1161 | */ | |
1162 | #if __cplusplus > 201703L | |
1163 | [[deprecated("use shrink_to_fit() instead")]] | |
1164 | #endif | |
b96e2ff9 | 1165 | _GLIBCXX20_CONSTEXPR |
140cf935 AL |
1166 | void |
1167 | reserve(); | |
34a2b755 JW |
1168 | |
1169 | /** | |
1170 | * Erases the string, making it empty. | |
1171 | */ | |
b96e2ff9 | 1172 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1173 | void |
1174 | clear() _GLIBCXX_NOEXCEPT | |
1175 | { _M_set_length(0); } | |
1176 | ||
1177 | /** | |
1178 | * Returns true if the %string is empty. Equivalent to | |
1179 | * <code>*this == ""</code>. | |
1180 | */ | |
b96e2ff9 ML |
1181 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
1182 | bool | |
34a2b755 JW |
1183 | empty() const _GLIBCXX_NOEXCEPT |
1184 | { return this->size() == 0; } | |
1185 | ||
1186 | // Element access: | |
1187 | /** | |
1188 | * @brief Subscript access to the data contained in the %string. | |
1189 | * @param __pos The index of the character to access. | |
1190 | * @return Read-only (constant) reference to the character. | |
1191 | * | |
1192 | * This operator allows for easy, array-style, data access. | |
1193 | * Note that data access with this operator is unchecked and | |
1194 | * out_of_range lookups are not defined. (For checked lookups | |
1195 | * see at().) | |
1196 | */ | |
b911ca42 | 1197 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1198 | const_reference |
1199 | operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT | |
1200 | { | |
2f1e8e7c | 1201 | __glibcxx_assert(__pos <= size()); |
34a2b755 JW |
1202 | return _M_data()[__pos]; |
1203 | } | |
1204 | ||
1205 | /** | |
1206 | * @brief Subscript access to the data contained in the %string. | |
1207 | * @param __pos The index of the character to access. | |
1208 | * @return Read/write reference to the character. | |
1209 | * | |
1210 | * This operator allows for easy, array-style, data access. | |
1211 | * Note that data access with this operator is unchecked and | |
1212 | * out_of_range lookups are not defined. (For checked lookups | |
1213 | * see at().) | |
1214 | */ | |
b911ca42 | 1215 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1216 | reference |
1217 | operator[](size_type __pos) | |
1218 | { | |
1219 | // Allow pos == size() both in C++98 mode, as v3 extension, | |
1220 | // and in C++11 mode. | |
2f1e8e7c | 1221 | __glibcxx_assert(__pos <= size()); |
34a2b755 JW |
1222 | // In pedantic mode be strict in C++98 mode. |
1223 | _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); | |
1224 | return _M_data()[__pos]; | |
1225 | } | |
1226 | ||
1227 | /** | |
1228 | * @brief Provides access to the data contained in the %string. | |
1229 | * @param __n The index of the character to access. | |
1230 | * @return Read-only (const) reference to the character. | |
1231 | * @throw std::out_of_range If @a n is an invalid index. | |
1232 | * | |
1233 | * This function provides for safer data access. The parameter is | |
1234 | * first checked that it is in the range of the string. The function | |
1235 | * throws out_of_range if the check fails. | |
1236 | */ | |
b911ca42 | 1237 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1238 | const_reference |
1239 | at(size_type __n) const | |
1240 | { | |
1241 | if (__n >= this->size()) | |
1242 | __throw_out_of_range_fmt(__N("basic_string::at: __n " | |
1243 | "(which is %zu) >= this->size() " | |
1244 | "(which is %zu)"), | |
1245 | __n, this->size()); | |
1246 | return _M_data()[__n]; | |
1247 | } | |
1248 | ||
1249 | /** | |
1250 | * @brief Provides access to the data contained in the %string. | |
1251 | * @param __n The index of the character to access. | |
1252 | * @return Read/write reference to the character. | |
1253 | * @throw std::out_of_range If @a n is an invalid index. | |
1254 | * | |
1255 | * This function provides for safer data access. The parameter is | |
1256 | * first checked that it is in the range of the string. The function | |
1257 | * throws out_of_range if the check fails. | |
1258 | */ | |
b911ca42 | 1259 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1260 | reference |
1261 | at(size_type __n) | |
1262 | { | |
1263 | if (__n >= size()) | |
1264 | __throw_out_of_range_fmt(__N("basic_string::at: __n " | |
1265 | "(which is %zu) >= this->size() " | |
1266 | "(which is %zu)"), | |
1267 | __n, this->size()); | |
1268 | return _M_data()[__n]; | |
1269 | } | |
1270 | ||
1271 | #if __cplusplus >= 201103L | |
1272 | /** | |
1273 | * Returns a read/write reference to the data at the first | |
1274 | * element of the %string. | |
1275 | */ | |
b911ca42 | 1276 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1277 | reference |
1278 | front() noexcept | |
e25d2617 | 1279 | { |
2f1e8e7c | 1280 | __glibcxx_assert(!empty()); |
e25d2617 FD |
1281 | return operator[](0); |
1282 | } | |
34a2b755 JW |
1283 | |
1284 | /** | |
1285 | * Returns a read-only (constant) reference to the data at the first | |
1286 | * element of the %string. | |
1287 | */ | |
b911ca42 | 1288 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1289 | const_reference |
1290 | front() const noexcept | |
e25d2617 | 1291 | { |
2f1e8e7c | 1292 | __glibcxx_assert(!empty()); |
e25d2617 FD |
1293 | return operator[](0); |
1294 | } | |
34a2b755 JW |
1295 | |
1296 | /** | |
1297 | * Returns a read/write reference to the data at the last | |
1298 | * element of the %string. | |
1299 | */ | |
b911ca42 | 1300 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1301 | reference |
1302 | back() noexcept | |
e25d2617 | 1303 | { |
2f1e8e7c | 1304 | __glibcxx_assert(!empty()); |
e25d2617 FD |
1305 | return operator[](this->size() - 1); |
1306 | } | |
34a2b755 JW |
1307 | |
1308 | /** | |
1309 | * Returns a read-only (constant) reference to the data at the | |
1310 | * last element of the %string. | |
1311 | */ | |
b911ca42 | 1312 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1313 | const_reference |
1314 | back() const noexcept | |
e25d2617 | 1315 | { |
2f1e8e7c | 1316 | __glibcxx_assert(!empty()); |
e25d2617 FD |
1317 | return operator[](this->size() - 1); |
1318 | } | |
34a2b755 JW |
1319 | #endif |
1320 | ||
1321 | // Modifiers: | |
1322 | /** | |
1323 | * @brief Append a string to this string. | |
1324 | * @param __str The string to append. | |
1325 | * @return Reference to this string. | |
1326 | */ | |
b96e2ff9 | 1327 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1328 | basic_string& |
1329 | operator+=(const basic_string& __str) | |
1330 | { return this->append(__str); } | |
1331 | ||
1332 | /** | |
1333 | * @brief Append a C string. | |
1334 | * @param __s The C string to append. | |
1335 | * @return Reference to this string. | |
1336 | */ | |
b96e2ff9 | 1337 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1338 | basic_string& |
1339 | operator+=(const _CharT* __s) | |
1340 | { return this->append(__s); } | |
1341 | ||
1342 | /** | |
1343 | * @brief Append a character. | |
1344 | * @param __c The character to append. | |
1345 | * @return Reference to this string. | |
1346 | */ | |
b96e2ff9 | 1347 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1348 | basic_string& |
1349 | operator+=(_CharT __c) | |
1350 | { | |
1351 | this->push_back(__c); | |
1352 | return *this; | |
1353 | } | |
1354 | ||
1355 | #if __cplusplus >= 201103L | |
1356 | /** | |
1357 | * @brief Append an initializer_list of characters. | |
1358 | * @param __l The initializer_list of characters to be appended. | |
1359 | * @return Reference to this string. | |
1360 | */ | |
b96e2ff9 | 1361 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1362 | basic_string& |
1363 | operator+=(initializer_list<_CharT> __l) | |
1364 | { return this->append(__l.begin(), __l.size()); } | |
1365 | #endif // C++11 | |
1366 | ||
1a289fa3 | 1367 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
1368 | /** |
1369 | * @brief Append a string_view. | |
df66af3b | 1370 | * @param __svt An object convertible to string_view to be appended. |
ca8f2cb1 VV |
1371 | * @return Reference to this string. |
1372 | */ | |
df66af3b | 1373 | template<typename _Tp> |
b96e2ff9 | 1374 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
1375 | _If_sv<_Tp, basic_string&> |
1376 | operator+=(const _Tp& __svt) | |
1377 | { return this->append(__svt); } | |
ca8f2cb1 VV |
1378 | #endif // C++17 |
1379 | ||
34a2b755 JW |
1380 | /** |
1381 | * @brief Append a string to this string. | |
1382 | * @param __str The string to append. | |
1383 | * @return Reference to this string. | |
1384 | */ | |
b96e2ff9 | 1385 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1386 | basic_string& |
1387 | append(const basic_string& __str) | |
2e806704 | 1388 | { return this->append(__str._M_data(), __str.size()); } |
34a2b755 JW |
1389 | |
1390 | /** | |
1391 | * @brief Append a substring. | |
1392 | * @param __str The string to append. | |
1393 | * @param __pos Index of the first character of str to append. | |
1394 | * @param __n The number of characters to append. | |
1395 | * @return Reference to this string. | |
1396 | * @throw std::out_of_range if @a __pos is not a valid index. | |
1397 | * | |
1398 | * This function appends @a __n characters from @a __str | |
1399 | * starting at @a __pos to this string. If @a __n is is larger | |
1400 | * than the number of available characters in @a __str, the | |
1401 | * remainder of @a __str is appended. | |
1402 | */ | |
b96e2ff9 | 1403 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 1404 | basic_string& |
852ee53c | 1405 | append(const basic_string& __str, size_type __pos, size_type __n = npos) |
2e806704 JM |
1406 | { return this->append(__str._M_data() |
1407 | + __str._M_check(__pos, "basic_string::append"), | |
1408 | __str._M_limit(__pos, __n)); } | |
34a2b755 JW |
1409 | |
1410 | /** | |
1411 | * @brief Append a C substring. | |
1412 | * @param __s The C string to append. | |
1413 | * @param __n The number of characters to append. | |
1414 | * @return Reference to this string. | |
1415 | */ | |
b96e2ff9 | 1416 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1417 | basic_string& |
1418 | append(const _CharT* __s, size_type __n) | |
1419 | { | |
1420 | __glibcxx_requires_string_len(__s, __n); | |
1421 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
1422 | return _M_append(__s, __n); | |
1423 | } | |
1424 | ||
1425 | /** | |
1426 | * @brief Append a C string. | |
1427 | * @param __s The C string to append. | |
1428 | * @return Reference to this string. | |
1429 | */ | |
b96e2ff9 | 1430 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1431 | basic_string& |
1432 | append(const _CharT* __s) | |
1433 | { | |
1434 | __glibcxx_requires_string(__s); | |
1435 | const size_type __n = traits_type::length(__s); | |
1436 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
1437 | return _M_append(__s, __n); | |
1438 | } | |
1439 | ||
1440 | /** | |
1441 | * @brief Append multiple characters. | |
1442 | * @param __n The number of characters to append. | |
1443 | * @param __c The character to use. | |
1444 | * @return Reference to this string. | |
1445 | * | |
1446 | * Appends __n copies of __c to this string. | |
1447 | */ | |
b96e2ff9 | 1448 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1449 | basic_string& |
1450 | append(size_type __n, _CharT __c) | |
1451 | { return _M_replace_aux(this->size(), size_type(0), __n, __c); } | |
1452 | ||
1453 | #if __cplusplus >= 201103L | |
1454 | /** | |
1455 | * @brief Append an initializer_list of characters. | |
1456 | * @param __l The initializer_list of characters to append. | |
1457 | * @return Reference to this string. | |
1458 | */ | |
b96e2ff9 | 1459 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1460 | basic_string& |
1461 | append(initializer_list<_CharT> __l) | |
1462 | { return this->append(__l.begin(), __l.size()); } | |
1463 | #endif // C++11 | |
1464 | ||
1465 | /** | |
1466 | * @brief Append a range of characters. | |
1467 | * @param __first Iterator referencing the first character to append. | |
1468 | * @param __last Iterator marking the end of the range. | |
1469 | * @return Reference to this string. | |
1470 | * | |
1471 | * Appends characters in the range [__first,__last) to this string. | |
1472 | */ | |
1473 | #if __cplusplus >= 201103L | |
1474 | template<class _InputIterator, | |
1475 | typename = std::_RequireInputIter<_InputIterator>> | |
b96e2ff9 | 1476 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1477 | #else |
1478 | template<class _InputIterator> | |
1479 | #endif | |
1480 | basic_string& | |
1481 | append(_InputIterator __first, _InputIterator __last) | |
1482 | { return this->replace(end(), end(), __first, __last); } | |
1483 | ||
1a289fa3 | 1484 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
1485 | /** |
1486 | * @brief Append a string_view. | |
df66af3b | 1487 | * @param __svt An object convertible to string_view to be appended. |
ca8f2cb1 VV |
1488 | * @return Reference to this string. |
1489 | */ | |
df66af3b | 1490 | template<typename _Tp> |
b96e2ff9 | 1491 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
1492 | _If_sv<_Tp, basic_string&> |
1493 | append(const _Tp& __svt) | |
1494 | { | |
1495 | __sv_type __sv = __svt; | |
1496 | return this->append(__sv.data(), __sv.size()); | |
1497 | } | |
ca8f2cb1 VV |
1498 | |
1499 | /** | |
1500 | * @brief Append a range of characters from a string_view. | |
df66af3b | 1501 | * @param __svt An object convertible to string_view to be appended from. |
ca8f2cb1 VV |
1502 | * @param __pos The position in the string_view to append from. |
1503 | * @param __n The number of characters to append from the string_view. | |
1504 | * @return Reference to this string. | |
1505 | */ | |
df66af3b | 1506 | template<typename _Tp> |
b96e2ff9 | 1507 | _GLIBCXX20_CONSTEXPR |
df66af3b | 1508 | _If_sv<_Tp, basic_string&> |
657213f7 JW |
1509 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) |
1510 | { | |
1511 | __sv_type __sv = __svt; | |
1512 | return _M_append(__sv.data() | |
fb8b3e29 JW |
1513 | + std::__sv_check(__sv.size(), __pos, "basic_string::append"), |
1514 | std::__sv_limit(__sv.size(), __pos, __n)); | |
657213f7 | 1515 | } |
ca8f2cb1 VV |
1516 | #endif // C++17 |
1517 | ||
34a2b755 JW |
1518 | /** |
1519 | * @brief Append a single character. | |
1520 | * @param __c Character to append. | |
1521 | */ | |
b96e2ff9 | 1522 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1523 | void |
1524 | push_back(_CharT __c) | |
1525 | { | |
1526 | const size_type __size = this->size(); | |
1527 | if (__size + 1 > this->capacity()) | |
1528 | this->_M_mutate(__size, size_type(0), 0, size_type(1)); | |
1529 | traits_type::assign(this->_M_data()[__size], __c); | |
1530 | this->_M_set_length(__size + 1); | |
1531 | } | |
1532 | ||
1533 | /** | |
1534 | * @brief Set value to contents of another string. | |
1535 | * @param __str Source string to use. | |
1536 | * @return Reference to this string. | |
1537 | */ | |
b96e2ff9 | 1538 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1539 | basic_string& |
1540 | assign(const basic_string& __str) | |
1541 | { | |
db33efde NDR |
1542 | #if __cplusplus >= 201103L |
1543 | if (_Alloc_traits::_S_propagate_on_copy_assign()) | |
1544 | { | |
1545 | if (!_Alloc_traits::_S_always_equal() && !_M_is_local() | |
1546 | && _M_get_allocator() != __str._M_get_allocator()) | |
1547 | { | |
1548 | // Propagating allocator cannot free existing storage so must | |
1549 | // deallocate it before replacing current allocator. | |
1550 | if (__str.size() <= _S_local_capacity) | |
1551 | { | |
1552 | _M_destroy(_M_allocated_capacity); | |
6afa1083 | 1553 | _M_data(_M_use_local_data()); |
db33efde NDR |
1554 | _M_set_length(0); |
1555 | } | |
1556 | else | |
1557 | { | |
1558 | const auto __len = __str.size(); | |
1559 | auto __alloc = __str._M_get_allocator(); | |
1560 | // If this allocation throws there are no effects: | |
1561 | auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1); | |
1562 | _M_destroy(_M_allocated_capacity); | |
1563 | _M_data(__ptr); | |
1564 | _M_capacity(__len); | |
1565 | _M_set_length(__len); | |
1566 | } | |
1567 | } | |
1568 | std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator()); | |
1569 | } | |
1570 | #endif | |
34a2b755 JW |
1571 | this->_M_assign(__str); |
1572 | return *this; | |
1573 | } | |
1574 | ||
1575 | #if __cplusplus >= 201103L | |
1576 | /** | |
1577 | * @brief Set value to contents of another string. | |
1578 | * @param __str Source string to use. | |
1579 | * @return Reference to this string. | |
1580 | * | |
1581 | * This function sets this string to the exact contents of @a __str. | |
1582 | * @a __str is a valid, but unspecified string. | |
1583 | */ | |
b96e2ff9 | 1584 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1585 | basic_string& |
1586 | assign(basic_string&& __str) | |
5caff414 | 1587 | noexcept(_Alloc_traits::_S_nothrow_move()) |
34a2b755 JW |
1588 | { |
1589 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1590 | // 2063. Contradictory requirements for string move assignment | |
1591 | return *this = std::move(__str); | |
1592 | } | |
1593 | #endif // C++11 | |
1594 | ||
1595 | /** | |
1596 | * @brief Set value to a substring of a string. | |
1597 | * @param __str The string to use. | |
1598 | * @param __pos Index of the first character of str. | |
1599 | * @param __n Number of characters to use. | |
1600 | * @return Reference to this string. | |
1601 | * @throw std::out_of_range if @a pos is not a valid index. | |
1602 | * | |
1603 | * This function sets this string to the substring of @a __str | |
1604 | * consisting of @a __n characters at @a __pos. If @a __n is | |
1605 | * is larger than the number of available characters in @a | |
1606 | * __str, the remainder of @a __str is used. | |
1607 | */ | |
b96e2ff9 | 1608 | _GLIBCXX20_CONSTEXPR |
34a2b755 | 1609 | basic_string& |
852ee53c | 1610 | assign(const basic_string& __str, size_type __pos, size_type __n = npos) |
2d60da10 | 1611 | { return _M_replace(size_type(0), this->size(), __str._M_data() |
34a2b755 JW |
1612 | + __str._M_check(__pos, "basic_string::assign"), |
1613 | __str._M_limit(__pos, __n)); } | |
1614 | ||
1615 | /** | |
1616 | * @brief Set value to a C substring. | |
1617 | * @param __s The C string to use. | |
1618 | * @param __n Number of characters to use. | |
1619 | * @return Reference to this string. | |
1620 | * | |
1621 | * This function sets the value of this string to the first @a __n | |
1622 | * characters of @a __s. If @a __n is is larger than the number of | |
1623 | * available characters in @a __s, the remainder of @a __s is used. | |
1624 | */ | |
b96e2ff9 | 1625 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1626 | basic_string& |
1627 | assign(const _CharT* __s, size_type __n) | |
1628 | { | |
1629 | __glibcxx_requires_string_len(__s, __n); | |
1630 | return _M_replace(size_type(0), this->size(), __s, __n); | |
1631 | } | |
1632 | ||
1633 | /** | |
1634 | * @brief Set value to contents of a C string. | |
1635 | * @param __s The C string to use. | |
1636 | * @return Reference to this string. | |
1637 | * | |
1638 | * This function sets the value of this string to the value of @a __s. | |
1639 | * The data is copied, so there is no dependence on @a __s once the | |
1640 | * function returns. | |
1641 | */ | |
b96e2ff9 | 1642 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1643 | basic_string& |
1644 | assign(const _CharT* __s) | |
1645 | { | |
1646 | __glibcxx_requires_string(__s); | |
1647 | return _M_replace(size_type(0), this->size(), __s, | |
1648 | traits_type::length(__s)); | |
1649 | } | |
1650 | ||
1651 | /** | |
1652 | * @brief Set value to multiple characters. | |
1653 | * @param __n Length of the resulting string. | |
1654 | * @param __c The character to use. | |
1655 | * @return Reference to this string. | |
1656 | * | |
1657 | * This function sets the value of this string to @a __n copies of | |
1658 | * character @a __c. | |
1659 | */ | |
b96e2ff9 | 1660 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1661 | basic_string& |
1662 | assign(size_type __n, _CharT __c) | |
1663 | { return _M_replace_aux(size_type(0), this->size(), __n, __c); } | |
1664 | ||
1665 | /** | |
1666 | * @brief Set value to a range of characters. | |
1667 | * @param __first Iterator referencing the first character to append. | |
1668 | * @param __last Iterator marking the end of the range. | |
1669 | * @return Reference to this string. | |
1670 | * | |
1671 | * Sets value of string to characters in the range [__first,__last). | |
1672 | */ | |
1673 | #if __cplusplus >= 201103L | |
1674 | template<class _InputIterator, | |
1675 | typename = std::_RequireInputIter<_InputIterator>> | |
b96e2ff9 | 1676 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1677 | #else |
1678 | template<class _InputIterator> | |
1679 | #endif | |
1680 | basic_string& | |
1681 | assign(_InputIterator __first, _InputIterator __last) | |
1682 | { return this->replace(begin(), end(), __first, __last); } | |
1683 | ||
1684 | #if __cplusplus >= 201103L | |
1685 | /** | |
1686 | * @brief Set value to an initializer_list of characters. | |
1687 | * @param __l The initializer_list of characters to assign. | |
1688 | * @return Reference to this string. | |
1689 | */ | |
b96e2ff9 | 1690 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1691 | basic_string& |
1692 | assign(initializer_list<_CharT> __l) | |
1693 | { return this->assign(__l.begin(), __l.size()); } | |
1694 | #endif // C++11 | |
1695 | ||
1a289fa3 | 1696 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
1697 | /** |
1698 | * @brief Set value from a string_view. | |
df66af3b | 1699 | * @param __svt The source object convertible to string_view. |
ca8f2cb1 VV |
1700 | * @return Reference to this string. |
1701 | */ | |
df66af3b | 1702 | template<typename _Tp> |
b96e2ff9 | 1703 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
1704 | _If_sv<_Tp, basic_string&> |
1705 | assign(const _Tp& __svt) | |
1706 | { | |
1707 | __sv_type __sv = __svt; | |
1708 | return this->assign(__sv.data(), __sv.size()); | |
1709 | } | |
ca8f2cb1 VV |
1710 | |
1711 | /** | |
1712 | * @brief Set value from a range of characters in a string_view. | |
df66af3b | 1713 | * @param __svt The source object convertible to string_view. |
ca8f2cb1 VV |
1714 | * @param __pos The position in the string_view to assign from. |
1715 | * @param __n The number of characters to assign. | |
1716 | * @return Reference to this string. | |
1717 | */ | |
df66af3b | 1718 | template<typename _Tp> |
b96e2ff9 | 1719 | _GLIBCXX20_CONSTEXPR |
657213f7 JW |
1720 | _If_sv<_Tp, basic_string&> |
1721 | assign(const _Tp& __svt, size_type __pos, size_type __n = npos) | |
1722 | { | |
1723 | __sv_type __sv = __svt; | |
fb8b3e29 JW |
1724 | return _M_replace(size_type(0), this->size(), |
1725 | __sv.data() | |
1726 | + std::__sv_check(__sv.size(), __pos, "basic_string::assign"), | |
1727 | std::__sv_limit(__sv.size(), __pos, __n)); | |
657213f7 | 1728 | } |
ca8f2cb1 VV |
1729 | #endif // C++17 |
1730 | ||
34a2b755 JW |
1731 | #if __cplusplus >= 201103L |
1732 | /** | |
1733 | * @brief Insert multiple characters. | |
1734 | * @param __p Const_iterator referencing location in string to | |
1735 | * insert at. | |
1736 | * @param __n Number of characters to insert | |
1737 | * @param __c The character to insert. | |
1738 | * @return Iterator referencing the first inserted char. | |
1739 | * @throw std::length_error If new length exceeds @c max_size(). | |
1740 | * | |
1741 | * Inserts @a __n copies of character @a __c starting at the | |
1742 | * position referenced by iterator @a __p. If adding | |
1743 | * characters causes the length to exceed max_size(), | |
1744 | * length_error is thrown. The value of the string doesn't | |
1745 | * change if an error is thrown. | |
1746 | */ | |
b96e2ff9 | 1747 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1748 | iterator |
1749 | insert(const_iterator __p, size_type __n, _CharT __c) | |
1750 | { | |
1751 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); | |
1752 | const size_type __pos = __p - begin(); | |
1753 | this->replace(__p, __p, __n, __c); | |
1754 | return iterator(this->_M_data() + __pos); | |
1755 | } | |
1756 | #else | |
1757 | /** | |
1758 | * @brief Insert multiple characters. | |
1759 | * @param __p Iterator referencing location in string to insert at. | |
1760 | * @param __n Number of characters to insert | |
1761 | * @param __c The character to insert. | |
1762 | * @throw std::length_error If new length exceeds @c max_size(). | |
1763 | * | |
1764 | * Inserts @a __n copies of character @a __c starting at the | |
1765 | * position referenced by iterator @a __p. If adding | |
1766 | * characters causes the length to exceed max_size(), | |
1767 | * length_error is thrown. The value of the string doesn't | |
1768 | * change if an error is thrown. | |
1769 | */ | |
1770 | void | |
1771 | insert(iterator __p, size_type __n, _CharT __c) | |
1772 | { this->replace(__p, __p, __n, __c); } | |
1773 | #endif | |
1774 | ||
1775 | #if __cplusplus >= 201103L | |
1776 | /** | |
1777 | * @brief Insert a range of characters. | |
1778 | * @param __p Const_iterator referencing location in string to | |
1779 | * insert at. | |
1780 | * @param __beg Start of range. | |
1781 | * @param __end End of range. | |
1782 | * @return Iterator referencing the first inserted char. | |
1783 | * @throw std::length_error If new length exceeds @c max_size(). | |
1784 | * | |
1785 | * Inserts characters in range [beg,end). If adding characters | |
1786 | * causes the length to exceed max_size(), length_error is | |
1787 | * thrown. The value of the string doesn't change if an error | |
1788 | * is thrown. | |
1789 | */ | |
1790 | template<class _InputIterator, | |
1791 | typename = std::_RequireInputIter<_InputIterator>> | |
b96e2ff9 | 1792 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1793 | iterator |
1794 | insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) | |
1795 | { | |
1796 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); | |
1797 | const size_type __pos = __p - begin(); | |
1798 | this->replace(__p, __p, __beg, __end); | |
1799 | return iterator(this->_M_data() + __pos); | |
1800 | } | |
1801 | #else | |
1802 | /** | |
1803 | * @brief Insert a range of characters. | |
1804 | * @param __p Iterator referencing location in string to insert at. | |
1805 | * @param __beg Start of range. | |
1806 | * @param __end End of range. | |
1807 | * @throw std::length_error If new length exceeds @c max_size(). | |
1808 | * | |
1809 | * Inserts characters in range [__beg,__end). If adding | |
1810 | * characters causes the length to exceed max_size(), | |
1811 | * length_error is thrown. The value of the string doesn't | |
1812 | * change if an error is thrown. | |
1813 | */ | |
1814 | template<class _InputIterator> | |
1815 | void | |
1816 | insert(iterator __p, _InputIterator __beg, _InputIterator __end) | |
1817 | { this->replace(__p, __p, __beg, __end); } | |
1818 | #endif | |
1819 | ||
1820 | #if __cplusplus >= 201103L | |
1821 | /** | |
1822 | * @brief Insert an initializer_list of characters. | |
1823 | * @param __p Iterator referencing location in string to insert at. | |
1824 | * @param __l The initializer_list of characters to insert. | |
1825 | * @throw std::length_error If new length exceeds @c max_size(). | |
1826 | */ | |
b96e2ff9 | 1827 | _GLIBCXX20_CONSTEXPR |
cda121ac JW |
1828 | iterator |
1829 | insert(const_iterator __p, initializer_list<_CharT> __l) | |
1830 | { return this->insert(__p, __l.begin(), __l.end()); } | |
1831 | ||
1832 | #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS | |
1833 | // See PR libstdc++/83328 | |
34a2b755 JW |
1834 | void |
1835 | insert(iterator __p, initializer_list<_CharT> __l) | |
1836 | { | |
1837 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); | |
1838 | this->insert(__p - begin(), __l.begin(), __l.size()); | |
1839 | } | |
cda121ac | 1840 | #endif |
34a2b755 JW |
1841 | #endif // C++11 |
1842 | ||
1843 | /** | |
1844 | * @brief Insert value of a string. | |
8c6a71e4 | 1845 | * @param __pos1 Position in string to insert at. |
34a2b755 JW |
1846 | * @param __str The string to insert. |
1847 | * @return Reference to this string. | |
1848 | * @throw std::length_error If new length exceeds @c max_size(). | |
1849 | * | |
1850 | * Inserts value of @a __str starting at @a __pos1. If adding | |
1851 | * characters causes the length to exceed max_size(), | |
1852 | * length_error is thrown. The value of the string doesn't | |
1853 | * change if an error is thrown. | |
1854 | */ | |
b96e2ff9 | 1855 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1856 | basic_string& |
1857 | insert(size_type __pos1, const basic_string& __str) | |
1858 | { return this->replace(__pos1, size_type(0), | |
2d60da10 | 1859 | __str._M_data(), __str.size()); } |
34a2b755 JW |
1860 | |
1861 | /** | |
1862 | * @brief Insert a substring. | |
8c6a71e4 JW |
1863 | * @param __pos1 Position in string to insert at. |
1864 | * @param __str The string to insert. | |
34a2b755 JW |
1865 | * @param __pos2 Start of characters in str to insert. |
1866 | * @param __n Number of characters to insert. | |
1867 | * @return Reference to this string. | |
1868 | * @throw std::length_error If new length exceeds @c max_size(). | |
1869 | * @throw std::out_of_range If @a pos1 > size() or | |
1870 | * @a __pos2 > @a str.size(). | |
1871 | * | |
1872 | * Starting at @a pos1, insert @a __n character of @a __str | |
1873 | * beginning with @a __pos2. If adding characters causes the | |
1874 | * length to exceed max_size(), length_error is thrown. If @a | |
1875 | * __pos1 is beyond the end of this string or @a __pos2 is | |
1876 | * beyond the end of @a __str, out_of_range is thrown. The | |
1877 | * value of the string doesn't change if an error is thrown. | |
1878 | */ | |
b96e2ff9 | 1879 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1880 | basic_string& |
1881 | insert(size_type __pos1, const basic_string& __str, | |
852ee53c | 1882 | size_type __pos2, size_type __n = npos) |
2d60da10 | 1883 | { return this->replace(__pos1, size_type(0), __str._M_data() |
34a2b755 JW |
1884 | + __str._M_check(__pos2, "basic_string::insert"), |
1885 | __str._M_limit(__pos2, __n)); } | |
1886 | ||
1887 | /** | |
1888 | * @brief Insert a C substring. | |
8c6a71e4 | 1889 | * @param __pos Position in string to insert at. |
34a2b755 JW |
1890 | * @param __s The C string to insert. |
1891 | * @param __n The number of characters to insert. | |
1892 | * @return Reference to this string. | |
1893 | * @throw std::length_error If new length exceeds @c max_size(). | |
1894 | * @throw std::out_of_range If @a __pos is beyond the end of this | |
1895 | * string. | |
1896 | * | |
1897 | * Inserts the first @a __n characters of @a __s starting at @a | |
1898 | * __pos. If adding characters causes the length to exceed | |
1899 | * max_size(), length_error is thrown. If @a __pos is beyond | |
1900 | * end(), out_of_range is thrown. The value of the string | |
1901 | * doesn't change if an error is thrown. | |
1902 | */ | |
b96e2ff9 | 1903 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1904 | basic_string& |
1905 | insert(size_type __pos, const _CharT* __s, size_type __n) | |
1906 | { return this->replace(__pos, size_type(0), __s, __n); } | |
1907 | ||
1908 | /** | |
1909 | * @brief Insert a C string. | |
8c6a71e4 | 1910 | * @param __pos Position in string to insert at. |
34a2b755 JW |
1911 | * @param __s The C string to insert. |
1912 | * @return Reference to this string. | |
1913 | * @throw std::length_error If new length exceeds @c max_size(). | |
1914 | * @throw std::out_of_range If @a pos is beyond the end of this | |
1915 | * string. | |
1916 | * | |
1917 | * Inserts the first @a n characters of @a __s starting at @a __pos. If | |
1918 | * adding characters causes the length to exceed max_size(), | |
1919 | * length_error is thrown. If @a __pos is beyond end(), out_of_range is | |
1920 | * thrown. The value of the string doesn't change if an error is | |
1921 | * thrown. | |
1922 | */ | |
b96e2ff9 | 1923 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1924 | basic_string& |
1925 | insert(size_type __pos, const _CharT* __s) | |
1926 | { | |
1927 | __glibcxx_requires_string(__s); | |
1928 | return this->replace(__pos, size_type(0), __s, | |
1929 | traits_type::length(__s)); | |
1930 | } | |
1931 | ||
1932 | /** | |
1933 | * @brief Insert multiple characters. | |
1934 | * @param __pos Index in string to insert at. | |
1935 | * @param __n Number of characters to insert | |
1936 | * @param __c The character to insert. | |
1937 | * @return Reference to this string. | |
1938 | * @throw std::length_error If new length exceeds @c max_size(). | |
1939 | * @throw std::out_of_range If @a __pos is beyond the end of this | |
1940 | * string. | |
1941 | * | |
1942 | * Inserts @a __n copies of character @a __c starting at index | |
1943 | * @a __pos. If adding characters causes the length to exceed | |
1944 | * max_size(), length_error is thrown. If @a __pos > length(), | |
1945 | * out_of_range is thrown. The value of the string doesn't | |
1946 | * change if an error is thrown. | |
1947 | */ | |
b96e2ff9 | 1948 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1949 | basic_string& |
1950 | insert(size_type __pos, size_type __n, _CharT __c) | |
1951 | { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), | |
1952 | size_type(0), __n, __c); } | |
1953 | ||
1954 | /** | |
1955 | * @brief Insert one character. | |
1956 | * @param __p Iterator referencing position in string to insert at. | |
1957 | * @param __c The character to insert. | |
1958 | * @return Iterator referencing newly inserted char. | |
1959 | * @throw std::length_error If new length exceeds @c max_size(). | |
1960 | * | |
1961 | * Inserts character @a __c at position referenced by @a __p. | |
1962 | * If adding character causes the length to exceed max_size(), | |
1963 | * length_error is thrown. If @a __p is beyond end of string, | |
1964 | * out_of_range is thrown. The value of the string doesn't | |
1965 | * change if an error is thrown. | |
1966 | */ | |
b96e2ff9 | 1967 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
1968 | iterator |
1969 | insert(__const_iterator __p, _CharT __c) | |
1970 | { | |
1971 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); | |
1972 | const size_type __pos = __p - begin(); | |
1973 | _M_replace_aux(__pos, size_type(0), size_type(1), __c); | |
1974 | return iterator(_M_data() + __pos); | |
1975 | } | |
1976 | ||
1a289fa3 | 1977 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
1978 | /** |
1979 | * @brief Insert a string_view. | |
8c6a71e4 | 1980 | * @param __pos Position in string to insert at. |
df66af3b | 1981 | * @param __svt The object convertible to string_view to insert. |
ca8f2cb1 VV |
1982 | * @return Reference to this string. |
1983 | */ | |
df66af3b | 1984 | template<typename _Tp> |
b96e2ff9 | 1985 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
1986 | _If_sv<_Tp, basic_string&> |
1987 | insert(size_type __pos, const _Tp& __svt) | |
1988 | { | |
1989 | __sv_type __sv = __svt; | |
1990 | return this->insert(__pos, __sv.data(), __sv.size()); | |
1991 | } | |
ca8f2cb1 VV |
1992 | |
1993 | /** | |
1994 | * @brief Insert a string_view. | |
8c6a71e4 JW |
1995 | * @param __pos1 Position in string to insert at. |
1996 | * @param __svt The object convertible to string_view to insert from. | |
1997 | * @param __pos2 Start of characters in str to insert. | |
ca8f2cb1 VV |
1998 | * @param __n The number of characters to insert. |
1999 | * @return Reference to this string. | |
2000 | */ | |
df66af3b | 2001 | template<typename _Tp> |
b96e2ff9 | 2002 | _GLIBCXX20_CONSTEXPR |
657213f7 JW |
2003 | _If_sv<_Tp, basic_string&> |
2004 | insert(size_type __pos1, const _Tp& __svt, | |
2005 | size_type __pos2, size_type __n = npos) | |
2006 | { | |
2007 | __sv_type __sv = __svt; | |
fb8b3e29 JW |
2008 | return this->replace(__pos1, size_type(0), |
2009 | __sv.data() | |
2010 | + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"), | |
2011 | std::__sv_limit(__sv.size(), __pos2, __n)); | |
657213f7 | 2012 | } |
ca8f2cb1 VV |
2013 | #endif // C++17 |
2014 | ||
34a2b755 JW |
2015 | /** |
2016 | * @brief Remove characters. | |
2017 | * @param __pos Index of first character to remove (default 0). | |
2018 | * @param __n Number of characters to remove (default remainder). | |
2019 | * @return Reference to this string. | |
2020 | * @throw std::out_of_range If @a pos is beyond the end of this | |
2021 | * string. | |
2022 | * | |
2023 | * Removes @a __n characters from this string starting at @a | |
2024 | * __pos. The length of the string is reduced by @a __n. If | |
2025 | * there are < @a __n characters to remove, the remainder of | |
2026 | * the string is truncated. If @a __p is beyond end of string, | |
2027 | * out_of_range is thrown. The value of the string doesn't | |
2028 | * change if an error is thrown. | |
2029 | */ | |
b96e2ff9 | 2030 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2031 | basic_string& |
2032 | erase(size_type __pos = 0, size_type __n = npos) | |
2033 | { | |
a922c5ff JW |
2034 | _M_check(__pos, "basic_string::erase"); |
2035 | if (__n == npos) | |
2036 | this->_M_set_length(__pos); | |
2037 | else if (__n != 0) | |
2038 | this->_M_erase(__pos, _M_limit(__pos, __n)); | |
34a2b755 JW |
2039 | return *this; |
2040 | } | |
2041 | ||
2042 | /** | |
2043 | * @brief Remove one character. | |
2044 | * @param __position Iterator referencing the character to remove. | |
2045 | * @return iterator referencing same location after removal. | |
2046 | * | |
2047 | * Removes the character at @a __position from this string. The value | |
2048 | * of the string doesn't change if an error is thrown. | |
2049 | */ | |
b96e2ff9 | 2050 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2051 | iterator |
2052 | erase(__const_iterator __position) | |
2053 | { | |
2054 | _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() | |
2055 | && __position < end()); | |
2056 | const size_type __pos = __position - begin(); | |
2057 | this->_M_erase(__pos, size_type(1)); | |
2058 | return iterator(_M_data() + __pos); | |
2059 | } | |
2060 | ||
2061 | /** | |
2062 | * @brief Remove a range of characters. | |
2063 | * @param __first Iterator referencing the first character to remove. | |
2064 | * @param __last Iterator referencing the end of the range. | |
2065 | * @return Iterator referencing location of first after removal. | |
2066 | * | |
2067 | * Removes the characters in the range [first,last) from this string. | |
2068 | * The value of the string doesn't change if an error is thrown. | |
2069 | */ | |
b96e2ff9 | 2070 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2071 | iterator |
2072 | erase(__const_iterator __first, __const_iterator __last) | |
2073 | { | |
2074 | _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last | |
2075 | && __last <= end()); | |
2076 | const size_type __pos = __first - begin(); | |
a922c5ff JW |
2077 | if (__last == end()) |
2078 | this->_M_set_length(__pos); | |
2079 | else | |
2080 | this->_M_erase(__pos, __last - __first); | |
34a2b755 JW |
2081 | return iterator(this->_M_data() + __pos); |
2082 | } | |
2083 | ||
2084 | #if __cplusplus >= 201103L | |
2085 | /** | |
2086 | * @brief Remove the last character. | |
2087 | * | |
2088 | * The string must be non-empty. | |
2089 | */ | |
b96e2ff9 | 2090 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2091 | void |
2092 | pop_back() noexcept | |
e25d2617 | 2093 | { |
2f1e8e7c | 2094 | __glibcxx_assert(!empty()); |
e25d2617 FD |
2095 | _M_erase(size() - 1, 1); |
2096 | } | |
34a2b755 JW |
2097 | #endif // C++11 |
2098 | ||
2099 | /** | |
2100 | * @brief Replace characters with value from another string. | |
2101 | * @param __pos Index of first character to replace. | |
2102 | * @param __n Number of characters to be replaced. | |
2103 | * @param __str String to insert. | |
2104 | * @return Reference to this string. | |
2105 | * @throw std::out_of_range If @a pos is beyond the end of this | |
2106 | * string. | |
2107 | * @throw std::length_error If new length exceeds @c max_size(). | |
2108 | * | |
2109 | * Removes the characters in the range [__pos,__pos+__n) from | |
2110 | * this string. In place, the value of @a __str is inserted. | |
2111 | * If @a __pos is beyond end of string, out_of_range is thrown. | |
2112 | * If the length of the result exceeds max_size(), length_error | |
2113 | * is thrown. The value of the string doesn't change if an | |
2114 | * error is thrown. | |
2115 | */ | |
b96e2ff9 | 2116 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2117 | basic_string& |
2118 | replace(size_type __pos, size_type __n, const basic_string& __str) | |
2d60da10 | 2119 | { return this->replace(__pos, __n, __str._M_data(), __str.size()); } |
34a2b755 JW |
2120 | |
2121 | /** | |
2122 | * @brief Replace characters with value from another string. | |
2123 | * @param __pos1 Index of first character to replace. | |
2124 | * @param __n1 Number of characters to be replaced. | |
2125 | * @param __str String to insert. | |
2126 | * @param __pos2 Index of first character of str to use. | |
2127 | * @param __n2 Number of characters from str to use. | |
2128 | * @return Reference to this string. | |
2129 | * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > | |
2130 | * __str.size(). | |
2131 | * @throw std::length_error If new length exceeds @c max_size(). | |
2132 | * | |
2133 | * Removes the characters in the range [__pos1,__pos1 + n) from this | |
2134 | * string. In place, the value of @a __str is inserted. If @a __pos is | |
2135 | * beyond end of string, out_of_range is thrown. If the length of the | |
2136 | * result exceeds max_size(), length_error is thrown. The value of the | |
2137 | * string doesn't change if an error is thrown. | |
2138 | */ | |
b96e2ff9 | 2139 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2140 | basic_string& |
2141 | replace(size_type __pos1, size_type __n1, const basic_string& __str, | |
852ee53c | 2142 | size_type __pos2, size_type __n2 = npos) |
2d60da10 | 2143 | { return this->replace(__pos1, __n1, __str._M_data() |
34a2b755 JW |
2144 | + __str._M_check(__pos2, "basic_string::replace"), |
2145 | __str._M_limit(__pos2, __n2)); } | |
2146 | ||
2147 | /** | |
2148 | * @brief Replace characters with value of a C substring. | |
2149 | * @param __pos Index of first character to replace. | |
2150 | * @param __n1 Number of characters to be replaced. | |
2151 | * @param __s C string to insert. | |
2152 | * @param __n2 Number of characters from @a s to use. | |
2153 | * @return Reference to this string. | |
2154 | * @throw std::out_of_range If @a pos1 > size(). | |
2155 | * @throw std::length_error If new length exceeds @c max_size(). | |
2156 | * | |
2157 | * Removes the characters in the range [__pos,__pos + __n1) | |
2158 | * from this string. In place, the first @a __n2 characters of | |
2159 | * @a __s are inserted, or all of @a __s if @a __n2 is too large. If | |
2160 | * @a __pos is beyond end of string, out_of_range is thrown. If | |
2161 | * the length of result exceeds max_size(), length_error is | |
2162 | * thrown. The value of the string doesn't change if an error | |
2163 | * is thrown. | |
2164 | */ | |
b96e2ff9 | 2165 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2166 | basic_string& |
2167 | replace(size_type __pos, size_type __n1, const _CharT* __s, | |
2168 | size_type __n2) | |
2169 | { | |
2170 | __glibcxx_requires_string_len(__s, __n2); | |
2171 | return _M_replace(_M_check(__pos, "basic_string::replace"), | |
2172 | _M_limit(__pos, __n1), __s, __n2); | |
2173 | } | |
2174 | ||
2175 | /** | |
2176 | * @brief Replace characters with value of a C string. | |
2177 | * @param __pos Index of first character to replace. | |
2178 | * @param __n1 Number of characters to be replaced. | |
2179 | * @param __s C string to insert. | |
2180 | * @return Reference to this string. | |
2181 | * @throw std::out_of_range If @a pos > size(). | |
2182 | * @throw std::length_error If new length exceeds @c max_size(). | |
2183 | * | |
2184 | * Removes the characters in the range [__pos,__pos + __n1) | |
2185 | * from this string. In place, the characters of @a __s are | |
2186 | * inserted. If @a __pos is beyond end of string, out_of_range | |
2187 | * is thrown. If the length of result exceeds max_size(), | |
2188 | * length_error is thrown. The value of the string doesn't | |
2189 | * change if an error is thrown. | |
2190 | */ | |
b96e2ff9 | 2191 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2192 | basic_string& |
2193 | replace(size_type __pos, size_type __n1, const _CharT* __s) | |
2194 | { | |
2195 | __glibcxx_requires_string(__s); | |
2196 | return this->replace(__pos, __n1, __s, traits_type::length(__s)); | |
2197 | } | |
2198 | ||
2199 | /** | |
2200 | * @brief Replace characters with multiple characters. | |
2201 | * @param __pos Index of first character to replace. | |
2202 | * @param __n1 Number of characters to be replaced. | |
2203 | * @param __n2 Number of characters to insert. | |
2204 | * @param __c Character to insert. | |
2205 | * @return Reference to this string. | |
2206 | * @throw std::out_of_range If @a __pos > size(). | |
2207 | * @throw std::length_error If new length exceeds @c max_size(). | |
2208 | * | |
2209 | * Removes the characters in the range [pos,pos + n1) from this | |
2210 | * string. In place, @a __n2 copies of @a __c are inserted. | |
2211 | * If @a __pos is beyond end of string, out_of_range is thrown. | |
2212 | * If the length of result exceeds max_size(), length_error is | |
2213 | * thrown. The value of the string doesn't change if an error | |
2214 | * is thrown. | |
2215 | */ | |
b96e2ff9 | 2216 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2217 | basic_string& |
2218 | replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) | |
2219 | { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), | |
2220 | _M_limit(__pos, __n1), __n2, __c); } | |
2221 | ||
2222 | /** | |
2223 | * @brief Replace range of characters with string. | |
2224 | * @param __i1 Iterator referencing start of range to replace. | |
2225 | * @param __i2 Iterator referencing end of range to replace. | |
2226 | * @param __str String value to insert. | |
2227 | * @return Reference to this string. | |
2228 | * @throw std::length_error If new length exceeds @c max_size(). | |
2229 | * | |
2230 | * Removes the characters in the range [__i1,__i2). In place, | |
2231 | * the value of @a __str is inserted. If the length of result | |
2232 | * exceeds max_size(), length_error is thrown. The value of | |
2233 | * the string doesn't change if an error is thrown. | |
2234 | */ | |
b96e2ff9 | 2235 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2236 | basic_string& |
2237 | replace(__const_iterator __i1, __const_iterator __i2, | |
2238 | const basic_string& __str) | |
2d60da10 | 2239 | { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } |
34a2b755 JW |
2240 | |
2241 | /** | |
2242 | * @brief Replace range of characters with C substring. | |
2243 | * @param __i1 Iterator referencing start of range to replace. | |
2244 | * @param __i2 Iterator referencing end of range to replace. | |
2245 | * @param __s C string value to insert. | |
2246 | * @param __n Number of characters from s to insert. | |
2247 | * @return Reference to this string. | |
2248 | * @throw std::length_error If new length exceeds @c max_size(). | |
2249 | * | |
2250 | * Removes the characters in the range [__i1,__i2). In place, | |
2251 | * the first @a __n characters of @a __s are inserted. If the | |
2252 | * length of result exceeds max_size(), length_error is thrown. | |
2253 | * The value of the string doesn't change if an error is | |
2254 | * thrown. | |
2255 | */ | |
b96e2ff9 | 2256 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2257 | basic_string& |
2258 | replace(__const_iterator __i1, __const_iterator __i2, | |
2259 | const _CharT* __s, size_type __n) | |
2260 | { | |
2261 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2262 | && __i2 <= end()); | |
2263 | return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); | |
2264 | } | |
2265 | ||
2266 | /** | |
2267 | * @brief Replace range of characters with C string. | |
2268 | * @param __i1 Iterator referencing start of range to replace. | |
2269 | * @param __i2 Iterator referencing end of range to replace. | |
2270 | * @param __s C string value to insert. | |
2271 | * @return Reference to this string. | |
2272 | * @throw std::length_error If new length exceeds @c max_size(). | |
2273 | * | |
2274 | * Removes the characters in the range [__i1,__i2). In place, | |
2275 | * the characters of @a __s are inserted. If the length of | |
2276 | * result exceeds max_size(), length_error is thrown. The | |
2277 | * value of the string doesn't change if an error is thrown. | |
2278 | */ | |
b96e2ff9 | 2279 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2280 | basic_string& |
2281 | replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) | |
2282 | { | |
2283 | __glibcxx_requires_string(__s); | |
2284 | return this->replace(__i1, __i2, __s, traits_type::length(__s)); | |
2285 | } | |
2286 | ||
2287 | /** | |
2288 | * @brief Replace range of characters with multiple characters | |
2289 | * @param __i1 Iterator referencing start of range to replace. | |
2290 | * @param __i2 Iterator referencing end of range to replace. | |
2291 | * @param __n Number of characters to insert. | |
2292 | * @param __c Character to insert. | |
2293 | * @return Reference to this string. | |
2294 | * @throw std::length_error If new length exceeds @c max_size(). | |
2295 | * | |
2296 | * Removes the characters in the range [__i1,__i2). In place, | |
2297 | * @a __n copies of @a __c are inserted. If the length of | |
2298 | * result exceeds max_size(), length_error is thrown. The | |
2299 | * value of the string doesn't change if an error is thrown. | |
2300 | */ | |
b96e2ff9 | 2301 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2302 | basic_string& |
2303 | replace(__const_iterator __i1, __const_iterator __i2, size_type __n, | |
2304 | _CharT __c) | |
2305 | { | |
2306 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2307 | && __i2 <= end()); | |
2308 | return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); | |
2309 | } | |
2310 | ||
2311 | /** | |
2312 | * @brief Replace range of characters with range. | |
2313 | * @param __i1 Iterator referencing start of range to replace. | |
2314 | * @param __i2 Iterator referencing end of range to replace. | |
2315 | * @param __k1 Iterator referencing start of range to insert. | |
2316 | * @param __k2 Iterator referencing end of range to insert. | |
2317 | * @return Reference to this string. | |
2318 | * @throw std::length_error If new length exceeds @c max_size(). | |
2319 | * | |
2320 | * Removes the characters in the range [__i1,__i2). In place, | |
2321 | * characters in the range [__k1,__k2) are inserted. If the | |
2322 | * length of result exceeds max_size(), length_error is thrown. | |
2323 | * The value of the string doesn't change if an error is | |
2324 | * thrown. | |
2325 | */ | |
2326 | #if __cplusplus >= 201103L | |
2327 | template<class _InputIterator, | |
2328 | typename = std::_RequireInputIter<_InputIterator>> | |
b96e2ff9 | 2329 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2330 | basic_string& |
2331 | replace(const_iterator __i1, const_iterator __i2, | |
2332 | _InputIterator __k1, _InputIterator __k2) | |
2333 | { | |
2334 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2335 | && __i2 <= end()); | |
2336 | __glibcxx_requires_valid_range(__k1, __k2); | |
2337 | return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, | |
2338 | std::__false_type()); | |
2339 | } | |
2340 | #else | |
2341 | template<class _InputIterator> | |
2342 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST | |
2343 | typename __enable_if_not_native_iterator<_InputIterator>::__type | |
2344 | #else | |
2345 | basic_string& | |
2346 | #endif | |
2347 | replace(iterator __i1, iterator __i2, | |
2348 | _InputIterator __k1, _InputIterator __k2) | |
2349 | { | |
2350 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2351 | && __i2 <= end()); | |
2352 | __glibcxx_requires_valid_range(__k1, __k2); | |
2353 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; | |
2354 | return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); | |
2355 | } | |
2356 | #endif | |
2357 | ||
2358 | // Specializations for the common case of pointer and iterator: | |
2359 | // useful to avoid the overhead of temporary buffering in _M_replace. | |
b96e2ff9 | 2360 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2361 | basic_string& |
2362 | replace(__const_iterator __i1, __const_iterator __i2, | |
2363 | _CharT* __k1, _CharT* __k2) | |
2364 | { | |
2365 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2366 | && __i2 <= end()); | |
2367 | __glibcxx_requires_valid_range(__k1, __k2); | |
2368 | return this->replace(__i1 - begin(), __i2 - __i1, | |
2369 | __k1, __k2 - __k1); | |
2370 | } | |
2371 | ||
b96e2ff9 | 2372 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2373 | basic_string& |
2374 | replace(__const_iterator __i1, __const_iterator __i2, | |
2375 | const _CharT* __k1, const _CharT* __k2) | |
2376 | { | |
2377 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2378 | && __i2 <= end()); | |
2379 | __glibcxx_requires_valid_range(__k1, __k2); | |
2380 | return this->replace(__i1 - begin(), __i2 - __i1, | |
2381 | __k1, __k2 - __k1); | |
2382 | } | |
2383 | ||
b96e2ff9 | 2384 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2385 | basic_string& |
2386 | replace(__const_iterator __i1, __const_iterator __i2, | |
2387 | iterator __k1, iterator __k2) | |
2388 | { | |
2389 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2390 | && __i2 <= end()); | |
2391 | __glibcxx_requires_valid_range(__k1, __k2); | |
2392 | return this->replace(__i1 - begin(), __i2 - __i1, | |
2393 | __k1.base(), __k2 - __k1); | |
2394 | } | |
2395 | ||
b96e2ff9 | 2396 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2397 | basic_string& |
2398 | replace(__const_iterator __i1, __const_iterator __i2, | |
2399 | const_iterator __k1, const_iterator __k2) | |
2400 | { | |
2401 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 | |
2402 | && __i2 <= end()); | |
2403 | __glibcxx_requires_valid_range(__k1, __k2); | |
2404 | return this->replace(__i1 - begin(), __i2 - __i1, | |
2405 | __k1.base(), __k2 - __k1); | |
2406 | } | |
2407 | ||
2408 | #if __cplusplus >= 201103L | |
2409 | /** | |
2410 | * @brief Replace range of characters with initializer_list. | |
2411 | * @param __i1 Iterator referencing start of range to replace. | |
2412 | * @param __i2 Iterator referencing end of range to replace. | |
2413 | * @param __l The initializer_list of characters to insert. | |
2414 | * @return Reference to this string. | |
2415 | * @throw std::length_error If new length exceeds @c max_size(). | |
2416 | * | |
2417 | * Removes the characters in the range [__i1,__i2). In place, | |
2418 | * characters in the range [__k1,__k2) are inserted. If the | |
2419 | * length of result exceeds max_size(), length_error is thrown. | |
2420 | * The value of the string doesn't change if an error is | |
2421 | * thrown. | |
2422 | */ | |
b96e2ff9 | 2423 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2424 | basic_string& replace(const_iterator __i1, const_iterator __i2, |
2425 | initializer_list<_CharT> __l) | |
17f8dc93 | 2426 | { return this->replace(__i1, __i2, __l.begin(), __l.size()); } |
34a2b755 JW |
2427 | #endif // C++11 |
2428 | ||
1a289fa3 | 2429 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2430 | /** |
2431 | * @brief Replace range of characters with string_view. | |
2432 | * @param __pos The position to replace at. | |
2433 | * @param __n The number of characters to replace. | |
df66af3b | 2434 | * @param __svt The object convertible to string_view to insert. |
ca8f2cb1 VV |
2435 | * @return Reference to this string. |
2436 | */ | |
df66af3b | 2437 | template<typename _Tp> |
b96e2ff9 | 2438 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2439 | _If_sv<_Tp, basic_string&> |
2440 | replace(size_type __pos, size_type __n, const _Tp& __svt) | |
2441 | { | |
2442 | __sv_type __sv = __svt; | |
2443 | return this->replace(__pos, __n, __sv.data(), __sv.size()); | |
2444 | } | |
ca8f2cb1 VV |
2445 | |
2446 | /** | |
2447 | * @brief Replace range of characters with string_view. | |
2448 | * @param __pos1 The position to replace at. | |
2449 | * @param __n1 The number of characters to replace. | |
df66af3b | 2450 | * @param __svt The object convertible to string_view to insert from. |
ca8f2cb1 VV |
2451 | * @param __pos2 The position in the string_view to insert from. |
2452 | * @param __n2 The number of characters to insert. | |
2453 | * @return Reference to this string. | |
2454 | */ | |
df66af3b | 2455 | template<typename _Tp> |
b96e2ff9 | 2456 | _GLIBCXX20_CONSTEXPR |
657213f7 JW |
2457 | _If_sv<_Tp, basic_string&> |
2458 | replace(size_type __pos1, size_type __n1, const _Tp& __svt, | |
2459 | size_type __pos2, size_type __n2 = npos) | |
2460 | { | |
2461 | __sv_type __sv = __svt; | |
fb8b3e29 JW |
2462 | return this->replace(__pos1, __n1, |
2463 | __sv.data() | |
2464 | + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"), | |
2465 | std::__sv_limit(__sv.size(), __pos2, __n2)); | |
657213f7 | 2466 | } |
ca8f2cb1 VV |
2467 | |
2468 | /** | |
2469 | * @brief Replace range of characters with string_view. | |
2470 | * @param __i1 An iterator referencing the start position | |
2471 | to replace at. | |
2472 | * @param __i2 An iterator referencing the end position | |
2473 | for the replace. | |
df66af3b | 2474 | * @param __svt The object convertible to string_view to insert from. |
ca8f2cb1 VV |
2475 | * @return Reference to this string. |
2476 | */ | |
df66af3b | 2477 | template<typename _Tp> |
b96e2ff9 | 2478 | _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2479 | _If_sv<_Tp, basic_string&> |
2480 | replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) | |
2481 | { | |
2482 | __sv_type __sv = __svt; | |
2483 | return this->replace(__i1 - begin(), __i2 - __i1, __sv); | |
2484 | } | |
ca8f2cb1 VV |
2485 | #endif // C++17 |
2486 | ||
34a2b755 JW |
2487 | private: |
2488 | template<class _Integer> | |
b96e2ff9 | 2489 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2490 | basic_string& |
2491 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, | |
2492 | _Integer __n, _Integer __val, __true_type) | |
2493 | { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } | |
2494 | ||
2495 | template<class _InputIterator> | |
b96e2ff9 | 2496 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2497 | basic_string& |
2498 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, | |
2499 | _InputIterator __k1, _InputIterator __k2, | |
2500 | __false_type); | |
2501 | ||
b96e2ff9 | 2502 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2503 | basic_string& |
2504 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, | |
2505 | _CharT __c); | |
2506 | ||
723ef5a9 JJ |
2507 | __attribute__((__noinline__, __noclone__, __cold__)) void |
2508 | _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s, | |
2509 | const size_type __len2, const size_type __how_much); | |
2510 | ||
b96e2ff9 | 2511 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2512 | basic_string& |
2513 | _M_replace(size_type __pos, size_type __len1, const _CharT* __s, | |
2514 | const size_type __len2); | |
2515 | ||
b96e2ff9 | 2516 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2517 | basic_string& |
2518 | _M_append(const _CharT* __s, size_type __n); | |
2519 | ||
2520 | public: | |
2521 | ||
2522 | /** | |
2523 | * @brief Copy substring into C string. | |
2524 | * @param __s C string to copy value into. | |
2525 | * @param __n Number of characters to copy. | |
2526 | * @param __pos Index of first character to copy. | |
2527 | * @return Number of characters actually copied | |
2528 | * @throw std::out_of_range If __pos > size(). | |
2529 | * | |
2530 | * Copies up to @a __n characters starting at @a __pos into the | |
2531 | * C string @a __s. If @a __pos is %greater than size(), | |
2532 | * out_of_range is thrown. | |
2533 | */ | |
b96e2ff9 | 2534 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2535 | size_type |
2536 | copy(_CharT* __s, size_type __n, size_type __pos = 0) const; | |
2537 | ||
2538 | /** | |
2539 | * @brief Swap contents with another string. | |
2540 | * @param __s String to swap with. | |
2541 | * | |
2542 | * Exchanges the contents of this string with that of @a __s in constant | |
2543 | * time. | |
2544 | */ | |
b96e2ff9 | 2545 | _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2546 | void |
2547 | swap(basic_string& __s) _GLIBCXX_NOEXCEPT; | |
2548 | ||
2549 | // String operations: | |
2550 | /** | |
2551 | * @brief Return const pointer to null-terminated contents. | |
2552 | * | |
2553 | * This is a handle to internal data. Do not modify or dire things may | |
2554 | * happen. | |
2555 | */ | |
b911ca42 | 2556 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2557 | const _CharT* |
2558 | c_str() const _GLIBCXX_NOEXCEPT | |
2d60da10 | 2559 | { return _M_data(); } |
34a2b755 JW |
2560 | |
2561 | /** | |
2562 | * @brief Return const pointer to contents. | |
2563 | * | |
92d58dee JW |
2564 | * This is a pointer to internal data. It is undefined to modify |
2565 | * the contents through the returned pointer. To get a pointer that | |
2566 | * allows modifying the contents use @c &str[0] instead, | |
2567 | * (or in C++17 the non-const @c str.data() overload). | |
34a2b755 | 2568 | */ |
b911ca42 | 2569 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2570 | const _CharT* |
2571 | data() const _GLIBCXX_NOEXCEPT | |
2d60da10 | 2572 | { return _M_data(); } |
34a2b755 | 2573 | |
1a289fa3 | 2574 | #if __cplusplus >= 201703L |
92d58dee JW |
2575 | /** |
2576 | * @brief Return non-const pointer to contents. | |
2577 | * | |
2578 | * This is a pointer to the character sequence held by the string. | |
2579 | * Modifying the characters in the sequence is allowed. | |
2580 | */ | |
b911ca42 | 2581 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
92d58dee JW |
2582 | _CharT* |
2583 | data() noexcept | |
2584 | { return _M_data(); } | |
2585 | #endif | |
2586 | ||
34a2b755 JW |
2587 | /** |
2588 | * @brief Return copy of allocator used to construct this string. | |
2589 | */ | |
b911ca42 | 2590 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2591 | allocator_type |
2592 | get_allocator() const _GLIBCXX_NOEXCEPT | |
2593 | { return _M_get_allocator(); } | |
2594 | ||
2595 | /** | |
2596 | * @brief Find position of a C substring. | |
2597 | * @param __s C string to locate. | |
2598 | * @param __pos Index of character to search from. | |
2599 | * @param __n Number of characters from @a s to search for. | |
2600 | * @return Index of start of first occurrence. | |
2601 | * | |
2602 | * Starting from @a __pos, searches forward for the first @a | |
2603 | * __n characters in @a __s within this string. If found, | |
2604 | * returns the index where it begins. If not found, returns | |
2605 | * npos. | |
2606 | */ | |
b911ca42 | 2607 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 2608 | size_type |
39a03251 JW |
2609 | find(const _CharT* __s, size_type __pos, size_type __n) const |
2610 | _GLIBCXX_NOEXCEPT; | |
34a2b755 JW |
2611 | |
2612 | /** | |
2613 | * @brief Find position of a string. | |
2614 | * @param __str String to locate. | |
2615 | * @param __pos Index of character to search from (default 0). | |
2616 | * @return Index of start of first occurrence. | |
2617 | * | |
2618 | * Starting from @a __pos, searches forward for value of @a __str within | |
2619 | * this string. If found, returns the index where it begins. If not | |
2620 | * found, returns npos. | |
2621 | */ | |
b911ca42 | 2622 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2623 | size_type |
2624 | find(const basic_string& __str, size_type __pos = 0) const | |
39a03251 | 2625 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2626 | { return this->find(__str.data(), __pos, __str.size()); } |
2627 | ||
1a289fa3 | 2628 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2629 | /** |
2630 | * @brief Find position of a string_view. | |
df66af3b | 2631 | * @param __svt The object convertible to string_view to locate. |
ca8f2cb1 VV |
2632 | * @param __pos Index of character to search from (default 0). |
2633 | * @return Index of start of first occurrence. | |
2634 | */ | |
df66af3b | 2635 | template<typename _Tp> |
b911ca42 | 2636 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2637 | _If_sv<_Tp, size_type> |
2638 | find(const _Tp& __svt, size_type __pos = 0) const | |
2639 | noexcept(is_same<_Tp, __sv_type>::value) | |
2640 | { | |
2641 | __sv_type __sv = __svt; | |
2642 | return this->find(__sv.data(), __pos, __sv.size()); | |
2643 | } | |
ca8f2cb1 VV |
2644 | #endif // C++17 |
2645 | ||
34a2b755 JW |
2646 | /** |
2647 | * @brief Find position of a C string. | |
2648 | * @param __s C string to locate. | |
2649 | * @param __pos Index of character to search from (default 0). | |
2650 | * @return Index of start of first occurrence. | |
2651 | * | |
2652 | * Starting from @a __pos, searches forward for the value of @a | |
2653 | * __s within this string. If found, returns the index where | |
2654 | * it begins. If not found, returns npos. | |
2655 | */ | |
b911ca42 | 2656 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 2657 | size_type |
39a03251 | 2658 | find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2659 | { |
2660 | __glibcxx_requires_string(__s); | |
2661 | return this->find(__s, __pos, traits_type::length(__s)); | |
2662 | } | |
2663 | ||
2664 | /** | |
2665 | * @brief Find position of a character. | |
2666 | * @param __c Character to locate. | |
2667 | * @param __pos Index of character to search from (default 0). | |
2668 | * @return Index of first occurrence. | |
2669 | * | |
2670 | * Starting from @a __pos, searches forward for @a __c within | |
2671 | * this string. If found, returns the index where it was | |
2672 | * found. If not found, returns npos. | |
2673 | */ | |
b911ca42 | 2674 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2675 | size_type |
2676 | find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; | |
2677 | ||
2678 | /** | |
2679 | * @brief Find last position of a string. | |
2680 | * @param __str String to locate. | |
2681 | * @param __pos Index of character to search back from (default end). | |
2682 | * @return Index of start of last occurrence. | |
2683 | * | |
2684 | * Starting from @a __pos, searches backward for value of @a | |
2685 | * __str within this string. If found, returns the index where | |
2686 | * it begins. If not found, returns npos. | |
2687 | */ | |
b911ca42 | 2688 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2689 | size_type |
2690 | rfind(const basic_string& __str, size_type __pos = npos) const | |
39a03251 | 2691 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2692 | { return this->rfind(__str.data(), __pos, __str.size()); } |
2693 | ||
1a289fa3 | 2694 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2695 | /** |
2696 | * @brief Find last position of a string_view. | |
df66af3b | 2697 | * @param __svt The object convertible to string_view to locate. |
ca8f2cb1 VV |
2698 | * @param __pos Index of character to search back from (default end). |
2699 | * @return Index of start of last occurrence. | |
2700 | */ | |
df66af3b | 2701 | template<typename _Tp> |
b911ca42 | 2702 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2703 | _If_sv<_Tp, size_type> |
2704 | rfind(const _Tp& __svt, size_type __pos = npos) const | |
2705 | noexcept(is_same<_Tp, __sv_type>::value) | |
2706 | { | |
2707 | __sv_type __sv = __svt; | |
2708 | return this->rfind(__sv.data(), __pos, __sv.size()); | |
2709 | } | |
ca8f2cb1 VV |
2710 | #endif // C++17 |
2711 | ||
34a2b755 JW |
2712 | /** |
2713 | * @brief Find last position of a C substring. | |
2714 | * @param __s C string to locate. | |
2715 | * @param __pos Index of character to search back from. | |
2716 | * @param __n Number of characters from s to search for. | |
2717 | * @return Index of start of last occurrence. | |
2718 | * | |
2719 | * Starting from @a __pos, searches backward for the first @a | |
2720 | * __n characters in @a __s within this string. If found, | |
2721 | * returns the index where it begins. If not found, returns | |
2722 | * npos. | |
2723 | */ | |
b911ca42 | 2724 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 2725 | size_type |
39a03251 JW |
2726 | rfind(const _CharT* __s, size_type __pos, size_type __n) const |
2727 | _GLIBCXX_NOEXCEPT; | |
34a2b755 JW |
2728 | |
2729 | /** | |
2730 | * @brief Find last position of a C string. | |
2731 | * @param __s C string to locate. | |
2732 | * @param __pos Index of character to start search at (default end). | |
2733 | * @return Index of start of last occurrence. | |
2734 | * | |
2735 | * Starting from @a __pos, searches backward for the value of | |
2736 | * @a __s within this string. If found, returns the index | |
2737 | * where it begins. If not found, returns npos. | |
2738 | */ | |
b911ca42 | 2739 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2740 | size_type |
2741 | rfind(const _CharT* __s, size_type __pos = npos) const | |
2742 | { | |
2743 | __glibcxx_requires_string(__s); | |
2744 | return this->rfind(__s, __pos, traits_type::length(__s)); | |
2745 | } | |
2746 | ||
2747 | /** | |
2748 | * @brief Find last position of a character. | |
2749 | * @param __c Character to locate. | |
2750 | * @param __pos Index of character to search back from (default end). | |
2751 | * @return Index of last occurrence. | |
2752 | * | |
2753 | * Starting from @a __pos, searches backward for @a __c within | |
2754 | * this string. If found, returns the index where it was | |
2755 | * found. If not found, returns npos. | |
2756 | */ | |
b911ca42 | 2757 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2758 | size_type |
2759 | rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; | |
2760 | ||
2761 | /** | |
2762 | * @brief Find position of a character of string. | |
2763 | * @param __str String containing characters to locate. | |
2764 | * @param __pos Index of character to search from (default 0). | |
2765 | * @return Index of first occurrence. | |
2766 | * | |
2767 | * Starting from @a __pos, searches forward for one of the | |
2768 | * characters of @a __str within this string. If found, | |
2769 | * returns the index where it was found. If not found, returns | |
2770 | * npos. | |
2771 | */ | |
b911ca42 | 2772 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2773 | size_type |
2774 | find_first_of(const basic_string& __str, size_type __pos = 0) const | |
39a03251 | 2775 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2776 | { return this->find_first_of(__str.data(), __pos, __str.size()); } |
2777 | ||
1a289fa3 | 2778 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2779 | /** |
2780 | * @brief Find position of a character of a string_view. | |
df66af3b DK |
2781 | * @param __svt An object convertible to string_view containing |
2782 | * characters to locate. | |
ca8f2cb1 VV |
2783 | * @param __pos Index of character to search from (default 0). |
2784 | * @return Index of first occurrence. | |
2785 | */ | |
df66af3b | 2786 | template<typename _Tp> |
b911ca42 | 2787 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2788 | _If_sv<_Tp, size_type> |
2789 | find_first_of(const _Tp& __svt, size_type __pos = 0) const | |
2790 | noexcept(is_same<_Tp, __sv_type>::value) | |
2791 | { | |
2792 | __sv_type __sv = __svt; | |
2793 | return this->find_first_of(__sv.data(), __pos, __sv.size()); | |
2794 | } | |
ca8f2cb1 VV |
2795 | #endif // C++17 |
2796 | ||
34a2b755 JW |
2797 | /** |
2798 | * @brief Find position of a character of C substring. | |
2799 | * @param __s String containing characters to locate. | |
2800 | * @param __pos Index of character to search from. | |
2801 | * @param __n Number of characters from s to search for. | |
2802 | * @return Index of first occurrence. | |
2803 | * | |
2804 | * Starting from @a __pos, searches forward for one of the | |
2805 | * first @a __n characters of @a __s within this string. If | |
2806 | * found, returns the index where it was found. If not found, | |
2807 | * returns npos. | |
2808 | */ | |
b911ca42 | 2809 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 2810 | size_type |
39a03251 JW |
2811 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
2812 | _GLIBCXX_NOEXCEPT; | |
34a2b755 JW |
2813 | |
2814 | /** | |
2815 | * @brief Find position of a character of C string. | |
2816 | * @param __s String containing characters to locate. | |
2817 | * @param __pos Index of character to search from (default 0). | |
2818 | * @return Index of first occurrence. | |
2819 | * | |
2820 | * Starting from @a __pos, searches forward for one of the | |
2821 | * characters of @a __s within this string. If found, returns | |
2822 | * the index where it was found. If not found, returns npos. | |
2823 | */ | |
b911ca42 | 2824 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2825 | size_type |
2826 | find_first_of(const _CharT* __s, size_type __pos = 0) const | |
39a03251 | 2827 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2828 | { |
2829 | __glibcxx_requires_string(__s); | |
2830 | return this->find_first_of(__s, __pos, traits_type::length(__s)); | |
2831 | } | |
2832 | ||
2833 | /** | |
2834 | * @brief Find position of a character. | |
2835 | * @param __c Character to locate. | |
2836 | * @param __pos Index of character to search from (default 0). | |
2837 | * @return Index of first occurrence. | |
2838 | * | |
2839 | * Starting from @a __pos, searches forward for the character | |
2840 | * @a __c within this string. If found, returns the index | |
2841 | * where it was found. If not found, returns npos. | |
2842 | * | |
2843 | * Note: equivalent to find(__c, __pos). | |
2844 | */ | |
b911ca42 | 2845 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2846 | size_type |
2847 | find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT | |
2848 | { return this->find(__c, __pos); } | |
2849 | ||
2850 | /** | |
2851 | * @brief Find last position of a character of string. | |
2852 | * @param __str String containing characters to locate. | |
2853 | * @param __pos Index of character to search back from (default end). | |
2854 | * @return Index of last occurrence. | |
2855 | * | |
2856 | * Starting from @a __pos, searches backward for one of the | |
2857 | * characters of @a __str within this string. If found, | |
2858 | * returns the index where it was found. If not found, returns | |
2859 | * npos. | |
2860 | */ | |
b911ca42 | 2861 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2862 | size_type |
2863 | find_last_of(const basic_string& __str, size_type __pos = npos) const | |
39a03251 | 2864 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2865 | { return this->find_last_of(__str.data(), __pos, __str.size()); } |
2866 | ||
1a289fa3 | 2867 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2868 | /** |
2869 | * @brief Find last position of a character of string. | |
df66af3b DK |
2870 | * @param __svt An object convertible to string_view containing |
2871 | * characters to locate. | |
ca8f2cb1 VV |
2872 | * @param __pos Index of character to search back from (default end). |
2873 | * @return Index of last occurrence. | |
2874 | */ | |
df66af3b | 2875 | template<typename _Tp> |
b911ca42 | 2876 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2877 | _If_sv<_Tp, size_type> |
2878 | find_last_of(const _Tp& __svt, size_type __pos = npos) const | |
2879 | noexcept(is_same<_Tp, __sv_type>::value) | |
2880 | { | |
2881 | __sv_type __sv = __svt; | |
2882 | return this->find_last_of(__sv.data(), __pos, __sv.size()); | |
2883 | } | |
ca8f2cb1 VV |
2884 | #endif // C++17 |
2885 | ||
34a2b755 JW |
2886 | /** |
2887 | * @brief Find last position of a character of C substring. | |
2888 | * @param __s C string containing characters to locate. | |
2889 | * @param __pos Index of character to search back from. | |
2890 | * @param __n Number of characters from s to search for. | |
2891 | * @return Index of last occurrence. | |
2892 | * | |
2893 | * Starting from @a __pos, searches backward for one of the | |
2894 | * first @a __n characters of @a __s within this string. If | |
2895 | * found, returns the index where it was found. If not found, | |
2896 | * returns npos. | |
2897 | */ | |
b911ca42 | 2898 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 2899 | size_type |
39a03251 JW |
2900 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
2901 | _GLIBCXX_NOEXCEPT; | |
34a2b755 JW |
2902 | |
2903 | /** | |
2904 | * @brief Find last position of a character of C string. | |
2905 | * @param __s C string containing characters to locate. | |
2906 | * @param __pos Index of character to search back from (default end). | |
2907 | * @return Index of last occurrence. | |
2908 | * | |
2909 | * Starting from @a __pos, searches backward for one of the | |
2910 | * characters of @a __s within this string. If found, returns | |
2911 | * the index where it was found. If not found, returns npos. | |
2912 | */ | |
b911ca42 | 2913 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2914 | size_type |
2915 | find_last_of(const _CharT* __s, size_type __pos = npos) const | |
39a03251 | 2916 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2917 | { |
2918 | __glibcxx_requires_string(__s); | |
2919 | return this->find_last_of(__s, __pos, traits_type::length(__s)); | |
2920 | } | |
2921 | ||
2922 | /** | |
2923 | * @brief Find last position of a character. | |
2924 | * @param __c Character to locate. | |
2925 | * @param __pos Index of character to search back from (default end). | |
2926 | * @return Index of last occurrence. | |
2927 | * | |
2928 | * Starting from @a __pos, searches backward for @a __c within | |
2929 | * this string. If found, returns the index where it was | |
2930 | * found. If not found, returns npos. | |
2931 | * | |
2932 | * Note: equivalent to rfind(__c, __pos). | |
2933 | */ | |
b911ca42 | 2934 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2935 | size_type |
2936 | find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT | |
2937 | { return this->rfind(__c, __pos); } | |
2938 | ||
2939 | /** | |
2940 | * @brief Find position of a character not in string. | |
2941 | * @param __str String containing characters to avoid. | |
2942 | * @param __pos Index of character to search from (default 0). | |
2943 | * @return Index of first occurrence. | |
2944 | * | |
2945 | * Starting from @a __pos, searches forward for a character not contained | |
2946 | * in @a __str within this string. If found, returns the index where it | |
2947 | * was found. If not found, returns npos. | |
2948 | */ | |
b911ca42 | 2949 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2950 | size_type |
2951 | find_first_not_of(const basic_string& __str, size_type __pos = 0) const | |
39a03251 | 2952 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
2953 | { return this->find_first_not_of(__str.data(), __pos, __str.size()); } |
2954 | ||
1a289fa3 | 2955 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
2956 | /** |
2957 | * @brief Find position of a character not in a string_view. | |
df66af3b DK |
2958 | * @param __svt A object convertible to string_view containing |
2959 | * characters to avoid. | |
ca8f2cb1 VV |
2960 | * @param __pos Index of character to search from (default 0). |
2961 | * @return Index of first occurrence. | |
2962 | */ | |
df66af3b | 2963 | template<typename _Tp> |
b911ca42 | 2964 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
2965 | _If_sv<_Tp, size_type> |
2966 | find_first_not_of(const _Tp& __svt, size_type __pos = 0) const | |
2967 | noexcept(is_same<_Tp, __sv_type>::value) | |
2968 | { | |
2969 | __sv_type __sv = __svt; | |
2970 | return this->find_first_not_of(__sv.data(), __pos, __sv.size()); | |
2971 | } | |
ca8f2cb1 VV |
2972 | #endif // C++17 |
2973 | ||
34a2b755 JW |
2974 | /** |
2975 | * @brief Find position of a character not in C substring. | |
2976 | * @param __s C string containing characters to avoid. | |
2977 | * @param __pos Index of character to search from. | |
2978 | * @param __n Number of characters from __s to consider. | |
2979 | * @return Index of first occurrence. | |
2980 | * | |
2981 | * Starting from @a __pos, searches forward for a character not | |
2982 | * contained in the first @a __n characters of @a __s within | |
2983 | * this string. If found, returns the index where it was | |
2984 | * found. If not found, returns npos. | |
2985 | */ | |
b911ca42 | 2986 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
2987 | size_type |
2988 | find_first_not_of(const _CharT* __s, size_type __pos, | |
39a03251 | 2989 | size_type __n) const _GLIBCXX_NOEXCEPT; |
34a2b755 JW |
2990 | |
2991 | /** | |
2992 | * @brief Find position of a character not in C string. | |
2993 | * @param __s C string containing characters to avoid. | |
2994 | * @param __pos Index of character to search from (default 0). | |
2995 | * @return Index of first occurrence. | |
2996 | * | |
2997 | * Starting from @a __pos, searches forward for a character not | |
2998 | * contained in @a __s within this string. If found, returns | |
2999 | * the index where it was found. If not found, returns npos. | |
3000 | */ | |
b911ca42 | 3001 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3002 | size_type |
3003 | find_first_not_of(const _CharT* __s, size_type __pos = 0) const | |
39a03251 | 3004 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
3005 | { |
3006 | __glibcxx_requires_string(__s); | |
3007 | return this->find_first_not_of(__s, __pos, traits_type::length(__s)); | |
3008 | } | |
3009 | ||
3010 | /** | |
3011 | * @brief Find position of a different character. | |
3012 | * @param __c Character to avoid. | |
3013 | * @param __pos Index of character to search from (default 0). | |
3014 | * @return Index of first occurrence. | |
3015 | * | |
3016 | * Starting from @a __pos, searches forward for a character | |
3017 | * other than @a __c within this string. If found, returns the | |
3018 | * index where it was found. If not found, returns npos. | |
3019 | */ | |
b911ca42 | 3020 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3021 | size_type |
3022 | find_first_not_of(_CharT __c, size_type __pos = 0) const | |
39a03251 | 3023 | _GLIBCXX_NOEXCEPT; |
34a2b755 JW |
3024 | |
3025 | /** | |
3026 | * @brief Find last position of a character not in string. | |
3027 | * @param __str String containing characters to avoid. | |
3028 | * @param __pos Index of character to search back from (default end). | |
3029 | * @return Index of last occurrence. | |
3030 | * | |
3031 | * Starting from @a __pos, searches backward for a character | |
3032 | * not contained in @a __str within this string. If found, | |
3033 | * returns the index where it was found. If not found, returns | |
3034 | * npos. | |
3035 | */ | |
b911ca42 | 3036 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3037 | size_type |
3038 | find_last_not_of(const basic_string& __str, size_type __pos = npos) const | |
39a03251 | 3039 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
3040 | { return this->find_last_not_of(__str.data(), __pos, __str.size()); } |
3041 | ||
1a289fa3 | 3042 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
3043 | /** |
3044 | * @brief Find last position of a character not in a string_view. | |
df66af3b DK |
3045 | * @param __svt An object convertible to string_view containing |
3046 | * characters to avoid. | |
ca8f2cb1 VV |
3047 | * @param __pos Index of character to search back from (default end). |
3048 | * @return Index of last occurrence. | |
3049 | */ | |
df66af3b | 3050 | template<typename _Tp> |
b911ca42 | 3051 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
3052 | _If_sv<_Tp, size_type> |
3053 | find_last_not_of(const _Tp& __svt, size_type __pos = npos) const | |
3054 | noexcept(is_same<_Tp, __sv_type>::value) | |
3055 | { | |
3056 | __sv_type __sv = __svt; | |
3057 | return this->find_last_not_of(__sv.data(), __pos, __sv.size()); | |
3058 | } | |
ca8f2cb1 VV |
3059 | #endif // C++17 |
3060 | ||
34a2b755 JW |
3061 | /** |
3062 | * @brief Find last position of a character not in C substring. | |
3063 | * @param __s C string containing characters to avoid. | |
3064 | * @param __pos Index of character to search back from. | |
3065 | * @param __n Number of characters from s to consider. | |
3066 | * @return Index of last occurrence. | |
3067 | * | |
3068 | * Starting from @a __pos, searches backward for a character not | |
3069 | * contained in the first @a __n characters of @a __s within this string. | |
3070 | * If found, returns the index where it was found. If not found, | |
3071 | * returns npos. | |
3072 | */ | |
b911ca42 | 3073 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3074 | size_type |
3075 | find_last_not_of(const _CharT* __s, size_type __pos, | |
39a03251 | 3076 | size_type __n) const _GLIBCXX_NOEXCEPT; |
34a2b755 JW |
3077 | /** |
3078 | * @brief Find last position of a character not in C string. | |
3079 | * @param __s C string containing characters to avoid. | |
3080 | * @param __pos Index of character to search back from (default end). | |
3081 | * @return Index of last occurrence. | |
3082 | * | |
3083 | * Starting from @a __pos, searches backward for a character | |
3084 | * not contained in @a __s within this string. If found, | |
3085 | * returns the index where it was found. If not found, returns | |
3086 | * npos. | |
3087 | */ | |
b911ca42 | 3088 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3089 | size_type |
3090 | find_last_not_of(const _CharT* __s, size_type __pos = npos) const | |
39a03251 | 3091 | _GLIBCXX_NOEXCEPT |
34a2b755 JW |
3092 | { |
3093 | __glibcxx_requires_string(__s); | |
3094 | return this->find_last_not_of(__s, __pos, traits_type::length(__s)); | |
3095 | } | |
3096 | ||
3097 | /** | |
3098 | * @brief Find last position of a different character. | |
3099 | * @param __c Character to avoid. | |
3100 | * @param __pos Index of character to search back from (default end). | |
3101 | * @return Index of last occurrence. | |
3102 | * | |
3103 | * Starting from @a __pos, searches backward for a character other than | |
3104 | * @a __c within this string. If found, returns the index where it was | |
3105 | * found. If not found, returns npos. | |
3106 | */ | |
b911ca42 | 3107 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3108 | size_type |
3109 | find_last_not_of(_CharT __c, size_type __pos = npos) const | |
39a03251 | 3110 | _GLIBCXX_NOEXCEPT; |
34a2b755 JW |
3111 | |
3112 | /** | |
3113 | * @brief Get a substring. | |
3114 | * @param __pos Index of first character (default 0). | |
3115 | * @param __n Number of characters in substring (default remainder). | |
3116 | * @return The new string. | |
3117 | * @throw std::out_of_range If __pos > size(). | |
3118 | * | |
3119 | * Construct and return a new string using the @a __n | |
3120 | * characters starting at @a __pos. If the string is too | |
3121 | * short, use the remainder of the characters. If @a __pos is | |
3122 | * beyond the end of the string, out_of_range is thrown. | |
3123 | */ | |
b911ca42 | 3124 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3125 | basic_string |
3126 | substr(size_type __pos = 0, size_type __n = npos) const | |
3127 | { return basic_string(*this, | |
3128 | _M_check(__pos, "basic_string::substr"), __n); } | |
3129 | ||
3130 | /** | |
3131 | * @brief Compare to a string. | |
3132 | * @param __str String to compare against. | |
3133 | * @return Integer < 0, 0, or > 0. | |
3134 | * | |
3135 | * Returns an integer < 0 if this string is ordered before @a | |
3136 | * __str, 0 if their values are equivalent, or > 0 if this | |
3137 | * string is ordered after @a __str. Determines the effective | |
3138 | * length rlen of the strings to compare as the smallest of | |
3139 | * size() and str.size(). The function then compares the two | |
3140 | * strings by calling traits::compare(data(), str.data(),rlen). | |
3141 | * If the result of the comparison is nonzero returns it, | |
3142 | * otherwise the shorter one is ordered first. | |
3143 | */ | |
b911ca42 | 3144 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3145 | int |
3146 | compare(const basic_string& __str) const | |
3147 | { | |
3148 | const size_type __size = this->size(); | |
3149 | const size_type __osize = __str.size(); | |
3150 | const size_type __len = std::min(__size, __osize); | |
3151 | ||
2d60da10 | 3152 | int __r = traits_type::compare(_M_data(), __str.data(), __len); |
34a2b755 JW |
3153 | if (!__r) |
3154 | __r = _S_compare(__size, __osize); | |
3155 | return __r; | |
3156 | } | |
3157 | ||
1a289fa3 | 3158 | #if __cplusplus >= 201703L |
ca8f2cb1 VV |
3159 | /** |
3160 | * @brief Compare to a string_view. | |
df66af3b | 3161 | * @param __svt An object convertible to string_view to compare against. |
ca8f2cb1 VV |
3162 | * @return Integer < 0, 0, or > 0. |
3163 | */ | |
df66af3b | 3164 | template<typename _Tp> |
b911ca42 | 3165 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
3166 | _If_sv<_Tp, int> |
3167 | compare(const _Tp& __svt) const | |
3168 | noexcept(is_same<_Tp, __sv_type>::value) | |
3169 | { | |
3170 | __sv_type __sv = __svt; | |
3171 | const size_type __size = this->size(); | |
3172 | const size_type __osize = __sv.size(); | |
3173 | const size_type __len = std::min(__size, __osize); | |
3174 | ||
3175 | int __r = traits_type::compare(_M_data(), __sv.data(), __len); | |
3176 | if (!__r) | |
3177 | __r = _S_compare(__size, __osize); | |
3178 | return __r; | |
3179 | } | |
ca8f2cb1 VV |
3180 | |
3181 | /** | |
3182 | * @brief Compare to a string_view. | |
3183 | * @param __pos A position in the string to start comparing from. | |
3184 | * @param __n The number of characters to compare. | |
df66af3b DK |
3185 | * @param __svt An object convertible to string_view to compare |
3186 | * against. | |
ca8f2cb1 VV |
3187 | * @return Integer < 0, 0, or > 0. |
3188 | */ | |
df66af3b | 3189 | template<typename _Tp> |
b911ca42 | 3190 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
df66af3b DK |
3191 | _If_sv<_Tp, int> |
3192 | compare(size_type __pos, size_type __n, const _Tp& __svt) const | |
3193 | noexcept(is_same<_Tp, __sv_type>::value) | |
3194 | { | |
3195 | __sv_type __sv = __svt; | |
3196 | return __sv_type(*this).substr(__pos, __n).compare(__sv); | |
3197 | } | |
ca8f2cb1 VV |
3198 | |
3199 | /** | |
3200 | * @brief Compare to a string_view. | |
3201 | * @param __pos1 A position in the string to start comparing from. | |
3202 | * @param __n1 The number of characters to compare. | |
df66af3b DK |
3203 | * @param __svt An object convertible to string_view to compare |
3204 | * against. | |
ca8f2cb1 VV |
3205 | * @param __pos2 A position in the string_view to start comparing from. |
3206 | * @param __n2 The number of characters to compare. | |
3207 | * @return Integer < 0, 0, or > 0. | |
3208 | */ | |
df66af3b | 3209 | template<typename _Tp> |
b911ca42 | 3210 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
657213f7 JW |
3211 | _If_sv<_Tp, int> |
3212 | compare(size_type __pos1, size_type __n1, const _Tp& __svt, | |
3213 | size_type __pos2, size_type __n2 = npos) const | |
df66af3b | 3214 | noexcept(is_same<_Tp, __sv_type>::value) |
657213f7 JW |
3215 | { |
3216 | __sv_type __sv = __svt; | |
3217 | return __sv_type(*this) | |
3218 | .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); | |
3219 | } | |
ca8f2cb1 VV |
3220 | #endif // C++17 |
3221 | ||
34a2b755 JW |
3222 | /** |
3223 | * @brief Compare substring to a string. | |
3224 | * @param __pos Index of first character of substring. | |
3225 | * @param __n Number of characters in substring. | |
3226 | * @param __str String to compare against. | |
3227 | * @return Integer < 0, 0, or > 0. | |
3228 | * | |
3229 | * Form the substring of this string from the @a __n characters | |
3230 | * starting at @a __pos. Returns an integer < 0 if the | |
3231 | * substring is ordered before @a __str, 0 if their values are | |
3232 | * equivalent, or > 0 if the substring is ordered after @a | |
3233 | * __str. Determines the effective length rlen of the strings | |
3234 | * to compare as the smallest of the length of the substring | |
3235 | * and @a __str.size(). The function then compares the two | |
3236 | * strings by calling | |
3237 | * traits::compare(substring.data(),str.data(),rlen). If the | |
3238 | * result of the comparison is nonzero returns it, otherwise | |
3239 | * the shorter one is ordered first. | |
3240 | */ | |
b911ca42 | 3241 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 3242 | int |
1b65779f JW |
3243 | compare(size_type __pos, size_type __n, const basic_string& __str) const |
3244 | { | |
3245 | _M_check(__pos, "basic_string::compare"); | |
3246 | __n = _M_limit(__pos, __n); | |
3247 | const size_type __osize = __str.size(); | |
3248 | const size_type __len = std::min(__n, __osize); | |
3249 | int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); | |
3250 | if (!__r) | |
3251 | __r = _S_compare(__n, __osize); | |
3252 | return __r; | |
3253 | } | |
34a2b755 JW |
3254 | |
3255 | /** | |
3256 | * @brief Compare substring to a substring. | |
3257 | * @param __pos1 Index of first character of substring. | |
3258 | * @param __n1 Number of characters in substring. | |
3259 | * @param __str String to compare against. | |
3260 | * @param __pos2 Index of first character of substring of str. | |
3261 | * @param __n2 Number of characters in substring of str. | |
3262 | * @return Integer < 0, 0, or > 0. | |
3263 | * | |
3264 | * Form the substring of this string from the @a __n1 | |
3265 | * characters starting at @a __pos1. Form the substring of @a | |
3266 | * __str from the @a __n2 characters starting at @a __pos2. | |
3267 | * Returns an integer < 0 if this substring is ordered before | |
3268 | * the substring of @a __str, 0 if their values are equivalent, | |
3269 | * or > 0 if this substring is ordered after the substring of | |
3270 | * @a __str. Determines the effective length rlen of the | |
3271 | * strings to compare as the smallest of the lengths of the | |
3272 | * substrings. The function then compares the two strings by | |
3273 | * calling | |
3274 | * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). | |
3275 | * If the result of the comparison is nonzero returns it, | |
3276 | * otherwise the shorter one is ordered first. | |
3277 | */ | |
b911ca42 | 3278 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3279 | int |
3280 | compare(size_type __pos1, size_type __n1, const basic_string& __str, | |
1b65779f JW |
3281 | size_type __pos2, size_type __n2 = npos) const |
3282 | { | |
3283 | _M_check(__pos1, "basic_string::compare"); | |
3284 | __str._M_check(__pos2, "basic_string::compare"); | |
3285 | __n1 = _M_limit(__pos1, __n1); | |
3286 | __n2 = __str._M_limit(__pos2, __n2); | |
3287 | const size_type __len = std::min(__n1, __n2); | |
3288 | int __r = traits_type::compare(_M_data() + __pos1, | |
3289 | __str.data() + __pos2, __len); | |
3290 | if (!__r) | |
3291 | __r = _S_compare(__n1, __n2); | |
3292 | return __r; | |
3293 | } | |
34a2b755 JW |
3294 | |
3295 | /** | |
3296 | * @brief Compare to a C string. | |
3297 | * @param __s C string to compare against. | |
3298 | * @return Integer < 0, 0, or > 0. | |
3299 | * | |
3300 | * Returns an integer < 0 if this string is ordered before @a __s, 0 if | |
3301 | * their values are equivalent, or > 0 if this string is ordered after | |
3302 | * @a __s. Determines the effective length rlen of the strings to | |
3303 | * compare as the smallest of size() and the length of a string | |
3304 | * constructed from @a __s. The function then compares the two strings | |
3305 | * by calling traits::compare(data(),s,rlen). If the result of the | |
3306 | * comparison is nonzero returns it, otherwise the shorter one is | |
3307 | * ordered first. | |
3308 | */ | |
b911ca42 | 3309 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 3310 | int |
1b65779f JW |
3311 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT |
3312 | { | |
3313 | __glibcxx_requires_string(__s); | |
3314 | const size_type __size = this->size(); | |
3315 | const size_type __osize = traits_type::length(__s); | |
3316 | const size_type __len = std::min(__size, __osize); | |
3317 | int __r = traits_type::compare(_M_data(), __s, __len); | |
3318 | if (!__r) | |
3319 | __r = _S_compare(__size, __osize); | |
3320 | return __r; | |
3321 | } | |
34a2b755 JW |
3322 | |
3323 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
3324 | // 5 String::compare specification questionable | |
3325 | /** | |
3326 | * @brief Compare substring to a C string. | |
3327 | * @param __pos Index of first character of substring. | |
3328 | * @param __n1 Number of characters in substring. | |
3329 | * @param __s C string to compare against. | |
3330 | * @return Integer < 0, 0, or > 0. | |
3331 | * | |
3332 | * Form the substring of this string from the @a __n1 | |
3333 | * characters starting at @a pos. Returns an integer < 0 if | |
3334 | * the substring is ordered before @a __s, 0 if their values | |
3335 | * are equivalent, or > 0 if the substring is ordered after @a | |
3336 | * __s. Determines the effective length rlen of the strings to | |
3337 | * compare as the smallest of the length of the substring and | |
3338 | * the length of a string constructed from @a __s. The | |
3339 | * function then compares the two string by calling | |
3340 | * traits::compare(substring.data(),__s,rlen). If the result of | |
3341 | * the comparison is nonzero returns it, otherwise the shorter | |
3342 | * one is ordered first. | |
3343 | */ | |
b911ca42 | 3344 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 | 3345 | int |
1b65779f JW |
3346 | compare(size_type __pos, size_type __n1, const _CharT* __s) const |
3347 | { | |
3348 | __glibcxx_requires_string(__s); | |
3349 | _M_check(__pos, "basic_string::compare"); | |
3350 | __n1 = _M_limit(__pos, __n1); | |
3351 | const size_type __osize = traits_type::length(__s); | |
3352 | const size_type __len = std::min(__n1, __osize); | |
3353 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); | |
3354 | if (!__r) | |
3355 | __r = _S_compare(__n1, __osize); | |
3356 | return __r; | |
3357 | } | |
34a2b755 JW |
3358 | |
3359 | /** | |
3360 | * @brief Compare substring against a character %array. | |
3361 | * @param __pos Index of first character of substring. | |
3362 | * @param __n1 Number of characters in substring. | |
3363 | * @param __s character %array to compare against. | |
3364 | * @param __n2 Number of characters of s. | |
3365 | * @return Integer < 0, 0, or > 0. | |
3366 | * | |
3367 | * Form the substring of this string from the @a __n1 | |
3368 | * characters starting at @a __pos. Form a string from the | |
3369 | * first @a __n2 characters of @a __s. Returns an integer < 0 | |
3370 | * if this substring is ordered before the string from @a __s, | |
3371 | * 0 if their values are equivalent, or > 0 if this substring | |
3372 | * is ordered after the string from @a __s. Determines the | |
3373 | * effective length rlen of the strings to compare as the | |
3374 | * smallest of the length of the substring and @a __n2. The | |
3375 | * function then compares the two strings by calling | |
3376 | * traits::compare(substring.data(),s,rlen). If the result of | |
3377 | * the comparison is nonzero returns it, otherwise the shorter | |
3378 | * one is ordered first. | |
3379 | * | |
3380 | * NB: s must have at least n2 characters, '\\0' has | |
3381 | * no special meaning. | |
3382 | */ | |
b911ca42 | 3383 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
34a2b755 JW |
3384 | int |
3385 | compare(size_type __pos, size_type __n1, const _CharT* __s, | |
1b65779f JW |
3386 | size_type __n2) const |
3387 | { | |
3388 | __glibcxx_requires_string_len(__s, __n2); | |
3389 | _M_check(__pos, "basic_string::compare"); | |
3390 | __n1 = _M_limit(__pos, __n1); | |
3391 | const size_type __len = std::min(__n1, __n2); | |
3392 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); | |
3393 | if (!__r) | |
3394 | __r = _S_compare(__n1, __n2); | |
3395 | return __r; | |
3396 | } | |
93ef155b | 3397 | |
b96e2ff9 | 3398 | #if __cplusplus >= 202002L |
b911ca42 | 3399 | [[nodiscard]] |
b96e2ff9 | 3400 | constexpr bool |
5bd624fb ESR |
3401 | starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3402 | { return __sv_type(this->data(), this->size()).starts_with(__x); } | |
3403 | ||
b911ca42 | 3404 | [[nodiscard]] |
b96e2ff9 | 3405 | constexpr bool |
5bd624fb ESR |
3406 | starts_with(_CharT __x) const noexcept |
3407 | { return __sv_type(this->data(), this->size()).starts_with(__x); } | |
3408 | ||
b911ca42 | 3409 | [[nodiscard, __gnu__::__nonnull__]] |
b96e2ff9 | 3410 | constexpr bool |
5bd624fb ESR |
3411 | starts_with(const _CharT* __x) const noexcept |
3412 | { return __sv_type(this->data(), this->size()).starts_with(__x); } | |
3413 | ||
b911ca42 | 3414 | [[nodiscard]] |
b96e2ff9 | 3415 | constexpr bool |
5bd624fb ESR |
3416 | ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3417 | { return __sv_type(this->data(), this->size()).ends_with(__x); } | |
3418 | ||
b911ca42 | 3419 | [[nodiscard]] |
b96e2ff9 | 3420 | constexpr bool |
5bd624fb ESR |
3421 | ends_with(_CharT __x) const noexcept |
3422 | { return __sv_type(this->data(), this->size()).ends_with(__x); } | |
3423 | ||
b911ca42 | 3424 | [[nodiscard, __gnu__::__nonnull__]] |
b96e2ff9 | 3425 | constexpr bool |
5bd624fb ESR |
3426 | ends_with(const _CharT* __x) const noexcept |
3427 | { return __sv_type(this->data(), this->size()).ends_with(__x); } | |
3428 | #endif // C++20 | |
3429 | ||
f004d6d9 | 3430 | #if __cplusplus > 202002L |
b911ca42 | 3431 | [[nodiscard]] |
b96e2ff9 | 3432 | constexpr bool |
f004d6d9 PF |
3433 | contains(basic_string_view<_CharT, _Traits> __x) const noexcept |
3434 | { return __sv_type(this->data(), this->size()).contains(__x); } | |
3435 | ||
b911ca42 | 3436 | [[nodiscard]] |
b96e2ff9 | 3437 | constexpr bool |
f004d6d9 PF |
3438 | contains(_CharT __x) const noexcept |
3439 | { return __sv_type(this->data(), this->size()).contains(__x); } | |
3440 | ||
b911ca42 | 3441 | [[nodiscard, __gnu__::__nonnull__]] |
b96e2ff9 | 3442 | constexpr bool |
f004d6d9 PF |
3443 | contains(const _CharT* __x) const noexcept |
3444 | { return __sv_type(this->data(), this->size()).contains(__x); } | |
3445 | #endif // C++23 | |
3446 | ||
93ef155b JW |
3447 | // Allow basic_stringbuf::__xfer_bufptrs to call _M_length: |
3448 | template<typename, typename, typename> friend class basic_stringbuf; | |
3449 | }; | |
34a2b755 | 3450 | _GLIBCXX_END_NAMESPACE_CXX11 |
7b527614 JW |
3451 | _GLIBCXX_END_NAMESPACE_VERSION |
3452 | } // namespace std | |
3453 | #endif // _GLIBCXX_USE_CXX11_ABI | |
ec61e852 | 3454 | |
7b527614 JW |
3455 | namespace std _GLIBCXX_VISIBILITY(default) |
3456 | { | |
3457 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
725dc051 | 3458 | |
6d82c562 JW |
3459 | #if __cpp_deduction_guides >= 201606 |
3460 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 | |
3461 | template<typename _InputIterator, typename _CharT | |
3462 | = typename iterator_traits<_InputIterator>::value_type, | |
3463 | typename _Allocator = allocator<_CharT>, | |
3464 | typename = _RequireInputIter<_InputIterator>, | |
3465 | typename = _RequireAllocator<_Allocator>> | |
3466 | basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator()) | |
3467 | -> basic_string<_CharT, char_traits<_CharT>, _Allocator>; | |
53e926c8 JW |
3468 | |
3469 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
3470 | // 3075. basic_string needs deduction guides from basic_string_view | |
3471 | template<typename _CharT, typename _Traits, | |
3472 | typename _Allocator = allocator<_CharT>, | |
3473 | typename = _RequireAllocator<_Allocator>> | |
3474 | basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator()) | |
3475 | -> basic_string<_CharT, _Traits, _Allocator>; | |
3476 | ||
3477 | template<typename _CharT, typename _Traits, | |
3478 | typename _Allocator = allocator<_CharT>, | |
3479 | typename = _RequireAllocator<_Allocator>> | |
3480 | basic_string(basic_string_view<_CharT, _Traits>, | |
3481 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, | |
3482 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, | |
3483 | const _Allocator& = _Allocator()) | |
3484 | -> basic_string<_CharT, _Traits, _Allocator>; | |
6d82c562 JW |
3485 | _GLIBCXX_END_NAMESPACE_CXX11 |
3486 | #endif | |
3487 | ||
c93baa93 WH |
3488 | template<typename _Str> |
3489 | _GLIBCXX20_CONSTEXPR | |
3490 | inline _Str | |
3491 | __str_concat(typename _Str::value_type const* __lhs, | |
3492 | typename _Str::size_type __lhs_len, | |
3493 | typename _Str::value_type const* __rhs, | |
3494 | typename _Str::size_type __rhs_len, | |
3495 | typename _Str::allocator_type const& __a) | |
3496 | { | |
3497 | typedef typename _Str::allocator_type allocator_type; | |
3498 | typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits; | |
3499 | _Str __str(_Alloc_traits::_S_select_on_copy(__a)); | |
3500 | __str.reserve(__lhs_len + __rhs_len); | |
3501 | __str.append(__lhs, __lhs_len); | |
3502 | __str.append(__rhs, __rhs_len); | |
3503 | return __str; | |
3504 | } | |
3505 | ||
725dc051 | 3506 | // operator+ |
284f19bf JQ |
3507 | /** |
3508 | * @brief Concatenate two strings. | |
93c66bc6 BK |
3509 | * @param __lhs First string. |
3510 | * @param __rhs Last string. | |
3511 | * @return New string with value of @a __lhs followed by @a __rhs. | |
ed6814f7 | 3512 | */ |
41b8e86c | 3513 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3514 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
c93baa93 | 3515 | inline basic_string<_CharT, _Traits, _Alloc> |
725dc051 BK |
3516 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3517 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
3518 | { | |
c93baa93 WH |
3519 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3520 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), | |
3521 | __rhs.c_str(), __rhs.size(), | |
3522 | __lhs.get_allocator()); | |
725dc051 BK |
3523 | } |
3524 | ||
284f19bf JQ |
3525 | /** |
3526 | * @brief Concatenate C string and string. | |
93c66bc6 BK |
3527 | * @param __lhs First string. |
3528 | * @param __rhs Last string. | |
3529 | * @return New string with value of @a __lhs followed by @a __rhs. | |
ed6814f7 | 3530 | */ |
725dc051 | 3531 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3532 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
c93baa93 | 3533 | inline basic_string<_CharT,_Traits,_Alloc> |
725dc051 | 3534 | operator+(const _CharT* __lhs, |
c93baa93 WH |
3535 | const basic_string<_CharT,_Traits,_Alloc>& __rhs) |
3536 | { | |
3537 | __glibcxx_requires_string(__lhs); | |
3538 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; | |
3539 | return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs), | |
3540 | __rhs.c_str(), __rhs.size(), | |
3541 | __rhs.get_allocator()); | |
3542 | } | |
725dc051 | 3543 | |
284f19bf JQ |
3544 | /** |
3545 | * @brief Concatenate character and string. | |
93c66bc6 BK |
3546 | * @param __lhs First string. |
3547 | * @param __rhs Last string. | |
3548 | * @return New string with @a __lhs followed by @a __rhs. | |
ed6814f7 | 3549 | */ |
725dc051 | 3550 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3551 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
c93baa93 WH |
3552 | inline basic_string<_CharT,_Traits,_Alloc> |
3553 | operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) | |
3554 | { | |
3555 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; | |
3556 | return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1, | |
3557 | __rhs.c_str(), __rhs.size(), | |
3558 | __rhs.get_allocator()); | |
3559 | } | |
725dc051 | 3560 | |
284f19bf JQ |
3561 | /** |
3562 | * @brief Concatenate string and C string. | |
93c66bc6 BK |
3563 | * @param __lhs First string. |
3564 | * @param __rhs Last string. | |
3565 | * @return New string with @a __lhs followed by @a __rhs. | |
ed6814f7 | 3566 | */ |
725dc051 | 3567 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3568 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
e5428086 | 3569 | inline basic_string<_CharT, _Traits, _Alloc> |
725dc051 | 3570 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
e5428086 JW |
3571 | const _CharT* __rhs) |
3572 | { | |
c93baa93 WH |
3573 | __glibcxx_requires_string(__rhs); |
3574 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; | |
3575 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), | |
3576 | __rhs, _Traits::length(__rhs), | |
3577 | __lhs.get_allocator()); | |
e5428086 | 3578 | } |
284f19bf JQ |
3579 | /** |
3580 | * @brief Concatenate string and character. | |
93c66bc6 BK |
3581 | * @param __lhs First string. |
3582 | * @param __rhs Last string. | |
3583 | * @return New string with @a __lhs followed by @a __rhs. | |
ed6814f7 | 3584 | */ |
725dc051 | 3585 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3586 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
725dc051 BK |
3587 | inline basic_string<_CharT, _Traits, _Alloc> |
3588 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) | |
3589 | { | |
c93baa93 WH |
3590 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3591 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), | |
3592 | __builtin_addressof(__rhs), 1, | |
3593 | __lhs.get_allocator()); | |
725dc051 BK |
3594 | } |
3595 | ||
734f5023 | 3596 | #if __cplusplus >= 201103L |
ce99f498 | 3597 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3598 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3599 | inline basic_string<_CharT, _Traits, _Alloc> |
3600 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, | |
3601 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
3602 | { return std::move(__lhs.append(__rhs)); } | |
3603 | ||
3604 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b96e2ff9 | 3605 | _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3606 | inline basic_string<_CharT, _Traits, _Alloc> |
3607 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3608 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) | |
3609 | { return std::move(__rhs.insert(0, __lhs)); } | |
3610 | ||
3611 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3612 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3613 | inline basic_string<_CharT, _Traits, _Alloc> |
3614 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, | |
3615 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) | |
37a68925 | 3616 | { |
f4e678ef NDR |
3617 | #if _GLIBCXX_USE_CXX11_ABI |
3618 | using _Alloc_traits = allocator_traits<_Alloc>; | |
3619 | bool __use_rhs = false; | |
3620 | if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{}) | |
3621 | __use_rhs = true; | |
3622 | else if (__lhs.get_allocator() == __rhs.get_allocator()) | |
3623 | __use_rhs = true; | |
3624 | if (__use_rhs) | |
3625 | #endif | |
3626 | { | |
3627 | const auto __size = __lhs.size() + __rhs.size(); | |
3628 | if (__size > __lhs.capacity() && __size <= __rhs.capacity()) | |
3629 | return std::move(__rhs.insert(0, __lhs)); | |
3630 | } | |
3631 | return std::move(__lhs.append(__rhs)); | |
37a68925 | 3632 | } |
ce99f498 PC |
3633 | |
3634 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3635 | _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3636 | inline basic_string<_CharT, _Traits, _Alloc> |
3637 | operator+(const _CharT* __lhs, | |
3638 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) | |
3639 | { return std::move(__rhs.insert(0, __lhs)); } | |
3640 | ||
3641 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3642 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 | 3643 | inline basic_string<_CharT, _Traits, _Alloc> |
37a68925 PC |
3644 | operator+(_CharT __lhs, |
3645 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) | |
ce99f498 PC |
3646 | { return std::move(__rhs.insert(0, 1, __lhs)); } |
3647 | ||
3648 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3649 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3650 | inline basic_string<_CharT, _Traits, _Alloc> |
3651 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, | |
3652 | const _CharT* __rhs) | |
3653 | { return std::move(__lhs.append(__rhs)); } | |
3654 | ||
3655 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3656 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
ce99f498 PC |
3657 | inline basic_string<_CharT, _Traits, _Alloc> |
3658 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, | |
3659 | _CharT __rhs) | |
3660 | { return std::move(__lhs.append(1, __rhs)); } | |
3661 | #endif | |
3662 | ||
725dc051 | 3663 | // operator == |
284f19bf JQ |
3664 | /** |
3665 | * @brief Test equivalence of two strings. | |
93c66bc6 BK |
3666 | * @param __lhs First string. |
3667 | * @param __rhs Second string. | |
3668 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. | |
ed6814f7 | 3669 | */ |
725dc051 | 3670 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3671 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
725dc051 BK |
3672 | inline bool |
3673 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3674 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3675 | _GLIBCXX_NOEXCEPT |
6abe3415 JW |
3676 | { |
3677 | return __lhs.size() == __rhs.size() | |
3678 | && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()); | |
3679 | } | |
bd12160a | 3680 | |
284f19bf JQ |
3681 | /** |
3682 | * @brief Test equivalence of string and C string. | |
93c66bc6 BK |
3683 | * @param __lhs String. |
3684 | * @param __rhs C string. | |
3685 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. | |
ed6814f7 | 3686 | */ |
725dc051 | 3687 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3688 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
725dc051 BK |
3689 | inline bool |
3690 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3691 | const _CharT* __rhs) | |
6abe3415 JW |
3692 | { |
3693 | return __lhs.size() == _Traits::length(__rhs) | |
3694 | && !_Traits::compare(__lhs.data(), __rhs, __lhs.size()); | |
3695 | } | |
725dc051 | 3696 | |
875d6cb3 JW |
3697 | #if __cpp_lib_three_way_comparison |
3698 | /** | |
3699 | * @brief Three-way comparison of a string and a C string. | |
3700 | * @param __lhs A string. | |
3701 | * @param __rhs A null-terminated string. | |
3702 | * @return A value indicating whether `__lhs` is less than, equal to, | |
3703 | * greater than, or incomparable with `__rhs`. | |
3704 | */ | |
3705 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3706 | [[nodiscard]] |
73e4d9f1 | 3707 | constexpr auto |
875d6cb3 JW |
3708 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3709 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept | |
3710 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) | |
3711 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } | |
3712 | ||
3713 | /** | |
3714 | * @brief Three-way comparison of a string and a C string. | |
3715 | * @param __lhs A string. | |
3716 | * @param __rhs A null-terminated string. | |
3717 | * @return A value indicating whether `__lhs` is less than, equal to, | |
3718 | * greater than, or incomparable with `__rhs`. | |
3719 | */ | |
3720 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3721 | [[nodiscard]] |
73e4d9f1 | 3722 | constexpr auto |
875d6cb3 JW |
3723 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3724 | const _CharT* __rhs) noexcept | |
3725 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) | |
3726 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } | |
3727 | #else | |
3728 | /** | |
3729 | * @brief Test equivalence of C string and string. | |
3730 | * @param __lhs C string. | |
3731 | * @param __rhs String. | |
3732 | * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. | |
3733 | */ | |
3734 | template<typename _CharT, typename _Traits, typename _Alloc> | |
b911ca42 | 3735 | _GLIBCXX_NODISCARD |
875d6cb3 JW |
3736 | inline bool |
3737 | operator==(const _CharT* __lhs, | |
3738 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
6abe3415 | 3739 | { return __rhs == __lhs; } |
875d6cb3 | 3740 | |
725dc051 | 3741 | // operator != |
284f19bf JQ |
3742 | /** |
3743 | * @brief Test difference of two strings. | |
93c66bc6 BK |
3744 | * @param __lhs First string. |
3745 | * @param __rhs Second string. | |
3746 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. | |
ed6814f7 | 3747 | */ |
725dc051 | 3748 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3749 | _GLIBCXX_NODISCARD |
725dc051 BK |
3750 | inline bool |
3751 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3752 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3753 | _GLIBCXX_NOEXCEPT |
bd12160a | 3754 | { return !(__lhs == __rhs); } |
725dc051 | 3755 | |
284f19bf JQ |
3756 | /** |
3757 | * @brief Test difference of C string and string. | |
93c66bc6 BK |
3758 | * @param __lhs C string. |
3759 | * @param __rhs String. | |
3760 | * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. | |
ed6814f7 | 3761 | */ |
725dc051 | 3762 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3763 | _GLIBCXX_NODISCARD |
725dc051 BK |
3764 | inline bool |
3765 | operator!=(const _CharT* __lhs, | |
3766 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
6abe3415 | 3767 | { return !(__rhs == __lhs); } |
725dc051 | 3768 | |
284f19bf JQ |
3769 | /** |
3770 | * @brief Test difference of string and C string. | |
93c66bc6 BK |
3771 | * @param __lhs String. |
3772 | * @param __rhs C string. | |
3773 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. | |
ed6814f7 | 3774 | */ |
725dc051 | 3775 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3776 | _GLIBCXX_NODISCARD |
725dc051 BK |
3777 | inline bool |
3778 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3779 | const _CharT* __rhs) | |
bd12160a | 3780 | { return !(__lhs == __rhs); } |
725dc051 BK |
3781 | |
3782 | // operator < | |
284f19bf JQ |
3783 | /** |
3784 | * @brief Test if string precedes string. | |
93c66bc6 BK |
3785 | * @param __lhs First string. |
3786 | * @param __rhs Second string. | |
3787 | * @return True if @a __lhs precedes @a __rhs. False otherwise. | |
ed6814f7 | 3788 | */ |
725dc051 | 3789 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3790 | _GLIBCXX_NODISCARD |
725dc051 BK |
3791 | inline bool |
3792 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3793 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3794 | _GLIBCXX_NOEXCEPT |
725dc051 BK |
3795 | { return __lhs.compare(__rhs) < 0; } |
3796 | ||
284f19bf JQ |
3797 | /** |
3798 | * @brief Test if string precedes C string. | |
93c66bc6 BK |
3799 | * @param __lhs String. |
3800 | * @param __rhs C string. | |
3801 | * @return True if @a __lhs precedes @a __rhs. False otherwise. | |
ed6814f7 | 3802 | */ |
725dc051 | 3803 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3804 | _GLIBCXX_NODISCARD |
725dc051 BK |
3805 | inline bool |
3806 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3807 | const _CharT* __rhs) | |
3808 | { return __lhs.compare(__rhs) < 0; } | |
3809 | ||
284f19bf JQ |
3810 | /** |
3811 | * @brief Test if C string precedes string. | |
93c66bc6 BK |
3812 | * @param __lhs C string. |
3813 | * @param __rhs String. | |
3814 | * @return True if @a __lhs precedes @a __rhs. False otherwise. | |
ed6814f7 | 3815 | */ |
725dc051 | 3816 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3817 | _GLIBCXX_NODISCARD |
725dc051 BK |
3818 | inline bool |
3819 | operator<(const _CharT* __lhs, | |
3820 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
3821 | { return __rhs.compare(__lhs) > 0; } | |
3822 | ||
3823 | // operator > | |
284f19bf JQ |
3824 | /** |
3825 | * @brief Test if string follows string. | |
93c66bc6 BK |
3826 | * @param __lhs First string. |
3827 | * @param __rhs Second string. | |
3828 | * @return True if @a __lhs follows @a __rhs. False otherwise. | |
ed6814f7 | 3829 | */ |
725dc051 | 3830 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3831 | _GLIBCXX_NODISCARD |
725dc051 BK |
3832 | inline bool |
3833 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3834 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3835 | _GLIBCXX_NOEXCEPT |
725dc051 BK |
3836 | { return __lhs.compare(__rhs) > 0; } |
3837 | ||
284f19bf JQ |
3838 | /** |
3839 | * @brief Test if string follows C string. | |
93c66bc6 BK |
3840 | * @param __lhs String. |
3841 | * @param __rhs C string. | |
3842 | * @return True if @a __lhs follows @a __rhs. False otherwise. | |
ed6814f7 | 3843 | */ |
725dc051 | 3844 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3845 | _GLIBCXX_NODISCARD |
725dc051 BK |
3846 | inline bool |
3847 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3848 | const _CharT* __rhs) | |
3849 | { return __lhs.compare(__rhs) > 0; } | |
3850 | ||
284f19bf JQ |
3851 | /** |
3852 | * @brief Test if C string follows string. | |
93c66bc6 BK |
3853 | * @param __lhs C string. |
3854 | * @param __rhs String. | |
3855 | * @return True if @a __lhs follows @a __rhs. False otherwise. | |
ed6814f7 | 3856 | */ |
725dc051 | 3857 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3858 | _GLIBCXX_NODISCARD |
725dc051 BK |
3859 | inline bool |
3860 | operator>(const _CharT* __lhs, | |
3861 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
3862 | { return __rhs.compare(__lhs) < 0; } | |
3863 | ||
3864 | // operator <= | |
284f19bf JQ |
3865 | /** |
3866 | * @brief Test if string doesn't follow string. | |
93c66bc6 BK |
3867 | * @param __lhs First string. |
3868 | * @param __rhs Second string. | |
3869 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. | |
ed6814f7 | 3870 | */ |
725dc051 | 3871 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3872 | _GLIBCXX_NODISCARD |
725dc051 BK |
3873 | inline bool |
3874 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3875 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3876 | _GLIBCXX_NOEXCEPT |
725dc051 BK |
3877 | { return __lhs.compare(__rhs) <= 0; } |
3878 | ||
284f19bf JQ |
3879 | /** |
3880 | * @brief Test if string doesn't follow C string. | |
93c66bc6 BK |
3881 | * @param __lhs String. |
3882 | * @param __rhs C string. | |
3883 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. | |
ed6814f7 | 3884 | */ |
725dc051 | 3885 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3886 | _GLIBCXX_NODISCARD |
725dc051 BK |
3887 | inline bool |
3888 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3889 | const _CharT* __rhs) | |
3890 | { return __lhs.compare(__rhs) <= 0; } | |
3891 | ||
284f19bf JQ |
3892 | /** |
3893 | * @brief Test if C string doesn't follow string. | |
93c66bc6 BK |
3894 | * @param __lhs C string. |
3895 | * @param __rhs String. | |
3896 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. | |
ed6814f7 | 3897 | */ |
725dc051 | 3898 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3899 | _GLIBCXX_NODISCARD |
725dc051 BK |
3900 | inline bool |
3901 | operator<=(const _CharT* __lhs, | |
3902 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
e9fb72e8 | 3903 | { return __rhs.compare(__lhs) >= 0; } |
725dc051 BK |
3904 | |
3905 | // operator >= | |
284f19bf JQ |
3906 | /** |
3907 | * @brief Test if string doesn't precede string. | |
93c66bc6 BK |
3908 | * @param __lhs First string. |
3909 | * @param __rhs Second string. | |
3910 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. | |
ed6814f7 | 3911 | */ |
725dc051 | 3912 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3913 | _GLIBCXX_NODISCARD |
725dc051 BK |
3914 | inline bool |
3915 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3916 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3917 | _GLIBCXX_NOEXCEPT |
725dc051 BK |
3918 | { return __lhs.compare(__rhs) >= 0; } |
3919 | ||
284f19bf JQ |
3920 | /** |
3921 | * @brief Test if string doesn't precede C string. | |
93c66bc6 BK |
3922 | * @param __lhs String. |
3923 | * @param __rhs C string. | |
3924 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. | |
ed6814f7 | 3925 | */ |
725dc051 | 3926 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3927 | _GLIBCXX_NODISCARD |
725dc051 BK |
3928 | inline bool |
3929 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3930 | const _CharT* __rhs) | |
3931 | { return __lhs.compare(__rhs) >= 0; } | |
3932 | ||
284f19bf JQ |
3933 | /** |
3934 | * @brief Test if C string doesn't precede string. | |
93c66bc6 BK |
3935 | * @param __lhs C string. |
3936 | * @param __rhs String. | |
3937 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. | |
ed6814f7 | 3938 | */ |
725dc051 | 3939 | template<typename _CharT, typename _Traits, typename _Alloc> |
b911ca42 | 3940 | _GLIBCXX_NODISCARD |
725dc051 BK |
3941 | inline bool |
3942 | operator>=(const _CharT* __lhs, | |
3943 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
3944 | { return __rhs.compare(__lhs) <= 0; } | |
875d6cb3 | 3945 | #endif // three-way comparison |
725dc051 | 3946 | |
284f19bf JQ |
3947 | /** |
3948 | * @brief Swap contents of two strings. | |
93c66bc6 BK |
3949 | * @param __lhs First string. |
3950 | * @param __rhs Second string. | |
284f19bf | 3951 | * |
93c66bc6 | 3952 | * Exchanges the contents of @a __lhs and @a __rhs in constant time. |
284f19bf | 3953 | */ |
725dc051 | 3954 | template<typename _CharT, typename _Traits, typename _Alloc> |
b96e2ff9 | 3955 | _GLIBCXX20_CONSTEXPR |
725dc051 BK |
3956 | inline void |
3957 | swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, | |
3958 | basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
5caff414 | 3959 | _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs))) |
725dc051 BK |
3960 | { __lhs.swap(__rhs); } |
3961 | ||
34a2b755 | 3962 | |
284f19bf JQ |
3963 | /** |
3964 | * @brief Read stream into a string. | |
93c66bc6 BK |
3965 | * @param __is Input stream. |
3966 | * @param __str Buffer to store into. | |
284f19bf JQ |
3967 | * @return Reference to the input stream. |
3968 | * | |
93c66bc6 BK |
3969 | * Stores characters from @a __is into @a __str until whitespace is |
3970 | * found, the end of the stream is encountered, or str.max_size() | |
3971 | * is reached. If is.width() is non-zero, that is the limit on the | |
3972 | * number of characters stored into @a __str. Any previous | |
3973 | * contents of @a __str are erased. | |
284f19bf | 3974 | */ |
725dc051 BK |
3975 | template<typename _CharT, typename _Traits, typename _Alloc> |
3976 | basic_istream<_CharT, _Traits>& | |
3977 | operator>>(basic_istream<_CharT, _Traits>& __is, | |
3978 | basic_string<_CharT, _Traits, _Alloc>& __str); | |
3979 | ||
ceed88b1 PC |
3980 | template<> |
3981 | basic_istream<char>& | |
3982 | operator>>(basic_istream<char>& __is, basic_string<char>& __str); | |
3983 | ||
284f19bf JQ |
3984 | /** |
3985 | * @brief Write string to a stream. | |
93c66bc6 BK |
3986 | * @param __os Output stream. |
3987 | * @param __str String to write out. | |
284f19bf JQ |
3988 | * @return Reference to the output stream. |
3989 | * | |
93c66bc6 | 3990 | * Output characters of @a __str into os following the same rules as for |
284f19bf JQ |
3991 | * writing a C string. |
3992 | */ | |
725dc051 | 3993 | template<typename _CharT, typename _Traits, typename _Alloc> |
70c99f6c | 3994 | inline basic_ostream<_CharT, _Traits>& |
725dc051 | 3995 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
ec2061a9 PC |
3996 | const basic_string<_CharT, _Traits, _Alloc>& __str) |
3997 | { | |
3998 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
3999 | // 586. string inserter not a formatted function | |
11202768 | 4000 | return __ostream_insert(__os, __str.data(), __str.size()); |
ec2061a9 | 4001 | } |
725dc051 | 4002 | |
284f19bf JQ |
4003 | /** |
4004 | * @brief Read a line from stream into a string. | |
93c66bc6 BK |
4005 | * @param __is Input stream. |
4006 | * @param __str Buffer to store into. | |
4007 | * @param __delim Character marking end of line. | |
284f19bf JQ |
4008 | * @return Reference to the input stream. |
4009 | * | |
93c66bc6 BK |
4010 | * Stores characters from @a __is into @a __str until @a __delim is |
4011 | * found, the end of the stream is encountered, or str.max_size() | |
3b2453a9 JW |
4012 | * is reached. Any previous contents of @a __str are erased. If |
4013 | * @a __delim is encountered, it is extracted but not stored into | |
4014 | * @a __str. | |
284f19bf | 4015 | */ |
725dc051 | 4016 | template<typename _CharT, typename _Traits, typename _Alloc> |
e9fb72e8 | 4017 | basic_istream<_CharT, _Traits>& |
725dc051 BK |
4018 | getline(basic_istream<_CharT, _Traits>& __is, |
4019 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); | |
4020 | ||
284f19bf JQ |
4021 | /** |
4022 | * @brief Read a line from stream into a string. | |
93c66bc6 BK |
4023 | * @param __is Input stream. |
4024 | * @param __str Buffer to store into. | |
284f19bf JQ |
4025 | * @return Reference to the input stream. |
4026 | * | |
93c66bc6 | 4027 | * Stores characters from is into @a __str until '\n' is |
2a60a9f6 | 4028 | * found, the end of the stream is encountered, or str.max_size() |
3b2453a9 JW |
4029 | * is reached. Any previous contents of @a __str are erased. If |
4030 | * end of line is encountered, it is extracted but not stored into | |
4031 | * @a __str. | |
284f19bf | 4032 | */ |
725dc051 | 4033 | template<typename _CharT, typename _Traits, typename _Alloc> |
e9fb72e8 | 4034 | inline basic_istream<_CharT, _Traits>& |
725dc051 | 4035 | getline(basic_istream<_CharT, _Traits>& __is, |
70c99f6c | 4036 | basic_string<_CharT, _Traits, _Alloc>& __str) |
19173661 JW |
4037 | { return std::getline(__is, __str, __is.widen('\n')); } |
4038 | ||
4039 | #if __cplusplus >= 201103L | |
4040 | /// Read a line from an rvalue stream into a string. | |
4041 | template<typename _CharT, typename _Traits, typename _Alloc> | |
caff45a6 | 4042 | inline basic_istream<_CharT, _Traits>& |
19173661 JW |
4043 | getline(basic_istream<_CharT, _Traits>&& __is, |
4044 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) | |
4045 | { return std::getline(__is, __str, __delim); } | |
4046 | ||
4047 | /// Read a line from an rvalue stream into a string. | |
4048 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4049 | inline basic_istream<_CharT, _Traits>& | |
4050 | getline(basic_istream<_CharT, _Traits>&& __is, | |
4051 | basic_string<_CharT, _Traits, _Alloc>& __str) | |
4052 | { return std::getline(__is, __str); } | |
4053 | #endif | |
70c99f6c | 4054 | |
e9fb72e8 PC |
4055 | template<> |
4056 | basic_istream<char>& | |
4057 | getline(basic_istream<char>& __in, basic_string<char>& __str, | |
4058 | char __delim); | |
4059 | ||
4060 | #ifdef _GLIBCXX_USE_WCHAR_T | |
4061 | template<> | |
4062 | basic_istream<wchar_t>& | |
4063 | getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, | |
4064 | wchar_t __delim); | |
4065 | #endif | |
3cbc7af0 | 4066 | |
12ffa228 BK |
4067 | _GLIBCXX_END_NAMESPACE_VERSION |
4068 | } // namespace | |
7364f286 | 4069 | |
23c64853 | 4070 | #if __cplusplus >= 201103L |
7364f286 | 4071 | |
a5a6b586 | 4072 | #include <ext/string_conversions.h> |
cd0b94e6 | 4073 | #include <bits/charconv.h> |
7364f286 | 4074 | |
12ffa228 BK |
4075 | namespace std _GLIBCXX_VISIBILITY(default) |
4076 | { | |
4077 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
34a2b755 | 4078 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
7364f286 | 4079 | |
23c64853 | 4080 | #if _GLIBCXX_USE_C99_STDLIB |
a5a6b586 PC |
4081 | // 21.4 Numeric Conversions [string.conversions]. |
4082 | inline int | |
4083 | stoi(const string& __str, size_t* __idx = 0, int __base = 10) | |
4084 | { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), | |
4085 | __idx, __base); } | |
4086 | ||
4087 | inline long | |
4088 | stol(const string& __str, size_t* __idx = 0, int __base = 10) | |
4089 | { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), | |
4090 | __idx, __base); } | |
4091 | ||
4092 | inline unsigned long | |
4093 | stoul(const string& __str, size_t* __idx = 0, int __base = 10) | |
4094 | { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), | |
4095 | __idx, __base); } | |
4096 | ||
4097 | inline long long | |
4098 | stoll(const string& __str, size_t* __idx = 0, int __base = 10) | |
4099 | { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), | |
4100 | __idx, __base); } | |
4101 | ||
4102 | inline unsigned long long | |
4103 | stoull(const string& __str, size_t* __idx = 0, int __base = 10) | |
4104 | { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), | |
4105 | __idx, __base); } | |
4106 | ||
4107 | // NB: strtof vs strtod. | |
4108 | inline float | |
4109 | stof(const string& __str, size_t* __idx = 0) | |
4110 | { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } | |
4111 | ||
4112 | inline double | |
4113 | stod(const string& __str, size_t* __idx = 0) | |
4114 | { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } | |
4115 | ||
4116 | inline long double | |
4117 | stold(const string& __str, size_t* __idx = 0) | |
4118 | { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } | |
23c64853 | 4119 | #endif // _GLIBCXX_USE_C99_STDLIB |
a5a6b586 | 4120 | |
cd0b94e6 | 4121 | // DR 1261. Insufficent overloads for to_string / to_wstring |
a4ecd144 | 4122 | |
b911ca42 | 4123 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4124 | inline string |
4125 | to_string(int __val) | |
9d813ddd JW |
4126 | #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32 |
4127 | noexcept // any 32-bit value fits in the SSO buffer | |
4128 | #endif | |
cd0b94e6 JW |
4129 | { |
4130 | const bool __neg = __val < 0; | |
4131 | const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val; | |
4132 | const auto __len = __detail::__to_chars_len(__uval); | |
4133 | string __str(__neg + __len, '-'); | |
4134 | __detail::__to_chars_10_impl(&__str[__neg], __len, __uval); | |
4135 | return __str; | |
4136 | } | |
a4ecd144 | 4137 | |
b911ca42 | 4138 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4139 | inline string |
4140 | to_string(unsigned __val) | |
9d813ddd JW |
4141 | #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32 |
4142 | noexcept // any 32-bit value fits in the SSO buffer | |
4143 | #endif | |
cd0b94e6 JW |
4144 | { |
4145 | string __str(__detail::__to_chars_len(__val), '\0'); | |
4146 | __detail::__to_chars_10_impl(&__str[0], __str.size(), __val); | |
4147 | return __str; | |
4148 | } | |
a4ecd144 | 4149 | |
b911ca42 | 4150 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4151 | inline string |
4152 | to_string(long __val) | |
9d813ddd JW |
4153 | #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32 |
4154 | noexcept // any 32-bit value fits in the SSO buffer | |
4155 | #endif | |
cd0b94e6 JW |
4156 | { |
4157 | const bool __neg = __val < 0; | |
4158 | const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val; | |
4159 | const auto __len = __detail::__to_chars_len(__uval); | |
4160 | string __str(__neg + __len, '-'); | |
4161 | __detail::__to_chars_10_impl(&__str[__neg], __len, __uval); | |
4162 | return __str; | |
4163 | } | |
a4ecd144 | 4164 | |
b911ca42 | 4165 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4166 | inline string |
4167 | to_string(unsigned long __val) | |
9d813ddd JW |
4168 | #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32 |
4169 | noexcept // any 32-bit value fits in the SSO buffer | |
4170 | #endif | |
cd0b94e6 JW |
4171 | { |
4172 | string __str(__detail::__to_chars_len(__val), '\0'); | |
4173 | __detail::__to_chars_10_impl(&__str[0], __str.size(), __val); | |
4174 | return __str; | |
4175 | } | |
a4ecd144 | 4176 | |
b911ca42 | 4177 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4178 | inline string |
4179 | to_string(long long __val) | |
cd0b94e6 JW |
4180 | { |
4181 | const bool __neg = __val < 0; | |
4182 | const unsigned long long __uval | |
4183 | = __neg ? (unsigned long long)~__val + 1ull : __val; | |
4184 | const auto __len = __detail::__to_chars_len(__uval); | |
4185 | string __str(__neg + __len, '-'); | |
4186 | __detail::__to_chars_10_impl(&__str[__neg], __len, __uval); | |
4187 | return __str; | |
4188 | } | |
a5a6b586 | 4189 | |
b911ca42 | 4190 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4191 | inline string |
4192 | to_string(unsigned long long __val) | |
cd0b94e6 JW |
4193 | { |
4194 | string __str(__detail::__to_chars_len(__val), '\0'); | |
4195 | __detail::__to_chars_10_impl(&__str[0], __str.size(), __val); | |
4196 | return __str; | |
4197 | } | |
4198 | ||
4199 | #if _GLIBCXX_USE_C99_STDIO | |
4200 | // NB: (v)snprintf vs sprintf. | |
a5a6b586 | 4201 | |
b911ca42 | 4202 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4203 | inline string |
4204 | to_string(float __val) | |
4205 | { | |
4206 | const int __n = | |
4207 | __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; | |
4208 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, | |
4209 | "%f", __val); | |
4210 | } | |
4211 | ||
b911ca42 | 4212 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4213 | inline string |
4214 | to_string(double __val) | |
4215 | { | |
4216 | const int __n = | |
4217 | __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; | |
4218 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, | |
4219 | "%f", __val); | |
4220 | } | |
4221 | ||
b911ca42 | 4222 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4223 | inline string |
4224 | to_string(long double __val) | |
4225 | { | |
4226 | const int __n = | |
4227 | __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; | |
4228 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, | |
4229 | "%Lf", __val); | |
4230 | } | |
23c64853 | 4231 | #endif // _GLIBCXX_USE_C99_STDIO |
7364f286 | 4232 | |
356510ac | 4233 | #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR |
a5a6b586 PC |
4234 | inline int |
4235 | stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) | |
4236 | { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), | |
4237 | __idx, __base); } | |
4238 | ||
4239 | inline long | |
4240 | stol(const wstring& __str, size_t* __idx = 0, int __base = 10) | |
4241 | { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), | |
4242 | __idx, __base); } | |
4243 | ||
4244 | inline unsigned long | |
4245 | stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) | |
4246 | { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), | |
4247 | __idx, __base); } | |
4248 | ||
4249 | inline long long | |
4250 | stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) | |
4251 | { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), | |
4252 | __idx, __base); } | |
4253 | ||
4254 | inline unsigned long long | |
4255 | stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) | |
4256 | { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), | |
4257 | __idx, __base); } | |
4258 | ||
4259 | // NB: wcstof vs wcstod. | |
4260 | inline float | |
4261 | stof(const wstring& __str, size_t* __idx = 0) | |
4262 | { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } | |
4263 | ||
4264 | inline double | |
4265 | stod(const wstring& __str, size_t* __idx = 0) | |
4266 | { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } | |
4267 | ||
4268 | inline long double | |
4269 | stold(const wstring& __str, size_t* __idx = 0) | |
4270 | { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } | |
4271 | ||
f37f5fb8 | 4272 | #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF |
a4ecd144 | 4273 | // DR 1261. |
b911ca42 | 4274 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4275 | inline wstring |
4276 | to_wstring(int __val) | |
4277 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int), | |
4278 | L"%d", __val); } | |
4279 | ||
b911ca42 | 4280 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4281 | inline wstring |
4282 | to_wstring(unsigned __val) | |
4283 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, | |
4284 | 4 * sizeof(unsigned), | |
4285 | L"%u", __val); } | |
4286 | ||
b911ca42 | 4287 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4288 | inline wstring |
4289 | to_wstring(long __val) | |
4290 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long), | |
4291 | L"%ld", __val); } | |
4292 | ||
b911ca42 | 4293 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4294 | inline wstring |
4295 | to_wstring(unsigned long __val) | |
4296 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, | |
4297 | 4 * sizeof(unsigned long), | |
4298 | L"%lu", __val); } | |
4299 | ||
b911ca42 | 4300 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4301 | inline wstring |
4302 | to_wstring(long long __val) | |
4303 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, | |
4304 | 4 * sizeof(long long), | |
4305 | L"%lld", __val); } | |
4306 | ||
b911ca42 | 4307 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4308 | inline wstring |
4309 | to_wstring(unsigned long long __val) | |
4310 | { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, | |
4311 | 4 * sizeof(unsigned long long), | |
4312 | L"%llu", __val); } | |
4313 | ||
b911ca42 | 4314 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4315 | inline wstring |
4316 | to_wstring(float __val) | |
4317 | { | |
4318 | const int __n = | |
4319 | __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; | |
4320 | return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, | |
4321 | L"%f", __val); | |
4322 | } | |
4323 | ||
b911ca42 | 4324 | _GLIBCXX_NODISCARD |
a4ecd144 PC |
4325 | inline wstring |
4326 | to_wstring(double __val) | |
4327 | { | |
4328 | const int __n = | |
4329 | __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; | |
4330 | return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, | |
4331 | L"%f", __val); | |
4332 | } | |
4333 | ||
b911ca42 | 4334 | _GLIBCXX_NODISCARD |
a5a6b586 PC |
4335 | inline wstring |
4336 | to_wstring(long double __val) | |
4337 | { | |
4338 | const int __n = | |
4339 | __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; | |
4340 | return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, | |
4341 | L"%Lf", __val); | |
4342 | } | |
f37f5fb8 | 4343 | #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF |
23c64853 | 4344 | #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR |
7364f286 | 4345 | |
34a2b755 | 4346 | _GLIBCXX_END_NAMESPACE_CXX11 |
12ffa228 BK |
4347 | _GLIBCXX_END_NAMESPACE_VERSION |
4348 | } // namespace | |
725dc051 | 4349 | |
23c64853 | 4350 | #endif /* C++11 */ |
15d81a3c | 4351 | |
734f5023 | 4352 | #if __cplusplus >= 201103L |
15d81a3c PC |
4353 | |
4354 | #include <bits/functional_hash.h> | |
4355 | ||
12ffa228 BK |
4356 | namespace std _GLIBCXX_VISIBILITY(default) |
4357 | { | |
4358 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
15d81a3c | 4359 | |
b370ed0b JW |
4360 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
4361 | // 3705. Hashability shouldn't depend on basic_string's allocator | |
15d81a3c | 4362 | |
b370ed0b JW |
4363 | template<typename _CharT, typename _Alloc, |
4364 | typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>> | |
4365 | struct __str_hash_base | |
4366 | : public __hash_base<size_t, _StrT> | |
15d81a3c | 4367 | { |
b911ca42 | 4368 | [[__nodiscard__]] |
15d81a3c | 4369 | size_t |
b370ed0b JW |
4370 | operator()(const _StrT& __s) const noexcept |
4371 | { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); } | |
15d81a3c PC |
4372 | }; |
4373 | ||
b370ed0b JW |
4374 | #ifndef _GLIBCXX_COMPATIBILITY_CXX0X |
4375 | /// std::hash specialization for string. | |
4376 | template<typename _Alloc> | |
4377 | struct hash<basic_string<char, char_traits<char>, _Alloc>> | |
4378 | : public __str_hash_base<char, _Alloc> | |
4df047dd FD |
4379 | { }; |
4380 | ||
15d81a3c | 4381 | /// std::hash specialization for wstring. |
b370ed0b JW |
4382 | template<typename _Alloc> |
4383 | struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>> | |
4384 | : public __str_hash_base<wchar_t, _Alloc> | |
4385 | { }; | |
4df047dd | 4386 | |
b370ed0b JW |
4387 | template<typename _Alloc> |
4388 | struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>, | |
4389 | _Alloc>>> | |
4390 | : std::false_type | |
4df047dd | 4391 | { }; |
15d81a3c PC |
4392 | #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ |
4393 | ||
c124af93 TH |
4394 | #ifdef _GLIBCXX_USE_CHAR8_T |
4395 | /// std::hash specialization for u8string. | |
b370ed0b JW |
4396 | template<typename _Alloc> |
4397 | struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>> | |
4398 | : public __str_hash_base<char8_t, _Alloc> | |
c124af93 TH |
4399 | { }; |
4400 | #endif | |
4401 | ||
15d81a3c | 4402 | /// std::hash specialization for u16string. |
b370ed0b JW |
4403 | template<typename _Alloc> |
4404 | struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>> | |
4405 | : public __str_hash_base<char16_t, _Alloc> | |
4df047dd FD |
4406 | { }; |
4407 | ||
15d81a3c | 4408 | /// std::hash specialization for u32string. |
b370ed0b JW |
4409 | template<typename _Alloc> |
4410 | struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>> | |
4411 | : public __str_hash_base<char32_t, _Alloc> | |
4412 | { }; | |
4df047dd | 4413 | |
b370ed0b JW |
4414 | #if ! _GLIBCXX_INLINE_VERSION |
4415 | // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI. | |
4416 | template<> struct __is_fast_hash<hash<string>> : std::false_type { }; | |
4417 | template<> struct __is_fast_hash<hash<wstring>> : std::false_type { }; | |
4418 | template<> struct __is_fast_hash<hash<u16string>> : std::false_type { }; | |
4419 | template<> struct __is_fast_hash<hash<u32string>> : std::false_type { }; | |
4420 | #ifdef _GLIBCXX_USE_CHAR8_T | |
4421 | template<> struct __is_fast_hash<hash<u8string>> : std::false_type { }; | |
4422 | #endif | |
4423 | #else | |
4424 | // For versioned namespace, assume every std::hash<basic_string<>> is slow. | |
4425 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4426 | struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>> | |
4427 | : std::false_type | |
4df047dd | 4428 | { }; |
b370ed0b | 4429 | #endif |
15d81a3c | 4430 | |
10f26de9 | 4431 | #if __cplusplus >= 201402L |
1c9f675f | 4432 | |
b8806796 | 4433 | #define __cpp_lib_string_udls 201304L |
a15f7cb8 | 4434 | |
0372af98 ESR |
4435 | inline namespace literals |
4436 | { | |
4437 | inline namespace string_literals | |
4438 | { | |
f03858e5 JW |
4439 | #pragma GCC diagnostic push |
4440 | #pragma GCC diagnostic ignored "-Wliteral-suffix" | |
b96e2ff9 ML |
4441 | |
4442 | #if __cpp_lib_constexpr_string >= 201907L | |
4443 | # define _GLIBCXX_STRING_CONSTEXPR constexpr | |
4444 | #else | |
4445 | # define _GLIBCXX_STRING_CONSTEXPR | |
4446 | #endif | |
4447 | ||
4448 | _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR | |
88c4d6b7 | 4449 | inline basic_string<char> |
e9a64492 | 4450 | operator""s(const char* __str, size_t __len) |
88c4d6b7 | 4451 | { return basic_string<char>{__str, __len}; } |
1c9f675f | 4452 | |
b96e2ff9 | 4453 | _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR |
88c4d6b7 | 4454 | inline basic_string<wchar_t> |
e9a64492 | 4455 | operator""s(const wchar_t* __str, size_t __len) |
88c4d6b7 | 4456 | { return basic_string<wchar_t>{__str, __len}; } |
1c9f675f | 4457 | |
c124af93 | 4458 | #ifdef _GLIBCXX_USE_CHAR8_T |
b96e2ff9 | 4459 | _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR |
c124af93 TH |
4460 | inline basic_string<char8_t> |
4461 | operator""s(const char8_t* __str, size_t __len) | |
4462 | { return basic_string<char8_t>{__str, __len}; } | |
4463 | #endif | |
4464 | ||
b96e2ff9 | 4465 | _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR |
88c4d6b7 | 4466 | inline basic_string<char16_t> |
e9a64492 | 4467 | operator""s(const char16_t* __str, size_t __len) |
88c4d6b7 | 4468 | { return basic_string<char16_t>{__str, __len}; } |
1c9f675f | 4469 | |
b96e2ff9 | 4470 | _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR |
88c4d6b7 | 4471 | inline basic_string<char32_t> |
e9a64492 | 4472 | operator""s(const char32_t* __str, size_t __len) |
88c4d6b7 | 4473 | { return basic_string<char32_t>{__str, __len}; } |
1c9f675f | 4474 | |
b96e2ff9 | 4475 | #undef _GLIBCXX_STRING_CONSTEXPR |
f03858e5 | 4476 | #pragma GCC diagnostic pop |
88c4d6b7 ESR |
4477 | } // inline namespace string_literals |
4478 | } // inline namespace literals | |
4479 | ||
10f26de9 JW |
4480 | #if __cplusplus >= 201703L |
4481 | namespace __detail::__variant | |
4482 | { | |
4483 | template<typename> struct _Never_valueless_alt; // see <variant> | |
4484 | ||
4485 | // Provide the strong exception-safety guarantee when emplacing a | |
47a468bd | 4486 | // basic_string into a variant, but only if moving the string cannot throw. |
10f26de9 JW |
4487 | template<typename _Tp, typename _Traits, typename _Alloc> |
4488 | struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>> | |
47a468bd JW |
4489 | : __and_< |
4490 | is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>, | |
4491 | is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>> | |
4492 | >::type | |
10f26de9 JW |
4493 | { }; |
4494 | } // namespace __detail::__variant | |
4495 | #endif // C++17 | |
4496 | #endif // C++14 | |
1c9f675f | 4497 | |
4a15d842 | 4498 | _GLIBCXX_END_NAMESPACE_VERSION |
1c9f675f ESR |
4499 | } // namespace std |
4500 | ||
734f5023 | 4501 | #endif // C++11 |
a5a6b586 | 4502 | |
3d7c150e | 4503 | #endif /* _BASIC_STRING_H */ |