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