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