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)
484 const size_type __poff = (__s - __p) + (__len2 - __len1);
485 this->_S_copy(__p, __p + __poff, __len2);
489 const size_type __nleft = (__p + __len1) - __s;
490 this->_S_move(__p, __s, __nleft);
491 this->_S_copy(__p + __nleft, __p + __len2,
498 this->_M_mutate(__pos, __len1, __s, __len2);
500 this->_M_set_length(__new_size);
504 template<
typename _CharT,
typename _Traits,
typename _Alloc>
505 typename basic_string<_CharT, _Traits, _Alloc>::size_type
507 copy(_CharT* __s, size_type __n, size_type __pos)
const 509 _M_check(__pos,
"basic_string::copy");
510 __n = _M_limit(__pos, __n);
511 __glibcxx_requires_string_len(__s, __n);
513 _S_copy(__s, _M_data() + __pos, __n);
518 #else // !_GLIBCXX_USE_CXX11_ABI 520 template<
typename _CharT,
typename _Traits,
typename _Alloc>
521 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
522 basic_string<_CharT, _Traits, _Alloc>::
523 _Rep::_S_max_size = (((npos -
sizeof(_Rep_base))/
sizeof(_CharT)) - 1) / 4;
525 template<
typename _CharT,
typename _Traits,
typename _Alloc>
527 basic_string<_CharT, _Traits, _Alloc>::
528 _Rep::_S_terminal = _CharT();
530 template<
typename _CharT,
typename _Traits,
typename _Alloc>
531 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
536 template<
typename _CharT,
typename _Traits,
typename _Alloc>
537 typename basic_string<_CharT, _Traits, _Alloc>::size_type
538 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
539 (
sizeof(_Rep_base) +
sizeof(_CharT) +
sizeof(size_type) - 1) /
546 template<
typename _CharT,
typename _Traits,
typename _Alloc>
547 template<
typename _InIterator>
549 basic_string<_CharT, _Traits, _Alloc>::
550 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
553 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 554 if (__beg == __end && __a == _Alloc())
555 return _S_empty_rep()._M_refdata();
560 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
562 __buf[__len++] = *__beg;
565 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
566 _M_copy(__r->_M_refdata(), __buf, __len);
569 while (__beg != __end)
571 if (__len == __r->_M_capacity)
574 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
575 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
576 __r->_M_destroy(__a);
579 __r->_M_refdata()[__len++] = *__beg;
585 __r->_M_destroy(__a);
586 __throw_exception_again;
588 __r->_M_set_length_and_sharable(__len);
589 return __r->_M_refdata();
592 template<
typename _CharT,
typename _Traits,
typename _Alloc>
593 template <
typename _InIterator>
595 basic_string<_CharT, _Traits, _Alloc>::
596 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
597 forward_iterator_tag)
599 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 600 if (__beg == __end && __a == _Alloc())
601 return _S_empty_rep()._M_refdata();
604 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
605 __throw_logic_error(__N(
"basic_string::_S_construct null not valid"));
607 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
610 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
612 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
615 __r->_M_destroy(__a);
616 __throw_exception_again;
618 __r->_M_set_length_and_sharable(__dnew);
619 return __r->_M_refdata();
622 template<
typename _CharT,
typename _Traits,
typename _Alloc>
624 basic_string<_CharT, _Traits, _Alloc>::
625 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
627 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 628 if (__n == 0 && __a == _Alloc())
629 return _S_empty_rep()._M_refdata();
632 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
634 _M_assign(__r->_M_refdata(), __n, __c);
636 __r->_M_set_length_and_sharable(__n);
637 return __r->_M_refdata();
640 template<
typename _CharT,
typename _Traits,
typename _Alloc>
643 : _M_dataplus(_S_construct(__str._M_data()
644 + __str._M_check(__pos,
645 "basic_string::basic_string"),
646 __str._M_data() + __str._M_limit(__pos, npos)
650 template<
typename _CharT,
typename _Traits,
typename _Alloc>
653 : _M_dataplus(_S_construct(__str._M_data()
654 + __str._M_check(__pos,
655 "basic_string::basic_string"),
656 __str._M_data() + __str._M_limit(__pos, __n)
657 + __pos, _Alloc()), _Alloc())
660 template<
typename _CharT,
typename _Traits,
typename _Alloc>
663 size_type __n,
const _Alloc& __a)
664 : _M_dataplus(_S_construct(__str._M_data()
665 + __str._M_check(__pos,
666 "basic_string::basic_string"),
667 __str._M_data() + __str._M_limit(__pos, __n)
671 template<
typename _CharT,
typename _Traits,
typename _Alloc>
676 if (_M_rep() != __str._M_rep())
679 const allocator_type __a = this->get_allocator();
680 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.
get_allocator());
681 _M_rep()->_M_dispose(__a);
687 template<
typename _CharT,
typename _Traits,
typename _Alloc>
692 __glibcxx_requires_string_len(__s, __n);
693 _M_check_length(this->
size(), __n,
"basic_string::assign");
694 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
695 return _M_replace_safe(size_type(0), this->
size(), __s, __n);
699 const size_type __pos = __s - _M_data();
701 _M_copy(_M_data(), __s, __n);
703 _M_move(_M_data(), __s, __n);
704 _M_rep()->_M_set_length_and_sharable(__n);
709 template<
typename _CharT,
typename _Traits,
typename _Alloc>
716 _M_check_length(size_type(0), __n,
"basic_string::append");
717 const size_type __len = __n + this->
size();
718 if (__len > this->capacity() || _M_rep()->_M_is_shared())
719 this->reserve(__len);
720 _M_assign(_M_data() + this->
size(), __n, __c);
721 _M_rep()->_M_set_length_and_sharable(__len);
726 template<
typename _CharT,
typename _Traits,
typename _Alloc>
731 __glibcxx_requires_string_len(__s, __n);
734 _M_check_length(size_type(0), __n,
"basic_string::append");
735 const size_type __len = __n + this->
size();
736 if (__len > this->capacity() || _M_rep()->_M_is_shared())
738 if (_M_disjunct(__s))
739 this->reserve(__len);
742 const size_type __off = __s - _M_data();
743 this->reserve(__len);
744 __s = _M_data() + __off;
747 _M_copy(_M_data() + this->
size(), __s, __n);
748 _M_rep()->_M_set_length_and_sharable(__len);
753 template<
typename _CharT,
typename _Traits,
typename _Alloc>
758 const size_type __size = __str.
size();
761 const size_type __len = __size + this->
size();
762 if (__len > this->capacity() || _M_rep()->_M_is_shared())
763 this->reserve(__len);
764 _M_copy(_M_data() + this->
size(), __str._M_data(), __size);
765 _M_rep()->_M_set_length_and_sharable(__len);
770 template<
typename _CharT,
typename _Traits,
typename _Alloc>
775 __str._M_check(__pos,
"basic_string::append");
776 __n = __str._M_limit(__pos, __n);
779 const size_type __len = __n + this->
size();
780 if (__len > this->capacity() || _M_rep()->_M_is_shared())
781 this->reserve(__len);
782 _M_copy(_M_data() + this->
size(), __str._M_data() + __pos, __n);
783 _M_rep()->_M_set_length_and_sharable(__len);
788 template<
typename _CharT,
typename _Traits,
typename _Alloc>
791 insert(size_type __pos,
const _CharT* __s, size_type __n)
793 __glibcxx_requires_string_len(__s, __n);
794 _M_check(__pos,
"basic_string::insert");
795 _M_check_length(size_type(0), __n,
"basic_string::insert");
796 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
797 return _M_replace_safe(__pos, size_type(0), __s, __n);
801 const size_type __off = __s - _M_data();
802 _M_mutate(__pos, 0, __n);
803 __s = _M_data() + __off;
804 _CharT* __p = _M_data() + __pos;
805 if (__s + __n <= __p)
806 _M_copy(__p, __s, __n);
808 _M_copy(__p, __s + __n, __n);
811 const size_type __nleft = __p - __s;
812 _M_copy(__p, __s, __nleft);
813 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
819 template<
typename _CharT,
typename _Traits,
typename _Alloc>
820 typename basic_string<_CharT, _Traits, _Alloc>::iterator
822 erase(iterator __first, iterator __last)
824 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
825 && __last <= _M_iend());
830 const size_type __size = __last - __first;
833 const size_type __pos = __first - _M_ibegin();
834 _M_mutate(__pos, __size, size_type(0));
835 _M_rep()->_M_set_leaked();
836 return iterator(_M_data() + __pos);
842 template<
typename _CharT,
typename _Traits,
typename _Alloc>
845 replace(size_type __pos, size_type __n1,
const _CharT* __s,
848 __glibcxx_requires_string_len(__s, __n2);
849 _M_check(__pos,
"basic_string::replace");
850 __n1 = _M_limit(__pos, __n1);
851 _M_check_length(__n1, __n2,
"basic_string::replace");
853 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
854 return _M_replace_safe(__pos, __n1, __s, __n2);
855 else if ((__left = __s + __n2 <= _M_data() + __pos)
856 || _M_data() + __pos + __n1 <= __s)
859 size_type __off = __s - _M_data();
860 __left ? __off : (__off += __n2 - __n1);
861 _M_mutate(__pos, __n1, __n2);
862 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
869 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
873 template<
typename _CharT,
typename _Traits,
typename _Alloc>
878 const size_type __size =
sizeof(_Rep_base) +
879 (this->_M_capacity + 1) *
sizeof(_CharT);
880 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(
this), __size);
883 template<
typename _CharT,
typename _Traits,
typename _Alloc>
885 basic_string<_CharT, _Traits, _Alloc>::
888 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 889 if (_M_rep() == &_S_empty_rep())
892 if (_M_rep()->_M_is_shared())
894 _M_rep()->_M_set_leaked();
897 template<
typename _CharT,
typename _Traits,
typename _Alloc>
899 basic_string<_CharT, _Traits, _Alloc>::
900 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
902 const size_type __old_size = this->
size();
903 const size_type __new_size = __old_size + __len2 - __len1;
904 const size_type __how_much = __old_size - __pos - __len1;
906 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
909 const allocator_type __a = get_allocator();
910 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
913 _M_copy(__r->_M_refdata(), _M_data(), __pos);
915 _M_copy(__r->_M_refdata() + __pos + __len2,
916 _M_data() + __pos + __len1, __how_much);
918 _M_rep()->_M_dispose(__a);
919 _M_data(__r->_M_refdata());
921 else if (__how_much && __len1 != __len2)
924 _M_move(_M_data() + __pos + __len2,
925 _M_data() + __pos + __len1, __how_much);
927 _M_rep()->_M_set_length_and_sharable(__new_size);
930 template<
typename _CharT,
typename _Traits,
typename _Alloc>
932 basic_string<_CharT, _Traits, _Alloc>::
933 reserve(size_type __res)
935 const size_type __capacity = capacity();
941 if (__res <= __capacity)
943 if (!_M_rep()->_M_is_shared())
950 const allocator_type __a = get_allocator();
951 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->
size());
952 _M_rep()->_M_dispose(__a);
956 template<
typename _CharT,
typename _Traits,
typename _Alloc>
962 if (_M_rep()->_M_is_leaked())
963 _M_rep()->_M_set_sharable();
964 if (__s._M_rep()->_M_is_leaked())
965 __s._M_rep()->_M_set_sharable();
966 if (this->get_allocator() == __s.get_allocator())
968 _CharT* __tmp = _M_data();
969 _M_data(__s._M_data());
976 __s.get_allocator());
977 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
978 this->get_allocator());
984 template<
typename _CharT,
typename _Traits,
typename _Alloc>
987 _S_create(size_type __capacity, size_type __old_capacity,
988 const _Alloc& __alloc)
992 if (__capacity > _S_max_size)
993 __throw_length_error(__N(
"basic_string::_S_create"));
1018 const size_type __pagesize = 4096;
1019 const size_type __malloc_header_size = 4 *
sizeof(
void*);
1027 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1028 __capacity = 2 * __old_capacity;
1033 size_type __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1035 const size_type __adj_size = __size + __malloc_header_size;
1036 if (__adj_size > __pagesize && __capacity > __old_capacity)
1038 const size_type __extra = __pagesize - __adj_size % __pagesize;
1039 __capacity += __extra /
sizeof(_CharT);
1041 if (__capacity > _S_max_size)
1042 __capacity = _S_max_size;
1043 __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1048 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1049 _Rep *__p =
new (__place) _Rep;
1050 __p->_M_capacity = __capacity;
1058 __p->_M_set_sharable();
1062 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1064 basic_string<_CharT, _Traits, _Alloc>::_Rep::
1065 _M_clone(
const _Alloc& __alloc, size_type __res)
1068 const size_type __requested_cap = this->_M_length + __res;
1069 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1071 if (this->_M_length)
1072 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1074 __r->_M_set_length_and_sharable(this->_M_length);
1075 return __r->_M_refdata();
1078 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1080 basic_string<_CharT, _Traits, _Alloc>::
1081 resize(size_type __n, _CharT __c)
1083 const size_type __size = this->
size();
1084 _M_check_length(__size, __n,
"basic_string::resize");
1086 this->append(__n - __size, __c);
1087 else if (__n < __size)
1092 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1093 template<
typename _InputIterator>
1097 _InputIterator __k2, __false_type)
1100 const size_type __n1 = __i2 - __i1;
1101 _M_check_length(__n1, __s.size(),
"basic_string::_M_replace_dispatch");
1102 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1106 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1107 basic_string<_CharT, _Traits, _Alloc>&
1108 basic_string<_CharT, _Traits, _Alloc>::
1109 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1112 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
1113 _M_mutate(__pos1, __n1, __n2);
1115 _M_assign(_M_data() + __pos1, __n2, __c);
1119 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1120 basic_string<_CharT, _Traits, _Alloc>&
1121 basic_string<_CharT, _Traits, _Alloc>::
1122 _M_replace_safe(size_type __pos1, size_type __n1,
const _CharT* __s,
1125 _M_mutate(__pos1, __n1, __n2);
1127 _M_copy(_M_data() + __pos1, __s, __n2);
1131 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1133 basic_string<_CharT, _Traits, _Alloc>::
1136 #if __cpp_exceptions 1137 if (length() < capacity() || _M_rep()->_M_is_shared())
1140 const allocator_type __a = get_allocator();
1141 _CharT* __tmp = _M_rep()->_M_clone(__a);
1142 _M_rep()->_M_dispose(__a);
1152 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1153 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1155 copy(_CharT* __s, size_type __n, size_type __pos)
const 1157 _M_check(__pos,
"basic_string::copy");
1158 __n = _M_limit(__pos, __n);
1159 __glibcxx_requires_string_len(__s, __n);
1161 _M_copy(__s, _M_data() + __pos, __n);
1165 #endif // !_GLIBCXX_USE_CXX11_ABI 1167 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1172 __glibcxx_requires_string(__lhs);
1174 typedef typename __string_type::size_type __size_type;
1176 rebind<_CharT>::other _Char_alloc_type;
1178 const __size_type __len = _Traits::length(__lhs);
1179 __string_type __str(_Alloc_traits::_S_select_on_copy(
1181 __str.reserve(__len + __rhs.
size());
1182 __str.append(__lhs, __len);
1183 __str.append(__rhs);
1187 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1188 basic_string<_CharT, _Traits, _Alloc>
1192 typedef typename __string_type::size_type __size_type;
1194 rebind<_CharT>::other _Char_alloc_type;
1196 __string_type __str(_Alloc_traits::_S_select_on_copy(
1198 const __size_type __len = __rhs.
size();
1199 __str.reserve(__len + 1);
1200 __str.append(__size_type(1), __lhs);
1201 __str.append(__rhs);
1205 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1206 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1207 basic_string<_CharT, _Traits, _Alloc>::
1208 find(
const _CharT* __s, size_type __pos, size_type __n)
const 1211 __glibcxx_requires_string_len(__s, __n);
1212 const size_type __size = this->
size();
1215 return __pos <= __size ? __pos : npos;
1216 if (__pos >= __size)
1219 const _CharT __elem0 = __s[0];
1220 const _CharT*
const __data =
data();
1221 const _CharT* __first = __data + __pos;
1222 const _CharT*
const __last = __data + __size;
1223 size_type __len = __size - __pos;
1225 while (__len >= __n)
1228 __first = traits_type::find(__first, __len - __n + 1, __elem0);
1234 if (traits_type::compare(__first, __s, __n) == 0)
1235 return __first - __data;
1236 __len = __last - ++__first;
1241 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1242 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1244 find(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1246 size_type __ret = npos;
1247 const size_type __size = this->
size();
1250 const _CharT* __data = _M_data();
1251 const size_type __n = __size - __pos;
1252 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1254 __ret = __p - __data;
1259 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1260 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1262 rfind(
const _CharT* __s, size_type __pos, size_type __n)
const 1265 __glibcxx_requires_string_len(__s, __n);
1266 const size_type __size = this->
size();
1269 __pos =
std::min(size_type(__size - __n), __pos);
1270 const _CharT* __data = _M_data();
1273 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1276 while (__pos-- > 0);
1281 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1282 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1284 rfind(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1286 size_type __size = this->
size();
1289 if (--__size > __pos)
1291 for (++__size; __size-- > 0; )
1292 if (traits_type::eq(_M_data()[__size], __c))
1298 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1299 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1304 __glibcxx_requires_string_len(__s, __n);
1305 for (; __n && __pos < this->
size(); ++__pos)
1307 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1314 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1315 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1320 __glibcxx_requires_string_len(__s, __n);
1321 size_type __size = this->
size();
1324 if (--__size > __pos)
1328 if (traits_type::find(__s, __n, _M_data()[__size]))
1331 while (__size-- != 0);
1336 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1337 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1342 __glibcxx_requires_string_len(__s, __n);
1343 for (; __pos < this->
size(); ++__pos)
1344 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1349 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1350 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1354 for (; __pos < this->
size(); ++__pos)
1355 if (!traits_type::eq(_M_data()[__pos], __c))
1360 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1361 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1366 __glibcxx_requires_string_len(__s, __n);
1367 size_type __size = this->
size();
1370 if (--__size > __pos)
1374 if (!traits_type::find(__s, __n, _M_data()[__size]))
1382 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1383 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1387 size_type __size = this->
size();
1390 if (--__size > __pos)
1394 if (!traits_type::eq(_M_data()[__size], __c))
1402 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1407 _M_check(__pos,
"basic_string::compare");
1408 __n = _M_limit(__pos, __n);
1409 const size_type __osize = __str.
size();
1410 const size_type __len =
std::min(__n, __osize);
1411 int __r = traits_type::compare(_M_data() + __pos, __str.
data(), __len);
1413 __r = _S_compare(__n, __osize);
1417 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1421 size_type __pos2, size_type __n2)
const 1423 _M_check(__pos1,
"basic_string::compare");
1424 __str._M_check(__pos2,
"basic_string::compare");
1425 __n1 = _M_limit(__pos1, __n1);
1426 __n2 = __str._M_limit(__pos2, __n2);
1427 const size_type __len =
std::min(__n1, __n2);
1428 int __r = traits_type::compare(_M_data() + __pos1,
1429 __str.
data() + __pos2, __len);
1431 __r = _S_compare(__n1, __n2);
1435 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1438 compare(
const _CharT* __s)
const _GLIBCXX_NOEXCEPT
1440 __glibcxx_requires_string(__s);
1441 const size_type __size = this->
size();
1442 const size_type __osize = traits_type::length(__s);
1443 const size_type __len =
std::min(__size, __osize);
1444 int __r = traits_type::compare(_M_data(), __s, __len);
1446 __r = _S_compare(__size, __osize);
1450 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1453 compare(size_type __pos, size_type __n1,
const _CharT* __s)
const 1455 __glibcxx_requires_string(__s);
1456 _M_check(__pos,
"basic_string::compare");
1457 __n1 = _M_limit(__pos, __n1);
1458 const size_type __osize = traits_type::length(__s);
1459 const size_type __len =
std::min(__n1, __osize);
1460 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1462 __r = _S_compare(__n1, __osize);
1466 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1469 compare(size_type __pos, size_type __n1,
const _CharT* __s,
1470 size_type __n2)
const 1472 __glibcxx_requires_string_len(__s, __n2);
1473 _M_check(__pos,
"basic_string::compare");
1474 __n1 = _M_limit(__pos, __n1);
1475 const size_type __len =
std::min(__n1, __n2);
1476 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1478 __r = _S_compare(__n1, __n2);
1483 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1490 typedef typename __istream_type::ios_base __ios_base;
1491 typedef typename __istream_type::int_type __int_type;
1492 typedef typename __string_type::size_type __size_type;
1494 typedef typename __ctype_type::ctype_base __ctype_base;
1496 __size_type __extracted = 0;
1497 typename __ios_base::iostate __err = __ios_base::goodbit;
1498 typename __istream_type::sentry __cerb(__in,
false);
1506 __size_type __len = 0;
1508 const __size_type __n = __w > 0 ?
static_cast<__size_type
>(__w)
1510 const __ctype_type& __ct = use_facet<__ctype_type>(__in.
getloc());
1511 const __int_type __eof = _Traits::eof();
1512 __int_type __c = __in.
rdbuf()->sgetc();
1514 while (__extracted < __n
1515 && !_Traits::eq_int_type(__c, __eof)
1516 && !__ct.is(__ctype_base::space,
1517 _Traits::to_char_type(__c)))
1519 if (__len ==
sizeof(__buf) /
sizeof(_CharT))
1521 __str.
append(__buf,
sizeof(__buf) /
sizeof(_CharT));
1524 __buf[__len++] = _Traits::to_char_type(__c);
1526 __c = __in.
rdbuf()->snextc();
1528 __str.
append(__buf, __len);
1530 if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
1531 __err |= __ios_base::eofbit;
1536 __in._M_setstate(__ios_base::badbit);
1537 __throw_exception_again;
1544 __in._M_setstate(__ios_base::badbit);
1549 __err |= __ios_base::failbit;
1555 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1556 basic_istream<_CharT, _Traits>&
1562 typedef typename __istream_type::ios_base __ios_base;
1563 typedef typename __istream_type::int_type __int_type;
1564 typedef typename __string_type::size_type __size_type;
1566 __size_type __extracted = 0;
1567 const __size_type __n = __str.
max_size();
1568 typename __ios_base::iostate __err = __ios_base::goodbit;
1569 typename __istream_type::sentry __cerb(__in,
true);
1575 const __int_type __idelim = _Traits::to_int_type(__delim);
1576 const __int_type __eof = _Traits::eof();
1577 __int_type __c = __in.
rdbuf()->sgetc();
1579 while (__extracted < __n
1580 && !_Traits::eq_int_type(__c, __eof)
1581 && !_Traits::eq_int_type(__c, __idelim))
1583 __str += _Traits::to_char_type(__c);
1585 __c = __in.
rdbuf()->snextc();
1588 if (_Traits::eq_int_type(__c, __eof))
1589 __err |= __ios_base::eofbit;
1590 else if (_Traits::eq_int_type(__c, __idelim))
1593 __in.
rdbuf()->sbumpc();
1596 __err |= __ios_base::failbit;
1600 __in._M_setstate(__ios_base::badbit);
1601 __throw_exception_again;
1608 __in._M_setstate(__ios_base::badbit);
1612 __err |= __ios_base::failbit;
1620 #if _GLIBCXX_EXTERN_TEMPLATE 1626 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0 1627 extern template class basic_string<char>;
1628 # elif ! _GLIBCXX_USE_CXX11_ABI 1631 extern template basic_string<char>::size_type
1632 basic_string<char>::_Rep::_S_empty_rep_storage[];
1636 basic_istream<char>&
1639 basic_ostream<char>&
1640 operator<<(basic_ostream<char>&,
const string&);
1642 basic_istream<char>&
1643 getline(basic_istream<char>&,
string&,
char);
1645 basic_istream<char>&
1646 getline(basic_istream<char>&,
string&);
1648 #ifdef _GLIBCXX_USE_WCHAR_T 1649 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0 1650 extern template class basic_string<wchar_t>;
1651 # elif ! _GLIBCXX_USE_CXX11_ABI 1652 extern template basic_string<wchar_t>::size_type
1653 basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1657 basic_istream<wchar_t>&
1660 basic_ostream<wchar_t>&
1661 operator<<(basic_ostream<wchar_t>&,
const wstring&);
1663 basic_istream<wchar_t>&
1666 basic_istream<wchar_t>&
1668 #endif // _GLIBCXX_USE_WCHAR_T 1669 #endif // _GLIBCXX_EXTERN_TEMPLATE 1671 _GLIBCXX_END_NAMESPACE_VERSION
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator+(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The sum of two durations.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Primary class template ctype facet.This template class defines classification and conversion function...
basic_string & append(const basic_string &__str)
Append a string to this string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
static const size_type npos
Value returned by various member functions when they fail.
Forward iterators support a superset of input iterator operations.
Uniform interface to all allocator types.
locale getloc() const
Locale access.
streamsize width() const
Flags access.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
basic_string() noexcept
Default constructor creates an empty string.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
Template class basic_istream.
basic_string< wchar_t > wstring
A string of wchar_t.
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
void reserve()
Equivalent to shrink_to_fit().
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
const _CharT * data() const noexcept
Return const pointer to contents.
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.
ISO C++ entities toplevel namespace is std.
Managing sequences of characters and character-like objects.
Uniform interface to C++98 and C++11 allocators.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
void setstate(iostate __state)
Sets additional flags in the error state.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.