30 #ifndef _SSO_STRING_BASE_H 
   31 #define _SSO_STRING_BASE_H 1 
   33 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
 
   35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   37   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
   38     class __sso_string_base
 
   39     : 
protected __vstring_utility<_CharT, _Traits, _Alloc>
 
   42       typedef _Traits                       traits_type;
 
   43       typedef typename _Traits::char_type           value_type;
 
   45       typedef __vstring_utility<_CharT, _Traits, _Alloc>    _Util_Base;
 
   46       typedef typename _Util_Base::_CharT_alloc_type        _CharT_alloc_type;
 
   47       typedef typename _CharT_alloc_type::size_type     size_type;
 
   51       typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
 
   53       size_type                                             _M_string_length;
 
   55       enum { _S_local_capacity = 15 };
 
   59     _CharT           _M_local_data[_S_local_capacity + 1];
 
   60     size_type        _M_allocated_capacity;
 
   65       { _M_dataplus._M_p = __p; }
 
   68       _M_length(size_type __length)
 
   69       { _M_string_length = __length; }
 
   72       _M_capacity(size_type __capacity)
 
   73       { _M_allocated_capacity = __capacity; }
 
   77       { 
return _M_data() == _M_local_data; }
 
   81       _M_create(size_type&, size_type);
 
   87       _M_destroy(_M_allocated_capacity);
 
   91       _M_destroy(size_type __size) 
throw()
 
   92       { _M_get_allocator().deallocate(_M_data(), __size + 1); }
 
   96       template<
typename _InIterator>
 
   98         _M_construct_aux(_InIterator __beg, _InIterator __end, 
 
  101           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
 
  102           _M_construct(__beg, __end, _Tag());
 
  107       template<
typename _Integer>
 
  109         _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
 
  110     { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
 
  113       _M_construct_aux_2(size_type __req, _CharT __c)
 
  114       { _M_construct(__req, __c); }
 
  116       template<
typename _InIterator>
 
  118         _M_construct(_InIterator __beg, _InIterator __end)
 
  120       typedef typename std::__is_integer<_InIterator>::__type _Integral;
 
  121       _M_construct_aux(__beg, __end, _Integral());
 
  125       template<
typename _InIterator>
 
  127         _M_construct(_InIterator __beg, _InIterator __end,
 
  132       template<
typename _FwdIterator>
 
  134         _M_construct(_FwdIterator __beg, _FwdIterator __end,
 
  138       _M_construct(size_type __req, _CharT __c);
 
  143       { 
return (_M_get_allocator().max_size() - 1) / 2; }
 
  147       { 
return _M_dataplus._M_p; }
 
  151       { 
return _M_string_length; }
 
  156     return _M_is_local() ? size_type(_S_local_capacity)
 
  157                          : _M_allocated_capacity; 
 
  171       _M_set_length(size_type __n)
 
  174     traits_type::assign(_M_data()[__n], _CharT());
 
  178       : _M_dataplus(_M_local_data)
 
  179       { _M_set_length(0); }
 
  181       __sso_string_base(
const _Alloc& __a);
 
  183       __sso_string_base(
const __sso_string_base& __rcs);
 
  185 #if __cplusplus >= 201103L 
  186       __sso_string_base(__sso_string_base&& __rcs);
 
  189       __sso_string_base(size_type __n, _CharT __c, 
const _Alloc& __a);
 
  191       template<
typename _InputIterator>
 
  192         __sso_string_base(_InputIterator __beg, _InputIterator __end,
 
  200       { 
return _M_dataplus; }
 
  202       const _CharT_alloc_type&
 
  203       _M_get_allocator()
 const 
  204       { 
return _M_dataplus; }
 
  207       _M_swap(__sso_string_base& __rcs);
 
  210       _M_assign(
const __sso_string_base& __rcs);
 
  213       _M_reserve(size_type __res);
 
  216       _M_mutate(size_type __pos, size_type __len1, 
const _CharT* __s,
 
  220       _M_erase(size_type __pos, size_type __n);
 
  224       { _M_set_length(0); }
 
  227       _M_compare(
const __sso_string_base&)
 const 
  231   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  233     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  234     _M_swap(__sso_string_base& __rcs)
 
  241       std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
 
  242                              __rcs._M_get_allocator());
 
  245     if (__rcs._M_is_local())
 
  247         if (_M_length() && __rcs._M_length())
 
  249         _CharT __tmp_data[_S_local_capacity + 1];
 
  250         traits_type::copy(__tmp_data, __rcs._M_local_data,
 
  251                   _S_local_capacity + 1);
 
  252         traits_type::copy(__rcs._M_local_data, _M_local_data,
 
  253                   _S_local_capacity + 1);
 
  254         traits_type::copy(_M_local_data, __tmp_data,
 
  255                   _S_local_capacity + 1);
 
  257         else if (__rcs._M_length())
 
  259         traits_type::copy(_M_local_data, __rcs._M_local_data,
 
  260                   _S_local_capacity + 1);
 
  261         _M_length(__rcs._M_length());
 
  262         __rcs._M_set_length(0);
 
  265         else if (_M_length())
 
  267         traits_type::copy(__rcs._M_local_data, _M_local_data,
 
  268                   _S_local_capacity + 1);
 
  269         __rcs._M_length(_M_length());
 
  276         const size_type __tmp_capacity = __rcs._M_allocated_capacity;
 
  277         traits_type::copy(__rcs._M_local_data, _M_local_data,
 
  278                   _S_local_capacity + 1);
 
  279         _M_data(__rcs._M_data());
 
  280         __rcs._M_data(__rcs._M_local_data);
 
  281         _M_capacity(__tmp_capacity);
 
  285       const size_type __tmp_capacity = _M_allocated_capacity;
 
  286       if (__rcs._M_is_local())
 
  288           traits_type::copy(_M_local_data, __rcs._M_local_data,
 
  289                 _S_local_capacity + 1);
 
  290           __rcs._M_data(_M_data());
 
  291           _M_data(_M_local_data);
 
  295           _CharT* __tmp_ptr = _M_data();
 
  296           _M_data(__rcs._M_data());
 
  297           __rcs._M_data(__tmp_ptr);
 
  298           _M_capacity(__rcs._M_allocated_capacity);
 
  300       __rcs._M_capacity(__tmp_capacity);
 
  303       const size_type __tmp_length = _M_length();
 
  304       _M_length(__rcs._M_length());
 
  305       __rcs._M_length(__tmp_length);
 
  308   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  310     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  311     _M_create(size_type& __capacity, size_type __old_capacity)
 
  315       if (__capacity > _M_max_size())
 
  316     std::__throw_length_error(__N(
"__sso_string_base::_M_create"));
 
  321       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
 
  323       __capacity = 2 * __old_capacity;
 
  325       if (__capacity > _M_max_size())
 
  326         __capacity = _M_max_size();
 
  331       return _M_get_allocator().allocate(__capacity + 1);
 
  334   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  335     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  336     __sso_string_base(
const _Alloc& __a)
 
  337     : _M_dataplus(__a, _M_local_data)
 
  338     { _M_set_length(0); }
 
  340   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  341     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  342     __sso_string_base(
const __sso_string_base& __rcs)
 
  343     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
 
  344     { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
 
  346 #if __cplusplus >= 201103L 
  347   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  348     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  349     __sso_string_base(__sso_string_base&& __rcs)
 
  350     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
 
  352       if (__rcs._M_is_local())
 
  354       if (__rcs._M_length())
 
  355         traits_type::copy(_M_local_data, __rcs._M_local_data,
 
  356                   _S_local_capacity + 1);
 
  360       _M_data(__rcs._M_data());
 
  361       _M_capacity(__rcs._M_allocated_capacity);
 
  364       _M_set_length(__rcs._M_length());
 
  365       __rcs._M_data(__rcs._M_local_data);
 
  366       __rcs._M_set_length(0);
 
  370   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  371     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  372     __sso_string_base(size_type __n, _CharT __c, 
const _Alloc& __a)
 
  373     : _M_dataplus(__a, _M_local_data)
 
  374     { _M_construct(__n, __c); }
 
  376   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  377     template<
typename _InputIterator>
 
  378     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  379     __sso_string_base(_InputIterator __beg, _InputIterator __end,
 
  381     : _M_dataplus(__a, _M_local_data)
 
  382     { _M_construct(__beg, __end); }
 
  388   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  389     template<
typename _InIterator>
 
  391       __sso_string_base<_CharT, _Traits, _Alloc>::
 
  392       _M_construct(_InIterator __beg, _InIterator __end,
 
  396     size_type __capacity = size_type(_S_local_capacity);
 
  398     while (__beg != __end && __len < __capacity)
 
  400         _M_data()[__len++] = *__beg;
 
  406         while (__beg != __end)
 
  408         if (__len == __capacity)
 
  411             __capacity = __len + 1;
 
  412             _CharT* __another = _M_create(__capacity, __len);
 
  413             this->_S_copy(__another, _M_data(), __len);
 
  416             _M_capacity(__capacity);
 
  418         _M_data()[__len++] = *__beg;
 
  425         __throw_exception_again;
 
  428     _M_set_length(__len);
 
  431   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  432     template<
typename _InIterator>
 
  434       __sso_string_base<_CharT, _Traits, _Alloc>::
 
  435       _M_construct(_InIterator __beg, _InIterator __end,
 
  439     if (__is_null_pointer(__beg) && __beg != __end)
 
  440       std::__throw_logic_error(__N(
"__sso_string_base::" 
  441                        "_M_construct null not valid"));
 
  443     size_type __dnew = 
static_cast<size_type
>(
std::distance(__beg, __end));
 
  445     if (__dnew > size_type(_S_local_capacity))
 
  447         _M_data(_M_create(__dnew, size_type(0)));
 
  453       { this->_S_copy_chars(_M_data(), __beg, __end); }
 
  457         __throw_exception_again;
 
  460     _M_set_length(__dnew);
 
  463   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  465     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  466     _M_construct(size_type __n, _CharT __c)
 
  468       if (__n > size_type(_S_local_capacity))
 
  470       _M_data(_M_create(__n, size_type(0)));
 
  475     this->_S_assign(_M_data(), __n, __c);
 
  480   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  482     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  483     _M_assign(
const __sso_string_base& __rcs)
 
  487       const size_type __rsize = __rcs._M_length();
 
  488       const size_type __capacity = _M_capacity();
 
  490       if (__rsize > __capacity)
 
  492           size_type __new_capacity = __rsize;
 
  493           _CharT* __tmp = _M_create(__new_capacity, __capacity);
 
  496           _M_capacity(__new_capacity);
 
  500         this->_S_copy(_M_data(), __rcs._M_data(), __rsize);
 
  502       _M_set_length(__rsize);
 
  506   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  508     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  509     _M_reserve(size_type __res)
 
  512       if (__res < _M_length())
 
  515       const size_type __capacity = _M_capacity();
 
  516       if (__res != __capacity)
 
  518       if (__res > __capacity
 
  519           || __res > size_type(_S_local_capacity))
 
  521           _CharT* __tmp = _M_create(__res, __capacity);
 
  522           this->_S_copy(__tmp, _M_data(), _M_length() + 1);
 
  527       else if (!_M_is_local())
 
  529           this->_S_copy(_M_local_data, _M_data(), _M_length() + 1);
 
  530           _M_destroy(__capacity);
 
  531           _M_data(_M_local_data);
 
  536   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  538     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  539     _M_mutate(size_type __pos, size_type __len1, 
const _CharT* __s,
 
  542       const size_type __how_much = _M_length() - __pos - __len1;
 
  544       size_type __new_capacity = _M_length() + __len2 - __len1;
 
  545       _CharT* __r = _M_create(__new_capacity, _M_capacity());
 
  548     this->_S_copy(__r, _M_data(), __pos);
 
  550     this->_S_copy(__r + __pos, __s, __len2);
 
  552     this->_S_copy(__r + __pos + __len2,
 
  553               _M_data() + __pos + __len1, __how_much);
 
  557       _M_capacity(__new_capacity);
 
  560   template<
typename _CharT, 
typename _Traits, 
typename _Alloc>
 
  562     __sso_string_base<_CharT, _Traits, _Alloc>::
 
  563     _M_erase(size_type __pos, size_type __n)
 
  565       const size_type __how_much = _M_length() - __pos - __n;
 
  567       if (__how_much && __n)
 
  568     this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
 
  570       _M_set_length(_M_length() - __n);
 
  573 _GLIBCXX_END_NAMESPACE_VERSION
 
GNU extensions for public use. 
Forward iterators support a superset of input iterator operations. 
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.