30 #ifndef _RC_STRING_BASE_H
31 #define _RC_STRING_BASE_H 1
37 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
82 template<
typename _CharT,
typename _Traits,
typename _Alloc>
84 :
protected __vstring_utility<_CharT, _Traits, _Alloc>
87 typedef _Traits traits_type;
88 typedef typename _Traits::char_type value_type;
89 typedef _Alloc allocator_type;
91 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
92 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
93 typedef typename _CharT_alloc_type::size_type size_type;
116 size_type _M_capacity;
117 _Atomic_word _M_refcount;
129 {
return reinterpret_cast<_CharT*
>(
this + 1); }
134 __atomic_add_dispatch(&_M_info._M_refcount, 1);
139 _M_set_length(size_type __n)
141 _M_info._M_refcount = 0;
142 _M_info._M_length = __n;
145 traits_type::assign(_M_refdata()[__n], _CharT());
150 _S_create(size_type, size_type,
const _Alloc&);
153 _M_destroy(
const _Alloc&)
throw();
156 _M_clone(
const _Alloc&, size_type __res = 0);
165 static _Rep_empty _S_empty_rep;
180 enum { _S_max_size = (((
static_cast<size_type
>(-1) - 2 *
sizeof(_Rep)
181 + 1) /
sizeof(_CharT)) - 1) / 2 };
184 mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
188 { _M_dataplus._M_p = __p; }
192 {
return &((
reinterpret_cast<_Rep*
>(_M_data()))[-1]); }
195 _M_grab(
const _Alloc& __alloc)
const
197 return (!_M_is_leaked() && _M_get_allocator() == __alloc)
198 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
205 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
207 if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
210 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
212 _M_rep()->_M_destroy(_M_get_allocator());
218 {
return _M_rep()->_M_info._M_refcount < 0; }
222 { _M_rep()->_M_info._M_refcount = 0; }
229 template<
typename _InIterator>
231 _S_construct_aux(_InIterator __beg, _InIterator __end,
232 const _Alloc& __a, std::__false_type)
236 return _S_construct(__beg, __end, __a, _Tag());
241 template<
typename _Integer>
243 _S_construct_aux(_Integer __beg, _Integer __end,
244 const _Alloc& __a, std::__true_type)
245 {
return _S_construct_aux_2(
static_cast<size_type
>(__beg),
249 _S_construct_aux_2(size_type __req, _CharT __c,
const _Alloc& __a)
250 {
return _S_construct(__req, __c, __a); }
252 template<
typename _InIterator>
254 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a)
256 typedef typename std::__is_integer<_InIterator>::__type _Integral;
257 return _S_construct_aux(__beg, __end, __a, _Integral());
261 template<
typename _InIterator>
263 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
268 template<
typename _FwdIterator>
270 _S_construct(_FwdIterator __beg, _FwdIterator __end,
const _Alloc& __a,
274 _S_construct(size_type __req, _CharT __c,
const _Alloc& __a);
279 {
return size_type(_S_max_size); }
283 {
return _M_dataplus._M_p; }
287 {
return _M_rep()->_M_info._M_length; }
291 {
return _M_rep()->_M_info._M_capacity; }
295 {
return _M_rep()->_M_info._M_refcount > 0; }
299 { _M_rep()->_M_info._M_refcount = -1; }
309 _M_set_length(size_type __n)
310 { _M_rep()->_M_set_length(__n); }
313 : _M_dataplus(_S_empty_rep._M_refcopy()) { }
319 #if __cplusplus >= 201103L
321 : _M_dataplus(__rcs._M_dataplus)
322 { __rcs._M_data(_S_empty_rep._M_refcopy()); }
327 template<
typename _InputIterator>
336 {
return _M_dataplus; }
338 const allocator_type&
339 _M_get_allocator()
const
340 {
return _M_dataplus; }
349 _M_reserve(size_type __res);
352 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
356 _M_erase(size_type __pos, size_type __n);
362 _M_data(_S_empty_rep._M_refcopy());
370 template<
typename _CharT,
typename _Traits,
typename _Alloc>
374 template<
typename _CharT,
typename _Traits,
typename _Alloc>
377 _S_create(size_type __capacity, size_type __old_capacity,
378 const _Alloc& __alloc)
382 if (__capacity > size_type(_S_max_size))
383 std::__throw_length_error(__N(
"__rc_string_base::_Rep::_S_create"));
408 const size_type __pagesize = 4096;
409 const size_type __malloc_header_size = 4 *
sizeof(
void*);
414 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
416 __capacity = 2 * __old_capacity;
418 if (__capacity > size_type(_S_max_size))
419 __capacity = size_type(_S_max_size);
427 size_type __size = ((__capacity + 1) *
sizeof(_CharT)
428 + 2 *
sizeof(_Rep) - 1);
430 const size_type __adj_size = __size + __malloc_header_size;
431 if (__adj_size > __pagesize && __capacity > __old_capacity)
433 const size_type __extra = __pagesize - __adj_size % __pagesize;
434 __capacity += __extra /
sizeof(_CharT);
435 if (__capacity > size_type(_S_max_size))
436 __capacity = size_type(_S_max_size);
437 __size = (__capacity + 1) *
sizeof(_CharT) + 2 *
sizeof(_Rep) - 1;
442 _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size /
sizeof(_Rep));
443 _Rep* __p =
new (__place) _Rep;
444 __p->_M_info._M_capacity = __capacity;
448 template<
typename _CharT,
typename _Traits,
typename _Alloc>
450 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
451 _M_destroy(
const _Alloc& __a)
throw ()
453 const size_type __size = ((_M_info._M_capacity + 1) *
sizeof(_CharT)
454 + 2 *
sizeof(_Rep) - 1);
455 _Rep_alloc_type(__a).deallocate(
this, __size /
sizeof(_Rep));
458 template<
typename _CharT,
typename _Traits,
typename _Alloc>
460 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
461 _M_clone(
const _Alloc& __alloc, size_type __res)
464 const size_type __requested_cap = _M_info._M_length + __res;
465 _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
468 if (_M_info._M_length)
469 __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
471 __r->_M_set_length(_M_info._M_length);
472 return __r->_M_refdata();
475 template<
typename _CharT,
typename _Traits,
typename _Alloc>
476 __rc_string_base<_CharT, _Traits, _Alloc>::
477 __rc_string_base(
const _Alloc& __a)
478 : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
480 template<
typename _CharT,
typename _Traits,
typename _Alloc>
481 __rc_string_base<_CharT, _Traits, _Alloc>::
482 __rc_string_base(
const __rc_string_base& __rcs)
483 : _M_dataplus(__rcs._M_get_allocator(),
484 __rcs._M_grab(__rcs._M_get_allocator())) { }
486 template<
typename _CharT,
typename _Traits,
typename _Alloc>
487 __rc_string_base<_CharT, _Traits, _Alloc>::
488 __rc_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
489 : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
491 template<
typename _CharT,
typename _Traits,
typename _Alloc>
492 template<
typename _InputIterator>
493 __rc_string_base<_CharT, _Traits, _Alloc>::
494 __rc_string_base(_InputIterator __beg, _InputIterator __end,
496 : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
498 template<
typename _CharT,
typename _Traits,
typename _Alloc>
500 __rc_string_base<_CharT, _Traits, _Alloc>::
512 template<
typename _CharT,
typename _Traits,
typename _Alloc>
513 template<
typename _InIterator>
515 __rc_string_base<_CharT, _Traits, _Alloc>::
516 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
519 if (__beg == __end && __a == _Alloc())
520 return _S_empty_rep._M_refcopy();
525 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
527 __buf[__len++] = *__beg;
530 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
531 _S_copy(__r->_M_refdata(), __buf, __len);
534 while (__beg != __end)
536 if (__len == __r->_M_info._M_capacity)
539 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
540 _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
541 __r->_M_destroy(__a);
544 __r->_M_refdata()[__len++] = *__beg;
550 __r->_M_destroy(__a);
551 __throw_exception_again;
553 __r->_M_set_length(__len);
554 return __r->_M_refdata();
557 template<
typename _CharT,
typename _Traits,
typename _Alloc>
558 template<
typename _InIterator>
560 __rc_string_base<_CharT, _Traits, _Alloc>::
561 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
564 if (__beg == __end && __a == _Alloc())
565 return _S_empty_rep._M_refcopy();
568 if (__is_null_pointer(__beg) && __beg != __end)
569 std::__throw_logic_error(__N(
"__rc_string_base::"
570 "_S_construct null not valid"));
572 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
575 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
577 { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
580 __r->_M_destroy(__a);
581 __throw_exception_again;
583 __r->_M_set_length(__dnew);
584 return __r->_M_refdata();
587 template<
typename _CharT,
typename _Traits,
typename _Alloc>
589 __rc_string_base<_CharT, _Traits, _Alloc>::
590 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
592 if (__n == 0 && __a == _Alloc())
593 return _S_empty_rep._M_refcopy();
596 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
598 __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
600 __r->_M_set_length(__n);
601 return __r->_M_refdata();
604 template<
typename _CharT,
typename _Traits,
typename _Alloc>
606 __rc_string_base<_CharT, _Traits, _Alloc>::
607 _M_swap(__rc_string_base& __rcs)
611 if (__rcs._M_is_leaked())
612 __rcs._M_set_sharable();
614 _CharT* __tmp = _M_data();
615 _M_data(__rcs._M_data());
616 __rcs._M_data(__tmp);
620 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
621 __rcs._M_get_allocator());
624 template<
typename _CharT,
typename _Traits,
typename _Alloc>
626 __rc_string_base<_CharT, _Traits, _Alloc>::
627 _M_assign(
const __rc_string_base& __rcs)
629 if (_M_rep() != __rcs._M_rep())
631 _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
637 template<
typename _CharT,
typename _Traits,
typename _Alloc>
639 __rc_string_base<_CharT, _Traits, _Alloc>::
640 _M_reserve(size_type __res)
643 if (__res < _M_length())
646 if (__res != _M_capacity() || _M_is_shared())
648 _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
649 __res - _M_length());
655 template<
typename _CharT,
typename _Traits,
typename _Alloc>
657 __rc_string_base<_CharT, _Traits, _Alloc>::
658 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
661 const size_type __how_much = _M_length() - __pos - __len1;
663 _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
664 _M_capacity(), _M_get_allocator());
667 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
669 this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
671 this->_S_copy(__r->_M_refdata() + __pos + __len2,
672 _M_data() + __pos + __len1, __how_much);
675 _M_data(__r->_M_refdata());
678 template<
typename _CharT,
typename _Traits,
typename _Alloc>
680 __rc_string_base<_CharT, _Traits, _Alloc>::
681 _M_erase(size_type __pos, size_type __n)
683 const size_type __new_size = _M_length() - __n;
684 const size_type __how_much = _M_length() - __pos - __n;
689 _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
693 this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
695 this->_S_copy(__r->_M_refdata() + __pos,
696 _M_data() + __pos + __n, __how_much);
699 _M_data(__r->_M_refdata());
701 else if (__how_much && __n)
704 this->_S_move(_M_data() + __pos,
705 _M_data() + __pos + __n, __how_much);
708 _M_rep()->_M_set_length(__new_size);
713 __rc_string_base<char, std::char_traits<char>,
715 _M_compare(
const __rc_string_base& __rcs)
const
717 if (_M_rep() == __rcs._M_rep())
724 __rc_string_base<wchar_t, std::char_traits<wchar_t>,
726 _M_compare(
const __rc_string_base& __rcs)
const
728 if (_M_rep() == __rcs._M_rep())
733 _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.