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 #if __cplusplus >= 201103L 279 template<
typename _CharT,
typename _Traits,
typename _Alloc>
281 basic_string<_CharT, _Traits, _Alloc>::
282 _M_copy_assign(
const basic_string& __str,
true_type)
287 _Guard(basic_string* __self)
288 : _M_self(__self), _M_alloc(
std::move(__self->_M_get_allocator())),
289 _M_ptr(__self->_M_data()),
290 _M_capacity(__self->_M_allocated_capacity), _M_len(__self->length())
292 __self->_M_data(__self->_M_local_data());
293 __self->_M_length(0);
301 _M_self->_M_get_allocator() = std::move(_M_alloc);
302 _M_self->_M_data(_M_ptr);
303 _M_self->_M_capacity(_M_capacity);
304 _M_self->_M_length(_M_len);
308 _Guard(
const _Guard&) =
delete;
309 _Guard& operator=(
const _Guard&) =
delete;
314 _Alloc_traits::deallocate(_M_alloc, _M_ptr, _M_capacity + 1);
318 basic_string* _M_self;
319 allocator_type _M_alloc;
321 size_type _M_capacity;
325 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
326 && _M_get_allocator() != __str._M_get_allocator())
329 _Guard __guard(
this);
330 _M_get_allocator() = __str._M_get_allocator();
331 this->_M_assign(__str);
332 __guard._M_release();
336 _M_get_allocator() = __str._M_get_allocator();
337 this->_M_assign(__str);
342 template<
typename _CharT,
typename _Traits,
typename _Alloc>
348 if (__res < length())
351 const size_type __capacity = capacity();
352 if (__res != __capacity)
354 if (__res > __capacity
355 || __res > size_type(_S_local_capacity))
357 pointer __tmp = _M_create(__res, __capacity);
358 this->_S_copy(__tmp, _M_data(), length() + 1);
363 else if (!_M_is_local())
365 this->_S_copy(_M_local_data(), _M_data(), length() + 1);
366 _M_destroy(__capacity);
367 _M_data(_M_local_data());
372 template<
typename _CharT,
typename _Traits,
typename _Alloc>
374 basic_string<_CharT, _Traits, _Alloc>::
375 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
378 const size_type __how_much = length() - __pos - __len1;
380 size_type __new_capacity = length() + __len2 - __len1;
381 pointer __r = _M_create(__new_capacity, capacity());
384 this->_S_copy(__r, _M_data(), __pos);
386 this->_S_copy(__r + __pos, __s, __len2);
388 this->_S_copy(__r + __pos + __len2,
389 _M_data() + __pos + __len1, __how_much);
393 _M_capacity(__new_capacity);
396 template<
typename _CharT,
typename _Traits,
typename _Alloc>
398 basic_string<_CharT, _Traits, _Alloc>::
399 _M_erase(size_type __pos, size_type __n)
401 const size_type __how_much = length() - __pos - __n;
403 if (__how_much && __n)
404 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
406 _M_set_length(length() - __n);
409 template<
typename _CharT,
typename _Traits,
typename _Alloc>
412 resize(size_type __n, _CharT __c)
414 const size_type __size = this->size();
416 this->append(__n - __size, __c);
417 else if (__n < __size)
418 this->_M_set_length(__n);
421 template<
typename _CharT,
typename _Traits,
typename _Alloc>
422 basic_string<_CharT, _Traits, _Alloc>&
423 basic_string<_CharT, _Traits, _Alloc>::
424 _M_append(
const _CharT* __s, size_type __n)
426 const size_type __len = __n + this->size();
428 if (__len <= this->capacity())
431 this->_S_copy(this->_M_data() + this->size(), __s, __n);
434 this->_M_mutate(this->size(), size_type(0), __s, __n);
436 this->_M_set_length(__len);
440 template<
typename _CharT,
typename _Traits,
typename _Alloc>
441 template<
typename _InputIterator>
442 basic_string<_CharT, _Traits, _Alloc>&
443 basic_string<_CharT, _Traits, _Alloc>::
444 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
445 _InputIterator __k1, _InputIterator __k2,
448 const basic_string __s(__k1, __k2);
449 const size_type __n1 = __i2 - __i1;
450 return _M_replace(__i1 -
begin(), __n1, __s._M_data(),
454 template<
typename _CharT,
typename _Traits,
typename _Alloc>
455 basic_string<_CharT, _Traits, _Alloc>&
456 basic_string<_CharT, _Traits, _Alloc>::
457 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
460 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
462 const size_type __old_size = this->size();
463 const size_type __new_size = __old_size + __n2 - __n1;
465 if (__new_size <= this->capacity())
467 pointer __p = this->_M_data() + __pos1;
469 const size_type __how_much = __old_size - __pos1 - __n1;
470 if (__how_much && __n1 != __n2)
471 this->_S_move(__p + __n2, __p + __n1, __how_much);
474 this->_M_mutate(__pos1, __n1, 0, __n2);
477 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
479 this->_M_set_length(__new_size);
483 template<
typename _CharT,
typename _Traits,
typename _Alloc>
484 basic_string<_CharT, _Traits, _Alloc>&
485 basic_string<_CharT, _Traits, _Alloc>::
486 _M_replace(size_type __pos, size_type __len1,
const _CharT* __s,
487 const size_type __len2)
489 _M_check_length(__len1, __len2,
"basic_string::_M_replace");
491 const size_type __old_size = this->size();
492 const size_type __new_size = __old_size + __len2 - __len1;
494 if (__new_size <= this->capacity())
496 pointer __p = this->_M_data() + __pos;
498 const size_type __how_much = __old_size - __pos - __len1;
499 if (_M_disjunct(__s))
501 if (__how_much && __len1 != __len2)
502 this->_S_move(__p + __len2, __p + __len1, __how_much);
504 this->_S_copy(__p, __s, __len2);
509 if (__len2 && __len2 <= __len1)
510 this->_S_move(__p, __s, __len2);
511 if (__how_much && __len1 != __len2)
512 this->_S_move(__p + __len2, __p + __len1, __how_much);
515 if (__s + __len2 <= __p + __len1)
516 this->_S_move(__p, __s, __len2);
517 else if (__s >= __p + __len1)
518 this->_S_copy(__p, __s + __len2 - __len1, __len2);
521 const size_type __nleft = (__p + __len1) - __s;
522 this->_S_move(__p, __s, __nleft);
523 this->_S_copy(__p + __nleft, __p + __len2,
530 this->_M_mutate(__pos, __len1, __s, __len2);
532 this->_M_set_length(__new_size);
536 template<
typename _CharT,
typename _Traits,
typename _Alloc>
537 typename basic_string<_CharT, _Traits, _Alloc>::size_type
539 copy(_CharT* __s, size_type __n, size_type __pos)
const 541 _M_check(__pos,
"basic_string::copy");
542 __n = _M_limit(__pos, __n);
543 __glibcxx_requires_string_len(__s, __n);
545 _S_copy(__s, _M_data() + __pos, __n);
550 #else // !_GLIBCXX_USE_CXX11_ABI 552 template<
typename _CharT,
typename _Traits,
typename _Alloc>
553 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
554 basic_string<_CharT, _Traits, _Alloc>::
555 _Rep::_S_max_size = (((npos -
sizeof(_Rep_base))/
sizeof(_CharT)) - 1) / 4;
557 template<
typename _CharT,
typename _Traits,
typename _Alloc>
559 basic_string<_CharT, _Traits, _Alloc>::
560 _Rep::_S_terminal = _CharT();
562 template<
typename _CharT,
typename _Traits,
typename _Alloc>
563 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
564 basic_string<_CharT, _Traits, _Alloc>::npos;
568 template<
typename _CharT,
typename _Traits,
typename _Alloc>
569 typename basic_string<_CharT, _Traits, _Alloc>::size_type
570 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
571 (
sizeof(_Rep_base) +
sizeof(_CharT) +
sizeof(size_type) - 1) /
578 template<
typename _CharT,
typename _Traits,
typename _Alloc>
579 template<
typename _InIterator>
581 basic_string<_CharT, _Traits, _Alloc>::
582 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
585 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 586 if (__beg == __end && __a == _Alloc())
587 return _S_empty_rep()._M_refdata();
592 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
594 __buf[__len++] = *__beg;
597 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
598 _M_copy(__r->_M_refdata(), __buf, __len);
601 while (__beg != __end)
603 if (__len == __r->_M_capacity)
606 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
607 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
608 __r->_M_destroy(__a);
611 __r->_M_refdata()[__len++] = *__beg;
617 __r->_M_destroy(__a);
618 __throw_exception_again;
620 __r->_M_set_length_and_sharable(__len);
621 return __r->_M_refdata();
624 template<
typename _CharT,
typename _Traits,
typename _Alloc>
625 template <
typename _InIterator>
627 basic_string<_CharT, _Traits, _Alloc>::
628 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
629 forward_iterator_tag)
631 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 632 if (__beg == __end && __a == _Alloc())
633 return _S_empty_rep()._M_refdata();
636 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
637 __throw_logic_error(__N(
"basic_string::_S_construct null not valid"));
639 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
642 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
644 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
647 __r->_M_destroy(__a);
648 __throw_exception_again;
650 __r->_M_set_length_and_sharable(__dnew);
651 return __r->_M_refdata();
654 template<
typename _CharT,
typename _Traits,
typename _Alloc>
656 basic_string<_CharT, _Traits, _Alloc>::
657 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
659 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 660 if (__n == 0 && __a == _Alloc())
661 return _S_empty_rep()._M_refdata();
664 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
666 _M_assign(__r->_M_refdata(), __n, __c);
668 __r->_M_set_length_and_sharable(__n);
669 return __r->_M_refdata();
672 template<
typename _CharT,
typename _Traits,
typename _Alloc>
675 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
676 __str.get_allocator()),
677 __str.get_allocator())
680 template<
typename _CharT,
typename _Traits,
typename _Alloc>
683 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
686 template<
typename _CharT,
typename _Traits,
typename _Alloc>
689 : _M_dataplus(_S_construct(__str._M_data()
690 + __str._M_check(__pos,
691 "basic_string::basic_string"),
692 __str._M_data() + __str._M_limit(__pos, npos)
696 template<
typename _CharT,
typename _Traits,
typename _Alloc>
699 : _M_dataplus(_S_construct(__str._M_data()
700 + __str._M_check(__pos,
701 "basic_string::basic_string"),
702 __str._M_data() + __str._M_limit(__pos, __n)
703 + __pos, _Alloc()), _Alloc())
706 template<
typename _CharT,
typename _Traits,
typename _Alloc>
709 size_type __n,
const _Alloc& __a)
710 : _M_dataplus(_S_construct(__str._M_data()
711 + __str._M_check(__pos,
712 "basic_string::basic_string"),
713 __str._M_data() + __str._M_limit(__pos, __n)
718 template<
typename _CharT,
typename _Traits,
typename _Alloc>
721 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
725 template<
typename _CharT,
typename _Traits,
typename _Alloc>
728 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
729 __s + npos, __a), __a)
732 template<
typename _CharT,
typename _Traits,
typename _Alloc>
735 : _M_dataplus(_S_construct(__n, __c, __a), __a)
739 template<
typename _CharT,
typename _Traits,
typename _Alloc>
740 template<
typename _InputIterator>
742 basic_string(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a)
743 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
746 #if __cplusplus >= 201103L 747 template<
typename _CharT,
typename _Traits,
typename _Alloc>
750 : _M_dataplus(_S_construct(__l.
begin(), __l.
end(), __a), __a)
754 template<
typename _CharT,
typename _Traits,
typename _Alloc>
759 if (_M_rep() != __str._M_rep())
762 const allocator_type __a = this->get_allocator();
763 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.
get_allocator());
764 _M_rep()->_M_dispose(__a);
770 template<
typename _CharT,
typename _Traits,
typename _Alloc>
775 __glibcxx_requires_string_len(__s, __n);
776 _M_check_length(this->size(), __n,
"basic_string::assign");
777 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
778 return _M_replace_safe(size_type(0), this->size(), __s, __n);
782 const size_type __pos = __s - _M_data();
784 _M_copy(_M_data(), __s, __n);
786 _M_move(_M_data(), __s, __n);
787 _M_rep()->_M_set_length_and_sharable(__n);
792 template<
typename _CharT,
typename _Traits,
typename _Alloc>
799 _M_check_length(size_type(0), __n,
"basic_string::append");
800 const size_type __len = __n + this->size();
801 if (__len > this->capacity() || _M_rep()->_M_is_shared())
802 this->reserve(__len);
803 _M_assign(_M_data() + this->size(), __n, __c);
804 _M_rep()->_M_set_length_and_sharable(__len);
809 template<
typename _CharT,
typename _Traits,
typename _Alloc>
814 __glibcxx_requires_string_len(__s, __n);
817 _M_check_length(size_type(0), __n,
"basic_string::append");
818 const size_type __len = __n + this->size();
819 if (__len > this->capacity() || _M_rep()->_M_is_shared())
821 if (_M_disjunct(__s))
822 this->reserve(__len);
825 const size_type __off = __s - _M_data();
826 this->reserve(__len);
827 __s = _M_data() + __off;
830 _M_copy(_M_data() + this->size(), __s, __n);
831 _M_rep()->_M_set_length_and_sharable(__len);
836 template<
typename _CharT,
typename _Traits,
typename _Alloc>
841 const size_type __size = __str.
size();
844 const size_type __len = __size + this->size();
845 if (__len > this->capacity() || _M_rep()->_M_is_shared())
846 this->reserve(__len);
847 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
848 _M_rep()->_M_set_length_and_sharable(__len);
853 template<
typename _CharT,
typename _Traits,
typename _Alloc>
858 __str._M_check(__pos,
"basic_string::append");
859 __n = __str._M_limit(__pos, __n);
862 const size_type __len = __n + this->size();
863 if (__len > this->capacity() || _M_rep()->_M_is_shared())
864 this->reserve(__len);
865 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
866 _M_rep()->_M_set_length_and_sharable(__len);
871 template<
typename _CharT,
typename _Traits,
typename _Alloc>
874 insert(size_type __pos,
const _CharT* __s, size_type __n)
876 __glibcxx_requires_string_len(__s, __n);
877 _M_check(__pos,
"basic_string::insert");
878 _M_check_length(size_type(0), __n,
"basic_string::insert");
879 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
880 return _M_replace_safe(__pos, size_type(0), __s, __n);
884 const size_type __off = __s - _M_data();
885 _M_mutate(__pos, 0, __n);
886 __s = _M_data() + __off;
887 _CharT* __p = _M_data() + __pos;
888 if (__s + __n <= __p)
889 _M_copy(__p, __s, __n);
891 _M_copy(__p, __s + __n, __n);
894 const size_type __nleft = __p - __s;
895 _M_copy(__p, __s, __nleft);
896 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
902 template<
typename _CharT,
typename _Traits,
typename _Alloc>
903 typename basic_string<_CharT, _Traits, _Alloc>::iterator
905 erase(iterator __first, iterator __last)
907 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
908 && __last <= _M_iend());
913 const size_type __size = __last - __first;
916 const size_type __pos = __first - _M_ibegin();
917 _M_mutate(__pos, __size, size_type(0));
918 _M_rep()->_M_set_leaked();
919 return iterator(_M_data() + __pos);
925 template<
typename _CharT,
typename _Traits,
typename _Alloc>
928 replace(size_type __pos, size_type __n1,
const _CharT* __s,
931 __glibcxx_requires_string_len(__s, __n2);
932 _M_check(__pos,
"basic_string::replace");
933 __n1 = _M_limit(__pos, __n1);
934 _M_check_length(__n1, __n2,
"basic_string::replace");
936 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
937 return _M_replace_safe(__pos, __n1, __s, __n2);
938 else if ((__left = __s + __n2 <= _M_data() + __pos)
939 || _M_data() + __pos + __n1 <= __s)
942 size_type __off = __s - _M_data();
943 __left ? __off : (__off += __n2 - __n1);
944 _M_mutate(__pos, __n1, __n2);
945 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
952 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
956 template<
typename _CharT,
typename _Traits,
typename _Alloc>
961 const size_type __size =
sizeof(_Rep_base) +
962 (this->_M_capacity + 1) *
sizeof(_CharT);
963 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(
this), __size);
966 template<
typename _CharT,
typename _Traits,
typename _Alloc>
971 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 972 if (_M_rep() == &_S_empty_rep())
975 if (_M_rep()->_M_is_shared())
977 _M_rep()->_M_set_leaked();
980 template<
typename _CharT,
typename _Traits,
typename _Alloc>
983 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
985 const size_type __old_size = this->size();
986 const size_type __new_size = __old_size + __len2 - __len1;
987 const size_type __how_much = __old_size - __pos - __len1;
989 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
992 const allocator_type __a = get_allocator();
993 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
996 _M_copy(__r->_M_refdata(), _M_data(), __pos);
998 _M_copy(__r->_M_refdata() + __pos + __len2,
999 _M_data() + __pos + __len1, __how_much);
1001 _M_rep()->_M_dispose(__a);
1002 _M_data(__r->_M_refdata());
1004 else if (__how_much && __len1 != __len2)
1007 _M_move(_M_data() + __pos + __len2,
1008 _M_data() + __pos + __len1, __how_much);
1010 _M_rep()->_M_set_length_and_sharable(__new_size);
1013 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1018 if (__res != this->capacity() || _M_rep()->_M_is_shared())
1021 if (__res < this->size())
1022 __res = this->size();
1023 const allocator_type __a = get_allocator();
1024 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
1025 _M_rep()->_M_dispose(__a);
1030 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1035 if (_M_rep()->_M_is_leaked())
1036 _M_rep()->_M_set_sharable();
1037 if (__s._M_rep()->_M_is_leaked())
1038 __s._M_rep()->_M_set_sharable();
1041 _CharT* __tmp = _M_data();
1042 _M_data(__s._M_data());
1050 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
1051 this->get_allocator());
1057 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1060 _S_create(size_type __capacity, size_type __old_capacity,
1061 const _Alloc& __alloc)
1065 if (__capacity > _S_max_size)
1066 __throw_length_error(__N(
"basic_string::_S_create"));
1091 const size_type __pagesize = 4096;
1092 const size_type __malloc_header_size = 4 *
sizeof(
void*);
1100 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1101 __capacity = 2 * __old_capacity;
1106 size_type __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1108 const size_type __adj_size = __size + __malloc_header_size;
1109 if (__adj_size > __pagesize && __capacity > __old_capacity)
1111 const size_type __extra = __pagesize - __adj_size % __pagesize;
1112 __capacity += __extra /
sizeof(_CharT);
1114 if (__capacity > _S_max_size)
1115 __capacity = _S_max_size;
1116 __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1121 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1122 _Rep *__p =
new (__place) _Rep;
1123 __p->_M_capacity = __capacity;
1131 __p->_M_set_sharable();
1135 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1138 _M_clone(
const _Alloc& __alloc, size_type __res)
1141 const size_type __requested_cap = this->_M_length + __res;
1142 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1144 if (this->_M_length)
1145 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1147 __r->_M_set_length_and_sharable(this->_M_length);
1148 return __r->_M_refdata();
1151 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1156 const size_type __size = this->size();
1157 _M_check_length(__size, __n,
"basic_string::resize");
1159 this->append(__n - __size, __c);
1160 else if (__n < __size)
1165 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1166 template<
typename _InputIterator>
1170 _InputIterator __k2, __false_type)
1173 const size_type __n1 = __i2 - __i1;
1174 _M_check_length(__n1, __s.
size(),
"basic_string::_M_replace_dispatch");
1175 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1179 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1185 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
1186 _M_mutate(__pos1, __n1, __n2);
1188 _M_assign(_M_data() + __pos1, __n2, __c);
1192 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1198 _M_mutate(__pos1, __n1, __n2);
1200 _M_copy(_M_data() + __pos1, __s, __n2);
1204 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1205 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1207 copy(_CharT* __s, size_type __n, size_type __pos)
const 1209 _M_check(__pos,
"basic_string::copy");
1210 __n = _M_limit(__pos, __n);
1211 __glibcxx_requires_string_len(__s, __n);
1213 _M_copy(__s, _M_data() + __pos, __n);
1217 #endif // !_GLIBCXX_USE_CXX11_ABI 1219 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1221 operator+(
const _CharT* __lhs,
1224 __glibcxx_requires_string(__lhs);
1226 typedef typename __string_type::size_type __size_type;
1227 const __size_type __len = _Traits::length(__lhs);
1228 __string_type __str;
1229 __str.reserve(__len + __rhs.
size());
1230 __str.append(__lhs, __len);
1231 __str.append(__rhs);
1235 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1240 typedef typename __string_type::size_type __size_type;
1241 __string_type __str;
1242 const __size_type __len = __rhs.
size();
1243 __str.reserve(__len + 1);
1244 __str.append(__size_type(1), __lhs);
1245 __str.append(__rhs);
1249 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1250 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1252 find(
const _CharT* __s, size_type __pos, size_type __n)
const 1255 __glibcxx_requires_string_len(__s, __n);
1256 const size_type __size = this->size();
1259 return __pos <= __size ? __pos : npos;
1260 if (__pos >= __size)
1263 const _CharT __elem0 = __s[0];
1264 const _CharT*
const __data = data();
1265 const _CharT* __first = __data + __pos;
1266 const _CharT*
const __last = __data + __size;
1267 size_type __len = __size - __pos;
1269 while (__len >= __n)
1272 __first = traits_type::find(__first, __len - __n + 1, __elem0);
1278 if (traits_type::compare(__first, __s, __n) == 0)
1279 return __first - __data;
1280 __len = __last - ++__first;
1285 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1286 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1288 find(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1290 size_type __ret = npos;
1291 const size_type __size = this->size();
1294 const _CharT* __data = _M_data();
1295 const size_type __n = __size - __pos;
1296 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1298 __ret = __p - __data;
1303 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1304 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1306 rfind(
const _CharT* __s, size_type __pos, size_type __n)
const 1309 __glibcxx_requires_string_len(__s, __n);
1310 const size_type __size = this->size();
1313 __pos =
std::min(size_type(__size - __n), __pos);
1314 const _CharT* __data = _M_data();
1317 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1320 while (__pos-- > 0);
1325 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1326 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1328 rfind(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1330 size_type __size = this->size();
1333 if (--__size > __pos)
1335 for (++__size; __size-- > 0; )
1336 if (traits_type::eq(_M_data()[__size], __c))
1342 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1343 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1348 __glibcxx_requires_string_len(__s, __n);
1349 for (; __n && __pos < this->size(); ++__pos)
1351 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1358 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1359 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1364 __glibcxx_requires_string_len(__s, __n);
1365 size_type __size = this->size();
1368 if (--__size > __pos)
1372 if (traits_type::find(__s, __n, _M_data()[__size]))
1375 while (__size-- != 0);
1380 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1381 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1386 __glibcxx_requires_string_len(__s, __n);
1387 for (; __pos < this->size(); ++__pos)
1388 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1393 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1394 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1398 for (; __pos < this->size(); ++__pos)
1399 if (!traits_type::eq(_M_data()[__pos], __c))
1404 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1405 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1410 __glibcxx_requires_string_len(__s, __n);
1411 size_type __size = this->size();
1414 if (--__size > __pos)
1418 if (!traits_type::find(__s, __n, _M_data()[__size]))
1426 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1427 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1431 size_type __size = this->size();
1434 if (--__size > __pos)
1438 if (!traits_type::eq(_M_data()[__size], __c))
1446 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1451 _M_check(__pos,
"basic_string::compare");
1452 __n = _M_limit(__pos, __n);
1453 const size_type __osize = __str.
size();
1454 const size_type __len =
std::min(__n, __osize);
1455 int __r = traits_type::compare(_M_data() + __pos, __str.
data(), __len);
1457 __r = _S_compare(__n, __osize);
1461 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1465 size_type __pos2, size_type __n2)
const 1467 _M_check(__pos1,
"basic_string::compare");
1468 __str._M_check(__pos2,
"basic_string::compare");
1469 __n1 = _M_limit(__pos1, __n1);
1470 __n2 = __str._M_limit(__pos2, __n2);
1471 const size_type __len =
std::min(__n1, __n2);
1472 int __r = traits_type::compare(_M_data() + __pos1,
1473 __str.
data() + __pos2, __len);
1475 __r = _S_compare(__n1, __n2);
1479 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1482 compare(
const _CharT* __s)
const _GLIBCXX_NOEXCEPT
1484 __glibcxx_requires_string(__s);
1485 const size_type __size = this->size();
1486 const size_type __osize = traits_type::length(__s);
1487 const size_type __len =
std::min(__size, __osize);
1488 int __r = traits_type::compare(_M_data(), __s, __len);
1490 __r = _S_compare(__size, __osize);
1494 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1497 compare(size_type __pos, size_type __n1,
const _CharT* __s)
const 1499 __glibcxx_requires_string(__s);
1500 _M_check(__pos,
"basic_string::compare");
1501 __n1 = _M_limit(__pos, __n1);
1502 const size_type __osize = traits_type::length(__s);
1503 const size_type __len =
std::min(__n1, __osize);
1504 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1506 __r = _S_compare(__n1, __osize);
1510 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1513 compare(size_type __pos, size_type __n1,
const _CharT* __s,
1514 size_type __n2)
const 1516 __glibcxx_requires_string_len(__s, __n2);
1517 _M_check(__pos,
"basic_string::compare");
1518 __n1 = _M_limit(__pos, __n1);
1519 const size_type __len =
std::min(__n1, __n2);
1520 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1522 __r = _S_compare(__n1, __n2);
1527 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1534 typedef typename __istream_type::ios_base __ios_base;
1535 typedef typename __istream_type::int_type __int_type;
1536 typedef typename __string_type::size_type __size_type;
1538 typedef typename __ctype_type::ctype_base __ctype_base;
1540 __size_type __extracted = 0;
1541 typename __ios_base::iostate __err = __ios_base::goodbit;
1542 typename __istream_type::sentry __cerb(__in,
false);
1550 __size_type __len = 0;
1552 const __size_type __n = __w > 0 ?
static_cast<__size_type
>(__w)
1554 const __ctype_type& __ct = use_facet<__ctype_type>(__in.
getloc());
1555 const __int_type __eof = _Traits::eof();
1556 __int_type __c = __in.
rdbuf()->sgetc();
1558 while (__extracted < __n
1559 && !_Traits::eq_int_type(__c, __eof)
1560 && !__ct.is(__ctype_base::space,
1561 _Traits::to_char_type(__c)))
1563 if (__len ==
sizeof(__buf) /
sizeof(_CharT))
1565 __str.
append(__buf,
sizeof(__buf) /
sizeof(_CharT));
1568 __buf[__len++] = _Traits::to_char_type(__c);
1570 __c = __in.
rdbuf()->snextc();
1572 __str.
append(__buf, __len);
1574 if (_Traits::eq_int_type(__c, __eof))
1575 __err |= __ios_base::eofbit;
1580 __in._M_setstate(__ios_base::badbit);
1581 __throw_exception_again;
1588 __in._M_setstate(__ios_base::badbit);
1593 __err |= __ios_base::failbit;
1599 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1606 typedef typename __istream_type::ios_base __ios_base;
1607 typedef typename __istream_type::int_type __int_type;
1608 typedef typename __string_type::size_type __size_type;
1610 __size_type __extracted = 0;
1611 const __size_type __n = __str.
max_size();
1612 typename __ios_base::iostate __err = __ios_base::goodbit;
1613 typename __istream_type::sentry __cerb(__in,
true);
1619 const __int_type __idelim = _Traits::to_int_type(__delim);
1620 const __int_type __eof = _Traits::eof();
1621 __int_type __c = __in.
rdbuf()->sgetc();
1623 while (__extracted < __n
1624 && !_Traits::eq_int_type(__c, __eof)
1625 && !_Traits::eq_int_type(__c, __idelim))
1627 __str += _Traits::to_char_type(__c);
1629 __c = __in.
rdbuf()->snextc();
1632 if (_Traits::eq_int_type(__c, __eof))
1633 __err |= __ios_base::eofbit;
1634 else if (_Traits::eq_int_type(__c, __idelim))
1637 __in.
rdbuf()->sbumpc();
1640 __err |= __ios_base::failbit;
1644 __in._M_setstate(__ios_base::badbit);
1645 __throw_exception_again;
1652 __in._M_setstate(__ios_base::badbit);
1656 __err |= __ios_base::failbit;
1664 #if _GLIBCXX_EXTERN_TEMPLATE > 0 && __cplusplus <= 201402L 1671 operator<<(basic_ostream<char>&,
const string&);
1679 #ifdef _GLIBCXX_USE_WCHAR_T 1686 operator<<(basic_ostream<wchar_t>&,
const wstring&);
1696 _GLIBCXX_END_NAMESPACE_VERSION
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
void setstate(iostate __state)
Sets additional flags in the error state.
streamsize width() const
Flags access.
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
ISO C++ entities toplevel namespace is std.
Primary class template ctype facet.This template class defines classification and conversion function...
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
basic_string & append(const basic_string &__str)
Append a string to this string.
Managing sequences of characters and character-like objects.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
locale getloc() const
Locale access.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
basic_string()
Default constructor creates an empty string.
void swap(basic_string &__s)
Swap contents with another string.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
static const size_type npos
Value returned by various member functions when they fail.
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
const _CharT * data() const noexcept
Return const pointer to contents.
Template class basic_istream.
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Forward iterators support a superset of input iterator operations.