30 #ifndef _RC_STRING_BASE_H 31 #define _RC_STRING_BASE_H 1 36 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
81 template<
typename _CharT,
typename _Traits,
typename _Alloc>
83 :
protected __vstring_utility<_CharT, _Traits, _Alloc>
86 typedef _Traits traits_type;
87 typedef typename _Traits::char_type value_type;
88 typedef _Alloc allocator_type;
90 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
91 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
92 typedef typename _CharT_alloc_type::size_type size_type;
115 size_type _M_capacity;
116 _Atomic_word _M_refcount;
123 typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
127 {
return reinterpret_cast<_CharT*>(
this + 1); }
132 __atomic_add_dispatch(&_M_info._M_refcount, 1);
137 _M_set_length(size_type __n)
139 _M_info._M_refcount = 0;
140 _M_info._M_length = __n;
143 traits_type::assign(_M_refdata()[__n], _CharT());
148 _S_create(size_type, size_type,
const _Alloc&);
151 _M_destroy(
const _Alloc&)
throw();
154 _M_clone(
const _Alloc&, size_type __res = 0);
163 static _Rep_empty _S_empty_rep;
178 enum { _S_max_size = (((static_cast<size_type>(-1) - 2 *
sizeof(_Rep)
179 + 1) /
sizeof(_CharT)) - 1) / 2 };
182 mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
186 { _M_dataplus._M_p = __p; }
190 {
return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
193 _M_grab(
const _Alloc& __alloc)
const 195 return (!_M_is_leaked() && _M_get_allocator() == __alloc)
196 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
203 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
205 if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
208 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
210 _M_rep()->_M_destroy(_M_get_allocator());
216 {
return _M_rep()->_M_info._M_refcount < 0; }
220 { _M_rep()->_M_info._M_refcount = 0; }
227 template<
typename _InIterator>
229 _S_construct_aux(_InIterator __beg, _InIterator __end,
230 const _Alloc& __a, std::__false_type)
232 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
233 return _S_construct(__beg, __end, __a, _Tag());
238 template<
typename _Integer>
240 _S_construct_aux(_Integer __beg, _Integer __end,
241 const _Alloc& __a, std::__true_type)
242 {
return _S_construct_aux_2(static_cast<size_type>(__beg),
246 _S_construct_aux_2(size_type __req, _CharT __c,
const _Alloc& __a)
247 {
return _S_construct(__req, __c, __a); }
249 template<
typename _InIterator>
251 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a)
253 typedef typename std::__is_integer<_InIterator>::__type _Integral;
254 return _S_construct_aux(__beg, __end, __a, _Integral());
258 template<
typename _InIterator>
260 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
265 template<
typename _FwdIterator>
267 _S_construct(_FwdIterator __beg, _FwdIterator __end,
const _Alloc& __a,
271 _S_construct(size_type __req, _CharT __c,
const _Alloc& __a);
276 {
return size_type(_S_max_size); }
280 {
return _M_dataplus._M_p; }
284 {
return _M_rep()->_M_info._M_length; }
288 {
return _M_rep()->_M_info._M_capacity; }
292 {
return _M_rep()->_M_info._M_refcount > 0; }
296 { _M_rep()->_M_info._M_refcount = -1; }
306 _M_set_length(size_type __n)
307 { _M_rep()->_M_set_length(__n); }
310 : _M_dataplus(_S_empty_rep._M_refcopy()) { }
316 #if __cplusplus >= 201103L 318 : _M_dataplus(__rcs._M_dataplus)
319 { __rcs._M_data(_S_empty_rep._M_refcopy()); }
324 template<
typename _InputIterator>
333 {
return _M_dataplus; }
335 const allocator_type&
336 _M_get_allocator()
const 337 {
return _M_dataplus; }
346 _M_reserve(size_type __res);
349 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
353 _M_erase(size_type __pos, size_type __n);
359 _M_data(_S_empty_rep._M_refcopy());
367 template<
typename _CharT,
typename _Traits,
typename _Alloc>
371 template<
typename _CharT,
typename _Traits,
typename _Alloc>
374 _S_create(size_type __capacity, size_type __old_capacity,
375 const _Alloc& __alloc)
379 if (__capacity > size_type(_S_max_size))
380 std::__throw_length_error(__N(
"__rc_string_base::_Rep::_S_create"));
405 const size_type __pagesize = 4096;
406 const size_type __malloc_header_size = 4 *
sizeof(
void*);
411 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
413 __capacity = 2 * __old_capacity;
415 if (__capacity > size_type(_S_max_size))
416 __capacity = size_type(_S_max_size);
424 size_type __size = ((__capacity + 1) *
sizeof(_CharT)
425 + 2 *
sizeof(_Rep) - 1);
427 const size_type __adj_size = __size + __malloc_header_size;
428 if (__adj_size > __pagesize && __capacity > __old_capacity)
430 const size_type __extra = __pagesize - __adj_size % __pagesize;
431 __capacity += __extra /
sizeof(_CharT);
432 if (__capacity > size_type(_S_max_size))
433 __capacity = size_type(_S_max_size);
434 __size = (__capacity + 1) *
sizeof(_CharT) + 2 *
sizeof(_Rep) - 1;
439 _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size /
sizeof(_Rep));
440 _Rep* __p =
new (__place) _Rep;
441 __p->_M_info._M_capacity = __capacity;
445 template<
typename _CharT,
typename _Traits,
typename _Alloc>
447 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
448 _M_destroy(
const _Alloc& __a)
throw ()
450 const size_type __size = ((_M_info._M_capacity + 1) *
sizeof(_CharT)
451 + 2 *
sizeof(_Rep) - 1);
452 _Rep_alloc_type(__a).deallocate(
this, __size /
sizeof(_Rep));
455 template<
typename _CharT,
typename _Traits,
typename _Alloc>
457 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
458 _M_clone(
const _Alloc& __alloc, size_type __res)
461 const size_type __requested_cap = _M_info._M_length + __res;
462 _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
465 if (_M_info._M_length)
466 __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
468 __r->_M_set_length(_M_info._M_length);
469 return __r->_M_refdata();
472 template<
typename _CharT,
typename _Traits,
typename _Alloc>
473 __rc_string_base<_CharT, _Traits, _Alloc>::
474 __rc_string_base(
const _Alloc& __a)
475 : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
477 template<
typename _CharT,
typename _Traits,
typename _Alloc>
478 __rc_string_base<_CharT, _Traits, _Alloc>::
479 __rc_string_base(
const __rc_string_base& __rcs)
480 : _M_dataplus(__rcs._M_get_allocator(),
481 __rcs._M_grab(__rcs._M_get_allocator())) { }
483 template<
typename _CharT,
typename _Traits,
typename _Alloc>
484 __rc_string_base<_CharT, _Traits, _Alloc>::
485 __rc_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
486 : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
488 template<
typename _CharT,
typename _Traits,
typename _Alloc>
489 template<
typename _InputIterator>
490 __rc_string_base<_CharT, _Traits, _Alloc>::
491 __rc_string_base(_InputIterator __beg, _InputIterator __end,
493 : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
495 template<
typename _CharT,
typename _Traits,
typename _Alloc>
497 __rc_string_base<_CharT, _Traits, _Alloc>::
509 template<
typename _CharT,
typename _Traits,
typename _Alloc>
510 template<
typename _InIterator>
512 __rc_string_base<_CharT, _Traits, _Alloc>::
513 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
516 if (__beg == __end && __a == _Alloc())
517 return _S_empty_rep._M_refcopy();
522 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
524 __buf[__len++] = *__beg;
527 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
528 _S_copy(__r->_M_refdata(), __buf, __len);
531 while (__beg != __end)
533 if (__len == __r->_M_info._M_capacity)
536 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
537 _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
538 __r->_M_destroy(__a);
541 __r->_M_refdata()[__len++] = *__beg;
547 __r->_M_destroy(__a);
548 __throw_exception_again;
550 __r->_M_set_length(__len);
551 return __r->_M_refdata();
554 template<
typename _CharT,
typename _Traits,
typename _Alloc>
555 template<
typename _InIterator>
557 __rc_string_base<_CharT, _Traits, _Alloc>::
558 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
561 if (__beg == __end && __a == _Alloc())
562 return _S_empty_rep._M_refcopy();
565 if (__is_null_pointer(__beg) && __beg != __end)
566 std::__throw_logic_error(__N(
"__rc_string_base::" 567 "_S_construct null not valid"));
569 const size_type __dnew = static_cast<size_type>(
std::distance(__beg,
572 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
574 { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
577 __r->_M_destroy(__a);
578 __throw_exception_again;
580 __r->_M_set_length(__dnew);
581 return __r->_M_refdata();
584 template<
typename _CharT,
typename _Traits,
typename _Alloc>
586 __rc_string_base<_CharT, _Traits, _Alloc>::
587 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
589 if (__n == 0 && __a == _Alloc())
590 return _S_empty_rep._M_refcopy();
593 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
595 __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
597 __r->_M_set_length(__n);
598 return __r->_M_refdata();
601 template<
typename _CharT,
typename _Traits,
typename _Alloc>
603 __rc_string_base<_CharT, _Traits, _Alloc>::
604 _M_swap(__rc_string_base& __rcs)
608 if (__rcs._M_is_leaked())
609 __rcs._M_set_sharable();
611 _CharT* __tmp = _M_data();
612 _M_data(__rcs._M_data());
613 __rcs._M_data(__tmp);
617 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
618 __rcs._M_get_allocator());
621 template<
typename _CharT,
typename _Traits,
typename _Alloc>
623 __rc_string_base<_CharT, _Traits, _Alloc>::
624 _M_assign(
const __rc_string_base& __rcs)
626 if (_M_rep() != __rcs._M_rep())
628 _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
634 template<
typename _CharT,
typename _Traits,
typename _Alloc>
636 __rc_string_base<_CharT, _Traits, _Alloc>::
637 _M_reserve(size_type __res)
640 if (__res < _M_length())
643 if (__res != _M_capacity() || _M_is_shared())
645 _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
646 __res - _M_length());
652 template<
typename _CharT,
typename _Traits,
typename _Alloc>
654 __rc_string_base<_CharT, _Traits, _Alloc>::
655 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
658 const size_type __how_much = _M_length() - __pos - __len1;
660 _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
661 _M_capacity(), _M_get_allocator());
664 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
666 this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
668 this->_S_copy(__r->_M_refdata() + __pos + __len2,
669 _M_data() + __pos + __len1, __how_much);
672 _M_data(__r->_M_refdata());
675 template<
typename _CharT,
typename _Traits,
typename _Alloc>
677 __rc_string_base<_CharT, _Traits, _Alloc>::
678 _M_erase(size_type __pos, size_type __n)
680 const size_type __new_size = _M_length() - __n;
681 const size_type __how_much = _M_length() - __pos - __n;
686 _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
690 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
692 this->_S_copy(__r->_M_refdata() + __pos,
693 _M_data() + __pos + __n, __how_much);
696 _M_data(__r->_M_refdata());
698 else if (__how_much && __n)
701 this->_S_move(_M_data() + __pos,
702 _M_data() + __pos + __n, __how_much);
705 _M_rep()->_M_set_length(__new_size);
710 __rc_string_base<char, std::char_traits<char>,
712 _M_compare(
const __rc_string_base& __rcs)
const 714 if (_M_rep() == __rcs._M_rep())
719 #ifdef _GLIBCXX_USE_WCHAR_T 722 __rc_string_base<wchar_t, std::char_traits<wchar_t>,
724 _M_compare(
const __rc_string_base& __rcs)
const 726 if (_M_rep() == __rcs._M_rep())
732 _GLIBCXX_END_NAMESPACE_VERSION
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
The standard allocator, as per [20.4].
Forward iterators support a superset of input iterator operations.
GNU extensions for public use.