// Short-string-optimized versatile string base -*- C++ -*-
-// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+// Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 2, or (at your option)
+// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING. If not, write to the Free
-// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
-// As a special exception, you may use this file as part of a free software
-// library without restriction. Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License. This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
/** @file ext/sso_string_base.h
* This file is a GNU extension to the Standard C++ Library.
typedef typename _CharT_alloc_type::size_type size_type;
private:
- // The maximum number of individual char_type elements of an
- // individual string is determined by _S_max_size. This is the
- // value that will be returned by max_size(). (Whereas npos
- // is the maximum number of bytes the allocator can allocate.)
- // If one was to divvy up the theoretical largest size string,
- // with a terminating character and m _CharT elements, it'd
- // look like this:
- // npos = m * sizeof(_CharT) + sizeof(_CharT)
- // Solving for m:
- // m = npos / sizeof(_CharT) - 1
- // In addition, this implementation halfs this amount.
- enum { _S_max_size = (((static_cast<size_type>(-1)
- / sizeof(_CharT)) - 1) / 2) };
-
- // Data Members (private):
+ // Data Members:
typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
_M_dataplus;
size_type _M_string_length;
}
void
- _M_destroy(size_type) throw();
+ _M_destroy(size_type __size) throw()
+ { _M_get_allocator().deallocate(_M_data(), __size + 1); }
// _M_construct_aux is used to implement the 21.3.1 para 15 which
// requires special behaviour if _InIterator is an integral type
template<typename _InIterator>
void
- _M_construct_aux(_InIterator __beg, _InIterator __end, __false_type)
+ _M_construct_aux(_InIterator __beg, _InIterator __end,
+ std::__false_type)
{
typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
_M_construct(__beg, __end, _Tag());
}
- template<typename _InIterator>
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 438. Ambiguity in the "do the right thing" clause
+ template<typename _Integer>
void
- _M_construct_aux(_InIterator __beg, _InIterator __end, __true_type)
- { _M_construct(static_cast<size_type>(__beg),
- static_cast<value_type>(__end)); }
+ _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
+ { _M_construct(static_cast<size_type>(__beg), __end); }
template<typename _InIterator>
void
public:
size_type
_M_max_size() const
- { return size_type(_S_max_size); }
+ { return (_M_get_allocator().max_size() - 1) / 2; }
_CharT*
_M_data() const
}
__sso_string_base()
- : _M_dataplus(_Alloc(), _M_local_data)
+ : _M_dataplus(_M_local_data)
{ _M_set_length(0); }
__sso_string_base(const _Alloc& __a);
__sso_string_base(const __sso_string_base& __rcs);
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+ __sso_string_base(__sso_string_base&& __rcs);
+#endif
+
__sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
template<typename _InputIterator>
template<typename _CharT, typename _Traits, typename _Alloc>
void
__sso_string_base<_CharT, _Traits, _Alloc>::
- _M_destroy(size_type __size) throw()
- { _M_dataplus._CharT_alloc_type::deallocate(_M_data(), __size + 1); }
-
- template<typename _CharT, typename _Traits, typename _Alloc>
- void
- __sso_string_base<_CharT, _Traits, _Alloc>::
_M_swap(__sso_string_base& __rcs)
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 83. String::npos vs. string::max_size()
- if (__capacity > size_type(_S_max_size))
+ if (__capacity > _M_max_size())
std::__throw_length_error(__N("__sso_string_base::_M_create"));
// The below implements an exponential growth policy, necessary to
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
{
__capacity = 2 * __old_capacity;
- // Never allocate a string bigger than _S_max_size.
- if (__capacity > size_type(_S_max_size))
- __capacity = size_type(_S_max_size);
+ // Never allocate a string bigger than max_size.
+ if (__capacity > _M_max_size())
+ __capacity = _M_max_size();
}
// NB: Need an array of char_type[__capacity], plus a terminating
// null char_type() element.
- return _M_dataplus._CharT_alloc_type::allocate(__capacity + 1);
+ return _M_get_allocator().allocate(__capacity + 1);
}
template<typename _CharT, typename _Traits, typename _Alloc>
: _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
{ _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+ template<typename _CharT, typename _Traits, typename _Alloc>
+ __sso_string_base<_CharT, _Traits, _Alloc>::
+ __sso_string_base(__sso_string_base&& __rcs)
+ : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
+ {
+ if (__rcs._M_is_local())
+ {
+ if (__rcs._M_length())
+ traits_type::copy(_M_local_data, __rcs._M_local_data,
+ _S_local_capacity + 1);
+ }
+ else
+ {
+ _M_data(__rcs._M_data());
+ _M_capacity(__rcs._M_allocated_capacity);
+ }
+
+ _M_length(__rcs._M_length());
+ __rcs._M_length(0);
+ __rcs._M_data(__rcs._M_local_data);
+ }
+#endif
+
template<typename _CharT, typename _Traits, typename _Alloc>
__sso_string_base<_CharT, _Traits, _Alloc>::
__sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
++__beg;
}
- try
+ __try
{
while (__beg != __end)
{
++__beg;
}
}
- catch(...)
+ __catch(...)
{
_M_dispose();
__throw_exception_again;
std::forward_iterator_tag)
{
// NB: Not required, but considered best practice.
- if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
+ if (__is_null_pointer(__beg) && __beg != __end)
std::__throw_logic_error(__N("__sso_string_base::"
"_M_construct NULL not valid"));
}
// Check for out_of_range and length_error exceptions.
- try
+ __try
{ _S_copy_chars(_M_data(), __beg, __end); }
- catch(...)
+ __catch(...)
{
_M_dispose();
__throw_exception_again;
void
__sso_string_base<_CharT, _Traits, _Alloc>::
_M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
- const size_type __len2)
+ size_type __len2)
{
const size_type __how_much = _M_length() - __pos - __len1;
_M_set_length(_M_length() - __n);
}
- template<>
- inline bool
- __sso_string_base<char, std::char_traits<char>,
- std::allocator<char> >::
- _M_compare(const __sso_string_base& __rcs) const
- {
- if (this == &__rcs)
- return true;
- return false;
- }
-
- template<>
- inline bool
- __sso_string_base<wchar_t, std::char_traits<wchar_t>,
- std::allocator<wchar_t> >::
- _M_compare(const __sso_string_base& __rcs) const
- {
- if (this == &__rcs)
- return true;
- return false;
- }
-
_GLIBCXX_END_NAMESPACE
#endif /* _SSO_STRING_BASE_H */