1 | // Components for manipulating sequences of characters -*- C++ -*- |
2 | |
3 | // Copyright (C) 1997-2019 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/basic_string.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{string} |
28 | */ |
29 | |
30 | // |
31 | // ISO C++ 14882: 21 Strings library |
32 | // |
33 | |
34 | #ifndef _BASIC_STRING_H |
35 | #define _BASIC_STRING_H 1 |
36 | |
37 | #pragma GCC system_header |
38 | |
39 | #include <ext/atomicity.h> |
40 | #include <ext/alloc_traits.h> |
41 | #include <debug/debug.h> |
42 | |
43 | #if __cplusplus >= 201103L |
44 | #include <initializer_list> |
45 | #endif |
46 | |
47 | #if __cplusplus >= 201703L |
48 | # include <string_view> |
49 | #endif |
50 | |
51 | namespace std _GLIBCXX_VISIBILITY(default) |
52 | { |
53 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
54 | |
55 | #if __cplusplus >= 201703L |
56 | // Support P0426R1 changes to char_traits in C++17. |
57 | # define __cpp_lib_constexpr_string 201611L |
58 | #endif |
59 | |
60 | #if _GLIBCXX_USE_CXX11_ABI |
61 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
62 | /** |
63 | * @class basic_string basic_string.h <string> |
64 | * @brief Managing sequences of characters and character-like objects. |
65 | * |
66 | * @ingroup strings |
67 | * @ingroup sequences |
68 | * |
69 | * @tparam _CharT Type of character |
70 | * @tparam _Traits Traits for character type, defaults to |
71 | * char_traits<_CharT>. |
72 | * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. |
73 | * |
74 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
75 | * <a href="tables.html#66">reversible container</a>, and a |
76 | * <a href="tables.html#67">sequence</a>. Of the |
77 | * <a href="tables.html#68">optional sequence requirements</a>, only |
78 | * @c push_back, @c at, and @c %array access are supported. |
79 | */ |
80 | template<typename _CharT, typename _Traits, typename _Alloc> |
81 | class basic_string |
82 | { |
83 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
84 | rebind<_CharT>::other _Char_alloc_type; |
85 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; |
86 | |
87 | // Types: |
88 | public: |
89 | typedef _Traits traits_type; |
90 | typedef typename _Traits::char_type value_type; |
91 | typedef _Char_alloc_type allocator_type; |
92 | typedef typename _Alloc_traits::size_type size_type; |
93 | typedef typename _Alloc_traits::difference_type difference_type; |
94 | typedef typename _Alloc_traits::reference reference; |
95 | typedef typename _Alloc_traits::const_reference const_reference; |
96 | typedef typename _Alloc_traits::pointer pointer; |
97 | typedef typename _Alloc_traits::const_pointer const_pointer; |
98 | typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; |
99 | typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> |
100 | const_iterator; |
101 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
102 | typedef std::reverse_iterator<iterator> reverse_iterator; |
103 | |
104 | /// Value returned by various member functions when they fail. |
105 | static const size_type npos = static_cast<size_type>(-1); |
106 | |
107 | protected: |
108 | // type used for positions in insert, erase etc. |
109 | #if __cplusplus < 201103L |
110 | typedef iterator __const_iterator; |
111 | #else |
112 | typedef const_iterator __const_iterator; |
113 | #endif |
114 | |
115 | private: |
116 | #if __cplusplus >= 201703L |
117 | // A helper type for avoiding boiler-plate. |
118 | typedef basic_string_view<_CharT, _Traits> __sv_type; |
119 | |
120 | template<typename _Tp, typename _Res> |
121 | using _If_sv = enable_if_t< |
122 | __and_<is_convertible<const _Tp&, __sv_type>, |
123 | __not_<is_convertible<const _Tp*, const basic_string*>>, |
124 | __not_<is_convertible<const _Tp&, const _CharT*>>>::value, |
125 | _Res>; |
126 | |
127 | // Allows an implicit conversion to __sv_type. |
128 | static __sv_type |
129 | _S_to_string_view(__sv_type __svt) noexcept |
130 | { return __svt; } |
131 | |
132 | // Wraps a string_view by explicit conversion and thus |
133 | // allows to add an internal constructor that does not |
134 | // participate in overload resolution when a string_view |
135 | // is provided. |
136 | struct __sv_wrapper |
137 | { |
138 | explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } |
139 | __sv_type _M_sv; |
140 | }; |
141 | |
142 | /** |
143 | * @brief Only internally used: Construct string from a string view |
144 | * wrapper. |
145 | * @param __svw string view wrapper. |
146 | * @param __a Allocator to use. |
147 | */ |
148 | explicit |
149 | basic_string(__sv_wrapper __svw, const _Alloc& __a) |
150 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } |
151 | #endif |
152 | |
153 | // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
154 | struct _Alloc_hider : allocator_type // TODO check __is_final |
155 | { |
156 | #if __cplusplus < 201103L |
157 | _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) |
158 | : allocator_type(__a), _M_p(__dat) { } |
159 | #else |
160 | _Alloc_hider(pointer __dat, const _Alloc& __a) |
161 | : allocator_type(__a), _M_p(__dat) { } |
162 | |
163 | _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc()) |
164 | : allocator_type(std::move(__a)), _M_p(__dat) { } |
165 | #endif |
166 | |
167 | pointer _M_p; // The actual data. |
168 | }; |
169 | |
170 | _Alloc_hider _M_dataplus; |
171 | size_type _M_string_length; |
172 | |
173 | enum { _S_local_capacity = 15 / sizeof(_CharT) }; |
174 | |
175 | union |
176 | { |
177 | _CharT _M_local_buf[_S_local_capacity + 1]; |
178 | size_type _M_allocated_capacity; |
179 | }; |
180 | |
181 | void |
182 | _M_data(pointer __p) |
183 | { _M_dataplus._M_p = __p; } |
184 | |
185 | void |
186 | _M_length(size_type __length) |
187 | { _M_string_length = __length; } |
188 | |
189 | pointer |
190 | _M_data() const |
191 | { return _M_dataplus._M_p; } |
192 | |
193 | pointer |
194 | _M_local_data() |
195 | { |
196 | #if __cplusplus >= 201103L |
197 | return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); |
198 | #else |
199 | return pointer(_M_local_buf); |
200 | #endif |
201 | } |
202 | |
203 | const_pointer |
204 | _M_local_data() const |
205 | { |
206 | #if __cplusplus >= 201103L |
207 | return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); |
208 | #else |
209 | return const_pointer(_M_local_buf); |
210 | #endif |
211 | } |
212 | |
213 | void |
214 | _M_capacity(size_type __capacity) |
215 | { _M_allocated_capacity = __capacity; } |
216 | |
217 | void |
218 | _M_set_length(size_type __n) |
219 | { |
220 | _M_length(__n); |
221 | traits_type::assign(_M_data()[__n], _CharT()); |
222 | } |
223 | |
224 | bool |
225 | _M_is_local() const |
226 | { return _M_data() == _M_local_data(); } |
227 | |
228 | // Create & Destroy |
229 | pointer |
230 | _M_create(size_type&, size_type); |
231 | |
232 | void |
233 | _M_dispose() |
234 | { |
235 | if (!_M_is_local()) |
236 | _M_destroy(_M_allocated_capacity); |
237 | } |
238 | |
239 | void |
240 | _M_destroy(size_type __size) throw() |
241 | { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } |
242 | |
243 | // _M_construct_aux is used to implement the 21.3.1 para 15 which |
244 | // requires special behaviour if _InIterator is an integral type |
245 | template<typename _InIterator> |
246 | void |
247 | _M_construct_aux(_InIterator __beg, _InIterator __end, |
248 | std::__false_type) |
249 | { |
250 | typedef typename iterator_traits<_InIterator>::iterator_category _Tag; |
251 | _M_construct(__beg, __end, _Tag()); |
252 | } |
253 | |
254 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
255 | // 438. Ambiguity in the "do the right thing" clause |
256 | template<typename _Integer> |
257 | void |
258 | _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) |
259 | { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } |
260 | |
261 | void |
262 | _M_construct_aux_2(size_type __req, _CharT __c) |
263 | { _M_construct(__req, __c); } |
264 | |
265 | template<typename _InIterator> |
266 | void |
267 | _M_construct(_InIterator __beg, _InIterator __end) |
268 | { |
269 | typedef typename std::__is_integer<_InIterator>::__type _Integral; |
270 | _M_construct_aux(__beg, __end, _Integral()); |
271 | } |
272 | |
273 | // For Input Iterators, used in istreambuf_iterators, etc. |
274 | template<typename _InIterator> |
275 | void |
276 | _M_construct(_InIterator __beg, _InIterator __end, |
277 | std::input_iterator_tag); |
278 | |
279 | // For forward_iterators up to random_access_iterators, used for |
280 | // string::iterator, _CharT*, etc. |
281 | template<typename _FwdIterator> |
282 | void |
283 | _M_construct(_FwdIterator __beg, _FwdIterator __end, |
284 | std::forward_iterator_tag); |
285 | |
286 | void |
287 | _M_construct(size_type __req, _CharT __c); |
288 | |
289 | allocator_type& |
290 | _M_get_allocator() |
291 | { return _M_dataplus; } |
292 | |
293 | const allocator_type& |
294 | _M_get_allocator() const |
295 | { return _M_dataplus; } |
296 | |
297 | private: |
298 | |
299 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
300 | // The explicit instantiations in misc-inst.cc require this due to |
301 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 |
302 | template<typename _Tp, bool _Requires = |
303 | !__are_same<_Tp, _CharT*>::__value |
304 | && !__are_same<_Tp, const _CharT*>::__value |
305 | && !__are_same<_Tp, iterator>::__value |
306 | && !__are_same<_Tp, const_iterator>::__value> |
307 | struct __enable_if_not_native_iterator |
308 | { typedef basic_string& __type; }; |
309 | template<typename _Tp> |
310 | struct __enable_if_not_native_iterator<_Tp, false> { }; |
311 | #endif |
312 | |
313 | size_type |
314 | _M_check(size_type __pos, const char* __s) const |
315 | { |
316 | if (__pos > this->size()) |
317 | __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " |
318 | "this->size() (which is %zu)" ), |
319 | __s, __pos, this->size()); |
320 | return __pos; |
321 | } |
322 | |
323 | void |
324 | _M_check_length(size_type __n1, size_type __n2, const char* __s) const |
325 | { |
326 | if (this->max_size() - (this->size() - __n1) < __n2) |
327 | __throw_length_error(__N(__s)); |
328 | } |
329 | |
330 | |
331 | // NB: _M_limit doesn't check for a bad __pos value. |
332 | size_type |
333 | _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT |
334 | { |
335 | const bool __testoff = __off < this->size() - __pos; |
336 | return __testoff ? __off : this->size() - __pos; |
337 | } |
338 | |
339 | // True if _Rep and source do not overlap. |
340 | bool |
341 | _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT |
342 | { |
343 | return (less<const _CharT*>()(__s, _M_data()) |
344 | || less<const _CharT*>()(_M_data() + this->size(), __s)); |
345 | } |
346 | |
347 | // When __n = 1 way faster than the general multichar |
348 | // traits_type::copy/move/assign. |
349 | static void |
350 | _S_copy(_CharT* __d, const _CharT* __s, size_type __n) |
351 | { |
352 | if (__n == 1) |
353 | traits_type::assign(*__d, *__s); |
354 | else |
355 | traits_type::copy(__d, __s, __n); |
356 | } |
357 | |
358 | static void |
359 | _S_move(_CharT* __d, const _CharT* __s, size_type __n) |
360 | { |
361 | if (__n == 1) |
362 | traits_type::assign(*__d, *__s); |
363 | else |
364 | traits_type::move(__d, __s, __n); |
365 | } |
366 | |
367 | static void |
368 | _S_assign(_CharT* __d, size_type __n, _CharT __c) |
369 | { |
370 | if (__n == 1) |
371 | traits_type::assign(*__d, __c); |
372 | else |
373 | traits_type::assign(__d, __n, __c); |
374 | } |
375 | |
376 | // _S_copy_chars is a separate template to permit specialization |
377 | // to optimize for the common case of pointers as iterators. |
378 | template<class _Iterator> |
379 | static void |
380 | _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) |
381 | { |
382 | for (; __k1 != __k2; ++__k1, (void)++__p) |
383 | traits_type::assign(*__p, *__k1); // These types are off. |
384 | } |
385 | |
386 | static void |
387 | _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT |
388 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
389 | |
390 | static void |
391 | _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) |
392 | _GLIBCXX_NOEXCEPT |
393 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
394 | |
395 | static void |
396 | _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT |
397 | { _S_copy(__p, __k1, __k2 - __k1); } |
398 | |
399 | static void |
400 | _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) |
401 | _GLIBCXX_NOEXCEPT |
402 | { _S_copy(__p, __k1, __k2 - __k1); } |
403 | |
404 | static int |
405 | _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT |
406 | { |
407 | const difference_type __d = difference_type(__n1 - __n2); |
408 | |
409 | if (__d > __gnu_cxx::__numeric_traits<int>::__max) |
410 | return __gnu_cxx::__numeric_traits<int>::__max; |
411 | else if (__d < __gnu_cxx::__numeric_traits<int>::__min) |
412 | return __gnu_cxx::__numeric_traits<int>::__min; |
413 | else |
414 | return int(__d); |
415 | } |
416 | |
417 | void |
418 | _M_assign(const basic_string&); |
419 | |
420 | void |
421 | _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, |
422 | size_type __len2); |
423 | |
424 | void |
425 | _M_erase(size_type __pos, size_type __n); |
426 | |
427 | public: |
428 | // Construct/copy/destroy: |
429 | // NB: We overload ctors in some cases instead of using default |
430 | // arguments, per 17.4.4.4 para. 2 item 2. |
431 | |
432 | /** |
433 | * @brief Default constructor creates an empty string. |
434 | */ |
435 | basic_string() |
436 | _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value) |
437 | : _M_dataplus(_M_local_data()) |
438 | { _M_set_length(0); } |
439 | |
440 | /** |
441 | * @brief Construct an empty string using allocator @a a. |
442 | */ |
443 | explicit |
444 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT |
445 | : _M_dataplus(_M_local_data(), __a) |
446 | { _M_set_length(0); } |
447 | |
448 | /** |
449 | * @brief Construct string with copy of value of @a __str. |
450 | * @param __str Source string. |
451 | */ |
452 | basic_string(const basic_string& __str) |
453 | : _M_dataplus(_M_local_data(), |
454 | _Alloc_traits::_S_select_on_copy(__str._M_get_allocator())) |
455 | { _M_construct(__str._M_data(), __str._M_data() + __str.length()); } |
456 | |
457 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
458 | // 2583. no way to supply an allocator for basic_string(str, pos) |
459 | /** |
460 | * @brief Construct string as copy of a substring. |
461 | * @param __str Source string. |
462 | * @param __pos Index of first character to copy from. |
463 | * @param __a Allocator to use. |
464 | */ |
465 | basic_string(const basic_string& __str, size_type __pos, |
466 | const _Alloc& __a = _Alloc()) |
467 | : _M_dataplus(_M_local_data(), __a) |
468 | { |
469 | const _CharT* __start = __str._M_data() |
470 | + __str._M_check(__pos, "basic_string::basic_string" ); |
471 | _M_construct(__start, __start + __str._M_limit(__pos, npos)); |
472 | } |
473 | |
474 | /** |
475 | * @brief Construct string as copy of a substring. |
476 | * @param __str Source string. |
477 | * @param __pos Index of first character to copy from. |
478 | * @param __n Number of characters to copy. |
479 | */ |
480 | basic_string(const basic_string& __str, size_type __pos, |
481 | size_type __n) |
482 | : _M_dataplus(_M_local_data()) |
483 | { |
484 | const _CharT* __start = __str._M_data() |
485 | + __str._M_check(__pos, "basic_string::basic_string" ); |
486 | _M_construct(__start, __start + __str._M_limit(__pos, __n)); |
487 | } |
488 | |
489 | /** |
490 | * @brief Construct string as copy of a substring. |
491 | * @param __str Source string. |
492 | * @param __pos Index of first character to copy from. |
493 | * @param __n Number of characters to copy. |
494 | * @param __a Allocator to use. |
495 | */ |
496 | basic_string(const basic_string& __str, size_type __pos, |
497 | size_type __n, const _Alloc& __a) |
498 | : _M_dataplus(_M_local_data(), __a) |
499 | { |
500 | const _CharT* __start |
501 | = __str._M_data() + __str._M_check(__pos, "string::string" ); |
502 | _M_construct(__start, __start + __str._M_limit(__pos, __n)); |
503 | } |
504 | |
505 | /** |
506 | * @brief Construct string initialized by a character %array. |
507 | * @param __s Source character %array. |
508 | * @param __n Number of characters to copy. |
509 | * @param __a Allocator to use (default is default allocator). |
510 | * |
511 | * NB: @a __s must have at least @a __n characters, '\\0' |
512 | * has no special meaning. |
513 | */ |
514 | basic_string(const _CharT* __s, size_type __n, |
515 | const _Alloc& __a = _Alloc()) |
516 | : _M_dataplus(_M_local_data(), __a) |
517 | { _M_construct(__s, __s + __n); } |
518 | |
519 | /** |
520 | * @brief Construct string as copy of a C string. |
521 | * @param __s Source C string. |
522 | * @param __a Allocator to use (default is default allocator). |
523 | */ |
524 | #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
525 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
526 | // 3076. basic_string CTAD ambiguity |
527 | template<typename = _RequireAllocator<_Alloc>> |
528 | #endif |
529 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) |
530 | : _M_dataplus(_M_local_data(), __a) |
531 | { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); } |
532 | |
533 | /** |
534 | * @brief Construct string as multiple characters. |
535 | * @param __n Number of characters. |
536 | * @param __c Character to use. |
537 | * @param __a Allocator to use (default is default allocator). |
538 | */ |
539 | #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
540 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
541 | // 3076. basic_string CTAD ambiguity |
542 | template<typename = _RequireAllocator<_Alloc>> |
543 | #endif |
544 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) |
545 | : _M_dataplus(_M_local_data(), __a) |
546 | { _M_construct(__n, __c); } |
547 | |
548 | #if __cplusplus >= 201103L |
549 | /** |
550 | * @brief Move construct string. |
551 | * @param __str Source string. |
552 | * |
553 | * The newly-created string contains the exact contents of @a __str. |
554 | * @a __str is a valid, but unspecified string. |
555 | **/ |
556 | basic_string(basic_string&& __str) noexcept |
557 | : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) |
558 | { |
559 | if (__str._M_is_local()) |
560 | { |
561 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
562 | _S_local_capacity + 1); |
563 | } |
564 | else |
565 | { |
566 | _M_data(__str._M_data()); |
567 | _M_capacity(__str._M_allocated_capacity); |
568 | } |
569 | |
570 | // Must use _M_length() here not _M_set_length() because |
571 | // basic_stringbuf relies on writing into unallocated capacity so |
572 | // we mess up the contents if we put a '\0' in the string. |
573 | _M_length(__str.length()); |
574 | __str._M_data(__str._M_local_data()); |
575 | __str._M_set_length(0); |
576 | } |
577 | |
578 | /** |
579 | * @brief Construct string from an initializer %list. |
580 | * @param __l std::initializer_list of characters. |
581 | * @param __a Allocator to use (default is default allocator). |
582 | */ |
583 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) |
584 | : _M_dataplus(_M_local_data(), __a) |
585 | { _M_construct(__l.begin(), __l.end()); } |
586 | |
587 | basic_string(const basic_string& __str, const _Alloc& __a) |
588 | : _M_dataplus(_M_local_data(), __a) |
589 | { _M_construct(__str.begin(), __str.end()); } |
590 | |
591 | basic_string(basic_string&& __str, const _Alloc& __a) |
592 | noexcept(_Alloc_traits::_S_always_equal()) |
593 | : _M_dataplus(_M_local_data(), __a) |
594 | { |
595 | if (__str._M_is_local()) |
596 | { |
597 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
598 | _S_local_capacity + 1); |
599 | _M_length(__str.length()); |
600 | __str._M_set_length(0); |
601 | } |
602 | else if (_Alloc_traits::_S_always_equal() |
603 | || __str.get_allocator() == __a) |
604 | { |
605 | _M_data(__str._M_data()); |
606 | _M_length(__str.length()); |
607 | _M_capacity(__str._M_allocated_capacity); |
608 | __str._M_data(__str._M_local_buf); |
609 | __str._M_set_length(0); |
610 | } |
611 | else |
612 | _M_construct(__str.begin(), __str.end()); |
613 | } |
614 | |
615 | #endif // C++11 |
616 | |
617 | /** |
618 | * @brief Construct string as copy of a range. |
619 | * @param __beg Start of range. |
620 | * @param __end End of range. |
621 | * @param __a Allocator to use (default is default allocator). |
622 | */ |
623 | #if __cplusplus >= 201103L |
624 | template<typename _InputIterator, |
625 | typename = std::_RequireInputIter<_InputIterator>> |
626 | #else |
627 | template<typename _InputIterator> |
628 | #endif |
629 | basic_string(_InputIterator __beg, _InputIterator __end, |
630 | const _Alloc& __a = _Alloc()) |
631 | : _M_dataplus(_M_local_data(), __a) |
632 | { _M_construct(__beg, __end); } |
633 | |
634 | #if __cplusplus >= 201703L |
635 | /** |
636 | * @brief Construct string from a substring of a string_view. |
637 | * @param __t Source object convertible to string view. |
638 | * @param __pos The index of the first character to copy from __t. |
639 | * @param __n The number of characters to copy from __t. |
640 | * @param __a Allocator to use. |
641 | */ |
642 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
643 | basic_string(const _Tp& __t, size_type __pos, size_type __n, |
644 | const _Alloc& __a = _Alloc()) |
645 | : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |
646 | |
647 | /** |
648 | * @brief Construct string from a string_view. |
649 | * @param __t Source object convertible to string view. |
650 | * @param __a Allocator to use (default is default allocator). |
651 | */ |
652 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
653 | explicit |
654 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) |
655 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } |
656 | #endif // C++17 |
657 | |
658 | /** |
659 | * @brief Destroy the string instance. |
660 | */ |
661 | ~basic_string() |
662 | { _M_dispose(); } |
663 | |
664 | /** |
665 | * @brief Assign the value of @a str to this string. |
666 | * @param __str Source string. |
667 | */ |
668 | basic_string& |
669 | operator=(const basic_string& __str) |
670 | { |
671 | #if __cplusplus >= 201103L |
672 | if (_Alloc_traits::_S_propagate_on_copy_assign()) |
673 | { |
674 | if (!_Alloc_traits::_S_always_equal() && !_M_is_local() |
675 | && _M_get_allocator() != __str._M_get_allocator()) |
676 | { |
677 | // Propagating allocator cannot free existing storage so must |
678 | // deallocate it before replacing current allocator. |
679 | if (__str.size() <= _S_local_capacity) |
680 | { |
681 | _M_destroy(_M_allocated_capacity); |
682 | _M_data(_M_local_data()); |
683 | _M_set_length(0); |
684 | } |
685 | else |
686 | { |
687 | const auto __len = __str.size(); |
688 | auto __alloc = __str._M_get_allocator(); |
689 | // If this allocation throws there are no effects: |
690 | auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1); |
691 | _M_destroy(_M_allocated_capacity); |
692 | _M_data(__ptr); |
693 | _M_capacity(__len); |
694 | _M_set_length(__len); |
695 | } |
696 | } |
697 | std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator()); |
698 | } |
699 | #endif |
700 | return this->assign(__str); |
701 | } |
702 | |
703 | /** |
704 | * @brief Copy contents of @a s into this string. |
705 | * @param __s Source null-terminated string. |
706 | */ |
707 | basic_string& |
708 | operator=(const _CharT* __s) |
709 | { return this->assign(__s); } |
710 | |
711 | /** |
712 | * @brief Set value to string of length 1. |
713 | * @param __c Source character. |
714 | * |
715 | * Assigning to a character makes this string length 1 and |
716 | * (*this)[0] == @a c. |
717 | */ |
718 | basic_string& |
719 | operator=(_CharT __c) |
720 | { |
721 | this->assign(1, __c); |
722 | return *this; |
723 | } |
724 | |
725 | #if __cplusplus >= 201103L |
726 | /** |
727 | * @brief Move assign the value of @a str to this string. |
728 | * @param __str Source string. |
729 | * |
730 | * The contents of @a str are moved into this string (without copying). |
731 | * @a str is a valid, but unspecified string. |
732 | **/ |
733 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
734 | // 2063. Contradictory requirements for string move assignment |
735 | basic_string& |
736 | operator=(basic_string&& __str) |
737 | noexcept(_Alloc_traits::_S_nothrow_move()) |
738 | { |
739 | if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign() |
740 | && !_Alloc_traits::_S_always_equal() |
741 | && _M_get_allocator() != __str._M_get_allocator()) |
742 | { |
743 | // Destroy existing storage before replacing allocator. |
744 | _M_destroy(_M_allocated_capacity); |
745 | _M_data(_M_local_data()); |
746 | _M_set_length(0); |
747 | } |
748 | // Replace allocator if POCMA is true. |
749 | std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator()); |
750 | |
751 | if (__str._M_is_local()) |
752 | { |
753 | // We've always got room for a short string, just copy it. |
754 | if (__str.size()) |
755 | this->_S_copy(_M_data(), __str._M_data(), __str.size()); |
756 | _M_set_length(__str.size()); |
757 | } |
758 | else if (_Alloc_traits::_S_propagate_on_move_assign() |
759 | || _Alloc_traits::_S_always_equal() |
760 | || _M_get_allocator() == __str._M_get_allocator()) |
761 | { |
762 | // Just move the allocated pointer, our allocator can free it. |
763 | pointer __data = nullptr; |
764 | size_type __capacity; |
765 | if (!_M_is_local()) |
766 | { |
767 | if (_Alloc_traits::_S_always_equal()) |
768 | { |
769 | // __str can reuse our existing storage. |
770 | __data = _M_data(); |
771 | __capacity = _M_allocated_capacity; |
772 | } |
773 | else // __str can't use it, so free it. |
774 | _M_destroy(_M_allocated_capacity); |
775 | } |
776 | |
777 | _M_data(__str._M_data()); |
778 | _M_length(__str.length()); |
779 | _M_capacity(__str._M_allocated_capacity); |
780 | if (__data) |
781 | { |
782 | __str._M_data(__data); |
783 | __str._M_capacity(__capacity); |
784 | } |
785 | else |
786 | __str._M_data(__str._M_local_buf); |
787 | } |
788 | else // Need to do a deep copy |
789 | assign(__str); |
790 | __str.clear(); |
791 | return *this; |
792 | } |
793 | |
794 | /** |
795 | * @brief Set value to string constructed from initializer %list. |
796 | * @param __l std::initializer_list. |
797 | */ |
798 | basic_string& |
799 | operator=(initializer_list<_CharT> __l) |
800 | { |
801 | this->assign(__l.begin(), __l.size()); |
802 | return *this; |
803 | } |
804 | #endif // C++11 |
805 | |
806 | #if __cplusplus >= 201703L |
807 | /** |
808 | * @brief Set value to string constructed from a string_view. |
809 | * @param __svt An object convertible to string_view. |
810 | */ |
811 | template<typename _Tp> |
812 | _If_sv<_Tp, basic_string&> |
813 | operator=(const _Tp& __svt) |
814 | { return this->assign(__svt); } |
815 | |
816 | /** |
817 | * @brief Convert to a string_view. |
818 | * @return A string_view. |
819 | */ |
820 | operator __sv_type() const noexcept |
821 | { return __sv_type(data(), size()); } |
822 | #endif // C++17 |
823 | |
824 | // Iterators: |
825 | /** |
826 | * Returns a read/write iterator that points to the first character in |
827 | * the %string. |
828 | */ |
829 | iterator |
830 | begin() _GLIBCXX_NOEXCEPT |
831 | { return iterator(_M_data()); } |
832 | |
833 | /** |
834 | * Returns a read-only (constant) iterator that points to the first |
835 | * character in the %string. |
836 | */ |
837 | const_iterator |
838 | begin() const _GLIBCXX_NOEXCEPT |
839 | { return const_iterator(_M_data()); } |
840 | |
841 | /** |
842 | * Returns a read/write iterator that points one past the last |
843 | * character in the %string. |
844 | */ |
845 | iterator |
846 | end() _GLIBCXX_NOEXCEPT |
847 | { return iterator(_M_data() + this->size()); } |
848 | |
849 | /** |
850 | * Returns a read-only (constant) iterator that points one past the |
851 | * last character in the %string. |
852 | */ |
853 | const_iterator |
854 | end() const _GLIBCXX_NOEXCEPT |
855 | { return const_iterator(_M_data() + this->size()); } |
856 | |
857 | /** |
858 | * Returns a read/write reverse iterator that points to the last |
859 | * character in the %string. Iteration is done in reverse element |
860 | * order. |
861 | */ |
862 | reverse_iterator |
863 | rbegin() _GLIBCXX_NOEXCEPT |
864 | { return reverse_iterator(this->end()); } |
865 | |
866 | /** |
867 | * Returns a read-only (constant) reverse iterator that points |
868 | * to the last character in the %string. Iteration is done in |
869 | * reverse element order. |
870 | */ |
871 | const_reverse_iterator |
872 | rbegin() const _GLIBCXX_NOEXCEPT |
873 | { return const_reverse_iterator(this->end()); } |
874 | |
875 | /** |
876 | * Returns a read/write reverse iterator that points to one before the |
877 | * first character in the %string. Iteration is done in reverse |
878 | * element order. |
879 | */ |
880 | reverse_iterator |
881 | rend() _GLIBCXX_NOEXCEPT |
882 | { return reverse_iterator(this->begin()); } |
883 | |
884 | /** |
885 | * Returns a read-only (constant) reverse iterator that points |
886 | * to one before the first character in the %string. Iteration |
887 | * is done in reverse element order. |
888 | */ |
889 | const_reverse_iterator |
890 | rend() const _GLIBCXX_NOEXCEPT |
891 | { return const_reverse_iterator(this->begin()); } |
892 | |
893 | #if __cplusplus >= 201103L |
894 | /** |
895 | * Returns a read-only (constant) iterator that points to the first |
896 | * character in the %string. |
897 | */ |
898 | const_iterator |
899 | cbegin() const noexcept |
900 | { return const_iterator(this->_M_data()); } |
901 | |
902 | /** |
903 | * Returns a read-only (constant) iterator that points one past the |
904 | * last character in the %string. |
905 | */ |
906 | const_iterator |
907 | cend() const noexcept |
908 | { return const_iterator(this->_M_data() + this->size()); } |
909 | |
910 | /** |
911 | * Returns a read-only (constant) reverse iterator that points |
912 | * to the last character in the %string. Iteration is done in |
913 | * reverse element order. |
914 | */ |
915 | const_reverse_iterator |
916 | crbegin() const noexcept |
917 | { return const_reverse_iterator(this->end()); } |
918 | |
919 | /** |
920 | * Returns a read-only (constant) reverse iterator that points |
921 | * to one before the first character in the %string. Iteration |
922 | * is done in reverse element order. |
923 | */ |
924 | const_reverse_iterator |
925 | crend() const noexcept |
926 | { return const_reverse_iterator(this->begin()); } |
927 | #endif |
928 | |
929 | public: |
930 | // Capacity: |
931 | /// Returns the number of characters in the string, not including any |
932 | /// null-termination. |
933 | size_type |
934 | size() const _GLIBCXX_NOEXCEPT |
935 | { return _M_string_length; } |
936 | |
937 | /// Returns the number of characters in the string, not including any |
938 | /// null-termination. |
939 | size_type |
940 | length() const _GLIBCXX_NOEXCEPT |
941 | { return _M_string_length; } |
942 | |
943 | /// Returns the size() of the largest possible %string. |
944 | size_type |
945 | max_size() const _GLIBCXX_NOEXCEPT |
946 | { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; } |
947 | |
948 | /** |
949 | * @brief Resizes the %string to the specified number of characters. |
950 | * @param __n Number of characters the %string should contain. |
951 | * @param __c Character to fill any new elements. |
952 | * |
953 | * This function will %resize the %string to the specified |
954 | * number of characters. If the number is smaller than the |
955 | * %string's current size the %string is truncated, otherwise |
956 | * the %string is extended and new elements are %set to @a __c. |
957 | */ |
958 | void |
959 | resize(size_type __n, _CharT __c); |
960 | |
961 | /** |
962 | * @brief Resizes the %string to the specified number of characters. |
963 | * @param __n Number of characters the %string should contain. |
964 | * |
965 | * This function will resize the %string to the specified length. If |
966 | * the new size is smaller than the %string's current size the %string |
967 | * is truncated, otherwise the %string is extended and new characters |
968 | * are default-constructed. For basic types such as char, this means |
969 | * setting them to 0. |
970 | */ |
971 | void |
972 | resize(size_type __n) |
973 | { this->resize(__n, _CharT()); } |
974 | |
975 | #if __cplusplus >= 201103L |
976 | /// A non-binding request to reduce capacity() to size(). |
977 | void |
978 | shrink_to_fit() noexcept |
979 | { |
980 | #if __cpp_exceptions |
981 | if (capacity() > size()) |
982 | { |
983 | try |
984 | { reserve(0); } |
985 | catch(...) |
986 | { } |
987 | } |
988 | #endif |
989 | } |
990 | #endif |
991 | |
992 | /** |
993 | * Returns the total number of characters that the %string can hold |
994 | * before needing to allocate more memory. |
995 | */ |
996 | size_type |
997 | capacity() const _GLIBCXX_NOEXCEPT |
998 | { |
999 | return _M_is_local() ? size_type(_S_local_capacity) |
1000 | : _M_allocated_capacity; |
1001 | } |
1002 | |
1003 | /** |
1004 | * @brief Attempt to preallocate enough memory for specified number of |
1005 | * characters. |
1006 | * @param __res_arg Number of characters required. |
1007 | * @throw std::length_error If @a __res_arg exceeds @c max_size(). |
1008 | * |
1009 | * This function attempts to reserve enough memory for the |
1010 | * %string to hold the specified number of characters. If the |
1011 | * number requested is more than max_size(), length_error is |
1012 | * thrown. |
1013 | * |
1014 | * The advantage of this function is that if optimal code is a |
1015 | * necessity and the user can determine the string length that will be |
1016 | * required, the user can reserve the memory in %advance, and thus |
1017 | * prevent a possible reallocation of memory and copying of %string |
1018 | * data. |
1019 | */ |
1020 | void |
1021 | reserve(size_type __res_arg = 0); |
1022 | |
1023 | /** |
1024 | * Erases the string, making it empty. |
1025 | */ |
1026 | void |
1027 | clear() _GLIBCXX_NOEXCEPT |
1028 | { _M_set_length(0); } |
1029 | |
1030 | /** |
1031 | * Returns true if the %string is empty. Equivalent to |
1032 | * <code>*this == ""</code>. |
1033 | */ |
1034 | _GLIBCXX_NODISCARD bool |
1035 | empty() const _GLIBCXX_NOEXCEPT |
1036 | { return this->size() == 0; } |
1037 | |
1038 | // Element access: |
1039 | /** |
1040 | * @brief Subscript access to the data contained in the %string. |
1041 | * @param __pos The index of the character to access. |
1042 | * @return Read-only (constant) reference to the character. |
1043 | * |
1044 | * This operator allows for easy, array-style, data access. |
1045 | * Note that data access with this operator is unchecked and |
1046 | * out_of_range lookups are not defined. (For checked lookups |
1047 | * see at().) |
1048 | */ |
1049 | const_reference |
1050 | operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT |
1051 | { |
1052 | __glibcxx_assert(__pos <= size()); |
1053 | return _M_data()[__pos]; |
1054 | } |
1055 | |
1056 | /** |
1057 | * @brief Subscript access to the data contained in the %string. |
1058 | * @param __pos The index of the character to access. |
1059 | * @return Read/write reference to the character. |
1060 | * |
1061 | * This operator allows for easy, array-style, data access. |
1062 | * Note that data access with this operator is unchecked and |
1063 | * out_of_range lookups are not defined. (For checked lookups |
1064 | * see at().) |
1065 | */ |
1066 | reference |
1067 | operator[](size_type __pos) |
1068 | { |
1069 | // Allow pos == size() both in C++98 mode, as v3 extension, |
1070 | // and in C++11 mode. |
1071 | __glibcxx_assert(__pos <= size()); |
1072 | // In pedantic mode be strict in C++98 mode. |
1073 | _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); |
1074 | return _M_data()[__pos]; |
1075 | } |
1076 | |
1077 | /** |
1078 | * @brief Provides access to the data contained in the %string. |
1079 | * @param __n The index of the character to access. |
1080 | * @return Read-only (const) reference to the character. |
1081 | * @throw std::out_of_range If @a n is an invalid index. |
1082 | * |
1083 | * This function provides for safer data access. The parameter is |
1084 | * first checked that it is in the range of the string. The function |
1085 | * throws out_of_range if the check fails. |
1086 | */ |
1087 | const_reference |
1088 | at(size_type __n) const |
1089 | { |
1090 | if (__n >= this->size()) |
1091 | __throw_out_of_range_fmt(__N("basic_string::at: __n " |
1092 | "(which is %zu) >= this->size() " |
1093 | "(which is %zu)" ), |
1094 | __n, this->size()); |
1095 | return _M_data()[__n]; |
1096 | } |
1097 | |
1098 | /** |
1099 | * @brief Provides access to the data contained in the %string. |
1100 | * @param __n The index of the character to access. |
1101 | * @return Read/write reference to the character. |
1102 | * @throw std::out_of_range If @a n is an invalid index. |
1103 | * |
1104 | * This function provides for safer data access. The parameter is |
1105 | * first checked that it is in the range of the string. The function |
1106 | * throws out_of_range if the check fails. |
1107 | */ |
1108 | reference |
1109 | at(size_type __n) |
1110 | { |
1111 | if (__n >= size()) |
1112 | __throw_out_of_range_fmt(__N("basic_string::at: __n " |
1113 | "(which is %zu) >= this->size() " |
1114 | "(which is %zu)" ), |
1115 | __n, this->size()); |
1116 | return _M_data()[__n]; |
1117 | } |
1118 | |
1119 | #if __cplusplus >= 201103L |
1120 | /** |
1121 | * Returns a read/write reference to the data at the first |
1122 | * element of the %string. |
1123 | */ |
1124 | reference |
1125 | front() noexcept |
1126 | { |
1127 | __glibcxx_assert(!empty()); |
1128 | return operator[](0); |
1129 | } |
1130 | |
1131 | /** |
1132 | * Returns a read-only (constant) reference to the data at the first |
1133 | * element of the %string. |
1134 | */ |
1135 | const_reference |
1136 | front() const noexcept |
1137 | { |
1138 | __glibcxx_assert(!empty()); |
1139 | return operator[](0); |
1140 | } |
1141 | |
1142 | /** |
1143 | * Returns a read/write reference to the data at the last |
1144 | * element of the %string. |
1145 | */ |
1146 | reference |
1147 | back() noexcept |
1148 | { |
1149 | __glibcxx_assert(!empty()); |
1150 | return operator[](this->size() - 1); |
1151 | } |
1152 | |
1153 | /** |
1154 | * Returns a read-only (constant) reference to the data at the |
1155 | * last element of the %string. |
1156 | */ |
1157 | const_reference |
1158 | back() const noexcept |
1159 | { |
1160 | __glibcxx_assert(!empty()); |
1161 | return operator[](this->size() - 1); |
1162 | } |
1163 | #endif |
1164 | |
1165 | // Modifiers: |
1166 | /** |
1167 | * @brief Append a string to this string. |
1168 | * @param __str The string to append. |
1169 | * @return Reference to this string. |
1170 | */ |
1171 | basic_string& |
1172 | operator+=(const basic_string& __str) |
1173 | { return this->append(__str); } |
1174 | |
1175 | /** |
1176 | * @brief Append a C string. |
1177 | * @param __s The C string to append. |
1178 | * @return Reference to this string. |
1179 | */ |
1180 | basic_string& |
1181 | operator+=(const _CharT* __s) |
1182 | { return this->append(__s); } |
1183 | |
1184 | /** |
1185 | * @brief Append a character. |
1186 | * @param __c The character to append. |
1187 | * @return Reference to this string. |
1188 | */ |
1189 | basic_string& |
1190 | operator+=(_CharT __c) |
1191 | { |
1192 | this->push_back(__c); |
1193 | return *this; |
1194 | } |
1195 | |
1196 | #if __cplusplus >= 201103L |
1197 | /** |
1198 | * @brief Append an initializer_list of characters. |
1199 | * @param __l The initializer_list of characters to be appended. |
1200 | * @return Reference to this string. |
1201 | */ |
1202 | basic_string& |
1203 | operator+=(initializer_list<_CharT> __l) |
1204 | { return this->append(__l.begin(), __l.size()); } |
1205 | #endif // C++11 |
1206 | |
1207 | #if __cplusplus >= 201703L |
1208 | /** |
1209 | * @brief Append a string_view. |
1210 | * @param __svt An object convertible to string_view to be appended. |
1211 | * @return Reference to this string. |
1212 | */ |
1213 | template<typename _Tp> |
1214 | _If_sv<_Tp, basic_string&> |
1215 | operator+=(const _Tp& __svt) |
1216 | { return this->append(__svt); } |
1217 | #endif // C++17 |
1218 | |
1219 | /** |
1220 | * @brief Append a string to this string. |
1221 | * @param __str The string to append. |
1222 | * @return Reference to this string. |
1223 | */ |
1224 | basic_string& |
1225 | append(const basic_string& __str) |
1226 | { return _M_append(__str._M_data(), __str.size()); } |
1227 | |
1228 | /** |
1229 | * @brief Append a substring. |
1230 | * @param __str The string to append. |
1231 | * @param __pos Index of the first character of str to append. |
1232 | * @param __n The number of characters to append. |
1233 | * @return Reference to this string. |
1234 | * @throw std::out_of_range if @a __pos is not a valid index. |
1235 | * |
1236 | * This function appends @a __n characters from @a __str |
1237 | * starting at @a __pos to this string. If @a __n is is larger |
1238 | * than the number of available characters in @a __str, the |
1239 | * remainder of @a __str is appended. |
1240 | */ |
1241 | basic_string& |
1242 | append(const basic_string& __str, size_type __pos, size_type __n = npos) |
1243 | { return _M_append(__str._M_data() |
1244 | + __str._M_check(__pos, "basic_string::append" ), |
1245 | __str._M_limit(__pos, __n)); } |
1246 | |
1247 | /** |
1248 | * @brief Append a C substring. |
1249 | * @param __s The C string to append. |
1250 | * @param __n The number of characters to append. |
1251 | * @return Reference to this string. |
1252 | */ |
1253 | basic_string& |
1254 | append(const _CharT* __s, size_type __n) |
1255 | { |
1256 | __glibcxx_requires_string_len(__s, __n); |
1257 | _M_check_length(size_type(0), __n, "basic_string::append" ); |
1258 | return _M_append(__s, __n); |
1259 | } |
1260 | |
1261 | /** |
1262 | * @brief Append a C string. |
1263 | * @param __s The C string to append. |
1264 | * @return Reference to this string. |
1265 | */ |
1266 | basic_string& |
1267 | append(const _CharT* __s) |
1268 | { |
1269 | __glibcxx_requires_string(__s); |
1270 | const size_type __n = traits_type::length(__s); |
1271 | _M_check_length(size_type(0), __n, "basic_string::append" ); |
1272 | return _M_append(__s, __n); |
1273 | } |
1274 | |
1275 | /** |
1276 | * @brief Append multiple characters. |
1277 | * @param __n The number of characters to append. |
1278 | * @param __c The character to use. |
1279 | * @return Reference to this string. |
1280 | * |
1281 | * Appends __n copies of __c to this string. |
1282 | */ |
1283 | basic_string& |
1284 | append(size_type __n, _CharT __c) |
1285 | { return _M_replace_aux(this->size(), size_type(0), __n, __c); } |
1286 | |
1287 | #if __cplusplus >= 201103L |
1288 | /** |
1289 | * @brief Append an initializer_list of characters. |
1290 | * @param __l The initializer_list of characters to append. |
1291 | * @return Reference to this string. |
1292 | */ |
1293 | basic_string& |
1294 | append(initializer_list<_CharT> __l) |
1295 | { return this->append(__l.begin(), __l.size()); } |
1296 | #endif // C++11 |
1297 | |
1298 | /** |
1299 | * @brief Append a range of characters. |
1300 | * @param __first Iterator referencing the first character to append. |
1301 | * @param __last Iterator marking the end of the range. |
1302 | * @return Reference to this string. |
1303 | * |
1304 | * Appends characters in the range [__first,__last) to this string. |
1305 | */ |
1306 | #if __cplusplus >= 201103L |
1307 | template<class _InputIterator, |
1308 | typename = std::_RequireInputIter<_InputIterator>> |
1309 | #else |
1310 | template<class _InputIterator> |
1311 | #endif |
1312 | basic_string& |
1313 | append(_InputIterator __first, _InputIterator __last) |
1314 | { return this->replace(end(), end(), __first, __last); } |
1315 | |
1316 | #if __cplusplus >= 201703L |
1317 | /** |
1318 | * @brief Append a string_view. |
1319 | * @param __svt An object convertible to string_view to be appended. |
1320 | * @return Reference to this string. |
1321 | */ |
1322 | template<typename _Tp> |
1323 | _If_sv<_Tp, basic_string&> |
1324 | append(const _Tp& __svt) |
1325 | { |
1326 | __sv_type __sv = __svt; |
1327 | return this->append(__sv.data(), __sv.size()); |
1328 | } |
1329 | |
1330 | /** |
1331 | * @brief Append a range of characters from a string_view. |
1332 | * @param __svt An object convertible to string_view to be appended from. |
1333 | * @param __pos The position in the string_view to append from. |
1334 | * @param __n The number of characters to append from the string_view. |
1335 | * @return Reference to this string. |
1336 | */ |
1337 | template<typename _Tp> |
1338 | _If_sv<_Tp, basic_string&> |
1339 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) |
1340 | { |
1341 | __sv_type __sv = __svt; |
1342 | return _M_append(__sv.data() |
1343 | + std::__sv_check(__sv.size(), __pos, "basic_string::append" ), |
1344 | std::__sv_limit(__sv.size(), __pos, __n)); |
1345 | } |
1346 | #endif // C++17 |
1347 | |
1348 | /** |
1349 | * @brief Append a single character. |
1350 | * @param __c Character to append. |
1351 | */ |
1352 | void |
1353 | push_back(_CharT __c) |
1354 | { |
1355 | const size_type __size = this->size(); |
1356 | if (__size + 1 > this->capacity()) |
1357 | this->_M_mutate(__size, size_type(0), 0, size_type(1)); |
1358 | traits_type::assign(this->_M_data()[__size], __c); |
1359 | this->_M_set_length(__size + 1); |
1360 | } |
1361 | |
1362 | /** |
1363 | * @brief Set value to contents of another string. |
1364 | * @param __str Source string to use. |
1365 | * @return Reference to this string. |
1366 | */ |
1367 | basic_string& |
1368 | assign(const basic_string& __str) |
1369 | { |
1370 | this->_M_assign(__str); |
1371 | return *this; |
1372 | } |
1373 | |
1374 | #if __cplusplus >= 201103L |
1375 | /** |
1376 | * @brief Set value to contents of another string. |
1377 | * @param __str Source string to use. |
1378 | * @return Reference to this string. |
1379 | * |
1380 | * This function sets this string to the exact contents of @a __str. |
1381 | * @a __str is a valid, but unspecified string. |
1382 | */ |
1383 | basic_string& |
1384 | assign(basic_string&& __str) |
1385 | noexcept(_Alloc_traits::_S_nothrow_move()) |
1386 | { |
1387 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1388 | // 2063. Contradictory requirements for string move assignment |
1389 | return *this = std::move(__str); |
1390 | } |
1391 | #endif // C++11 |
1392 | |
1393 | /** |
1394 | * @brief Set value to a substring of a string. |
1395 | * @param __str The string to use. |
1396 | * @param __pos Index of the first character of str. |
1397 | * @param __n Number of characters to use. |
1398 | * @return Reference to this string. |
1399 | * @throw std::out_of_range if @a pos is not a valid index. |
1400 | * |
1401 | * This function sets this string to the substring of @a __str |
1402 | * consisting of @a __n characters at @a __pos. If @a __n is |
1403 | * is larger than the number of available characters in @a |
1404 | * __str, the remainder of @a __str is used. |
1405 | */ |
1406 | basic_string& |
1407 | assign(const basic_string& __str, size_type __pos, size_type __n = npos) |
1408 | { return _M_replace(size_type(0), this->size(), __str._M_data() |
1409 | + __str._M_check(__pos, "basic_string::assign" ), |
1410 | __str._M_limit(__pos, __n)); } |
1411 | |
1412 | /** |
1413 | * @brief Set value to a C substring. |
1414 | * @param __s The C string to use. |
1415 | * @param __n Number of characters to use. |
1416 | * @return Reference to this string. |
1417 | * |
1418 | * This function sets the value of this string to the first @a __n |
1419 | * characters of @a __s. If @a __n is is larger than the number of |
1420 | * available characters in @a __s, the remainder of @a __s is used. |
1421 | */ |
1422 | basic_string& |
1423 | assign(const _CharT* __s, size_type __n) |
1424 | { |
1425 | __glibcxx_requires_string_len(__s, __n); |
1426 | return _M_replace(size_type(0), this->size(), __s, __n); |
1427 | } |
1428 | |
1429 | /** |
1430 | * @brief Set value to contents of a C string. |
1431 | * @param __s The C string to use. |
1432 | * @return Reference to this string. |
1433 | * |
1434 | * This function sets the value of this string to the value of @a __s. |
1435 | * The data is copied, so there is no dependence on @a __s once the |
1436 | * function returns. |
1437 | */ |
1438 | basic_string& |
1439 | assign(const _CharT* __s) |
1440 | { |
1441 | __glibcxx_requires_string(__s); |
1442 | return _M_replace(size_type(0), this->size(), __s, |
1443 | traits_type::length(__s)); |
1444 | } |
1445 | |
1446 | /** |
1447 | * @brief Set value to multiple characters. |
1448 | * @param __n Length of the resulting string. |
1449 | * @param __c The character to use. |
1450 | * @return Reference to this string. |
1451 | * |
1452 | * This function sets the value of this string to @a __n copies of |
1453 | * character @a __c. |
1454 | */ |
1455 | basic_string& |
1456 | assign(size_type __n, _CharT __c) |
1457 | { return _M_replace_aux(size_type(0), this->size(), __n, __c); } |
1458 | |
1459 | /** |
1460 | * @brief Set value to a range of characters. |
1461 | * @param __first Iterator referencing the first character to append. |
1462 | * @param __last Iterator marking the end of the range. |
1463 | * @return Reference to this string. |
1464 | * |
1465 | * Sets value of string to characters in the range [__first,__last). |
1466 | */ |
1467 | #if __cplusplus >= 201103L |
1468 | template<class _InputIterator, |
1469 | typename = std::_RequireInputIter<_InputIterator>> |
1470 | #else |
1471 | template<class _InputIterator> |
1472 | #endif |
1473 | basic_string& |
1474 | assign(_InputIterator __first, _InputIterator __last) |
1475 | { return this->replace(begin(), end(), __first, __last); } |
1476 | |
1477 | #if __cplusplus >= 201103L |
1478 | /** |
1479 | * @brief Set value to an initializer_list of characters. |
1480 | * @param __l The initializer_list of characters to assign. |
1481 | * @return Reference to this string. |
1482 | */ |
1483 | basic_string& |
1484 | assign(initializer_list<_CharT> __l) |
1485 | { return this->assign(__l.begin(), __l.size()); } |
1486 | #endif // C++11 |
1487 | |
1488 | #if __cplusplus >= 201703L |
1489 | /** |
1490 | * @brief Set value from a string_view. |
1491 | * @param __svt The source object convertible to string_view. |
1492 | * @return Reference to this string. |
1493 | */ |
1494 | template<typename _Tp> |
1495 | _If_sv<_Tp, basic_string&> |
1496 | assign(const _Tp& __svt) |
1497 | { |
1498 | __sv_type __sv = __svt; |
1499 | return this->assign(__sv.data(), __sv.size()); |
1500 | } |
1501 | |
1502 | /** |
1503 | * @brief Set value from a range of characters in a string_view. |
1504 | * @param __svt The source object convertible to string_view. |
1505 | * @param __pos The position in the string_view to assign from. |
1506 | * @param __n The number of characters to assign. |
1507 | * @return Reference to this string. |
1508 | */ |
1509 | template<typename _Tp> |
1510 | _If_sv<_Tp, basic_string&> |
1511 | assign(const _Tp& __svt, size_type __pos, size_type __n = npos) |
1512 | { |
1513 | __sv_type __sv = __svt; |
1514 | return _M_replace(size_type(0), this->size(), |
1515 | __sv.data() |
1516 | + std::__sv_check(__sv.size(), __pos, "basic_string::assign" ), |
1517 | std::__sv_limit(__sv.size(), __pos, __n)); |
1518 | } |
1519 | #endif // C++17 |
1520 | |
1521 | #if __cplusplus >= 201103L |
1522 | /** |
1523 | * @brief Insert multiple characters. |
1524 | * @param __p Const_iterator referencing location in string to |
1525 | * insert at. |
1526 | * @param __n Number of characters to insert |
1527 | * @param __c The character to insert. |
1528 | * @return Iterator referencing the first inserted char. |
1529 | * @throw std::length_error If new length exceeds @c max_size(). |
1530 | * |
1531 | * Inserts @a __n copies of character @a __c starting at the |
1532 | * position referenced by iterator @a __p. If adding |
1533 | * characters causes the length to exceed max_size(), |
1534 | * length_error is thrown. The value of the string doesn't |
1535 | * change if an error is thrown. |
1536 | */ |
1537 | iterator |
1538 | insert(const_iterator __p, size_type __n, _CharT __c) |
1539 | { |
1540 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1541 | const size_type __pos = __p - begin(); |
1542 | this->replace(__p, __p, __n, __c); |
1543 | return iterator(this->_M_data() + __pos); |
1544 | } |
1545 | #else |
1546 | /** |
1547 | * @brief Insert multiple characters. |
1548 | * @param __p Iterator referencing location in string to insert at. |
1549 | * @param __n Number of characters to insert |
1550 | * @param __c The character to insert. |
1551 | * @throw std::length_error If new length exceeds @c max_size(). |
1552 | * |
1553 | * Inserts @a __n copies of character @a __c starting at the |
1554 | * position referenced by iterator @a __p. If adding |
1555 | * characters causes the length to exceed max_size(), |
1556 | * length_error is thrown. The value of the string doesn't |
1557 | * change if an error is thrown. |
1558 | */ |
1559 | void |
1560 | insert(iterator __p, size_type __n, _CharT __c) |
1561 | { this->replace(__p, __p, __n, __c); } |
1562 | #endif |
1563 | |
1564 | #if __cplusplus >= 201103L |
1565 | /** |
1566 | * @brief Insert a range of characters. |
1567 | * @param __p Const_iterator referencing location in string to |
1568 | * insert at. |
1569 | * @param __beg Start of range. |
1570 | * @param __end End of range. |
1571 | * @return Iterator referencing the first inserted char. |
1572 | * @throw std::length_error If new length exceeds @c max_size(). |
1573 | * |
1574 | * Inserts characters in range [beg,end). If adding characters |
1575 | * causes the length to exceed max_size(), length_error is |
1576 | * thrown. The value of the string doesn't change if an error |
1577 | * is thrown. |
1578 | */ |
1579 | template<class _InputIterator, |
1580 | typename = std::_RequireInputIter<_InputIterator>> |
1581 | iterator |
1582 | insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) |
1583 | { |
1584 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1585 | const size_type __pos = __p - begin(); |
1586 | this->replace(__p, __p, __beg, __end); |
1587 | return iterator(this->_M_data() + __pos); |
1588 | } |
1589 | #else |
1590 | /** |
1591 | * @brief Insert a range of characters. |
1592 | * @param __p Iterator referencing location in string to insert at. |
1593 | * @param __beg Start of range. |
1594 | * @param __end End of range. |
1595 | * @throw std::length_error If new length exceeds @c max_size(). |
1596 | * |
1597 | * Inserts characters in range [__beg,__end). If adding |
1598 | * characters causes the length to exceed max_size(), |
1599 | * length_error is thrown. The value of the string doesn't |
1600 | * change if an error is thrown. |
1601 | */ |
1602 | template<class _InputIterator> |
1603 | void |
1604 | insert(iterator __p, _InputIterator __beg, _InputIterator __end) |
1605 | { this->replace(__p, __p, __beg, __end); } |
1606 | #endif |
1607 | |
1608 | #if __cplusplus >= 201103L |
1609 | /** |
1610 | * @brief Insert an initializer_list of characters. |
1611 | * @param __p Iterator referencing location in string to insert at. |
1612 | * @param __l The initializer_list of characters to insert. |
1613 | * @throw std::length_error If new length exceeds @c max_size(). |
1614 | */ |
1615 | iterator |
1616 | insert(const_iterator __p, initializer_list<_CharT> __l) |
1617 | { return this->insert(__p, __l.begin(), __l.end()); } |
1618 | |
1619 | #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
1620 | // See PR libstdc++/83328 |
1621 | void |
1622 | insert(iterator __p, initializer_list<_CharT> __l) |
1623 | { |
1624 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1625 | this->insert(__p - begin(), __l.begin(), __l.size()); |
1626 | } |
1627 | #endif |
1628 | #endif // C++11 |
1629 | |
1630 | /** |
1631 | * @brief Insert value of a string. |
1632 | * @param __pos1 Iterator referencing location in string to insert at. |
1633 | * @param __str The string to insert. |
1634 | * @return Reference to this string. |
1635 | * @throw std::length_error If new length exceeds @c max_size(). |
1636 | * |
1637 | * Inserts value of @a __str starting at @a __pos1. If adding |
1638 | * characters causes the length to exceed max_size(), |
1639 | * length_error is thrown. The value of the string doesn't |
1640 | * change if an error is thrown. |
1641 | */ |
1642 | basic_string& |
1643 | insert(size_type __pos1, const basic_string& __str) |
1644 | { return this->replace(__pos1, size_type(0), |
1645 | __str._M_data(), __str.size()); } |
1646 | |
1647 | /** |
1648 | * @brief Insert a substring. |
1649 | * @param __pos1 Iterator referencing location in string to insert at. |
1650 | * @param __str The string to insert. |
1651 | * @param __pos2 Start of characters in str to insert. |
1652 | * @param __n Number of characters to insert. |
1653 | * @return Reference to this string. |
1654 | * @throw std::length_error If new length exceeds @c max_size(). |
1655 | * @throw std::out_of_range If @a pos1 > size() or |
1656 | * @a __pos2 > @a str.size(). |
1657 | * |
1658 | * Starting at @a pos1, insert @a __n character of @a __str |
1659 | * beginning with @a __pos2. If adding characters causes the |
1660 | * length to exceed max_size(), length_error is thrown. If @a |
1661 | * __pos1 is beyond the end of this string or @a __pos2 is |
1662 | * beyond the end of @a __str, out_of_range is thrown. The |
1663 | * value of the string doesn't change if an error is thrown. |
1664 | */ |
1665 | basic_string& |
1666 | insert(size_type __pos1, const basic_string& __str, |
1667 | size_type __pos2, size_type __n = npos) |
1668 | { return this->replace(__pos1, size_type(0), __str._M_data() |
1669 | + __str._M_check(__pos2, "basic_string::insert" ), |
1670 | __str._M_limit(__pos2, __n)); } |
1671 | |
1672 | /** |
1673 | * @brief Insert a C substring. |
1674 | * @param __pos Iterator referencing location in string to insert at. |
1675 | * @param __s The C string to insert. |
1676 | * @param __n The number of characters to insert. |
1677 | * @return Reference to this string. |
1678 | * @throw std::length_error If new length exceeds @c max_size(). |
1679 | * @throw std::out_of_range If @a __pos is beyond the end of this |
1680 | * string. |
1681 | * |
1682 | * Inserts the first @a __n characters of @a __s starting at @a |
1683 | * __pos. If adding characters causes the length to exceed |
1684 | * max_size(), length_error is thrown. If @a __pos is beyond |
1685 | * end(), out_of_range is thrown. The value of the string |
1686 | * doesn't change if an error is thrown. |
1687 | */ |
1688 | basic_string& |
1689 | insert(size_type __pos, const _CharT* __s, size_type __n) |
1690 | { return this->replace(__pos, size_type(0), __s, __n); } |
1691 | |
1692 | /** |
1693 | * @brief Insert a C string. |
1694 | * @param __pos Iterator referencing location in string to insert at. |
1695 | * @param __s The C string to insert. |
1696 | * @return Reference to this string. |
1697 | * @throw std::length_error If new length exceeds @c max_size(). |
1698 | * @throw std::out_of_range If @a pos is beyond the end of this |
1699 | * string. |
1700 | * |
1701 | * Inserts the first @a n characters of @a __s starting at @a __pos. If |
1702 | * adding characters causes the length to exceed max_size(), |
1703 | * length_error is thrown. If @a __pos is beyond end(), out_of_range is |
1704 | * thrown. The value of the string doesn't change if an error is |
1705 | * thrown. |
1706 | */ |
1707 | basic_string& |
1708 | insert(size_type __pos, const _CharT* __s) |
1709 | { |
1710 | __glibcxx_requires_string(__s); |
1711 | return this->replace(__pos, size_type(0), __s, |
1712 | traits_type::length(__s)); |
1713 | } |
1714 | |
1715 | /** |
1716 | * @brief Insert multiple characters. |
1717 | * @param __pos Index in string to insert at. |
1718 | * @param __n Number of characters to insert |
1719 | * @param __c The character to insert. |
1720 | * @return Reference to this string. |
1721 | * @throw std::length_error If new length exceeds @c max_size(). |
1722 | * @throw std::out_of_range If @a __pos is beyond the end of this |
1723 | * string. |
1724 | * |
1725 | * Inserts @a __n copies of character @a __c starting at index |
1726 | * @a __pos. If adding characters causes the length to exceed |
1727 | * max_size(), length_error is thrown. If @a __pos > length(), |
1728 | * out_of_range is thrown. The value of the string doesn't |
1729 | * change if an error is thrown. |
1730 | */ |
1731 | basic_string& |
1732 | insert(size_type __pos, size_type __n, _CharT __c) |
1733 | { return _M_replace_aux(_M_check(__pos, "basic_string::insert" ), |
1734 | size_type(0), __n, __c); } |
1735 | |
1736 | /** |
1737 | * @brief Insert one character. |
1738 | * @param __p Iterator referencing position in string to insert at. |
1739 | * @param __c The character to insert. |
1740 | * @return Iterator referencing newly inserted char. |
1741 | * @throw std::length_error If new length exceeds @c max_size(). |
1742 | * |
1743 | * Inserts character @a __c at position referenced by @a __p. |
1744 | * If adding character causes the length to exceed max_size(), |
1745 | * length_error is thrown. If @a __p is beyond end of string, |
1746 | * out_of_range is thrown. The value of the string doesn't |
1747 | * change if an error is thrown. |
1748 | */ |
1749 | iterator |
1750 | insert(__const_iterator __p, _CharT __c) |
1751 | { |
1752 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1753 | const size_type __pos = __p - begin(); |
1754 | _M_replace_aux(__pos, size_type(0), size_type(1), __c); |
1755 | return iterator(_M_data() + __pos); |
1756 | } |
1757 | |
1758 | #if __cplusplus >= 201703L |
1759 | /** |
1760 | * @brief Insert a string_view. |
1761 | * @param __pos Iterator referencing position in string to insert at. |
1762 | * @param __svt The object convertible to string_view to insert. |
1763 | * @return Reference to this string. |
1764 | */ |
1765 | template<typename _Tp> |
1766 | _If_sv<_Tp, basic_string&> |
1767 | insert(size_type __pos, const _Tp& __svt) |
1768 | { |
1769 | __sv_type __sv = __svt; |
1770 | return this->insert(__pos, __sv.data(), __sv.size()); |
1771 | } |
1772 | |
1773 | /** |
1774 | * @brief Insert a string_view. |
1775 | * @param __pos Iterator referencing position in string to insert at. |
1776 | * @param __svt The object convertible to string_view to insert from. |
1777 | * @param __pos Iterator referencing position in string_view to insert |
1778 | * from. |
1779 | * @param __n The number of characters to insert. |
1780 | * @return Reference to this string. |
1781 | */ |
1782 | template<typename _Tp> |
1783 | _If_sv<_Tp, basic_string&> |
1784 | insert(size_type __pos1, const _Tp& __svt, |
1785 | size_type __pos2, size_type __n = npos) |
1786 | { |
1787 | __sv_type __sv = __svt; |
1788 | return this->replace(__pos1, size_type(0), |
1789 | __sv.data() |
1790 | + std::__sv_check(__sv.size(), __pos2, "basic_string::insert" ), |
1791 | std::__sv_limit(__sv.size(), __pos2, __n)); |
1792 | } |
1793 | #endif // C++17 |
1794 | |
1795 | /** |
1796 | * @brief Remove characters. |
1797 | * @param __pos Index of first character to remove (default 0). |
1798 | * @param __n Number of characters to remove (default remainder). |
1799 | * @return Reference to this string. |
1800 | * @throw std::out_of_range If @a pos is beyond the end of this |
1801 | * string. |
1802 | * |
1803 | * Removes @a __n characters from this string starting at @a |
1804 | * __pos. The length of the string is reduced by @a __n. If |
1805 | * there are < @a __n characters to remove, the remainder of |
1806 | * the string is truncated. If @a __p is beyond end of string, |
1807 | * out_of_range is thrown. The value of the string doesn't |
1808 | * change if an error is thrown. |
1809 | */ |
1810 | basic_string& |
1811 | erase(size_type __pos = 0, size_type __n = npos) |
1812 | { |
1813 | _M_check(__pos, "basic_string::erase" ); |
1814 | if (__n == npos) |
1815 | this->_M_set_length(__pos); |
1816 | else if (__n != 0) |
1817 | this->_M_erase(__pos, _M_limit(__pos, __n)); |
1818 | return *this; |
1819 | } |
1820 | |
1821 | /** |
1822 | * @brief Remove one character. |
1823 | * @param __position Iterator referencing the character to remove. |
1824 | * @return iterator referencing same location after removal. |
1825 | * |
1826 | * Removes the character at @a __position from this string. The value |
1827 | * of the string doesn't change if an error is thrown. |
1828 | */ |
1829 | iterator |
1830 | erase(__const_iterator __position) |
1831 | { |
1832 | _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() |
1833 | && __position < end()); |
1834 | const size_type __pos = __position - begin(); |
1835 | this->_M_erase(__pos, size_type(1)); |
1836 | return iterator(_M_data() + __pos); |
1837 | } |
1838 | |
1839 | /** |
1840 | * @brief Remove a range of characters. |
1841 | * @param __first Iterator referencing the first character to remove. |
1842 | * @param __last Iterator referencing the end of the range. |
1843 | * @return Iterator referencing location of first after removal. |
1844 | * |
1845 | * Removes the characters in the range [first,last) from this string. |
1846 | * The value of the string doesn't change if an error is thrown. |
1847 | */ |
1848 | iterator |
1849 | erase(__const_iterator __first, __const_iterator __last) |
1850 | { |
1851 | _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last |
1852 | && __last <= end()); |
1853 | const size_type __pos = __first - begin(); |
1854 | if (__last == end()) |
1855 | this->_M_set_length(__pos); |
1856 | else |
1857 | this->_M_erase(__pos, __last - __first); |
1858 | return iterator(this->_M_data() + __pos); |
1859 | } |
1860 | |
1861 | #if __cplusplus >= 201103L |
1862 | /** |
1863 | * @brief Remove the last character. |
1864 | * |
1865 | * The string must be non-empty. |
1866 | */ |
1867 | void |
1868 | pop_back() noexcept |
1869 | { |
1870 | __glibcxx_assert(!empty()); |
1871 | _M_erase(size() - 1, 1); |
1872 | } |
1873 | #endif // C++11 |
1874 | |
1875 | /** |
1876 | * @brief Replace characters with value from another string. |
1877 | * @param __pos Index of first character to replace. |
1878 | * @param __n Number of characters to be replaced. |
1879 | * @param __str String to insert. |
1880 | * @return Reference to this string. |
1881 | * @throw std::out_of_range If @a pos is beyond the end of this |
1882 | * string. |
1883 | * @throw std::length_error If new length exceeds @c max_size(). |
1884 | * |
1885 | * Removes the characters in the range [__pos,__pos+__n) from |
1886 | * this string. In place, the value of @a __str is inserted. |
1887 | * If @a __pos is beyond end of string, out_of_range is thrown. |
1888 | * If the length of the result exceeds max_size(), length_error |
1889 | * is thrown. The value of the string doesn't change if an |
1890 | * error is thrown. |
1891 | */ |
1892 | basic_string& |
1893 | replace(size_type __pos, size_type __n, const basic_string& __str) |
1894 | { return this->replace(__pos, __n, __str._M_data(), __str.size()); } |
1895 | |
1896 | /** |
1897 | * @brief Replace characters with value from another string. |
1898 | * @param __pos1 Index of first character to replace. |
1899 | * @param __n1 Number of characters to be replaced. |
1900 | * @param __str String to insert. |
1901 | * @param __pos2 Index of first character of str to use. |
1902 | * @param __n2 Number of characters from str to use. |
1903 | * @return Reference to this string. |
1904 | * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > |
1905 | * __str.size(). |
1906 | * @throw std::length_error If new length exceeds @c max_size(). |
1907 | * |
1908 | * Removes the characters in the range [__pos1,__pos1 + n) from this |
1909 | * string. In place, the value of @a __str is inserted. If @a __pos is |
1910 | * beyond end of string, out_of_range is thrown. If the length of the |
1911 | * result exceeds max_size(), length_error is thrown. The value of the |
1912 | * string doesn't change if an error is thrown. |
1913 | */ |
1914 | basic_string& |
1915 | replace(size_type __pos1, size_type __n1, const basic_string& __str, |
1916 | size_type __pos2, size_type __n2 = npos) |
1917 | { return this->replace(__pos1, __n1, __str._M_data() |
1918 | + __str._M_check(__pos2, "basic_string::replace" ), |
1919 | __str._M_limit(__pos2, __n2)); } |
1920 | |
1921 | /** |
1922 | * @brief Replace characters with value of a C substring. |
1923 | * @param __pos Index of first character to replace. |
1924 | * @param __n1 Number of characters to be replaced. |
1925 | * @param __s C string to insert. |
1926 | * @param __n2 Number of characters from @a s to use. |
1927 | * @return Reference to this string. |
1928 | * @throw std::out_of_range If @a pos1 > size(). |
1929 | * @throw std::length_error If new length exceeds @c max_size(). |
1930 | * |
1931 | * Removes the characters in the range [__pos,__pos + __n1) |
1932 | * from this string. In place, the first @a __n2 characters of |
1933 | * @a __s are inserted, or all of @a __s if @a __n2 is too large. If |
1934 | * @a __pos is beyond end of string, out_of_range is thrown. If |
1935 | * the length of result exceeds max_size(), length_error is |
1936 | * thrown. The value of the string doesn't change if an error |
1937 | * is thrown. |
1938 | */ |
1939 | basic_string& |
1940 | replace(size_type __pos, size_type __n1, const _CharT* __s, |
1941 | size_type __n2) |
1942 | { |
1943 | __glibcxx_requires_string_len(__s, __n2); |
1944 | return _M_replace(_M_check(__pos, "basic_string::replace" ), |
1945 | _M_limit(__pos, __n1), __s, __n2); |
1946 | } |
1947 | |
1948 | /** |
1949 | * @brief Replace characters with value of a C string. |
1950 | * @param __pos Index of first character to replace. |
1951 | * @param __n1 Number of characters to be replaced. |
1952 | * @param __s C string to insert. |
1953 | * @return Reference to this string. |
1954 | * @throw std::out_of_range If @a pos > size(). |
1955 | * @throw std::length_error If new length exceeds @c max_size(). |
1956 | * |
1957 | * Removes the characters in the range [__pos,__pos + __n1) |
1958 | * from this string. In place, the characters of @a __s are |
1959 | * inserted. If @a __pos is beyond end of string, out_of_range |
1960 | * is thrown. If the length of result exceeds max_size(), |
1961 | * length_error is thrown. The value of the string doesn't |
1962 | * change if an error is thrown. |
1963 | */ |
1964 | basic_string& |
1965 | replace(size_type __pos, size_type __n1, const _CharT* __s) |
1966 | { |
1967 | __glibcxx_requires_string(__s); |
1968 | return this->replace(__pos, __n1, __s, traits_type::length(__s)); |
1969 | } |
1970 | |
1971 | /** |
1972 | * @brief Replace characters with multiple characters. |
1973 | * @param __pos Index of first character to replace. |
1974 | * @param __n1 Number of characters to be replaced. |
1975 | * @param __n2 Number of characters to insert. |
1976 | * @param __c Character to insert. |
1977 | * @return Reference to this string. |
1978 | * @throw std::out_of_range If @a __pos > size(). |
1979 | * @throw std::length_error If new length exceeds @c max_size(). |
1980 | * |
1981 | * Removes the characters in the range [pos,pos + n1) from this |
1982 | * string. In place, @a __n2 copies of @a __c are inserted. |
1983 | * If @a __pos is beyond end of string, out_of_range is thrown. |
1984 | * If the length of result exceeds max_size(), length_error is |
1985 | * thrown. The value of the string doesn't change if an error |
1986 | * is thrown. |
1987 | */ |
1988 | basic_string& |
1989 | replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) |
1990 | { return _M_replace_aux(_M_check(__pos, "basic_string::replace" ), |
1991 | _M_limit(__pos, __n1), __n2, __c); } |
1992 | |
1993 | /** |
1994 | * @brief Replace range of characters with string. |
1995 | * @param __i1 Iterator referencing start of range to replace. |
1996 | * @param __i2 Iterator referencing end of range to replace. |
1997 | * @param __str String value to insert. |
1998 | * @return Reference to this string. |
1999 | * @throw std::length_error If new length exceeds @c max_size(). |
2000 | * |
2001 | * Removes the characters in the range [__i1,__i2). In place, |
2002 | * the value of @a __str is inserted. If the length of result |
2003 | * exceeds max_size(), length_error is thrown. The value of |
2004 | * the string doesn't change if an error is thrown. |
2005 | */ |
2006 | basic_string& |
2007 | replace(__const_iterator __i1, __const_iterator __i2, |
2008 | const basic_string& __str) |
2009 | { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } |
2010 | |
2011 | /** |
2012 | * @brief Replace range of characters with C substring. |
2013 | * @param __i1 Iterator referencing start of range to replace. |
2014 | * @param __i2 Iterator referencing end of range to replace. |
2015 | * @param __s C string value to insert. |
2016 | * @param __n Number of characters from s to insert. |
2017 | * @return Reference to this string. |
2018 | * @throw std::length_error If new length exceeds @c max_size(). |
2019 | * |
2020 | * Removes the characters in the range [__i1,__i2). In place, |
2021 | * the first @a __n characters of @a __s are inserted. If the |
2022 | * length of result exceeds max_size(), length_error is thrown. |
2023 | * The value of the string doesn't change if an error is |
2024 | * thrown. |
2025 | */ |
2026 | basic_string& |
2027 | replace(__const_iterator __i1, __const_iterator __i2, |
2028 | const _CharT* __s, size_type __n) |
2029 | { |
2030 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2031 | && __i2 <= end()); |
2032 | return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); |
2033 | } |
2034 | |
2035 | /** |
2036 | * @brief Replace range of characters with C string. |
2037 | * @param __i1 Iterator referencing start of range to replace. |
2038 | * @param __i2 Iterator referencing end of range to replace. |
2039 | * @param __s C string value to insert. |
2040 | * @return Reference to this string. |
2041 | * @throw std::length_error If new length exceeds @c max_size(). |
2042 | * |
2043 | * Removes the characters in the range [__i1,__i2). In place, |
2044 | * the characters of @a __s are inserted. If the length of |
2045 | * result exceeds max_size(), length_error is thrown. The |
2046 | * value of the string doesn't change if an error is thrown. |
2047 | */ |
2048 | basic_string& |
2049 | replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) |
2050 | { |
2051 | __glibcxx_requires_string(__s); |
2052 | return this->replace(__i1, __i2, __s, traits_type::length(__s)); |
2053 | } |
2054 | |
2055 | /** |
2056 | * @brief Replace range of characters with multiple characters |
2057 | * @param __i1 Iterator referencing start of range to replace. |
2058 | * @param __i2 Iterator referencing end of range to replace. |
2059 | * @param __n Number of characters to insert. |
2060 | * @param __c Character to insert. |
2061 | * @return Reference to this string. |
2062 | * @throw std::length_error If new length exceeds @c max_size(). |
2063 | * |
2064 | * Removes the characters in the range [__i1,__i2). In place, |
2065 | * @a __n copies of @a __c are inserted. If the length of |
2066 | * result exceeds max_size(), length_error is thrown. The |
2067 | * value of the string doesn't change if an error is thrown. |
2068 | */ |
2069 | basic_string& |
2070 | replace(__const_iterator __i1, __const_iterator __i2, size_type __n, |
2071 | _CharT __c) |
2072 | { |
2073 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2074 | && __i2 <= end()); |
2075 | return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); |
2076 | } |
2077 | |
2078 | /** |
2079 | * @brief Replace range of characters with range. |
2080 | * @param __i1 Iterator referencing start of range to replace. |
2081 | * @param __i2 Iterator referencing end of range to replace. |
2082 | * @param __k1 Iterator referencing start of range to insert. |
2083 | * @param __k2 Iterator referencing end of range to insert. |
2084 | * @return Reference to this string. |
2085 | * @throw std::length_error If new length exceeds @c max_size(). |
2086 | * |
2087 | * Removes the characters in the range [__i1,__i2). In place, |
2088 | * characters in the range [__k1,__k2) are inserted. If the |
2089 | * length of result exceeds max_size(), length_error is thrown. |
2090 | * The value of the string doesn't change if an error is |
2091 | * thrown. |
2092 | */ |
2093 | #if __cplusplus >= 201103L |
2094 | template<class _InputIterator, |
2095 | typename = std::_RequireInputIter<_InputIterator>> |
2096 | basic_string& |
2097 | replace(const_iterator __i1, const_iterator __i2, |
2098 | _InputIterator __k1, _InputIterator __k2) |
2099 | { |
2100 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2101 | && __i2 <= end()); |
2102 | __glibcxx_requires_valid_range(__k1, __k2); |
2103 | return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, |
2104 | std::__false_type()); |
2105 | } |
2106 | #else |
2107 | template<class _InputIterator> |
2108 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
2109 | typename __enable_if_not_native_iterator<_InputIterator>::__type |
2110 | #else |
2111 | basic_string& |
2112 | #endif |
2113 | replace(iterator __i1, iterator __i2, |
2114 | _InputIterator __k1, _InputIterator __k2) |
2115 | { |
2116 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2117 | && __i2 <= end()); |
2118 | __glibcxx_requires_valid_range(__k1, __k2); |
2119 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
2120 | return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); |
2121 | } |
2122 | #endif |
2123 | |
2124 | // Specializations for the common case of pointer and iterator: |
2125 | // useful to avoid the overhead of temporary buffering in _M_replace. |
2126 | basic_string& |
2127 | replace(__const_iterator __i1, __const_iterator __i2, |
2128 | _CharT* __k1, _CharT* __k2) |
2129 | { |
2130 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2131 | && __i2 <= end()); |
2132 | __glibcxx_requires_valid_range(__k1, __k2); |
2133 | return this->replace(__i1 - begin(), __i2 - __i1, |
2134 | __k1, __k2 - __k1); |
2135 | } |
2136 | |
2137 | basic_string& |
2138 | replace(__const_iterator __i1, __const_iterator __i2, |
2139 | const _CharT* __k1, const _CharT* __k2) |
2140 | { |
2141 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2142 | && __i2 <= end()); |
2143 | __glibcxx_requires_valid_range(__k1, __k2); |
2144 | return this->replace(__i1 - begin(), __i2 - __i1, |
2145 | __k1, __k2 - __k1); |
2146 | } |
2147 | |
2148 | basic_string& |
2149 | replace(__const_iterator __i1, __const_iterator __i2, |
2150 | iterator __k1, iterator __k2) |
2151 | { |
2152 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2153 | && __i2 <= end()); |
2154 | __glibcxx_requires_valid_range(__k1, __k2); |
2155 | return this->replace(__i1 - begin(), __i2 - __i1, |
2156 | __k1.base(), __k2 - __k1); |
2157 | } |
2158 | |
2159 | basic_string& |
2160 | replace(__const_iterator __i1, __const_iterator __i2, |
2161 | const_iterator __k1, const_iterator __k2) |
2162 | { |
2163 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2164 | && __i2 <= end()); |
2165 | __glibcxx_requires_valid_range(__k1, __k2); |
2166 | return this->replace(__i1 - begin(), __i2 - __i1, |
2167 | __k1.base(), __k2 - __k1); |
2168 | } |
2169 | |
2170 | #if __cplusplus >= 201103L |
2171 | /** |
2172 | * @brief Replace range of characters with initializer_list. |
2173 | * @param __i1 Iterator referencing start of range to replace. |
2174 | * @param __i2 Iterator referencing end of range to replace. |
2175 | * @param __l The initializer_list of characters to insert. |
2176 | * @return Reference to this string. |
2177 | * @throw std::length_error If new length exceeds @c max_size(). |
2178 | * |
2179 | * Removes the characters in the range [__i1,__i2). In place, |
2180 | * characters in the range [__k1,__k2) are inserted. If the |
2181 | * length of result exceeds max_size(), length_error is thrown. |
2182 | * The value of the string doesn't change if an error is |
2183 | * thrown. |
2184 | */ |
2185 | basic_string& replace(const_iterator __i1, const_iterator __i2, |
2186 | initializer_list<_CharT> __l) |
2187 | { return this->replace(__i1, __i2, __l.begin(), __l.size()); } |
2188 | #endif // C++11 |
2189 | |
2190 | #if __cplusplus >= 201703L |
2191 | /** |
2192 | * @brief Replace range of characters with string_view. |
2193 | * @param __pos The position to replace at. |
2194 | * @param __n The number of characters to replace. |
2195 | * @param __svt The object convertible to string_view to insert. |
2196 | * @return Reference to this string. |
2197 | */ |
2198 | template<typename _Tp> |
2199 | _If_sv<_Tp, basic_string&> |
2200 | replace(size_type __pos, size_type __n, const _Tp& __svt) |
2201 | { |
2202 | __sv_type __sv = __svt; |
2203 | return this->replace(__pos, __n, __sv.data(), __sv.size()); |
2204 | } |
2205 | |
2206 | /** |
2207 | * @brief Replace range of characters with string_view. |
2208 | * @param __pos1 The position to replace at. |
2209 | * @param __n1 The number of characters to replace. |
2210 | * @param __svt The object convertible to string_view to insert from. |
2211 | * @param __pos2 The position in the string_view to insert from. |
2212 | * @param __n2 The number of characters to insert. |
2213 | * @return Reference to this string. |
2214 | */ |
2215 | template<typename _Tp> |
2216 | _If_sv<_Tp, basic_string&> |
2217 | replace(size_type __pos1, size_type __n1, const _Tp& __svt, |
2218 | size_type __pos2, size_type __n2 = npos) |
2219 | { |
2220 | __sv_type __sv = __svt; |
2221 | return this->replace(__pos1, __n1, |
2222 | __sv.data() |
2223 | + std::__sv_check(__sv.size(), __pos2, "basic_string::replace" ), |
2224 | std::__sv_limit(__sv.size(), __pos2, __n2)); |
2225 | } |
2226 | |
2227 | /** |
2228 | * @brief Replace range of characters with string_view. |
2229 | * @param __i1 An iterator referencing the start position |
2230 | to replace at. |
2231 | * @param __i2 An iterator referencing the end position |
2232 | for the replace. |
2233 | * @param __svt The object convertible to string_view to insert from. |
2234 | * @return Reference to this string. |
2235 | */ |
2236 | template<typename _Tp> |
2237 | _If_sv<_Tp, basic_string&> |
2238 | replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) |
2239 | { |
2240 | __sv_type __sv = __svt; |
2241 | return this->replace(__i1 - begin(), __i2 - __i1, __sv); |
2242 | } |
2243 | #endif // C++17 |
2244 | |
2245 | private: |
2246 | template<class _Integer> |
2247 | basic_string& |
2248 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
2249 | _Integer __n, _Integer __val, __true_type) |
2250 | { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } |
2251 | |
2252 | template<class _InputIterator> |
2253 | basic_string& |
2254 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
2255 | _InputIterator __k1, _InputIterator __k2, |
2256 | __false_type); |
2257 | |
2258 | basic_string& |
2259 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, |
2260 | _CharT __c); |
2261 | |
2262 | basic_string& |
2263 | _M_replace(size_type __pos, size_type __len1, const _CharT* __s, |
2264 | const size_type __len2); |
2265 | |
2266 | basic_string& |
2267 | _M_append(const _CharT* __s, size_type __n); |
2268 | |
2269 | public: |
2270 | |
2271 | /** |
2272 | * @brief Copy substring into C string. |
2273 | * @param __s C string to copy value into. |
2274 | * @param __n Number of characters to copy. |
2275 | * @param __pos Index of first character to copy. |
2276 | * @return Number of characters actually copied |
2277 | * @throw std::out_of_range If __pos > size(). |
2278 | * |
2279 | * Copies up to @a __n characters starting at @a __pos into the |
2280 | * C string @a __s. If @a __pos is %greater than size(), |
2281 | * out_of_range is thrown. |
2282 | */ |
2283 | size_type |
2284 | copy(_CharT* __s, size_type __n, size_type __pos = 0) const; |
2285 | |
2286 | /** |
2287 | * @brief Swap contents with another string. |
2288 | * @param __s String to swap with. |
2289 | * |
2290 | * Exchanges the contents of this string with that of @a __s in constant |
2291 | * time. |
2292 | */ |
2293 | void |
2294 | swap(basic_string& __s) _GLIBCXX_NOEXCEPT; |
2295 | |
2296 | // String operations: |
2297 | /** |
2298 | * @brief Return const pointer to null-terminated contents. |
2299 | * |
2300 | * This is a handle to internal data. Do not modify or dire things may |
2301 | * happen. |
2302 | */ |
2303 | const _CharT* |
2304 | c_str() const _GLIBCXX_NOEXCEPT |
2305 | { return _M_data(); } |
2306 | |
2307 | /** |
2308 | * @brief Return const pointer to contents. |
2309 | * |
2310 | * This is a pointer to internal data. It is undefined to modify |
2311 | * the contents through the returned pointer. To get a pointer that |
2312 | * allows modifying the contents use @c &str[0] instead, |
2313 | * (or in C++17 the non-const @c str.data() overload). |
2314 | */ |
2315 | const _CharT* |
2316 | data() const _GLIBCXX_NOEXCEPT |
2317 | { return _M_data(); } |
2318 | |
2319 | #if __cplusplus >= 201703L |
2320 | /** |
2321 | * @brief Return non-const pointer to contents. |
2322 | * |
2323 | * This is a pointer to the character sequence held by the string. |
2324 | * Modifying the characters in the sequence is allowed. |
2325 | */ |
2326 | _CharT* |
2327 | data() noexcept |
2328 | { return _M_data(); } |
2329 | #endif |
2330 | |
2331 | /** |
2332 | * @brief Return copy of allocator used to construct this string. |
2333 | */ |
2334 | allocator_type |
2335 | get_allocator() const _GLIBCXX_NOEXCEPT |
2336 | { return _M_get_allocator(); } |
2337 | |
2338 | /** |
2339 | * @brief Find position of a C substring. |
2340 | * @param __s C string to locate. |
2341 | * @param __pos Index of character to search from. |
2342 | * @param __n Number of characters from @a s to search for. |
2343 | * @return Index of start of first occurrence. |
2344 | * |
2345 | * Starting from @a __pos, searches forward for the first @a |
2346 | * __n characters in @a __s within this string. If found, |
2347 | * returns the index where it begins. If not found, returns |
2348 | * npos. |
2349 | */ |
2350 | size_type |
2351 | find(const _CharT* __s, size_type __pos, size_type __n) const |
2352 | _GLIBCXX_NOEXCEPT; |
2353 | |
2354 | /** |
2355 | * @brief Find position of a string. |
2356 | * @param __str String to locate. |
2357 | * @param __pos Index of character to search from (default 0). |
2358 | * @return Index of start of first occurrence. |
2359 | * |
2360 | * Starting from @a __pos, searches forward for value of @a __str within |
2361 | * this string. If found, returns the index where it begins. If not |
2362 | * found, returns npos. |
2363 | */ |
2364 | size_type |
2365 | find(const basic_string& __str, size_type __pos = 0) const |
2366 | _GLIBCXX_NOEXCEPT |
2367 | { return this->find(__str.data(), __pos, __str.size()); } |
2368 | |
2369 | #if __cplusplus >= 201703L |
2370 | /** |
2371 | * @brief Find position of a string_view. |
2372 | * @param __svt The object convertible to string_view to locate. |
2373 | * @param __pos Index of character to search from (default 0). |
2374 | * @return Index of start of first occurrence. |
2375 | */ |
2376 | template<typename _Tp> |
2377 | _If_sv<_Tp, size_type> |
2378 | find(const _Tp& __svt, size_type __pos = 0) const |
2379 | noexcept(is_same<_Tp, __sv_type>::value) |
2380 | { |
2381 | __sv_type __sv = __svt; |
2382 | return this->find(__sv.data(), __pos, __sv.size()); |
2383 | } |
2384 | #endif // C++17 |
2385 | |
2386 | /** |
2387 | * @brief Find position of a C string. |
2388 | * @param __s C string to locate. |
2389 | * @param __pos Index of character to search from (default 0). |
2390 | * @return Index of start of first occurrence. |
2391 | * |
2392 | * Starting from @a __pos, searches forward for the value of @a |
2393 | * __s within this string. If found, returns the index where |
2394 | * it begins. If not found, returns npos. |
2395 | */ |
2396 | size_type |
2397 | find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT |
2398 | { |
2399 | __glibcxx_requires_string(__s); |
2400 | return this->find(__s, __pos, traits_type::length(__s)); |
2401 | } |
2402 | |
2403 | /** |
2404 | * @brief Find position of a character. |
2405 | * @param __c Character to locate. |
2406 | * @param __pos Index of character to search from (default 0). |
2407 | * @return Index of first occurrence. |
2408 | * |
2409 | * Starting from @a __pos, searches forward for @a __c within |
2410 | * this string. If found, returns the index where it was |
2411 | * found. If not found, returns npos. |
2412 | */ |
2413 | size_type |
2414 | find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; |
2415 | |
2416 | /** |
2417 | * @brief Find last position of a string. |
2418 | * @param __str String to locate. |
2419 | * @param __pos Index of character to search back from (default end). |
2420 | * @return Index of start of last occurrence. |
2421 | * |
2422 | * Starting from @a __pos, searches backward for value of @a |
2423 | * __str within this string. If found, returns the index where |
2424 | * it begins. If not found, returns npos. |
2425 | */ |
2426 | size_type |
2427 | rfind(const basic_string& __str, size_type __pos = npos) const |
2428 | _GLIBCXX_NOEXCEPT |
2429 | { return this->rfind(__str.data(), __pos, __str.size()); } |
2430 | |
2431 | #if __cplusplus >= 201703L |
2432 | /** |
2433 | * @brief Find last position of a string_view. |
2434 | * @param __svt The object convertible to string_view to locate. |
2435 | * @param __pos Index of character to search back from (default end). |
2436 | * @return Index of start of last occurrence. |
2437 | */ |
2438 | template<typename _Tp> |
2439 | _If_sv<_Tp, size_type> |
2440 | rfind(const _Tp& __svt, size_type __pos = npos) const |
2441 | noexcept(is_same<_Tp, __sv_type>::value) |
2442 | { |
2443 | __sv_type __sv = __svt; |
2444 | return this->rfind(__sv.data(), __pos, __sv.size()); |
2445 | } |
2446 | #endif // C++17 |
2447 | |
2448 | /** |
2449 | * @brief Find last position of a C substring. |
2450 | * @param __s C string to locate. |
2451 | * @param __pos Index of character to search back from. |
2452 | * @param __n Number of characters from s to search for. |
2453 | * @return Index of start of last occurrence. |
2454 | * |
2455 | * Starting from @a __pos, searches backward for the first @a |
2456 | * __n characters in @a __s within this string. If found, |
2457 | * returns the index where it begins. If not found, returns |
2458 | * npos. |
2459 | */ |
2460 | size_type |
2461 | rfind(const _CharT* __s, size_type __pos, size_type __n) const |
2462 | _GLIBCXX_NOEXCEPT; |
2463 | |
2464 | /** |
2465 | * @brief Find last position of a C string. |
2466 | * @param __s C string to locate. |
2467 | * @param __pos Index of character to start search at (default end). |
2468 | * @return Index of start of last occurrence. |
2469 | * |
2470 | * Starting from @a __pos, searches backward for the value of |
2471 | * @a __s within this string. If found, returns the index |
2472 | * where it begins. If not found, returns npos. |
2473 | */ |
2474 | size_type |
2475 | rfind(const _CharT* __s, size_type __pos = npos) const |
2476 | { |
2477 | __glibcxx_requires_string(__s); |
2478 | return this->rfind(__s, __pos, traits_type::length(__s)); |
2479 | } |
2480 | |
2481 | /** |
2482 | * @brief Find last position of a character. |
2483 | * @param __c Character to locate. |
2484 | * @param __pos Index of character to search back from (default end). |
2485 | * @return Index of last occurrence. |
2486 | * |
2487 | * Starting from @a __pos, searches backward for @a __c within |
2488 | * this string. If found, returns the index where it was |
2489 | * found. If not found, returns npos. |
2490 | */ |
2491 | size_type |
2492 | rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; |
2493 | |
2494 | /** |
2495 | * @brief Find position of a character of string. |
2496 | * @param __str String containing characters to locate. |
2497 | * @param __pos Index of character to search from (default 0). |
2498 | * @return Index of first occurrence. |
2499 | * |
2500 | * Starting from @a __pos, searches forward for one of the |
2501 | * characters of @a __str within this string. If found, |
2502 | * returns the index where it was found. If not found, returns |
2503 | * npos. |
2504 | */ |
2505 | size_type |
2506 | find_first_of(const basic_string& __str, size_type __pos = 0) const |
2507 | _GLIBCXX_NOEXCEPT |
2508 | { return this->find_first_of(__str.data(), __pos, __str.size()); } |
2509 | |
2510 | #if __cplusplus >= 201703L |
2511 | /** |
2512 | * @brief Find position of a character of a string_view. |
2513 | * @param __svt An object convertible to string_view containing |
2514 | * characters to locate. |
2515 | * @param __pos Index of character to search from (default 0). |
2516 | * @return Index of first occurrence. |
2517 | */ |
2518 | template<typename _Tp> |
2519 | _If_sv<_Tp, size_type> |
2520 | find_first_of(const _Tp& __svt, size_type __pos = 0) const |
2521 | noexcept(is_same<_Tp, __sv_type>::value) |
2522 | { |
2523 | __sv_type __sv = __svt; |
2524 | return this->find_first_of(__sv.data(), __pos, __sv.size()); |
2525 | } |
2526 | #endif // C++17 |
2527 | |
2528 | /** |
2529 | * @brief Find position of a character of C substring. |
2530 | * @param __s String containing characters to locate. |
2531 | * @param __pos Index of character to search from. |
2532 | * @param __n Number of characters from s to search for. |
2533 | * @return Index of first occurrence. |
2534 | * |
2535 | * Starting from @a __pos, searches forward for one of the |
2536 | * first @a __n characters of @a __s within this string. If |
2537 | * found, returns the index where it was found. If not found, |
2538 | * returns npos. |
2539 | */ |
2540 | size_type |
2541 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
2542 | _GLIBCXX_NOEXCEPT; |
2543 | |
2544 | /** |
2545 | * @brief Find position of a character of C string. |
2546 | * @param __s String containing characters to locate. |
2547 | * @param __pos Index of character to search from (default 0). |
2548 | * @return Index of first occurrence. |
2549 | * |
2550 | * Starting from @a __pos, searches forward for one of the |
2551 | * characters of @a __s within this string. If found, returns |
2552 | * the index where it was found. If not found, returns npos. |
2553 | */ |
2554 | size_type |
2555 | find_first_of(const _CharT* __s, size_type __pos = 0) const |
2556 | _GLIBCXX_NOEXCEPT |
2557 | { |
2558 | __glibcxx_requires_string(__s); |
2559 | return this->find_first_of(__s, __pos, traits_type::length(__s)); |
2560 | } |
2561 | |
2562 | /** |
2563 | * @brief Find position of a character. |
2564 | * @param __c Character to locate. |
2565 | * @param __pos Index of character to search from (default 0). |
2566 | * @return Index of first occurrence. |
2567 | * |
2568 | * Starting from @a __pos, searches forward for the character |
2569 | * @a __c within this string. If found, returns the index |
2570 | * where it was found. If not found, returns npos. |
2571 | * |
2572 | * Note: equivalent to find(__c, __pos). |
2573 | */ |
2574 | size_type |
2575 | find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT |
2576 | { return this->find(__c, __pos); } |
2577 | |
2578 | /** |
2579 | * @brief Find last position of a character of string. |
2580 | * @param __str String containing characters to locate. |
2581 | * @param __pos Index of character to search back from (default end). |
2582 | * @return Index of last occurrence. |
2583 | * |
2584 | * Starting from @a __pos, searches backward for one of the |
2585 | * characters of @a __str within this string. If found, |
2586 | * returns the index where it was found. If not found, returns |
2587 | * npos. |
2588 | */ |
2589 | size_type |
2590 | find_last_of(const basic_string& __str, size_type __pos = npos) const |
2591 | _GLIBCXX_NOEXCEPT |
2592 | { return this->find_last_of(__str.data(), __pos, __str.size()); } |
2593 | |
2594 | #if __cplusplus >= 201703L |
2595 | /** |
2596 | * @brief Find last position of a character of string. |
2597 | * @param __svt An object convertible to string_view containing |
2598 | * characters to locate. |
2599 | * @param __pos Index of character to search back from (default end). |
2600 | * @return Index of last occurrence. |
2601 | */ |
2602 | template<typename _Tp> |
2603 | _If_sv<_Tp, size_type> |
2604 | find_last_of(const _Tp& __svt, size_type __pos = npos) const |
2605 | noexcept(is_same<_Tp, __sv_type>::value) |
2606 | { |
2607 | __sv_type __sv = __svt; |
2608 | return this->find_last_of(__sv.data(), __pos, __sv.size()); |
2609 | } |
2610 | #endif // C++17 |
2611 | |
2612 | /** |
2613 | * @brief Find last position of a character of C substring. |
2614 | * @param __s C string containing characters to locate. |
2615 | * @param __pos Index of character to search back from. |
2616 | * @param __n Number of characters from s to search for. |
2617 | * @return Index of last occurrence. |
2618 | * |
2619 | * Starting from @a __pos, searches backward for one of the |
2620 | * first @a __n characters of @a __s within this string. If |
2621 | * found, returns the index where it was found. If not found, |
2622 | * returns npos. |
2623 | */ |
2624 | size_type |
2625 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
2626 | _GLIBCXX_NOEXCEPT; |
2627 | |
2628 | /** |
2629 | * @brief Find last position of a character of C string. |
2630 | * @param __s C string containing characters to locate. |
2631 | * @param __pos Index of character to search back from (default end). |
2632 | * @return Index of last occurrence. |
2633 | * |
2634 | * Starting from @a __pos, searches backward for one of the |
2635 | * characters of @a __s within this string. If found, returns |
2636 | * the index where it was found. If not found, returns npos. |
2637 | */ |
2638 | size_type |
2639 | find_last_of(const _CharT* __s, size_type __pos = npos) const |
2640 | _GLIBCXX_NOEXCEPT |
2641 | { |
2642 | __glibcxx_requires_string(__s); |
2643 | return this->find_last_of(__s, __pos, traits_type::length(__s)); |
2644 | } |
2645 | |
2646 | /** |
2647 | * @brief Find last position of a character. |
2648 | * @param __c Character to locate. |
2649 | * @param __pos Index of character to search back from (default end). |
2650 | * @return Index of last occurrence. |
2651 | * |
2652 | * Starting from @a __pos, searches backward for @a __c within |
2653 | * this string. If found, returns the index where it was |
2654 | * found. If not found, returns npos. |
2655 | * |
2656 | * Note: equivalent to rfind(__c, __pos). |
2657 | */ |
2658 | size_type |
2659 | find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT |
2660 | { return this->rfind(__c, __pos); } |
2661 | |
2662 | /** |
2663 | * @brief Find position of a character not in string. |
2664 | * @param __str String containing characters to avoid. |
2665 | * @param __pos Index of character to search from (default 0). |
2666 | * @return Index of first occurrence. |
2667 | * |
2668 | * Starting from @a __pos, searches forward for a character not contained |
2669 | * in @a __str within this string. If found, returns the index where it |
2670 | * was found. If not found, returns npos. |
2671 | */ |
2672 | size_type |
2673 | find_first_not_of(const basic_string& __str, size_type __pos = 0) const |
2674 | _GLIBCXX_NOEXCEPT |
2675 | { return this->find_first_not_of(__str.data(), __pos, __str.size()); } |
2676 | |
2677 | #if __cplusplus >= 201703L |
2678 | /** |
2679 | * @brief Find position of a character not in a string_view. |
2680 | * @param __svt A object convertible to string_view containing |
2681 | * characters to avoid. |
2682 | * @param __pos Index of character to search from (default 0). |
2683 | * @return Index of first occurrence. |
2684 | */ |
2685 | template<typename _Tp> |
2686 | _If_sv<_Tp, size_type> |
2687 | find_first_not_of(const _Tp& __svt, size_type __pos = 0) const |
2688 | noexcept(is_same<_Tp, __sv_type>::value) |
2689 | { |
2690 | __sv_type __sv = __svt; |
2691 | return this->find_first_not_of(__sv.data(), __pos, __sv.size()); |
2692 | } |
2693 | #endif // C++17 |
2694 | |
2695 | /** |
2696 | * @brief Find position of a character not in C substring. |
2697 | * @param __s C string containing characters to avoid. |
2698 | * @param __pos Index of character to search from. |
2699 | * @param __n Number of characters from __s to consider. |
2700 | * @return Index of first occurrence. |
2701 | * |
2702 | * Starting from @a __pos, searches forward for a character not |
2703 | * contained in the first @a __n characters of @a __s within |
2704 | * this string. If found, returns the index where it was |
2705 | * found. If not found, returns npos. |
2706 | */ |
2707 | size_type |
2708 | find_first_not_of(const _CharT* __s, size_type __pos, |
2709 | size_type __n) const _GLIBCXX_NOEXCEPT; |
2710 | |
2711 | /** |
2712 | * @brief Find position of a character not in C string. |
2713 | * @param __s C string containing characters to avoid. |
2714 | * @param __pos Index of character to search from (default 0). |
2715 | * @return Index of first occurrence. |
2716 | * |
2717 | * Starting from @a __pos, searches forward for a character not |
2718 | * contained in @a __s within this string. If found, returns |
2719 | * the index where it was found. If not found, returns npos. |
2720 | */ |
2721 | size_type |
2722 | find_first_not_of(const _CharT* __s, size_type __pos = 0) const |
2723 | _GLIBCXX_NOEXCEPT |
2724 | { |
2725 | __glibcxx_requires_string(__s); |
2726 | return this->find_first_not_of(__s, __pos, traits_type::length(__s)); |
2727 | } |
2728 | |
2729 | /** |
2730 | * @brief Find position of a different character. |
2731 | * @param __c Character to avoid. |
2732 | * @param __pos Index of character to search from (default 0). |
2733 | * @return Index of first occurrence. |
2734 | * |
2735 | * Starting from @a __pos, searches forward for a character |
2736 | * other than @a __c within this string. If found, returns the |
2737 | * index where it was found. If not found, returns npos. |
2738 | */ |
2739 | size_type |
2740 | find_first_not_of(_CharT __c, size_type __pos = 0) const |
2741 | _GLIBCXX_NOEXCEPT; |
2742 | |
2743 | /** |
2744 | * @brief Find last position of a character not in string. |
2745 | * @param __str String containing characters to avoid. |
2746 | * @param __pos Index of character to search back from (default end). |
2747 | * @return Index of last occurrence. |
2748 | * |
2749 | * Starting from @a __pos, searches backward for a character |
2750 | * not contained in @a __str within this string. If found, |
2751 | * returns the index where it was found. If not found, returns |
2752 | * npos. |
2753 | */ |
2754 | size_type |
2755 | find_last_not_of(const basic_string& __str, size_type __pos = npos) const |
2756 | _GLIBCXX_NOEXCEPT |
2757 | { return this->find_last_not_of(__str.data(), __pos, __str.size()); } |
2758 | |
2759 | #if __cplusplus >= 201703L |
2760 | /** |
2761 | * @brief Find last position of a character not in a string_view. |
2762 | * @param __svt An object convertible to string_view containing |
2763 | * characters to avoid. |
2764 | * @param __pos Index of character to search back from (default end). |
2765 | * @return Index of last occurrence. |
2766 | */ |
2767 | template<typename _Tp> |
2768 | _If_sv<_Tp, size_type> |
2769 | find_last_not_of(const _Tp& __svt, size_type __pos = npos) const |
2770 | noexcept(is_same<_Tp, __sv_type>::value) |
2771 | { |
2772 | __sv_type __sv = __svt; |
2773 | return this->find_last_not_of(__sv.data(), __pos, __sv.size()); |
2774 | } |
2775 | #endif // C++17 |
2776 | |
2777 | /** |
2778 | * @brief Find last position of a character not in C substring. |
2779 | * @param __s C string containing characters to avoid. |
2780 | * @param __pos Index of character to search back from. |
2781 | * @param __n Number of characters from s to consider. |
2782 | * @return Index of last occurrence. |
2783 | * |
2784 | * Starting from @a __pos, searches backward for a character not |
2785 | * contained in the first @a __n characters of @a __s within this string. |
2786 | * If found, returns the index where it was found. If not found, |
2787 | * returns npos. |
2788 | */ |
2789 | size_type |
2790 | find_last_not_of(const _CharT* __s, size_type __pos, |
2791 | size_type __n) const _GLIBCXX_NOEXCEPT; |
2792 | /** |
2793 | * @brief Find last position of a character not in C string. |
2794 | * @param __s C string containing characters to avoid. |
2795 | * @param __pos Index of character to search back from (default end). |
2796 | * @return Index of last occurrence. |
2797 | * |
2798 | * Starting from @a __pos, searches backward for a character |
2799 | * not contained in @a __s within this string. If found, |
2800 | * returns the index where it was found. If not found, returns |
2801 | * npos. |
2802 | */ |
2803 | size_type |
2804 | find_last_not_of(const _CharT* __s, size_type __pos = npos) const |
2805 | _GLIBCXX_NOEXCEPT |
2806 | { |
2807 | __glibcxx_requires_string(__s); |
2808 | return this->find_last_not_of(__s, __pos, traits_type::length(__s)); |
2809 | } |
2810 | |
2811 | /** |
2812 | * @brief Find last position of a different character. |
2813 | * @param __c Character to avoid. |
2814 | * @param __pos Index of character to search back from (default end). |
2815 | * @return Index of last occurrence. |
2816 | * |
2817 | * Starting from @a __pos, searches backward for a character other than |
2818 | * @a __c within this string. If found, returns the index where it was |
2819 | * found. If not found, returns npos. |
2820 | */ |
2821 | size_type |
2822 | find_last_not_of(_CharT __c, size_type __pos = npos) const |
2823 | _GLIBCXX_NOEXCEPT; |
2824 | |
2825 | /** |
2826 | * @brief Get a substring. |
2827 | * @param __pos Index of first character (default 0). |
2828 | * @param __n Number of characters in substring (default remainder). |
2829 | * @return The new string. |
2830 | * @throw std::out_of_range If __pos > size(). |
2831 | * |
2832 | * Construct and return a new string using the @a __n |
2833 | * characters starting at @a __pos. If the string is too |
2834 | * short, use the remainder of the characters. If @a __pos is |
2835 | * beyond the end of the string, out_of_range is thrown. |
2836 | */ |
2837 | basic_string |
2838 | substr(size_type __pos = 0, size_type __n = npos) const |
2839 | { return basic_string(*this, |
2840 | _M_check(__pos, "basic_string::substr" ), __n); } |
2841 | |
2842 | /** |
2843 | * @brief Compare to a string. |
2844 | * @param __str String to compare against. |
2845 | * @return Integer < 0, 0, or > 0. |
2846 | * |
2847 | * Returns an integer < 0 if this string is ordered before @a |
2848 | * __str, 0 if their values are equivalent, or > 0 if this |
2849 | * string is ordered after @a __str. Determines the effective |
2850 | * length rlen of the strings to compare as the smallest of |
2851 | * size() and str.size(). The function then compares the two |
2852 | * strings by calling traits::compare(data(), str.data(),rlen). |
2853 | * If the result of the comparison is nonzero returns it, |
2854 | * otherwise the shorter one is ordered first. |
2855 | */ |
2856 | int |
2857 | compare(const basic_string& __str) const |
2858 | { |
2859 | const size_type __size = this->size(); |
2860 | const size_type __osize = __str.size(); |
2861 | const size_type __len = std::min(__size, __osize); |
2862 | |
2863 | int __r = traits_type::compare(_M_data(), __str.data(), __len); |
2864 | if (!__r) |
2865 | __r = _S_compare(__size, __osize); |
2866 | return __r; |
2867 | } |
2868 | |
2869 | #if __cplusplus >= 201703L |
2870 | /** |
2871 | * @brief Compare to a string_view. |
2872 | * @param __svt An object convertible to string_view to compare against. |
2873 | * @return Integer < 0, 0, or > 0. |
2874 | */ |
2875 | template<typename _Tp> |
2876 | _If_sv<_Tp, int> |
2877 | compare(const _Tp& __svt) const |
2878 | noexcept(is_same<_Tp, __sv_type>::value) |
2879 | { |
2880 | __sv_type __sv = __svt; |
2881 | const size_type __size = this->size(); |
2882 | const size_type __osize = __sv.size(); |
2883 | const size_type __len = std::min(__size, __osize); |
2884 | |
2885 | int __r = traits_type::compare(_M_data(), __sv.data(), __len); |
2886 | if (!__r) |
2887 | __r = _S_compare(__size, __osize); |
2888 | return __r; |
2889 | } |
2890 | |
2891 | /** |
2892 | * @brief Compare to a string_view. |
2893 | * @param __pos A position in the string to start comparing from. |
2894 | * @param __n The number of characters to compare. |
2895 | * @param __svt An object convertible to string_view to compare |
2896 | * against. |
2897 | * @return Integer < 0, 0, or > 0. |
2898 | */ |
2899 | template<typename _Tp> |
2900 | _If_sv<_Tp, int> |
2901 | compare(size_type __pos, size_type __n, const _Tp& __svt) const |
2902 | noexcept(is_same<_Tp, __sv_type>::value) |
2903 | { |
2904 | __sv_type __sv = __svt; |
2905 | return __sv_type(*this).substr(__pos, __n).compare(__sv); |
2906 | } |
2907 | |
2908 | /** |
2909 | * @brief Compare to a string_view. |
2910 | * @param __pos1 A position in the string to start comparing from. |
2911 | * @param __n1 The number of characters to compare. |
2912 | * @param __svt An object convertible to string_view to compare |
2913 | * against. |
2914 | * @param __pos2 A position in the string_view to start comparing from. |
2915 | * @param __n2 The number of characters to compare. |
2916 | * @return Integer < 0, 0, or > 0. |
2917 | */ |
2918 | template<typename _Tp> |
2919 | _If_sv<_Tp, int> |
2920 | compare(size_type __pos1, size_type __n1, const _Tp& __svt, |
2921 | size_type __pos2, size_type __n2 = npos) const |
2922 | noexcept(is_same<_Tp, __sv_type>::value) |
2923 | { |
2924 | __sv_type __sv = __svt; |
2925 | return __sv_type(*this) |
2926 | .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); |
2927 | } |
2928 | #endif // C++17 |
2929 | |
2930 | /** |
2931 | * @brief Compare substring to a string. |
2932 | * @param __pos Index of first character of substring. |
2933 | * @param __n Number of characters in substring. |
2934 | * @param __str String to compare against. |
2935 | * @return Integer < 0, 0, or > 0. |
2936 | * |
2937 | * Form the substring of this string from the @a __n characters |
2938 | * starting at @a __pos. Returns an integer < 0 if the |
2939 | * substring is ordered before @a __str, 0 if their values are |
2940 | * equivalent, or > 0 if the substring is ordered after @a |
2941 | * __str. Determines the effective length rlen of the strings |
2942 | * to compare as the smallest of the length of the substring |
2943 | * and @a __str.size(). The function then compares the two |
2944 | * strings by calling |
2945 | * traits::compare(substring.data(),str.data(),rlen). If the |
2946 | * result of the comparison is nonzero returns it, otherwise |
2947 | * the shorter one is ordered first. |
2948 | */ |
2949 | int |
2950 | compare(size_type __pos, size_type __n, const basic_string& __str) const; |
2951 | |
2952 | /** |
2953 | * @brief Compare substring to a substring. |
2954 | * @param __pos1 Index of first character of substring. |
2955 | * @param __n1 Number of characters in substring. |
2956 | * @param __str String to compare against. |
2957 | * @param __pos2 Index of first character of substring of str. |
2958 | * @param __n2 Number of characters in substring of str. |
2959 | * @return Integer < 0, 0, or > 0. |
2960 | * |
2961 | * Form the substring of this string from the @a __n1 |
2962 | * characters starting at @a __pos1. Form the substring of @a |
2963 | * __str from the @a __n2 characters starting at @a __pos2. |
2964 | * Returns an integer < 0 if this substring is ordered before |
2965 | * the substring of @a __str, 0 if their values are equivalent, |
2966 | * or > 0 if this substring is ordered after the substring of |
2967 | * @a __str. Determines the effective length rlen of the |
2968 | * strings to compare as the smallest of the lengths of the |
2969 | * substrings. The function then compares the two strings by |
2970 | * calling |
2971 | * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). |
2972 | * If the result of the comparison is nonzero returns it, |
2973 | * otherwise the shorter one is ordered first. |
2974 | */ |
2975 | int |
2976 | compare(size_type __pos1, size_type __n1, const basic_string& __str, |
2977 | size_type __pos2, size_type __n2 = npos) const; |
2978 | |
2979 | /** |
2980 | * @brief Compare to a C string. |
2981 | * @param __s C string to compare against. |
2982 | * @return Integer < 0, 0, or > 0. |
2983 | * |
2984 | * Returns an integer < 0 if this string is ordered before @a __s, 0 if |
2985 | * their values are equivalent, or > 0 if this string is ordered after |
2986 | * @a __s. Determines the effective length rlen of the strings to |
2987 | * compare as the smallest of size() and the length of a string |
2988 | * constructed from @a __s. The function then compares the two strings |
2989 | * by calling traits::compare(data(),s,rlen). If the result of the |
2990 | * comparison is nonzero returns it, otherwise the shorter one is |
2991 | * ordered first. |
2992 | */ |
2993 | int |
2994 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT; |
2995 | |
2996 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
2997 | // 5 String::compare specification questionable |
2998 | /** |
2999 | * @brief Compare substring to a C string. |
3000 | * @param __pos Index of first character of substring. |
3001 | * @param __n1 Number of characters in substring. |
3002 | * @param __s C string to compare against. |
3003 | * @return Integer < 0, 0, or > 0. |
3004 | * |
3005 | * Form the substring of this string from the @a __n1 |
3006 | * characters starting at @a pos. Returns an integer < 0 if |
3007 | * the substring is ordered before @a __s, 0 if their values |
3008 | * are equivalent, or > 0 if the substring is ordered after @a |
3009 | * __s. Determines the effective length rlen of the strings to |
3010 | * compare as the smallest of the length of the substring and |
3011 | * the length of a string constructed from @a __s. The |
3012 | * function then compares the two string by calling |
3013 | * traits::compare(substring.data(),__s,rlen). If the result of |
3014 | * the comparison is nonzero returns it, otherwise the shorter |
3015 | * one is ordered first. |
3016 | */ |
3017 | int |
3018 | compare(size_type __pos, size_type __n1, const _CharT* __s) const; |
3019 | |
3020 | /** |
3021 | * @brief Compare substring against a character %array. |
3022 | * @param __pos Index of first character of substring. |
3023 | * @param __n1 Number of characters in substring. |
3024 | * @param __s character %array to compare against. |
3025 | * @param __n2 Number of characters of s. |
3026 | * @return Integer < 0, 0, or > 0. |
3027 | * |
3028 | * Form the substring of this string from the @a __n1 |
3029 | * characters starting at @a __pos. Form a string from the |
3030 | * first @a __n2 characters of @a __s. Returns an integer < 0 |
3031 | * if this substring is ordered before the string from @a __s, |
3032 | * 0 if their values are equivalent, or > 0 if this substring |
3033 | * is ordered after the string from @a __s. Determines the |
3034 | * effective length rlen of the strings to compare as the |
3035 | * smallest of the length of the substring and @a __n2. The |
3036 | * function then compares the two strings by calling |
3037 | * traits::compare(substring.data(),s,rlen). If the result of |
3038 | * the comparison is nonzero returns it, otherwise the shorter |
3039 | * one is ordered first. |
3040 | * |
3041 | * NB: s must have at least n2 characters, '\\0' has |
3042 | * no special meaning. |
3043 | */ |
3044 | int |
3045 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
3046 | size_type __n2) const; |
3047 | |
3048 | #if __cplusplus > 201703L |
3049 | bool |
3050 | starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3051 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3052 | |
3053 | bool |
3054 | starts_with(_CharT __x) const noexcept |
3055 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3056 | |
3057 | bool |
3058 | starts_with(const _CharT* __x) const noexcept |
3059 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3060 | |
3061 | bool |
3062 | ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3063 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3064 | |
3065 | bool |
3066 | ends_with(_CharT __x) const noexcept |
3067 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3068 | |
3069 | bool |
3070 | ends_with(const _CharT* __x) const noexcept |
3071 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3072 | #endif // C++20 |
3073 | |
3074 | // Allow basic_stringbuf::__xfer_bufptrs to call _M_length: |
3075 | template<typename, typename, typename> friend class basic_stringbuf; |
3076 | }; |
3077 | _GLIBCXX_END_NAMESPACE_CXX11 |
3078 | #else // !_GLIBCXX_USE_CXX11_ABI |
3079 | // Reference-counted COW string implentation |
3080 | |
3081 | /** |
3082 | * @class basic_string basic_string.h <string> |
3083 | * @brief Managing sequences of characters and character-like objects. |
3084 | * |
3085 | * @ingroup strings |
3086 | * @ingroup sequences |
3087 | * |
3088 | * @tparam _CharT Type of character |
3089 | * @tparam _Traits Traits for character type, defaults to |
3090 | * char_traits<_CharT>. |
3091 | * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. |
3092 | * |
3093 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
3094 | * <a href="tables.html#66">reversible container</a>, and a |
3095 | * <a href="tables.html#67">sequence</a>. Of the |
3096 | * <a href="tables.html#68">optional sequence requirements</a>, only |
3097 | * @c push_back, @c at, and @c %array access are supported. |
3098 | * |
3099 | * @doctodo |
3100 | * |
3101 | * |
3102 | * Documentation? What's that? |
3103 | * Nathan Myers <ncm@cantrip.org>. |
3104 | * |
3105 | * A string looks like this: |
3106 | * |
3107 | * @code |
3108 | * [_Rep] |
3109 | * _M_length |
3110 | * [basic_string<char_type>] _M_capacity |
3111 | * _M_dataplus _M_refcount |
3112 | * _M_p ----------------> unnamed array of char_type |
3113 | * @endcode |
3114 | * |
3115 | * Where the _M_p points to the first character in the string, and |
3116 | * you cast it to a pointer-to-_Rep and subtract 1 to get a |
3117 | * pointer to the header. |
3118 | * |
3119 | * This approach has the enormous advantage that a string object |
3120 | * requires only one allocation. All the ugliness is confined |
3121 | * within a single %pair of inline functions, which each compile to |
3122 | * a single @a add instruction: _Rep::_M_data(), and |
3123 | * string::_M_rep(); and the allocation function which gets a |
3124 | * block of raw bytes and with room enough and constructs a _Rep |
3125 | * object at the front. |
3126 | * |
3127 | * The reason you want _M_data pointing to the character %array and |
3128 | * not the _Rep is so that the debugger can see the string |
3129 | * contents. (Probably we should add a non-inline member to get |
3130 | * the _Rep for the debugger to use, so users can check the actual |
3131 | * string length.) |
3132 | * |
3133 | * Note that the _Rep object is a POD so that you can have a |
3134 | * static <em>empty string</em> _Rep object already @a constructed before |
3135 | * static constructors have run. The reference-count encoding is |
3136 | * chosen so that a 0 indicates one reference, so you never try to |
3137 | * destroy the empty-string _Rep object. |
3138 | * |
3139 | * All but the last paragraph is considered pretty conventional |
3140 | * for a C++ string implementation. |
3141 | */ |
3142 | // 21.3 Template class basic_string |
3143 | template<typename _CharT, typename _Traits, typename _Alloc> |
3144 | class basic_string |
3145 | { |
3146 | typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type; |
3147 | |
3148 | // Types: |
3149 | public: |
3150 | typedef _Traits traits_type; |
3151 | typedef typename _Traits::char_type value_type; |
3152 | typedef _Alloc allocator_type; |
3153 | typedef typename _CharT_alloc_type::size_type size_type; |
3154 | typedef typename _CharT_alloc_type::difference_type difference_type; |
3155 | #if __cplusplus < 201103L |
3156 | typedef typename _CharT_alloc_type::reference reference; |
3157 | typedef typename _CharT_alloc_type::const_reference const_reference; |
3158 | #else |
3159 | typedef value_type& reference; |
3160 | typedef const value_type& const_reference; |
3161 | #endif |
3162 | typedef typename _CharT_alloc_type::pointer pointer; |
3163 | typedef typename _CharT_alloc_type::const_pointer const_pointer; |
3164 | typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; |
3165 | typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> |
3166 | const_iterator; |
3167 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
3168 | typedef std::reverse_iterator<iterator> reverse_iterator; |
3169 | |
3170 | protected: |
3171 | // type used for positions in insert, erase etc. |
3172 | typedef iterator __const_iterator; |
3173 | |
3174 | private: |
3175 | // _Rep: string representation |
3176 | // Invariants: |
3177 | // 1. String really contains _M_length + 1 characters: due to 21.3.4 |
3178 | // must be kept null-terminated. |
3179 | // 2. _M_capacity >= _M_length |
3180 | // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). |
3181 | // 3. _M_refcount has three states: |
3182 | // -1: leaked, one reference, no ref-copies allowed, non-const. |
3183 | // 0: one reference, non-const. |
3184 | // n>0: n + 1 references, operations require a lock, const. |
3185 | // 4. All fields==0 is an empty string, given the extra storage |
3186 | // beyond-the-end for a null terminator; thus, the shared |
3187 | // empty string representation needs no constructor. |
3188 | |
3189 | struct _Rep_base |
3190 | { |
3191 | size_type _M_length; |
3192 | size_type _M_capacity; |
3193 | _Atomic_word _M_refcount; |
3194 | }; |
3195 | |
3196 | struct _Rep : _Rep_base |
3197 | { |
3198 | // Types: |
3199 | typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; |
3200 | |
3201 | // (Public) Data members: |
3202 | |
3203 | // The maximum number of individual char_type elements of an |
3204 | // individual string is determined by _S_max_size. This is the |
3205 | // value that will be returned by max_size(). (Whereas npos |
3206 | // is the maximum number of bytes the allocator can allocate.) |
3207 | // If one was to divvy up the theoretical largest size string, |
3208 | // with a terminating character and m _CharT elements, it'd |
3209 | // look like this: |
3210 | // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) |
3211 | // Solving for m: |
3212 | // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 |
3213 | // In addition, this implementation quarters this amount. |
3214 | static const size_type _S_max_size; |
3215 | static const _CharT _S_terminal; |
3216 | |
3217 | // The following storage is init'd to 0 by the linker, resulting |
3218 | // (carefully) in an empty string with one reference. |
3219 | static size_type _S_empty_rep_storage[]; |
3220 | |
3221 | static _Rep& |
3222 | _S_empty_rep() _GLIBCXX_NOEXCEPT |
3223 | { |
3224 | // NB: Mild hack to avoid strict-aliasing warnings. Note that |
3225 | // _S_empty_rep_storage is never modified and the punning should |
3226 | // be reasonably safe in this case. |
3227 | void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); |
3228 | return *reinterpret_cast<_Rep*>(__p); |
3229 | } |
3230 | |
3231 | bool |
3232 | _M_is_leaked() const _GLIBCXX_NOEXCEPT |
3233 | { |
3234 | #if defined(__GTHREADS) |
3235 | // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, |
3236 | // so we need to use an atomic load. However, _M_is_leaked |
3237 | // predicate does not change concurrently (i.e. the string is either |
3238 | // leaked or not), so a relaxed load is enough. |
3239 | return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0; |
3240 | #else |
3241 | return this->_M_refcount < 0; |
3242 | #endif |
3243 | } |
3244 | |
3245 | bool |
3246 | _M_is_shared() const _GLIBCXX_NOEXCEPT |
3247 | { |
3248 | #if defined(__GTHREADS) |
3249 | // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, |
3250 | // so we need to use an atomic load. Another thread can drop last |
3251 | // but one reference concurrently with this check, so we need this |
3252 | // load to be acquire to synchronize with release fetch_and_add in |
3253 | // _M_dispose. |
3254 | return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0; |
3255 | #else |
3256 | return this->_M_refcount > 0; |
3257 | #endif |
3258 | } |
3259 | |
3260 | void |
3261 | _M_set_leaked() _GLIBCXX_NOEXCEPT |
3262 | { this->_M_refcount = -1; } |
3263 | |
3264 | void |
3265 | _M_set_sharable() _GLIBCXX_NOEXCEPT |
3266 | { this->_M_refcount = 0; } |
3267 | |
3268 | void |
3269 | _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT |
3270 | { |
3271 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3272 | if (__builtin_expect(this != &_S_empty_rep(), false)) |
3273 | #endif |
3274 | { |
3275 | this->_M_set_sharable(); // One reference. |
3276 | this->_M_length = __n; |
3277 | traits_type::assign(this->_M_refdata()[__n], _S_terminal); |
3278 | // grrr. (per 21.3.4) |
3279 | // You cannot leave those LWG people alone for a second. |
3280 | } |
3281 | } |
3282 | |
3283 | _CharT* |
3284 | _M_refdata() throw() |
3285 | { return reinterpret_cast<_CharT*>(this + 1); } |
3286 | |
3287 | _CharT* |
3288 | _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) |
3289 | { |
3290 | return (!_M_is_leaked() && __alloc1 == __alloc2) |
3291 | ? _M_refcopy() : _M_clone(__alloc1); |
3292 | } |
3293 | |
3294 | // Create & Destroy |
3295 | static _Rep* |
3296 | _S_create(size_type, size_type, const _Alloc&); |
3297 | |
3298 | void |
3299 | _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT |
3300 | { |
3301 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3302 | if (__builtin_expect(this != &_S_empty_rep(), false)) |
3303 | #endif |
3304 | { |
3305 | // Be race-detector-friendly. For more info see bits/c++config. |
3306 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount); |
3307 | // Decrement of _M_refcount is acq_rel, because: |
3308 | // - all but last decrements need to release to synchronize with |
3309 | // the last decrement that will delete the object. |
3310 | // - the last decrement needs to acquire to synchronize with |
3311 | // all the previous decrements. |
3312 | // - last but one decrement needs to release to synchronize with |
3313 | // the acquire load in _M_is_shared that will conclude that |
3314 | // the object is not shared anymore. |
3315 | if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, |
3316 | -1) <= 0) |
3317 | { |
3318 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount); |
3319 | _M_destroy(__a); |
3320 | } |
3321 | } |
3322 | } // XXX MT |
3323 | |
3324 | void |
3325 | _M_destroy(const _Alloc&) throw(); |
3326 | |
3327 | _CharT* |
3328 | _M_refcopy() throw() |
3329 | { |
3330 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3331 | if (__builtin_expect(this != &_S_empty_rep(), false)) |
3332 | #endif |
3333 | __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); |
3334 | return _M_refdata(); |
3335 | } // XXX MT |
3336 | |
3337 | _CharT* |
3338 | _M_clone(const _Alloc&, size_type __res = 0); |
3339 | }; |
3340 | |
3341 | // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
3342 | struct _Alloc_hider : _Alloc |
3343 | { |
3344 | _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT |
3345 | : _Alloc(__a), _M_p(__dat) { } |
3346 | |
3347 | _CharT* _M_p; // The actual data. |
3348 | }; |
3349 | |
3350 | public: |
3351 | // Data Members (public): |
3352 | // NB: This is an unsigned type, and thus represents the maximum |
3353 | // size that the allocator can hold. |
3354 | /// Value returned by various member functions when they fail. |
3355 | static const size_type npos = static_cast<size_type>(-1); |
3356 | |
3357 | private: |
3358 | // Data Members (private): |
3359 | mutable _Alloc_hider _M_dataplus; |
3360 | |
3361 | _CharT* |
3362 | _M_data() const _GLIBCXX_NOEXCEPT |
3363 | { return _M_dataplus._M_p; } |
3364 | |
3365 | _CharT* |
3366 | _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT |
3367 | { return (_M_dataplus._M_p = __p); } |
3368 | |
3369 | _Rep* |
3370 | _M_rep() const _GLIBCXX_NOEXCEPT |
3371 | { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } |
3372 | |
3373 | // For the internal use we have functions similar to `begin'/`end' |
3374 | // but they do not call _M_leak. |
3375 | iterator |
3376 | _M_ibegin() const _GLIBCXX_NOEXCEPT |
3377 | { return iterator(_M_data()); } |
3378 | |
3379 | iterator |
3380 | _M_iend() const _GLIBCXX_NOEXCEPT |
3381 | { return iterator(_M_data() + this->size()); } |
3382 | |
3383 | void |
3384 | _M_leak() // for use in begin() & non-const op[] |
3385 | { |
3386 | if (!_M_rep()->_M_is_leaked()) |
3387 | _M_leak_hard(); |
3388 | } |
3389 | |
3390 | size_type |
3391 | _M_check(size_type __pos, const char* __s) const |
3392 | { |
3393 | if (__pos > this->size()) |
3394 | __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " |
3395 | "this->size() (which is %zu)" ), |
3396 | __s, __pos, this->size()); |
3397 | return __pos; |
3398 | } |
3399 | |
3400 | void |
3401 | _M_check_length(size_type __n1, size_type __n2, const char* __s) const |
3402 | { |
3403 | if (this->max_size() - (this->size() - __n1) < __n2) |
3404 | __throw_length_error(__N(__s)); |
3405 | } |
3406 | |
3407 | // NB: _M_limit doesn't check for a bad __pos value. |
3408 | size_type |
3409 | _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT |
3410 | { |
3411 | const bool __testoff = __off < this->size() - __pos; |
3412 | return __testoff ? __off : this->size() - __pos; |
3413 | } |
3414 | |
3415 | // True if _Rep and source do not overlap. |
3416 | bool |
3417 | _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT |
3418 | { |
3419 | return (less<const _CharT*>()(__s, _M_data()) |
3420 | || less<const _CharT*>()(_M_data() + this->size(), __s)); |
3421 | } |
3422 | |
3423 | // When __n = 1 way faster than the general multichar |
3424 | // traits_type::copy/move/assign. |
3425 | static void |
3426 | _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT |
3427 | { |
3428 | if (__n == 1) |
3429 | traits_type::assign(*__d, *__s); |
3430 | else |
3431 | traits_type::copy(__d, __s, __n); |
3432 | } |
3433 | |
3434 | static void |
3435 | _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT |
3436 | { |
3437 | if (__n == 1) |
3438 | traits_type::assign(*__d, *__s); |
3439 | else |
3440 | traits_type::move(__d, __s, __n); |
3441 | } |
3442 | |
3443 | static void |
3444 | _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT |
3445 | { |
3446 | if (__n == 1) |
3447 | traits_type::assign(*__d, __c); |
3448 | else |
3449 | traits_type::assign(__d, __n, __c); |
3450 | } |
3451 | |
3452 | // _S_copy_chars is a separate template to permit specialization |
3453 | // to optimize for the common case of pointers as iterators. |
3454 | template<class _Iterator> |
3455 | static void |
3456 | _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) |
3457 | { |
3458 | for (; __k1 != __k2; ++__k1, (void)++__p) |
3459 | traits_type::assign(*__p, *__k1); // These types are off. |
3460 | } |
3461 | |
3462 | static void |
3463 | _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT |
3464 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
3465 | |
3466 | static void |
3467 | _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) |
3468 | _GLIBCXX_NOEXCEPT |
3469 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
3470 | |
3471 | static void |
3472 | _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT |
3473 | { _M_copy(__p, __k1, __k2 - __k1); } |
3474 | |
3475 | static void |
3476 | _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) |
3477 | _GLIBCXX_NOEXCEPT |
3478 | { _M_copy(__p, __k1, __k2 - __k1); } |
3479 | |
3480 | static int |
3481 | _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT |
3482 | { |
3483 | const difference_type __d = difference_type(__n1 - __n2); |
3484 | |
3485 | if (__d > __gnu_cxx::__numeric_traits<int>::__max) |
3486 | return __gnu_cxx::__numeric_traits<int>::__max; |
3487 | else if (__d < __gnu_cxx::__numeric_traits<int>::__min) |
3488 | return __gnu_cxx::__numeric_traits<int>::__min; |
3489 | else |
3490 | return int(__d); |
3491 | } |
3492 | |
3493 | void |
3494 | _M_mutate(size_type __pos, size_type __len1, size_type __len2); |
3495 | |
3496 | void |
3497 | _M_leak_hard(); |
3498 | |
3499 | static _Rep& |
3500 | _S_empty_rep() _GLIBCXX_NOEXCEPT |
3501 | { return _Rep::_S_empty_rep(); } |
3502 | |
3503 | #if __cplusplus >= 201703L |
3504 | // A helper type for avoiding boiler-plate. |
3505 | typedef basic_string_view<_CharT, _Traits> __sv_type; |
3506 | |
3507 | template<typename _Tp, typename _Res> |
3508 | using _If_sv = enable_if_t< |
3509 | __and_<is_convertible<const _Tp&, __sv_type>, |
3510 | __not_<is_convertible<const _Tp*, const basic_string*>>, |
3511 | __not_<is_convertible<const _Tp&, const _CharT*>>>::value, |
3512 | _Res>; |
3513 | |
3514 | // Allows an implicit conversion to __sv_type. |
3515 | static __sv_type |
3516 | _S_to_string_view(__sv_type __svt) noexcept |
3517 | { return __svt; } |
3518 | |
3519 | // Wraps a string_view by explicit conversion and thus |
3520 | // allows to add an internal constructor that does not |
3521 | // participate in overload resolution when a string_view |
3522 | // is provided. |
3523 | struct __sv_wrapper |
3524 | { |
3525 | explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } |
3526 | __sv_type _M_sv; |
3527 | }; |
3528 | |
3529 | /** |
3530 | * @brief Only internally used: Construct string from a string view |
3531 | * wrapper. |
3532 | * @param __svw string view wrapper. |
3533 | * @param __a Allocator to use. |
3534 | */ |
3535 | explicit |
3536 | basic_string(__sv_wrapper __svw, const _Alloc& __a) |
3537 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } |
3538 | #endif |
3539 | |
3540 | public: |
3541 | // Construct/copy/destroy: |
3542 | // NB: We overload ctors in some cases instead of using default |
3543 | // arguments, per 17.4.4.4 para. 2 item 2. |
3544 | |
3545 | /** |
3546 | * @brief Default constructor creates an empty string. |
3547 | */ |
3548 | basic_string() |
3549 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3550 | _GLIBCXX_NOEXCEPT |
3551 | : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) |
3552 | #else |
3553 | : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) |
3554 | #endif |
3555 | { } |
3556 | |
3557 | /** |
3558 | * @brief Construct an empty string using allocator @a a. |
3559 | */ |
3560 | explicit |
3561 | basic_string(const _Alloc& __a); |
3562 | |
3563 | // NB: per LWG issue 42, semantics different from IS: |
3564 | /** |
3565 | * @brief Construct string with copy of value of @a str. |
3566 | * @param __str Source string. |
3567 | */ |
3568 | basic_string(const basic_string& __str); |
3569 | |
3570 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
3571 | // 2583. no way to supply an allocator for basic_string(str, pos) |
3572 | /** |
3573 | * @brief Construct string as copy of a substring. |
3574 | * @param __str Source string. |
3575 | * @param __pos Index of first character to copy from. |
3576 | * @param __a Allocator to use. |
3577 | */ |
3578 | basic_string(const basic_string& __str, size_type __pos, |
3579 | const _Alloc& __a = _Alloc()); |
3580 | |
3581 | /** |
3582 | * @brief Construct string as copy of a substring. |
3583 | * @param __str Source string. |
3584 | * @param __pos Index of first character to copy from. |
3585 | * @param __n Number of characters to copy. |
3586 | */ |
3587 | basic_string(const basic_string& __str, size_type __pos, |
3588 | size_type __n); |
3589 | /** |
3590 | * @brief Construct string as copy of a substring. |
3591 | * @param __str Source string. |
3592 | * @param __pos Index of first character to copy from. |
3593 | * @param __n Number of characters to copy. |
3594 | * @param __a Allocator to use. |
3595 | */ |
3596 | basic_string(const basic_string& __str, size_type __pos, |
3597 | size_type __n, const _Alloc& __a); |
3598 | |
3599 | /** |
3600 | * @brief Construct string initialized by a character %array. |
3601 | * @param __s Source character %array. |
3602 | * @param __n Number of characters to copy. |
3603 | * @param __a Allocator to use (default is default allocator). |
3604 | * |
3605 | * NB: @a __s must have at least @a __n characters, '\\0' |
3606 | * has no special meaning. |
3607 | */ |
3608 | basic_string(const _CharT* __s, size_type __n, |
3609 | const _Alloc& __a = _Alloc()); |
3610 | /** |
3611 | * @brief Construct string as copy of a C string. |
3612 | * @param __s Source C string. |
3613 | * @param __a Allocator to use (default is default allocator). |
3614 | */ |
3615 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); |
3616 | /** |
3617 | * @brief Construct string as multiple characters. |
3618 | * @param __n Number of characters. |
3619 | * @param __c Character to use. |
3620 | * @param __a Allocator to use (default is default allocator). |
3621 | */ |
3622 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); |
3623 | |
3624 | #if __cplusplus >= 201103L |
3625 | /** |
3626 | * @brief Move construct string. |
3627 | * @param __str Source string. |
3628 | * |
3629 | * The newly-created string contains the exact contents of @a __str. |
3630 | * @a __str is a valid, but unspecified string. |
3631 | **/ |
3632 | basic_string(basic_string&& __str) |
3633 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3634 | noexcept // FIXME C++11: should always be noexcept. |
3635 | #endif |
3636 | : _M_dataplus(std::move(__str._M_dataplus)) |
3637 | { |
3638 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3639 | __str._M_data(_S_empty_rep()._M_refdata()); |
3640 | #else |
3641 | __str._M_data(_S_construct(size_type(), _CharT(), get_allocator())); |
3642 | #endif |
3643 | } |
3644 | |
3645 | /** |
3646 | * @brief Construct string from an initializer %list. |
3647 | * @param __l std::initializer_list of characters. |
3648 | * @param __a Allocator to use (default is default allocator). |
3649 | */ |
3650 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()); |
3651 | |
3652 | basic_string(const basic_string& __str, const _Alloc& __a) |
3653 | : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a) |
3654 | { } |
3655 | |
3656 | basic_string(basic_string&& __str, const _Alloc& __a) |
3657 | : _M_dataplus(__str._M_data(), __a) |
3658 | { |
3659 | if (__a == __str.get_allocator()) |
3660 | { |
3661 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3662 | __str._M_data(_S_empty_rep()._M_refdata()); |
3663 | #else |
3664 | __str._M_data(_S_construct(size_type(), _CharT(), __a)); |
3665 | #endif |
3666 | } |
3667 | else |
3668 | _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a); |
3669 | } |
3670 | #endif // C++11 |
3671 | |
3672 | /** |
3673 | * @brief Construct string as copy of a range. |
3674 | * @param __beg Start of range. |
3675 | * @param __end End of range. |
3676 | * @param __a Allocator to use (default is default allocator). |
3677 | */ |
3678 | template<class _InputIterator> |
3679 | basic_string(_InputIterator __beg, _InputIterator __end, |
3680 | const _Alloc& __a = _Alloc()); |
3681 | |
3682 | #if __cplusplus >= 201703L |
3683 | /** |
3684 | * @brief Construct string from a substring of a string_view. |
3685 | * @param __t Source object convertible to string view. |
3686 | * @param __pos The index of the first character to copy from __t. |
3687 | * @param __n The number of characters to copy from __t. |
3688 | * @param __a Allocator to use. |
3689 | */ |
3690 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
3691 | basic_string(const _Tp& __t, size_type __pos, size_type __n, |
3692 | const _Alloc& __a = _Alloc()) |
3693 | : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |
3694 | |
3695 | /** |
3696 | * @brief Construct string from a string_view. |
3697 | * @param __t Source object convertible to string view. |
3698 | * @param __a Allocator to use (default is default allocator). |
3699 | */ |
3700 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
3701 | explicit |
3702 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) |
3703 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } |
3704 | #endif // C++17 |
3705 | |
3706 | /** |
3707 | * @brief Destroy the string instance. |
3708 | */ |
3709 | ~basic_string() _GLIBCXX_NOEXCEPT |
3710 | { _M_rep()->_M_dispose(this->get_allocator()); } |
3711 | |
3712 | /** |
3713 | * @brief Assign the value of @a str to this string. |
3714 | * @param __str Source string. |
3715 | */ |
3716 | basic_string& |
3717 | operator=(const basic_string& __str) |
3718 | { return this->assign(__str); } |
3719 | |
3720 | /** |
3721 | * @brief Copy contents of @a s into this string. |
3722 | * @param __s Source null-terminated string. |
3723 | */ |
3724 | basic_string& |
3725 | operator=(const _CharT* __s) |
3726 | { return this->assign(__s); } |
3727 | |
3728 | /** |
3729 | * @brief Set value to string of length 1. |
3730 | * @param __c Source character. |
3731 | * |
3732 | * Assigning to a character makes this string length 1 and |
3733 | * (*this)[0] == @a c. |
3734 | */ |
3735 | basic_string& |
3736 | operator=(_CharT __c) |
3737 | { |
3738 | this->assign(1, __c); |
3739 | return *this; |
3740 | } |
3741 | |
3742 | #if __cplusplus >= 201103L |
3743 | /** |
3744 | * @brief Move assign the value of @a str to this string. |
3745 | * @param __str Source string. |
3746 | * |
3747 | * The contents of @a str are moved into this string (without copying). |
3748 | * @a str is a valid, but unspecified string. |
3749 | **/ |
3750 | basic_string& |
3751 | operator=(basic_string&& __str) |
3752 | _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value) |
3753 | { |
3754 | // NB: DR 1204. |
3755 | this->swap(__str); |
3756 | return *this; |
3757 | } |
3758 | |
3759 | /** |
3760 | * @brief Set value to string constructed from initializer %list. |
3761 | * @param __l std::initializer_list. |
3762 | */ |
3763 | basic_string& |
3764 | operator=(initializer_list<_CharT> __l) |
3765 | { |
3766 | this->assign(__l.begin(), __l.size()); |
3767 | return *this; |
3768 | } |
3769 | #endif // C++11 |
3770 | |
3771 | #if __cplusplus >= 201703L |
3772 | /** |
3773 | * @brief Set value to string constructed from a string_view. |
3774 | * @param __svt An object convertible to string_view. |
3775 | */ |
3776 | template<typename _Tp> |
3777 | _If_sv<_Tp, basic_string&> |
3778 | operator=(const _Tp& __svt) |
3779 | { return this->assign(__svt); } |
3780 | |
3781 | /** |
3782 | * @brief Convert to a string_view. |
3783 | * @return A string_view. |
3784 | */ |
3785 | operator __sv_type() const noexcept |
3786 | { return __sv_type(data(), size()); } |
3787 | #endif // C++17 |
3788 | |
3789 | // Iterators: |
3790 | /** |
3791 | * Returns a read/write iterator that points to the first character in |
3792 | * the %string. Unshares the string. |
3793 | */ |
3794 | iterator |
3795 | begin() // FIXME C++11: should be noexcept. |
3796 | { |
3797 | _M_leak(); |
3798 | return iterator(_M_data()); |
3799 | } |
3800 | |
3801 | /** |
3802 | * Returns a read-only (constant) iterator that points to the first |
3803 | * character in the %string. |
3804 | */ |
3805 | const_iterator |
3806 | begin() const _GLIBCXX_NOEXCEPT |
3807 | { return const_iterator(_M_data()); } |
3808 | |
3809 | /** |
3810 | * Returns a read/write iterator that points one past the last |
3811 | * character in the %string. Unshares the string. |
3812 | */ |
3813 | iterator |
3814 | end() // FIXME C++11: should be noexcept. |
3815 | { |
3816 | _M_leak(); |
3817 | return iterator(_M_data() + this->size()); |
3818 | } |
3819 | |
3820 | /** |
3821 | * Returns a read-only (constant) iterator that points one past the |
3822 | * last character in the %string. |
3823 | */ |
3824 | const_iterator |
3825 | end() const _GLIBCXX_NOEXCEPT |
3826 | { return const_iterator(_M_data() + this->size()); } |
3827 | |
3828 | /** |
3829 | * Returns a read/write reverse iterator that points to the last |
3830 | * character in the %string. Iteration is done in reverse element |
3831 | * order. Unshares the string. |
3832 | */ |
3833 | reverse_iterator |
3834 | rbegin() // FIXME C++11: should be noexcept. |
3835 | { return reverse_iterator(this->end()); } |
3836 | |
3837 | /** |
3838 | * Returns a read-only (constant) reverse iterator that points |
3839 | * to the last character in the %string. Iteration is done in |
3840 | * reverse element order. |
3841 | */ |
3842 | const_reverse_iterator |
3843 | rbegin() const _GLIBCXX_NOEXCEPT |
3844 | { return const_reverse_iterator(this->end()); } |
3845 | |
3846 | /** |
3847 | * Returns a read/write reverse iterator that points to one before the |
3848 | * first character in the %string. Iteration is done in reverse |
3849 | * element order. Unshares the string. |
3850 | */ |
3851 | reverse_iterator |
3852 | rend() // FIXME C++11: should be noexcept. |
3853 | { return reverse_iterator(this->begin()); } |
3854 | |
3855 | /** |
3856 | * Returns a read-only (constant) reverse iterator that points |
3857 | * to one before the first character in the %string. Iteration |
3858 | * is done in reverse element order. |
3859 | */ |
3860 | const_reverse_iterator |
3861 | rend() const _GLIBCXX_NOEXCEPT |
3862 | { return const_reverse_iterator(this->begin()); } |
3863 | |
3864 | #if __cplusplus >= 201103L |
3865 | /** |
3866 | * Returns a read-only (constant) iterator that points to the first |
3867 | * character in the %string. |
3868 | */ |
3869 | const_iterator |
3870 | cbegin() const noexcept |
3871 | { return const_iterator(this->_M_data()); } |
3872 | |
3873 | /** |
3874 | * Returns a read-only (constant) iterator that points one past the |
3875 | * last character in the %string. |
3876 | */ |
3877 | const_iterator |
3878 | cend() const noexcept |
3879 | { return const_iterator(this->_M_data() + this->size()); } |
3880 | |
3881 | /** |
3882 | * Returns a read-only (constant) reverse iterator that points |
3883 | * to the last character in the %string. Iteration is done in |
3884 | * reverse element order. |
3885 | */ |
3886 | const_reverse_iterator |
3887 | crbegin() const noexcept |
3888 | { return const_reverse_iterator(this->end()); } |
3889 | |
3890 | /** |
3891 | * Returns a read-only (constant) reverse iterator that points |
3892 | * to one before the first character in the %string. Iteration |
3893 | * is done in reverse element order. |
3894 | */ |
3895 | const_reverse_iterator |
3896 | crend() const noexcept |
3897 | { return const_reverse_iterator(this->begin()); } |
3898 | #endif |
3899 | |
3900 | public: |
3901 | // Capacity: |
3902 | /// Returns the number of characters in the string, not including any |
3903 | /// null-termination. |
3904 | size_type |
3905 | size() const _GLIBCXX_NOEXCEPT |
3906 | { return _M_rep()->_M_length; } |
3907 | |
3908 | /// Returns the number of characters in the string, not including any |
3909 | /// null-termination. |
3910 | size_type |
3911 | length() const _GLIBCXX_NOEXCEPT |
3912 | { return _M_rep()->_M_length; } |
3913 | |
3914 | /// Returns the size() of the largest possible %string. |
3915 | size_type |
3916 | max_size() const _GLIBCXX_NOEXCEPT |
3917 | { return _Rep::_S_max_size; } |
3918 | |
3919 | /** |
3920 | * @brief Resizes the %string to the specified number of characters. |
3921 | * @param __n Number of characters the %string should contain. |
3922 | * @param __c Character to fill any new elements. |
3923 | * |
3924 | * This function will %resize the %string to the specified |
3925 | * number of characters. If the number is smaller than the |
3926 | * %string's current size the %string is truncated, otherwise |
3927 | * the %string is extended and new elements are %set to @a __c. |
3928 | */ |
3929 | void |
3930 | resize(size_type __n, _CharT __c); |
3931 | |
3932 | /** |
3933 | * @brief Resizes the %string to the specified number of characters. |
3934 | * @param __n Number of characters the %string should contain. |
3935 | * |
3936 | * This function will resize the %string to the specified length. If |
3937 | * the new size is smaller than the %string's current size the %string |
3938 | * is truncated, otherwise the %string is extended and new characters |
3939 | * are default-constructed. For basic types such as char, this means |
3940 | * setting them to 0. |
3941 | */ |
3942 | void |
3943 | resize(size_type __n) |
3944 | { this->resize(__n, _CharT()); } |
3945 | |
3946 | #if __cplusplus >= 201103L |
3947 | /// A non-binding request to reduce capacity() to size(). |
3948 | void |
3949 | shrink_to_fit() _GLIBCXX_NOEXCEPT |
3950 | { |
3951 | #if __cpp_exceptions |
3952 | if (capacity() > size()) |
3953 | { |
3954 | try |
3955 | { reserve(0); } |
3956 | catch(...) |
3957 | { } |
3958 | } |
3959 | #endif |
3960 | } |
3961 | #endif |
3962 | |
3963 | /** |
3964 | * Returns the total number of characters that the %string can hold |
3965 | * before needing to allocate more memory. |
3966 | */ |
3967 | size_type |
3968 | capacity() const _GLIBCXX_NOEXCEPT |
3969 | { return _M_rep()->_M_capacity; } |
3970 | |
3971 | /** |
3972 | * @brief Attempt to preallocate enough memory for specified number of |
3973 | * characters. |
3974 | * @param __res_arg Number of characters required. |
3975 | * @throw std::length_error If @a __res_arg exceeds @c max_size(). |
3976 | * |
3977 | * This function attempts to reserve enough memory for the |
3978 | * %string to hold the specified number of characters. If the |
3979 | * number requested is more than max_size(), length_error is |
3980 | * thrown. |
3981 | * |
3982 | * The advantage of this function is that if optimal code is a |
3983 | * necessity and the user can determine the string length that will be |
3984 | * required, the user can reserve the memory in %advance, and thus |
3985 | * prevent a possible reallocation of memory and copying of %string |
3986 | * data. |
3987 | */ |
3988 | void |
3989 | reserve(size_type __res_arg = 0); |
3990 | |
3991 | /** |
3992 | * Erases the string, making it empty. |
3993 | */ |
3994 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
3995 | void |
3996 | clear() _GLIBCXX_NOEXCEPT |
3997 | { |
3998 | if (_M_rep()->_M_is_shared()) |
3999 | { |
4000 | _M_rep()->_M_dispose(this->get_allocator()); |
4001 | _M_data(_S_empty_rep()._M_refdata()); |
4002 | } |
4003 | else |
4004 | _M_rep()->_M_set_length_and_sharable(0); |
4005 | } |
4006 | #else |
4007 | // PR 56166: this should not throw. |
4008 | void |
4009 | clear() |
4010 | { _M_mutate(0, this->size(), 0); } |
4011 | #endif |
4012 | |
4013 | /** |
4014 | * Returns true if the %string is empty. Equivalent to |
4015 | * <code>*this == ""</code>. |
4016 | */ |
4017 | _GLIBCXX_NODISCARD bool |
4018 | empty() const _GLIBCXX_NOEXCEPT |
4019 | { return this->size() == 0; } |
4020 | |
4021 | // Element access: |
4022 | /** |
4023 | * @brief Subscript access to the data contained in the %string. |
4024 | * @param __pos The index of the character to access. |
4025 | * @return Read-only (constant) reference to the character. |
4026 | * |
4027 | * This operator allows for easy, array-style, data access. |
4028 | * Note that data access with this operator is unchecked and |
4029 | * out_of_range lookups are not defined. (For checked lookups |
4030 | * see at().) |
4031 | */ |
4032 | const_reference |
4033 | operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT |
4034 | { |
4035 | __glibcxx_assert(__pos <= size()); |
4036 | return _M_data()[__pos]; |
4037 | } |
4038 | |
4039 | /** |
4040 | * @brief Subscript access to the data contained in the %string. |
4041 | * @param __pos The index of the character to access. |
4042 | * @return Read/write reference to the character. |
4043 | * |
4044 | * This operator allows for easy, array-style, data access. |
4045 | * Note that data access with this operator is unchecked and |
4046 | * out_of_range lookups are not defined. (For checked lookups |
4047 | * see at().) Unshares the string. |
4048 | */ |
4049 | reference |
4050 | operator[](size_type __pos) |
4051 | { |
4052 | // Allow pos == size() both in C++98 mode, as v3 extension, |
4053 | // and in C++11 mode. |
4054 | __glibcxx_assert(__pos <= size()); |
4055 | // In pedantic mode be strict in C++98 mode. |
4056 | _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); |
4057 | _M_leak(); |
4058 | return _M_data()[__pos]; |
4059 | } |
4060 | |
4061 | /** |
4062 | * @brief Provides access to the data contained in the %string. |
4063 | * @param __n The index of the character to access. |
4064 | * @return Read-only (const) reference to the character. |
4065 | * @throw std::out_of_range If @a n is an invalid index. |
4066 | * |
4067 | * This function provides for safer data access. The parameter is |
4068 | * first checked that it is in the range of the string. The function |
4069 | * throws out_of_range if the check fails. |
4070 | */ |
4071 | const_reference |
4072 | at(size_type __n) const |
4073 | { |
4074 | if (__n >= this->size()) |
4075 | __throw_out_of_range_fmt(__N("basic_string::at: __n " |
4076 | "(which is %zu) >= this->size() " |
4077 | "(which is %zu)" ), |
4078 | __n, this->size()); |
4079 | return _M_data()[__n]; |
4080 | } |
4081 | |
4082 | /** |
4083 | * @brief Provides access to the data contained in the %string. |
4084 | * @param __n The index of the character to access. |
4085 | * @return Read/write reference to the character. |
4086 | * @throw std::out_of_range If @a n is an invalid index. |
4087 | * |
4088 | * This function provides for safer data access. The parameter is |
4089 | * first checked that it is in the range of the string. The function |
4090 | * throws out_of_range if the check fails. Success results in |
4091 | * unsharing the string. |
4092 | */ |
4093 | reference |
4094 | at(size_type __n) |
4095 | { |
4096 | if (__n >= size()) |
4097 | __throw_out_of_range_fmt(__N("basic_string::at: __n " |
4098 | "(which is %zu) >= this->size() " |
4099 | "(which is %zu)" ), |
4100 | __n, this->size()); |
4101 | _M_leak(); |
4102 | return _M_data()[__n]; |
4103 | } |
4104 | |
4105 | #if __cplusplus >= 201103L |
4106 | /** |
4107 | * Returns a read/write reference to the data at the first |
4108 | * element of the %string. |
4109 | */ |
4110 | reference |
4111 | front() |
4112 | { |
4113 | __glibcxx_assert(!empty()); |
4114 | return operator[](0); |
4115 | } |
4116 | |
4117 | /** |
4118 | * Returns a read-only (constant) reference to the data at the first |
4119 | * element of the %string. |
4120 | */ |
4121 | const_reference |
4122 | front() const noexcept |
4123 | { |
4124 | __glibcxx_assert(!empty()); |
4125 | return operator[](0); |
4126 | } |
4127 | |
4128 | /** |
4129 | * Returns a read/write reference to the data at the last |
4130 | * element of the %string. |
4131 | */ |
4132 | reference |
4133 | back() |
4134 | { |
4135 | __glibcxx_assert(!empty()); |
4136 | return operator[](this->size() - 1); |
4137 | } |
4138 | |
4139 | /** |
4140 | * Returns a read-only (constant) reference to the data at the |
4141 | * last element of the %string. |
4142 | */ |
4143 | const_reference |
4144 | back() const noexcept |
4145 | { |
4146 | __glibcxx_assert(!empty()); |
4147 | return operator[](this->size() - 1); |
4148 | } |
4149 | #endif |
4150 | |
4151 | // Modifiers: |
4152 | /** |
4153 | * @brief Append a string to this string. |
4154 | * @param __str The string to append. |
4155 | * @return Reference to this string. |
4156 | */ |
4157 | basic_string& |
4158 | operator+=(const basic_string& __str) |
4159 | { return this->append(__str); } |
4160 | |
4161 | /** |
4162 | * @brief Append a C string. |
4163 | * @param __s The C string to append. |
4164 | * @return Reference to this string. |
4165 | */ |
4166 | basic_string& |
4167 | operator+=(const _CharT* __s) |
4168 | { return this->append(__s); } |
4169 | |
4170 | /** |
4171 | * @brief Append a character. |
4172 | * @param __c The character to append. |
4173 | * @return Reference to this string. |
4174 | */ |
4175 | basic_string& |
4176 | operator+=(_CharT __c) |
4177 | { |
4178 | this->push_back(__c); |
4179 | return *this; |
4180 | } |
4181 | |
4182 | #if __cplusplus >= 201103L |
4183 | /** |
4184 | * @brief Append an initializer_list of characters. |
4185 | * @param __l The initializer_list of characters to be appended. |
4186 | * @return Reference to this string. |
4187 | */ |
4188 | basic_string& |
4189 | operator+=(initializer_list<_CharT> __l) |
4190 | { return this->append(__l.begin(), __l.size()); } |
4191 | #endif // C++11 |
4192 | |
4193 | #if __cplusplus >= 201703L |
4194 | /** |
4195 | * @brief Append a string_view. |
4196 | * @param __svt The object convertible to string_view to be appended. |
4197 | * @return Reference to this string. |
4198 | */ |
4199 | template<typename _Tp> |
4200 | _If_sv<_Tp, basic_string&> |
4201 | operator+=(const _Tp& __svt) |
4202 | { return this->append(__svt); } |
4203 | #endif // C++17 |
4204 | |
4205 | /** |
4206 | * @brief Append a string to this string. |
4207 | * @param __str The string to append. |
4208 | * @return Reference to this string. |
4209 | */ |
4210 | basic_string& |
4211 | append(const basic_string& __str); |
4212 | |
4213 | /** |
4214 | * @brief Append a substring. |
4215 | * @param __str The string to append. |
4216 | * @param __pos Index of the first character of str to append. |
4217 | * @param __n The number of characters to append. |
4218 | * @return Reference to this string. |
4219 | * @throw std::out_of_range if @a __pos is not a valid index. |
4220 | * |
4221 | * This function appends @a __n characters from @a __str |
4222 | * starting at @a __pos to this string. If @a __n is is larger |
4223 | * than the number of available characters in @a __str, the |
4224 | * remainder of @a __str is appended. |
4225 | */ |
4226 | basic_string& |
4227 | append(const basic_string& __str, size_type __pos, size_type __n = npos); |
4228 | |
4229 | /** |
4230 | * @brief Append a C substring. |
4231 | * @param __s The C string to append. |
4232 | * @param __n The number of characters to append. |
4233 | * @return Reference to this string. |
4234 | */ |
4235 | basic_string& |
4236 | append(const _CharT* __s, size_type __n); |
4237 | |
4238 | /** |
4239 | * @brief Append a C string. |
4240 | * @param __s The C string to append. |
4241 | * @return Reference to this string. |
4242 | */ |
4243 | basic_string& |
4244 | append(const _CharT* __s) |
4245 | { |
4246 | __glibcxx_requires_string(__s); |
4247 | return this->append(__s, traits_type::length(__s)); |
4248 | } |
4249 | |
4250 | /** |
4251 | * @brief Append multiple characters. |
4252 | * @param __n The number of characters to append. |
4253 | * @param __c The character to use. |
4254 | * @return Reference to this string. |
4255 | * |
4256 | * Appends __n copies of __c to this string. |
4257 | */ |
4258 | basic_string& |
4259 | append(size_type __n, _CharT __c); |
4260 | |
4261 | #if __cplusplus >= 201103L |
4262 | /** |
4263 | * @brief Append an initializer_list of characters. |
4264 | * @param __l The initializer_list of characters to append. |
4265 | * @return Reference to this string. |
4266 | */ |
4267 | basic_string& |
4268 | append(initializer_list<_CharT> __l) |
4269 | { return this->append(__l.begin(), __l.size()); } |
4270 | #endif // C++11 |
4271 | |
4272 | /** |
4273 | * @brief Append a range of characters. |
4274 | * @param __first Iterator referencing the first character to append. |
4275 | * @param __last Iterator marking the end of the range. |
4276 | * @return Reference to this string. |
4277 | * |
4278 | * Appends characters in the range [__first,__last) to this string. |
4279 | */ |
4280 | template<class _InputIterator> |
4281 | basic_string& |
4282 | append(_InputIterator __first, _InputIterator __last) |
4283 | { return this->replace(_M_iend(), _M_iend(), __first, __last); } |
4284 | |
4285 | #if __cplusplus >= 201703L |
4286 | /** |
4287 | * @brief Append a string_view. |
4288 | * @param __svt The object convertible to string_view to be appended. |
4289 | * @return Reference to this string. |
4290 | */ |
4291 | template<typename _Tp> |
4292 | _If_sv<_Tp, basic_string&> |
4293 | append(const _Tp& __svt) |
4294 | { |
4295 | __sv_type __sv = __svt; |
4296 | return this->append(__sv.data(), __sv.size()); |
4297 | } |
4298 | |
4299 | /** |
4300 | * @brief Append a range of characters from a string_view. |
4301 | * @param __svt The object convertible to string_view to be appended |
4302 | * from. |
4303 | * @param __pos The position in the string_view to append from. |
4304 | * @param __n The number of characters to append from the string_view. |
4305 | * @return Reference to this string. |
4306 | */ |
4307 | template<typename _Tp> |
4308 | _If_sv<_Tp, basic_string&> |
4309 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) |
4310 | { |
4311 | __sv_type __sv = __svt; |
4312 | return append(__sv.data() |
4313 | + std::__sv_check(__sv.size(), __pos, "basic_string::append" ), |
4314 | std::__sv_limit(__sv.size(), __pos, __n)); |
4315 | } |
4316 | #endif // C++17 |
4317 | |
4318 | /** |
4319 | * @brief Append a single character. |
4320 | * @param __c Character to append. |
4321 | */ |
4322 | void |
4323 | push_back(_CharT __c) |
4324 | { |
4325 | const size_type __len = 1 + this->size(); |
4326 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) |
4327 | this->reserve(__len); |
4328 | traits_type::assign(_M_data()[this->size()], __c); |
4329 | _M_rep()->_M_set_length_and_sharable(__len); |
4330 | } |
4331 | |
4332 | /** |
4333 | * @brief Set value to contents of another string. |
4334 | * @param __str Source string to use. |
4335 | * @return Reference to this string. |
4336 | */ |
4337 | basic_string& |
4338 | assign(const basic_string& __str); |
4339 | |
4340 | #if __cplusplus >= 201103L |
4341 | /** |
4342 | * @brief Set value to contents of another string. |
4343 | * @param __str Source string to use. |
4344 | * @return Reference to this string. |
4345 | * |
4346 | * This function sets this string to the exact contents of @a __str. |
4347 | * @a __str is a valid, but unspecified string. |
4348 | */ |
4349 | basic_string& |
4350 | assign(basic_string&& __str) |
4351 | noexcept(allocator_traits<_Alloc>::is_always_equal::value) |
4352 | { |
4353 | this->swap(__str); |
4354 | return *this; |
4355 | } |
4356 | #endif // C++11 |
4357 | |
4358 | /** |
4359 | * @brief Set value to a substring of a string. |
4360 | * @param __str The string to use. |
4361 | * @param __pos Index of the first character of str. |
4362 | * @param __n Number of characters to use. |
4363 | * @return Reference to this string. |
4364 | * @throw std::out_of_range if @a pos is not a valid index. |
4365 | * |
4366 | * This function sets this string to the substring of @a __str |
4367 | * consisting of @a __n characters at @a __pos. If @a __n is |
4368 | * is larger than the number of available characters in @a |
4369 | * __str, the remainder of @a __str is used. |
4370 | */ |
4371 | basic_string& |
4372 | assign(const basic_string& __str, size_type __pos, size_type __n = npos) |
4373 | { return this->assign(__str._M_data() |
4374 | + __str._M_check(__pos, "basic_string::assign" ), |
4375 | __str._M_limit(__pos, __n)); } |
4376 | |
4377 | /** |
4378 | * @brief Set value to a C substring. |
4379 | * @param __s The C string to use. |
4380 | * @param __n Number of characters to use. |
4381 | * @return Reference to this string. |
4382 | * |
4383 | * This function sets the value of this string to the first @a __n |
4384 | * characters of @a __s. If @a __n is is larger than the number of |
4385 | * available characters in @a __s, the remainder of @a __s is used. |
4386 | */ |
4387 | basic_string& |
4388 | assign(const _CharT* __s, size_type __n); |
4389 | |
4390 | /** |
4391 | * @brief Set value to contents of a C string. |
4392 | * @param __s The C string to use. |
4393 | * @return Reference to this string. |
4394 | * |
4395 | * This function sets the value of this string to the value of @a __s. |
4396 | * The data is copied, so there is no dependence on @a __s once the |
4397 | * function returns. |
4398 | */ |
4399 | basic_string& |
4400 | assign(const _CharT* __s) |
4401 | { |
4402 | __glibcxx_requires_string(__s); |
4403 | return this->assign(__s, traits_type::length(__s)); |
4404 | } |
4405 | |
4406 | /** |
4407 | * @brief Set value to multiple characters. |
4408 | * @param __n Length of the resulting string. |
4409 | * @param __c The character to use. |
4410 | * @return Reference to this string. |
4411 | * |
4412 | * This function sets the value of this string to @a __n copies of |
4413 | * character @a __c. |
4414 | */ |
4415 | basic_string& |
4416 | assign(size_type __n, _CharT __c) |
4417 | { return _M_replace_aux(size_type(0), this->size(), __n, __c); } |
4418 | |
4419 | /** |
4420 | * @brief Set value to a range of characters. |
4421 | * @param __first Iterator referencing the first character to append. |
4422 | * @param __last Iterator marking the end of the range. |
4423 | * @return Reference to this string. |
4424 | * |
4425 | * Sets value of string to characters in the range [__first,__last). |
4426 | */ |
4427 | template<class _InputIterator> |
4428 | basic_string& |
4429 | assign(_InputIterator __first, _InputIterator __last) |
4430 | { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } |
4431 | |
4432 | #if __cplusplus >= 201103L |
4433 | /** |
4434 | * @brief Set value to an initializer_list of characters. |
4435 | * @param __l The initializer_list of characters to assign. |
4436 | * @return Reference to this string. |
4437 | */ |
4438 | basic_string& |
4439 | assign(initializer_list<_CharT> __l) |
4440 | { return this->assign(__l.begin(), __l.size()); } |
4441 | #endif // C++11 |
4442 | |
4443 | #if __cplusplus >= 201703L |
4444 | /** |
4445 | * @brief Set value from a string_view. |
4446 | * @param __svt The source object convertible to string_view. |
4447 | * @return Reference to this string. |
4448 | */ |
4449 | template<typename _Tp> |
4450 | _If_sv<_Tp, basic_string&> |
4451 | assign(const _Tp& __svt) |
4452 | { |
4453 | __sv_type __sv = __svt; |
4454 | return this->assign(__sv.data(), __sv.size()); |
4455 | } |
4456 | |
4457 | /** |
4458 | * @brief Set value from a range of characters in a string_view. |
4459 | * @param __svt The source object convertible to string_view. |
4460 | * @param __pos The position in the string_view to assign from. |
4461 | * @param __n The number of characters to assign. |
4462 | * @return Reference to this string. |
4463 | */ |
4464 | template<typename _Tp> |
4465 | _If_sv<_Tp, basic_string&> |
4466 | assign(const _Tp& __svt, size_type __pos, size_type __n = npos) |
4467 | { |
4468 | __sv_type __sv = __svt; |
4469 | return assign(__sv.data() |
4470 | + std::__sv_check(__sv.size(), __pos, "basic_string::assign" ), |
4471 | std::__sv_limit(__sv.size(), __pos, __n)); |
4472 | } |
4473 | #endif // C++17 |
4474 | |
4475 | /** |
4476 | * @brief Insert multiple characters. |
4477 | * @param __p Iterator referencing location in string to insert at. |
4478 | * @param __n Number of characters to insert |
4479 | * @param __c The character to insert. |
4480 | * @throw std::length_error If new length exceeds @c max_size(). |
4481 | * |
4482 | * Inserts @a __n copies of character @a __c starting at the |
4483 | * position referenced by iterator @a __p. If adding |
4484 | * characters causes the length to exceed max_size(), |
4485 | * length_error is thrown. The value of the string doesn't |
4486 | * change if an error is thrown. |
4487 | */ |
4488 | void |
4489 | insert(iterator __p, size_type __n, _CharT __c) |
4490 | { this->replace(__p, __p, __n, __c); } |
4491 | |
4492 | /** |
4493 | * @brief Insert a range of characters. |
4494 | * @param __p Iterator referencing location in string to insert at. |
4495 | * @param __beg Start of range. |
4496 | * @param __end End of range. |
4497 | * @throw std::length_error If new length exceeds @c max_size(). |
4498 | * |
4499 | * Inserts characters in range [__beg,__end). If adding |
4500 | * characters causes the length to exceed max_size(), |
4501 | * length_error is thrown. The value of the string doesn't |
4502 | * change if an error is thrown. |
4503 | */ |
4504 | template<class _InputIterator> |
4505 | void |
4506 | insert(iterator __p, _InputIterator __beg, _InputIterator __end) |
4507 | { this->replace(__p, __p, __beg, __end); } |
4508 | |
4509 | #if __cplusplus >= 201103L |
4510 | /** |
4511 | * @brief Insert an initializer_list of characters. |
4512 | * @param __p Iterator referencing location in string to insert at. |
4513 | * @param __l The initializer_list of characters to insert. |
4514 | * @throw std::length_error If new length exceeds @c max_size(). |
4515 | */ |
4516 | void |
4517 | insert(iterator __p, initializer_list<_CharT> __l) |
4518 | { |
4519 | _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); |
4520 | this->insert(__p - _M_ibegin(), __l.begin(), __l.size()); |
4521 | } |
4522 | #endif // C++11 |
4523 | |
4524 | /** |
4525 | * @brief Insert value of a string. |
4526 | * @param __pos1 Iterator referencing location in string to insert at. |
4527 | * @param __str The string to insert. |
4528 | * @return Reference to this string. |
4529 | * @throw std::length_error If new length exceeds @c max_size(). |
4530 | * |
4531 | * Inserts value of @a __str starting at @a __pos1. If adding |
4532 | * characters causes the length to exceed max_size(), |
4533 | * length_error is thrown. The value of the string doesn't |
4534 | * change if an error is thrown. |
4535 | */ |
4536 | basic_string& |
4537 | insert(size_type __pos1, const basic_string& __str) |
4538 | { return this->insert(__pos1, __str, size_type(0), __str.size()); } |
4539 | |
4540 | /** |
4541 | * @brief Insert a substring. |
4542 | * @param __pos1 Iterator referencing location in string to insert at. |
4543 | * @param __str The string to insert. |
4544 | * @param __pos2 Start of characters in str to insert. |
4545 | * @param __n Number of characters to insert. |
4546 | * @return Reference to this string. |
4547 | * @throw std::length_error If new length exceeds @c max_size(). |
4548 | * @throw std::out_of_range If @a pos1 > size() or |
4549 | * @a __pos2 > @a str.size(). |
4550 | * |
4551 | * Starting at @a pos1, insert @a __n character of @a __str |
4552 | * beginning with @a __pos2. If adding characters causes the |
4553 | * length to exceed max_size(), length_error is thrown. If @a |
4554 | * __pos1 is beyond the end of this string or @a __pos2 is |
4555 | * beyond the end of @a __str, out_of_range is thrown. The |
4556 | * value of the string doesn't change if an error is thrown. |
4557 | */ |
4558 | basic_string& |
4559 | insert(size_type __pos1, const basic_string& __str, |
4560 | size_type __pos2, size_type __n = npos) |
4561 | { return this->insert(__pos1, __str._M_data() |
4562 | + __str._M_check(__pos2, "basic_string::insert" ), |
4563 | __str._M_limit(__pos2, __n)); } |
4564 | |
4565 | /** |
4566 | * @brief Insert a C substring. |
4567 | * @param __pos Iterator referencing location in string to insert at. |
4568 | * @param __s The C string to insert. |
4569 | * @param __n The number of characters to insert. |
4570 | * @return Reference to this string. |
4571 | * @throw std::length_error If new length exceeds @c max_size(). |
4572 | * @throw std::out_of_range If @a __pos is beyond the end of this |
4573 | * string. |
4574 | * |
4575 | * Inserts the first @a __n characters of @a __s starting at @a |
4576 | * __pos. If adding characters causes the length to exceed |
4577 | * max_size(), length_error is thrown. If @a __pos is beyond |
4578 | * end(), out_of_range is thrown. The value of the string |
4579 | * doesn't change if an error is thrown. |
4580 | */ |
4581 | basic_string& |
4582 | insert(size_type __pos, const _CharT* __s, size_type __n); |
4583 | |
4584 | /** |
4585 | * @brief Insert a C string. |
4586 | * @param __pos Iterator referencing location in string to insert at. |
4587 | * @param __s The C string to insert. |
4588 | * @return Reference to this string. |
4589 | * @throw std::length_error If new length exceeds @c max_size(). |
4590 | * @throw std::out_of_range If @a pos is beyond the end of this |
4591 | * string. |
4592 | * |
4593 | * Inserts the first @a n characters of @a __s starting at @a __pos. If |
4594 | * adding characters causes the length to exceed max_size(), |
4595 | * length_error is thrown. If @a __pos is beyond end(), out_of_range is |
4596 | * thrown. The value of the string doesn't change if an error is |
4597 | * thrown. |
4598 | */ |
4599 | basic_string& |
4600 | insert(size_type __pos, const _CharT* __s) |
4601 | { |
4602 | __glibcxx_requires_string(__s); |
4603 | return this->insert(__pos, __s, traits_type::length(__s)); |
4604 | } |
4605 | |
4606 | /** |
4607 | * @brief Insert multiple characters. |
4608 | * @param __pos Index in string to insert at. |
4609 | * @param __n Number of characters to insert |
4610 | * @param __c The character to insert. |
4611 | * @return Reference to this string. |
4612 | * @throw std::length_error If new length exceeds @c max_size(). |
4613 | * @throw std::out_of_range If @a __pos is beyond the end of this |
4614 | * string. |
4615 | * |
4616 | * Inserts @a __n copies of character @a __c starting at index |
4617 | * @a __pos. If adding characters causes the length to exceed |
4618 | * max_size(), length_error is thrown. If @a __pos > length(), |
4619 | * out_of_range is thrown. The value of the string doesn't |
4620 | * change if an error is thrown. |
4621 | */ |
4622 | basic_string& |
4623 | insert(size_type __pos, size_type __n, _CharT __c) |
4624 | { return _M_replace_aux(_M_check(__pos, "basic_string::insert" ), |
4625 | size_type(0), __n, __c); } |
4626 | |
4627 | /** |
4628 | * @brief Insert one character. |
4629 | * @param __p Iterator referencing position in string to insert at. |
4630 | * @param __c The character to insert. |
4631 | * @return Iterator referencing newly inserted char. |
4632 | * @throw std::length_error If new length exceeds @c max_size(). |
4633 | * |
4634 | * Inserts character @a __c at position referenced by @a __p. |
4635 | * If adding character causes the length to exceed max_size(), |
4636 | * length_error is thrown. If @a __p is beyond end of string, |
4637 | * out_of_range is thrown. The value of the string doesn't |
4638 | * change if an error is thrown. |
4639 | */ |
4640 | iterator |
4641 | insert(iterator __p, _CharT __c) |
4642 | { |
4643 | _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); |
4644 | const size_type __pos = __p - _M_ibegin(); |
4645 | _M_replace_aux(__pos, size_type(0), size_type(1), __c); |
4646 | _M_rep()->_M_set_leaked(); |
4647 | return iterator(_M_data() + __pos); |
4648 | } |
4649 | |
4650 | #if __cplusplus >= 201703L |
4651 | /** |
4652 | * @brief Insert a string_view. |
4653 | * @param __pos Iterator referencing position in string to insert at. |
4654 | * @param __svt The object convertible to string_view to insert. |
4655 | * @return Reference to this string. |
4656 | */ |
4657 | template<typename _Tp> |
4658 | _If_sv<_Tp, basic_string&> |
4659 | insert(size_type __pos, const _Tp& __svt) |
4660 | { |
4661 | __sv_type __sv = __svt; |
4662 | return this->insert(__pos, __sv.data(), __sv.size()); |
4663 | } |
4664 | |
4665 | /** |
4666 | * @brief Insert a string_view. |
4667 | * @param __pos1 Position in string to insert at. |
4668 | * @param __svt The object convertible to string_view to insert from. |
4669 | * @param __pos2 Position in string_view to insert from. |
4670 | * @param __n The number of characters to insert. |
4671 | * @return Reference to this string. |
4672 | */ |
4673 | template<typename _Tp> |
4674 | _If_sv<_Tp, basic_string&> |
4675 | insert(size_type __pos1, const _Tp& __svt, |
4676 | size_type __pos2, size_type __n = npos) |
4677 | { |
4678 | __sv_type __sv = __svt; |
4679 | return this->replace(__pos1, size_type(0), __sv.data() |
4680 | + std::__sv_check(__sv.size(), __pos2, "basic_string::insert" ), |
4681 | std::__sv_limit(__sv.size(), __pos2, __n)); |
4682 | } |
4683 | #endif // C++17 |
4684 | |
4685 | /** |
4686 | * @brief Remove characters. |
4687 | * @param __pos Index of first character to remove (default 0). |
4688 | * @param __n Number of characters to remove (default remainder). |
4689 | * @return Reference to this string. |
4690 | * @throw std::out_of_range If @a pos is beyond the end of this |
4691 | * string. |
4692 | * |
4693 | * Removes @a __n characters from this string starting at @a |
4694 | * __pos. The length of the string is reduced by @a __n. If |
4695 | * there are < @a __n characters to remove, the remainder of |
4696 | * the string is truncated. If @a __p is beyond end of string, |
4697 | * out_of_range is thrown. The value of the string doesn't |
4698 | * change if an error is thrown. |
4699 | */ |
4700 | basic_string& |
4701 | erase(size_type __pos = 0, size_type __n = npos) |
4702 | { |
4703 | _M_mutate(_M_check(__pos, "basic_string::erase" ), |
4704 | _M_limit(__pos, __n), size_type(0)); |
4705 | return *this; |
4706 | } |
4707 | |
4708 | /** |
4709 | * @brief Remove one character. |
4710 | * @param __position Iterator referencing the character to remove. |
4711 | * @return iterator referencing same location after removal. |
4712 | * |
4713 | * Removes the character at @a __position from this string. The value |
4714 | * of the string doesn't change if an error is thrown. |
4715 | */ |
4716 | iterator |
4717 | erase(iterator __position) |
4718 | { |
4719 | _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() |
4720 | && __position < _M_iend()); |
4721 | const size_type __pos = __position - _M_ibegin(); |
4722 | _M_mutate(__pos, size_type(1), size_type(0)); |
4723 | _M_rep()->_M_set_leaked(); |
4724 | return iterator(_M_data() + __pos); |
4725 | } |
4726 | |
4727 | /** |
4728 | * @brief Remove a range of characters. |
4729 | * @param __first Iterator referencing the first character to remove. |
4730 | * @param __last Iterator referencing the end of the range. |
4731 | * @return Iterator referencing location of first after removal. |
4732 | * |
4733 | * Removes the characters in the range [first,last) from this string. |
4734 | * The value of the string doesn't change if an error is thrown. |
4735 | */ |
4736 | iterator |
4737 | erase(iterator __first, iterator __last); |
4738 | |
4739 | #if __cplusplus >= 201103L |
4740 | /** |
4741 | * @brief Remove the last character. |
4742 | * |
4743 | * The string must be non-empty. |
4744 | */ |
4745 | void |
4746 | pop_back() // FIXME C++11: should be noexcept. |
4747 | { |
4748 | __glibcxx_assert(!empty()); |
4749 | erase(size() - 1, 1); |
4750 | } |
4751 | #endif // C++11 |
4752 | |
4753 | /** |
4754 | * @brief Replace characters with value from another string. |
4755 | * @param __pos Index of first character to replace. |
4756 | * @param __n Number of characters to be replaced. |
4757 | * @param __str String to insert. |
4758 | * @return Reference to this string. |
4759 | * @throw std::out_of_range If @a pos is beyond the end of this |
4760 | * string. |
4761 | * @throw std::length_error If new length exceeds @c max_size(). |
4762 | * |
4763 | * Removes the characters in the range [__pos,__pos+__n) from |
4764 | * this string. In place, the value of @a __str is inserted. |
4765 | * If @a __pos is beyond end of string, out_of_range is thrown. |
4766 | * If the length of the result exceeds max_size(), length_error |
4767 | * is thrown. The value of the string doesn't change if an |
4768 | * error is thrown. |
4769 | */ |
4770 | basic_string& |
4771 | replace(size_type __pos, size_type __n, const basic_string& __str) |
4772 | { return this->replace(__pos, __n, __str._M_data(), __str.size()); } |
4773 | |
4774 | /** |
4775 | * @brief Replace characters with value from another string. |
4776 | * @param __pos1 Index of first character to replace. |
4777 | * @param __n1 Number of characters to be replaced. |
4778 | * @param __str String to insert. |
4779 | * @param __pos2 Index of first character of str to use. |
4780 | * @param __n2 Number of characters from str to use. |
4781 | * @return Reference to this string. |
4782 | * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > |
4783 | * __str.size(). |
4784 | * @throw std::length_error If new length exceeds @c max_size(). |
4785 | * |
4786 | * Removes the characters in the range [__pos1,__pos1 + n) from this |
4787 | * string. In place, the value of @a __str is inserted. If @a __pos is |
4788 | * beyond end of string, out_of_range is thrown. If the length of the |
4789 | * result exceeds max_size(), length_error is thrown. The value of the |
4790 | * string doesn't change if an error is thrown. |
4791 | */ |
4792 | basic_string& |
4793 | replace(size_type __pos1, size_type __n1, const basic_string& __str, |
4794 | size_type __pos2, size_type __n2 = npos) |
4795 | { return this->replace(__pos1, __n1, __str._M_data() |
4796 | + __str._M_check(__pos2, "basic_string::replace" ), |
4797 | __str._M_limit(__pos2, __n2)); } |
4798 | |
4799 | /** |
4800 | * @brief Replace characters with value of a C substring. |
4801 | * @param __pos Index of first character to replace. |
4802 | * @param __n1 Number of characters to be replaced. |
4803 | * @param __s C string to insert. |
4804 | * @param __n2 Number of characters from @a s to use. |
4805 | * @return Reference to this string. |
4806 | * @throw std::out_of_range If @a pos1 > size(). |
4807 | * @throw std::length_error If new length exceeds @c max_size(). |
4808 | * |
4809 | * Removes the characters in the range [__pos,__pos + __n1) |
4810 | * from this string. In place, the first @a __n2 characters of |
4811 | * @a __s are inserted, or all of @a __s if @a __n2 is too large. If |
4812 | * @a __pos is beyond end of string, out_of_range is thrown. If |
4813 | * the length of result exceeds max_size(), length_error is |
4814 | * thrown. The value of the string doesn't change if an error |
4815 | * is thrown. |
4816 | */ |
4817 | basic_string& |
4818 | replace(size_type __pos, size_type __n1, const _CharT* __s, |
4819 | size_type __n2); |
4820 | |
4821 | /** |
4822 | * @brief Replace characters with value of a C string. |
4823 | * @param __pos Index of first character to replace. |
4824 | * @param __n1 Number of characters to be replaced. |
4825 | * @param __s C string to insert. |
4826 | * @return Reference to this string. |
4827 | * @throw std::out_of_range If @a pos > size(). |
4828 | * @throw std::length_error If new length exceeds @c max_size(). |
4829 | * |
4830 | * Removes the characters in the range [__pos,__pos + __n1) |
4831 | * from this string. In place, the characters of @a __s are |
4832 | * inserted. If @a __pos is beyond end of string, out_of_range |
4833 | * is thrown. If the length of result exceeds max_size(), |
4834 | * length_error is thrown. The value of the string doesn't |
4835 | * change if an error is thrown. |
4836 | */ |
4837 | basic_string& |
4838 | replace(size_type __pos, size_type __n1, const _CharT* __s) |
4839 | { |
4840 | __glibcxx_requires_string(__s); |
4841 | return this->replace(__pos, __n1, __s, traits_type::length(__s)); |
4842 | } |
4843 | |
4844 | /** |
4845 | * @brief Replace characters with multiple characters. |
4846 | * @param __pos Index of first character to replace. |
4847 | * @param __n1 Number of characters to be replaced. |
4848 | * @param __n2 Number of characters to insert. |
4849 | * @param __c Character to insert. |
4850 | * @return Reference to this string. |
4851 | * @throw std::out_of_range If @a __pos > size(). |
4852 | * @throw std::length_error If new length exceeds @c max_size(). |
4853 | * |
4854 | * Removes the characters in the range [pos,pos + n1) from this |
4855 | * string. In place, @a __n2 copies of @a __c are inserted. |
4856 | * If @a __pos is beyond end of string, out_of_range is thrown. |
4857 | * If the length of result exceeds max_size(), length_error is |
4858 | * thrown. The value of the string doesn't change if an error |
4859 | * is thrown. |
4860 | */ |
4861 | basic_string& |
4862 | replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) |
4863 | { return _M_replace_aux(_M_check(__pos, "basic_string::replace" ), |
4864 | _M_limit(__pos, __n1), __n2, __c); } |
4865 | |
4866 | /** |
4867 | * @brief Replace range of characters with string. |
4868 | * @param __i1 Iterator referencing start of range to replace. |
4869 | * @param __i2 Iterator referencing end of range to replace. |
4870 | * @param __str String value to insert. |
4871 | * @return Reference to this string. |
4872 | * @throw std::length_error If new length exceeds @c max_size(). |
4873 | * |
4874 | * Removes the characters in the range [__i1,__i2). In place, |
4875 | * the value of @a __str is inserted. If the length of result |
4876 | * exceeds max_size(), length_error is thrown. The value of |
4877 | * the string doesn't change if an error is thrown. |
4878 | */ |
4879 | basic_string& |
4880 | replace(iterator __i1, iterator __i2, const basic_string& __str) |
4881 | { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } |
4882 | |
4883 | /** |
4884 | * @brief Replace range of characters with C substring. |
4885 | * @param __i1 Iterator referencing start of range to replace. |
4886 | * @param __i2 Iterator referencing end of range to replace. |
4887 | * @param __s C string value to insert. |
4888 | * @param __n Number of characters from s to insert. |
4889 | * @return Reference to this string. |
4890 | * @throw std::length_error If new length exceeds @c max_size(). |
4891 | * |
4892 | * Removes the characters in the range [__i1,__i2). In place, |
4893 | * the first @a __n characters of @a __s are inserted. If the |
4894 | * length of result exceeds max_size(), length_error is thrown. |
4895 | * The value of the string doesn't change if an error is |
4896 | * thrown. |
4897 | */ |
4898 | basic_string& |
4899 | replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) |
4900 | { |
4901 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
4902 | && __i2 <= _M_iend()); |
4903 | return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); |
4904 | } |
4905 | |
4906 | /** |
4907 | * @brief Replace range of characters with C string. |
4908 | * @param __i1 Iterator referencing start of range to replace. |
4909 | * @param __i2 Iterator referencing end of range to replace. |
4910 | * @param __s C string value to insert. |
4911 | * @return Reference to this string. |
4912 | * @throw std::length_error If new length exceeds @c max_size(). |
4913 | * |
4914 | * Removes the characters in the range [__i1,__i2). In place, |
4915 | * the characters of @a __s are inserted. If the length of |
4916 | * result exceeds max_size(), length_error is thrown. The |
4917 | * value of the string doesn't change if an error is thrown. |
4918 | */ |
4919 | basic_string& |
4920 | replace(iterator __i1, iterator __i2, const _CharT* __s) |
4921 | { |
4922 | __glibcxx_requires_string(__s); |
4923 | return this->replace(__i1, __i2, __s, traits_type::length(__s)); |
4924 | } |
4925 | |
4926 | /** |
4927 | * @brief Replace range of characters with multiple characters |
4928 | * @param __i1 Iterator referencing start of range to replace. |
4929 | * @param __i2 Iterator referencing end of range to replace. |
4930 | * @param __n Number of characters to insert. |
4931 | * @param __c Character to insert. |
4932 | * @return Reference to this string. |
4933 | * @throw std::length_error If new length exceeds @c max_size(). |
4934 | * |
4935 | * Removes the characters in the range [__i1,__i2). In place, |
4936 | * @a __n copies of @a __c are inserted. If the length of |
4937 | * result exceeds max_size(), length_error is thrown. The |
4938 | * value of the string doesn't change if an error is thrown. |
4939 | */ |
4940 | basic_string& |
4941 | replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) |
4942 | { |
4943 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
4944 | && __i2 <= _M_iend()); |
4945 | return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); |
4946 | } |
4947 | |
4948 | /** |
4949 | * @brief Replace range of characters with range. |
4950 | * @param __i1 Iterator referencing start of range to replace. |
4951 | * @param __i2 Iterator referencing end of range to replace. |
4952 | * @param __k1 Iterator referencing start of range to insert. |
4953 | * @param __k2 Iterator referencing end of range to insert. |
4954 | * @return Reference to this string. |
4955 | * @throw std::length_error If new length exceeds @c max_size(). |
4956 | * |
4957 | * Removes the characters in the range [__i1,__i2). In place, |
4958 | * characters in the range [__k1,__k2) are inserted. If the |
4959 | * length of result exceeds max_size(), length_error is thrown. |
4960 | * The value of the string doesn't change if an error is |
4961 | * thrown. |
4962 | */ |
4963 | template<class _InputIterator> |
4964 | basic_string& |
4965 | replace(iterator __i1, iterator __i2, |
4966 | _InputIterator __k1, _InputIterator __k2) |
4967 | { |
4968 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
4969 | && __i2 <= _M_iend()); |
4970 | __glibcxx_requires_valid_range(__k1, __k2); |
4971 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
4972 | return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); |
4973 | } |
4974 | |
4975 | // Specializations for the common case of pointer and iterator: |
4976 | // useful to avoid the overhead of temporary buffering in _M_replace. |
4977 | basic_string& |
4978 | replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) |
4979 | { |
4980 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
4981 | && __i2 <= _M_iend()); |
4982 | __glibcxx_requires_valid_range(__k1, __k2); |
4983 | return this->replace(__i1 - _M_ibegin(), __i2 - __i1, |
4984 | __k1, __k2 - __k1); |
4985 | } |
4986 | |
4987 | basic_string& |
4988 | replace(iterator __i1, iterator __i2, |
4989 | const _CharT* __k1, const _CharT* __k2) |
4990 | { |
4991 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
4992 | && __i2 <= _M_iend()); |
4993 | __glibcxx_requires_valid_range(__k1, __k2); |
4994 | return this->replace(__i1 - _M_ibegin(), __i2 - __i1, |
4995 | __k1, __k2 - __k1); |
4996 | } |
4997 | |
4998 | basic_string& |
4999 | replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) |
5000 | { |
5001 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
5002 | && __i2 <= _M_iend()); |
5003 | __glibcxx_requires_valid_range(__k1, __k2); |
5004 | return this->replace(__i1 - _M_ibegin(), __i2 - __i1, |
5005 | __k1.base(), __k2 - __k1); |
5006 | } |
5007 | |
5008 | basic_string& |
5009 | replace(iterator __i1, iterator __i2, |
5010 | const_iterator __k1, const_iterator __k2) |
5011 | { |
5012 | _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 |
5013 | && __i2 <= _M_iend()); |
5014 | __glibcxx_requires_valid_range(__k1, __k2); |
5015 | return this->replace(__i1 - _M_ibegin(), __i2 - __i1, |
5016 | __k1.base(), __k2 - __k1); |
5017 | } |
5018 | |
5019 | #if __cplusplus >= 201103L |
5020 | /** |
5021 | * @brief Replace range of characters with initializer_list. |
5022 | * @param __i1 Iterator referencing start of range to replace. |
5023 | * @param __i2 Iterator referencing end of range to replace. |
5024 | * @param __l The initializer_list of characters to insert. |
5025 | * @return Reference to this string. |
5026 | * @throw std::length_error If new length exceeds @c max_size(). |
5027 | * |
5028 | * Removes the characters in the range [__i1,__i2). In place, |
5029 | * characters in the range [__k1,__k2) are inserted. If the |
5030 | * length of result exceeds max_size(), length_error is thrown. |
5031 | * The value of the string doesn't change if an error is |
5032 | * thrown. |
5033 | */ |
5034 | basic_string& replace(iterator __i1, iterator __i2, |
5035 | initializer_list<_CharT> __l) |
5036 | { return this->replace(__i1, __i2, __l.begin(), __l.end()); } |
5037 | #endif // C++11 |
5038 | |
5039 | #if __cplusplus >= 201703L |
5040 | /** |
5041 | * @brief Replace range of characters with string_view. |
5042 | * @param __pos The position to replace at. |
5043 | * @param __n The number of characters to replace. |
5044 | * @param __svt The object convertible to string_view to insert. |
5045 | * @return Reference to this string. |
5046 | */ |
5047 | template<typename _Tp> |
5048 | _If_sv<_Tp, basic_string&> |
5049 | replace(size_type __pos, size_type __n, const _Tp& __svt) |
5050 | { |
5051 | __sv_type __sv = __svt; |
5052 | return this->replace(__pos, __n, __sv.data(), __sv.size()); |
5053 | } |
5054 | |
5055 | /** |
5056 | * @brief Replace range of characters with string_view. |
5057 | * @param __pos1 The position to replace at. |
5058 | * @param __n1 The number of characters to replace. |
5059 | * @param __svt The object convertible to string_view to insert from. |
5060 | * @param __pos2 The position in the string_view to insert from. |
5061 | * @param __n2 The number of characters to insert. |
5062 | * @return Reference to this string. |
5063 | */ |
5064 | template<typename _Tp> |
5065 | _If_sv<_Tp, basic_string&> |
5066 | replace(size_type __pos1, size_type __n1, const _Tp& __svt, |
5067 | size_type __pos2, size_type __n2 = npos) |
5068 | { |
5069 | __sv_type __sv = __svt; |
5070 | return this->replace(__pos1, __n1, |
5071 | __sv.data() |
5072 | + std::__sv_check(__sv.size(), __pos2, "basic_string::replace" ), |
5073 | std::__sv_limit(__sv.size(), __pos2, __n2)); |
5074 | } |
5075 | |
5076 | /** |
5077 | * @brief Replace range of characters with string_view. |
5078 | * @param __i1 An iterator referencing the start position |
5079 | to replace at. |
5080 | * @param __i2 An iterator referencing the end position |
5081 | for the replace. |
5082 | * @param __svt The object convertible to string_view to insert from. |
5083 | * @return Reference to this string. |
5084 | */ |
5085 | template<typename _Tp> |
5086 | _If_sv<_Tp, basic_string&> |
5087 | replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) |
5088 | { |
5089 | __sv_type __sv = __svt; |
5090 | return this->replace(__i1 - begin(), __i2 - __i1, __sv); |
5091 | } |
5092 | #endif // C++17 |
5093 | |
5094 | private: |
5095 | template<class _Integer> |
5096 | basic_string& |
5097 | _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, |
5098 | _Integer __val, __true_type) |
5099 | { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } |
5100 | |
5101 | template<class _InputIterator> |
5102 | basic_string& |
5103 | _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, |
5104 | _InputIterator __k2, __false_type); |
5105 | |
5106 | basic_string& |
5107 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, |
5108 | _CharT __c); |
5109 | |
5110 | basic_string& |
5111 | _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, |
5112 | size_type __n2); |
5113 | |
5114 | // _S_construct_aux is used to implement the 21.3.1 para 15 which |
5115 | // requires special behaviour if _InIter is an integral type |
5116 | template<class _InIterator> |
5117 | static _CharT* |
5118 | _S_construct_aux(_InIterator __beg, _InIterator __end, |
5119 | const _Alloc& __a, __false_type) |
5120 | { |
5121 | typedef typename iterator_traits<_InIterator>::iterator_category _Tag; |
5122 | return _S_construct(__beg, __end, __a, _Tag()); |
5123 | } |
5124 | |
5125 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
5126 | // 438. Ambiguity in the "do the right thing" clause |
5127 | template<class _Integer> |
5128 | static _CharT* |
5129 | _S_construct_aux(_Integer __beg, _Integer __end, |
5130 | const _Alloc& __a, __true_type) |
5131 | { return _S_construct_aux_2(static_cast<size_type>(__beg), |
5132 | __end, __a); } |
5133 | |
5134 | static _CharT* |
5135 | _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a) |
5136 | { return _S_construct(__req, __c, __a); } |
5137 | |
5138 | template<class _InIterator> |
5139 | static _CharT* |
5140 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) |
5141 | { |
5142 | typedef typename std::__is_integer<_InIterator>::__type _Integral; |
5143 | return _S_construct_aux(__beg, __end, __a, _Integral()); |
5144 | } |
5145 | |
5146 | // For Input Iterators, used in istreambuf_iterators, etc. |
5147 | template<class _InIterator> |
5148 | static _CharT* |
5149 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, |
5150 | input_iterator_tag); |
5151 | |
5152 | // For forward_iterators up to random_access_iterators, used for |
5153 | // string::iterator, _CharT*, etc. |
5154 | template<class _FwdIterator> |
5155 | static _CharT* |
5156 | _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, |
5157 | forward_iterator_tag); |
5158 | |
5159 | static _CharT* |
5160 | _S_construct(size_type __req, _CharT __c, const _Alloc& __a); |
5161 | |
5162 | public: |
5163 | |
5164 | /** |
5165 | * @brief Copy substring into C string. |
5166 | * @param __s C string to copy value into. |
5167 | * @param __n Number of characters to copy. |
5168 | * @param __pos Index of first character to copy. |
5169 | * @return Number of characters actually copied |
5170 | * @throw std::out_of_range If __pos > size(). |
5171 | * |
5172 | * Copies up to @a __n characters starting at @a __pos into the |
5173 | * C string @a __s. If @a __pos is %greater than size(), |
5174 | * out_of_range is thrown. |
5175 | */ |
5176 | size_type |
5177 | copy(_CharT* __s, size_type __n, size_type __pos = 0) const; |
5178 | |
5179 | /** |
5180 | * @brief Swap contents with another string. |
5181 | * @param __s String to swap with. |
5182 | * |
5183 | * Exchanges the contents of this string with that of @a __s in constant |
5184 | * time. |
5185 | */ |
5186 | void |
5187 | swap(basic_string& __s) |
5188 | _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value); |
5189 | |
5190 | // String operations: |
5191 | /** |
5192 | * @brief Return const pointer to null-terminated contents. |
5193 | * |
5194 | * This is a handle to internal data. Do not modify or dire things may |
5195 | * happen. |
5196 | */ |
5197 | const _CharT* |
5198 | c_str() const _GLIBCXX_NOEXCEPT |
5199 | { return _M_data(); } |
5200 | |
5201 | /** |
5202 | * @brief Return const pointer to contents. |
5203 | * |
5204 | * This is a pointer to internal data. It is undefined to modify |
5205 | * the contents through the returned pointer. To get a pointer that |
5206 | * allows modifying the contents use @c &str[0] instead, |
5207 | * (or in C++17 the non-const @c str.data() overload). |
5208 | */ |
5209 | const _CharT* |
5210 | data() const _GLIBCXX_NOEXCEPT |
5211 | { return _M_data(); } |
5212 | |
5213 | #if __cplusplus >= 201703L |
5214 | /** |
5215 | * @brief Return non-const pointer to contents. |
5216 | * |
5217 | * This is a pointer to the character sequence held by the string. |
5218 | * Modifying the characters in the sequence is allowed. |
5219 | */ |
5220 | _CharT* |
5221 | data() noexcept |
5222 | { |
5223 | _M_leak(); |
5224 | return _M_data(); |
5225 | } |
5226 | #endif |
5227 | |
5228 | /** |
5229 | * @brief Return copy of allocator used to construct this string. |
5230 | */ |
5231 | allocator_type |
5232 | get_allocator() const _GLIBCXX_NOEXCEPT |
5233 | { return _M_dataplus; } |
5234 | |
5235 | /** |
5236 | * @brief Find position of a C substring. |
5237 | * @param __s C string to locate. |
5238 | * @param __pos Index of character to search from. |
5239 | * @param __n Number of characters from @a s to search for. |
5240 | * @return Index of start of first occurrence. |
5241 | * |
5242 | * Starting from @a __pos, searches forward for the first @a |
5243 | * __n characters in @a __s within this string. If found, |
5244 | * returns the index where it begins. If not found, returns |
5245 | * npos. |
5246 | */ |
5247 | size_type |
5248 | find(const _CharT* __s, size_type __pos, size_type __n) const |
5249 | _GLIBCXX_NOEXCEPT; |
5250 | |
5251 | /** |
5252 | * @brief Find position of a string. |
5253 | * @param __str String to locate. |
5254 | * @param __pos Index of character to search from (default 0). |
5255 | * @return Index of start of first occurrence. |
5256 | * |
5257 | * Starting from @a __pos, searches forward for value of @a __str within |
5258 | * this string. If found, returns the index where it begins. If not |
5259 | * found, returns npos. |
5260 | */ |
5261 | size_type |
5262 | find(const basic_string& __str, size_type __pos = 0) const |
5263 | _GLIBCXX_NOEXCEPT |
5264 | { return this->find(__str.data(), __pos, __str.size()); } |
5265 | |
5266 | /** |
5267 | * @brief Find position of a C string. |
5268 | * @param __s C string to locate. |
5269 | * @param __pos Index of character to search from (default 0). |
5270 | * @return Index of start of first occurrence. |
5271 | * |
5272 | * Starting from @a __pos, searches forward for the value of @a |
5273 | * __s within this string. If found, returns the index where |
5274 | * it begins. If not found, returns npos. |
5275 | */ |
5276 | size_type |
5277 | find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT |
5278 | { |
5279 | __glibcxx_requires_string(__s); |
5280 | return this->find(__s, __pos, traits_type::length(__s)); |
5281 | } |
5282 | |
5283 | /** |
5284 | * @brief Find position of a character. |
5285 | * @param __c Character to locate. |
5286 | * @param __pos Index of character to search from (default 0). |
5287 | * @return Index of first occurrence. |
5288 | * |
5289 | * Starting from @a __pos, searches forward for @a __c within |
5290 | * this string. If found, returns the index where it was |
5291 | * found. If not found, returns npos. |
5292 | */ |
5293 | size_type |
5294 | find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; |
5295 | |
5296 | #if __cplusplus >= 201703L |
5297 | /** |
5298 | * @brief Find position of a string_view. |
5299 | * @param __svt The object convertible to string_view to locate. |
5300 | * @param __pos Index of character to search from (default 0). |
5301 | * @return Index of start of first occurrence. |
5302 | */ |
5303 | template<typename _Tp> |
5304 | _If_sv<_Tp, size_type> |
5305 | find(const _Tp& __svt, size_type __pos = 0) const |
5306 | noexcept(is_same<_Tp, __sv_type>::value) |
5307 | { |
5308 | __sv_type __sv = __svt; |
5309 | return this->find(__sv.data(), __pos, __sv.size()); |
5310 | } |
5311 | #endif // C++17 |
5312 | |
5313 | /** |
5314 | * @brief Find last position of a string. |
5315 | * @param __str String to locate. |
5316 | * @param __pos Index of character to search back from (default end). |
5317 | * @return Index of start of last occurrence. |
5318 | * |
5319 | * Starting from @a __pos, searches backward for value of @a |
5320 | * __str within this string. If found, returns the index where |
5321 | * it begins. If not found, returns npos. |
5322 | */ |
5323 | size_type |
5324 | rfind(const basic_string& __str, size_type __pos = npos) const |
5325 | _GLIBCXX_NOEXCEPT |
5326 | { return this->rfind(__str.data(), __pos, __str.size()); } |
5327 | |
5328 | /** |
5329 | * @brief Find last position of a C substring. |
5330 | * @param __s C string to locate. |
5331 | * @param __pos Index of character to search back from. |
5332 | * @param __n Number of characters from s to search for. |
5333 | * @return Index of start of last occurrence. |
5334 | * |
5335 | * Starting from @a __pos, searches backward for the first @a |
5336 | * __n characters in @a __s within this string. If found, |
5337 | * returns the index where it begins. If not found, returns |
5338 | * npos. |
5339 | */ |
5340 | size_type |
5341 | rfind(const _CharT* __s, size_type __pos, size_type __n) const |
5342 | _GLIBCXX_NOEXCEPT; |
5343 | |
5344 | /** |
5345 | * @brief Find last position of a C string. |
5346 | * @param __s C string to locate. |
5347 | * @param __pos Index of character to start search at (default end). |
5348 | * @return Index of start of last occurrence. |
5349 | * |
5350 | * Starting from @a __pos, searches backward for the value of |
5351 | * @a __s within this string. If found, returns the index |
5352 | * where it begins. If not found, returns npos. |
5353 | */ |
5354 | size_type |
5355 | rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT |
5356 | { |
5357 | __glibcxx_requires_string(__s); |
5358 | return this->rfind(__s, __pos, traits_type::length(__s)); |
5359 | } |
5360 | |
5361 | /** |
5362 | * @brief Find last position of a character. |
5363 | * @param __c Character to locate. |
5364 | * @param __pos Index of character to search back from (default end). |
5365 | * @return Index of last occurrence. |
5366 | * |
5367 | * Starting from @a __pos, searches backward for @a __c within |
5368 | * this string. If found, returns the index where it was |
5369 | * found. If not found, returns npos. |
5370 | */ |
5371 | size_type |
5372 | rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; |
5373 | |
5374 | #if __cplusplus >= 201703L |
5375 | /** |
5376 | * @brief Find last position of a string_view. |
5377 | * @param __svt The object convertible to string_view to locate. |
5378 | * @param __pos Index of character to search back from (default end). |
5379 | * @return Index of start of last occurrence. |
5380 | */ |
5381 | template<typename _Tp> |
5382 | _If_sv<_Tp, size_type> |
5383 | rfind(const _Tp& __svt, size_type __pos = npos) const |
5384 | noexcept(is_same<_Tp, __sv_type>::value) |
5385 | { |
5386 | __sv_type __sv = __svt; |
5387 | return this->rfind(__sv.data(), __pos, __sv.size()); |
5388 | } |
5389 | #endif // C++17 |
5390 | |
5391 | /** |
5392 | * @brief Find position of a character of string. |
5393 | * @param __str String containing characters to locate. |
5394 | * @param __pos Index of character to search from (default 0). |
5395 | * @return Index of first occurrence. |
5396 | * |
5397 | * Starting from @a __pos, searches forward for one of the |
5398 | * characters of @a __str within this string. If found, |
5399 | * returns the index where it was found. If not found, returns |
5400 | * npos. |
5401 | */ |
5402 | size_type |
5403 | find_first_of(const basic_string& __str, size_type __pos = 0) const |
5404 | _GLIBCXX_NOEXCEPT |
5405 | { return this->find_first_of(__str.data(), __pos, __str.size()); } |
5406 | |
5407 | /** |
5408 | * @brief Find position of a character of C substring. |
5409 | * @param __s String containing characters to locate. |
5410 | * @param __pos Index of character to search from. |
5411 | * @param __n Number of characters from s to search for. |
5412 | * @return Index of first occurrence. |
5413 | * |
5414 | * Starting from @a __pos, searches forward for one of the |
5415 | * first @a __n characters of @a __s within this string. If |
5416 | * found, returns the index where it was found. If not found, |
5417 | * returns npos. |
5418 | */ |
5419 | size_type |
5420 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
5421 | _GLIBCXX_NOEXCEPT; |
5422 | |
5423 | /** |
5424 | * @brief Find position of a character of C string. |
5425 | * @param __s String containing characters to locate. |
5426 | * @param __pos Index of character to search from (default 0). |
5427 | * @return Index of first occurrence. |
5428 | * |
5429 | * Starting from @a __pos, searches forward for one of the |
5430 | * characters of @a __s within this string. If found, returns |
5431 | * the index where it was found. If not found, returns npos. |
5432 | */ |
5433 | size_type |
5434 | find_first_of(const _CharT* __s, size_type __pos = 0) const |
5435 | _GLIBCXX_NOEXCEPT |
5436 | { |
5437 | __glibcxx_requires_string(__s); |
5438 | return this->find_first_of(__s, __pos, traits_type::length(__s)); |
5439 | } |
5440 | |
5441 | /** |
5442 | * @brief Find position of a character. |
5443 | * @param __c Character to locate. |
5444 | * @param __pos Index of character to search from (default 0). |
5445 | * @return Index of first occurrence. |
5446 | * |
5447 | * Starting from @a __pos, searches forward for the character |
5448 | * @a __c within this string. If found, returns the index |
5449 | * where it was found. If not found, returns npos. |
5450 | * |
5451 | * Note: equivalent to find(__c, __pos). |
5452 | */ |
5453 | size_type |
5454 | find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT |
5455 | { return this->find(__c, __pos); } |
5456 | |
5457 | #if __cplusplus >= 201703L |
5458 | /** |
5459 | * @brief Find position of a character of a string_view. |
5460 | * @param __svt An object convertible to string_view containing |
5461 | * characters to locate. |
5462 | * @param __pos Index of character to search from (default 0). |
5463 | * @return Index of first occurrence. |
5464 | */ |
5465 | template<typename _Tp> |
5466 | _If_sv<_Tp, size_type> |
5467 | find_first_of(const _Tp& __svt, size_type __pos = 0) const |
5468 | noexcept(is_same<_Tp, __sv_type>::value) |
5469 | { |
5470 | __sv_type __sv = __svt; |
5471 | return this->find_first_of(__sv.data(), __pos, __sv.size()); |
5472 | } |
5473 | #endif // C++17 |
5474 | |
5475 | /** |
5476 | * @brief Find last position of a character of string. |
5477 | * @param __str String containing characters to locate. |
5478 | * @param __pos Index of character to search back from (default end). |
5479 | * @return Index of last occurrence. |
5480 | * |
5481 | * Starting from @a __pos, searches backward for one of the |
5482 | * characters of @a __str within this string. If found, |
5483 | * returns the index where it was found. If not found, returns |
5484 | * npos. |
5485 | */ |
5486 | size_type |
5487 | find_last_of(const basic_string& __str, size_type __pos = npos) const |
5488 | _GLIBCXX_NOEXCEPT |
5489 | { return this->find_last_of(__str.data(), __pos, __str.size()); } |
5490 | |
5491 | /** |
5492 | * @brief Find last position of a character of C substring. |
5493 | * @param __s C string containing characters to locate. |
5494 | * @param __pos Index of character to search back from. |
5495 | * @param __n Number of characters from s to search for. |
5496 | * @return Index of last occurrence. |
5497 | * |
5498 | * Starting from @a __pos, searches backward for one of the |
5499 | * first @a __n characters of @a __s within this string. If |
5500 | * found, returns the index where it was found. If not found, |
5501 | * returns npos. |
5502 | */ |
5503 | size_type |
5504 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
5505 | _GLIBCXX_NOEXCEPT; |
5506 | |
5507 | /** |
5508 | * @brief Find last position of a character of C string. |
5509 | * @param __s C string containing characters to locate. |
5510 | * @param __pos Index of character to search back from (default end). |
5511 | * @return Index of last occurrence. |
5512 | * |
5513 | * Starting from @a __pos, searches backward for one of the |
5514 | * characters of @a __s within this string. If found, returns |
5515 | * the index where it was found. If not found, returns npos. |
5516 | */ |
5517 | size_type |
5518 | find_last_of(const _CharT* __s, size_type __pos = npos) const |
5519 | _GLIBCXX_NOEXCEPT |
5520 | { |
5521 | __glibcxx_requires_string(__s); |
5522 | return this->find_last_of(__s, __pos, traits_type::length(__s)); |
5523 | } |
5524 | |
5525 | /** |
5526 | * @brief Find last position of a character. |
5527 | * @param __c Character to locate. |
5528 | * @param __pos Index of character to search back from (default end). |
5529 | * @return Index of last occurrence. |
5530 | * |
5531 | * Starting from @a __pos, searches backward for @a __c within |
5532 | * this string. If found, returns the index where it was |
5533 | * found. If not found, returns npos. |
5534 | * |
5535 | * Note: equivalent to rfind(__c, __pos). |
5536 | */ |
5537 | size_type |
5538 | find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT |
5539 | { return this->rfind(__c, __pos); } |
5540 | |
5541 | #if __cplusplus >= 201703L |
5542 | /** |
5543 | * @brief Find last position of a character of string. |
5544 | * @param __svt An object convertible to string_view containing |
5545 | * characters to locate. |
5546 | * @param __pos Index of character to search back from (default end). |
5547 | * @return Index of last occurrence. |
5548 | */ |
5549 | template<typename _Tp> |
5550 | _If_sv<_Tp, size_type> |
5551 | find_last_of(const _Tp& __svt, size_type __pos = npos) const |
5552 | noexcept(is_same<_Tp, __sv_type>::value) |
5553 | { |
5554 | __sv_type __sv = __svt; |
5555 | return this->find_last_of(__sv.data(), __pos, __sv.size()); |
5556 | } |
5557 | #endif // C++17 |
5558 | |
5559 | /** |
5560 | * @brief Find position of a character not in string. |
5561 | * @param __str String containing characters to avoid. |
5562 | * @param __pos Index of character to search from (default 0). |
5563 | * @return Index of first occurrence. |
5564 | * |
5565 | * Starting from @a __pos, searches forward for a character not contained |
5566 | * in @a __str within this string. If found, returns the index where it |
5567 | * was found. If not found, returns npos. |
5568 | */ |
5569 | size_type |
5570 | find_first_not_of(const basic_string& __str, size_type __pos = 0) const |
5571 | _GLIBCXX_NOEXCEPT |
5572 | { return this->find_first_not_of(__str.data(), __pos, __str.size()); } |
5573 | |
5574 | /** |
5575 | * @brief Find position of a character not in C substring. |
5576 | * @param __s C string containing characters to avoid. |
5577 | * @param __pos Index of character to search from. |
5578 | * @param __n Number of characters from __s to consider. |
5579 | * @return Index of first occurrence. |
5580 | * |
5581 | * Starting from @a __pos, searches forward for a character not |
5582 | * contained in the first @a __n characters of @a __s within |
5583 | * this string. If found, returns the index where it was |
5584 | * found. If not found, returns npos. |
5585 | */ |
5586 | size_type |
5587 | find_first_not_of(const _CharT* __s, size_type __pos, |
5588 | size_type __n) const _GLIBCXX_NOEXCEPT; |
5589 | |
5590 | /** |
5591 | * @brief Find position of a character not in C string. |
5592 | * @param __s C string containing characters to avoid. |
5593 | * @param __pos Index of character to search from (default 0). |
5594 | * @return Index of first occurrence. |
5595 | * |
5596 | * Starting from @a __pos, searches forward for a character not |
5597 | * contained in @a __s within this string. If found, returns |
5598 | * the index where it was found. If not found, returns npos. |
5599 | */ |
5600 | size_type |
5601 | find_first_not_of(const _CharT* __s, size_type __pos = 0) const |
5602 | _GLIBCXX_NOEXCEPT |
5603 | { |
5604 | __glibcxx_requires_string(__s); |
5605 | return this->find_first_not_of(__s, __pos, traits_type::length(__s)); |
5606 | } |
5607 | |
5608 | /** |
5609 | * @brief Find position of a different character. |
5610 | * @param __c Character to avoid. |
5611 | * @param __pos Index of character to search from (default 0). |
5612 | * @return Index of first occurrence. |
5613 | * |
5614 | * Starting from @a __pos, searches forward for a character |
5615 | * other than @a __c within this string. If found, returns the |
5616 | * index where it was found. If not found, returns npos. |
5617 | */ |
5618 | size_type |
5619 | find_first_not_of(_CharT __c, size_type __pos = 0) const |
5620 | _GLIBCXX_NOEXCEPT; |
5621 | |
5622 | #if __cplusplus >= 201703L |
5623 | /** |
5624 | * @brief Find position of a character not in a string_view. |
5625 | * @param __svt An object convertible to string_view containing |
5626 | * characters to avoid. |
5627 | * @param __pos Index of character to search from (default 0). |
5628 | * @return Index of first occurrence. |
5629 | */ |
5630 | template<typename _Tp> |
5631 | _If_sv<_Tp, size_type> |
5632 | find_first_not_of(const _Tp& __svt, size_type __pos = 0) const |
5633 | noexcept(is_same<_Tp, __sv_type>::value) |
5634 | { |
5635 | __sv_type __sv = __svt; |
5636 | return this->find_first_not_of(__sv.data(), __pos, __sv.size()); |
5637 | } |
5638 | #endif // C++17 |
5639 | |
5640 | /** |
5641 | * @brief Find last position of a character not in string. |
5642 | * @param __str String containing characters to avoid. |
5643 | * @param __pos Index of character to search back from (default end). |
5644 | * @return Index of last occurrence. |
5645 | * |
5646 | * Starting from @a __pos, searches backward for a character |
5647 | * not contained in @a __str within this string. If found, |
5648 | * returns the index where it was found. If not found, returns |
5649 | * npos. |
5650 | */ |
5651 | size_type |
5652 | find_last_not_of(const basic_string& __str, size_type __pos = npos) const |
5653 | _GLIBCXX_NOEXCEPT |
5654 | { return this->find_last_not_of(__str.data(), __pos, __str.size()); } |
5655 | |
5656 | /** |
5657 | * @brief Find last position of a character not in C substring. |
5658 | * @param __s C string containing characters to avoid. |
5659 | * @param __pos Index of character to search back from. |
5660 | * @param __n Number of characters from s to consider. |
5661 | * @return Index of last occurrence. |
5662 | * |
5663 | * Starting from @a __pos, searches backward for a character not |
5664 | * contained in the first @a __n characters of @a __s within this string. |
5665 | * If found, returns the index where it was found. If not found, |
5666 | * returns npos. |
5667 | */ |
5668 | size_type |
5669 | find_last_not_of(const _CharT* __s, size_type __pos, |
5670 | size_type __n) const _GLIBCXX_NOEXCEPT; |
5671 | /** |
5672 | * @brief Find last position of a character not in C string. |
5673 | * @param __s C string containing characters to avoid. |
5674 | * @param __pos Index of character to search back from (default end). |
5675 | * @return Index of last occurrence. |
5676 | * |
5677 | * Starting from @a __pos, searches backward for a character |
5678 | * not contained in @a __s within this string. If found, |
5679 | * returns the index where it was found. If not found, returns |
5680 | * npos. |
5681 | */ |
5682 | size_type |
5683 | find_last_not_of(const _CharT* __s, size_type __pos = npos) const |
5684 | _GLIBCXX_NOEXCEPT |
5685 | { |
5686 | __glibcxx_requires_string(__s); |
5687 | return this->find_last_not_of(__s, __pos, traits_type::length(__s)); |
5688 | } |
5689 | |
5690 | /** |
5691 | * @brief Find last position of a different character. |
5692 | * @param __c Character to avoid. |
5693 | * @param __pos Index of character to search back from (default end). |
5694 | * @return Index of last occurrence. |
5695 | * |
5696 | * Starting from @a __pos, searches backward for a character other than |
5697 | * @a __c within this string. If found, returns the index where it was |
5698 | * found. If not found, returns npos. |
5699 | */ |
5700 | size_type |
5701 | find_last_not_of(_CharT __c, size_type __pos = npos) const |
5702 | _GLIBCXX_NOEXCEPT; |
5703 | |
5704 | #if __cplusplus >= 201703L |
5705 | /** |
5706 | * @brief Find last position of a character not in a string_view. |
5707 | * @param __svt An object convertible to string_view containing |
5708 | * characters to avoid. |
5709 | * @param __pos Index of character to search back from (default end). |
5710 | * @return Index of last occurrence. |
5711 | */ |
5712 | template<typename _Tp> |
5713 | _If_sv<_Tp, size_type> |
5714 | find_last_not_of(const _Tp& __svt, size_type __pos = npos) const |
5715 | noexcept(is_same<_Tp, __sv_type>::value) |
5716 | { |
5717 | __sv_type __sv = __svt; |
5718 | return this->find_last_not_of(__sv.data(), __pos, __sv.size()); |
5719 | } |
5720 | #endif // C++17 |
5721 | |
5722 | /** |
5723 | * @brief Get a substring. |
5724 | * @param __pos Index of first character (default 0). |
5725 | * @param __n Number of characters in substring (default remainder). |
5726 | * @return The new string. |
5727 | * @throw std::out_of_range If __pos > size(). |
5728 | * |
5729 | * Construct and return a new string using the @a __n |
5730 | * characters starting at @a __pos. If the string is too |
5731 | * short, use the remainder of the characters. If @a __pos is |
5732 | * beyond the end of the string, out_of_range is thrown. |
5733 | */ |
5734 | basic_string |
5735 | substr(size_type __pos = 0, size_type __n = npos) const |
5736 | { return basic_string(*this, |
5737 | _M_check(__pos, "basic_string::substr" ), __n); } |
5738 | |
5739 | /** |
5740 | * @brief Compare to a string. |
5741 | * @param __str String to compare against. |
5742 | * @return Integer < 0, 0, or > 0. |
5743 | * |
5744 | * Returns an integer < 0 if this string is ordered before @a |
5745 | * __str, 0 if their values are equivalent, or > 0 if this |
5746 | * string is ordered after @a __str. Determines the effective |
5747 | * length rlen of the strings to compare as the smallest of |
5748 | * size() and str.size(). The function then compares the two |
5749 | * strings by calling traits::compare(data(), str.data(),rlen). |
5750 | * If the result of the comparison is nonzero returns it, |
5751 | * otherwise the shorter one is ordered first. |
5752 | */ |
5753 | int |
5754 | compare(const basic_string& __str) const |
5755 | { |
5756 | const size_type __size = this->size(); |
5757 | const size_type __osize = __str.size(); |
5758 | const size_type __len = std::min(__size, __osize); |
5759 | |
5760 | int __r = traits_type::compare(_M_data(), __str.data(), __len); |
5761 | if (!__r) |
5762 | __r = _S_compare(__size, __osize); |
5763 | return __r; |
5764 | } |
5765 | |
5766 | #if __cplusplus >= 201703L |
5767 | /** |
5768 | * @brief Compare to a string_view. |
5769 | * @param __svt An object convertible to string_view to compare against. |
5770 | * @return Integer < 0, 0, or > 0. |
5771 | */ |
5772 | template<typename _Tp> |
5773 | _If_sv<_Tp, int> |
5774 | compare(const _Tp& __svt) const |
5775 | noexcept(is_same<_Tp, __sv_type>::value) |
5776 | { |
5777 | __sv_type __sv = __svt; |
5778 | const size_type __size = this->size(); |
5779 | const size_type __osize = __sv.size(); |
5780 | const size_type __len = std::min(__size, __osize); |
5781 | |
5782 | int __r = traits_type::compare(_M_data(), __sv.data(), __len); |
5783 | if (!__r) |
5784 | __r = _S_compare(__size, __osize); |
5785 | return __r; |
5786 | } |
5787 | |
5788 | /** |
5789 | * @brief Compare to a string_view. |
5790 | * @param __pos A position in the string to start comparing from. |
5791 | * @param __n The number of characters to compare. |
5792 | * @param __svt An object convertible to string_view to compare |
5793 | * against. |
5794 | * @return Integer < 0, 0, or > 0. |
5795 | */ |
5796 | template<typename _Tp> |
5797 | _If_sv<_Tp, int> |
5798 | compare(size_type __pos, size_type __n, const _Tp& __svt) const |
5799 | noexcept(is_same<_Tp, __sv_type>::value) |
5800 | { |
5801 | __sv_type __sv = __svt; |
5802 | return __sv_type(*this).substr(__pos, __n).compare(__sv); |
5803 | } |
5804 | |
5805 | /** |
5806 | * @brief Compare to a string_view. |
5807 | * @param __pos1 A position in the string to start comparing from. |
5808 | * @param __n1 The number of characters to compare. |
5809 | * @param __svt An object convertible to string_view to compare |
5810 | * against. |
5811 | * @param __pos2 A position in the string_view to start comparing from. |
5812 | * @param __n2 The number of characters to compare. |
5813 | * @return Integer < 0, 0, or > 0. |
5814 | */ |
5815 | template<typename _Tp> |
5816 | _If_sv<_Tp, int> |
5817 | compare(size_type __pos1, size_type __n1, const _Tp& __svt, |
5818 | size_type __pos2, size_type __n2 = npos) const |
5819 | noexcept(is_same<_Tp, __sv_type>::value) |
5820 | { |
5821 | __sv_type __sv = __svt; |
5822 | return __sv_type(*this) |
5823 | .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); |
5824 | } |
5825 | #endif // C++17 |
5826 | |
5827 | /** |
5828 | * @brief Compare substring to a string. |
5829 | * @param __pos Index of first character of substring. |
5830 | * @param __n Number of characters in substring. |
5831 | * @param __str String to compare against. |
5832 | * @return Integer < 0, 0, or > 0. |
5833 | * |
5834 | * Form the substring of this string from the @a __n characters |
5835 | * starting at @a __pos. Returns an integer < 0 if the |
5836 | * substring is ordered before @a __str, 0 if their values are |
5837 | * equivalent, or > 0 if the substring is ordered after @a |
5838 | * __str. Determines the effective length rlen of the strings |
5839 | * to compare as the smallest of the length of the substring |
5840 | * and @a __str.size(). The function then compares the two |
5841 | * strings by calling |
5842 | * traits::compare(substring.data(),str.data(),rlen). If the |
5843 | * result of the comparison is nonzero returns it, otherwise |
5844 | * the shorter one is ordered first. |
5845 | */ |
5846 | int |
5847 | compare(size_type __pos, size_type __n, const basic_string& __str) const; |
5848 | |
5849 | /** |
5850 | * @brief Compare substring to a substring. |
5851 | * @param __pos1 Index of first character of substring. |
5852 | * @param __n1 Number of characters in substring. |
5853 | * @param __str String to compare against. |
5854 | * @param __pos2 Index of first character of substring of str. |
5855 | * @param __n2 Number of characters in substring of str. |
5856 | * @return Integer < 0, 0, or > 0. |
5857 | * |
5858 | * Form the substring of this string from the @a __n1 |
5859 | * characters starting at @a __pos1. Form the substring of @a |
5860 | * __str from the @a __n2 characters starting at @a __pos2. |
5861 | * Returns an integer < 0 if this substring is ordered before |
5862 | * the substring of @a __str, 0 if their values are equivalent, |
5863 | * or > 0 if this substring is ordered after the substring of |
5864 | * @a __str. Determines the effective length rlen of the |
5865 | * strings to compare as the smallest of the lengths of the |
5866 | * substrings. The function then compares the two strings by |
5867 | * calling |
5868 | * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). |
5869 | * If the result of the comparison is nonzero returns it, |
5870 | * otherwise the shorter one is ordered first. |
5871 | */ |
5872 | int |
5873 | compare(size_type __pos1, size_type __n1, const basic_string& __str, |
5874 | size_type __pos2, size_type __n2 = npos) const; |
5875 | |
5876 | /** |
5877 | * @brief Compare to a C string. |
5878 | * @param __s C string to compare against. |
5879 | * @return Integer < 0, 0, or > 0. |
5880 | * |
5881 | * Returns an integer < 0 if this string is ordered before @a __s, 0 if |
5882 | * their values are equivalent, or > 0 if this string is ordered after |
5883 | * @a __s. Determines the effective length rlen of the strings to |
5884 | * compare as the smallest of size() and the length of a string |
5885 | * constructed from @a __s. The function then compares the two strings |
5886 | * by calling traits::compare(data(),s,rlen). If the result of the |
5887 | * comparison is nonzero returns it, otherwise the shorter one is |
5888 | * ordered first. |
5889 | */ |
5890 | int |
5891 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT; |
5892 | |
5893 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
5894 | // 5 String::compare specification questionable |
5895 | /** |
5896 | * @brief Compare substring to a C string. |
5897 | * @param __pos Index of first character of substring. |
5898 | * @param __n1 Number of characters in substring. |
5899 | * @param __s C string to compare against. |
5900 | * @return Integer < 0, 0, or > 0. |
5901 | * |
5902 | * Form the substring of this string from the @a __n1 |
5903 | * characters starting at @a pos. Returns an integer < 0 if |
5904 | * the substring is ordered before @a __s, 0 if their values |
5905 | * are equivalent, or > 0 if the substring is ordered after @a |
5906 | * __s. Determines the effective length rlen of the strings to |
5907 | * compare as the smallest of the length of the substring and |
5908 | * the length of a string constructed from @a __s. The |
5909 | * function then compares the two string by calling |
5910 | * traits::compare(substring.data(),__s,rlen). If the result of |
5911 | * the comparison is nonzero returns it, otherwise the shorter |
5912 | * one is ordered first. |
5913 | */ |
5914 | int |
5915 | compare(size_type __pos, size_type __n1, const _CharT* __s) const; |
5916 | |
5917 | /** |
5918 | * @brief Compare substring against a character %array. |
5919 | * @param __pos Index of first character of substring. |
5920 | * @param __n1 Number of characters in substring. |
5921 | * @param __s character %array to compare against. |
5922 | * @param __n2 Number of characters of s. |
5923 | * @return Integer < 0, 0, or > 0. |
5924 | * |
5925 | * Form the substring of this string from the @a __n1 |
5926 | * characters starting at @a __pos. Form a string from the |
5927 | * first @a __n2 characters of @a __s. Returns an integer < 0 |
5928 | * if this substring is ordered before the string from @a __s, |
5929 | * 0 if their values are equivalent, or > 0 if this substring |
5930 | * is ordered after the string from @a __s. Determines the |
5931 | * effective length rlen of the strings to compare as the |
5932 | * smallest of the length of the substring and @a __n2. The |
5933 | * function then compares the two strings by calling |
5934 | * traits::compare(substring.data(),s,rlen). If the result of |
5935 | * the comparison is nonzero returns it, otherwise the shorter |
5936 | * one is ordered first. |
5937 | * |
5938 | * NB: s must have at least n2 characters, '\\0' has |
5939 | * no special meaning. |
5940 | */ |
5941 | int |
5942 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
5943 | size_type __n2) const; |
5944 | |
5945 | #if __cplusplus > 201703L |
5946 | bool |
5947 | starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
5948 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
5949 | |
5950 | bool |
5951 | starts_with(_CharT __x) const noexcept |
5952 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
5953 | |
5954 | bool |
5955 | starts_with(const _CharT* __x) const noexcept |
5956 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
5957 | |
5958 | bool |
5959 | ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
5960 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
5961 | |
5962 | bool |
5963 | ends_with(_CharT __x) const noexcept |
5964 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
5965 | |
5966 | bool |
5967 | ends_with(const _CharT* __x) const noexcept |
5968 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
5969 | #endif // C++20 |
5970 | |
5971 | # ifdef _GLIBCXX_TM_TS_INTERNAL |
5972 | friend void |
5973 | ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s, |
5974 | void* exc); |
5975 | friend const char* |
5976 | ::_txnal_cow_string_c_str(const void *that); |
5977 | friend void |
5978 | ::_txnal_cow_string_D1(void *that); |
5979 | friend void |
5980 | ::_txnal_cow_string_D1_commit(void *that); |
5981 | # endif |
5982 | }; |
5983 | #endif // !_GLIBCXX_USE_CXX11_ABI |
5984 | |
5985 | #if __cpp_deduction_guides >= 201606 |
5986 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
5987 | template<typename _InputIterator, typename _CharT |
5988 | = typename iterator_traits<_InputIterator>::value_type, |
5989 | typename _Allocator = allocator<_CharT>, |
5990 | typename = _RequireInputIter<_InputIterator>, |
5991 | typename = _RequireAllocator<_Allocator>> |
5992 | basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator()) |
5993 | -> basic_string<_CharT, char_traits<_CharT>, _Allocator>; |
5994 | |
5995 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
5996 | // 3075. basic_string needs deduction guides from basic_string_view |
5997 | template<typename _CharT, typename _Traits, |
5998 | typename _Allocator = allocator<_CharT>, |
5999 | typename = _RequireAllocator<_Allocator>> |
6000 | basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator()) |
6001 | -> basic_string<_CharT, _Traits, _Allocator>; |
6002 | |
6003 | template<typename _CharT, typename _Traits, |
6004 | typename _Allocator = allocator<_CharT>, |
6005 | typename = _RequireAllocator<_Allocator>> |
6006 | basic_string(basic_string_view<_CharT, _Traits>, |
6007 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
6008 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
6009 | const _Allocator& = _Allocator()) |
6010 | -> basic_string<_CharT, _Traits, _Allocator>; |
6011 | _GLIBCXX_END_NAMESPACE_CXX11 |
6012 | #endif |
6013 | |
6014 | // operator+ |
6015 | /** |
6016 | * @brief Concatenate two strings. |
6017 | * @param __lhs First string. |
6018 | * @param __rhs Last string. |
6019 | * @return New string with value of @a __lhs followed by @a __rhs. |
6020 | */ |
6021 | template<typename _CharT, typename _Traits, typename _Alloc> |
6022 | basic_string<_CharT, _Traits, _Alloc> |
6023 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
6024 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
6025 | { |
6026 | basic_string<_CharT, _Traits, _Alloc> __str(__lhs); |
6027 | __str.append(__rhs); |
6028 | return __str; |
6029 | } |
6030 | |
6031 | /** |
6032 | * @brief Concatenate C string and string. |
6033 | * @param __lhs First string. |
6034 | * @param __rhs Last string. |
6035 | * @return New string with value of @a __lhs followed by @a __rhs. |
6036 | */ |
6037 | template<typename _CharT, typename _Traits, typename _Alloc> |
6038 | basic_string<_CharT,_Traits,_Alloc> |
6039 | operator+(const _CharT* __lhs, |
6040 | const basic_string<_CharT,_Traits,_Alloc>& __rhs); |
6041 | |
6042 | /** |
6043 | * @brief Concatenate character and string. |
6044 | * @param __lhs First string. |
6045 | * @param __rhs Last string. |
6046 | * @return New string with @a __lhs followed by @a __rhs. |
6047 | */ |
6048 | template<typename _CharT, typename _Traits, typename _Alloc> |
6049 | basic_string<_CharT,_Traits,_Alloc> |
6050 | operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); |
6051 | |
6052 | /** |
6053 | * @brief Concatenate string and C string. |
6054 | * @param __lhs First string. |
6055 | * @param __rhs Last string. |
6056 | * @return New string with @a __lhs followed by @a __rhs. |
6057 | */ |
6058 | template<typename _CharT, typename _Traits, typename _Alloc> |
6059 | inline basic_string<_CharT, _Traits, _Alloc> |
6060 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
6061 | const _CharT* __rhs) |
6062 | { |
6063 | basic_string<_CharT, _Traits, _Alloc> __str(__lhs); |
6064 | __str.append(__rhs); |
6065 | return __str; |
6066 | } |
6067 | |
6068 | /** |
6069 | * @brief Concatenate string and character. |
6070 | * @param __lhs First string. |
6071 | * @param __rhs Last string. |
6072 | * @return New string with @a __lhs followed by @a __rhs. |
6073 | */ |
6074 | template<typename _CharT, typename _Traits, typename _Alloc> |
6075 | inline basic_string<_CharT, _Traits, _Alloc> |
6076 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) |
6077 | { |
6078 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; |
6079 | typedef typename __string_type::size_type __size_type; |
6080 | __string_type __str(__lhs); |
6081 | __str.append(__size_type(1), __rhs); |
6082 | return __str; |
6083 | } |
6084 | |
6085 | #if __cplusplus >= 201103L |
6086 | template<typename _CharT, typename _Traits, typename _Alloc> |
6087 | inline basic_string<_CharT, _Traits, _Alloc> |
6088 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
6089 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
6090 | { return std::move(__lhs.append(__rhs)); } |
6091 | |
6092 | template<typename _CharT, typename _Traits, typename _Alloc> |
6093 | inline basic_string<_CharT, _Traits, _Alloc> |
6094 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
6095 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
6096 | { return std::move(__rhs.insert(0, __lhs)); } |
6097 | |
6098 | template<typename _CharT, typename _Traits, typename _Alloc> |
6099 | inline basic_string<_CharT, _Traits, _Alloc> |
6100 | operator+( |
---|