31 #ifndef _RC_STRING_BASE_H
32 #define _RC_STRING_BASE_H 1
37 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
80 template<typename _CharT, typename _Traits, typename _Alloc>
82 : protected __vstring_utility<_CharT, _Traits, _Alloc>
85 typedef _Traits traits_type;
86 typedef typename _Traits::char_type value_type;
87 typedef _Alloc allocator_type;
89 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
90 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
91 typedef typename _CharT_alloc_type::size_type size_type;
114 size_type _M_capacity;
115 _Atomic_word _M_refcount;
122 typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
126 {
return reinterpret_cast<_CharT*
>(
this + 1); }
131 __atomic_add_dispatch(&_M_info._M_refcount, 1);
136 _M_set_length(size_type __n)
138 _M_info._M_refcount = 0;
139 _M_info._M_length = __n;
142 traits_type::assign(_M_refdata()[__n], _CharT());
147 _S_create(size_type, size_type,
const _Alloc&);
150 _M_destroy(
const _Alloc&)
throw();
153 _M_clone(
const _Alloc&, size_type __res = 0);
162 static _Rep_empty _S_empty_rep;
177 enum { _S_max_size = (((
static_cast<size_type
>(-1) - 2 *
sizeof(_Rep)
178 + 1) /
sizeof(_CharT)) - 1) / 2 };
181 mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
185 { _M_dataplus._M_p = __p; }
189 {
return &((
reinterpret_cast<_Rep*
>(_M_data()))[-1]); }
192 _M_grab(
const _Alloc& __alloc)
const
194 return (!_M_is_leaked() && _M_get_allocator() == __alloc)
195 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
201 if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
203 _M_rep()->_M_destroy(_M_get_allocator());
208 {
return _M_rep()->_M_info._M_refcount < 0; }
212 { _M_rep()->_M_info._M_refcount = 0; }
219 template<
typename _InIterator>
221 _S_construct_aux(_InIterator __beg, _InIterator __end,
222 const _Alloc& __a, std::__false_type)
224 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
225 return _S_construct(__beg, __end, __a, _Tag());
230 template<
typename _Integer>
232 _S_construct_aux(_Integer __beg, _Integer __end,
233 const _Alloc& __a, std::__true_type)
234 {
return _S_construct(static_cast<size_type>(__beg), __end, __a); }
236 template<
typename _InIterator>
238 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a)
240 typedef typename std::__is_integer<_InIterator>::__type _Integral;
241 return _S_construct_aux(__beg, __end, __a, _Integral());
245 template<
typename _InIterator>
247 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
252 template<
typename _FwdIterator>
254 _S_construct(_FwdIterator __beg, _FwdIterator __end,
const _Alloc& __a,
258 _S_construct(size_type __req, _CharT __c,
const _Alloc& __a);
263 {
return size_type(_S_max_size); }
267 {
return _M_dataplus._M_p; }
271 {
return _M_rep()->_M_info._M_length; }
275 {
return _M_rep()->_M_info._M_capacity; }
279 {
return _M_rep()->_M_info._M_refcount > 0; }
283 { _M_rep()->_M_info._M_refcount = -1; }
293 _M_set_length(size_type __n)
294 { _M_rep()->_M_set_length(__n); }
297 : _M_dataplus(_S_empty_rep._M_refcopy()) { }
303 #ifdef __GXX_EXPERIMENTAL_CXX0X__
305 : _M_dataplus(__rcs._M_get_allocator(), __rcs._M_data())
306 { __rcs._M_data(_S_empty_rep._M_refcopy()); }
311 template<
typename _InputIterator>
320 {
return _M_dataplus; }
322 const allocator_type&
323 _M_get_allocator()
const
324 {
return _M_dataplus; }
333 _M_reserve(size_type __res);
336 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
340 _M_erase(size_type __pos, size_type __n);
344 { _M_erase(size_type(0), _M_length()); }
351 template<
typename _CharT,
typename _Traits,
typename _Alloc>
355 template<
typename _CharT,
typename _Traits,
typename _Alloc>
358 _S_create(size_type __capacity, size_type __old_capacity,
359 const _Alloc& __alloc)
363 if (__capacity > size_type(_S_max_size))
364 std::__throw_length_error(__N(
"__rc_string_base::_Rep::_S_create"));
389 const size_type __pagesize = 4096;
390 const size_type __malloc_header_size = 4 *
sizeof(
void*);
395 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
397 __capacity = 2 * __old_capacity;
399 if (__capacity > size_type(_S_max_size))
400 __capacity = size_type(_S_max_size);
408 size_type __size = ((__capacity + 1) *
sizeof(_CharT)
409 + 2 *
sizeof(_Rep) - 1);
411 const size_type __adj_size = __size + __malloc_header_size;
412 if (__adj_size > __pagesize && __capacity > __old_capacity)
414 const size_type __extra = __pagesize - __adj_size % __pagesize;
415 __capacity += __extra /
sizeof(_CharT);
416 if (__capacity > size_type(_S_max_size))
417 __capacity = size_type(_S_max_size);
418 __size = (__capacity + 1) *
sizeof(_CharT) + 2 *
sizeof(_Rep) - 1;
423 _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size /
sizeof(_Rep));
424 _Rep* __p =
new (__place) _Rep;
425 __p->_M_info._M_capacity = __capacity;
429 template<
typename _CharT,
typename _Traits,
typename _Alloc>
431 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
432 _M_destroy(
const _Alloc& __a)
throw ()
434 const size_type __size = ((_M_info._M_capacity + 1) *
sizeof(_CharT)
435 + 2 *
sizeof(_Rep) - 1);
436 _Rep_alloc_type(__a).deallocate(
this, __size /
sizeof(_Rep));
439 template<
typename _CharT,
typename _Traits,
typename _Alloc>
441 __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
442 _M_clone(
const _Alloc& __alloc, size_type __res)
445 const size_type __requested_cap = _M_info._M_length + __res;
446 _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
449 if (_M_info._M_length)
450 _S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
452 __r->_M_set_length(_M_info._M_length);
453 return __r->_M_refdata();
456 template<
typename _CharT,
typename _Traits,
typename _Alloc>
457 __rc_string_base<_CharT, _Traits, _Alloc>::
458 __rc_string_base(
const _Alloc& __a)
459 : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
461 template<
typename _CharT,
typename _Traits,
typename _Alloc>
462 __rc_string_base<_CharT, _Traits, _Alloc>::
463 __rc_string_base(
const __rc_string_base& __rcs)
464 : _M_dataplus(__rcs._M_get_allocator(),
465 __rcs._M_grab(__rcs._M_get_allocator())) { }
467 template<
typename _CharT,
typename _Traits,
typename _Alloc>
468 __rc_string_base<_CharT, _Traits, _Alloc>::
469 __rc_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
470 : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
472 template<
typename _CharT,
typename _Traits,
typename _Alloc>
473 template<
typename _InputIterator>
474 __rc_string_base<_CharT, _Traits, _Alloc>::
475 __rc_string_base(_InputIterator __beg, _InputIterator __end,
477 : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
479 template<
typename _CharT,
typename _Traits,
typename _Alloc>
481 __rc_string_base<_CharT, _Traits, _Alloc>::
493 template<
typename _CharT,
typename _Traits,
typename _Alloc>
494 template<
typename _InIterator>
496 __rc_string_base<_CharT, _Traits, _Alloc>::
497 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
500 if (__beg == __end && __a == _Alloc())
501 return _S_empty_rep._M_refcopy();
506 while (__beg != __end && __len <
sizeof(__buf) /
sizeof(_CharT))
508 __buf[__len++] = *__beg;
511 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
512 _S_copy(__r->_M_refdata(), __buf, __len);
515 while (__beg != __end)
517 if (__len == __r->_M_info._M_capacity)
520 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
521 _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
522 __r->_M_destroy(__a);
525 __r->_M_refdata()[__len++] = *__beg;
531 __r->_M_destroy(__a);
532 __throw_exception_again;
534 __r->_M_set_length(__len);
535 return __r->_M_refdata();
538 template<
typename _CharT,
typename _Traits,
typename _Alloc>
539 template<
typename _InIterator>
541 __rc_string_base<_CharT, _Traits, _Alloc>::
542 _S_construct(_InIterator __beg, _InIterator __end,
const _Alloc& __a,
545 if (__beg == __end && __a == _Alloc())
546 return _S_empty_rep._M_refcopy();
549 if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
550 std::__throw_logic_error(__N(
"__rc_string_base::"
551 "_S_construct NULL not valid"));
553 const size_type __dnew =
static_cast<size_type
>(
std::distance(__beg,
556 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
558 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
561 __r->_M_destroy(__a);
562 __throw_exception_again;
564 __r->_M_set_length(__dnew);
565 return __r->_M_refdata();
568 template<
typename _CharT,
typename _Traits,
typename _Alloc>
570 __rc_string_base<_CharT, _Traits, _Alloc>::
571 _S_construct(size_type __n, _CharT __c,
const _Alloc& __a)
573 if (__n == 0 && __a == _Alloc())
574 return _S_empty_rep._M_refcopy();
577 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
579 _S_assign(__r->_M_refdata(), __n, __c);
581 __r->_M_set_length(__n);
582 return __r->_M_refdata();
585 template<
typename _CharT,
typename _Traits,
typename _Alloc>
587 __rc_string_base<_CharT, _Traits, _Alloc>::
588 _M_swap(__rc_string_base& __rcs)
592 if (__rcs._M_is_leaked())
593 __rcs._M_set_sharable();
595 _CharT* __tmp = _M_data();
596 _M_data(__rcs._M_data());
597 __rcs._M_data(__tmp);
601 std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
602 __rcs._M_get_allocator());
605 template<
typename _CharT,
typename _Traits,
typename _Alloc>
607 __rc_string_base<_CharT, _Traits, _Alloc>::
608 _M_assign(
const __rc_string_base& __rcs)
610 if (_M_rep() != __rcs._M_rep())
612 _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
618 template<
typename _CharT,
typename _Traits,
typename _Alloc>
620 __rc_string_base<_CharT, _Traits, _Alloc>::
621 _M_reserve(size_type __res)
624 if (__res < _M_length())
627 if (__res != _M_capacity() || _M_is_shared())
629 _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
630 __res - _M_length());
636 template<
typename _CharT,
typename _Traits,
typename _Alloc>
638 __rc_string_base<_CharT, _Traits, _Alloc>::
639 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
642 const size_type __how_much = _M_length() - __pos - __len1;
644 _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
645 _M_capacity(), _M_get_allocator());
648 _S_copy(__r->_M_refdata(), _M_data(), __pos);
650 _S_copy(__r->_M_refdata() + __pos, __s, __len2);
652 _S_copy(__r->_M_refdata() + __pos + __len2,
653 _M_data() + __pos + __len1, __how_much);
656 _M_data(__r->_M_refdata());
659 template<
typename _CharT,
typename _Traits,
typename _Alloc>
661 __rc_string_base<_CharT, _Traits, _Alloc>::
662 _M_erase(size_type __pos, size_type __n)
664 const size_type __new_size = _M_length() - __n;
665 const size_type __how_much = _M_length() - __pos - __n;
670 _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
674 _S_copy(__r->_M_refdata(), _M_data(), __pos);
676 _S_copy(__r->_M_refdata() + __pos,
677 _M_data() + __pos + __n, __how_much);
680 _M_data(__r->_M_refdata());
682 else if (__how_much && __n)
685 _S_move(_M_data() + __pos,
686 _M_data() + __pos + __n, __how_much);
689 _M_rep()->_M_set_length(__new_size);
694 __rc_string_base<char, std::char_traits<char>,
696 _M_compare(
const __rc_string_base& __rcs)
const
698 if (_M_rep() == __rcs._M_rep())
703 #ifdef _GLIBCXX_USE_WCHAR_T
706 __rc_string_base<wchar_t, std::char_traits<wchar_t>,
708 _M_compare(
const __rc_string_base& __rcs)
const
710 if (_M_rep() == __rcs._M_rep())
716 _GLIBCXX_END_NAMESPACE
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
The "standard" allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manu...
Forward iterators support a superset of input iterator operations.