]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/basic_string.h
basic_string.h (basic_string<>::front()): Add !empty debug check.
[thirdparty/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 { return this->_M_refcount < 0; }
2605
2606 bool
2607 _M_is_shared() const _GLIBCXX_NOEXCEPT
2608 { return this->_M_refcount > 0; }
2609
2610 void
2611 _M_set_leaked() _GLIBCXX_NOEXCEPT
2612 { this->_M_refcount = -1; }
2613
2614 void
2615 _M_set_sharable() _GLIBCXX_NOEXCEPT
2616 { this->_M_refcount = 0; }
2617
2618 void
2619 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
2620 {
2621 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2622 if (__builtin_expect(this != &_S_empty_rep(), false))
2623 #endif
2624 {
2625 this->_M_set_sharable(); // One reference.
2626 this->_M_length = __n;
2627 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
2628 // grrr. (per 21.3.4)
2629 // You cannot leave those LWG people alone for a second.
2630 }
2631 }
2632
2633 _CharT*
2634 _M_refdata() throw()
2635 { return reinterpret_cast<_CharT*>(this + 1); }
2636
2637 _CharT*
2638 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
2639 {
2640 return (!_M_is_leaked() && __alloc1 == __alloc2)
2641 ? _M_refcopy() : _M_clone(__alloc1);
2642 }
2643
2644 // Create & Destroy
2645 static _Rep*
2646 _S_create(size_type, size_type, const _Alloc&);
2647
2648 void
2649 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
2650 {
2651 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2652 if (__builtin_expect(this != &_S_empty_rep(), false))
2653 #endif
2654 {
2655 // Be race-detector-friendly. For more info see bits/c++config.
2656 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
2657 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
2658 -1) <= 0)
2659 {
2660 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
2661 _M_destroy(__a);
2662 }
2663 }
2664 } // XXX MT
2665
2666 void
2667 _M_destroy(const _Alloc&) throw();
2668
2669 _CharT*
2670 _M_refcopy() throw()
2671 {
2672 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2673 if (__builtin_expect(this != &_S_empty_rep(), false))
2674 #endif
2675 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
2676 return _M_refdata();
2677 } // XXX MT
2678
2679 _CharT*
2680 _M_clone(const _Alloc&, size_type __res = 0);
2681 };
2682
2683 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
2684 struct _Alloc_hider : _Alloc
2685 {
2686 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
2687 : _Alloc(__a), _M_p(__dat) { }
2688
2689 _CharT* _M_p; // The actual data.
2690 };
2691
2692 public:
2693 // Data Members (public):
2694 // NB: This is an unsigned type, and thus represents the maximum
2695 // size that the allocator can hold.
2696 /// Value returned by various member functions when they fail.
2697 static const size_type npos = static_cast<size_type>(-1);
2698
2699 private:
2700 // Data Members (private):
2701 mutable _Alloc_hider _M_dataplus;
2702
2703 _CharT*
2704 _M_data() const _GLIBCXX_NOEXCEPT
2705 { return _M_dataplus._M_p; }
2706
2707 _CharT*
2708 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
2709 { return (_M_dataplus._M_p = __p); }
2710
2711 _Rep*
2712 _M_rep() const _GLIBCXX_NOEXCEPT
2713 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
2714
2715 // For the internal use we have functions similar to `begin'/`end'
2716 // but they do not call _M_leak.
2717 iterator
2718 _M_ibegin() const _GLIBCXX_NOEXCEPT
2719 { return iterator(_M_data()); }
2720
2721 iterator
2722 _M_iend() const _GLIBCXX_NOEXCEPT
2723 { return iterator(_M_data() + this->size()); }
2724
2725 void
2726 _M_leak() // for use in begin() & non-const op[]
2727 {
2728 if (!_M_rep()->_M_is_leaked())
2729 _M_leak_hard();
2730 }
2731
2732 size_type
2733 _M_check(size_type __pos, const char* __s) const
2734 {
2735 if (__pos > this->size())
2736 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
2737 "this->size() (which is %zu)"),
2738 __s, __pos, this->size());
2739 return __pos;
2740 }
2741
2742 void
2743 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
2744 {
2745 if (this->max_size() - (this->size() - __n1) < __n2)
2746 __throw_length_error(__N(__s));
2747 }
2748
2749 // NB: _M_limit doesn't check for a bad __pos value.
2750 size_type
2751 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
2752 {
2753 const bool __testoff = __off < this->size() - __pos;
2754 return __testoff ? __off : this->size() - __pos;
2755 }
2756
2757 // True if _Rep and source do not overlap.
2758 bool
2759 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
2760 {
2761 return (less<const _CharT*>()(__s, _M_data())
2762 || less<const _CharT*>()(_M_data() + this->size(), __s));
2763 }
2764
2765 // When __n = 1 way faster than the general multichar
2766 // traits_type::copy/move/assign.
2767 static void
2768 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
2769 {
2770 if (__n == 1)
2771 traits_type::assign(*__d, *__s);
2772 else
2773 traits_type::copy(__d, __s, __n);
2774 }
2775
2776 static void
2777 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
2778 {
2779 if (__n == 1)
2780 traits_type::assign(*__d, *__s);
2781 else
2782 traits_type::move(__d, __s, __n);
2783 }
2784
2785 static void
2786 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
2787 {
2788 if (__n == 1)
2789 traits_type::assign(*__d, __c);
2790 else
2791 traits_type::assign(__d, __n, __c);
2792 }
2793
2794 // _S_copy_chars is a separate template to permit specialization
2795 // to optimize for the common case of pointers as iterators.
2796 template<class _Iterator>
2797 static void
2798 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
2799 _GLIBCXX_NOEXCEPT
2800 {
2801 for (; __k1 != __k2; ++__k1, (void)++__p)
2802 traits_type::assign(*__p, *__k1); // These types are off.
2803 }
2804
2805 static void
2806 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
2807 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
2808
2809 static void
2810 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
2811 _GLIBCXX_NOEXCEPT
2812 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
2813
2814 static void
2815 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
2816 { _M_copy(__p, __k1, __k2 - __k1); }
2817
2818 static void
2819 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
2820 _GLIBCXX_NOEXCEPT
2821 { _M_copy(__p, __k1, __k2 - __k1); }
2822
2823 static int
2824 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
2825 {
2826 const difference_type __d = difference_type(__n1 - __n2);
2827
2828 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
2829 return __gnu_cxx::__numeric_traits<int>::__max;
2830 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
2831 return __gnu_cxx::__numeric_traits<int>::__min;
2832 else
2833 return int(__d);
2834 }
2835
2836 void
2837 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
2838
2839 void
2840 _M_leak_hard();
2841
2842 static _Rep&
2843 _S_empty_rep() _GLIBCXX_NOEXCEPT
2844 { return _Rep::_S_empty_rep(); }
2845
2846 public:
2847 // Construct/copy/destroy:
2848 // NB: We overload ctors in some cases instead of using default
2849 // arguments, per 17.4.4.4 para. 2 item 2.
2850
2851 /**
2852 * @brief Default constructor creates an empty string.
2853 */
2854 basic_string()
2855 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2856 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2857 #else
2858 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
2859 #endif
2860
2861 /**
2862 * @brief Construct an empty string using allocator @a a.
2863 */
2864 explicit
2865 basic_string(const _Alloc& __a);
2866
2867 // NB: per LWG issue 42, semantics different from IS:
2868 /**
2869 * @brief Construct string with copy of value of @a str.
2870 * @param __str Source string.
2871 */
2872 basic_string(const basic_string& __str);
2873 /**
2874 * @brief Construct string as copy of a substring.
2875 * @param __str Source string.
2876 * @param __pos Index of first character to copy from.
2877 * @param __n Number of characters to copy (default remainder).
2878 */
2879 basic_string(const basic_string& __str, size_type __pos,
2880 size_type __n = npos);
2881 /**
2882 * @brief Construct string as copy of a substring.
2883 * @param __str Source string.
2884 * @param __pos Index of first character to copy from.
2885 * @param __n Number of characters to copy.
2886 * @param __a Allocator to use.
2887 */
2888 basic_string(const basic_string& __str, size_type __pos,
2889 size_type __n, const _Alloc& __a);
2890
2891 /**
2892 * @brief Construct string initialized by a character %array.
2893 * @param __s Source character %array.
2894 * @param __n Number of characters to copy.
2895 * @param __a Allocator to use (default is default allocator).
2896 *
2897 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
2898 * has no special meaning.
2899 */
2900 basic_string(const _CharT* __s, size_type __n,
2901 const _Alloc& __a = _Alloc());
2902 /**
2903 * @brief Construct string as copy of a C string.
2904 * @param __s Source C string.
2905 * @param __a Allocator to use (default is default allocator).
2906 */
2907 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
2908 /**
2909 * @brief Construct string as multiple characters.
2910 * @param __n Number of characters.
2911 * @param __c Character to use.
2912 * @param __a Allocator to use (default is default allocator).
2913 */
2914 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
2915
2916 #if __cplusplus >= 201103L
2917 /**
2918 * @brief Move construct string.
2919 * @param __str Source string.
2920 *
2921 * The newly-created string contains the exact contents of @a __str.
2922 * @a __str is a valid, but unspecified string.
2923 **/
2924 basic_string(basic_string&& __str)
2925 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2926 noexcept // FIXME C++11: should always be noexcept.
2927 #endif
2928 : _M_dataplus(__str._M_dataplus)
2929 {
2930 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2931 __str._M_data(_S_empty_rep()._M_refdata());
2932 #else
2933 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
2934 #endif
2935 }
2936
2937 /**
2938 * @brief Construct string from an initializer %list.
2939 * @param __l std::initializer_list of characters.
2940 * @param __a Allocator to use (default is default allocator).
2941 */
2942 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
2943 #endif // C++11
2944
2945 /**
2946 * @brief Construct string as copy of a range.
2947 * @param __beg Start of range.
2948 * @param __end End of range.
2949 * @param __a Allocator to use (default is default allocator).
2950 */
2951 template<class _InputIterator>
2952 basic_string(_InputIterator __beg, _InputIterator __end,
2953 const _Alloc& __a = _Alloc());
2954
2955 /**
2956 * @brief Destroy the string instance.
2957 */
2958 ~basic_string() _GLIBCXX_NOEXCEPT
2959 { _M_rep()->_M_dispose(this->get_allocator()); }
2960
2961 /**
2962 * @brief Assign the value of @a str to this string.
2963 * @param __str Source string.
2964 */
2965 basic_string&
2966 operator=(const basic_string& __str)
2967 { return this->assign(__str); }
2968
2969 /**
2970 * @brief Copy contents of @a s into this string.
2971 * @param __s Source null-terminated string.
2972 */
2973 basic_string&
2974 operator=(const _CharT* __s)
2975 { return this->assign(__s); }
2976
2977 /**
2978 * @brief Set value to string of length 1.
2979 * @param __c Source character.
2980 *
2981 * Assigning to a character makes this string length 1 and
2982 * (*this)[0] == @a c.
2983 */
2984 basic_string&
2985 operator=(_CharT __c)
2986 {
2987 this->assign(1, __c);
2988 return *this;
2989 }
2990
2991 #if __cplusplus >= 201103L
2992 /**
2993 * @brief Move assign the value of @a str to this string.
2994 * @param __str Source string.
2995 *
2996 * The contents of @a str are moved into this string (without copying).
2997 * @a str is a valid, but unspecified string.
2998 **/
2999 // PR 58265, this should be noexcept.
3000 basic_string&
3001 operator=(basic_string&& __str)
3002 {
3003 // NB: DR 1204.
3004 this->swap(__str);
3005 return *this;
3006 }
3007
3008 /**
3009 * @brief Set value to string constructed from initializer %list.
3010 * @param __l std::initializer_list.
3011 */
3012 basic_string&
3013 operator=(initializer_list<_CharT> __l)
3014 {
3015 this->assign(__l.begin(), __l.size());
3016 return *this;
3017 }
3018 #endif // C++11
3019
3020 // Iterators:
3021 /**
3022 * Returns a read/write iterator that points to the first character in
3023 * the %string. Unshares the string.
3024 */
3025 iterator
3026 begin() // FIXME C++11: should be noexcept.
3027 {
3028 _M_leak();
3029 return iterator(_M_data());
3030 }
3031
3032 /**
3033 * Returns a read-only (constant) iterator that points to the first
3034 * character in the %string.
3035 */
3036 const_iterator
3037 begin() const _GLIBCXX_NOEXCEPT
3038 { return const_iterator(_M_data()); }
3039
3040 /**
3041 * Returns a read/write iterator that points one past the last
3042 * character in the %string. Unshares the string.
3043 */
3044 iterator
3045 end() // FIXME C++11: should be noexcept.
3046 {
3047 _M_leak();
3048 return iterator(_M_data() + this->size());
3049 }
3050
3051 /**
3052 * Returns a read-only (constant) iterator that points one past the
3053 * last character in the %string.
3054 */
3055 const_iterator
3056 end() const _GLIBCXX_NOEXCEPT
3057 { return const_iterator(_M_data() + this->size()); }
3058
3059 /**
3060 * Returns a read/write reverse iterator that points to the last
3061 * character in the %string. Iteration is done in reverse element
3062 * order. Unshares the string.
3063 */
3064 reverse_iterator
3065 rbegin() // FIXME C++11: should be noexcept.
3066 { return reverse_iterator(this->end()); }
3067
3068 /**
3069 * Returns a read-only (constant) reverse iterator that points
3070 * to the last character in the %string. Iteration is done in
3071 * reverse element order.
3072 */
3073 const_reverse_iterator
3074 rbegin() const _GLIBCXX_NOEXCEPT
3075 { return const_reverse_iterator(this->end()); }
3076
3077 /**
3078 * Returns a read/write reverse iterator that points to one before the
3079 * first character in the %string. Iteration is done in reverse
3080 * element order. Unshares the string.
3081 */
3082 reverse_iterator
3083 rend() // FIXME C++11: should be noexcept.
3084 { return reverse_iterator(this->begin()); }
3085
3086 /**
3087 * Returns a read-only (constant) reverse iterator that points
3088 * to one before the first character in the %string. Iteration
3089 * is done in reverse element order.
3090 */
3091 const_reverse_iterator
3092 rend() const _GLIBCXX_NOEXCEPT
3093 { return const_reverse_iterator(this->begin()); }
3094
3095 #if __cplusplus >= 201103L
3096 /**
3097 * Returns a read-only (constant) iterator that points to the first
3098 * character in the %string.
3099 */
3100 const_iterator
3101 cbegin() const noexcept
3102 { return const_iterator(this->_M_data()); }
3103
3104 /**
3105 * Returns a read-only (constant) iterator that points one past the
3106 * last character in the %string.
3107 */
3108 const_iterator
3109 cend() const noexcept
3110 { return const_iterator(this->_M_data() + this->size()); }
3111
3112 /**
3113 * Returns a read-only (constant) reverse iterator that points
3114 * to the last character in the %string. Iteration is done in
3115 * reverse element order.
3116 */
3117 const_reverse_iterator
3118 crbegin() const noexcept
3119 { return const_reverse_iterator(this->end()); }
3120
3121 /**
3122 * Returns a read-only (constant) reverse iterator that points
3123 * to one before the first character in the %string. Iteration
3124 * is done in reverse element order.
3125 */
3126 const_reverse_iterator
3127 crend() const noexcept
3128 { return const_reverse_iterator(this->begin()); }
3129 #endif
3130
3131 public:
3132 // Capacity:
3133 /// Returns the number of characters in the string, not including any
3134 /// null-termination.
3135 size_type
3136 size() const _GLIBCXX_NOEXCEPT
3137 { return _M_rep()->_M_length; }
3138
3139 /// Returns the number of characters in the string, not including any
3140 /// null-termination.
3141 size_type
3142 length() const _GLIBCXX_NOEXCEPT
3143 { return _M_rep()->_M_length; }
3144
3145 /// Returns the size() of the largest possible %string.
3146 size_type
3147 max_size() const _GLIBCXX_NOEXCEPT
3148 { return _Rep::_S_max_size; }
3149
3150 /**
3151 * @brief Resizes the %string to the specified number of characters.
3152 * @param __n Number of characters the %string should contain.
3153 * @param __c Character to fill any new elements.
3154 *
3155 * This function will %resize the %string to the specified
3156 * number of characters. If the number is smaller than the
3157 * %string's current size the %string is truncated, otherwise
3158 * the %string is extended and new elements are %set to @a __c.
3159 */
3160 void
3161 resize(size_type __n, _CharT __c);
3162
3163 /**
3164 * @brief Resizes the %string to the specified number of characters.
3165 * @param __n Number of characters the %string should contain.
3166 *
3167 * This function will resize the %string to the specified length. If
3168 * the new size is smaller than the %string's current size the %string
3169 * is truncated, otherwise the %string is extended and new characters
3170 * are default-constructed. For basic types such as char, this means
3171 * setting them to 0.
3172 */
3173 void
3174 resize(size_type __n)
3175 { this->resize(__n, _CharT()); }
3176
3177 #if __cplusplus >= 201103L
3178 /// A non-binding request to reduce capacity() to size().
3179 void
3180 shrink_to_fit() _GLIBCXX_NOEXCEPT
3181 {
3182 if (capacity() > size())
3183 {
3184 __try
3185 { reserve(0); }
3186 __catch(...)
3187 { }
3188 }
3189 }
3190 #endif
3191
3192 /**
3193 * Returns the total number of characters that the %string can hold
3194 * before needing to allocate more memory.
3195 */
3196 size_type
3197 capacity() const _GLIBCXX_NOEXCEPT
3198 { return _M_rep()->_M_capacity; }
3199
3200 /**
3201 * @brief Attempt to preallocate enough memory for specified number of
3202 * characters.
3203 * @param __res_arg Number of characters required.
3204 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3205 *
3206 * This function attempts to reserve enough memory for the
3207 * %string to hold the specified number of characters. If the
3208 * number requested is more than max_size(), length_error is
3209 * thrown.
3210 *
3211 * The advantage of this function is that if optimal code is a
3212 * necessity and the user can determine the string length that will be
3213 * required, the user can reserve the memory in %advance, and thus
3214 * prevent a possible reallocation of memory and copying of %string
3215 * data.
3216 */
3217 void
3218 reserve(size_type __res_arg = 0);
3219
3220 /**
3221 * Erases the string, making it empty.
3222 */
3223 // PR 56166: this should not throw.
3224 void
3225 clear()
3226 { _M_mutate(0, this->size(), 0); }
3227
3228 /**
3229 * Returns true if the %string is empty. Equivalent to
3230 * <code>*this == ""</code>.
3231 */
3232 bool
3233 empty() const _GLIBCXX_NOEXCEPT
3234 { return this->size() == 0; }
3235
3236 // Element access:
3237 /**
3238 * @brief Subscript access to the data contained in the %string.
3239 * @param __pos The index of the character to access.
3240 * @return Read-only (constant) reference to the character.
3241 *
3242 * This operator allows for easy, array-style, data access.
3243 * Note that data access with this operator is unchecked and
3244 * out_of_range lookups are not defined. (For checked lookups
3245 * see at().)
3246 */
3247 const_reference
3248 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
3249 {
3250 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
3251 return _M_data()[__pos];
3252 }
3253
3254 /**
3255 * @brief Subscript access to the data contained in the %string.
3256 * @param __pos The index of the character to access.
3257 * @return Read/write reference to the character.
3258 *
3259 * This operator allows for easy, array-style, data access.
3260 * Note that data access with this operator is unchecked and
3261 * out_of_range lookups are not defined. (For checked lookups
3262 * see at().) Unshares the string.
3263 */
3264 reference
3265 operator[](size_type __pos)
3266 {
3267 // Allow pos == size() both in C++98 mode, as v3 extension,
3268 // and in C++11 mode.
3269 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
3270 // In pedantic mode be strict in C++98 mode.
3271 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
3272 _M_leak();
3273 return _M_data()[__pos];
3274 }
3275
3276 /**
3277 * @brief Provides access to the data contained in the %string.
3278 * @param __n The index of the character to access.
3279 * @return Read-only (const) reference to the character.
3280 * @throw std::out_of_range If @a n is an invalid index.
3281 *
3282 * This function provides for safer data access. The parameter is
3283 * first checked that it is in the range of the string. The function
3284 * throws out_of_range if the check fails.
3285 */
3286 const_reference
3287 at(size_type __n) const
3288 {
3289 if (__n >= this->size())
3290 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3291 "(which is %zu) >= this->size() "
3292 "(which is %zu)"),
3293 __n, this->size());
3294 return _M_data()[__n];
3295 }
3296
3297 /**
3298 * @brief Provides access to the data contained in the %string.
3299 * @param __n The index of the character to access.
3300 * @return Read/write reference to the character.
3301 * @throw std::out_of_range If @a n is an invalid index.
3302 *
3303 * This function provides for safer data access. The parameter is
3304 * first checked that it is in the range of the string. The function
3305 * throws out_of_range if the check fails. Success results in
3306 * unsharing the string.
3307 */
3308 reference
3309 at(size_type __n)
3310 {
3311 if (__n >= size())
3312 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3313 "(which is %zu) >= this->size() "
3314 "(which is %zu)"),
3315 __n, this->size());
3316 _M_leak();
3317 return _M_data()[__n];
3318 }
3319
3320 #if __cplusplus >= 201103L
3321 /**
3322 * Returns a read/write reference to the data at the first
3323 * element of the %string.
3324 */
3325 reference
3326 front()
3327 {
3328 _GLIBCXX_DEBUG_ASSERT(!empty());
3329 return operator[](0);
3330 }
3331
3332 /**
3333 * Returns a read-only (constant) reference to the data at the first
3334 * element of the %string.
3335 */
3336 const_reference
3337 front() const _GLIBCXX_NOEXCEPT
3338 {
3339 _GLIBCXX_DEBUG_ASSERT(!empty());
3340 return operator[](0);
3341 }
3342
3343 /**
3344 * Returns a read/write reference to the data at the last
3345 * element of the %string.
3346 */
3347 reference
3348 back()
3349 {
3350 _GLIBCXX_DEBUG_ASSERT(!empty());
3351 return operator[](this->size() - 1);
3352 }
3353
3354 /**
3355 * Returns a read-only (constant) reference to the data at the
3356 * last element of the %string.
3357 */
3358 const_reference
3359 back() const _GLIBCXX_NOEXCEPT
3360 {
3361 _GLIBCXX_DEBUG_ASSERT(!empty());
3362 return operator[](this->size() - 1);
3363 }
3364 #endif
3365
3366 // Modifiers:
3367 /**
3368 * @brief Append a string to this string.
3369 * @param __str The string to append.
3370 * @return Reference to this string.
3371 */
3372 basic_string&
3373 operator+=(const basic_string& __str)
3374 { return this->append(__str); }
3375
3376 /**
3377 * @brief Append a C string.
3378 * @param __s The C string to append.
3379 * @return Reference to this string.
3380 */
3381 basic_string&
3382 operator+=(const _CharT* __s)
3383 { return this->append(__s); }
3384
3385 /**
3386 * @brief Append a character.
3387 * @param __c The character to append.
3388 * @return Reference to this string.
3389 */
3390 basic_string&
3391 operator+=(_CharT __c)
3392 {
3393 this->push_back(__c);
3394 return *this;
3395 }
3396
3397 #if __cplusplus >= 201103L
3398 /**
3399 * @brief Append an initializer_list of characters.
3400 * @param __l The initializer_list of characters to be appended.
3401 * @return Reference to this string.
3402 */
3403 basic_string&
3404 operator+=(initializer_list<_CharT> __l)
3405 { return this->append(__l.begin(), __l.size()); }
3406 #endif // C++11
3407
3408 /**
3409 * @brief Append a string to this string.
3410 * @param __str The string to append.
3411 * @return Reference to this string.
3412 */
3413 basic_string&
3414 append(const basic_string& __str);
3415
3416 /**
3417 * @brief Append a substring.
3418 * @param __str The string to append.
3419 * @param __pos Index of the first character of str to append.
3420 * @param __n The number of characters to append.
3421 * @return Reference to this string.
3422 * @throw std::out_of_range if @a __pos is not a valid index.
3423 *
3424 * This function appends @a __n characters from @a __str
3425 * starting at @a __pos to this string. If @a __n is is larger
3426 * than the number of available characters in @a __str, the
3427 * remainder of @a __str is appended.
3428 */
3429 basic_string&
3430 append(const basic_string& __str, size_type __pos, size_type __n);
3431
3432 /**
3433 * @brief Append a C substring.
3434 * @param __s The C string to append.
3435 * @param __n The number of characters to append.
3436 * @return Reference to this string.
3437 */
3438 basic_string&
3439 append(const _CharT* __s, size_type __n);
3440
3441 /**
3442 * @brief Append a C string.
3443 * @param __s The C string to append.
3444 * @return Reference to this string.
3445 */
3446 basic_string&
3447 append(const _CharT* __s)
3448 {
3449 __glibcxx_requires_string(__s);
3450 return this->append(__s, traits_type::length(__s));
3451 }
3452
3453 /**
3454 * @brief Append multiple characters.
3455 * @param __n The number of characters to append.
3456 * @param __c The character to use.
3457 * @return Reference to this string.
3458 *
3459 * Appends __n copies of __c to this string.
3460 */
3461 basic_string&
3462 append(size_type __n, _CharT __c);
3463
3464 #if __cplusplus >= 201103L
3465 /**
3466 * @brief Append an initializer_list of characters.
3467 * @param __l The initializer_list of characters to append.
3468 * @return Reference to this string.
3469 */
3470 basic_string&
3471 append(initializer_list<_CharT> __l)
3472 { return this->append(__l.begin(), __l.size()); }
3473 #endif // C++11
3474
3475 /**
3476 * @brief Append a range of characters.
3477 * @param __first Iterator referencing the first character to append.
3478 * @param __last Iterator marking the end of the range.
3479 * @return Reference to this string.
3480 *
3481 * Appends characters in the range [__first,__last) to this string.
3482 */
3483 template<class _InputIterator>
3484 basic_string&
3485 append(_InputIterator __first, _InputIterator __last)
3486 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
3487
3488 /**
3489 * @brief Append a single character.
3490 * @param __c Character to append.
3491 */
3492 void
3493 push_back(_CharT __c)
3494 {
3495 const size_type __len = 1 + this->size();
3496 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3497 this->reserve(__len);
3498 traits_type::assign(_M_data()[this->size()], __c);
3499 _M_rep()->_M_set_length_and_sharable(__len);
3500 }
3501
3502 /**
3503 * @brief Set value to contents of another string.
3504 * @param __str Source string to use.
3505 * @return Reference to this string.
3506 */
3507 basic_string&
3508 assign(const basic_string& __str);
3509
3510 #if __cplusplus >= 201103L
3511 /**
3512 * @brief Set value to contents of another string.
3513 * @param __str Source string to use.
3514 * @return Reference to this string.
3515 *
3516 * This function sets this string to the exact contents of @a __str.
3517 * @a __str is a valid, but unspecified string.
3518 */
3519 // PR 58265, this should be noexcept.
3520 basic_string&
3521 assign(basic_string&& __str)
3522 {
3523 this->swap(__str);
3524 return *this;
3525 }
3526 #endif // C++11
3527
3528 /**
3529 * @brief Set value to a substring of a string.
3530 * @param __str The string to use.
3531 * @param __pos Index of the first character of str.
3532 * @param __n Number of characters to use.
3533 * @return Reference to this string.
3534 * @throw std::out_of_range if @a pos is not a valid index.
3535 *
3536 * This function sets this string to the substring of @a __str
3537 * consisting of @a __n characters at @a __pos. If @a __n is
3538 * is larger than the number of available characters in @a
3539 * __str, the remainder of @a __str is used.
3540 */
3541 basic_string&
3542 assign(const basic_string& __str, size_type __pos, size_type __n)
3543 { return this->assign(__str._M_data()
3544 + __str._M_check(__pos, "basic_string::assign"),
3545 __str._M_limit(__pos, __n)); }
3546
3547 /**
3548 * @brief Set value to a C substring.
3549 * @param __s The C string to use.
3550 * @param __n Number of characters to use.
3551 * @return Reference to this string.
3552 *
3553 * This function sets the value of this string to the first @a __n
3554 * characters of @a __s. If @a __n is is larger than the number of
3555 * available characters in @a __s, the remainder of @a __s is used.
3556 */
3557 basic_string&
3558 assign(const _CharT* __s, size_type __n);
3559
3560 /**
3561 * @brief Set value to contents of a C string.
3562 * @param __s The C string to use.
3563 * @return Reference to this string.
3564 *
3565 * This function sets the value of this string to the value of @a __s.
3566 * The data is copied, so there is no dependence on @a __s once the
3567 * function returns.
3568 */
3569 basic_string&
3570 assign(const _CharT* __s)
3571 {
3572 __glibcxx_requires_string(__s);
3573 return this->assign(__s, traits_type::length(__s));
3574 }
3575
3576 /**
3577 * @brief Set value to multiple characters.
3578 * @param __n Length of the resulting string.
3579 * @param __c The character to use.
3580 * @return Reference to this string.
3581 *
3582 * This function sets the value of this string to @a __n copies of
3583 * character @a __c.
3584 */
3585 basic_string&
3586 assign(size_type __n, _CharT __c)
3587 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
3588
3589 /**
3590 * @brief Set value to a range of characters.
3591 * @param __first Iterator referencing the first character to append.
3592 * @param __last Iterator marking the end of the range.
3593 * @return Reference to this string.
3594 *
3595 * Sets value of string to characters in the range [__first,__last).
3596 */
3597 template<class _InputIterator>
3598 basic_string&
3599 assign(_InputIterator __first, _InputIterator __last)
3600 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
3601
3602 #if __cplusplus >= 201103L
3603 /**
3604 * @brief Set value to an initializer_list of characters.
3605 * @param __l The initializer_list of characters to assign.
3606 * @return Reference to this string.
3607 */
3608 basic_string&
3609 assign(initializer_list<_CharT> __l)
3610 { return this->assign(__l.begin(), __l.size()); }
3611 #endif // C++11
3612
3613 /**
3614 * @brief Insert multiple characters.
3615 * @param __p Iterator referencing location in string to insert at.
3616 * @param __n Number of characters to insert
3617 * @param __c The character to insert.
3618 * @throw std::length_error If new length exceeds @c max_size().
3619 *
3620 * Inserts @a __n copies of character @a __c starting at the
3621 * position referenced by iterator @a __p. If adding
3622 * characters causes the length to exceed max_size(),
3623 * length_error is thrown. The value of the string doesn't
3624 * change if an error is thrown.
3625 */
3626 void
3627 insert(iterator __p, size_type __n, _CharT __c)
3628 { this->replace(__p, __p, __n, __c); }
3629
3630 /**
3631 * @brief Insert a range of characters.
3632 * @param __p Iterator referencing location in string to insert at.
3633 * @param __beg Start of range.
3634 * @param __end End of range.
3635 * @throw std::length_error If new length exceeds @c max_size().
3636 *
3637 * Inserts characters in range [__beg,__end). If adding
3638 * characters causes the length to exceed max_size(),
3639 * length_error is thrown. The value of the string doesn't
3640 * change if an error is thrown.
3641 */
3642 template<class _InputIterator>
3643 void
3644 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
3645 { this->replace(__p, __p, __beg, __end); }
3646
3647 #if __cplusplus >= 201103L
3648 /**
3649 * @brief Insert an initializer_list of characters.
3650 * @param __p Iterator referencing location in string to insert at.
3651 * @param __l The initializer_list of characters to insert.
3652 * @throw std::length_error If new length exceeds @c max_size().
3653 */
3654 void
3655 insert(iterator __p, initializer_list<_CharT> __l)
3656 {
3657 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
3658 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
3659 }
3660 #endif // C++11
3661
3662 /**
3663 * @brief Insert value of a string.
3664 * @param __pos1 Iterator referencing location in string to insert at.
3665 * @param __str The string to insert.
3666 * @return Reference to this string.
3667 * @throw std::length_error If new length exceeds @c max_size().
3668 *
3669 * Inserts value of @a __str starting at @a __pos1. If adding
3670 * characters causes the length to exceed max_size(),
3671 * length_error is thrown. The value of the string doesn't
3672 * change if an error is thrown.
3673 */
3674 basic_string&
3675 insert(size_type __pos1, const basic_string& __str)
3676 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
3677
3678 /**
3679 * @brief Insert a substring.
3680 * @param __pos1 Iterator referencing location in string to insert at.
3681 * @param __str The string to insert.
3682 * @param __pos2 Start of characters in str to insert.
3683 * @param __n Number of characters to insert.
3684 * @return Reference to this string.
3685 * @throw std::length_error If new length exceeds @c max_size().
3686 * @throw std::out_of_range If @a pos1 > size() or
3687 * @a __pos2 > @a str.size().
3688 *
3689 * Starting at @a pos1, insert @a __n character of @a __str
3690 * beginning with @a __pos2. If adding characters causes the
3691 * length to exceed max_size(), length_error is thrown. If @a
3692 * __pos1 is beyond the end of this string or @a __pos2 is
3693 * beyond the end of @a __str, out_of_range is thrown. The
3694 * value of the string doesn't change if an error is thrown.
3695 */
3696 basic_string&
3697 insert(size_type __pos1, const basic_string& __str,
3698 size_type __pos2, size_type __n)
3699 { return this->insert(__pos1, __str._M_data()
3700 + __str._M_check(__pos2, "basic_string::insert"),
3701 __str._M_limit(__pos2, __n)); }
3702
3703 /**
3704 * @brief Insert a C substring.
3705 * @param __pos Iterator referencing location in string to insert at.
3706 * @param __s The C string to insert.
3707 * @param __n The number of characters to insert.
3708 * @return Reference to this string.
3709 * @throw std::length_error If new length exceeds @c max_size().
3710 * @throw std::out_of_range If @a __pos is beyond the end of this
3711 * string.
3712 *
3713 * Inserts the first @a __n characters of @a __s starting at @a
3714 * __pos. If adding characters causes the length to exceed
3715 * max_size(), length_error is thrown. If @a __pos is beyond
3716 * end(), out_of_range is thrown. The value of the string
3717 * doesn't change if an error is thrown.
3718 */
3719 basic_string&
3720 insert(size_type __pos, const _CharT* __s, size_type __n);
3721
3722 /**
3723 * @brief Insert a C string.
3724 * @param __pos Iterator referencing location in string to insert at.
3725 * @param __s The C string to insert.
3726 * @return Reference to this string.
3727 * @throw std::length_error If new length exceeds @c max_size().
3728 * @throw std::out_of_range If @a pos is beyond the end of this
3729 * string.
3730 *
3731 * Inserts the first @a n characters of @a __s starting at @a __pos. If
3732 * adding characters causes the length to exceed max_size(),
3733 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
3734 * thrown. The value of the string doesn't change if an error is
3735 * thrown.
3736 */
3737 basic_string&
3738 insert(size_type __pos, const _CharT* __s)
3739 {
3740 __glibcxx_requires_string(__s);
3741 return this->insert(__pos, __s, traits_type::length(__s));
3742 }
3743
3744 /**
3745 * @brief Insert multiple characters.
3746 * @param __pos Index in string to insert at.
3747 * @param __n Number of characters to insert
3748 * @param __c The character to insert.
3749 * @return Reference to this string.
3750 * @throw std::length_error If new length exceeds @c max_size().
3751 * @throw std::out_of_range If @a __pos is beyond the end of this
3752 * string.
3753 *
3754 * Inserts @a __n copies of character @a __c starting at index
3755 * @a __pos. If adding characters causes the length to exceed
3756 * max_size(), length_error is thrown. If @a __pos > length(),
3757 * out_of_range is thrown. The value of the string doesn't
3758 * change if an error is thrown.
3759 */
3760 basic_string&
3761 insert(size_type __pos, size_type __n, _CharT __c)
3762 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
3763 size_type(0), __n, __c); }
3764
3765 /**
3766 * @brief Insert one character.
3767 * @param __p Iterator referencing position in string to insert at.
3768 * @param __c The character to insert.
3769 * @return Iterator referencing newly inserted char.
3770 * @throw std::length_error If new length exceeds @c max_size().
3771 *
3772 * Inserts character @a __c at position referenced by @a __p.
3773 * If adding character causes the length to exceed max_size(),
3774 * length_error is thrown. If @a __p is beyond end of string,
3775 * out_of_range is thrown. The value of the string doesn't
3776 * change if an error is thrown.
3777 */
3778 iterator
3779 insert(iterator __p, _CharT __c)
3780 {
3781 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
3782 const size_type __pos = __p - _M_ibegin();
3783 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
3784 _M_rep()->_M_set_leaked();
3785 return iterator(_M_data() + __pos);
3786 }
3787
3788 /**
3789 * @brief Remove characters.
3790 * @param __pos Index of first character to remove (default 0).
3791 * @param __n Number of characters to remove (default remainder).
3792 * @return Reference to this string.
3793 * @throw std::out_of_range If @a pos is beyond the end of this
3794 * string.
3795 *
3796 * Removes @a __n characters from this string starting at @a
3797 * __pos. The length of the string is reduced by @a __n. If
3798 * there are < @a __n characters to remove, the remainder of
3799 * the string is truncated. If @a __p is beyond end of string,
3800 * out_of_range is thrown. The value of the string doesn't
3801 * change if an error is thrown.
3802 */
3803 basic_string&
3804 erase(size_type __pos = 0, size_type __n = npos)
3805 {
3806 _M_mutate(_M_check(__pos, "basic_string::erase"),
3807 _M_limit(__pos, __n), size_type(0));
3808 return *this;
3809 }
3810
3811 /**
3812 * @brief Remove one character.
3813 * @param __position Iterator referencing the character to remove.
3814 * @return iterator referencing same location after removal.
3815 *
3816 * Removes the character at @a __position from this string. The value
3817 * of the string doesn't change if an error is thrown.
3818 */
3819 iterator
3820 erase(iterator __position)
3821 {
3822 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
3823 && __position < _M_iend());
3824 const size_type __pos = __position - _M_ibegin();
3825 _M_mutate(__pos, size_type(1), size_type(0));
3826 _M_rep()->_M_set_leaked();
3827 return iterator(_M_data() + __pos);
3828 }
3829
3830 /**
3831 * @brief Remove a range of characters.
3832 * @param __first Iterator referencing the first character to remove.
3833 * @param __last Iterator referencing the end of the range.
3834 * @return Iterator referencing location of first after removal.
3835 *
3836 * Removes the characters in the range [first,last) from this string.
3837 * The value of the string doesn't change if an error is thrown.
3838 */
3839 iterator
3840 erase(iterator __first, iterator __last);
3841
3842 #if __cplusplus >= 201103L
3843 /**
3844 * @brief Remove the last character.
3845 *
3846 * The string must be non-empty.
3847 */
3848 void
3849 pop_back() // FIXME C++11: should be noexcept.
3850 {
3851 _GLIBCXX_DEBUG_ASSERT(!empty());
3852 erase(size() - 1, 1);
3853 }
3854 #endif // C++11
3855
3856 /**
3857 * @brief Replace characters with value from another string.
3858 * @param __pos Index of first character to replace.
3859 * @param __n Number of characters to be replaced.
3860 * @param __str String to insert.
3861 * @return Reference to this string.
3862 * @throw std::out_of_range If @a pos is beyond the end of this
3863 * string.
3864 * @throw std::length_error If new length exceeds @c max_size().
3865 *
3866 * Removes the characters in the range [__pos,__pos+__n) from
3867 * this string. In place, the value of @a __str is inserted.
3868 * If @a __pos is beyond end of string, out_of_range is thrown.
3869 * If the length of the result exceeds max_size(), length_error
3870 * is thrown. The value of the string doesn't change if an
3871 * error is thrown.
3872 */
3873 basic_string&
3874 replace(size_type __pos, size_type __n, const basic_string& __str)
3875 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
3876
3877 /**
3878 * @brief Replace characters with value from another string.
3879 * @param __pos1 Index of first character to replace.
3880 * @param __n1 Number of characters to be replaced.
3881 * @param __str String to insert.
3882 * @param __pos2 Index of first character of str to use.
3883 * @param __n2 Number of characters from str to use.
3884 * @return Reference to this string.
3885 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
3886 * __str.size().
3887 * @throw std::length_error If new length exceeds @c max_size().
3888 *
3889 * Removes the characters in the range [__pos1,__pos1 + n) from this
3890 * string. In place, the value of @a __str is inserted. If @a __pos is
3891 * beyond end of string, out_of_range is thrown. If the length of the
3892 * result exceeds max_size(), length_error is thrown. The value of the
3893 * string doesn't change if an error is thrown.
3894 */
3895 basic_string&
3896 replace(size_type __pos1, size_type __n1, const basic_string& __str,
3897 size_type __pos2, size_type __n2)
3898 { return this->replace(__pos1, __n1, __str._M_data()
3899 + __str._M_check(__pos2, "basic_string::replace"),
3900 __str._M_limit(__pos2, __n2)); }
3901
3902 /**
3903 * @brief Replace characters with value of a C substring.
3904 * @param __pos Index of first character to replace.
3905 * @param __n1 Number of characters to be replaced.
3906 * @param __s C string to insert.
3907 * @param __n2 Number of characters from @a s to use.
3908 * @return Reference to this string.
3909 * @throw std::out_of_range If @a pos1 > size().
3910 * @throw std::length_error If new length exceeds @c max_size().
3911 *
3912 * Removes the characters in the range [__pos,__pos + __n1)
3913 * from this string. In place, the first @a __n2 characters of
3914 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
3915 * @a __pos is beyond end of string, out_of_range is thrown. If
3916 * the length of result exceeds max_size(), length_error is
3917 * thrown. The value of the string doesn't change if an error
3918 * is thrown.
3919 */
3920 basic_string&
3921 replace(size_type __pos, size_type __n1, const _CharT* __s,
3922 size_type __n2);
3923
3924 /**
3925 * @brief Replace characters with value of a C string.
3926 * @param __pos Index of first character to replace.
3927 * @param __n1 Number of characters to be replaced.
3928 * @param __s C string to insert.
3929 * @return Reference to this string.
3930 * @throw std::out_of_range If @a pos > size().
3931 * @throw std::length_error If new length exceeds @c max_size().
3932 *
3933 * Removes the characters in the range [__pos,__pos + __n1)
3934 * from this string. In place, the characters of @a __s are
3935 * inserted. If @a __pos is beyond end of string, out_of_range
3936 * is thrown. If the length of result exceeds max_size(),
3937 * length_error is thrown. The value of the string doesn't
3938 * change if an error is thrown.
3939 */
3940 basic_string&
3941 replace(size_type __pos, size_type __n1, const _CharT* __s)
3942 {
3943 __glibcxx_requires_string(__s);
3944 return this->replace(__pos, __n1, __s, traits_type::length(__s));
3945 }
3946
3947 /**
3948 * @brief Replace characters with multiple characters.
3949 * @param __pos Index of first character to replace.
3950 * @param __n1 Number of characters to be replaced.
3951 * @param __n2 Number of characters to insert.
3952 * @param __c Character to insert.
3953 * @return Reference to this string.
3954 * @throw std::out_of_range If @a __pos > size().
3955 * @throw std::length_error If new length exceeds @c max_size().
3956 *
3957 * Removes the characters in the range [pos,pos + n1) from this
3958 * string. In place, @a __n2 copies of @a __c are inserted.
3959 * If @a __pos is beyond end of string, out_of_range is thrown.
3960 * If the length of result exceeds max_size(), length_error is
3961 * thrown. The value of the string doesn't change if an error
3962 * is thrown.
3963 */
3964 basic_string&
3965 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
3966 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
3967 _M_limit(__pos, __n1), __n2, __c); }
3968
3969 /**
3970 * @brief Replace range of characters with string.
3971 * @param __i1 Iterator referencing start of range to replace.
3972 * @param __i2 Iterator referencing end of range to replace.
3973 * @param __str String value to insert.
3974 * @return Reference to this string.
3975 * @throw std::length_error If new length exceeds @c max_size().
3976 *
3977 * Removes the characters in the range [__i1,__i2). In place,
3978 * the value of @a __str is inserted. If the length of result
3979 * exceeds max_size(), length_error is thrown. The value of
3980 * the string doesn't change if an error is thrown.
3981 */
3982 basic_string&
3983 replace(iterator __i1, iterator __i2, const basic_string& __str)
3984 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
3985
3986 /**
3987 * @brief Replace range of characters with C substring.
3988 * @param __i1 Iterator referencing start of range to replace.
3989 * @param __i2 Iterator referencing end of range to replace.
3990 * @param __s C string value to insert.
3991 * @param __n Number of characters from s to insert.
3992 * @return Reference to this string.
3993 * @throw std::length_error If new length exceeds @c max_size().
3994 *
3995 * Removes the characters in the range [__i1,__i2). In place,
3996 * the first @a __n characters of @a __s are inserted. If the
3997 * length of result exceeds max_size(), length_error is thrown.
3998 * The value of the string doesn't change if an error is
3999 * thrown.
4000 */
4001 basic_string&
4002 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4003 {
4004 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4005 && __i2 <= _M_iend());
4006 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4007 }
4008
4009 /**
4010 * @brief Replace range of characters with C string.
4011 * @param __i1 Iterator referencing start of range to replace.
4012 * @param __i2 Iterator referencing end of range to replace.
4013 * @param __s C string value to insert.
4014 * @return Reference to this string.
4015 * @throw std::length_error If new length exceeds @c max_size().
4016 *
4017 * Removes the characters in the range [__i1,__i2). In place,
4018 * the characters of @a __s are inserted. If the length of
4019 * result exceeds max_size(), length_error is thrown. The
4020 * value of the string doesn't change if an error is thrown.
4021 */
4022 basic_string&
4023 replace(iterator __i1, iterator __i2, const _CharT* __s)
4024 {
4025 __glibcxx_requires_string(__s);
4026 return this->replace(__i1, __i2, __s, traits_type::length(__s));
4027 }
4028
4029 /**
4030 * @brief Replace range of characters with multiple characters
4031 * @param __i1 Iterator referencing start of range to replace.
4032 * @param __i2 Iterator referencing end of range to replace.
4033 * @param __n Number of characters to insert.
4034 * @param __c Character to insert.
4035 * @return Reference to this string.
4036 * @throw std::length_error If new length exceeds @c max_size().
4037 *
4038 * Removes the characters in the range [__i1,__i2). In place,
4039 * @a __n copies of @a __c are inserted. If the length of
4040 * result exceeds max_size(), length_error is thrown. The
4041 * value of the string doesn't change if an error is thrown.
4042 */
4043 basic_string&
4044 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4045 {
4046 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4047 && __i2 <= _M_iend());
4048 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4049 }
4050
4051 /**
4052 * @brief Replace range of characters with range.
4053 * @param __i1 Iterator referencing start of range to replace.
4054 * @param __i2 Iterator referencing end of range to replace.
4055 * @param __k1 Iterator referencing start of range to insert.
4056 * @param __k2 Iterator referencing end of range to insert.
4057 * @return Reference to this string.
4058 * @throw std::length_error If new length exceeds @c max_size().
4059 *
4060 * Removes the characters in the range [__i1,__i2). In place,
4061 * characters in the range [__k1,__k2) are inserted. If the
4062 * length of result exceeds max_size(), length_error is thrown.
4063 * The value of the string doesn't change if an error is
4064 * thrown.
4065 */
4066 template<class _InputIterator>
4067 basic_string&
4068 replace(iterator __i1, iterator __i2,
4069 _InputIterator __k1, _InputIterator __k2)
4070 {
4071 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4072 && __i2 <= _M_iend());
4073 __glibcxx_requires_valid_range(__k1, __k2);
4074 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4075 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4076 }
4077
4078 // Specializations for the common case of pointer and iterator:
4079 // useful to avoid the overhead of temporary buffering in _M_replace.
4080 basic_string&
4081 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4082 {
4083 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4084 && __i2 <= _M_iend());
4085 __glibcxx_requires_valid_range(__k1, __k2);
4086 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4087 __k1, __k2 - __k1);
4088 }
4089
4090 basic_string&
4091 replace(iterator __i1, iterator __i2,
4092 const _CharT* __k1, const _CharT* __k2)
4093 {
4094 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4095 && __i2 <= _M_iend());
4096 __glibcxx_requires_valid_range(__k1, __k2);
4097 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4098 __k1, __k2 - __k1);
4099 }
4100
4101 basic_string&
4102 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4103 {
4104 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4105 && __i2 <= _M_iend());
4106 __glibcxx_requires_valid_range(__k1, __k2);
4107 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4108 __k1.base(), __k2 - __k1);
4109 }
4110
4111 basic_string&
4112 replace(iterator __i1, iterator __i2,
4113 const_iterator __k1, const_iterator __k2)
4114 {
4115 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4116 && __i2 <= _M_iend());
4117 __glibcxx_requires_valid_range(__k1, __k2);
4118 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4119 __k1.base(), __k2 - __k1);
4120 }
4121
4122 #if __cplusplus >= 201103L
4123 /**
4124 * @brief Replace range of characters with initializer_list.
4125 * @param __i1 Iterator referencing start of range to replace.
4126 * @param __i2 Iterator referencing end of range to replace.
4127 * @param __l The initializer_list of characters to insert.
4128 * @return Reference to this string.
4129 * @throw std::length_error If new length exceeds @c max_size().
4130 *
4131 * Removes the characters in the range [__i1,__i2). In place,
4132 * characters in the range [__k1,__k2) are inserted. If the
4133 * length of result exceeds max_size(), length_error is thrown.
4134 * The value of the string doesn't change if an error is
4135 * thrown.
4136 */
4137 basic_string& replace(iterator __i1, iterator __i2,
4138 initializer_list<_CharT> __l)
4139 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
4140 #endif // C++11
4141
4142 private:
4143 template<class _Integer>
4144 basic_string&
4145 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
4146 _Integer __val, __true_type)
4147 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
4148
4149 template<class _InputIterator>
4150 basic_string&
4151 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
4152 _InputIterator __k2, __false_type);
4153
4154 basic_string&
4155 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
4156 _CharT __c);
4157
4158 basic_string&
4159 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
4160 size_type __n2);
4161
4162 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4163 // requires special behaviour if _InIter is an integral type
4164 template<class _InIterator>
4165 static _CharT*
4166 _S_construct_aux(_InIterator __beg, _InIterator __end,
4167 const _Alloc& __a, __false_type)
4168 {
4169 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
4170 return _S_construct(__beg, __end, __a, _Tag());
4171 }
4172
4173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4174 // 438. Ambiguity in the "do the right thing" clause
4175 template<class _Integer>
4176 static _CharT*
4177 _S_construct_aux(_Integer __beg, _Integer __end,
4178 const _Alloc& __a, __true_type)
4179 { return _S_construct_aux_2(static_cast<size_type>(__beg),
4180 __end, __a); }
4181
4182 static _CharT*
4183 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
4184 { return _S_construct(__req, __c, __a); }
4185
4186 template<class _InIterator>
4187 static _CharT*
4188 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
4189 {
4190 typedef typename std::__is_integer<_InIterator>::__type _Integral;
4191 return _S_construct_aux(__beg, __end, __a, _Integral());
4192 }
4193
4194 // For Input Iterators, used in istreambuf_iterators, etc.
4195 template<class _InIterator>
4196 static _CharT*
4197 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
4198 input_iterator_tag);
4199
4200 // For forward_iterators up to random_access_iterators, used for
4201 // string::iterator, _CharT*, etc.
4202 template<class _FwdIterator>
4203 static _CharT*
4204 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
4205 forward_iterator_tag);
4206
4207 static _CharT*
4208 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
4209
4210 public:
4211
4212 /**
4213 * @brief Copy substring into C string.
4214 * @param __s C string to copy value into.
4215 * @param __n Number of characters to copy.
4216 * @param __pos Index of first character to copy.
4217 * @return Number of characters actually copied
4218 * @throw std::out_of_range If __pos > size().
4219 *
4220 * Copies up to @a __n characters starting at @a __pos into the
4221 * C string @a __s. If @a __pos is %greater than size(),
4222 * out_of_range is thrown.
4223 */
4224 size_type
4225 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
4226
4227 /**
4228 * @brief Swap contents with another string.
4229 * @param __s String to swap with.
4230 *
4231 * Exchanges the contents of this string with that of @a __s in constant
4232 * time.
4233 */
4234 // PR 58265, this should be noexcept.
4235 void
4236 swap(basic_string& __s);
4237
4238 // String operations:
4239 /**
4240 * @brief Return const pointer to null-terminated contents.
4241 *
4242 * This is a handle to internal data. Do not modify or dire things may
4243 * happen.
4244 */
4245 const _CharT*
4246 c_str() const _GLIBCXX_NOEXCEPT
4247 { return _M_data(); }
4248
4249 /**
4250 * @brief Return const pointer to contents.
4251 *
4252 * This is a handle to internal data. Do not modify or dire things may
4253 * happen.
4254 */
4255 const _CharT*
4256 data() const _GLIBCXX_NOEXCEPT
4257 { return _M_data(); }
4258
4259 /**
4260 * @brief Return copy of allocator used to construct this string.
4261 */
4262 allocator_type
4263 get_allocator() const _GLIBCXX_NOEXCEPT
4264 { return _M_dataplus; }
4265
4266 /**
4267 * @brief Find position of a C substring.
4268 * @param __s C string to locate.
4269 * @param __pos Index of character to search from.
4270 * @param __n Number of characters from @a s to search for.
4271 * @return Index of start of first occurrence.
4272 *
4273 * Starting from @a __pos, searches forward for the first @a
4274 * __n characters in @a __s within this string. If found,
4275 * returns the index where it begins. If not found, returns
4276 * npos.
4277 */
4278 size_type
4279 find(const _CharT* __s, size_type __pos, size_type __n) const;
4280
4281 /**
4282 * @brief Find position of a string.
4283 * @param __str String to locate.
4284 * @param __pos Index of character to search from (default 0).
4285 * @return Index of start of first occurrence.
4286 *
4287 * Starting from @a __pos, searches forward for value of @a __str within
4288 * this string. If found, returns the index where it begins. If not
4289 * found, returns npos.
4290 */
4291 size_type
4292 find(const basic_string& __str, size_type __pos = 0) const
4293 _GLIBCXX_NOEXCEPT
4294 { return this->find(__str.data(), __pos, __str.size()); }
4295
4296 /**
4297 * @brief Find position of a C string.
4298 * @param __s C string to locate.
4299 * @param __pos Index of character to search from (default 0).
4300 * @return Index of start of first occurrence.
4301 *
4302 * Starting from @a __pos, searches forward for the value of @a
4303 * __s within this string. If found, returns the index where
4304 * it begins. If not found, returns npos.
4305 */
4306 size_type
4307 find(const _CharT* __s, size_type __pos = 0) const
4308 {
4309 __glibcxx_requires_string(__s);
4310 return this->find(__s, __pos, traits_type::length(__s));
4311 }
4312
4313 /**
4314 * @brief Find position of a character.
4315 * @param __c Character to locate.
4316 * @param __pos Index of character to search from (default 0).
4317 * @return Index of first occurrence.
4318 *
4319 * Starting from @a __pos, searches forward for @a __c within
4320 * this string. If found, returns the index where it was
4321 * found. If not found, returns npos.
4322 */
4323 size_type
4324 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
4325
4326 /**
4327 * @brief Find last position of a string.
4328 * @param __str String to locate.
4329 * @param __pos Index of character to search back from (default end).
4330 * @return Index of start of last occurrence.
4331 *
4332 * Starting from @a __pos, searches backward for value of @a
4333 * __str within this string. If found, returns the index where
4334 * it begins. If not found, returns npos.
4335 */
4336 size_type
4337 rfind(const basic_string& __str, size_type __pos = npos) const
4338 _GLIBCXX_NOEXCEPT
4339 { return this->rfind(__str.data(), __pos, __str.size()); }
4340
4341 /**
4342 * @brief Find last position of a C substring.
4343 * @param __s C string to locate.
4344 * @param __pos Index of character to search back from.
4345 * @param __n Number of characters from s to search for.
4346 * @return Index of start of last occurrence.
4347 *
4348 * Starting from @a __pos, searches backward for the first @a
4349 * __n characters in @a __s within this string. If found,
4350 * returns the index where it begins. If not found, returns
4351 * npos.
4352 */
4353 size_type
4354 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
4355
4356 /**
4357 * @brief Find last position of a C string.
4358 * @param __s C string to locate.
4359 * @param __pos Index of character to start search at (default end).
4360 * @return Index of start of last occurrence.
4361 *
4362 * Starting from @a __pos, searches backward for the value of
4363 * @a __s within this string. If found, returns the index
4364 * where it begins. If not found, returns npos.
4365 */
4366 size_type
4367 rfind(const _CharT* __s, size_type __pos = npos) const
4368 {
4369 __glibcxx_requires_string(__s);
4370 return this->rfind(__s, __pos, traits_type::length(__s));
4371 }
4372
4373 /**
4374 * @brief Find last position of a character.
4375 * @param __c Character to locate.
4376 * @param __pos Index of character to search back from (default end).
4377 * @return Index of last occurrence.
4378 *
4379 * Starting from @a __pos, searches backward for @a __c within
4380 * this string. If found, returns the index where it was
4381 * found. If not found, returns npos.
4382 */
4383 size_type
4384 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
4385
4386 /**
4387 * @brief Find position of a character of string.
4388 * @param __str String containing characters to locate.
4389 * @param __pos Index of character to search from (default 0).
4390 * @return Index of first occurrence.
4391 *
4392 * Starting from @a __pos, searches forward for one of the
4393 * characters of @a __str within this string. If found,
4394 * returns the index where it was found. If not found, returns
4395 * npos.
4396 */
4397 size_type
4398 find_first_of(const basic_string& __str, size_type __pos = 0) const
4399 _GLIBCXX_NOEXCEPT
4400 { return this->find_first_of(__str.data(), __pos, __str.size()); }
4401
4402 /**
4403 * @brief Find position of a character of C substring.
4404 * @param __s String containing characters to locate.
4405 * @param __pos Index of character to search from.
4406 * @param __n Number of characters from s to search for.
4407 * @return Index of first occurrence.
4408 *
4409 * Starting from @a __pos, searches forward for one of the
4410 * first @a __n characters of @a __s within this string. If
4411 * found, returns the index where it was found. If not found,
4412 * returns npos.
4413 */
4414 size_type
4415 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
4416
4417 /**
4418 * @brief Find position of a character of C string.
4419 * @param __s String containing characters to locate.
4420 * @param __pos Index of character to search from (default 0).
4421 * @return Index of first occurrence.
4422 *
4423 * Starting from @a __pos, searches forward for one of the
4424 * characters of @a __s within this string. If found, returns
4425 * the index where it was found. If not found, returns npos.
4426 */
4427 size_type
4428 find_first_of(const _CharT* __s, size_type __pos = 0) const
4429 {
4430 __glibcxx_requires_string(__s);
4431 return this->find_first_of(__s, __pos, traits_type::length(__s));
4432 }
4433
4434 /**
4435 * @brief Find position of a character.
4436 * @param __c Character to locate.
4437 * @param __pos Index of character to search from (default 0).
4438 * @return Index of first occurrence.
4439 *
4440 * Starting from @a __pos, searches forward for the character
4441 * @a __c within this string. If found, returns the index
4442 * where it was found. If not found, returns npos.
4443 *
4444 * Note: equivalent to find(__c, __pos).
4445 */
4446 size_type
4447 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
4448 { return this->find(__c, __pos); }
4449
4450 /**
4451 * @brief Find last position of a character of string.
4452 * @param __str String containing characters to locate.
4453 * @param __pos Index of character to search back from (default end).
4454 * @return Index of last occurrence.
4455 *
4456 * Starting from @a __pos, searches backward for one of the
4457 * characters of @a __str within this string. If found,
4458 * returns the index where it was found. If not found, returns
4459 * npos.
4460 */
4461 size_type
4462 find_last_of(const basic_string& __str, size_type __pos = npos) const
4463 _GLIBCXX_NOEXCEPT
4464 { return this->find_last_of(__str.data(), __pos, __str.size()); }
4465
4466 /**
4467 * @brief Find last position of a character of C substring.
4468 * @param __s C string containing characters to locate.
4469 * @param __pos Index of character to search back from.
4470 * @param __n Number of characters from s to search for.
4471 * @return Index of last occurrence.
4472 *
4473 * Starting from @a __pos, searches backward for one of the
4474 * first @a __n characters of @a __s within this string. If
4475 * found, returns the index where it was found. If not found,
4476 * returns npos.
4477 */
4478 size_type
4479 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
4480
4481 /**
4482 * @brief Find last position of a character of C string.
4483 * @param __s C string containing characters to locate.
4484 * @param __pos Index of character to search back from (default end).
4485 * @return Index of last occurrence.
4486 *
4487 * Starting from @a __pos, searches backward for one of the
4488 * characters of @a __s within this string. If found, returns
4489 * the index where it was found. If not found, returns npos.
4490 */
4491 size_type
4492 find_last_of(const _CharT* __s, size_type __pos = npos) const
4493 {
4494 __glibcxx_requires_string(__s);
4495 return this->find_last_of(__s, __pos, traits_type::length(__s));
4496 }
4497
4498 /**
4499 * @brief Find last position of a character.
4500 * @param __c Character to locate.
4501 * @param __pos Index of character to search back from (default end).
4502 * @return Index of last occurrence.
4503 *
4504 * Starting from @a __pos, searches backward for @a __c within
4505 * this string. If found, returns the index where it was
4506 * found. If not found, returns npos.
4507 *
4508 * Note: equivalent to rfind(__c, __pos).
4509 */
4510 size_type
4511 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
4512 { return this->rfind(__c, __pos); }
4513
4514 /**
4515 * @brief Find position of a character not in string.
4516 * @param __str String containing characters to avoid.
4517 * @param __pos Index of character to search from (default 0).
4518 * @return Index of first occurrence.
4519 *
4520 * Starting from @a __pos, searches forward for a character not contained
4521 * in @a __str within this string. If found, returns the index where it
4522 * was found. If not found, returns npos.
4523 */
4524 size_type
4525 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
4526 _GLIBCXX_NOEXCEPT
4527 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
4528
4529 /**
4530 * @brief Find position of a character not in C substring.
4531 * @param __s C string containing characters to avoid.
4532 * @param __pos Index of character to search from.
4533 * @param __n Number of characters from __s to consider.
4534 * @return Index of first occurrence.
4535 *
4536 * Starting from @a __pos, searches forward for a character not
4537 * contained in the first @a __n characters of @a __s within
4538 * this string. If found, returns the index where it was
4539 * found. If not found, returns npos.
4540 */
4541 size_type
4542 find_first_not_of(const _CharT* __s, size_type __pos,
4543 size_type __n) const;
4544
4545 /**
4546 * @brief Find position of a character not in C string.
4547 * @param __s C string containing characters to avoid.
4548 * @param __pos Index of character to search from (default 0).
4549 * @return Index of first occurrence.
4550 *
4551 * Starting from @a __pos, searches forward for a character not
4552 * contained in @a __s within this string. If found, returns
4553 * the index where it was found. If not found, returns npos.
4554 */
4555 size_type
4556 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
4557 {
4558 __glibcxx_requires_string(__s);
4559 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
4560 }
4561
4562 /**
4563 * @brief Find position of a different character.
4564 * @param __c Character to avoid.
4565 * @param __pos Index of character to search from (default 0).
4566 * @return Index of first occurrence.
4567 *
4568 * Starting from @a __pos, searches forward for a character
4569 * other than @a __c within this string. If found, returns the
4570 * index where it was found. If not found, returns npos.
4571 */
4572 size_type
4573 find_first_not_of(_CharT __c, size_type __pos = 0) const
4574 _GLIBCXX_NOEXCEPT;
4575
4576 /**
4577 * @brief Find last position of a character not in string.
4578 * @param __str String containing characters to avoid.
4579 * @param __pos Index of character to search back from (default end).
4580 * @return Index of last occurrence.
4581 *
4582 * Starting from @a __pos, searches backward for a character
4583 * not contained in @a __str within this string. If found,
4584 * returns the index where it was found. If not found, returns
4585 * npos.
4586 */
4587 size_type
4588 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
4589 _GLIBCXX_NOEXCEPT
4590 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
4591
4592 /**
4593 * @brief Find last position of a character not in C substring.
4594 * @param __s C string containing characters to avoid.
4595 * @param __pos Index of character to search back from.
4596 * @param __n Number of characters from s to consider.
4597 * @return Index of last occurrence.
4598 *
4599 * Starting from @a __pos, searches backward for a character not
4600 * contained in the first @a __n characters of @a __s within this string.
4601 * If found, returns the index where it was found. If not found,
4602 * returns npos.
4603 */
4604 size_type
4605 find_last_not_of(const _CharT* __s, size_type __pos,
4606 size_type __n) const;
4607 /**
4608 * @brief Find last position of a character not in C string.
4609 * @param __s C string containing characters to avoid.
4610 * @param __pos Index of character to search back from (default end).
4611 * @return Index of last occurrence.
4612 *
4613 * Starting from @a __pos, searches backward for a character
4614 * not contained in @a __s within this string. If found,
4615 * returns the index where it was found. If not found, returns
4616 * npos.
4617 */
4618 size_type
4619 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
4620 {
4621 __glibcxx_requires_string(__s);
4622 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
4623 }
4624
4625 /**
4626 * @brief Find last position of a different character.
4627 * @param __c Character to avoid.
4628 * @param __pos Index of character to search back from (default end).
4629 * @return Index of last occurrence.
4630 *
4631 * Starting from @a __pos, searches backward for a character other than
4632 * @a __c within this string. If found, returns the index where it was
4633 * found. If not found, returns npos.
4634 */
4635 size_type
4636 find_last_not_of(_CharT __c, size_type __pos = npos) const
4637 _GLIBCXX_NOEXCEPT;
4638
4639 /**
4640 * @brief Get a substring.
4641 * @param __pos Index of first character (default 0).
4642 * @param __n Number of characters in substring (default remainder).
4643 * @return The new string.
4644 * @throw std::out_of_range If __pos > size().
4645 *
4646 * Construct and return a new string using the @a __n
4647 * characters starting at @a __pos. If the string is too
4648 * short, use the remainder of the characters. If @a __pos is
4649 * beyond the end of the string, out_of_range is thrown.
4650 */
4651 basic_string
4652 substr(size_type __pos = 0, size_type __n = npos) const
4653 { return basic_string(*this,
4654 _M_check(__pos, "basic_string::substr"), __n); }
4655
4656 /**
4657 * @brief Compare to a string.
4658 * @param __str String to compare against.
4659 * @return Integer < 0, 0, or > 0.
4660 *
4661 * Returns an integer < 0 if this string is ordered before @a
4662 * __str, 0 if their values are equivalent, or > 0 if this
4663 * string is ordered after @a __str. Determines the effective
4664 * length rlen of the strings to compare as the smallest of
4665 * size() and str.size(). The function then compares the two
4666 * strings by calling traits::compare(data(), str.data(),rlen).
4667 * If the result of the comparison is nonzero returns it,
4668 * otherwise the shorter one is ordered first.
4669 */
4670 int
4671 compare(const basic_string& __str) const
4672 {
4673 const size_type __size = this->size();
4674 const size_type __osize = __str.size();
4675 const size_type __len = std::min(__size, __osize);
4676
4677 int __r = traits_type::compare(_M_data(), __str.data(), __len);
4678 if (!__r)
4679 __r = _S_compare(__size, __osize);
4680 return __r;
4681 }
4682
4683 /**
4684 * @brief Compare substring to a string.
4685 * @param __pos Index of first character of substring.
4686 * @param __n Number of characters in substring.
4687 * @param __str String to compare against.
4688 * @return Integer < 0, 0, or > 0.
4689 *
4690 * Form the substring of this string from the @a __n characters
4691 * starting at @a __pos. Returns an integer < 0 if the
4692 * substring is ordered before @a __str, 0 if their values are
4693 * equivalent, or > 0 if the substring is ordered after @a
4694 * __str. Determines the effective length rlen of the strings
4695 * to compare as the smallest of the length of the substring
4696 * and @a __str.size(). The function then compares the two
4697 * strings by calling
4698 * traits::compare(substring.data(),str.data(),rlen). If the
4699 * result of the comparison is nonzero returns it, otherwise
4700 * the shorter one is ordered first.
4701 */
4702 int
4703 compare(size_type __pos, size_type __n, const basic_string& __str) const;
4704
4705 /**
4706 * @brief Compare substring to a substring.
4707 * @param __pos1 Index of first character of substring.
4708 * @param __n1 Number of characters in substring.
4709 * @param __str String to compare against.
4710 * @param __pos2 Index of first character of substring of str.
4711 * @param __n2 Number of characters in substring of str.
4712 * @return Integer < 0, 0, or > 0.
4713 *
4714 * Form the substring of this string from the @a __n1
4715 * characters starting at @a __pos1. Form the substring of @a
4716 * __str from the @a __n2 characters starting at @a __pos2.
4717 * Returns an integer < 0 if this substring is ordered before
4718 * the substring of @a __str, 0 if their values are equivalent,
4719 * or > 0 if this substring is ordered after the substring of
4720 * @a __str. Determines the effective length rlen of the
4721 * strings to compare as the smallest of the lengths of the
4722 * substrings. The function then compares the two strings by
4723 * calling
4724 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
4725 * If the result of the comparison is nonzero returns it,
4726 * otherwise the shorter one is ordered first.
4727 */
4728 int
4729 compare(size_type __pos1, size_type __n1, const basic_string& __str,
4730 size_type __pos2, size_type __n2) const;
4731
4732 /**
4733 * @brief Compare to a C string.
4734 * @param __s C string to compare against.
4735 * @return Integer < 0, 0, or > 0.
4736 *
4737 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
4738 * their values are equivalent, or > 0 if this string is ordered after
4739 * @a __s. Determines the effective length rlen of the strings to
4740 * compare as the smallest of size() and the length of a string
4741 * constructed from @a __s. The function then compares the two strings
4742 * by calling traits::compare(data(),s,rlen). If the result of the
4743 * comparison is nonzero returns it, otherwise the shorter one is
4744 * ordered first.
4745 */
4746 int
4747 compare(const _CharT* __s) const;
4748
4749 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4750 // 5 String::compare specification questionable
4751 /**
4752 * @brief Compare substring to a C string.
4753 * @param __pos Index of first character of substring.
4754 * @param __n1 Number of characters in substring.
4755 * @param __s C string to compare against.
4756 * @return Integer < 0, 0, or > 0.
4757 *
4758 * Form the substring of this string from the @a __n1
4759 * characters starting at @a pos. Returns an integer < 0 if
4760 * the substring is ordered before @a __s, 0 if their values
4761 * are equivalent, or > 0 if the substring is ordered after @a
4762 * __s. Determines the effective length rlen of the strings to
4763 * compare as the smallest of the length of the substring and
4764 * the length of a string constructed from @a __s. The
4765 * function then compares the two string by calling
4766 * traits::compare(substring.data(),__s,rlen). If the result of
4767 * the comparison is nonzero returns it, otherwise the shorter
4768 * one is ordered first.
4769 */
4770 int
4771 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
4772
4773 /**
4774 * @brief Compare substring against a character %array.
4775 * @param __pos Index of first character of substring.
4776 * @param __n1 Number of characters in substring.
4777 * @param __s character %array to compare against.
4778 * @param __n2 Number of characters of s.
4779 * @return Integer < 0, 0, or > 0.
4780 *
4781 * Form the substring of this string from the @a __n1
4782 * characters starting at @a __pos. Form a string from the
4783 * first @a __n2 characters of @a __s. Returns an integer < 0
4784 * if this substring is ordered before the string from @a __s,
4785 * 0 if their values are equivalent, or > 0 if this substring
4786 * is ordered after the string from @a __s. Determines the
4787 * effective length rlen of the strings to compare as the
4788 * smallest of the length of the substring and @a __n2. The
4789 * function then compares the two strings by calling
4790 * traits::compare(substring.data(),s,rlen). If the result of
4791 * the comparison is nonzero returns it, otherwise the shorter
4792 * one is ordered first.
4793 *
4794 * NB: s must have at least n2 characters, &apos;\\0&apos; has
4795 * no special meaning.
4796 */
4797 int
4798 compare(size_type __pos, size_type __n1, const _CharT* __s,
4799 size_type __n2) const;
4800 };
4801 #endif // !_GLIBCXX_USE_CXX11_ABI
4802
4803 // operator+
4804 /**
4805 * @brief Concatenate two strings.
4806 * @param __lhs First string.
4807 * @param __rhs Last string.
4808 * @return New string with value of @a __lhs followed by @a __rhs.
4809 */
4810 template<typename _CharT, typename _Traits, typename _Alloc>
4811 basic_string<_CharT, _Traits, _Alloc>
4812 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4813 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4814 {
4815 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
4816 __str.append(__rhs);
4817 return __str;
4818 }
4819
4820 /**
4821 * @brief Concatenate C string and string.
4822 * @param __lhs First string.
4823 * @param __rhs Last string.
4824 * @return New string with value of @a __lhs followed by @a __rhs.
4825 */
4826 template<typename _CharT, typename _Traits, typename _Alloc>
4827 basic_string<_CharT,_Traits,_Alloc>
4828 operator+(const _CharT* __lhs,
4829 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
4830
4831 /**
4832 * @brief Concatenate character and string.
4833 * @param __lhs First string.
4834 * @param __rhs Last string.
4835 * @return New string with @a __lhs followed by @a __rhs.
4836 */
4837 template<typename _CharT, typename _Traits, typename _Alloc>
4838 basic_string<_CharT,_Traits,_Alloc>
4839 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
4840
4841 /**
4842 * @brief Concatenate string and C string.
4843 * @param __lhs First string.
4844 * @param __rhs Last string.
4845 * @return New string with @a __lhs followed by @a __rhs.
4846 */
4847 template<typename _CharT, typename _Traits, typename _Alloc>
4848 inline basic_string<_CharT, _Traits, _Alloc>
4849 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4850 const _CharT* __rhs)
4851 {
4852 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
4853 __str.append(__rhs);
4854 return __str;
4855 }
4856
4857 /**
4858 * @brief Concatenate string and character.
4859 * @param __lhs First string.
4860 * @param __rhs Last string.
4861 * @return New string with @a __lhs followed by @a __rhs.
4862 */
4863 template<typename _CharT, typename _Traits, typename _Alloc>
4864 inline basic_string<_CharT, _Traits, _Alloc>
4865 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
4866 {
4867 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
4868 typedef typename __string_type::size_type __size_type;
4869 __string_type __str(__lhs);
4870 __str.append(__size_type(1), __rhs);
4871 return __str;
4872 }
4873
4874 #if __cplusplus >= 201103L
4875 template<typename _CharT, typename _Traits, typename _Alloc>
4876 inline basic_string<_CharT, _Traits, _Alloc>
4877 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4878 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4879 { return std::move(__lhs.append(__rhs)); }
4880
4881 template<typename _CharT, typename _Traits, typename _Alloc>
4882 inline basic_string<_CharT, _Traits, _Alloc>
4883 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4884 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4885 { return std::move(__rhs.insert(0, __lhs)); }
4886
4887 template<typename _CharT, typename _Traits, typename _Alloc>
4888 inline basic_string<_CharT, _Traits, _Alloc>
4889 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4890 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4891 {
4892 const auto __size = __lhs.size() + __rhs.size();
4893 const bool __cond = (__size > __lhs.capacity()
4894 && __size <= __rhs.capacity());
4895 return __cond ? std::move(__rhs.insert(0, __lhs))
4896 : std::move(__lhs.append(__rhs));
4897 }
4898
4899 template<typename _CharT, typename _Traits, typename _Alloc>
4900 inline basic_string<_CharT, _Traits, _Alloc>
4901 operator+(const _CharT* __lhs,
4902 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4903 { return std::move(__rhs.insert(0, __lhs)); }
4904
4905 template<typename _CharT, typename _Traits, typename _Alloc>
4906 inline basic_string<_CharT, _Traits, _Alloc>
4907 operator+(_CharT __lhs,
4908 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4909 { return std::move(__rhs.insert(0, 1, __lhs)); }
4910
4911 template<typename _CharT, typename _Traits, typename _Alloc>
4912 inline basic_string<_CharT, _Traits, _Alloc>
4913 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4914 const _CharT* __rhs)
4915 { return std::move(__lhs.append(__rhs)); }
4916
4917 template<typename _CharT, typename _Traits, typename _Alloc>
4918 inline basic_string<_CharT, _Traits, _Alloc>
4919 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4920 _CharT __rhs)
4921 { return std::move(__lhs.append(1, __rhs)); }
4922 #endif
4923
4924 // operator ==
4925 /**
4926 * @brief Test equivalence of two strings.
4927 * @param __lhs First string.
4928 * @param __rhs Second string.
4929 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4930 */
4931 template<typename _CharT, typename _Traits, typename _Alloc>
4932 inline bool
4933 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4934 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4935 { return __lhs.compare(__rhs) == 0; }
4936
4937 template<typename _CharT>
4938 inline
4939 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
4940 operator==(const basic_string<_CharT>& __lhs,
4941 const basic_string<_CharT>& __rhs)
4942 { return (__lhs.size() == __rhs.size()
4943 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
4944 __lhs.size())); }
4945
4946 /**
4947 * @brief Test equivalence of C string and string.
4948 * @param __lhs C string.
4949 * @param __rhs String.
4950 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4951 */
4952 template<typename _CharT, typename _Traits, typename _Alloc>
4953 inline bool
4954 operator==(const _CharT* __lhs,
4955 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4956 { return __rhs.compare(__lhs) == 0; }
4957
4958 /**
4959 * @brief Test equivalence of string and C string.
4960 * @param __lhs String.
4961 * @param __rhs C string.
4962 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4963 */
4964 template<typename _CharT, typename _Traits, typename _Alloc>
4965 inline bool
4966 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4967 const _CharT* __rhs)
4968 { return __lhs.compare(__rhs) == 0; }
4969
4970 // operator !=
4971 /**
4972 * @brief Test difference of two strings.
4973 * @param __lhs First string.
4974 * @param __rhs Second string.
4975 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4976 */
4977 template<typename _CharT, typename _Traits, typename _Alloc>
4978 inline bool
4979 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4980 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4981 { return !(__lhs == __rhs); }
4982
4983 /**
4984 * @brief Test difference of C string and string.
4985 * @param __lhs C string.
4986 * @param __rhs String.
4987 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4988 */
4989 template<typename _CharT, typename _Traits, typename _Alloc>
4990 inline bool
4991 operator!=(const _CharT* __lhs,
4992 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4993 { return !(__lhs == __rhs); }
4994
4995 /**
4996 * @brief Test difference of string and C string.
4997 * @param __lhs String.
4998 * @param __rhs C string.
4999 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5000 */
5001 template<typename _CharT, typename _Traits, typename _Alloc>
5002 inline bool
5003 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5004 const _CharT* __rhs)
5005 { return !(__lhs == __rhs); }
5006
5007 // operator <
5008 /**
5009 * @brief Test if string precedes string.
5010 * @param __lhs First string.
5011 * @param __rhs Second string.
5012 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5013 */
5014 template<typename _CharT, typename _Traits, typename _Alloc>
5015 inline bool
5016 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5017 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5018 { return __lhs.compare(__rhs) < 0; }
5019
5020 /**
5021 * @brief Test if string precedes C string.
5022 * @param __lhs String.
5023 * @param __rhs C string.
5024 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5025 */
5026 template<typename _CharT, typename _Traits, typename _Alloc>
5027 inline bool
5028 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5029 const _CharT* __rhs)
5030 { return __lhs.compare(__rhs) < 0; }
5031
5032 /**
5033 * @brief Test if C string precedes string.
5034 * @param __lhs C string.
5035 * @param __rhs String.
5036 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5037 */
5038 template<typename _CharT, typename _Traits, typename _Alloc>
5039 inline bool
5040 operator<(const _CharT* __lhs,
5041 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5042 { return __rhs.compare(__lhs) > 0; }
5043
5044 // operator >
5045 /**
5046 * @brief Test if string follows string.
5047 * @param __lhs First string.
5048 * @param __rhs Second string.
5049 * @return True if @a __lhs follows @a __rhs. False otherwise.
5050 */
5051 template<typename _CharT, typename _Traits, typename _Alloc>
5052 inline bool
5053 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5054 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5055 { return __lhs.compare(__rhs) > 0; }
5056
5057 /**
5058 * @brief Test if string follows C string.
5059 * @param __lhs String.
5060 * @param __rhs C string.
5061 * @return True if @a __lhs follows @a __rhs. False otherwise.
5062 */
5063 template<typename _CharT, typename _Traits, typename _Alloc>
5064 inline bool
5065 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5066 const _CharT* __rhs)
5067 { return __lhs.compare(__rhs) > 0; }
5068
5069 /**
5070 * @brief Test if C string follows string.
5071 * @param __lhs C string.
5072 * @param __rhs String.
5073 * @return True if @a __lhs follows @a __rhs. False otherwise.
5074 */
5075 template<typename _CharT, typename _Traits, typename _Alloc>
5076 inline bool
5077 operator>(const _CharT* __lhs,
5078 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5079 { return __rhs.compare(__lhs) < 0; }
5080
5081 // operator <=
5082 /**
5083 * @brief Test if string doesn't follow string.
5084 * @param __lhs First string.
5085 * @param __rhs Second string.
5086 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5087 */
5088 template<typename _CharT, typename _Traits, typename _Alloc>
5089 inline bool
5090 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5091 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5092 { return __lhs.compare(__rhs) <= 0; }
5093
5094 /**
5095 * @brief Test if string doesn't follow C string.
5096 * @param __lhs String.
5097 * @param __rhs C string.
5098 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5099 */
5100 template<typename _CharT, typename _Traits, typename _Alloc>
5101 inline bool
5102 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5103 const _CharT* __rhs)
5104 { return __lhs.compare(__rhs) <= 0; }
5105
5106 /**
5107 * @brief Test if C string doesn't follow string.
5108 * @param __lhs C string.
5109 * @param __rhs String.
5110 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5111 */
5112 template<typename _CharT, typename _Traits, typename _Alloc>
5113 inline bool
5114 operator<=(const _CharT* __lhs,
5115 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5116 { return __rhs.compare(__lhs) >= 0; }
5117
5118 // operator >=
5119 /**
5120 * @brief Test if string doesn't precede string.
5121 * @param __lhs First string.
5122 * @param __rhs Second string.
5123 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5124 */
5125 template<typename _CharT, typename _Traits, typename _Alloc>
5126 inline bool
5127 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5128 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5129 { return __lhs.compare(__rhs) >= 0; }
5130
5131 /**
5132 * @brief Test if string doesn't precede C string.
5133 * @param __lhs String.
5134 * @param __rhs C string.
5135 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5136 */
5137 template<typename _CharT, typename _Traits, typename _Alloc>
5138 inline bool
5139 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5140 const _CharT* __rhs)
5141 { return __lhs.compare(__rhs) >= 0; }
5142
5143 /**
5144 * @brief Test if C string doesn't precede string.
5145 * @param __lhs C string.
5146 * @param __rhs String.
5147 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5148 */
5149 template<typename _CharT, typename _Traits, typename _Alloc>
5150 inline bool
5151 operator>=(const _CharT* __lhs,
5152 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5153 { return __rhs.compare(__lhs) <= 0; }
5154
5155 /**
5156 * @brief Swap contents of two strings.
5157 * @param __lhs First string.
5158 * @param __rhs Second string.
5159 *
5160 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5161 */
5162 template<typename _CharT, typename _Traits, typename _Alloc>
5163 inline void
5164 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
5165 basic_string<_CharT, _Traits, _Alloc>& __rhs)
5166 { __lhs.swap(__rhs); }
5167
5168
5169 /**
5170 * @brief Read stream into a string.
5171 * @param __is Input stream.
5172 * @param __str Buffer to store into.
5173 * @return Reference to the input stream.
5174 *
5175 * Stores characters from @a __is into @a __str until whitespace is
5176 * found, the end of the stream is encountered, or str.max_size()
5177 * is reached. If is.width() is non-zero, that is the limit on the
5178 * number of characters stored into @a __str. Any previous
5179 * contents of @a __str are erased.
5180 */
5181 template<typename _CharT, typename _Traits, typename _Alloc>
5182 basic_istream<_CharT, _Traits>&
5183 operator>>(basic_istream<_CharT, _Traits>& __is,
5184 basic_string<_CharT, _Traits, _Alloc>& __str);
5185
5186 template<>
5187 basic_istream<char>&
5188 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
5189
5190 /**
5191 * @brief Write string to a stream.
5192 * @param __os Output stream.
5193 * @param __str String to write out.
5194 * @return Reference to the output stream.
5195 *
5196 * Output characters of @a __str into os following the same rules as for
5197 * writing a C string.
5198 */
5199 template<typename _CharT, typename _Traits, typename _Alloc>
5200 inline basic_ostream<_CharT, _Traits>&
5201 operator<<(basic_ostream<_CharT, _Traits>& __os,
5202 const basic_string<_CharT, _Traits, _Alloc>& __str)
5203 {
5204 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5205 // 586. string inserter not a formatted function
5206 return __ostream_insert(__os, __str.data(), __str.size());
5207 }
5208
5209 /**
5210 * @brief Read a line from stream into a string.
5211 * @param __is Input stream.
5212 * @param __str Buffer to store into.
5213 * @param __delim Character marking end of line.
5214 * @return Reference to the input stream.
5215 *
5216 * Stores characters from @a __is into @a __str until @a __delim is
5217 * found, the end of the stream is encountered, or str.max_size()
5218 * is reached. Any previous contents of @a __str are erased. If
5219 * @a __delim is encountered, it is extracted but not stored into
5220 * @a __str.
5221 */
5222 template<typename _CharT, typename _Traits, typename _Alloc>
5223 basic_istream<_CharT, _Traits>&
5224 getline(basic_istream<_CharT, _Traits>& __is,
5225 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
5226
5227 /**
5228 * @brief Read a line from stream into a string.
5229 * @param __is Input stream.
5230 * @param __str Buffer to store into.
5231 * @return Reference to the input stream.
5232 *
5233 * Stores characters from is into @a __str until &apos;\n&apos; is
5234 * found, the end of the stream is encountered, or str.max_size()
5235 * is reached. Any previous contents of @a __str are erased. If
5236 * end of line is encountered, it is extracted but not stored into
5237 * @a __str.
5238 */
5239 template<typename _CharT, typename _Traits, typename _Alloc>
5240 inline basic_istream<_CharT, _Traits>&
5241 getline(basic_istream<_CharT, _Traits>& __is,
5242 basic_string<_CharT, _Traits, _Alloc>& __str)
5243 { return std::getline(__is, __str, __is.widen('\n')); }
5244
5245 #if __cplusplus >= 201103L
5246 /// Read a line from an rvalue stream into a string.
5247 template<typename _CharT, typename _Traits, typename _Alloc>
5248 inline basic_istream<_CharT, _Traits>&
5249 getline(basic_istream<_CharT, _Traits>&& __is,
5250 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
5251 { return std::getline(__is, __str, __delim); }
5252
5253 /// Read a line from an rvalue stream into a string.
5254 template<typename _CharT, typename _Traits, typename _Alloc>
5255 inline basic_istream<_CharT, _Traits>&
5256 getline(basic_istream<_CharT, _Traits>&& __is,
5257 basic_string<_CharT, _Traits, _Alloc>& __str)
5258 { return std::getline(__is, __str); }
5259 #endif
5260
5261 template<>
5262 basic_istream<char>&
5263 getline(basic_istream<char>& __in, basic_string<char>& __str,
5264 char __delim);
5265
5266 #ifdef _GLIBCXX_USE_WCHAR_T
5267 template<>
5268 basic_istream<wchar_t>&
5269 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
5270 wchar_t __delim);
5271 #endif
5272
5273 _GLIBCXX_END_NAMESPACE_VERSION
5274 } // namespace
5275
5276 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
5277
5278 #include <ext/string_conversions.h>
5279
5280 namespace std _GLIBCXX_VISIBILITY(default)
5281 {
5282 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5283 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5284
5285 // 21.4 Numeric Conversions [string.conversions].
5286 inline int
5287 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
5288 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
5289 __idx, __base); }
5290
5291 inline long
5292 stol(const string& __str, size_t* __idx = 0, int __base = 10)
5293 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
5294 __idx, __base); }
5295
5296 inline unsigned long
5297 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
5298 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
5299 __idx, __base); }
5300
5301 inline long long
5302 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
5303 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
5304 __idx, __base); }
5305
5306 inline unsigned long long
5307 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
5308 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
5309 __idx, __base); }
5310
5311 // NB: strtof vs strtod.
5312 inline float
5313 stof(const string& __str, size_t* __idx = 0)
5314 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
5315
5316 inline double
5317 stod(const string& __str, size_t* __idx = 0)
5318 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
5319
5320 inline long double
5321 stold(const string& __str, size_t* __idx = 0)
5322 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
5323
5324 // NB: (v)snprintf vs sprintf.
5325
5326 // DR 1261.
5327 inline string
5328 to_string(int __val)
5329 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
5330 "%d", __val); }
5331
5332 inline string
5333 to_string(unsigned __val)
5334 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5335 4 * sizeof(unsigned),
5336 "%u", __val); }
5337
5338 inline string
5339 to_string(long __val)
5340 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
5341 "%ld", __val); }
5342
5343 inline string
5344 to_string(unsigned long __val)
5345 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5346 4 * sizeof(unsigned long),
5347 "%lu", __val); }
5348
5349 inline string
5350 to_string(long long __val)
5351 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5352 4 * sizeof(long long),
5353 "%lld", __val); }
5354
5355 inline string
5356 to_string(unsigned long long __val)
5357 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5358 4 * sizeof(unsigned long long),
5359 "%llu", __val); }
5360
5361 inline string
5362 to_string(float __val)
5363 {
5364 const int __n =
5365 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
5366 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5367 "%f", __val);
5368 }
5369
5370 inline string
5371 to_string(double __val)
5372 {
5373 const int __n =
5374 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
5375 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5376 "%f", __val);
5377 }
5378
5379 inline string
5380 to_string(long double __val)
5381 {
5382 const int __n =
5383 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
5384 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5385 "%Lf", __val);
5386 }
5387
5388 #ifdef _GLIBCXX_USE_WCHAR_T
5389 inline int
5390 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
5391 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
5392 __idx, __base); }
5393
5394 inline long
5395 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
5396 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
5397 __idx, __base); }
5398
5399 inline unsigned long
5400 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
5401 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
5402 __idx, __base); }
5403
5404 inline long long
5405 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
5406 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
5407 __idx, __base); }
5408
5409 inline unsigned long long
5410 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
5411 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
5412 __idx, __base); }
5413
5414 // NB: wcstof vs wcstod.
5415 inline float
5416 stof(const wstring& __str, size_t* __idx = 0)
5417 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
5418
5419 inline double
5420 stod(const wstring& __str, size_t* __idx = 0)
5421 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
5422
5423 inline long double
5424 stold(const wstring& __str, size_t* __idx = 0)
5425 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
5426
5427 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5428 // DR 1261.
5429 inline wstring
5430 to_wstring(int __val)
5431 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
5432 L"%d", __val); }
5433
5434 inline wstring
5435 to_wstring(unsigned __val)
5436 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5437 4 * sizeof(unsigned),
5438 L"%u", __val); }
5439
5440 inline wstring
5441 to_wstring(long __val)
5442 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
5443 L"%ld", __val); }
5444
5445 inline wstring
5446 to_wstring(unsigned long __val)
5447 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5448 4 * sizeof(unsigned long),
5449 L"%lu", __val); }
5450
5451 inline wstring
5452 to_wstring(long long __val)
5453 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5454 4 * sizeof(long long),
5455 L"%lld", __val); }
5456
5457 inline wstring
5458 to_wstring(unsigned long long __val)
5459 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5460 4 * sizeof(unsigned long long),
5461 L"%llu", __val); }
5462
5463 inline wstring
5464 to_wstring(float __val)
5465 {
5466 const int __n =
5467 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
5468 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
5469 L"%f", __val);
5470 }
5471
5472 inline wstring
5473 to_wstring(double __val)
5474 {
5475 const int __n =
5476 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
5477 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
5478 L"%f", __val);
5479 }
5480
5481 inline wstring
5482 to_wstring(long double __val)
5483 {
5484 const int __n =
5485 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
5486 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
5487 L"%Lf", __val);
5488 }
5489 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5490 #endif
5491
5492 _GLIBCXX_END_NAMESPACE_CXX11
5493 _GLIBCXX_END_NAMESPACE_VERSION
5494 } // namespace
5495
5496 #endif /* C++11 && _GLIBCXX_USE_C99 ... */
5497
5498 #if __cplusplus >= 201103L
5499
5500 #include <bits/functional_hash.h>
5501
5502 namespace std _GLIBCXX_VISIBILITY(default)
5503 {
5504 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5505
5506 // DR 1182.
5507
5508 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5509 /// std::hash specialization for string.
5510 template<>
5511 struct hash<string>
5512 : public __hash_base<size_t, string>
5513 {
5514 size_t
5515 operator()(const string& __s) const noexcept
5516 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
5517 };
5518
5519 template<>
5520 struct __is_fast_hash<hash<string>> : std::false_type
5521 { };
5522
5523 #ifdef _GLIBCXX_USE_WCHAR_T
5524 /// std::hash specialization for wstring.
5525 template<>
5526 struct hash<wstring>
5527 : public __hash_base<size_t, wstring>
5528 {
5529 size_t
5530 operator()(const wstring& __s) const noexcept
5531 { return std::_Hash_impl::hash(__s.data(),
5532 __s.length() * sizeof(wchar_t)); }
5533 };
5534
5535 template<>
5536 struct __is_fast_hash<hash<wstring>> : std::false_type
5537 { };
5538 #endif
5539 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5540
5541 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5542 /// std::hash specialization for u16string.
5543 template<>
5544 struct hash<u16string>
5545 : public __hash_base<size_t, u16string>
5546 {
5547 size_t
5548 operator()(const u16string& __s) const noexcept
5549 { return std::_Hash_impl::hash(__s.data(),
5550 __s.length() * sizeof(char16_t)); }
5551 };
5552
5553 template<>
5554 struct __is_fast_hash<hash<u16string>> : std::false_type
5555 { };
5556
5557 /// std::hash specialization for u32string.
5558 template<>
5559 struct hash<u32string>
5560 : public __hash_base<size_t, u32string>
5561 {
5562 size_t
5563 operator()(const u32string& __s) const noexcept
5564 { return std::_Hash_impl::hash(__s.data(),
5565 __s.length() * sizeof(char32_t)); }
5566 };
5567
5568 template<>
5569 struct __is_fast_hash<hash<u32string>> : std::false_type
5570 { };
5571 #endif
5572
5573 #if __cplusplus > 201103L
5574
5575 #define __cpp_lib_string_udls 201304
5576
5577 inline namespace literals
5578 {
5579 inline namespace string_literals
5580 {
5581
5582 _GLIBCXX_DEFAULT_ABI_TAG
5583 inline basic_string<char>
5584 operator""s(const char* __str, size_t __len)
5585 { return basic_string<char>{__str, __len}; }
5586
5587 #ifdef _GLIBCXX_USE_WCHAR_T
5588 _GLIBCXX_DEFAULT_ABI_TAG
5589 inline basic_string<wchar_t>
5590 operator""s(const wchar_t* __str, size_t __len)
5591 { return basic_string<wchar_t>{__str, __len}; }
5592 #endif
5593
5594 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5595 _GLIBCXX_DEFAULT_ABI_TAG
5596 inline basic_string<char16_t>
5597 operator""s(const char16_t* __str, size_t __len)
5598 { return basic_string<char16_t>{__str, __len}; }
5599
5600 _GLIBCXX_DEFAULT_ABI_TAG
5601 inline basic_string<char32_t>
5602 operator""s(const char32_t* __str, size_t __len)
5603 { return basic_string<char32_t>{__str, __len}; }
5604 #endif
5605
5606 } // inline namespace string_literals
5607 } // inline namespace literals
5608
5609 #endif // __cplusplus > 201103L
5610
5611 _GLIBCXX_END_NAMESPACE_VERSION
5612 } // namespace std
5613
5614 #endif // C++11
5615
5616 #endif /* _BASIC_STRING_H */