30#ifndef _RC_STRING_BASE_H
31#define _RC_STRING_BASE_H 1
39namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
84 template<
typename _CharT,
typename _Traits,
typename _Alloc>
86 :
protected __vstring_utility<_CharT, _Traits, _Alloc>
89 typedef _Traits traits_type;
90 typedef typename _Traits::char_type value_type;
91 typedef _Alloc allocator_type;
93 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
94 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
95 typedef typename _CharT_alloc_type::size_type size_type;
118 size_type _M_capacity;
119 _Atomic_word _M_refcount;
131 {
return reinterpret_cast<_CharT*
>(
this + 1); }
136 __atomic_add_dispatch(&_M_info._M_refcount, 1);
141 _M_set_length(size_type __n)
143 _M_info._M_refcount = 0;
144 _M_info._M_length = __n;
147 traits_type::assign(_M_refdata()[__n], _CharT());
152 _S_create(size_type, size_type,
const _Alloc&);
155 _M_destroy(
const _Alloc&)
throw();
158 _M_clone(
const _Alloc&, size_type __res = 0);
167 static _Rep_empty _S_empty_rep;
182 enum { _S_max_size = (((
static_cast<size_type
>(-1) - 2 *
sizeof(_Rep)
183 + 1) /
sizeof(_CharT)) - 1) / 2 };
186 mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
190 { _M_dataplus._M_p = __p; }
194 {
return &((
reinterpret_cast<_Rep*
>(_M_data()))[-1]); }
197 _M_grab(
const _Alloc& __alloc)
const
199 return (!_M_is_leaked() && _M_get_allocator() == __alloc)
200 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
207 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
209 if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
212 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
214 _M_rep()->_M_destroy(_M_get_allocator());
220 {
return _M_rep()->_M_info._M_refcount < 0; }
224 { _M_rep()->_M_info._M_refcount = 0; }
231 template<
typename _InIterator>
233 _S_construct_aux(_InIterator __beg, _InIterator __end,
234 const _Alloc& __a, std::__false_type)
238 return _S_construct(__beg, __end, __a, _Tag());
243 template<
typename _Integer>
245 _S_construct_aux(_Integer __beg, _Integer __end,
246 const _Alloc& __a, std::__true_type)
247 {
return _S_construct_aux_2(
static_cast<size_type
>(__beg),
251 _S_construct_aux_2(size_type __req, _CharT __c,
const _Alloc& __a)
252 {
return _S_construct(__req, __c, __a); }
254 template<
typename _InIterator>
256 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a)
258 typedef typename std::__is_integer<_InIterator>::__type _Integral;
259 return _S_construct_aux(__beg, __end, __a, _Integral());
263 template<
typename _InIterator>
265 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
270 template<
typename _FwdIterator>
272 _S_construct(_FwdIterator __beg, _FwdIterator __end,
const _Alloc& __a,
276 _S_construct(size_type __req, _CharT __c,
const _Alloc& __a);
281 {
return size_type(_S_max_size); }
285 {
return _M_dataplus._M_p; }
289 {
return _M_rep()->_M_info._M_length; }
293 {
return _M_rep()->_M_info._M_capacity; }
297 {
return _M_rep()->_M_info._M_refcount > 0; }
301 { _M_rep()->_M_info._M_refcount = -1; }
311 _M_set_length(size_type __n)
312 { _M_rep()->_M_set_length(__n); }
315 : _M_dataplus(_S_empty_rep._M_refcopy()) { }
321#if __cplusplus >= 201103L
323 : _M_dataplus(__rcs._M_dataplus)
324 { __rcs._M_data(_S_empty_rep._M_refcopy()); }
329 template<
typename _InputIterator>
338 {
return _M_dataplus; }
340 const allocator_type&
341 _M_get_allocator()
const
342 {
return _M_dataplus; }
351 _M_reserve(size_type __res);
354 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
358 _M_erase(size_type __pos, size_type __n);
364 _M_data(_S_empty_rep._M_refcopy());
372 template<
typename _CharT,
typename _Traits,
typename _Alloc>
376 template<
typename _CharT,
typename _Traits,
typename _Alloc>
379 _S_create(size_type __capacity, size_type __old_capacity,
380 const _Alloc& __alloc)
384 if (__capacity > size_type(_S_max_size))
385 std::__throw_length_error(__N(
"__rc_string_base::_Rep::_S_create"));
410 const size_type __pagesize = 4096;
411 const size_type __malloc_header_size = 4 *
sizeof(
void*);
416 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
418 __capacity = 2 * __old_capacity;
420 if (__capacity > size_type(_S_max_size))
421 __capacity = size_type(_S_max_size);
429 size_type __size = ((__capacity + 1) *
sizeof(_CharT)
430 + 2 *
sizeof(_Rep) - 1);
432 const size_type __adj_size = __size + __malloc_header_size;
433 if (__adj_size > __pagesize && __capacity > __old_capacity)
435 const size_type __extra = __pagesize - __adj_size % __pagesize;
436 __capacity += __extra /
sizeof(_CharT);
437 if (__capacity > size_type(_S_max_size))
438 __capacity = size_type(_S_max_size);
439 __size = (__capacity + 1) *
sizeof(_CharT) + 2 *
sizeof(_Rep) - 1;
444 _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size /
sizeof(_Rep));
445 _Rep* __p =
new (__place) _Rep;
446 __p->_M_info._M_capacity = __capacity;
450 template<
typename _CharT,
typename _Traits,
typename _Alloc>
452 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
453 _M_destroy(
const _Alloc& __a)
throw ()
455 const size_type __size = ((_M_info._M_capacity + 1) *
sizeof(_CharT)
456 + 2 *
sizeof(_Rep) - 1);
457 _Rep_alloc_type(__a).deallocate(
this, __size /
sizeof(_Rep));
460 template<
typename _CharT,
typename _Traits,
typename _Alloc>
462 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
463 _M_clone(
const _Alloc& __alloc, size_type __res)
466 const size_type __requested_cap = _M_info._M_length + __res;
467 _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
470 if (_M_info._M_length)
471 __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
473 __r->_M_set_length(_M_info._M_length);
474 return __r->_M_refdata();
477 template<
typename _CharT,
typename _Traits,
typename _Alloc>
478 __rc_string_base<_CharT, _Traits, _Alloc>::
479 __rc_string_base(
const _Alloc& __a)
480 : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
482 template<
typename _CharT,
typename _Traits,
typename _Alloc>
483 __rc_string_base<_CharT, _Traits, _Alloc>::
484 __rc_string_base(
const __rc_string_base& __rcs)
485 : _M_dataplus(__rcs._M_get_allocator(),
486 __rcs._M_grab(__rcs._M_get_allocator())) { }
488 template<
typename _CharT,
typename _Traits,
typename _Alloc>
489 __rc_string_base<_CharT, _Traits, _Alloc>::
490 __rc_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
491 : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
493 template<
typename _CharT,
typename _Traits,
typename _Alloc>
494 template<
typename _InputIterator>
495 __rc_string_base<_CharT, _Traits, _Alloc>::
496 __rc_string_base(_InputIterator __beg, _InputIterator __end,
498 : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
500 template<
typename _CharT,
typename _Traits,
typename _Alloc>
502 __rc_string_base<_CharT, _Traits, _Alloc>::
514 template<
typename _CharT,
typename _Traits,
typename _Alloc>
515 template<
typename _InIterator>
517 __rc_string_base<_CharT, _Traits, _Alloc>::
518 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
521 if (__beg == __end && __a == _Alloc())
522 return _S_empty_rep._M_refcopy();
527 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
529 __buf[__len++] = *__beg;
532 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
533 _S_copy(__r->_M_refdata(), __buf, __len);
536 while (__beg != __end)
538 if (__len == __r->_M_info._M_capacity)
541 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
542 _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
543 __r->_M_destroy(__a);
546 __r->_M_refdata()[__len++] = *__beg;
552 __r->_M_destroy(__a);
553 __throw_exception_again;
555 __r->_M_set_length(__len);
556 return __r->_M_refdata();
559 template<
typename _CharT,
typename _Traits,
typename _Alloc>
560 template<
typename _InIterator>
562 __rc_string_base<_CharT, _Traits, _Alloc>::
563 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
566 if (__beg == __end && __a == _Alloc())
567 return _S_empty_rep._M_refcopy();
570 if (__is_null_pointer(__beg) && __beg != __end)
571 std::__throw_logic_error(__N(
"__rc_string_base::"
572 "_S_construct null not valid"));
574 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
577 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
579 { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
582 __r->_M_destroy(__a);
583 __throw_exception_again;
585 __r->_M_set_length(__dnew);
586 return __r->_M_refdata();
589 template<
typename _CharT,
typename _Traits,
typename _Alloc>
591 __rc_string_base<_CharT, _Traits, _Alloc>::
592 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
594 if (__n == 0 && __a == _Alloc())
595 return _S_empty_rep._M_refcopy();
598 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
600 __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
602 __r->_M_set_length(__n);
603 return __r->_M_refdata();
606 template<
typename _CharT,
typename _Traits,
typename _Alloc>
608 __rc_string_base<_CharT, _Traits, _Alloc>::
609 _M_swap(__rc_string_base& __rcs)
613 if (__rcs._M_is_leaked())
614 __rcs._M_set_sharable();
616 _CharT* __tmp = _M_data();
617 _M_data(__rcs._M_data());
618 __rcs._M_data(__tmp);
622 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
623 __rcs._M_get_allocator());
626 template<
typename _CharT,
typename _Traits,
typename _Alloc>
628 __rc_string_base<_CharT, _Traits, _Alloc>::
629 _M_assign(
const __rc_string_base& __rcs)
631 if (_M_rep() != __rcs._M_rep())
633 _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
639 template<
typename _CharT,
typename _Traits,
typename _Alloc>
641 __rc_string_base<_CharT, _Traits, _Alloc>::
642 _M_reserve(size_type __res)
645 if (__res < _M_length())
648 if (__res != _M_capacity() || _M_is_shared())
650 _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
651 __res - _M_length());
657 template<
typename _CharT,
typename _Traits,
typename _Alloc>
659 __rc_string_base<_CharT, _Traits, _Alloc>::
660 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
663 const size_type __how_much = _M_length() - __pos - __len1;
665 _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
666 _M_capacity(), _M_get_allocator());
669 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
671 this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
673 this->_S_copy(__r->_M_refdata() + __pos + __len2,
674 _M_data() + __pos + __len1, __how_much);
677 _M_data(__r->_M_refdata());
680 template<
typename _CharT,
typename _Traits,
typename _Alloc>
682 __rc_string_base<_CharT, _Traits, _Alloc>::
683 _M_erase(size_type __pos, size_type __n)
685 const size_type __new_size = _M_length() - __n;
686 const size_type __how_much = _M_length() - __pos - __n;
691 _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
695 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
697 this->_S_copy(__r->_M_refdata() + __pos,
698 _M_data() + __pos + __n, __how_much);
701 _M_data(__r->_M_refdata());
703 else if (__how_much && __n)
706 this->_S_move(_M_data() + __pos,
707 _M_data() + __pos + __n, __how_much);
710 _M_rep()->_M_set_length(__new_size);
715 __rc_string_base<char, std::char_traits<char>,
717 _M_compare(
const __rc_string_base& __rcs)
const
719 if (_M_rep() == __rcs._M_rep())
726 __rc_string_base<wchar_t, std::char_traits<wchar_t>,
728 _M_compare(
const __rc_string_base& __rcs)
const
730 if (_M_rep() == __rcs._M_rep())
735_GLIBCXX_END_NAMESPACE_VERSION
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
GNU extensions for public use.
The standard allocator, as per C++03 [20.4.1].
Traits class for iterators.
Forward iterators support a superset of input iterator operations.
Uniform interface to C++98 and C++11 allocators.