Home | History | Annotate | Download | only in ext
      1 // Short-string-optimized versatile string base -*- C++ -*-
      2 
      3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
      4 // Free Software Foundation, Inc.
      5 //
      6 // This file is part of the GNU ISO C++ Library.  This library is free
      7 // software; you can redistribute it and/or modify it under the
      8 // terms of the GNU General Public License as published by the
      9 // Free Software Foundation; either version 3, or (at your option)
     10 // any later version.
     11 
     12 // This library is distributed in the hope that it will be useful,
     13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 // GNU General Public License for more details.
     16 
     17 // Under Section 7 of GPL version 3, you are granted additional
     18 // permissions described in the GCC Runtime Library Exception, version
     19 // 3.1, as published by the Free Software Foundation.
     20 
     21 // You should have received a copy of the GNU General Public License and
     22 // a copy of the GCC Runtime Library Exception along with this program;
     23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 // <http://www.gnu.org/licenses/>.
     25 
     26 /** @file ext/sso_string_base.h
     27  *  This is an internal header file, included by other library headers.
     28  *  Do not attempt to use it directly. @headername{ext/vstring.h}
     29  */
     30 
     31 #ifndef _SSO_STRING_BASE_H
     32 #define _SSO_STRING_BASE_H 1
     33 
     34 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
     35 {
     36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     37 
     38   template<typename _CharT, typename _Traits, typename _Alloc>
     39     class __sso_string_base
     40     : protected __vstring_utility<_CharT, _Traits, _Alloc>
     41     {
     42     public:
     43       typedef _Traits					    traits_type;
     44       typedef typename _Traits::char_type		    value_type;
     45 
     46       typedef __vstring_utility<_CharT, _Traits, _Alloc>    _Util_Base;
     47       typedef typename _Util_Base::_CharT_alloc_type        _CharT_alloc_type;
     48       typedef typename _CharT_alloc_type::size_type	    size_type;
     49 
     50     private:
     51       // Data Members:
     52       typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
     53                                                             _M_dataplus;
     54       size_type                                             _M_string_length;
     55 
     56       enum { _S_local_capacity = 15 };
     57 
     58       union
     59       {
     60 	_CharT           _M_local_data[_S_local_capacity + 1];
     61 	size_type        _M_allocated_capacity;
     62       };
     63 
     64       void
     65       _M_data(_CharT* __p)
     66       { _M_dataplus._M_p = __p; }
     67 
     68       void
     69       _M_length(size_type __length)
     70       { _M_string_length = __length; }
     71 
     72       void
     73       _M_capacity(size_type __capacity)
     74       { _M_allocated_capacity = __capacity; }
     75 
     76       bool
     77       _M_is_local() const
     78       { return _M_data() == _M_local_data; }
     79 
     80       // Create & Destroy
     81       _CharT*
     82       _M_create(size_type&, size_type);
     83 
     84       void
     85       _M_dispose()
     86       {
     87 	if (!_M_is_local())
     88 	  _M_destroy(_M_allocated_capacity);
     89 #if __google_stl_debug_string_dangling
     90 	else {
     91           // Wipe local storage for destructed string with 0xCD.
     92           // This mimics what DebugAllocation does to free()d memory.
     93           __builtin_memset(_M_local_data, 0xcd, sizeof(_M_local_data));
     94         }
     95 #endif
     96       }
     97 
     98       void
     99       _M_destroy(size_type __size) throw()
    100       { _M_get_allocator().deallocate(_M_data(), __size + 1); }
    101 
    102       // _M_construct_aux is used to implement the 21.3.1 para 15 which
    103       // requires special behaviour if _InIterator is an integral type
    104       template<typename _InIterator>
    105         void
    106         _M_construct_aux(_InIterator __beg, _InIterator __end,
    107 			 std::__false_type)
    108 	{
    109           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
    110           _M_construct(__beg, __end, _Tag());
    111 	}
    112 
    113       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    114       // 438. Ambiguity in the "do the right thing" clause
    115       template<typename _Integer>
    116         void
    117         _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
    118 	{ _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
    119 
    120       void
    121       _M_construct_aux_2(size_type __req, _CharT __c)
    122       { _M_construct(__req, __c); }
    123 
    124       template<typename _InIterator>
    125         void
    126         _M_construct(_InIterator __beg, _InIterator __end)
    127 	{
    128 	  typedef typename std::__is_integer<_InIterator>::__type _Integral;
    129 	  _M_construct_aux(__beg, __end, _Integral());
    130         }
    131 
    132       // For Input Iterators, used in istreambuf_iterators, etc.
    133       template<typename _InIterator>
    134         void
    135         _M_construct(_InIterator __beg, _InIterator __end,
    136 		     std::input_iterator_tag);
    137 
    138       // For forward_iterators up to random_access_iterators, used for
    139       // string::iterator, _CharT*, etc.
    140       template<typename _FwdIterator>
    141         void
    142         _M_construct(_FwdIterator __beg, _FwdIterator __end,
    143 		     std::forward_iterator_tag);
    144 
    145       void
    146       _M_construct(size_type __req, _CharT __c);
    147 
    148     public:
    149       size_type
    150       _M_max_size() const
    151       { return (_M_get_allocator().max_size() - 1) / 2; }
    152 
    153       _CharT*
    154       _M_data() const
    155       { return _M_dataplus._M_p; }
    156 
    157       size_type
    158       _M_length() const
    159       { return _M_string_length; }
    160 
    161       size_type
    162       _M_capacity() const
    163       {
    164 	return _M_is_local() ? size_type(_S_local_capacity)
    165 	                     : _M_allocated_capacity;
    166       }
    167 
    168       bool
    169       _M_is_shared() const
    170       { return false; }
    171 
    172       void
    173       _M_set_leaked() { }
    174 
    175       void
    176       _M_leak() { }
    177 
    178       void
    179       _M_set_length_no_wipe(size_type __n)
    180       {
    181 	_M_length(__n);
    182 	traits_type::assign(_M_data()[__n], _CharT());
    183       }
    184 
    185       void
    186       _M_set_length(size_type __n)
    187       {
    188 #if __google_stl_debug_string_dangling
    189 	if (__n + 1 < _M_length())
    190 	  {
    191 	    // Wipe the storage with 0xCD.
    192 	    // Also wipes the old NUL terminator.
    193 	    __builtin_memset(_M_data() + __n + 1, 0xcd, _M_length() - __n);
    194 	  }
    195 #endif
    196 	  _M_set_length_no_wipe(__n);
    197       }
    198 
    199       __sso_string_base()
    200       : _M_dataplus(_M_local_data)
    201       { _M_set_length_no_wipe(0); }
    202 
    203       __sso_string_base(const _Alloc& __a);
    204 
    205       __sso_string_base(const __sso_string_base& __rcs);
    206 
    207 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    208       __sso_string_base(__sso_string_base&& __rcs);
    209 #endif
    210 
    211       __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
    212 
    213       template<typename _InputIterator>
    214         __sso_string_base(_InputIterator __beg, _InputIterator __end,
    215 			  const _Alloc& __a);
    216 
    217       ~__sso_string_base()
    218       { _M_dispose(); }
    219 
    220       _CharT_alloc_type&
    221       _M_get_allocator()
    222       { return _M_dataplus; }
    223 
    224       const _CharT_alloc_type&
    225       _M_get_allocator() const
    226       { return _M_dataplus; }
    227 
    228       void
    229       _M_swap(__sso_string_base& __rcs);
    230 
    231       void
    232       _M_assign(const __sso_string_base& __rcs);
    233 
    234       void
    235       _M_reserve(size_type __res);
    236 
    237       void
    238       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
    239 		size_type __len2);
    240 
    241       void
    242       _M_erase(size_type __pos, size_type __n);
    243 
    244       void
    245       _M_clear()
    246       { _M_set_length(0); }
    247 
    248       bool
    249       _M_compare(const __sso_string_base&) const
    250       { return false; }
    251     };
    252 
    253   template<typename _CharT, typename _Traits, typename _Alloc>
    254     void
    255     __sso_string_base<_CharT, _Traits, _Alloc>::
    256     _M_swap(__sso_string_base& __rcs)
    257     {
    258       if (this == &__rcs)
    259 	return;
    260 
    261       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    262       // 431. Swapping containers with unequal allocators.
    263       std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
    264 						     __rcs._M_get_allocator());
    265 
    266       if (_M_is_local())
    267 	if (__rcs._M_is_local())
    268 	  {
    269 	    if (_M_length() && __rcs._M_length())
    270 	      {
    271 		_CharT __tmp_data[_S_local_capacity + 1];
    272 		traits_type::copy(__tmp_data, __rcs._M_local_data,
    273 				  _S_local_capacity + 1);
    274 		traits_type::copy(__rcs._M_local_data, _M_local_data,
    275 				  _S_local_capacity + 1);
    276 		traits_type::copy(_M_local_data, __tmp_data,
    277 				  _S_local_capacity + 1);
    278 	      }
    279 	    else if (__rcs._M_length())
    280 	      {
    281 		traits_type::copy(_M_local_data, __rcs._M_local_data,
    282 				  _S_local_capacity + 1);
    283 		_M_length(__rcs._M_length());
    284 		__rcs._M_set_length(0);
    285 		return;
    286 	      }
    287 	    else if (_M_length())
    288 	      {
    289 		traits_type::copy(__rcs._M_local_data, _M_local_data,
    290 				  _S_local_capacity + 1);
    291 		__rcs._M_length(_M_length());
    292 		_M_set_length(0);
    293 		return;
    294 	      }
    295 	  }
    296 	else
    297 	  {
    298 	    const size_type __tmp_capacity = __rcs._M_allocated_capacity;
    299 	    traits_type::copy(__rcs._M_local_data, _M_local_data,
    300 			      _S_local_capacity + 1);
    301 	    _M_data(__rcs._M_data());
    302 	    __rcs._M_data(__rcs._M_local_data);
    303 	    _M_capacity(__tmp_capacity);
    304 	  }
    305       else
    306 	{
    307 	  const size_type __tmp_capacity = _M_allocated_capacity;
    308 	  if (__rcs._M_is_local())
    309 	    {
    310 	      traits_type::copy(_M_local_data, __rcs._M_local_data,
    311 				_S_local_capacity + 1);
    312 	      __rcs._M_data(_M_data());
    313 	      _M_data(_M_local_data);
    314 	    }
    315 	  else
    316 	    {
    317 	      _CharT* __tmp_ptr = _M_data();
    318 	      _M_data(__rcs._M_data());
    319 	      __rcs._M_data(__tmp_ptr);
    320 	      _M_capacity(__rcs._M_allocated_capacity);
    321 	    }
    322 	  __rcs._M_capacity(__tmp_capacity);
    323 	}
    324 
    325       const size_type __tmp_length = _M_length();
    326       _M_length(__rcs._M_length());
    327       __rcs._M_length(__tmp_length);
    328     }
    329 
    330   template<typename _CharT, typename _Traits, typename _Alloc>
    331     _CharT*
    332     __sso_string_base<_CharT, _Traits, _Alloc>::
    333     _M_create(size_type& __capacity, size_type __old_capacity)
    334     {
    335       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    336       // 83.  String::npos vs. string::max_size()
    337       if (__capacity > _M_max_size())
    338 	std::__throw_length_error(__N("__sso_string_base::_M_create"));
    339 
    340       // The below implements an exponential growth policy, necessary to
    341       // meet amortized linear time requirements of the library: see
    342       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
    343       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
    344 	{
    345 	  __capacity = 2 * __old_capacity;
    346 	  // Never allocate a string bigger than max_size.
    347 	  if (__capacity > _M_max_size())
    348 	    __capacity = _M_max_size();
    349 	}
    350 
    351       // NB: Need an array of char_type[__capacity], plus a terminating
    352       // null char_type() element.
    353       return _M_get_allocator().allocate(__capacity + 1);
    354     }
    355 
    356   template<typename _CharT, typename _Traits, typename _Alloc>
    357     __sso_string_base<_CharT, _Traits, _Alloc>::
    358     __sso_string_base(const _Alloc& __a)
    359     : _M_dataplus(__a, _M_local_data)
    360     { _M_set_length_no_wipe(0); }
    361 
    362   template<typename _CharT, typename _Traits, typename _Alloc>
    363     __sso_string_base<_CharT, _Traits, _Alloc>::
    364     __sso_string_base(const __sso_string_base& __rcs)
    365     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
    366     { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
    367 
    368 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    369   template<typename _CharT, typename _Traits, typename _Alloc>
    370     __sso_string_base<_CharT, _Traits, _Alloc>::
    371     __sso_string_base(__sso_string_base&& __rcs)
    372     : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
    373     {
    374       if (__rcs._M_is_local())
    375 	{
    376 	  if (__rcs._M_length())
    377 	    traits_type::copy(_M_local_data, __rcs._M_local_data,
    378 			      _S_local_capacity + 1);
    379 	}
    380       else
    381 	{
    382 	  _M_data(__rcs._M_data());
    383 	  _M_capacity(__rcs._M_allocated_capacity);
    384 	}
    385 
    386       _M_length(__rcs._M_length());
    387       __rcs._M_length(0);
    388       __rcs._M_data(__rcs._M_local_data);
    389     }
    390 #endif
    391 
    392   template<typename _CharT, typename _Traits, typename _Alloc>
    393     __sso_string_base<_CharT, _Traits, _Alloc>::
    394     __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
    395     : _M_dataplus(__a, _M_local_data)
    396     { _M_construct(__n, __c); }
    397 
    398   template<typename _CharT, typename _Traits, typename _Alloc>
    399     template<typename _InputIterator>
    400     __sso_string_base<_CharT, _Traits, _Alloc>::
    401     __sso_string_base(_InputIterator __beg, _InputIterator __end,
    402 		      const _Alloc& __a)
    403     : _M_dataplus(__a, _M_local_data)
    404     { _M_construct(__beg, __end); }
    405 
    406   // NB: This is the special case for Input Iterators, used in
    407   // istreambuf_iterators, etc.
    408   // Input Iterators have a cost structure very different from
    409   // pointers, calling for a different coding style.
    410   template<typename _CharT, typename _Traits, typename _Alloc>
    411     template<typename _InIterator>
    412       void
    413       __sso_string_base<_CharT, _Traits, _Alloc>::
    414       _M_construct(_InIterator __beg, _InIterator __end,
    415 		   std::input_iterator_tag)
    416       {
    417 	size_type __len = 0;
    418 	size_type __capacity = size_type(_S_local_capacity);
    419 
    420 	while (__beg != __end && __len < __capacity)
    421 	  {
    422 	    _M_data()[__len++] = *__beg;
    423 	    ++__beg;
    424 	  }
    425 
    426 	__try
    427 	  {
    428 	    while (__beg != __end)
    429 	      {
    430 		if (__len == __capacity)
    431 		  {
    432 		    // Allocate more space.
    433 		    __capacity = __len + 1;
    434 		    _CharT* __another = _M_create(__capacity, __len);
    435 		    this->_S_copy(__another, _M_data(), __len);
    436 		    _M_dispose();
    437 		    _M_data(__another);
    438 		    _M_capacity(__capacity);
    439 		  }
    440 		_M_data()[__len++] = *__beg;
    441 		++__beg;
    442 	      }
    443 	  }
    444 	__catch(...)
    445 	  {
    446 	    _M_dispose();
    447 	    __throw_exception_again;
    448 	  }
    449 
    450 	_M_set_length_no_wipe(__len);
    451       }
    452 
    453   template<typename _CharT, typename _Traits, typename _Alloc>
    454     template<typename _InIterator>
    455       void
    456       __sso_string_base<_CharT, _Traits, _Alloc>::
    457       _M_construct(_InIterator __beg, _InIterator __end,
    458 		   std::forward_iterator_tag)
    459       {
    460 	// NB: Not required, but considered best practice.
    461 	if (__is_null_pointer(__beg) && __beg != __end)
    462 	  std::__throw_logic_error(__N("__sso_string_base::"
    463 				       "_M_construct null not valid"));
    464 
    465 	size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
    466 
    467 	if (__dnew > size_type(_S_local_capacity))
    468 	  {
    469 	    _M_data(_M_create(__dnew, size_type(0)));
    470 	    _M_capacity(__dnew);
    471 	  }
    472 
    473 	// Check for out_of_range and length_error exceptions.
    474 	__try
    475 	  { this->_S_copy_chars(_M_data(), __beg, __end); }
    476 	__catch(...)
    477 	  {
    478 	    _M_dispose();
    479 	    __throw_exception_again;
    480 	  }
    481 
    482 	_M_set_length_no_wipe(__dnew);
    483       }
    484 
    485   template<typename _CharT, typename _Traits, typename _Alloc>
    486     void
    487     __sso_string_base<_CharT, _Traits, _Alloc>::
    488     _M_construct(size_type __n, _CharT __c)
    489     {
    490       if (__n > size_type(_S_local_capacity))
    491 	{
    492 	  _M_data(_M_create(__n, size_type(0)));
    493 	  _M_capacity(__n);
    494 	}
    495 
    496       if (__n)
    497 	this->_S_assign(_M_data(), __n, __c);
    498 
    499       _M_set_length_no_wipe(__n);
    500     }
    501 
    502   template<typename _CharT, typename _Traits, typename _Alloc>
    503     void
    504     __sso_string_base<_CharT, _Traits, _Alloc>::
    505     _M_assign(const __sso_string_base& __rcs)
    506     {
    507       if (this != &__rcs)
    508 	{
    509 	  const size_type __rsize = __rcs._M_length();
    510 	  const size_type __capacity = _M_capacity();
    511 
    512 	  if (__rsize > __capacity)
    513 	    {
    514 	      size_type __new_capacity = __rsize;
    515 	      _CharT* __tmp = _M_create(__new_capacity, __capacity);
    516 	      _M_dispose();
    517 	      _M_data(__tmp);
    518 	      _M_capacity(__new_capacity);
    519 	    }
    520 
    521 	  if (__rsize)
    522 	    this->_S_copy(_M_data(), __rcs._M_data(), __rsize);
    523 
    524 	  _M_set_length(__rsize);
    525 	}
    526     }
    527 
    528   template<typename _CharT, typename _Traits, typename _Alloc>
    529     void
    530     __sso_string_base<_CharT, _Traits, _Alloc>::
    531     _M_reserve(size_type __res)
    532     {
    533       // Make sure we don't shrink below the current size.
    534       if (__res < _M_length())
    535 	__res = _M_length();
    536 
    537       const size_type __capacity = _M_capacity();
    538       if (__res != __capacity)
    539 	{
    540 	  if (__res > __capacity
    541 	      || __res > size_type(_S_local_capacity))
    542 	    {
    543 	      _CharT* __tmp = _M_create(__res, __capacity);
    544 	      this->_S_copy(__tmp, _M_data(), _M_length() + 1);
    545 	      _M_dispose();
    546 	      _M_data(__tmp);
    547 	      _M_capacity(__res);
    548 	    }
    549 	  else if (!_M_is_local())
    550 	    {
    551 	      this->_S_copy(_M_local_data, _M_data(), _M_length() + 1);
    552 	      _M_destroy(__capacity);
    553 	      _M_data(_M_local_data);
    554 	    }
    555 	}
    556     }
    557 
    558   template<typename _CharT, typename _Traits, typename _Alloc>
    559     void
    560     __sso_string_base<_CharT, _Traits, _Alloc>::
    561     _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
    562 	      size_type __len2)
    563     {
    564       const size_type __how_much = _M_length() - __pos - __len1;
    565 
    566       size_type __new_capacity = _M_length() + __len2 - __len1;
    567       _CharT* __r = _M_create(__new_capacity, _M_capacity());
    568 
    569       if (__pos)
    570 	this->_S_copy(__r, _M_data(), __pos);
    571       if (__s && __len2)
    572 	this->_S_copy(__r + __pos, __s, __len2);
    573       if (__how_much)
    574 	this->_S_copy(__r + __pos + __len2,
    575 		      _M_data() + __pos + __len1, __how_much);
    576 
    577       _M_dispose();
    578       _M_data(__r);
    579       _M_capacity(__new_capacity);
    580     }
    581 
    582   template<typename _CharT, typename _Traits, typename _Alloc>
    583     void
    584     __sso_string_base<_CharT, _Traits, _Alloc>::
    585     _M_erase(size_type __pos, size_type __n)
    586     {
    587       const size_type __how_much = _M_length() - __pos - __n;
    588 
    589       if (__how_much && __n)
    590 	this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
    591 
    592       _M_set_length(_M_length() - __n);
    593     }
    594 
    595 _GLIBCXX_END_NAMESPACE_VERSION
    596 } // namespace
    597 
    598 #endif /* _SSO_STRING_BASE_H */
    599