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