39#ifndef _BASIC_STRING_TCC
40#define _BASIC_STRING_TCC 1
42#pragma GCC system_header
46namespace 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>
284 if (__res < length())
287 const size_type __capacity = capacity();
288 if (__res != __capacity)
290 if (__res > __capacity
291 || __res > size_type(_S_local_capacity))
293 pointer __tmp = _M_create(__res, __capacity);
294 this->_S_copy(__tmp, _M_data(), length() + 1);
299 else if (!_M_is_local())
301 this->_S_copy(_M_local_data(), _M_data(), length() + 1);
302 _M_destroy(__capacity);
303 _M_data(_M_local_data());
308 template<
typename _CharT,
typename _Traits,
typename _Alloc>
310 basic_string<_CharT, _Traits, _Alloc>::
311 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
314 const size_type __how_much = length() - __pos - __len1;
316 size_type __new_capacity = length() + __len2 - __len1;
317 pointer __r = _M_create(__new_capacity, capacity());
320 this->_S_copy(__r, _M_data(), __pos);
322 this->_S_copy(__r + __pos, __s, __len2);
324 this->_S_copy(__r + __pos + __len2,
325 _M_data() + __pos + __len1, __how_much);
329 _M_capacity(__new_capacity);
332 template<
typename _CharT,
typename _Traits,
typename _Alloc>
334 basic_string<_CharT, _Traits, _Alloc>::
335 _M_erase(size_type __pos, size_type __n)
337 const size_type __how_much = length() - __pos - __n;
339 if (__how_much && __n)
340 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
342 _M_set_length(length() - __n);
345 template<
typename _CharT,
typename _Traits,
typename _Alloc>
348 resize(size_type __n, _CharT __c)
350 const size_type __size = this->size();
352 this->append(__n - __size, __c);
353 else if (__n < __size)
354 this->_M_set_length(__n);
357 template<
typename _CharT,
typename _Traits,
typename _Alloc>
358 basic_string<_CharT, _Traits, _Alloc>&
359 basic_string<_CharT, _Traits, _Alloc>::
360 _M_append(
const _CharT* __s, size_type __n)
362 const size_type __len = __n + this->size();
364 if (__len <= this->capacity())
367 this->_S_copy(this->_M_data() + this->size(), __s, __n);
370 this->_M_mutate(this->size(), size_type(0), __s, __n);
372 this->_M_set_length(__len);
376 template<
typename _CharT,
typename _Traits,
typename _Alloc>
377 template<
typename _InputIterator>
378 basic_string<_CharT, _Traits, _Alloc>&
379 basic_string<_CharT, _Traits, _Alloc>::
380 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
381 _InputIterator __k1, _InputIterator __k2,
384 const basic_string __s(__k1, __k2);
385 const size_type __n1 = __i2 - __i1;
386 return _M_replace(__i1 -
begin(), __n1, __s._M_data(),
390 template<
typename _CharT,
typename _Traits,
typename _Alloc>
391 basic_string<_CharT, _Traits, _Alloc>&
392 basic_string<_CharT, _Traits, _Alloc>::
393 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
396 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
398 const size_type __old_size = this->size();
399 const size_type __new_size = __old_size + __n2 - __n1;
401 if (__new_size <= this->capacity())
403 pointer __p = this->_M_data() + __pos1;
405 const size_type __how_much = __old_size - __pos1 - __n1;
406 if (__how_much && __n1 != __n2)
407 this->_S_move(__p + __n2, __p + __n1, __how_much);
410 this->_M_mutate(__pos1, __n1, 0, __n2);
413 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
415 this->_M_set_length(__new_size);
419 template<
typename _CharT,
typename _Traits,
typename _Alloc>
420 basic_string<_CharT, _Traits, _Alloc>&
421 basic_string<_CharT, _Traits, _Alloc>::
422 _M_replace(size_type __pos, size_type __len1,
const _CharT* __s,
423 const size_type __len2)
425 _M_check_length(__len1, __len2,
"basic_string::_M_replace");
427 const size_type __old_size = this->size();
428 const size_type __new_size = __old_size + __len2 - __len1;
430 if (__new_size <= this->capacity())
432 pointer __p = this->_M_data() + __pos;
434 const size_type __how_much = __old_size - __pos - __len1;
435 if (_M_disjunct(__s))
437 if (__how_much && __len1 != __len2)
438 this->_S_move(__p + __len2, __p + __len1, __how_much);
440 this->_S_copy(__p, __s, __len2);
445 if (__len2 && __len2 <= __len1)
446 this->_S_move(__p, __s, __len2);
447 if (__how_much && __len1 != __len2)
448 this->_S_move(__p + __len2, __p + __len1, __how_much);
451 if (__s + __len2 <= __p + __len1)
452 this->_S_move(__p, __s, __len2);
453 else if (__s >= __p + __len1)
454 this->_S_copy(__p, __s + __len2 - __len1, __len2);
457 const size_type __nleft = (__p + __len1) - __s;
458 this->_S_move(__p, __s, __nleft);
459 this->_S_copy(__p + __nleft, __p + __len2,
466 this->_M_mutate(__pos, __len1, __s, __len2);
468 this->_M_set_length(__new_size);
472 template<
typename _CharT,
typename _Traits,
typename _Alloc>
473 typename basic_string<_CharT, _Traits, _Alloc>::size_type
475 copy(_CharT* __s, size_type __n, size_type __pos)
const
477 _M_check(__pos,
"basic_string::copy");
478 __n = _M_limit(__pos, __n);
479 __glibcxx_requires_string_len(__s, __n);
481 _S_copy(__s, _M_data() + __pos, __n);
488 template<
typename _CharT,
typename _Traits,
typename _Alloc>
489 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
490 basic_string<_CharT, _Traits, _Alloc>::
491 _Rep::_S_max_size = (((npos -
sizeof(_Rep_base))/
sizeof(_CharT)) - 1) / 4;
493 template<
typename _CharT,
typename _Traits,
typename _Alloc>
495 basic_string<_CharT, _Traits, _Alloc>::
496 _Rep::_S_terminal = _CharT();
498 template<
typename _CharT,
typename _Traits,
typename _Alloc>
499 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
504 template<
typename _CharT,
typename _Traits,
typename _Alloc>
505 typename basic_string<_CharT, _Traits, _Alloc>::size_type
506 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
507 (
sizeof(_Rep_base) +
sizeof(_CharT) +
sizeof(size_type) - 1) /
514 template<
typename _CharT,
typename _Traits,
typename _Alloc>
515 template<
typename _InIterator>
517 basic_string<_CharT, _Traits, _Alloc>::
518 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
521#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
522 if (__beg == __end && __a == _Alloc())
523 return _S_empty_rep()._M_refdata();
528 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
530 __buf[__len++] = *__beg;
533 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
534 _M_copy(__r->_M_refdata(), __buf, __len);
537 while (__beg != __end)
539 if (__len == __r->_M_capacity)
542 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
543 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
544 __r->_M_destroy(__a);
547 __r->_M_refdata()[__len++] = *__beg;
553 __r->_M_destroy(__a);
554 __throw_exception_again;
556 __r->_M_set_length_and_sharable(__len);
557 return __r->_M_refdata();
560 template<
typename _CharT,
typename _Traits,
typename _Alloc>
561 template <
typename _InIterator>
563 basic_string<_CharT, _Traits, _Alloc>::
564 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
565 forward_iterator_tag)
567#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
568 if (__beg == __end && __a == _Alloc())
569 return _S_empty_rep()._M_refdata();
572 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
573 __throw_logic_error(__N(
"basic_string::_S_construct null not valid"));
575 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
578 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
580 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
583 __r->_M_destroy(__a);
584 __throw_exception_again;
586 __r->_M_set_length_and_sharable(__dnew);
587 return __r->_M_refdata();
590 template<
typename _CharT,
typename _Traits,
typename _Alloc>
592 basic_string<_CharT, _Traits, _Alloc>::
593 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
595#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
596 if (__n == 0 && __a == _Alloc())
597 return _S_empty_rep()._M_refdata();
600 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
602 _M_assign(__r->_M_refdata(), __n, __c);
604 __r->_M_set_length_and_sharable(__n);
605 return __r->_M_refdata();
608 template<
typename _CharT,
typename _Traits,
typename _Alloc>
611 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
612 __str.get_allocator()),
613 __str.get_allocator())
616 template<
typename _CharT,
typename _Traits,
typename _Alloc>
619 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
622 template<
typename _CharT,
typename _Traits,
typename _Alloc>
625 : _M_dataplus(_S_construct(__str._M_data()
626 + __str._M_check(__pos,
627 "basic_string::basic_string"),
628 __str._M_data() + __str._M_limit(__pos, npos)
632 template<
typename _CharT,
typename _Traits,
typename _Alloc>
635 : _M_dataplus(_S_construct(__str._M_data()
636 + __str._M_check(__pos,
637 "basic_string::basic_string"),
638 __str._M_data() + __str._M_limit(__pos, __n)
639 + __pos, _Alloc()), _Alloc())
642 template<
typename _CharT,
typename _Traits,
typename _Alloc>
645 size_type __n,
const _Alloc& __a)
646 : _M_dataplus(_S_construct(__str._M_data()
647 + __str._M_check(__pos,
648 "basic_string::basic_string"),
649 __str._M_data() + __str._M_limit(__pos, __n)
654 template<
typename _CharT,
typename _Traits,
typename _Alloc>
656 basic_string(
const _CharT* __s, size_type __n,
const _Alloc& __a)
657 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
661 template<
typename _CharT,
typename _Traits,
typename _Alloc>
664 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
665 __s + npos, __a), __a)
668 template<
typename _CharT,
typename _Traits,
typename _Alloc>
670 basic_string(size_type __n, _CharT __c,
const _Alloc& __a)
671 : _M_dataplus(_S_construct(__n, __c, __a), __a)
675 template<
typename _CharT,
typename _Traits,
typename _Alloc>
676 template<
typename _InputIterator>
678 basic_string(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a)
679 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
682#if __cplusplus >= 201103L
683 template<
typename _CharT,
typename _Traits,
typename _Alloc>
686 : _M_dataplus(_S_construct(__l.
begin(), __l.
end(), __a), __a)
690 template<
typename _CharT,
typename _Traits,
typename _Alloc>
695 if (_M_rep() != __str._M_rep())
698 const allocator_type __a = this->get_allocator();
699 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.
get_allocator());
700 _M_rep()->_M_dispose(__a);
706 template<
typename _CharT,
typename _Traits,
typename _Alloc>
709 assign(
const _CharT* __s, size_type __n)
711 __glibcxx_requires_string_len(__s, __n);
712 _M_check_length(this->size(), __n,
"basic_string::assign");
713 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
714 return _M_replace_safe(size_type(0), this->size(), __s, __n);
718 const size_type __pos = __s - _M_data();
720 _M_copy(_M_data(), __s, __n);
722 _M_move(_M_data(), __s, __n);
723 _M_rep()->_M_set_length_and_sharable(__n);
728 template<
typename _CharT,
typename _Traits,
typename _Alloc>
731 append(size_type __n, _CharT __c)
735 _M_check_length(size_type(0), __n,
"basic_string::append");
736 const size_type __len = __n + this->size();
737 if (__len > this->capacity() || _M_rep()->_M_is_shared())
738 this->reserve(__len);
739 _M_assign(_M_data() + this->size(), __n, __c);
740 _M_rep()->_M_set_length_and_sharable(__len);
745 template<
typename _CharT,
typename _Traits,
typename _Alloc>
748 append(
const _CharT* __s, size_type __n)
750 __glibcxx_requires_string_len(__s, __n);
753 _M_check_length(size_type(0), __n,
"basic_string::append");
754 const size_type __len = __n + this->size();
755 if (__len > this->capacity() || _M_rep()->_M_is_shared())
757 if (_M_disjunct(__s))
758 this->reserve(__len);
761 const size_type __off = __s - _M_data();
762 this->reserve(__len);
763 __s = _M_data() + __off;
766 _M_copy(_M_data() + this->size(), __s, __n);
767 _M_rep()->_M_set_length_and_sharable(__len);
772 template<
typename _CharT,
typename _Traits,
typename _Alloc>
777 const size_type __size = __str.
size();
780 const size_type __len = __size + this->size();
781 if (__len > this->capacity() || _M_rep()->_M_is_shared())
782 this->reserve(__len);
783 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
784 _M_rep()->_M_set_length_and_sharable(__len);
789 template<
typename _CharT,
typename _Traits,
typename _Alloc>
794 __str._M_check(__pos,
"basic_string::append");
795 __n = __str._M_limit(__pos, __n);
798 const size_type __len = __n + this->size();
799 if (__len > this->capacity() || _M_rep()->_M_is_shared())
800 this->reserve(__len);
801 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
802 _M_rep()->_M_set_length_and_sharable(__len);
807 template<
typename _CharT,
typename _Traits,
typename _Alloc>
810 insert(size_type __pos,
const _CharT* __s, size_type __n)
812 __glibcxx_requires_string_len(__s, __n);
813 _M_check(__pos,
"basic_string::insert");
814 _M_check_length(size_type(0), __n,
"basic_string::insert");
815 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
816 return _M_replace_safe(__pos, size_type(0), __s, __n);
820 const size_type __off = __s - _M_data();
821 _M_mutate(__pos, 0, __n);
822 __s = _M_data() + __off;
823 _CharT* __p = _M_data() + __pos;
824 if (__s + __n <= __p)
825 _M_copy(__p, __s, __n);
827 _M_copy(__p, __s + __n, __n);
830 const size_type __nleft = __p - __s;
831 _M_copy(__p, __s, __nleft);
832 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
838 template<
typename _CharT,
typename _Traits,
typename _Alloc>
839 typename basic_string<_CharT, _Traits, _Alloc>::iterator
841 erase(iterator __first, iterator __last)
843 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
844 && __last <= _M_iend());
849 const size_type __size = __last - __first;
852 const size_type __pos = __first - _M_ibegin();
853 _M_mutate(__pos, __size, size_type(0));
854 _M_rep()->_M_set_leaked();
855 return iterator(_M_data() + __pos);
861 template<
typename _CharT,
typename _Traits,
typename _Alloc>
864 replace(size_type __pos, size_type __n1,
const _CharT* __s,
867 __glibcxx_requires_string_len(__s, __n2);
868 _M_check(__pos,
"basic_string::replace");
869 __n1 = _M_limit(__pos, __n1);
870 _M_check_length(__n1, __n2,
"basic_string::replace");
872 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
873 return _M_replace_safe(__pos, __n1, __s, __n2);
874 else if ((__left = __s + __n2 <= _M_data() + __pos)
875 || _M_data() + __pos + __n1 <= __s)
878 size_type __off = __s - _M_data();
879 __left ? __off : (__off += __n2 - __n1);
880 _M_mutate(__pos, __n1, __n2);
881 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
888 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
892 template<
typename _CharT,
typename _Traits,
typename _Alloc>
897 const size_type __size =
sizeof(_Rep_base) +
898 (this->_M_capacity + 1) *
sizeof(_CharT);
899 _Raw_bytes_alloc(__a).deallocate(
reinterpret_cast<char*
>(
this), __size);
902 template<
typename _CharT,
typename _Traits,
typename _Alloc>
904 basic_string<_CharT, _Traits, _Alloc>::
907#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
908 if (_M_rep() == &_S_empty_rep())
911 if (_M_rep()->_M_is_shared())
913 _M_rep()->_M_set_leaked();
916 template<
typename _CharT,
typename _Traits,
typename _Alloc>
918 basic_string<_CharT, _Traits, _Alloc>::
919 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
921 const size_type __old_size = this->size();
922 const size_type __new_size = __old_size + __len2 - __len1;
923 const size_type __how_much = __old_size - __pos - __len1;
925 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
928 const allocator_type __a = get_allocator();
929 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
932 _M_copy(__r->_M_refdata(), _M_data(), __pos);
934 _M_copy(__r->_M_refdata() + __pos + __len2,
935 _M_data() + __pos + __len1, __how_much);
937 _M_rep()->_M_dispose(__a);
938 _M_data(__r->_M_refdata());
940 else if (__how_much && __len1 != __len2)
943 _M_move(_M_data() + __pos + __len2,
944 _M_data() + __pos + __len1, __how_much);
946 _M_rep()->_M_set_length_and_sharable(__new_size);
949 template<
typename _CharT,
typename _Traits,
typename _Alloc>
954 if (__res != this->capacity() || _M_rep()->_M_is_shared())
957 if (__res < this->size())
958 __res = this->size();
959 const allocator_type __a = get_allocator();
960 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
961 _M_rep()->_M_dispose(__a);
966 template<
typename _CharT,
typename _Traits,
typename _Alloc>
972 if (_M_rep()->_M_is_leaked())
973 _M_rep()->_M_set_sharable();
974 if (__s._M_rep()->_M_is_leaked())
975 __s._M_rep()->_M_set_sharable();
976 if (this->get_allocator() == __s.get_allocator())
978 _CharT* __tmp = _M_data();
979 _M_data(__s._M_data());
986 __s.get_allocator());
987 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
988 this->get_allocator());
994 template<
typename _CharT,
typename _Traits,
typename _Alloc>
997 _S_create(size_type __capacity, size_type __old_capacity,
998 const _Alloc& __alloc)
1002 if (__capacity > _S_max_size)
1003 __throw_length_error(__N(
"basic_string::_S_create"));
1028 const size_type __pagesize = 4096;
1029 const size_type __malloc_header_size = 4 *
sizeof(
void*);
1037 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1038 __capacity = 2 * __old_capacity;
1043 size_type __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1045 const size_type __adj_size = __size + __malloc_header_size;
1046 if (__adj_size > __pagesize && __capacity > __old_capacity)
1048 const size_type __extra = __pagesize - __adj_size % __pagesize;
1049 __capacity += __extra /
sizeof(_CharT);
1051 if (__capacity > _S_max_size)
1052 __capacity = _S_max_size;
1053 __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1058 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1059 _Rep *__p =
new (__place) _Rep;
1060 __p->_M_capacity = __capacity;
1068 __p->_M_set_sharable();
1072 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1074 basic_string<_CharT, _Traits, _Alloc>::_Rep::
1075 _M_clone(
const _Alloc& __alloc, size_type __res)
1078 const size_type __requested_cap = this->_M_length + __res;
1079 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1081 if (this->_M_length)
1082 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1084 __r->_M_set_length_and_sharable(this->_M_length);
1085 return __r->_M_refdata();
1088 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1091 resize(size_type __n, _CharT __c)
1093 const size_type __size = this->size();
1094 _M_check_length(__size, __n,
"basic_string::resize");
1096 this->append(__n - __size, __c);
1097 else if (__n < __size)
1102 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1103 template<
typename _InputIterator>
1107 _InputIterator __k2, __false_type)
1110 const size_type __n1 = __i2 - __i1;
1111 _M_check_length(__n1, __s.size(),
"basic_string::_M_replace_dispatch");
1112 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1116 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1117 basic_string<_CharT, _Traits, _Alloc>&
1118 basic_string<_CharT, _Traits, _Alloc>::
1119 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1122 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
1123 _M_mutate(__pos1, __n1, __n2);
1125 _M_assign(_M_data() + __pos1, __n2, __c);
1129 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1130 basic_string<_CharT, _Traits, _Alloc>&
1131 basic_string<_CharT, _Traits, _Alloc>::
1132 _M_replace_safe(size_type __pos1, size_type __n1,
const _CharT* __s,
1135 _M_mutate(__pos1, __n1, __n2);
1137 _M_copy(_M_data() + __pos1, __s, __n2);
1141 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1142 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1144 copy(_CharT* __s, size_type __n, size_type __pos)
const
1146 _M_check(__pos,
"basic_string::copy");
1147 __n = _M_limit(__pos, __n);
1148 __glibcxx_requires_string_len(__s, __n);
1150 _M_copy(__s, _M_data() + __pos, __n);
1156 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1161 __glibcxx_requires_string(__lhs);
1163 typedef typename __string_type::size_type __size_type;
1164 const __size_type __len = _Traits::length(__lhs);
1165 __string_type __str;
1166 __str.reserve(__len + __rhs.
size());
1167 __str.append(__lhs, __len);
1168 __str.append(__rhs);
1172 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1173 basic_string<_CharT, _Traits, _Alloc>
1177 typedef typename __string_type::size_type __size_type;
1178 __string_type __str;
1179 const __size_type __len = __rhs.
size();
1180 __str.reserve(__len + 1);
1181 __str.append(__size_type(1), __lhs);
1182 __str.append(__rhs);
1186 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1187 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1189 find(
const _CharT* __s, size_type __pos, size_type __n)
const
1192 __glibcxx_requires_string_len(__s, __n);
1193 const size_type __size = this->size();
1196 return __pos <= __size ? __pos : npos;
1197 if (__pos >= __size)
1200 const _CharT __elem0 = __s[0];
1201 const _CharT*
const __data = data();
1202 const _CharT* __first = __data + __pos;
1203 const _CharT*
const __last = __data + __size;
1204 size_type __len = __size - __pos;
1206 while (__len >= __n)
1209 __first = traits_type::find(__first, __len - __n + 1, __elem0);
1215 if (traits_type::compare(__first, __s, __n) == 0)
1216 return __first - __data;
1217 __len = __last - ++__first;
1222 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1223 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1225 find(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1227 size_type __ret = npos;
1228 const size_type __size = this->size();
1231 const _CharT* __data = _M_data();
1232 const size_type __n = __size - __pos;
1233 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1235 __ret = __p - __data;
1240 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1241 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1243 rfind(
const _CharT* __s, size_type __pos, size_type __n)
const
1246 __glibcxx_requires_string_len(__s, __n);
1247 const size_type __size = this->size();
1250 __pos =
std::min(size_type(__size - __n), __pos);
1251 const _CharT* __data = _M_data();
1254 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1257 while (__pos-- > 0);
1262 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1263 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1265 rfind(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1267 size_type __size = this->size();
1270 if (--__size > __pos)
1272 for (++__size; __size-- > 0; )
1273 if (traits_type::eq(_M_data()[__size], __c))
1279 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1280 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1282 find_first_of(
const _CharT* __s, size_type __pos, size_type __n)
const
1285 __glibcxx_requires_string_len(__s, __n);
1286 for (; __n && __pos < this->size(); ++__pos)
1288 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1295 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1296 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1298 find_last_of(
const _CharT* __s, size_type __pos, size_type __n)
const
1301 __glibcxx_requires_string_len(__s, __n);
1302 size_type __size = this->size();
1305 if (--__size > __pos)
1309 if (traits_type::find(__s, __n, _M_data()[__size]))
1312 while (__size-- != 0);
1317 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1318 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1323 __glibcxx_requires_string_len(__s, __n);
1324 for (; __pos < this->size(); ++__pos)
1325 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1330 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1331 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1335 for (; __pos < this->size(); ++__pos)
1336 if (!traits_type::eq(_M_data()[__pos], __c))
1341 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1342 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1347 __glibcxx_requires_string_len(__s, __n);
1348 size_type __size = this->size();
1351 if (--__size > __pos)
1355 if (!traits_type::find(__s, __n, _M_data()[__size]))
1363 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1364 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1368 size_type __size = this->size();
1371 if (--__size > __pos)
1375 if (!traits_type::eq(_M_data()[__size], __c))
1383 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1388 _M_check(__pos,
"basic_string::compare");
1389 __n = _M_limit(__pos, __n);
1390 const size_type __osize = __str.
size();
1391 const size_type __len =
std::min(__n, __osize);
1392 int __r = traits_type::compare(_M_data() + __pos, __str.
data(), __len);
1394 __r = _S_compare(__n, __osize);
1398 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1402 size_type __pos2, size_type __n2)
const
1404 _M_check(__pos1,
"basic_string::compare");
1405 __str._M_check(__pos2,
"basic_string::compare");
1406 __n1 = _M_limit(__pos1, __n1);
1407 __n2 = __str._M_limit(__pos2, __n2);
1408 const size_type __len =
std::min(__n1, __n2);
1409 int __r = traits_type::compare(_M_data() + __pos1,
1410 __str.
data() + __pos2, __len);
1412 __r = _S_compare(__n1, __n2);
1416 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1419 compare(
const _CharT* __s)
const _GLIBCXX_NOEXCEPT
1421 __glibcxx_requires_string(__s);
1422 const size_type __size = this->size();
1423 const size_type __osize = traits_type::length(__s);
1424 const size_type __len =
std::min(__size, __osize);
1425 int __r = traits_type::compare(_M_data(), __s, __len);
1427 __r = _S_compare(__size, __osize);
1431 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1434 compare(size_type __pos, size_type __n1,
const _CharT* __s)
const
1436 __glibcxx_requires_string(__s);
1437 _M_check(__pos,
"basic_string::compare");
1438 __n1 = _M_limit(__pos, __n1);
1439 const size_type __osize = traits_type::length(__s);
1440 const size_type __len =
std::min(__n1, __osize);
1441 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1443 __r = _S_compare(__n1, __osize);
1447 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1450 compare(size_type __pos, size_type __n1,
const _CharT* __s,
1451 size_type __n2)
const
1453 __glibcxx_requires_string_len(__s, __n2);
1454 _M_check(__pos,
"basic_string::compare");
1455 __n1 = _M_limit(__pos, __n1);
1456 const size_type __len =
std::min(__n1, __n2);
1457 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1459 __r = _S_compare(__n1, __n2);
1464 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1471 typedef typename __istream_type::ios_base __ios_base;
1472 typedef typename __istream_type::int_type __int_type;
1473 typedef typename __string_type::size_type __size_type;
1475 typedef typename __ctype_type::ctype_base __ctype_base;
1477 __size_type __extracted = 0;
1478 typename __ios_base::iostate __err = __ios_base::goodbit;
1479 typename __istream_type::sentry __cerb(__in,
false);
1487 __size_type __len = 0;
1489 const __size_type __n = __w > 0 ?
static_cast<__size_type
>(__w)
1491 const __ctype_type& __ct = use_facet<__ctype_type>(__in.
getloc());
1492 const __int_type __eof = _Traits::eof();
1493 __int_type __c = __in.
rdbuf()->sgetc();
1495 while (__extracted < __n
1496 && !_Traits::eq_int_type(__c, __eof)
1497 && !__ct.is(__ctype_base::space,
1498 _Traits::to_char_type(__c)))
1500 if (__len ==
sizeof(__buf) /
sizeof(_CharT))
1502 __str.
append(__buf,
sizeof(__buf) /
sizeof(_CharT));
1505 __buf[__len++] = _Traits::to_char_type(__c);
1507 __c = __in.
rdbuf()->snextc();
1509 __str.
append(__buf, __len);
1511 if (_Traits::eq_int_type(__c, __eof))
1512 __err |= __ios_base::eofbit;
1517 __in._M_setstate(__ios_base::badbit);
1518 __throw_exception_again;
1525 __in._M_setstate(__ios_base::badbit);
1530 __err |= __ios_base::failbit;
1536 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1537 basic_istream<_CharT, _Traits>&
1543 typedef typename __istream_type::ios_base __ios_base;
1544 typedef typename __istream_type::int_type __int_type;
1545 typedef typename __string_type::size_type __size_type;
1547 __size_type __extracted = 0;
1548 const __size_type __n = __str.
max_size();
1549 typename __ios_base::iostate __err = __ios_base::goodbit;
1550 typename __istream_type::sentry __cerb(__in,
true);
1556 const __int_type __idelim = _Traits::to_int_type(__delim);
1557 const __int_type __eof = _Traits::eof();
1558 __int_type __c = __in.
rdbuf()->sgetc();
1560 while (__extracted < __n
1561 && !_Traits::eq_int_type(__c, __eof)
1562 && !_Traits::eq_int_type(__c, __idelim))
1564 __str += _Traits::to_char_type(__c);
1566 __c = __in.
rdbuf()->snextc();
1569 if (_Traits::eq_int_type(__c, __eof))
1570 __err |= __ios_base::eofbit;
1571 else if (_Traits::eq_int_type(__c, __idelim))
1574 __in.
rdbuf()->sbumpc();
1577 __err |= __ios_base::failbit;
1581 __in._M_setstate(__ios_base::badbit);
1582 __throw_exception_again;
1589 __in._M_setstate(__ios_base::badbit);
1593 __err |= __ios_base::failbit;
1601#if _GLIBCXX_EXTERN_TEMPLATE
1607# if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1608 extern template class basic_string<char>;
1609# elif ! _GLIBCXX_USE_CXX11_ABI
1612 extern template basic_string<char>::size_type
1613 basic_string<char>::_Rep::_S_empty_rep_storage[];
1617 basic_istream<char>&
1620 basic_ostream<char>&
1621 operator<<(basic_ostream<char>&,
const string&);
1623 basic_istream<char>&
1624 getline(basic_istream<char>&,
string&,
char);
1626 basic_istream<char>&
1627 getline(basic_istream<char>&,
string&);
1629#ifdef _GLIBCXX_USE_WCHAR_T
1630# if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1631 extern template class basic_string<wchar_t>;
1632# elif ! _GLIBCXX_USE_CXX11_ABI
1633 extern template basic_string<wchar_t>::size_type
1634 basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1638 basic_istream<wchar_t>&
1641 basic_ostream<wchar_t>&
1644 basic_istream<wchar_t>&
1647 basic_istream<wchar_t>&
1652_GLIBCXX_END_NAMESPACE_VERSION
_GLIBCXX20_CONSTEXPR complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
basic_string< wchar_t > wstring
A string of wchar_t.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
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.
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
void setstate(iostate __state)
Sets additional flags in the error state.
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Template class basic_istream.
Uniform interface to all allocator types.
Managing sequences of characters and character-like objects.
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
const _CharT * data() const noexcept
Return const pointer to contents.
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
int compare(const basic_string &__str) const
Compare to a string.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
basic_string & append(const basic_string &__str)
Append a string to this string.
static const size_type npos
Value returned by various member functions when they fail.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
basic_string() noexcept
Default constructor creates an empty string.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Thrown as part of forced unwinding.
streamsize width() const
Flags access.
locale getloc() const
Locale access.
Primary class template ctype facet.
Forward iterators support a superset of input iterator operations.