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