39 #ifndef _BASIC_STRING_TCC
40 #define _BASIC_STRING_TCC 1
42 #pragma GCC system_header
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #if _GLIBCXX_USE_CXX11_ABI
52 template<
typename _CharT,
typename _Traits,
typename _Alloc>
53 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
56 template<
typename _CharT,
typename _Traits,
typename _Alloc>
59 swap(basic_string& __s) _GLIBCXX_NOEXCEPT
64 _Alloc_traits::_S_on_swap(_M_get_allocator(), __s._M_get_allocator());
67 if (__s._M_is_local())
69 if (length() && __s.length())
71 _CharT __tmp_data[_S_local_capacity + 1];
72 traits_type::copy(__tmp_data, __s._M_local_buf,
73 _S_local_capacity + 1);
74 traits_type::copy(__s._M_local_buf, _M_local_buf,
75 _S_local_capacity + 1);
76 traits_type::copy(_M_local_buf, __tmp_data,
77 _S_local_capacity + 1);
79 else if (__s.length())
81 traits_type::copy(_M_local_buf, __s._M_local_buf,
82 _S_local_capacity + 1);
83 _M_length(__s.length());
89 traits_type::copy(__s._M_local_buf, _M_local_buf,
90 _S_local_capacity + 1);
91 __s._M_length(length());
98 const size_type __tmp_capacity = __s._M_allocated_capacity;
99 traits_type::copy(__s._M_local_buf, _M_local_buf,
100 _S_local_capacity + 1);
101 _M_data(__s._M_data());
102 __s._M_data(__s._M_local_buf);
103 _M_capacity(__tmp_capacity);
107 const size_type __tmp_capacity = _M_allocated_capacity;
108 if (__s._M_is_local())
110 traits_type::copy(_M_local_buf, __s._M_local_buf,
111 _S_local_capacity + 1);
112 __s._M_data(_M_data());
113 _M_data(_M_local_buf);
117 pointer __tmp_ptr = _M_data();
118 _M_data(__s._M_data());
119 __s._M_data(__tmp_ptr);
120 _M_capacity(__s._M_allocated_capacity);
122 __s._M_capacity(__tmp_capacity);
125 const size_type __tmp_length = length();
126 _M_length(__s.length());
127 __s._M_length(__tmp_length);
130 template<
typename _CharT,
typename _Traits,
typename _Alloc>
131 typename basic_string<_CharT, _Traits, _Alloc>::pointer
132 basic_string<_CharT, _Traits, _Alloc>::
133 _M_create(size_type& __capacity, size_type __old_capacity)
137 if (__capacity > max_size())
138 std::__throw_length_error(__N(
"basic_string::_M_create"));
143 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
145 __capacity = 2 * __old_capacity;
147 if (__capacity > max_size())
148 __capacity = max_size();
153 return _Alloc_traits::allocate(_M_get_allocator(), __capacity + 1);
160 template<
typename _CharT,
typename _Traits,
typename _Alloc>
161 template<
typename _InIterator>
163 basic_string<_CharT, _Traits, _Alloc>::
164 _M_construct(_InIterator __beg, _InIterator __end,
168 size_type __capacity = size_type(_S_local_capacity);
170 while (__beg != __end && __len < __capacity)
172 _M_data()[__len++] = *__beg;
178 while (__beg != __end)
180 if (__len == __capacity)
183 __capacity = __len + 1;
184 pointer __another = _M_create(__capacity, __len);
185 this->_S_copy(__another, _M_data(), __len);
188 _M_capacity(__capacity);
190 _M_data()[__len++] = *__beg;
197 __throw_exception_again;
200 _M_set_length(__len);
203 template<
typename _CharT,
typename _Traits,
typename _Alloc>
204 template<
typename _InIterator>
206 basic_string<_CharT, _Traits, _Alloc>::
207 _M_construct(_InIterator __beg, _InIterator __end,
211 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
212 std::__throw_logic_error(__N(
"basic_string::"
213 "_M_construct null not valid"));
215 size_type __dnew =
static_cast<size_type
>(
std::distance(__beg, __end));
217 if (__dnew > size_type(_S_local_capacity))
219 _M_data(_M_create(__dnew, size_type(0)));
225 { this->_S_copy_chars(_M_data(), __beg, __end); }
229 __throw_exception_again;
232 _M_set_length(__dnew);
235 template<
typename _CharT,
typename _Traits,
typename _Alloc>
237 basic_string<_CharT, _Traits, _Alloc>::
238 _M_construct(size_type __n, _CharT __c)
240 if (__n > size_type(_S_local_capacity))
242 _M_data(_M_create(__n, size_type(0)));
247 this->_S_assign(_M_data(), __n, __c);
252 template<
typename _CharT,
typename _Traits,
typename _Alloc>
254 basic_string<_CharT, _Traits, _Alloc>::
255 _M_assign(
const basic_string& __str)
259 const size_type __rsize = __str.length();
260 const size_type __capacity = capacity();
262 if (__rsize > __capacity)
264 size_type __new_capacity = __rsize;
265 pointer __tmp = _M_create(__new_capacity, __capacity);
268 _M_capacity(__new_capacity);
272 this->_S_copy(_M_data(), __str._M_data(), __rsize);
274 _M_set_length(__rsize);
278 template<
typename _CharT,
typename _Traits,
typename _Alloc>
283 const size_type __capacity = capacity();
288 if (__res <= __capacity)
291 pointer __tmp = _M_create(__res, __capacity);
292 this->_S_copy(__tmp, _M_data(), length() + 1);
298 template<
typename _CharT,
typename _Traits,
typename _Alloc>
300 basic_string<_CharT, _Traits, _Alloc>::
301 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
304 const size_type __how_much = length() - __pos - __len1;
306 size_type __new_capacity = length() + __len2 - __len1;
307 pointer __r = _M_create(__new_capacity, capacity());
310 this->_S_copy(__r, _M_data(), __pos);
312 this->_S_copy(__r + __pos, __s, __len2);
314 this->_S_copy(__r + __pos + __len2,
315 _M_data() + __pos + __len1, __how_much);
319 _M_capacity(__new_capacity);
322 template<
typename _CharT,
typename _Traits,
typename _Alloc>
324 basic_string<_CharT, _Traits, _Alloc>::
325 _M_erase(size_type __pos, size_type __n)
327 const size_type __how_much = length() - __pos - __n;
329 if (__how_much && __n)
330 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
332 _M_set_length(length() - __n);
335 template<
typename _CharT,
typename _Traits,
typename _Alloc>
343 const size_type __length = length();
344 const size_type __capacity = _M_allocated_capacity;
346 if (__length <= size_type(_S_local_capacity))
348 this->_S_copy(_M_local_data(), _M_data(), __length + 1);
349 _M_destroy(__capacity);
350 _M_data(_M_local_data());
353 else if (__length < __capacity)
357 = _Alloc_traits::allocate(_M_get_allocator(), __length + 1);
358 this->_S_copy(__tmp, _M_data(), __length + 1);
361 _M_capacity(__length);
370 template<
typename _CharT,
typename _Traits,
typename _Alloc>
373 resize(size_type __n, _CharT __c)
375 const size_type __size = this->
size();
377 this->append(__n - __size, __c);
378 else if (__n < __size)
379 this->_M_set_length(__n);
382 template<
typename _CharT,
typename _Traits,
typename _Alloc>
383 basic_string<_CharT, _Traits, _Alloc>&
384 basic_string<_CharT, _Traits, _Alloc>::
385 _M_append(
const _CharT* __s, size_type __n)
387 const size_type __len = __n + this->
size();
389 if (__len <= this->capacity())
392 this->_S_copy(this->_M_data() + this->
size(), __s, __n);
395 this->_M_mutate(this->
size(), size_type(0), __s, __n);
397 this->_M_set_length(__len);
401 template<
typename _CharT,
typename _Traits,
typename _Alloc>
402 template<
typename _InputIterator>
403 basic_string<_CharT, _Traits, _Alloc>&
404 basic_string<_CharT, _Traits, _Alloc>::
405 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
406 _InputIterator __k1, _InputIterator __k2,
411 const basic_string __s(__k1, __k2, this->get_allocator());
412 const size_type __n1 = __i2 - __i1;
413 return _M_replace(__i1 -
begin(), __n1, __s._M_data(),
417 template<
typename _CharT,
typename _Traits,
typename _Alloc>
418 basic_string<_CharT, _Traits, _Alloc>&
419 basic_string<_CharT, _Traits, _Alloc>::
420 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
423 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
425 const size_type __old_size = this->
size();
426 const size_type __new_size = __old_size + __n2 - __n1;
428 if (__new_size <= this->capacity())
430 pointer __p = this->_M_data() + __pos1;
432 const size_type __how_much = __old_size - __pos1 - __n1;
433 if (__how_much && __n1 != __n2)
434 this->_S_move(__p + __n2, __p + __n1, __how_much);
437 this->_M_mutate(__pos1, __n1, 0, __n2);
440 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
442 this->_M_set_length(__new_size);
446 template<
typename _CharT,
typename _Traits,
typename _Alloc>
447 basic_string<_CharT, _Traits, _Alloc>&
448 basic_string<_CharT, _Traits, _Alloc>::
449 _M_replace(size_type __pos, size_type __len1,
const _CharT* __s,
450 const size_type __len2)
452 _M_check_length(__len1, __len2,
"basic_string::_M_replace");
454 const size_type __old_size = this->
size();
455 const size_type __new_size = __old_size + __len2 - __len1;
457 if (__new_size <= this->capacity())
459 pointer __p = this->_M_data() + __pos;
461 const size_type __how_much = __old_size - __pos - __len1;
462 if (_M_disjunct(__s))
464 if (__how_much && __len1 != __len2)
465 this->_S_move(__p + __len2, __p + __len1, __how_much);
467 this->_S_copy(__p, __s, __len2);
472 if (__len2 && __len2 <= __len1)
473 this->_S_move(__p, __s, __len2);
474 if (__how_much && __len1 != __len2)
475 this->_S_move(__p + __len2, __p + __len1, __how_much);
478 if (__s + __len2 <= __p + __len1)
479 this->_S_move(__p, __s, __len2);
480 else if (__s >= __p + __len1)
481 this->_S_copy(__p, __s + __len2 - __len1, __len2);
484 const size_type __nleft = (__p + __len1) - __s;
485 this->_S_move(__p, __s, __nleft);
486 this->_S_copy(__p + __nleft, __p + __len2,
493 this->_M_mutate(__pos, __len1, __s, __len2);
495 this->_M_set_length(__new_size);
499 template<
typename _CharT,
typename _Traits,
typename _Alloc>
500 typename basic_string<_CharT, _Traits, _Alloc>::size_type
502 copy(_CharT* __s, size_type __n, size_type __pos)
const
504 _M_check(__pos,
"basic_string::copy");
505 __n = _M_limit(__pos, __n);
506 __glibcxx_requires_string_len(__s, __n);
508 _S_copy(__s, _M_data() + __pos, __n);
513 #else // !_GLIBCXX_USE_CXX11_ABI
515 template<
typename _CharT,
typename _Traits,
typename _Alloc>
516 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
517 basic_string<_CharT, _Traits, _Alloc>::
518 _Rep::_S_max_size = (((npos -
sizeof(_Rep_base))/
sizeof(_CharT)) - 1) / 4;
520 template<
typename _CharT,
typename _Traits,
typename _Alloc>
522 basic_string<_CharT, _Traits, _Alloc>::
523 _Rep::_S_terminal = _CharT();
525 template<
typename _CharT,
typename _Traits,
typename _Alloc>
526 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
531 template<
typename _CharT,
typename _Traits,
typename _Alloc>
532 typename basic_string<_CharT, _Traits, _Alloc>::size_type
533 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
534 (
sizeof(_Rep_base) +
sizeof(_CharT) +
sizeof(size_type) - 1) /
541 template<
typename _CharT,
typename _Traits,
typename _Alloc>
542 template<
typename _InIterator>
544 basic_string<_CharT, _Traits, _Alloc>::
545 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
548 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
549 if (__beg == __end && __a == _Alloc())
550 return _S_empty_rep()._M_refdata();
555 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
557 __buf[__len++] = *__beg;
560 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
561 _M_copy(__r->_M_refdata(), __buf, __len);
564 while (__beg != __end)
566 if (__len == __r->_M_capacity)
569 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
570 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
571 __r->_M_destroy(__a);
574 __r->_M_refdata()[__len++] = *__beg;
580 __r->_M_destroy(__a);
581 __throw_exception_again;
583 __r->_M_set_length_and_sharable(__len);
584 return __r->_M_refdata();
587 template<
typename _CharT,
typename _Traits,
typename _Alloc>
588 template <
typename _InIterator>
590 basic_string<_CharT, _Traits, _Alloc>::
591 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
592 forward_iterator_tag)
594 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
595 if (__beg == __end && __a == _Alloc())
596 return _S_empty_rep()._M_refdata();
599 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
600 __throw_logic_error(__N(
"basic_string::_S_construct null not valid"));
602 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
605 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
607 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
610 __r->_M_destroy(__a);
611 __throw_exception_again;
613 __r->_M_set_length_and_sharable(__dnew);
614 return __r->_M_refdata();
617 template<
typename _CharT,
typename _Traits,
typename _Alloc>
619 basic_string<_CharT, _Traits, _Alloc>::
620 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
622 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
623 if (__n == 0 && __a == _Alloc())
624 return _S_empty_rep()._M_refdata();
627 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
629 _M_assign(__r->_M_refdata(), __n, __c);
631 __r->_M_set_length_and_sharable(__n);
632 return __r->_M_refdata();
635 template<
typename _CharT,
typename _Traits,
typename _Alloc>
638 : _M_dataplus(_S_construct(__str._M_data()
639 + __str._M_check(__pos,
640 "basic_string::basic_string"),
641 __str._M_data() + __str._M_limit(__pos, npos)
645 template<
typename _CharT,
typename _Traits,
typename _Alloc>
648 : _M_dataplus(_S_construct(__str._M_data()
649 + __str._M_check(__pos,
650 "basic_string::basic_string"),
651 __str._M_data() + __str._M_limit(__pos, __n)
652 + __pos, _Alloc()), _Alloc())
655 template<
typename _CharT,
typename _Traits,
typename _Alloc>
658 size_type __n,
const _Alloc& __a)
659 : _M_dataplus(_S_construct(__str._M_data()
660 + __str._M_check(__pos,
661 "basic_string::basic_string"),
662 __str._M_data() + __str._M_limit(__pos, __n)
666 template<
typename _CharT,
typename _Traits,
typename _Alloc>
671 if (_M_rep() != __str._M_rep())
674 const allocator_type __a = this->get_allocator();
675 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.
get_allocator());
676 _M_rep()->_M_dispose(__a);
682 template<
typename _CharT,
typename _Traits,
typename _Alloc>
685 assign(
const _CharT* __s, size_type __n)
687 __glibcxx_requires_string_len(__s, __n);
688 _M_check_length(this->
size(), __n,
"basic_string::assign");
689 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
690 return _M_replace_safe(size_type(0), this->
size(), __s, __n);
694 const size_type __pos = __s - _M_data();
696 _M_copy(_M_data(), __s, __n);
698 _M_move(_M_data(), __s, __n);
699 _M_rep()->_M_set_length_and_sharable(__n);
704 template<
typename _CharT,
typename _Traits,
typename _Alloc>
707 append(size_type __n, _CharT __c)
711 _M_check_length(size_type(0), __n,
"basic_string::append");
712 const size_type __len = __n + this->
size();
713 if (__len > this->capacity() || _M_rep()->_M_is_shared())
714 this->reserve(__len);
715 _M_assign(_M_data() + this->
size(), __n, __c);
716 _M_rep()->_M_set_length_and_sharable(__len);
721 template<
typename _CharT,
typename _Traits,
typename _Alloc>
724 append(
const _CharT* __s, size_type __n)
726 __glibcxx_requires_string_len(__s, __n);
729 _M_check_length(size_type(0), __n,
"basic_string::append");
730 const size_type __len = __n + this->
size();
731 if (__len > this->capacity() || _M_rep()->_M_is_shared())
733 if (_M_disjunct(__s))
734 this->reserve(__len);
737 const size_type __off = __s - _M_data();
738 this->reserve(__len);
739 __s = _M_data() + __off;
742 _M_copy(_M_data() + this->
size(), __s, __n);
743 _M_rep()->_M_set_length_and_sharable(__len);
748 template<
typename _CharT,
typename _Traits,
typename _Alloc>
753 const size_type __size = __str.
size();
756 const size_type __len = __size + this->
size();
757 if (__len > this->capacity() || _M_rep()->_M_is_shared())
758 this->reserve(__len);
759 _M_copy(_M_data() + this->
size(), __str._M_data(), __size);
760 _M_rep()->_M_set_length_and_sharable(__len);
765 template<
typename _CharT,
typename _Traits,
typename _Alloc>
770 __str._M_check(__pos,
"basic_string::append");
771 __n = __str._M_limit(__pos, __n);
774 const size_type __len = __n + this->
size();
775 if (__len > this->capacity() || _M_rep()->_M_is_shared())
776 this->reserve(__len);
777 _M_copy(_M_data() + this->
size(), __str._M_data() + __pos, __n);
778 _M_rep()->_M_set_length_and_sharable(__len);
783 template<
typename _CharT,
typename _Traits,
typename _Alloc>
786 insert(size_type __pos,
const _CharT* __s, size_type __n)
788 __glibcxx_requires_string_len(__s, __n);
789 _M_check(__pos,
"basic_string::insert");
790 _M_check_length(size_type(0), __n,
"basic_string::insert");
791 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
792 return _M_replace_safe(__pos, size_type(0), __s, __n);
796 const size_type __off = __s - _M_data();
797 _M_mutate(__pos, 0, __n);
798 __s = _M_data() + __off;
799 _CharT* __p = _M_data() + __pos;
800 if (__s + __n <= __p)
801 _M_copy(__p, __s, __n);
803 _M_copy(__p, __s + __n, __n);
806 const size_type __nleft = __p - __s;
807 _M_copy(__p, __s, __nleft);
808 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
814 template<
typename _CharT,
typename _Traits,
typename _Alloc>
815 typename basic_string<_CharT, _Traits, _Alloc>::iterator
817 erase(iterator __first, iterator __last)
819 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
820 && __last <= _M_iend());
825 const size_type __size = __last - __first;
828 const size_type __pos = __first - _M_ibegin();
829 _M_mutate(__pos, __size, size_type(0));
830 _M_rep()->_M_set_leaked();
831 return iterator(_M_data() + __pos);
837 template<
typename _CharT,
typename _Traits,
typename _Alloc>
840 replace(size_type __pos, size_type __n1,
const _CharT* __s,
843 __glibcxx_requires_string_len(__s, __n2);
844 _M_check(__pos,
"basic_string::replace");
845 __n1 = _M_limit(__pos, __n1);
846 _M_check_length(__n1, __n2,
"basic_string::replace");
848 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
849 return _M_replace_safe(__pos, __n1, __s, __n2);
850 else if ((__left = __s + __n2 <= _M_data() + __pos)
851 || _M_data() + __pos + __n1 <= __s)
854 size_type __off = __s - _M_data();
855 __left ? __off : (__off += __n2 - __n1);
856 _M_mutate(__pos, __n1, __n2);
857 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
864 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
868 template<
typename _CharT,
typename _Traits,
typename _Alloc>
873 const size_type __size =
sizeof(_Rep_base) +
874 (this->_M_capacity + 1) *
sizeof(_CharT);
875 _Raw_bytes_alloc(__a).deallocate(
reinterpret_cast<char*
>(
this), __size);
878 template<
typename _CharT,
typename _Traits,
typename _Alloc>
880 basic_string<_CharT, _Traits, _Alloc>::
883 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
884 if (_M_rep() == &_S_empty_rep())
887 if (_M_rep()->_M_is_shared())
889 _M_rep()->_M_set_leaked();
892 template<
typename _CharT,
typename _Traits,
typename _Alloc>
894 basic_string<_CharT, _Traits, _Alloc>::
895 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
897 const size_type __old_size = this->
size();
898 const size_type __new_size = __old_size + __len2 - __len1;
899 const size_type __how_much = __old_size - __pos - __len1;
901 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
904 const allocator_type __a = get_allocator();
905 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
908 _M_copy(__r->_M_refdata(), _M_data(), __pos);
910 _M_copy(__r->_M_refdata() + __pos + __len2,
911 _M_data() + __pos + __len1, __how_much);
913 _M_rep()->_M_dispose(__a);
914 _M_data(__r->_M_refdata());
916 else if (__how_much && __len1 != __len2)
919 _M_move(_M_data() + __pos + __len2,
920 _M_data() + __pos + __len1, __how_much);
922 _M_rep()->_M_set_length_and_sharable(__new_size);
925 template<
typename _CharT,
typename _Traits,
typename _Alloc>
930 const size_type __capacity = capacity();
936 if (__res <= __capacity)
938 if (!_M_rep()->_M_is_shared())
945 const allocator_type __a = get_allocator();
946 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->
size());
947 _M_rep()->_M_dispose(__a);
951 template<
typename _CharT,
typename _Traits,
typename _Alloc>
957 if (_M_rep()->_M_is_leaked())
958 _M_rep()->_M_set_sharable();
959 if (__s._M_rep()->_M_is_leaked())
960 __s._M_rep()->_M_set_sharable();
961 if (this->get_allocator() == __s.get_allocator())
963 _CharT* __tmp = _M_data();
964 _M_data(__s._M_data());
971 __s.get_allocator());
972 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
973 this->get_allocator());
979 template<
typename _CharT,
typename _Traits,
typename _Alloc>
982 _S_create(size_type __capacity, size_type __old_capacity,
983 const _Alloc& __alloc)
987 if (__capacity > _S_max_size)
988 __throw_length_error(__N(
"basic_string::_S_create"));
1013 const size_type __pagesize = 4096;
1014 const size_type __malloc_header_size = 4 *
sizeof(
void*);
1022 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1023 __capacity = 2 * __old_capacity;
1028 size_type __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1030 const size_type __adj_size = __size + __malloc_header_size;
1031 if (__adj_size > __pagesize && __capacity > __old_capacity)
1033 const size_type __extra = __pagesize - __adj_size % __pagesize;
1034 __capacity += __extra /
sizeof(_CharT);
1036 if (__capacity > _S_max_size)
1037 __capacity = _S_max_size;
1038 __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1043 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1044 _Rep *__p =
new (__place) _Rep;
1045 __p->_M_capacity = __capacity;
1053 __p->_M_set_sharable();
1057 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1059 basic_string<_CharT, _Traits, _Alloc>::_Rep::
1060 _M_clone(
const _Alloc& __alloc, size_type __res)
1063 const size_type __requested_cap = this->_M_length + __res;
1064 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1066 if (this->_M_length)
1067 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1069 __r->_M_set_length_and_sharable(this->_M_length);
1070 return __r->_M_refdata();
1073 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1076 resize(size_type __n, _CharT __c)
1078 const size_type __size = this->
size();
1079 _M_check_length(__size, __n,
"basic_string::resize");
1081 this->append(__n - __size, __c);
1082 else if (__n < __size)
1087 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1088 template<
typename _InputIterator>
1092 _InputIterator __k2, __false_type)
1095 const size_type __n1 = __i2 - __i1;
1096 _M_check_length(__n1, __s.size(),
"basic_string::_M_replace_dispatch");
1097 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1101 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1102 basic_string<_CharT, _Traits, _Alloc>&
1103 basic_string<_CharT, _Traits, _Alloc>::
1104 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1107 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
1108 _M_mutate(__pos1, __n1, __n2);
1110 _M_assign(_M_data() + __pos1, __n2, __c);
1114 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1115 basic_string<_CharT, _Traits, _Alloc>&
1116 basic_string<_CharT, _Traits, _Alloc>::
1117 _M_replace_safe(size_type __pos1, size_type __n1,
const _CharT* __s,
1120 _M_mutate(__pos1, __n1, __n2);
1122 _M_copy(_M_data() + __pos1, __s, __n2);
1126 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1131 #if __cpp_exceptions
1132 if (length() < capacity() || _M_rep()->_M_is_shared())
1135 const allocator_type __a = get_allocator();
1136 _CharT* __tmp = _M_rep()->_M_clone(__a);
1137 _M_rep()->_M_dispose(__a);
1147 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1148 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1150 copy(_CharT* __s, size_type __n, size_type __pos)
const
1152 _M_check(__pos,
"basic_string::copy");
1153 __n = _M_limit(__pos, __n);
1154 __glibcxx_requires_string_len(__s, __n);
1156 _M_copy(__s, _M_data() + __pos, __n);
1160 #endif // !_GLIBCXX_USE_CXX11_ABI
1162 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1167 __glibcxx_requires_string(__lhs);
1169 typedef typename __string_type::size_type __size_type;
1171 rebind<_CharT>::other _Char_alloc_type;
1173 const __size_type __len = _Traits::length(__lhs);
1174 __string_type __str(_Alloc_traits::_S_select_on_copy(
1176 __str.reserve(__len + __rhs.
size());
1177 __str.append(__lhs, __len);
1178 __str.append(__rhs);
1182 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1183 basic_string<_CharT, _Traits, _Alloc>
1187 typedef typename __string_type::size_type __size_type;
1189 rebind<_CharT>::other _Char_alloc_type;
1191 __string_type __str(_Alloc_traits::_S_select_on_copy(
1193 const __size_type __len = __rhs.
size();
1194 __str.reserve(__len + 1);
1195 __str.append(__size_type(1), __lhs);
1196 __str.append(__rhs);
1200 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1201 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1203 find(
const _CharT* __s, size_type __pos, size_type __n)
const
1206 __glibcxx_requires_string_len(__s, __n);
1207 const size_type __size = this->
size();
1210 return __pos <= __size ? __pos : npos;
1211 if (__pos >= __size)
1214 const _CharT __elem0 = __s[0];
1215 const _CharT*
const __data =
data();
1216 const _CharT* __first = __data + __pos;
1217 const _CharT*
const __last = __data + __size;
1218 size_type __len = __size - __pos;
1220 while (__len >= __n)
1223 __first = traits_type::find(__first, __len - __n + 1, __elem0);
1229 if (traits_type::compare(__first, __s, __n) == 0)
1230 return __first - __data;
1231 __len = __last - ++__first;
1236 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1237 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1239 find(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1241 size_type __ret = npos;
1242 const size_type __size = this->
size();
1245 const _CharT* __data = _M_data();
1246 const size_type __n = __size - __pos;
1247 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1249 __ret = __p - __data;
1254 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1255 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1257 rfind(
const _CharT* __s, size_type __pos, size_type __n)
const
1260 __glibcxx_requires_string_len(__s, __n);
1261 const size_type __size = this->
size();
1264 __pos =
std::min(size_type(__size - __n), __pos);
1265 const _CharT* __data = _M_data();
1268 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1271 while (__pos-- > 0);
1276 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1277 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1279 rfind(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1281 size_type __size = this->
size();
1284 if (--__size > __pos)
1286 for (++__size; __size-- > 0; )
1287 if (traits_type::eq(_M_data()[__size], __c))
1293 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1294 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1296 find_first_of(
const _CharT* __s, size_type __pos, size_type __n)
const
1299 __glibcxx_requires_string_len(__s, __n);
1300 for (; __n && __pos < this->
size(); ++__pos)
1302 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1309 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1310 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1312 find_last_of(
const _CharT* __s, size_type __pos, size_type __n)
const
1315 __glibcxx_requires_string_len(__s, __n);
1316 size_type __size = this->
size();
1319 if (--__size > __pos)
1323 if (traits_type::find(__s, __n, _M_data()[__size]))
1326 while (__size-- != 0);
1331 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1332 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1337 __glibcxx_requires_string_len(__s, __n);
1338 for (; __pos < this->
size(); ++__pos)
1339 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1344 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1345 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1349 for (; __pos < this->
size(); ++__pos)
1350 if (!traits_type::eq(_M_data()[__pos], __c))
1355 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1356 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1361 __glibcxx_requires_string_len(__s, __n);
1362 size_type __size = this->
size();
1365 if (--__size > __pos)
1369 if (!traits_type::find(__s, __n, _M_data()[__size]))
1377 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1378 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1382 size_type __size = this->
size();
1385 if (--__size > __pos)
1389 if (!traits_type::eq(_M_data()[__size], __c))
1397 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1402 _M_check(__pos,
"basic_string::compare");
1403 __n = _M_limit(__pos, __n);
1404 const size_type __osize = __str.
size();
1405 const size_type __len =
std::min(__n, __osize);
1406 int __r = traits_type::compare(_M_data() + __pos, __str.
data(), __len);
1408 __r = _S_compare(__n, __osize);
1412 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1416 size_type __pos2, size_type __n2)
const
1418 _M_check(__pos1,
"basic_string::compare");
1419 __str._M_check(__pos2,
"basic_string::compare");
1420 __n1 = _M_limit(__pos1, __n1);
1421 __n2 = __str._M_limit(__pos2, __n2);
1422 const size_type __len =
std::min(__n1, __n2);
1423 int __r = traits_type::compare(_M_data() + __pos1,
1424 __str.
data() + __pos2, __len);
1426 __r = _S_compare(__n1, __n2);
1430 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1433 compare(
const _CharT* __s)
const _GLIBCXX_NOEXCEPT
1435 __glibcxx_requires_string(__s);
1436 const size_type __size = this->
size();
1437 const size_type __osize = traits_type::length(__s);
1438 const size_type __len =
std::min(__size, __osize);
1439 int __r = traits_type::compare(_M_data(), __s, __len);
1441 __r = _S_compare(__size, __osize);
1445 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1448 compare(size_type __pos, size_type __n1,
const _CharT* __s)
const
1450 __glibcxx_requires_string(__s);
1451 _M_check(__pos,
"basic_string::compare");
1452 __n1 = _M_limit(__pos, __n1);
1453 const size_type __osize = traits_type::length(__s);
1454 const size_type __len =
std::min(__n1, __osize);
1455 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1457 __r = _S_compare(__n1, __osize);
1461 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1464 compare(size_type __pos, size_type __n1,
const _CharT* __s,
1465 size_type __n2)
const
1467 __glibcxx_requires_string_len(__s, __n2);
1468 _M_check(__pos,
"basic_string::compare");
1469 __n1 = _M_limit(__pos, __n1);
1470 const size_type __len =
std::min(__n1, __n2);
1471 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1473 __r = _S_compare(__n1, __n2);
1478 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1485 typedef typename __istream_type::ios_base __ios_base;
1486 typedef typename __istream_type::int_type __int_type;
1487 typedef typename __string_type::size_type __size_type;
1489 typedef typename __ctype_type::ctype_base __ctype_base;
1491 __size_type __extracted = 0;
1492 typename __ios_base::iostate __err = __ios_base::goodbit;
1493 typename __istream_type::sentry __cerb(__in,
false);
1501 __size_type __len = 0;
1503 const __size_type __n = __w > 0 ?
static_cast<__size_type
>(__w)
1505 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1506 const __int_type __eof = _Traits::eof();
1507 __int_type __c = __in.rdbuf()->sgetc();
1509 while (__extracted < __n
1510 && !_Traits::eq_int_type(__c, __eof)
1511 && !__ct.is(__ctype_base::space,
1512 _Traits::to_char_type(__c)))
1514 if (__len ==
sizeof(__buf) /
sizeof(_CharT))
1516 __str.
append(__buf,
sizeof(__buf) /
sizeof(_CharT));
1519 __buf[__len++] = _Traits::to_char_type(__c);
1521 __c = __in.rdbuf()->snextc();
1523 __str.
append(__buf, __len);
1525 if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
1526 __err |= __ios_base::eofbit;
1531 __in._M_setstate(__ios_base::badbit);
1532 __throw_exception_again;
1539 __in._M_setstate(__ios_base::badbit);
1544 __err |= __ios_base::failbit;
1546 __in.setstate(__err);
1550 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1551 basic_istream<_CharT, _Traits>&
1557 typedef typename __istream_type::ios_base __ios_base;
1558 typedef typename __istream_type::int_type __int_type;
1559 typedef typename __string_type::size_type __size_type;
1561 __size_type __extracted = 0;
1562 const __size_type __n = __str.
max_size();
1563 typename __ios_base::iostate __err = __ios_base::goodbit;
1564 typename __istream_type::sentry __cerb(__in,
true);
1570 const __int_type __idelim = _Traits::to_int_type(__delim);
1571 const __int_type __eof = _Traits::eof();
1572 __int_type __c = __in.rdbuf()->sgetc();
1574 while (__extracted < __n
1575 && !_Traits::eq_int_type(__c, __eof)
1576 && !_Traits::eq_int_type(__c, __idelim))
1578 __str += _Traits::to_char_type(__c);
1580 __c = __in.rdbuf()->snextc();
1583 if (_Traits::eq_int_type(__c, __eof))
1584 __err |= __ios_base::eofbit;
1585 else if (_Traits::eq_int_type(__c, __idelim))
1588 __in.rdbuf()->sbumpc();
1591 __err |= __ios_base::failbit;
1595 __in._M_setstate(__ios_base::badbit);
1596 __throw_exception_again;
1603 __in._M_setstate(__ios_base::badbit);
1607 __err |= __ios_base::failbit;
1609 __in.setstate(__err);
1615 #if _GLIBCXX_EXTERN_TEMPLATE
1621 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1622 extern template class basic_string<char>;
1623 # elif ! _GLIBCXX_USE_CXX11_ABI
1626 extern template basic_string<char>::size_type
1627 basic_string<char>::_Rep::_S_empty_rep_storage[];
1631 basic_istream<char>&
1634 basic_ostream<char>&
1635 operator<<(basic_ostream<char>&,
const string&);
1637 basic_istream<char>&
1638 getline(basic_istream<char>&,
string&,
char);
1640 basic_istream<char>&
1641 getline(basic_istream<char>&,
string&);
1643 #ifdef _GLIBCXX_USE_WCHAR_T
1644 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1645 extern template class basic_string<wchar_t>;
1646 # elif ! _GLIBCXX_USE_CXX11_ABI
1647 extern template basic_string<wchar_t>::size_type
1648 basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1652 basic_istream<wchar_t>&
1655 basic_ostream<wchar_t>&
1656 operator<<(basic_ostream<wchar_t>&,
const wstring&);
1658 basic_istream<wchar_t>&
1661 basic_istream<wchar_t>&
1663 #endif // _GLIBCXX_USE_WCHAR_T
1664 #endif // _GLIBCXX_EXTERN_TEMPLATE
1666 _GLIBCXX_END_NAMESPACE_VERSION