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