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