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
67 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
68 __s._M_get_allocator());
71 if (__s._M_is_local())
73 if (length() && __s.length())
75 _CharT __tmp_data[_S_local_capacity + 1];
76 traits_type::copy(__tmp_data, __s._M_local_buf,
77 _S_local_capacity + 1);
78 traits_type::copy(__s._M_local_buf, _M_local_buf,
79 _S_local_capacity + 1);
80 traits_type::copy(_M_local_buf, __tmp_data,
81 _S_local_capacity + 1);
83 else if (__s.length())
85 traits_type::copy(_M_local_buf, __s._M_local_buf,
86 _S_local_capacity + 1);
87 _M_length(__s.length());
93 traits_type::copy(__s._M_local_buf, _M_local_buf,
94 _S_local_capacity + 1);
95 __s._M_length(length());
102 const size_type __tmp_capacity = __s._M_allocated_capacity;
103 traits_type::copy(__s._M_local_buf, _M_local_buf,
104 _S_local_capacity + 1);
105 _M_data(__s._M_data());
106 __s._M_data(__s._M_local_buf);
107 _M_capacity(__tmp_capacity);
111 const size_type __tmp_capacity = _M_allocated_capacity;
112 if (__s._M_is_local())
114 traits_type::copy(_M_local_buf, __s._M_local_buf,
115 _S_local_capacity + 1);
116 __s._M_data(_M_data());
117 _M_data(_M_local_buf);
121 pointer __tmp_ptr = _M_data();
122 _M_data(__s._M_data());
123 __s._M_data(__tmp_ptr);
124 _M_capacity(__s._M_allocated_capacity);
126 __s._M_capacity(__tmp_capacity);
129 const size_type __tmp_length = length();
130 _M_length(__s.length());
131 __s._M_length(__tmp_length);
134 template<
typename _CharT,
typename _Traits,
typename _Alloc>
135 typename basic_string<_CharT, _Traits, _Alloc>::pointer
136 basic_string<_CharT, _Traits, _Alloc>::
137 _M_create(size_type& __capacity, size_type __old_capacity)
141 if (__capacity > max_size())
142 std::__throw_length_error(__N(
"basic_string::_M_create"));
147 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
149 __capacity = 2 * __old_capacity;
151 if (__capacity > max_size())
152 __capacity = max_size();
157 return _Alloc_traits::allocate(_M_get_allocator(), __capacity + 1);
164 template<
typename _CharT,
typename _Traits,
typename _Alloc>
165 template<
typename _InIterator>
167 basic_string<_CharT, _Traits, _Alloc>::
168 _M_construct(_InIterator __beg, _InIterator __end,
172 size_type __capacity = size_type(_S_local_capacity);
174 while (__beg != __end && __len < __capacity)
176 _M_data()[__len++] = *__beg;
182 while (__beg != __end)
184 if (__len == __capacity)
187 __capacity = __len + 1;
188 pointer __another = _M_create(__capacity, __len);
189 this->_S_copy(__another, _M_data(), __len);
192 _M_capacity(__capacity);
194 _M_data()[__len++] = *__beg;
201 __throw_exception_again;
204 _M_set_length(__len);
207 template<
typename _CharT,
typename _Traits,
typename _Alloc>
208 template<
typename _InIterator>
210 basic_string<_CharT, _Traits, _Alloc>::
211 _M_construct(_InIterator __beg, _InIterator __end,
215 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
216 std::__throw_logic_error(__N(
"basic_string::" 217 "_M_construct null not valid"));
219 size_type __dnew =
static_cast<size_type
>(
std::distance(__beg, __end));
221 if (__dnew > size_type(_S_local_capacity))
223 _M_data(_M_create(__dnew, size_type(0)));
229 { this->_S_copy_chars(_M_data(), __beg, __end); }
233 __throw_exception_again;
236 _M_set_length(__dnew);
239 template<
typename _CharT,
typename _Traits,
typename _Alloc>
241 basic_string<_CharT, _Traits, _Alloc>::
242 _M_construct(size_type __n, _CharT __c)
244 if (__n > size_type(_S_local_capacity))
246 _M_data(_M_create(__n, size_type(0)));
251 this->_S_assign(_M_data(), __n, __c);
256 template<
typename _CharT,
typename _Traits,
typename _Alloc>
258 basic_string<_CharT, _Traits, _Alloc>::
259 _M_assign(
const basic_string& __str)
263 const size_type __rsize = __str.length();
264 const size_type __capacity = capacity();
266 if (__rsize > __capacity)
268 size_type __new_capacity = __rsize;
269 pointer __tmp = _M_create(__new_capacity, __capacity);
272 _M_capacity(__new_capacity);
276 this->_S_copy(_M_data(), __str._M_data(), __rsize);
278 _M_set_length(__rsize);
282 template<
typename _CharT,
typename _Traits,
typename _Alloc>
288 if (__res < length())
291 const size_type __capacity = capacity();
292 if (__res != __capacity)
294 if (__res > __capacity
295 || __res > size_type(_S_local_capacity))
297 pointer __tmp = _M_create(__res, __capacity);
298 this->_S_copy(__tmp, _M_data(), length() + 1);
303 else if (!_M_is_local())
305 this->_S_copy(_M_local_data(), _M_data(), length() + 1);
306 _M_destroy(__capacity);
307 _M_data(_M_local_data());
312 template<
typename _CharT,
typename _Traits,
typename _Alloc>
314 basic_string<_CharT, _Traits, _Alloc>::
315 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
318 const size_type __how_much = length() - __pos - __len1;
320 size_type __new_capacity = length() + __len2 - __len1;
321 pointer __r = _M_create(__new_capacity, capacity());
324 this->_S_copy(__r, _M_data(), __pos);
326 this->_S_copy(__r + __pos, __s, __len2);
328 this->_S_copy(__r + __pos + __len2,
329 _M_data() + __pos + __len1, __how_much);
333 _M_capacity(__new_capacity);
336 template<
typename _CharT,
typename _Traits,
typename _Alloc>
338 basic_string<_CharT, _Traits, _Alloc>::
339 _M_erase(size_type __pos, size_type __n)
341 const size_type __how_much = length() - __pos - __n;
343 if (__how_much && __n)
344 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
346 _M_set_length(length() - __n);
349 template<
typename _CharT,
typename _Traits,
typename _Alloc>
352 resize(size_type __n, _CharT __c)
354 const size_type __size = this->size();
356 this->append(__n - __size, __c);
357 else if (__n < __size)
358 this->_M_erase(__n, __size - __n);
361 template<
typename _CharT,
typename _Traits,
typename _Alloc>
362 basic_string<_CharT, _Traits, _Alloc>&
363 basic_string<_CharT, _Traits, _Alloc>::
364 _M_append(
const _CharT* __s, size_type __n)
366 const size_type __len = __n + this->size();
368 if (__len <= this->capacity())
371 this->_S_copy(this->_M_data() + this->size(), __s, __n);
374 this->_M_mutate(this->size(), size_type(0), __s, __n);
376 this->_M_set_length(__len);
380 template<
typename _CharT,
typename _Traits,
typename _Alloc>
381 template<
typename _InputIterator>
382 basic_string<_CharT, _Traits, _Alloc>&
383 basic_string<_CharT, _Traits, _Alloc>::
384 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
385 _InputIterator __k1, _InputIterator __k2,
388 const basic_string __s(__k1, __k2);
389 const size_type __n1 = __i2 - __i1;
390 return _M_replace(__i1 -
begin(), __n1, __s._M_data(),
394 template<
typename _CharT,
typename _Traits,
typename _Alloc>
395 basic_string<_CharT, _Traits, _Alloc>&
396 basic_string<_CharT, _Traits, _Alloc>::
397 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
400 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
402 const size_type __old_size = this->size();
403 const size_type __new_size = __old_size + __n2 - __n1;
405 if (__new_size <= this->capacity())
407 _CharT* __p = this->_M_data() + __pos1;
409 const size_type __how_much = __old_size - __pos1 - __n1;
410 if (__how_much && __n1 != __n2)
411 this->_S_move(__p + __n2, __p + __n1, __how_much);
414 this->_M_mutate(__pos1, __n1, 0, __n2);
417 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
419 this->_M_set_length(__new_size);
423 template<
typename _CharT,
typename _Traits,
typename _Alloc>
424 basic_string<_CharT, _Traits, _Alloc>&
425 basic_string<_CharT, _Traits, _Alloc>::
426 _M_replace(size_type __pos, size_type __len1,
const _CharT* __s,
427 const size_type __len2)
429 _M_check_length(__len1, __len2,
"basic_string::_M_replace");
431 const size_type __old_size = this->size();
432 const size_type __new_size = __old_size + __len2 - __len1;
434 if (__new_size <= this->capacity())
436 _CharT* __p = this->_M_data() + __pos;
438 const size_type __how_much = __old_size - __pos - __len1;
439 if (_M_disjunct(__s))
441 if (__how_much && __len1 != __len2)
442 this->_S_move(__p + __len2, __p + __len1, __how_much);
444 this->_S_copy(__p, __s, __len2);
449 if (__len2 && __len2 <= __len1)
450 this->_S_move(__p, __s, __len2);
451 if (__how_much && __len1 != __len2)
452 this->_S_move(__p + __len2, __p + __len1, __how_much);
455 if (__s + __len2 <= __p + __len1)
456 this->_S_move(__p, __s, __len2);
457 else if (__s >= __p + __len1)
458 this->_S_copy(__p, __s + __len2 - __len1, __len2);
461 const size_type __nleft = (__p + __len1) - __s;
462 this->_S_move(__p, __s, __nleft);
463 this->_S_copy(__p + __nleft, __p + __len2,
470 this->_M_mutate(__pos, __len1, __s, __len2);
472 this->_M_set_length(__new_size);
476 template<
typename _CharT,
typename _Traits,
typename _Alloc>
477 typename basic_string<_CharT, _Traits, _Alloc>::size_type
479 copy(_CharT* __s, size_type __n, size_type __pos)
const 481 _M_check(__pos,
"basic_string::copy");
482 __n = _M_limit(__pos, __n);
483 __glibcxx_requires_string_len(__s, __n);
485 _S_copy(__s, _M_data() + __pos, __n);
490 #else // !_GLIBCXX_USE_CXX11_ABI 492 template<
typename _CharT,
typename _Traits,
typename _Alloc>
493 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
494 basic_string<_CharT, _Traits, _Alloc>::
495 _Rep::_S_max_size = (((npos -
sizeof(_Rep_base))/
sizeof(_CharT)) - 1) / 4;
497 template<
typename _CharT,
typename _Traits,
typename _Alloc>
499 basic_string<_CharT, _Traits, _Alloc>::
500 _Rep::_S_terminal = _CharT();
502 template<
typename _CharT,
typename _Traits,
typename _Alloc>
503 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
504 basic_string<_CharT, _Traits, _Alloc>::npos;
508 template<
typename _CharT,
typename _Traits,
typename _Alloc>
509 typename basic_string<_CharT, _Traits, _Alloc>::size_type
510 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
511 (
sizeof(_Rep_base) +
sizeof(_CharT) +
sizeof(size_type) - 1) /
518 template<
typename _CharT,
typename _Traits,
typename _Alloc>
519 template<
typename _InIterator>
521 basic_string<_CharT, _Traits, _Alloc>::
522 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
525 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 526 if (__beg == __end && __a == _Alloc())
527 return _S_empty_rep()._M_refdata();
532 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
534 __buf[__len++] = *__beg;
537 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
538 _M_copy(__r->_M_refdata(), __buf, __len);
541 while (__beg != __end)
543 if (__len == __r->_M_capacity)
546 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
547 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
548 __r->_M_destroy(__a);
551 __r->_M_refdata()[__len++] = *__beg;
557 __r->_M_destroy(__a);
558 __throw_exception_again;
560 __r->_M_set_length_and_sharable(__len);
561 return __r->_M_refdata();
564 template<
typename _CharT,
typename _Traits,
typename _Alloc>
565 template <
typename _InIterator>
567 basic_string<_CharT, _Traits, _Alloc>::
568 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
569 forward_iterator_tag)
571 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 572 if (__beg == __end && __a == _Alloc())
573 return _S_empty_rep()._M_refdata();
576 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
577 __throw_logic_error(__N(
"basic_string::_S_construct null not valid"));
579 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
582 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
584 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
587 __r->_M_destroy(__a);
588 __throw_exception_again;
590 __r->_M_set_length_and_sharable(__dnew);
591 return __r->_M_refdata();
594 template<
typename _CharT,
typename _Traits,
typename _Alloc>
596 basic_string<_CharT, _Traits, _Alloc>::
597 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
599 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 600 if (__n == 0 && __a == _Alloc())
601 return _S_empty_rep()._M_refdata();
604 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
606 _M_assign(__r->_M_refdata(), __n, __c);
608 __r->_M_set_length_and_sharable(__n);
609 return __r->_M_refdata();
612 template<
typename _CharT,
typename _Traits,
typename _Alloc>
615 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
616 __str.get_allocator()),
617 __str.get_allocator())
620 template<
typename _CharT,
typename _Traits,
typename _Alloc>
623 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
626 template<
typename _CharT,
typename _Traits,
typename _Alloc>
629 : _M_dataplus(_S_construct(__str._M_data()
630 + __str._M_check(__pos,
631 "basic_string::basic_string"),
632 __str._M_data() + __str._M_limit(__pos, __n)
633 + __pos, _Alloc()), _Alloc())
636 template<
typename _CharT,
typename _Traits,
typename _Alloc>
639 size_type __n,
const _Alloc& __a)
640 : _M_dataplus(_S_construct(__str._M_data()
641 + __str._M_check(__pos,
642 "basic_string::basic_string"),
643 __str._M_data() + __str._M_limit(__pos, __n)
648 template<
typename _CharT,
typename _Traits,
typename _Alloc>
651 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
655 template<
typename _CharT,
typename _Traits,
typename _Alloc>
658 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::
length(__s) :
659 __s +
npos, __a), __a)
662 template<
typename _CharT,
typename _Traits,
typename _Alloc>
665 : _M_dataplus(_S_construct(__n, __c, __a), __a)
669 template<
typename _CharT,
typename _Traits,
typename _Alloc>
670 template<
typename _InputIterator>
672 basic_string(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a)
673 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
676 #if __cplusplus >= 201103L 677 template<
typename _CharT,
typename _Traits,
typename _Alloc>
680 : _M_dataplus(_S_construct(__l.
begin(), __l.
end(), __a), __a)
684 template<
typename _CharT,
typename _Traits,
typename _Alloc>
689 if (_M_rep() != __str._M_rep())
693 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.
get_allocator());
694 _M_rep()->_M_dispose(__a);
700 template<
typename _CharT,
typename _Traits,
typename _Alloc>
705 __glibcxx_requires_string_len(__s, __n);
706 _M_check_length(this->
size(), __n,
"basic_string::assign");
707 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
708 return _M_replace_safe(size_type(0), this->
size(), __s, __n);
712 const size_type __pos = __s - _M_data();
714 _M_copy(_M_data(), __s, __n);
716 _M_move(_M_data(), __s, __n);
717 _M_rep()->_M_set_length_and_sharable(__n);
722 template<
typename _CharT,
typename _Traits,
typename _Alloc>
729 _M_check_length(size_type(0), __n,
"basic_string::append");
730 const size_type __len = __n + this->
size();
731 if (__len > this->
capacity() || _M_rep()->_M_is_shared())
733 _M_assign(_M_data() + this->
size(), __n, __c);
734 _M_rep()->_M_set_length_and_sharable(__len);
739 template<
typename _CharT,
typename _Traits,
typename _Alloc>
744 __glibcxx_requires_string_len(__s, __n);
747 _M_check_length(size_type(0), __n,
"basic_string::append");
748 const size_type __len = __n + this->
size();
749 if (__len > this->
capacity() || _M_rep()->_M_is_shared())
751 if (_M_disjunct(__s))
755 const size_type __off = __s - _M_data();
757 __s = _M_data() + __off;
760 _M_copy(_M_data() + this->
size(), __s, __n);
761 _M_rep()->_M_set_length_and_sharable(__len);
766 template<
typename _CharT,
typename _Traits,
typename _Alloc>
771 const size_type __size = __str.
size();
774 const size_type __len = __size + this->
size();
775 if (__len > this->
capacity() || _M_rep()->_M_is_shared())
777 _M_copy(_M_data() + this->
size(), __str._M_data(), __size);
778 _M_rep()->_M_set_length_and_sharable(__len);
783 template<
typename _CharT,
typename _Traits,
typename _Alloc>
788 __str._M_check(__pos,
"basic_string::append");
789 __n = __str._M_limit(__pos, __n);
792 const size_type __len = __n + this->
size();
793 if (__len > this->
capacity() || _M_rep()->_M_is_shared())
795 _M_copy(_M_data() + this->
size(), __str._M_data() + __pos, __n);
796 _M_rep()->_M_set_length_and_sharable(__len);
801 template<
typename _CharT,
typename _Traits,
typename _Alloc>
804 insert(size_type __pos,
const _CharT* __s, size_type __n)
806 __glibcxx_requires_string_len(__s, __n);
807 _M_check(__pos,
"basic_string::insert");
808 _M_check_length(size_type(0), __n,
"basic_string::insert");
809 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
810 return _M_replace_safe(__pos, size_type(0), __s, __n);
814 const size_type __off = __s - _M_data();
815 _M_mutate(__pos, 0, __n);
816 __s = _M_data() + __off;
817 _CharT* __p = _M_data() + __pos;
818 if (__s + __n <= __p)
819 _M_copy(__p, __s, __n);
821 _M_copy(__p, __s + __n, __n);
824 const size_type __nleft = __p - __s;
825 _M_copy(__p, __s, __nleft);
826 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
832 template<
typename _CharT,
typename _Traits,
typename _Alloc>
833 typename basic_string<_CharT, _Traits, _Alloc>::iterator
835 erase(iterator __first, iterator __last)
837 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
838 && __last <= _M_iend());
843 const size_type __size = __last - __first;
846 const size_type __pos = __first - _M_ibegin();
847 _M_mutate(__pos, __size, size_type(0));
848 _M_rep()->_M_set_leaked();
849 return iterator(_M_data() + __pos);
855 template<
typename _CharT,
typename _Traits,
typename _Alloc>
858 replace(size_type __pos, size_type __n1,
const _CharT* __s,
861 __glibcxx_requires_string_len(__s, __n2);
862 _M_check(__pos,
"basic_string::replace");
863 __n1 = _M_limit(__pos, __n1);
864 _M_check_length(__n1, __n2,
"basic_string::replace");
866 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
867 return _M_replace_safe(__pos, __n1, __s, __n2);
868 else if ((__left = __s + __n2 <= _M_data() + __pos)
869 || _M_data() + __pos + __n1 <= __s)
872 size_type __off = __s - _M_data();
873 __left ? __off : (__off += __n2 - __n1);
874 _M_mutate(__pos, __n1, __n2);
875 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
882 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
886 template<
typename _CharT,
typename _Traits,
typename _Alloc>
891 const size_type __size =
sizeof(_Rep_base) +
892 (this->_M_capacity + 1) *
sizeof(_CharT);
893 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(
this), __size);
896 template<
typename _CharT,
typename _Traits,
typename _Alloc>
901 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 902 if (_M_rep() == &_S_empty_rep())
905 if (_M_rep()->_M_is_shared())
907 _M_rep()->_M_set_leaked();
910 template<
typename _CharT,
typename _Traits,
typename _Alloc>
913 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
915 const size_type __old_size = this->
size();
916 const size_type __new_size = __old_size + __len2 - __len1;
917 const size_type __how_much = __old_size - __pos - __len1;
919 if (__new_size > this->
capacity() || _M_rep()->_M_is_shared())
923 _Rep* __r = _Rep::_S_create(__new_size, this->
capacity(), __a);
926 _M_copy(__r->_M_refdata(), _M_data(), __pos);
928 _M_copy(__r->_M_refdata() + __pos + __len2,
929 _M_data() + __pos + __len1, __how_much);
931 _M_rep()->_M_dispose(__a);
932 _M_data(__r->_M_refdata());
934 else if (__how_much && __len1 != __len2)
937 _M_move(_M_data() + __pos + __len2,
938 _M_data() + __pos + __len1, __how_much);
940 _M_rep()->_M_set_length_and_sharable(__new_size);
943 template<
typename _CharT,
typename _Traits,
typename _Alloc>
948 if (__res != this->
capacity() || _M_rep()->_M_is_shared())
951 if (__res < this->
size())
952 __res = this->
size();
954 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->
size());
955 _M_rep()->_M_dispose(__a);
960 template<
typename _CharT,
typename _Traits,
typename _Alloc>
965 if (_M_rep()->_M_is_leaked())
966 _M_rep()->_M_set_sharable();
967 if (__s._M_rep()->_M_is_leaked())
968 __s._M_rep()->_M_set_sharable();
971 _CharT* __tmp = _M_data();
972 _M_data(__s._M_data());
980 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
987 template<
typename _CharT,
typename _Traits,
typename _Alloc>
990 _S_create(size_type __capacity, size_type __old_capacity,
991 const _Alloc& __alloc)
995 if (__capacity > _S_max_size)
996 __throw_length_error(__N(
"basic_string::_S_create"));
1021 const size_type __pagesize = 4096;
1022 const size_type __malloc_header_size = 4 *
sizeof(
void*);
1030 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1031 __capacity = 2 * __old_capacity;
1036 size_type __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1038 const size_type __adj_size = __size + __malloc_header_size;
1039 if (__adj_size > __pagesize && __capacity > __old_capacity)
1041 const size_type __extra = __pagesize - __adj_size % __pagesize;
1042 __capacity += __extra /
sizeof(_CharT);
1044 if (__capacity > _S_max_size)
1045 __capacity = _S_max_size;
1046 __size = (__capacity + 1) *
sizeof(_CharT) +
sizeof(_Rep);
1051 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1052 _Rep *__p =
new (__place) _Rep;
1053 __p->_M_capacity = __capacity;
1061 __p->_M_set_sharable();
1065 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1068 _M_clone(
const _Alloc& __alloc, size_type __res)
1071 const size_type __requested_cap = this->_M_length + __res;
1072 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1074 if (this->_M_length)
1075 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1077 __r->_M_set_length_and_sharable(this->_M_length);
1078 return __r->_M_refdata();
1081 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1086 const size_type __size = this->
size();
1087 _M_check_length(__size, __n,
"basic_string::resize");
1089 this->
append(__n - __size, __c);
1090 else if (__n < __size)
1095 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1096 template<
typename _InputIterator>
1100 _InputIterator __k2, __false_type)
1103 const size_type __n1 = __i2 - __i1;
1104 _M_check_length(__n1, __s.
size(),
"basic_string::_M_replace_dispatch");
1105 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1109 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1115 _M_check_length(__n1, __n2,
"basic_string::_M_replace_aux");
1116 _M_mutate(__pos1, __n1, __n2);
1118 _M_assign(_M_data() + __pos1, __n2, __c);
1122 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1128 _M_mutate(__pos1, __n1, __n2);
1130 _M_copy(_M_data() + __pos1, __s, __n2);
1134 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1135 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1137 copy(_CharT* __s, size_type __n, size_type __pos)
const 1139 _M_check(__pos,
"basic_string::copy");
1140 __n = _M_limit(__pos, __n);
1141 __glibcxx_requires_string_len(__s, __n);
1143 _M_copy(__s, _M_data() + __pos, __n);
1147 #endif // !_GLIBCXX_USE_CXX11_ABI 1149 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1154 __glibcxx_requires_string(__lhs);
1156 typedef typename __string_type::size_type __size_type;
1157 const __size_type __len = _Traits::length(__lhs);
1158 __string_type __str;
1159 __str.reserve(__len + __rhs.
size());
1160 __str.append(__lhs, __len);
1161 __str.append(__rhs);
1165 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1170 typedef typename __string_type::size_type __size_type;
1171 __string_type __str;
1172 const __size_type __len = __rhs.
size();
1173 __str.reserve(__len + 1);
1174 __str.append(__size_type(1), __lhs);
1175 __str.append(__rhs);
1179 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1180 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1182 find(
const _CharT* __s, size_type __pos, size_type __n)
const 1184 __glibcxx_requires_string_len(__s, __n);
1185 const size_type __size = this->
size();
1186 const _CharT* __data = _M_data();
1189 return __pos <= __size ? __pos :
npos;
1193 for (; __pos <= __size - __n; ++__pos)
1194 if (traits_type::eq(__data[__pos], __s[0])
1195 && traits_type::compare(__data + __pos + 1,
1196 __s + 1, __n - 1) == 0)
1202 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1203 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1205 find(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1207 size_type __ret =
npos;
1208 const size_type __size = this->
size();
1211 const _CharT* __data = _M_data();
1212 const size_type __n = __size - __pos;
1213 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1215 __ret = __p - __data;
1220 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1221 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1223 rfind(
const _CharT* __s, size_type __pos, size_type __n)
const 1225 __glibcxx_requires_string_len(__s, __n);
1226 const size_type __size = this->
size();
1229 __pos =
std::min(size_type(__size - __n), __pos);
1230 const _CharT* __data = _M_data();
1233 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1236 while (__pos-- > 0);
1241 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1242 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1244 rfind(_CharT __c, size_type __pos)
const _GLIBCXX_NOEXCEPT
1246 size_type __size = this->
size();
1249 if (--__size > __pos)
1251 for (++__size; __size-- > 0; )
1252 if (traits_type::eq(_M_data()[__size], __c))
1258 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1259 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1263 __glibcxx_requires_string_len(__s, __n);
1264 for (; __n && __pos < this->
size(); ++__pos)
1266 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1273 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1274 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1278 __glibcxx_requires_string_len(__s, __n);
1279 size_type __size = this->
size();
1282 if (--__size > __pos)
1286 if (traits_type::find(__s, __n, _M_data()[__size]))
1289 while (__size-- != 0);
1294 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1295 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1299 __glibcxx_requires_string_len(__s, __n);
1300 for (; __pos < this->
size(); ++__pos)
1301 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1306 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1307 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1311 for (; __pos < this->
size(); ++__pos)
1312 if (!traits_type::eq(_M_data()[__pos], __c))
1317 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1318 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1322 __glibcxx_requires_string_len(__s, __n);
1323 size_type __size = this->
size();
1326 if (--__size > __pos)
1330 if (!traits_type::find(__s, __n, _M_data()[__size]))
1338 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1339 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1343 size_type __size = this->
size();
1346 if (--__size > __pos)
1350 if (!traits_type::eq(_M_data()[__size], __c))
1358 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1363 _M_check(__pos,
"basic_string::compare");
1364 __n = _M_limit(__pos, __n);
1365 const size_type __osize = __str.
size();
1366 const size_type __len =
std::min(__n, __osize);
1367 int __r = traits_type::compare(_M_data() + __pos, __str.
data(), __len);
1369 __r = _S_compare(__n, __osize);
1373 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1377 size_type __pos2, size_type __n2)
const 1379 _M_check(__pos1,
"basic_string::compare");
1380 __str._M_check(__pos2,
"basic_string::compare");
1381 __n1 = _M_limit(__pos1, __n1);
1382 __n2 = __str._M_limit(__pos2, __n2);
1383 const size_type __len =
std::min(__n1, __n2);
1384 int __r = traits_type::compare(_M_data() + __pos1,
1385 __str.
data() + __pos2, __len);
1387 __r = _S_compare(__n1, __n2);
1391 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1396 __glibcxx_requires_string(__s);
1397 const size_type __size = this->
size();
1398 const size_type __osize = traits_type::length(__s);
1399 const size_type __len =
std::min(__size, __osize);
1400 int __r = traits_type::compare(_M_data(), __s, __len);
1402 __r = _S_compare(__size, __osize);
1406 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1409 compare(size_type __pos, size_type __n1,
const _CharT* __s)
const 1411 __glibcxx_requires_string(__s);
1412 _M_check(__pos,
"basic_string::compare");
1413 __n1 = _M_limit(__pos, __n1);
1414 const size_type __osize = traits_type::length(__s);
1415 const size_type __len =
std::min(__n1, __osize);
1416 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1418 __r = _S_compare(__n1, __osize);
1422 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1425 compare(size_type __pos, size_type __n1,
const _CharT* __s,
1426 size_type __n2)
const 1428 __glibcxx_requires_string_len(__s, __n2);
1429 _M_check(__pos,
"basic_string::compare");
1430 __n1 = _M_limit(__pos, __n1);
1431 const size_type __len =
std::min(__n1, __n2);
1432 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1434 __r = _S_compare(__n1, __n2);
1439 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1446 typedef typename __istream_type::ios_base __ios_base;
1447 typedef typename __istream_type::int_type __int_type;
1448 typedef typename __string_type::size_type __size_type;
1450 typedef typename __ctype_type::ctype_base __ctype_base;
1452 __size_type __extracted = 0;
1453 typename __ios_base::iostate __err = __ios_base::goodbit;
1454 typename __istream_type::sentry __cerb(__in,
false);
1462 __size_type __len = 0;
1464 const __size_type __n = __w > 0 ?
static_cast<__size_type
>(__w)
1466 const __ctype_type& __ct = use_facet<__ctype_type>(__in.
getloc());
1467 const __int_type __eof = _Traits::eof();
1468 __int_type __c = __in.
rdbuf()->sgetc();
1470 while (__extracted < __n
1471 && !_Traits::eq_int_type(__c, __eof)
1472 && !__ct.is(__ctype_base::space,
1473 _Traits::to_char_type(__c)))
1475 if (__len ==
sizeof(__buf) /
sizeof(_CharT))
1477 __str.
append(__buf,
sizeof(__buf) /
sizeof(_CharT));
1480 __buf[__len++] = _Traits::to_char_type(__c);
1482 __c = __in.
rdbuf()->snextc();
1484 __str.
append(__buf, __len);
1486 if (_Traits::eq_int_type(__c, __eof))
1487 __err |= __ios_base::eofbit;
1492 __in._M_setstate(__ios_base::badbit);
1493 __throw_exception_again;
1500 __in._M_setstate(__ios_base::badbit);
1505 __err |= __ios_base::failbit;
1511 template<
typename _CharT,
typename _Traits,
typename _Alloc>
1518 typedef typename __istream_type::ios_base __ios_base;
1519 typedef typename __istream_type::int_type __int_type;
1520 typedef typename __string_type::size_type __size_type;
1522 __size_type __extracted = 0;
1523 const __size_type __n = __str.
max_size();
1524 typename __ios_base::iostate __err = __ios_base::goodbit;
1525 typename __istream_type::sentry __cerb(__in,
true);
1531 const __int_type __idelim = _Traits::to_int_type(__delim);
1532 const __int_type __eof = _Traits::eof();
1533 __int_type __c = __in.
rdbuf()->sgetc();
1535 while (__extracted < __n
1536 && !_Traits::eq_int_type(__c, __eof)
1537 && !_Traits::eq_int_type(__c, __idelim))
1539 __str += _Traits::to_char_type(__c);
1541 __c = __in.
rdbuf()->snextc();
1544 if (_Traits::eq_int_type(__c, __eof))
1545 __err |= __ios_base::eofbit;
1546 else if (_Traits::eq_int_type(__c, __idelim))
1549 __in.
rdbuf()->sbumpc();
1552 __err |= __ios_base::failbit;
1556 __in._M_setstate(__ios_base::badbit);
1557 __throw_exception_again;
1564 __in._M_setstate(__ios_base::badbit);
1568 __err |= __ios_base::failbit;
1576 #if _GLIBCXX_EXTERN_TEMPLATE > 0 1583 operator<<(basic_ostream<char>&,
const string&);
1591 #ifdef _GLIBCXX_USE_WCHAR_T 1598 operator<<(basic_ostream<wchar_t>&,
const wstring&);
1608 _GLIBCXX_END_NAMESPACE_VERSION
size_type max_size() const noexcept
Returns the size() of the largest possible string.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
basic_string()
Default constructor creates an empty string.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
int compare(const basic_string &__str) const
Compare to a string.
size_type find(const _CharT *__s, size_type __pos, size_type __n) const
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.
Managing sequences of characters and character-like objects.
basic_string & append(const basic_string &__str)
Append a string to this string.
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.
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Primary class template ctype facet.This template class defines classification and conversion function...
Template class basic_istream.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
void swap(basic_string &__s)
Swap contents with another 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.
Forward iterators support a superset of input iterator operations.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
const _CharT * data() const noexcept
Return const pointer to contents.
ISO C++ entities toplevel namespace is std.
size_type capacity() const noexcept
locale getloc() const
Locale access.
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.
void setstate(iostate __state)
Sets additional flags in the error state.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.