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