summaryrefslogtreecommitdiff
path: root/boost/container/string.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/container/string.hpp')
-rw-r--r--boost/container/string.hpp1870
1 files changed, 933 insertions, 937 deletions
diff --git a/boost/container/string.hpp b/boost/container/string.hpp
index 2a64cec428..1c3cf3bbc8 100644
--- a/boost/container/string.hpp
+++ b/boost/container/string.hpp
@@ -1,58 +1,43 @@
//////////////////////////////////////////////////////////////////////////////
//
-// (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
+// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
-// Copyright (c) 1996,1997
-// Silicon Graphics Computer Systems, Inc.
-//
-// Permission to use, copy, modify, distribute and sell this software
-// and its documentation for any purpose is hereby granted without fee,
-// provided that the above copyright notice appear in all copies and
-// that both that copyright notice and this permission notice appear
-// in supporting documentation. Silicon Graphics makes no
-// representations about the suitability of this software for any
-// purpose. It is provided "as is" without express or implied warranty.
-//
-//
-// Copyright (c) 1994
-// Hewlett-Packard Company
-//
-// Permission to use, copy, modify, distribute and sell this software
-// and its documentation for any purpose is hereby granted without fee,
-// provided that the above copyright notice appear in all copies and
-// that both that copyright notice and this permission notice appear
-// in supporting documentation. Hewlett-Packard Company makes no
-// representations about the suitability of this software for any
-// purpose. It is provided "as is" without express or implied warranty.
#ifndef BOOST_CONTAINER_STRING_HPP
#define BOOST_CONTAINER_STRING_HPP
+#if defined(_MSC_VER)
+# pragma once
+#endif
+
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/container_fwd.hpp>
+#include <boost/container/throw_exception.hpp>
#include <boost/container/detail/utilities.hpp>
#include <boost/container/detail/iterators.hpp>
#include <boost/container/detail/algorithms.hpp>
#include <boost/container/detail/version_type.hpp>
#include <boost/container/detail/allocation_type.hpp>
#include <boost/container/allocator_traits.hpp>
+#include <boost/container/detail/allocator_version_traits.hpp>
#include <boost/container/detail/mpl.hpp>
-#include <boost/move/move.hpp>
+#include <boost/move/utility_core.hpp>
#include <boost/static_assert.hpp>
#include <boost/functional/hash.hpp>
+#include <boost/intrusive/pointer_traits.hpp>
+#include <boost/core/no_exceptions_support.hpp>
#include <functional>
#include <string>
-#include <stdexcept>
-#include <utility>
+#include <utility>
#include <iterator>
#include <memory>
#include <algorithm>
@@ -64,43 +49,39 @@
#include <cstddef>
#include <climits>
#include <boost/container/detail/type_traits.hpp>
-#include <boost/detail/no_exceptions_support.hpp>
#include <boost/type_traits/has_trivial_destructor.hpp>
#include <boost/aligned_storage.hpp>
+#include <boost/move/traits.hpp>
-#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
-namespace boost {
-namespace container {
-#else
namespace boost {
namespace container {
-#endif
-/// @cond
+#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
namespace container_detail {
// ------------------------------------------------------------
-// Class basic_string_base.
+// Class basic_string_base.
// basic_string_base is a helper class that makes it it easier to write
// an exception-safe version of basic_string. The constructor allocates,
// but does not initialize, a block of memory. The destructor
// deallocates, but does not destroy elements within, a block of
-// memory. The destructor assumes that the memory either is the internal buffer,
-// or else points to a block of memory that was allocated using _String_base's
+// memory. The destructor assumes that the memory either is the internal buffer,
+// or else points to a block of memory that was allocated using string_base's
// allocator and whose size is this->m_storage.
-template <class A>
+template <class Allocator>
class basic_string_base
{
- BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_string_base)
+ basic_string_base & operator=(const basic_string_base &);
+ basic_string_base(const basic_string_base &);
- typedef allocator_traits<A> allocator_traits_type;
+ typedef allocator_traits<Allocator> allocator_traits_type;
public:
- typedef A allocator_type;
- //! The stored allocator type
- typedef allocator_type stored_allocator_type;
+ typedef Allocator allocator_type;
+ typedef allocator_type stored_allocator_type;
typedef typename allocator_traits_type::pointer pointer;
typedef typename allocator_traits_type::value_type value_type;
typedef typename allocator_traits_type::size_type size_type;
+ typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
basic_string_base()
: members_()
@@ -110,28 +91,22 @@ class basic_string_base
: members_(a)
{ init(); }
+ basic_string_base(BOOST_RV_REF(allocator_type) a)
+ : members_(boost::move(a))
+ { this->init(); }
+
basic_string_base(const allocator_type& a, size_type n)
: members_(a)
- {
+ {
this->init();
this->allocate_initial_block(n);
}
- basic_string_base(BOOST_RV_REF(basic_string_base) b)
- : members_(boost::move(b.alloc()))
- {
- this->init();
- this->swap_data(b);
- }
-
~basic_string_base()
- {
+ {
if(!this->is_short()){
this->deallocate_block();
- allocator_traits_type::destroy
- ( this->alloc()
- , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
- );
+ this->is_short(true);
}
}
@@ -176,7 +151,7 @@ class basic_string_base
//This type has the same alignment and size as long_t but it's POD
//so, unlike long_t, it can be placed in a union
-
+
typedef typename boost::aligned_storage< sizeof(long_t),
container_detail::alignment_of<long_t>::value>::type long_raw_t;
@@ -203,32 +178,38 @@ class basic_string_base
long_raw_t r;
short_t s;
- short_t &short_repr() const
- { return *const_cast<short_t *>(&s); }
+ const short_t &short_repr() const
+ { return s; }
+
+ const long_t &long_repr() const
+ { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }
- long_t &long_repr() const
- { return *const_cast<long_t*>(reinterpret_cast<const long_t*>(&r)); }
+ short_t &short_repr()
+ { return s; }
+
+ long_t &long_repr()
+ { return *static_cast<long_t*>(static_cast<void*>(&r)); }
};
struct members_holder
- : public A
+ : public Allocator
{
members_holder()
- : A()
+ : Allocator()
{}
template<class AllocatorConvertible>
explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
- : A(boost::forward<AllocatorConvertible>(a))
+ : Allocator(boost::forward<AllocatorConvertible>(a))
{}
repr_t m_repr;
} members_;
- const A &alloc() const
+ const Allocator &alloc() const
{ return members_; }
- A &alloc()
+ Allocator &alloc()
{ return members_; }
static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
@@ -242,20 +223,22 @@ class basic_string_base
{ return static_cast<bool>(this->members_.m_repr.s.h.is_short != 0); }
void is_short(bool yes)
- {
- if(yes && !this->is_short()){
+ {
+ const bool was_short = this->is_short();
+ if(yes && !was_short){
allocator_traits_type::destroy
( this->alloc()
, static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
);
+ this->members_.m_repr.s.h.is_short = true;
}
- else{
+ else if(!yes && was_short){
allocator_traits_type::construct
( this->alloc()
, static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
);
+ this->members_.m_repr.s.h.is_short = false;
}
- this->members_.m_repr.s.h.is_short = yes;
}
private:
@@ -270,7 +253,7 @@ class basic_string_base
typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
typedef container_detail::integral_constant<unsigned,
- boost::container::container_detail::version<A>::value> alloc_version;
+ boost::container::container_detail::version<Allocator>::value> alloc_version;
std::pair<pointer, bool>
allocation_command(allocation_type command,
@@ -279,46 +262,23 @@ class basic_string_base
size_type &received_size, pointer reuse = 0)
{
if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
- reuse = pointer(0);
+ reuse = pointer();
command &= ~(expand_fwd | expand_bwd);
}
- return this->allocation_command
- (command, limit_size, preferred_size, received_size, reuse, alloc_version());
- }
-
- std::pair<pointer, bool>
- allocation_command(allocation_type command,
- size_type limit_size,
- size_type preferred_size,
- size_type &received_size,
- const pointer &reuse,
- allocator_v1)
- {
- (void)limit_size;
- (void)reuse;
- if(!(command & allocate_new))
- return std::pair<pointer, bool>(pointer(0), false);
- received_size = preferred_size;
- return std::make_pair(this->alloc().allocate(received_size), false);
+ return container_detail::allocator_version_traits<Allocator>::allocation_command
+ (this->alloc(), command, limit_size, preferred_size, received_size, reuse);
}
- std::pair<pointer, bool>
- allocation_command(allocation_type command,
- size_type limit_size,
- size_type preferred_size,
- size_type &received_size,
- pointer reuse,
- allocator_v2)
+ size_type next_capacity(size_type additional_objects) const
{
- return this->alloc().allocation_command(command, limit_size, preferred_size,
- received_size, reuse);
+ return next_capacity_calculator
+ <size_type, NextCapacityDouble /*NextCapacity60Percent*/>::
+ get( allocator_traits_type::max_size(this->alloc())
+ , this->priv_storage(), additional_objects );
}
- size_type next_capacity(size_type additional_objects) const
- { return get_next_capacity(allocator_traits_type::max_size(this->alloc()), this->priv_storage(), additional_objects); }
-
void deallocate(pointer p, size_type n)
- {
+ {
if (p && (n > InternalBufferChars))
this->alloc().deallocate(p, n);
}
@@ -334,11 +294,9 @@ class basic_string_base
void destroy(pointer p, size_type n)
{
- for(; n--; ++p){
- allocator_traits_type::destroy
- ( this->alloc()
- , container_detail::to_raw_pointer(p)
- );
+ value_type *raw_p = container_detail::to_raw_pointer(p);
+ for(; n--; ++raw_p){
+ allocator_traits_type::destroy( this->alloc(), raw_p);
}
}
@@ -358,39 +316,46 @@ class basic_string_base
pointer p = this->allocation_command(allocate_new, n, new_cap, new_cap).first;
this->is_short(false);
this->priv_long_addr(p);
- this->priv_size(0);
+ this->priv_long_size(0);
this->priv_storage(new_cap);
}
}
- else
- throw_length_error();
+ else{
+ throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
+ }
}
void deallocate_block()
{ this->deallocate(this->priv_addr(), this->priv_storage()); }
-
+
size_type max_size() const
{ return allocator_traits_type::max_size(this->alloc()) - 1; }
- // Helper functions for exception handling.
- void throw_length_error() const
- { throw(std::length_error("basic_string")); }
-
- void throw_out_of_range() const
- { throw(std::out_of_range("basic_string")); }
-
protected:
size_type priv_capacity() const
{ return this->priv_storage() - 1; }
pointer priv_short_addr() const
- { return pointer(&this->members_.m_repr.short_repr().data[0]); }
+ { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
pointer priv_long_addr() const
{ return this->members_.m_repr.long_repr().start; }
pointer priv_addr() const
- { return this->is_short() ? pointer(&this->members_.m_repr.short_repr().data[0]) : this->members_.m_repr.long_repr().start; }
+ {
+ return this->is_short()
+ ? priv_short_addr()
+ : priv_long_addr()
+ ;
+ }
+
+ pointer priv_end_addr() const
+ {
+ return this->is_short()
+ ? this->priv_short_addr() + this->priv_short_size()
+ : this->priv_long_addr() + this->priv_long_size()
+ ;
+ }
void priv_long_addr(pointer addr)
{ this->members_.m_repr.long_repr().start = addr; }
@@ -405,18 +370,18 @@ class basic_string_base
{ return this->members_.m_repr.long_repr().storage; }
void priv_storage(size_type storage)
- {
+ {
if(!this->is_short())
this->priv_long_storage(storage);
}
void priv_long_storage(size_type storage)
- {
+ {
this->members_.m_repr.long_repr().storage = storage;
}
size_type priv_size() const
- { return this->is_short() ? priv_short_size() : priv_long_size(); }
+ { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
size_type priv_short_size() const
{ return this->members_.m_repr.short_repr().h.length; }
@@ -425,7 +390,7 @@ class basic_string_base
{ return this->members_.m_repr.long_repr().length; }
void priv_size(size_type sz)
- {
+ {
if(this->is_short())
this->priv_short_size(sz);
else
@@ -433,35 +398,41 @@ class basic_string_base
}
void priv_short_size(size_type sz)
- {
+ {
this->members_.m_repr.s.h.length = (unsigned char)sz;
}
void priv_long_size(size_type sz)
- {
- this->members_.m_repr.long_repr().length = static_cast<typename allocator_traits_type::size_type>(sz);
+ {
+ this->members_.m_repr.long_repr().length = sz;
}
void swap_data(basic_string_base& other)
{
if(this->is_short()){
if(other.is_short()){
- container_detail::do_swap(this->members_.m_repr, other.members_.m_repr);
+ std::swap(this->members_.m_repr, other.members_.m_repr);
}
else{
- repr_t copied(this->members_.m_repr);
- this->members_.m_repr.long_repr() = other.members_.m_repr.long_repr();
- other.members_.m_repr = copied;
+ short_t short_backup(this->members_.m_repr.short_repr());
+ long_t long_backup (other.members_.m_repr.long_repr());
+ other.members_.m_repr.long_repr().~long_t();
+ ::new(&this->members_.m_repr.long_repr()) long_t;
+ this->members_.m_repr.long_repr() = long_backup;
+ other.members_.m_repr.short_repr() = short_backup;
}
}
else{
if(other.is_short()){
- repr_t copied(other.members_.m_repr);
- other.members_.m_repr.long_repr() = this->members_.m_repr.long_repr();
- this->members_.m_repr = copied;
+ short_t short_backup(other.members_.m_repr.short_repr());
+ long_t long_backup (this->members_.m_repr.long_repr());
+ this->members_.m_repr.long_repr().~long_t();
+ ::new(&other.members_.m_repr.long_repr()) long_t;
+ other.members_.m_repr.long_repr() = long_backup;
+ this->members_.m_repr.short_repr() = short_backup;
}
else{
- container_detail::do_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
+ boost::container::swap_dispatch(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
}
}
}
@@ -469,7 +440,7 @@ class basic_string_base
} //namespace container_detail {
-/// @endcond
+#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
//! The basic_string class represents a Sequence of characters. It contains all the
//! usual operations of a Sequence, and, additionally, it contains standard string
@@ -499,39 +470,46 @@ class basic_string_base
//! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
//! In this implementation, iterators are only invalidated by member functions that
//! explicitly change the string's contents.
+//!
+//! \tparam CharT The type of character it contains.
+//! \tparam Traits The Character Traits type, which encapsulates basic character operations
+//! \tparam Allocator The allocator, used for internal memory management.
#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
-template <class CharT, class Traits = std::char_traits<CharT>, class A = std::allocator<CharT> >
+template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> >
#else
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
#endif
class basic_string
- : private container_detail::basic_string_base<A>
+ : private container_detail::basic_string_base<Allocator>
{
- /// @cond
+ #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
- typedef allocator_traits<A> allocator_traits_type;
+ typedef allocator_traits<Allocator> allocator_traits_type;
BOOST_COPYABLE_AND_MOVABLE(basic_string)
- typedef container_detail::basic_string_base<A> base_t;
+ typedef container_detail::basic_string_base<Allocator> base_t;
static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
protected:
- // A helper class to use a char_traits as a function object.
+ // Allocator helper class to use a char_traits as a function object.
template <class Tr>
struct Eq_traits
- : public std::binary_function<typename Tr::char_type,
- typename Tr::char_type,
- bool>
{
- bool operator()(const typename Tr::char_type& x,
- const typename Tr::char_type& y) const
+ //Compatibility with std::binary_function
+ typedef typename Tr::char_type first_argument_type;
+ typedef typename Tr::char_type second_argument_type;
+ typedef bool result_type;
+
+ bool operator()(const first_argument_type& x, const second_argument_type& y) const
{ return Tr::eq(x, y); }
};
template <class Tr>
struct Not_within_traits
- : public std::unary_function<typename Tr::char_type, bool>
{
+ typedef typename Tr::char_type argument_type;
+ typedef bool result_type;
+
typedef const typename Tr::char_type* Pointer;
const Pointer m_first;
const Pointer m_last;
@@ -545,51 +523,46 @@ class basic_string
std::bind1st(Eq_traits<Tr>(), x)) == m_last;
}
};
- /// @endcond
+ #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public:
-
- //! The allocator type
- typedef A allocator_type;
- //! The stored allocator type
- typedef allocator_type stored_allocator_type;
- //! The type of object, CharT, stored in the string
- typedef CharT value_type;
- //! The second template parameter Traits
- typedef Traits traits_type;
- //! Pointer to CharT
- typedef typename allocator_traits_type::pointer pointer;
- //! Const pointer to CharT
- typedef typename allocator_traits_type::const_pointer const_pointer;
- //! Reference to CharT
- typedef typename allocator_traits_type::reference reference;
- //! Const reference to CharT
- typedef typename allocator_traits_type::const_reference const_reference;
- //! An unsigned integral type
- typedef typename allocator_traits_type::size_type size_type;
- //! A signed integral type
- typedef typename allocator_traits_type::difference_type difference_type;
- //! Iterator used to iterate through a string. It's a Random Access Iterator
- typedef pointer iterator;
- //! Const iterator used to iterate through a string. It's a Random Access Iterator
- typedef const_pointer const_iterator;
- //! Iterator used to iterate backwards through a string
- typedef std::reverse_iterator<iterator> reverse_iterator;
- //! Const iterator used to iterate backwards through a string
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- //! The largest possible value of type size_type. That is, size_type(-1).
- static const size_type npos;
-
- /// @cond
+ //////////////////////////////////////////////
+ //
+ // types
+ //
+ //////////////////////////////////////////////
+ typedef Traits traits_type;
+ typedef CharT value_type;
+ typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
+ typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
+ typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
+ typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
+ typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
+ typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
+ typedef Allocator allocator_type;
+ typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
+ typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
+ typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
+ typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator<iterator>) reverse_iterator;
+ typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator<const_iterator>) const_reverse_iterator;
+ static const size_type npos = size_type(-1);
+
+ #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
typedef constant_iterator<CharT, difference_type> cvalue_iterator;
typedef typename base_t::allocator_v1 allocator_v1;
typedef typename base_t::allocator_v2 allocator_v2;
typedef typename base_t::alloc_version alloc_version;
- /// @endcond
+ typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
+ #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public: // Constructor, destructor, assignment.
- /// @cond
+ //////////////////////////////////////////////
+ //
+ // construct/copy/destroy
+ //
+ //////////////////////////////////////////////
+ #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
struct reserve_t {};
basic_string(reserve_t, size_type n,
@@ -600,7 +573,7 @@ class basic_string
, n + 1)
{ this->priv_terminate_string(); }
- /// @endcond
+ #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
//! <b>Effects</b>: Default constructs a basic_string.
//!
@@ -612,8 +585,8 @@ class basic_string
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
//!
- //! <b>Throws</b>: If allocator_type's copy constructor throws.
- explicit basic_string(const allocator_type& a)
+ //! <b>Throws</b>: Nothing
+ explicit basic_string(const allocator_type& a) BOOST_CONTAINER_NOEXCEPT
: base_t(a)
{ this->priv_terminate_string(); }
@@ -621,19 +594,29 @@ class basic_string
//!
//! <b>Postcondition</b>: x == *this.
//!
- //! <b>Throws</b>: If allocator_type's default constructor throws.
+ //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
basic_string(const basic_string& s)
: base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
- { this->priv_range_initialize(s.begin(), s.end()); }
+ {
+ this->priv_terminate_string();
+ this->assign(s.begin(), s.end());
+ }
//! <b>Effects</b>: Move constructor. Moves s's resources to *this.
//!
- //! <b>Throws</b>: If allocator_type's copy constructor throws.
+ //! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- basic_string(BOOST_RV_REF(basic_string) s)
- : base_t(boost::move((base_t&)s))
- {}
+ basic_string(BOOST_RV_REF(basic_string) s) BOOST_CONTAINER_NOEXCEPT
+ : base_t(boost::move(s.alloc()))
+ {
+ if(s.alloc() == this->alloc()){
+ this->swap_data(s);
+ }
+ else{
+ this->assign(s.begin(), s.end());
+ }
+ }
//! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
//!
@@ -642,7 +625,10 @@ class basic_string
//! <b>Throws</b>: If allocation throws.
basic_string(const basic_string& s, const allocator_type &a)
: base_t(a)
- { this->priv_range_initialize(s.begin(), s.end()); }
+ {
+ this->priv_terminate_string();
+ this->assign(s.begin(), s.end());
+ }
//! <b>Effects</b>: Move constructor using the specified allocator.
//! Moves s's resources to *this.
@@ -653,62 +639,73 @@ class basic_string
basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
: base_t(a)
{
+ this->priv_terminate_string();
if(a == this->alloc()){
this->swap_data(s);
}
else{
- this->priv_range_initialize(s.begin(), s.end());
+ this->assign(s.begin(), s.end());
}
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by a specific number of characters of the s string.
basic_string(const basic_string& s, size_type pos, size_type n = npos,
- const allocator_type& a = allocator_type())
+ const allocator_type& a = allocator_type())
: base_t(a)
{
+ this->priv_terminate_string();
if (pos > s.size())
- this->throw_out_of_range();
+ throw_out_of_range("basic_string::basic_string out of range position");
else
- this->priv_range_initialize
+ this->assign
(s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by a specific number of characters of the s c-string.
- basic_string(const CharT* s, size_type n,
- const allocator_type& a = allocator_type())
+ basic_string(const CharT* s, size_type n, const allocator_type& a = allocator_type())
: base_t(a)
- { this->priv_range_initialize(s, s + n); }
+ {
+ this->priv_terminate_string();
+ this->assign(s, s + n);
+ }
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by the null-terminated s c-string.
- basic_string(const CharT* s,
- const allocator_type& a = allocator_type())
+ basic_string(const CharT* s, const allocator_type& a = allocator_type())
: base_t(a)
- { this->priv_range_initialize(s, s + Traits::length(s)); }
+ {
+ this->priv_terminate_string();
+ this->assign(s, s + Traits::length(s));
+ }
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by n copies of c.
- basic_string(size_type n, CharT c,
- const allocator_type& a = allocator_type())
+ basic_string(size_type n, CharT c, const allocator_type& a = allocator_type())
: base_t(a)
- {
- this->priv_range_initialize(cvalue_iterator(c, n),
- cvalue_iterator());
+ {
+ this->priv_terminate_string();
+ this->assign(n, c);
+ }
+
+ //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
+ //! and is initialized by n default-initialized characters.
+ basic_string(size_type n, default_init_t, const allocator_type& a = allocator_type())
+ : base_t(a, n + 1)
+ {
+ this->priv_size(n);
+ this->priv_terminate_string();
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and a range of iterators.
template <class InputIterator>
- basic_string(InputIterator f, InputIterator l,
- const allocator_type& a = allocator_type())
+ basic_string(InputIterator f, InputIterator l, const allocator_type& a = allocator_type())
: base_t(a)
{
- //Dispatch depending on integer/iterator
- const bool aux_boolean = container_detail::is_convertible<InputIterator, size_type>::value;
- typedef container_detail::bool_<aux_boolean> Result;
- this->priv_initialize_dispatch(f, l, Result());
+ this->priv_terminate_string();
+ this->assign(f, l);
}
//! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
@@ -716,9 +713,9 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- ~basic_string()
+ ~basic_string() BOOST_CONTAINER_NOEXCEPT
{}
-
+
//! <b>Effects</b>: Copy constructs a string.
//!
//! <b>Postcondition</b>: x == *this.
@@ -735,8 +732,8 @@ class basic_string
if(!this->is_short()){
this->deallocate_block();
this->is_short(true);
- Traits::assign(*this->priv_addr(), this->priv_null());
- this->priv_size(0);
+ Traits::assign(*this->priv_addr(), CharT(0));
+ this->priv_short_size(0);
}
}
container_detail::assign_alloc(this->alloc(), x.alloc(), flag);
@@ -745,30 +742,38 @@ class basic_string
return *this;
}
- //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.
+ //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
//!
- //! <b>Throws</b>: If allocator_type's copy constructor throws.
+ //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
+ //! is false and allocation throws
//!
- //! <b>Complexity</b>: Constant.
+ //! <b>Complexity</b>: Constant if allocator_traits_type::
+ //! propagate_on_container_move_assignment is true or
+ //! this->get>allocator() == x.get_allocator(). Linear otherwise.
basic_string& operator=(BOOST_RV_REF(basic_string) x)
- {
- if (&x != this){
- allocator_type &this_alloc = this->alloc();
- allocator_type &x_alloc = x.alloc();
- //If allocators are equal we can just swap pointers
- if(this_alloc == x_alloc){
- //Destroy objects but retain memory in case x reuses it in the future
- this->clear();
- this->swap_data(x);
- //Move allocator if needed
- container_detail::bool_<allocator_traits_type::
- propagate_on_container_move_assignment::value> flag;
- container_detail::move_alloc(this_alloc, x_alloc, flag);
- }
- //If unequal allocators, then do a one by one move
- else{
- this->assign( x.begin(), x.end());
- }
+ BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value)
+ {
+ //for move constructor, no aliasing (&x != this) is assummed.
+ BOOST_ASSERT(this != &x);
+ allocator_type &this_alloc = this->alloc();
+ allocator_type &x_alloc = x.alloc();
+ const bool propagate_alloc = allocator_traits_type::
+ propagate_on_container_move_assignment::value;
+ container_detail::bool_<propagate_alloc> flag;
+ const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
+ //Resources can be transferred if both allocators are
+ //going to be equal after this function (either propagated or already equal)
+ if(propagate_alloc || allocators_equal){
+ //Destroy objects but retain memory in case x reuses it in the future
+ this->clear();
+ //Move allocator if needed
+ container_detail::move_alloc(this_alloc, x_alloc, flag);
+ //Nothrow swap
+ this->swap_data(x);
+ }
+ //Else do a one by one move
+ else{
+ this->assign( x.begin(), x.end());
}
return *this;
}
@@ -781,71 +786,80 @@ class basic_string
basic_string& operator=(CharT c)
{ return this->assign(static_cast<size_type>(1), c); }
- //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
+ //! <b>Effects</b>: Returns a copy of the internal allocator.
//!
- //! <b>Throws</b>: Nothing.
+ //! <b>Throws</b>: If allocator's copy constructor throws.
//!
//! <b>Complexity</b>: Constant.
- iterator begin()
- { return this->priv_addr(); }
+ allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
+ { return this->alloc(); }
- //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
+ //! <b>Effects</b>: Returns a reference to the internal allocator.
//!
- //! <b>Throws</b>: Nothing.
+ //! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
- const_iterator begin() const
- { return this->priv_addr(); }
+ //!
+ //! <b>Note</b>: Non-standard extension.
+ stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
+ { return this->alloc(); }
- //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
+ //! <b>Effects</b>: Returns a reference to the internal allocator.
//!
- //! <b>Throws</b>: Nothing.
+ //! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
- const_iterator cbegin() const
- { return this->priv_addr(); }
+ //!
+ //! <b>Note</b>: Non-standard extension.
+ const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
+ { return this->alloc(); }
- //! <b>Effects</b>: Returns an iterator to the end of the vector.
+ //////////////////////////////////////////////
+ //
+ // iterators
+ //
+ //////////////////////////////////////////////
+
+ //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- iterator end()
- { return this->priv_addr() + this->priv_size(); }
+ iterator begin() BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_addr(); }
- //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
+ //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_iterator end() const
- { return this->priv_addr() + this->priv_size(); }
+ const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_addr(); }
- //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
+ //! <b>Effects</b>: Returns an iterator to the end of the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_iterator cend() const
- { return this->priv_addr() + this->priv_size(); }
+ iterator end() BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_end_addr(); }
- //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
- //! of the reversed vector.
+ //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- reverse_iterator rbegin()
- { return reverse_iterator(this->priv_addr() + this->priv_size()); }
+ const_iterator end() const BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_end_addr(); }
- //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
+ //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
//! of the reversed vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_reverse_iterator rbegin() const
- { return this->crbegin(); }
+ reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
+ { return reverse_iterator(this->priv_end_addr()); }
//! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
//! of the reversed vector.
@@ -853,8 +867,8 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_reverse_iterator crbegin() const
- { return const_reverse_iterator(this->priv_addr() + this->priv_size()); }
+ const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
+ { return this->crbegin(); }
//! <b>Effects</b>: Returns a reverse_iterator pointing to the end
//! of the reversed vector.
@@ -862,7 +876,7 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- reverse_iterator rend()
+ reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
{ return reverse_iterator(this->priv_addr()); }
//! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
@@ -871,52 +885,63 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_reverse_iterator rend() const
+ const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
{ return this->crend(); }
- //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
- //! of the reversed vector.
+ //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_reverse_iterator crend() const
- { return const_reverse_iterator(this->priv_addr()); }
+ const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_addr(); }
- //! <b>Effects</b>: Returns a copy of the internal allocator.
+ //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
//!
- //! <b>Throws</b>: If allocator's copy constructor throws.
+ //! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- allocator_type get_allocator() const
- { return this->alloc(); }
+ const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
+ { return this->priv_end_addr(); }
- //! <b>Effects</b>: Returns a reference to the internal allocator.
+ //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
+ //! of the reversed vector.
//!
- //! <b>Throws</b>: Nothing
+ //! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- //!
- //! <b>Note</b>: Non-standard extension.
- const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
- { return this->alloc(); }
+ const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
+ { return const_reverse_iterator(this->priv_end_addr()); }
- //! <b>Effects</b>: Returns a reference to the internal allocator.
+ //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
+ //! of the reversed vector.
//!
- //! <b>Throws</b>: Nothing
+ //! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
+ const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
+ { return const_reverse_iterator(this->priv_addr()); }
+
+ //////////////////////////////////////////////
+ //
+ // capacity
+ //
+ //////////////////////////////////////////////
+
+ //! <b>Effects</b>: Returns true if the vector contains no elements.
//!
- //! <b>Note</b>: Non-standard extension.
- stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
- { return this->alloc(); }
+ //! <b>Throws</b>: Nothing.
+ //!
+ //! <b>Complexity</b>: Constant.
+ bool empty() const BOOST_CONTAINER_NOEXCEPT
+ { return !this->priv_size(); }
//! <b>Effects</b>: Returns the number of the elements contained in the vector.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- size_type size() const
+ size_type size() const BOOST_CONTAINER_NOEXCEPT
{ return this->priv_size(); }
//! <b>Effects</b>: Returns the number of the elements contained in the vector.
@@ -924,7 +949,7 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- size_type length() const
+ size_type length() const BOOST_CONTAINER_NOEXCEPT
{ return this->size(); }
//! <b>Effects</b>: Returns the largest possible size of the vector.
@@ -932,7 +957,7 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- size_type max_size() const
+ size_type max_size() const BOOST_CONTAINER_NOEXCEPT
{ return base_t::max_size(); }
//! <b>Effects</b>: Inserts or erases elements at the end such that
@@ -943,47 +968,38 @@ class basic_string
//! <b>Complexity</b>: Linear to the difference between size() and new_size.
void resize(size_type n, CharT c)
{
- if (n <= size())
+ if (n <= this->size())
this->erase(this->begin() + n, this->end());
else
this->append(n - this->size(), c);
}
//! <b>Effects</b>: Inserts or erases elements at the end such that
- //! the size becomes n. New elements are default constructed.
+ //! the size becomes n. New elements are value initialized.
//!
//! <b>Throws</b>: If memory allocation throws
//!
//! <b>Complexity</b>: Linear to the difference between size() and new_size.
void resize(size_type n)
- { resize(n, this->priv_null()); }
+ { resize(n, CharT()); }
- //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
- //! effect. Otherwise, it is a request for allocation of additional memory.
- //! If the request is successful, then capacity() is greater than or equal to
- //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
+
+ //! <b>Effects</b>: Inserts or erases elements at the end such that
+ //! the size becomes n. New elements are uninitialized.
//!
- //! <b>Throws</b>: If memory allocation allocation throws
- void reserve(size_type res_arg)
+ //! <b>Throws</b>: If memory allocation throws
+ //!
+ //! <b>Complexity</b>: Linear to the difference between size() and new_size.
+ //!
+ //! <b>Note</b>: Non-standard extension
+ void resize(size_type n, default_init_t)
{
- if (res_arg > this->max_size())
- this->throw_length_error();
-
- if (this->capacity() < res_arg){
- size_type n = container_detail::max_value(res_arg, this->size()) + 1;
- size_type new_cap = this->next_capacity(n);
- pointer new_start = this->allocation_command
- (allocate_new, n, new_cap, new_cap).first;
- size_type new_length = 0;
-
- new_length += priv_uninitialized_copy
- (this->priv_addr(), this->priv_addr() + this->priv_size(), new_start);
- this->priv_construct_null(new_start + new_length);
- this->deallocate_block();
- this->is_short(false);
- this->priv_long_addr(new_start);
- this->priv_size(new_length);
- this->priv_storage(new_cap);
+ if (n <= this->size())
+ this->erase(this->begin() + n, this->end());
+ else{
+ this->priv_reserve(n, false);
+ this->priv_size(n);
+ this->priv_terminate_string();
}
}
@@ -993,21 +1009,17 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- size_type capacity() const
+ size_type capacity() const BOOST_CONTAINER_NOEXCEPT
{ return this->priv_capacity(); }
- //! <b>Effects</b>: Erases all the elements of the vector.
- //!
- //! <b>Throws</b>: Nothing.
+ //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
+ //! effect. Otherwise, it is a request for allocation of additional memory.
+ //! If the request is successful, then capacity() is greater than or equal to
+ //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
//!
- //! <b>Complexity</b>: Linear to the number of elements in the vector.
- void clear()
- {
- if (!empty()) {
- Traits::assign(*this->priv_addr(), this->priv_null());
- this->priv_size(0);
- }
- }
+ //! <b>Throws</b>: If memory allocation allocation throws
+ void reserve(size_type res_arg)
+ { this->priv_reserve(res_arg); }
//! <b>Effects</b>: Tries to deallocate the excess of memory created
//! with previous allocations. The size of the string is unchanged
@@ -1021,7 +1033,7 @@ class basic_string
if(this->priv_storage() > InternalBufferChars){
//Check if we should pass from dynamically allocated buffer
//to the internal storage
- if(this->priv_size() < (InternalBufferChars)){
+ if(this->priv_size() < InternalBufferChars){
//Dynamically allocated buffer attributes
pointer long_addr = this->priv_long_addr();
size_type long_storage = this->priv_long_storage();
@@ -1040,13 +1052,11 @@ class basic_string
}
}
- //! <b>Effects</b>: Returns true if the vector contains no elements.
- //!
- //! <b>Throws</b>: Nothing.
- //!
- //! <b>Complexity</b>: Constant.
- bool empty() const
- { return !this->priv_size(); }
+ //////////////////////////////////////////////
+ //
+ // element access
+ //
+ //////////////////////////////////////////////
//! <b>Requires</b>: size() > n.
//!
@@ -1056,7 +1066,7 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- reference operator[](size_type n)
+ reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT
{ return *(this->priv_addr() + n); }
//! <b>Requires</b>: size() > n.
@@ -1067,7 +1077,7 @@ class basic_string
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
- const_reference operator[](size_type n) const
+ const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT
{ return *(this->priv_addr() + n); }
//! <b>Requires</b>: size() > n.
@@ -1078,9 +1088,10 @@ class basic_string
//! <b>Throws</b>: std::range_error if n >= size()
//!
//! <b>Complexity</b>: Constant.
- reference at(size_type n) {
- if (n >= size())
- this->throw_out_of_range();
+ reference at(size_type n)
+ {
+ if (n >= this->size())
+ throw_out_of_range("basic_string::at invalid subscript");
return *(this->priv_addr() + n);
}
@@ -1093,11 +1104,17 @@ class basic_string
//!
//! <b>Complexity</b>: Constant.
const_reference at(size_type n) const {
- if (n >= size())
- this->throw_out_of_range();
+ if (n >= this->size())
+ throw_out_of_range("basic_string::at invalid subscript");
return *(this->priv_addr() + n);
}
+ //////////////////////////////////////////////
+ //
+ // modifiers
+ //
+ //////////////////////////////////////////////
+
//! <b>Effects</b>: Calls append(str.data, str.size()).
//!
//! <b>Returns</b>: *this
@@ -1133,7 +1150,7 @@ class basic_string
basic_string& append(const basic_string& s, size_type pos, size_type n)
{
if (pos > s.size())
- this->throw_out_of_range();
+ throw_out_of_range("basic_string::append out of range position");
return this->append(s.begin() + pos,
s.begin() + pos + container_detail::min_value(n, s.size() - pos));
}
@@ -1177,14 +1194,16 @@ class basic_string
//! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
void push_back(CharT c)
{
- if (this->priv_size() < this->capacity()){
- this->priv_construct_null(this->priv_addr() + (this->priv_size() + 1));
- Traits::assign(this->priv_addr()[this->priv_size()], c);
- this->priv_size(this->priv_size()+1);
+ const size_type old_size = this->priv_size();
+ if (old_size < this->capacity()){
+ const pointer addr = this->priv_addr();
+ this->priv_construct_null(addr + old_size + 1);
+ Traits::assign(addr[old_size], c);
+ this->priv_size(old_size+1);
}
else{
//No enough memory, insert a new object at the end
- this->append((size_type)1, c);
+ this->append(size_type(1), c);
}
}
@@ -1201,7 +1220,7 @@ class basic_string
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: *this
- basic_string& assign(BOOST_RV_REF(basic_string) ms)
+ basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_CONTAINER_NOEXCEPT
{ return this->swap_data(ms), *this; }
//! <b>Requires</b>: pos <= str.size()
@@ -1212,10 +1231,10 @@ class basic_string
//! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
//!
//! <b>Returns</b>: *this
- basic_string& assign(const basic_string& s,
- size_type pos, size_type n) {
+ basic_string& assign(const basic_string& s, size_type pos, size_type n)
+ {
if (pos > s.size())
- this->throw_out_of_range();
+ throw_out_of_range("basic_string::assign out of range position");
return this->assign(s.begin() + pos,
s.begin() + pos + container_detail::min_value(n, s.size() - pos));
}
@@ -1226,7 +1245,7 @@ class basic_string
//! length n whose elements are a copy of those pointed to by s.
//!
//! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
- //!
+ //!
//! <b>Returns</b>: *this
basic_string& assign(const CharT* s, size_type n)
{ return this->assign(s, s + n); }
@@ -1246,15 +1265,46 @@ class basic_string
{ return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
//! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
+ //!
+ //! <b>Returns</b>: *this
+ basic_string& assign(const CharT* first, const CharT* last)
+ {
+ size_type n = static_cast<size_type>(last - first);
+ this->reserve(n);
+ CharT* ptr = container_detail::to_raw_pointer(this->priv_addr());
+ Traits::copy(ptr, first, n);
+ this->priv_construct_null(ptr + n);
+ this->priv_size(n);
+ return *this;
+ }
+
+ //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
//!
//! <b>Returns</b>: *this
template <class InputIter>
- basic_string& assign(InputIter first, InputIter last)
+ basic_string& assign(InputIter first, InputIter last
+ #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
+ , typename container_detail::enable_if_c
+ < !container_detail::is_convertible<InputIter, size_type>::value
+ >::type * = 0
+ #endif
+ )
{
- //Dispatch depending on integer/iterator
- const bool aux_boolean = container_detail::is_convertible<InputIter, size_type>::value;
- typedef container_detail::bool_<aux_boolean> Result;
- return this->priv_assign_dispatch(first, last, Result());
+ size_type cur = 0;
+ const pointer addr = this->priv_addr();
+ CharT *ptr = container_detail::to_raw_pointer(addr);
+ const size_type old_size = this->priv_size();
+ while (first != last && cur != old_size) {
+ Traits::assign(*ptr, *first);
+ ++first;
+ ++cur;
+ ++ptr;
+ }
+ if (first == last)
+ this->erase(addr + cur, addr + old_size);
+ else
+ this->append(first, last);
+ return *this;
}
//! <b>Requires</b>: pos <= size().
@@ -1266,10 +1316,11 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& insert(size_type pos, const basic_string& s)
{
- if (pos > size())
- this->throw_out_of_range();
- if (this->size() > this->max_size() - s.size())
- this->throw_length_error();
+ const size_type sz = this->size();
+ if (pos > sz)
+ throw_out_of_range("basic_string::insert out of range position");
+ if (sz > this->max_size() - s.size())
+ throw_length_error("basic_string::insert max_size() exceeded");
this->insert(this->priv_addr() + pos, s.begin(), s.end());
return *this;
}
@@ -1282,14 +1333,15 @@ class basic_string
//! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
//!
//! <b>Returns</b>: *this
- basic_string& insert(size_type pos1, const basic_string& s,
- size_type pos2, size_type n)
- {
- if (pos1 > this->size() || pos2 > s.size())
- this->throw_out_of_range();
- size_type len = container_detail::min_value(n, s.size() - pos2);
- if (this->size() > this->max_size() - len)
- this->throw_length_error();
+ basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n)
+ {
+ const size_type sz = this->size();
+ const size_type str_size = s.size();
+ if (pos1 > sz || pos2 > str_size)
+ throw_out_of_range("basic_string::insert out of range position");
+ size_type len = container_detail::min_value(n, str_size - pos2);
+ if (sz > this->max_size() - len)
+ throw_length_error("basic_string::insert max_size() exceeded");
const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2;
const CharT *end_ptr = beg_ptr + len;
this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
@@ -1310,9 +1362,9 @@ class basic_string
basic_string& insert(size_type pos, const CharT* s, size_type n)
{
if (pos > this->size())
- this->throw_out_of_range();
+ throw_out_of_range("basic_string::insert out of range position");
if (this->size() > this->max_size() - n)
- this->throw_length_error();
+ throw_length_error("basic_string::insert max_size() exceeded");
this->insert(this->priv_addr() + pos, s, s + n);
return *this;
}
@@ -1327,11 +1379,11 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& insert(size_type pos, const CharT* s)
{
- if (pos > size())
- this->throw_out_of_range();
+ if (pos > this->size())
+ throw_out_of_range("basic_string::insert out of range position");
size_type len = Traits::length(s);
if (this->size() > this->max_size() - len)
- this->throw_length_error();
+ throw_length_error("basic_string::insert max_size() exceeded");
this->insert(this->priv_addr() + pos, s, s + len);
return *this;
}
@@ -1345,9 +1397,9 @@ class basic_string
basic_string& insert(size_type pos, size_type n, CharT c)
{
if (pos > this->size())
- this->throw_out_of_range();
+ throw_out_of_range("basic_string::insert out of range position");
if (this->size() > this->max_size() - n)
- this->throw_length_error();
+ throw_length_error("basic_string::insert max_size() exceeded");
this->insert(const_iterator(this->priv_addr() + pos), n, c);
return *this;
}
@@ -1359,7 +1411,7 @@ class basic_string
//! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
iterator insert(const_iterator p, CharT c)
{
- size_type new_offset = p - this->priv_addr() + 1;
+ size_type new_offset = p - this->priv_addr();
this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
return this->priv_addr() + new_offset;
}
@@ -1369,27 +1421,143 @@ class basic_string
//!
//! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
//!
- //! <b>Returns</b>: An iterator which refers to the copy of the first
- //! inserted character, or p if n == 0.
- void insert(const_iterator p, size_type n, CharT c)
- {
- this->insert(p, cvalue_iterator(c, n), cvalue_iterator());
- }
+ //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
+ iterator insert(const_iterator p, size_type n, CharT c)
+ { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
//! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
//!
//! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
//!
- //! <b>Returns</b>: An iterator which refers to the copy of the first
- //! inserted character, or p if first == last.
+ //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
template <class InputIter>
- void insert(const_iterator p, InputIter first, InputIter last)
- {
- //Dispatch depending on integer/iterator
- const bool aux_boolean = container_detail::is_convertible<InputIter, size_type>::value;
- typedef container_detail::bool_<aux_boolean> Result;
- this->priv_insert_dispatch(p, first, last, Result());
+ iterator insert(const_iterator p, InputIter first, InputIter last
+ #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
+ , typename container_detail::enable_if_c
+ < !container_detail::is_convertible<InputIter, size_type>::value
+ && container_detail::is_input_iterator<InputIter>::value
+ >::type * = 0
+ #endif
+ )
+ {
+ const size_type n_pos = p - this->cbegin();
+ for ( ; first != last; ++first, ++p) {
+ p = this->insert(p, *first);
+ }
+ return this->begin() + n_pos;
+ }
+
+ #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
+ template <class ForwardIter>
+ iterator insert(const_iterator p, ForwardIter first, ForwardIter last
+ , typename container_detail::enable_if_c
+ < !container_detail::is_convertible<ForwardIter, size_type>::value
+ && !container_detail::is_input_iterator<ForwardIter>::value
+ >::type * = 0
+ )
+ {
+ const size_type n_pos = p - this->cbegin();
+ if (first != last) {
+ const size_type n = std::distance(first, last);
+ const size_type old_size = this->priv_size();
+ const size_type remaining = this->capacity() - old_size;
+ const pointer old_start = this->priv_addr();
+ bool enough_capacity = false;
+ std::pair<pointer, bool> allocation_ret;
+ size_type new_cap = 0;
+
+ //Check if we have enough capacity
+ if (remaining >= n){
+ enough_capacity = true;
+ }
+ else {
+ //Otherwise expand current buffer or allocate new storage
+ new_cap = this->next_capacity(n);
+ allocation_ret = this->allocation_command
+ (allocate_new | expand_fwd | expand_bwd, old_size + n + 1,
+ new_cap, new_cap, old_start);
+
+ //Check forward expansion
+ if(old_start == allocation_ret.first){
+ enough_capacity = true;
+ this->priv_storage(new_cap);
+ }
+ }
+
+ //Reuse same buffer
+ if(enough_capacity){
+ const size_type elems_after = old_size - (p - old_start);
+ const size_type old_length = old_size;
+ if (elems_after >= n) {
+ const pointer pointer_past_last = old_start + old_size + 1;
+ priv_uninitialized_copy(old_start + (old_size - n + 1),
+ pointer_past_last, pointer_past_last);
+
+ this->priv_size(old_size+n);
+ Traits::move(const_cast<CharT*>(container_detail::to_raw_pointer(p + n)),
+ container_detail::to_raw_pointer(p),
+ (elems_after - n) + 1);
+ this->priv_copy(first, last, const_cast<CharT*>(container_detail::to_raw_pointer(p)));
+ }
+ else {
+ ForwardIter mid = first;
+ std::advance(mid, elems_after + 1);
+
+ priv_uninitialized_copy(mid, last, old_start + old_size + 1);
+ const size_type newer_size = old_size + (n - elems_after);
+ this->priv_size(newer_size);
+ priv_uninitialized_copy
+ (p, const_iterator(old_start + old_length + 1),
+ old_start + newer_size);
+ this->priv_size(newer_size + elems_after);
+ this->priv_copy(first, mid, const_cast<CharT*>(container_detail::to_raw_pointer(p)));
+ }
+ }
+ else{
+ pointer new_start = allocation_ret.first;
+ if(!allocation_ret.second){
+ //Copy data to new buffer
+ size_type new_length = 0;
+ //This can't throw, since characters are POD
+ new_length += priv_uninitialized_copy
+ (const_iterator(old_start), p, new_start);
+ new_length += priv_uninitialized_copy
+ (first, last, new_start + new_length);
+ new_length += priv_uninitialized_copy
+ (p, const_iterator(old_start + old_size),
+ new_start + new_length);
+ this->priv_construct_null(new_start + new_length);
+
+ this->deallocate_block();
+ this->is_short(false);
+ this->priv_long_addr(new_start);
+ this->priv_long_size(new_length);
+ this->priv_long_storage(new_cap);
+ }
+ else{
+ //value_type is POD, so backwards expansion is much easier
+ //than with vector<T>
+ value_type * const oldbuf = container_detail::to_raw_pointer(old_start);
+ value_type * const newbuf = container_detail::to_raw_pointer(new_start);
+ const value_type *const pos = container_detail::to_raw_pointer(p);
+ const size_type before = pos - oldbuf;
+
+ //First move old data
+ Traits::move(newbuf, oldbuf, before);
+ Traits::move(newbuf + before + n, pos, old_size - before);
+ //Now initialize the new data
+ priv_uninitialized_copy(first, last, new_start + before);
+ this->priv_construct_null(new_start + (old_size + n));
+ this->is_short(false);
+ this->priv_long_addr(new_start);
+ this->priv_long_size(old_size + n);
+ this->priv_long_storage(new_cap);
+ }
+ }
+ }
+ return this->begin() + n_pos;
}
+ #endif
//! <b>Requires</b>: pos <= size()
//!
@@ -1404,11 +1572,12 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& erase(size_type pos = 0, size_type n = npos)
{
- if (pos > size())
- this->throw_out_of_range();
- erase(this->priv_addr() + pos, this->priv_addr() + pos + container_detail::min_value(n, size() - pos));
+ if (pos > this->size())
+ throw_out_of_range("basic_string::erase out of range position");
+ const pointer addr = this->priv_addr();
+ erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos));
return *this;
- }
+ }
//! <b>Effects</b>: Removes the character referred to by p.
//!
@@ -1416,14 +1585,15 @@ class basic_string
//!
//! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
//! erased. If no such element exists, end() is returned.
- iterator erase(const_iterator p)
+ iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT
{
// The move includes the terminating null.
- CharT *ptr = const_cast<CharT*>(container_detail::to_raw_pointer(p));
+ CharT * const ptr = const_cast<CharT*>(container_detail::to_raw_pointer(p));
+ const size_type old_size = this->priv_size();
Traits::move(ptr,
container_detail::to_raw_pointer(p + 1),
- this->priv_size() - (p - this->priv_addr()));
- this->priv_size(this->priv_size()-1);
+ old_size - (p - this->priv_addr()));
+ this->priv_size(old_size-1);
return iterator(ptr);
}
@@ -1435,15 +1605,16 @@ class basic_string
//!
//! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
//! the other elements being erased. If no such element exists, end() is returned.
- iterator erase(const_iterator first, const_iterator last)
+ iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT
{
CharT * f = const_cast<CharT*>(container_detail::to_raw_pointer(first));
if (first != last) { // The move includes the terminating null.
- size_type num_erased = last - first;
+ const size_type num_erased = last - first;
+ const size_type old_size = this->priv_size();
Traits::move(f,
container_detail::to_raw_pointer(last),
- (this->priv_size() + 1)-(last - this->priv_addr()));
- size_type new_length = this->priv_size() - num_erased;
+ (old_size + 1)-(last - this->priv_addr()));
+ const size_type new_length = old_size - num_erased;
this->priv_size(new_length);
}
return iterator(f);
@@ -1454,10 +1625,24 @@ class basic_string
//! <b>Throws</b>: Nothing
//!
//! <b>Effects</b>: Equivalent to erase(size() - 1, 1).
- void pop_back()
+ void pop_back() BOOST_CONTAINER_NOEXCEPT
+ {
+ const size_type old_size = this->priv_size();
+ Traits::assign(this->priv_addr()[old_size-1], CharT(0));
+ this->priv_size(old_size-1);;
+ }
+
+ //! <b>Effects</b>: Erases all the elements of the vector.
+ //!
+ //! <b>Throws</b>: Nothing.
+ //!
+ //! <b>Complexity</b>: Linear to the number of elements in the vector.
+ void clear() BOOST_CONTAINER_NOEXCEPT
{
- Traits::assign(this->priv_addr()[this->priv_size()-1], this->priv_null());
- this->priv_size(this->priv_size()-1);;
+ if (!this->empty()) {
+ Traits::assign(*this->priv_addr(), CharT(0));
+ this->priv_size(0);
+ }
}
//! <b>Requires</b>: pos1 <= size().
@@ -1469,13 +1654,15 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
{
- if (pos1 > size())
- this->throw_out_of_range();
- const size_type len = container_detail::min_value(n1, size() - pos1);
+ if (pos1 > this->size())
+ throw_out_of_range("basic_string::replace out of range position");
+ const size_type len = container_detail::min_value(n1, this->size() - pos1);
if (this->size() - len >= this->max_size() - str.size())
- this->throw_length_error();
- return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len,
- str.begin(), str.end());
+ throw_length_error("basic_string::replace max_size() exceeded");
+ const pointer addr = this->priv_addr();
+ return this->replace( const_iterator(addr + pos1)
+ , const_iterator(addr + pos1 + len)
+ , str.begin(), str.end());
}
//! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
@@ -1490,14 +1677,16 @@ class basic_string
basic_string& replace(size_type pos1, size_type n1,
const basic_string& str, size_type pos2, size_type n2)
{
- if (pos1 > size() || pos2 > str.size())
- this->throw_out_of_range();
- const size_type len1 = container_detail::min_value(n1, size() - pos1);
+ if (pos1 > this->size() || pos2 > str.size())
+ throw_out_of_range("basic_string::replace out of range position");
+ const size_type len1 = container_detail::min_value(n1, this->size() - pos1);
const size_type len2 = container_detail::min_value(n2, str.size() - pos2);
if (this->size() - len1 >= this->max_size() - len2)
- this->throw_length_error();
- return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len1,
- str.priv_addr() + pos2, str.priv_addr() + pos2 + len2);
+ throw_length_error("basic_string::replace max_size() exceeded");
+ const pointer addr = this->priv_addr();
+ const pointer straddr = str.priv_addr();
+ return this->replace(addr + pos1, addr + pos1 + len1,
+ straddr + pos2, straddr + pos2 + len2);
}
//! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
@@ -1514,16 +1703,15 @@ class basic_string
//! if the length of the resulting string would exceed max_size()
//!
//! <b>Returns</b>: *this
- basic_string& replace(size_type pos1, size_type n1,
- const CharT* s, size_type n2)
+ basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
{
- if (pos1 > size())
- this->throw_out_of_range();
- const size_type len = container_detail::min_value(n1, size() - pos1);
+ if (pos1 > this->size())
+ throw_out_of_range("basic_string::replace out of range position");
+ const size_type len = container_detail::min_value(n1, this->size() - pos1);
if (n2 > this->max_size() || size() - len >= this->max_size() - n2)
- this->throw_length_error();
- return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len,
- s, s + n2);
+ throw_length_error("basic_string::replace max_size() exceeded");
+ const pointer addr = this->priv_addr();
+ return this->replace(addr + pos1, addr + pos1 + len, s, s + n2);
}
//! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
@@ -1542,13 +1730,14 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& replace(size_type pos, size_type n1, const CharT* s)
{
- if (pos > size())
- this->throw_out_of_range();
- const size_type len = container_detail::min_value(n1, size() - pos);
+ if (pos > this->size())
+ throw_out_of_range("basic_string::replace out of range position");
+ const size_type len = container_detail::min_value(n1, this->size() - pos);
const size_type n2 = Traits::length(s);
- if (n2 > this->max_size() || size() - len >= this->max_size() - n2)
- this->throw_length_error();
- return this->replace(this->priv_addr() + pos, this->priv_addr() + pos + len,
+ if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
+ throw_length_error("basic_string::replace max_size() exceeded");
+ const pointer addr = this->priv_addr();
+ return this->replace(addr + pos, addr + pos + len,
s, s + Traits::length(s));
}
@@ -1562,12 +1751,13 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
{
- if (pos1 > size())
- this->throw_out_of_range();
- const size_type len = container_detail::min_value(n1, size() - pos1);
- if (n2 > this->max_size() || size() - len >= this->max_size() - n2)
- this->throw_length_error();
- return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len, n2, c);
+ if (pos1 > this->size())
+ throw_out_of_range("basic_string::replace out of range position");
+ const size_type len = container_detail::min_value(n1, this->size() - pos1);
+ if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
+ throw_length_error("basic_string::replace max_size() exceeded");
+ const pointer addr = this->priv_addr();
+ return this->replace(addr + pos1, addr + pos1 + len, n2, c);
}
//! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
@@ -1631,13 +1821,50 @@ class basic_string
//!
//! <b>Returns</b>: *this
template <class InputIter>
- basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2)
- {
- //Dispatch depending on integer/iterator
- const bool aux_boolean = container_detail::is_convertible<InputIter, size_type>::value;
- typedef container_detail::bool_<aux_boolean> Result;
- return this->priv_replace_dispatch(i1, i2, j1, j2, Result());
+ basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
+ #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
+ , typename container_detail::enable_if_c
+ < !container_detail::is_convertible<InputIter, size_type>::value
+ && container_detail::is_input_iterator<InputIter>::value
+ >::type * = 0
+ #endif
+ )
+ {
+ for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
+ Traits::assign(*const_cast<CharT*>(container_detail::to_raw_pointer(i1)), *j1);
+ }
+
+ if (j1 == j2)
+ this->erase(i1, i2);
+ else
+ this->insert(i2, j1, j2);
+ return *this;
+ }
+
+ #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
+ template <class ForwardIter>
+ basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
+ , typename container_detail::enable_if_c
+ < !container_detail::is_convertible<ForwardIter, size_type>::value
+ && !container_detail::is_input_iterator<ForwardIter>::value
+ >::type * = 0
+ )
+ {
+ difference_type n = std::distance(j1, j2);
+ const difference_type len = i2 - i1;
+ if (len >= n) {
+ this->priv_copy(j1, j2, const_cast<CharT*>(container_detail::to_raw_pointer(i1)));
+ this->erase(i1 + n, i2);
+ }
+ else {
+ ForwardIter m = j1;
+ std::advance(m, len);
+ this->priv_copy(j1, m, const_cast<CharT*>(container_detail::to_raw_pointer(i1)));
+ this->insert(i2, m, j2);
+ }
+ return *this;
}
+ #endif
//! <b>Requires</b>: pos <= size()
//!
@@ -1652,9 +1879,9 @@ class basic_string
//! <b>Returns</b>: rlen
size_type copy(CharT* s, size_type n, size_type pos = 0) const
{
- if (pos > size())
- this->throw_out_of_range();
- const size_type len = container_detail::min_value(n, size() - pos);
+ if (pos > this->size())
+ throw_out_of_range("basic_string::copy out of range position");
+ const size_type len = container_detail::min_value(n, this->size() - pos);
Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len);
return len;
}
@@ -1670,22 +1897,34 @@ class basic_string
container_detail::swap_alloc(this->alloc(), x.alloc(), flag);
}
+ //////////////////////////////////////////////
+ //
+ // data access
+ //
+ //////////////////////////////////////////////
+
//! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
//!
- //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
+ //! <b>Returns</b>: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()].
//!
//! <b>Complexity</b>: constant time.
- const CharT* c_str() const
+ const CharT* c_str() const BOOST_CONTAINER_NOEXCEPT
{ return container_detail::to_raw_pointer(this->priv_addr()); }
//! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
//!
- //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
+ //! <b>Returns</b>: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()].
//!
//! <b>Complexity</b>: constant time.
- const CharT* data() const
+ const CharT* data() const BOOST_CONTAINER_NOEXCEPT
{ return container_detail::to_raw_pointer(this->priv_addr()); }
+ //////////////////////////////////////////////
+ //
+ // string operations
+ //
+ //////////////////////////////////////////////
+
//! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
//! of the following conditions obtain: 19 pos <= xpos and xpos + str.size() <= size();
//! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
@@ -1703,12 +1942,13 @@ class basic_string
//! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
size_type find(const CharT* s, size_type pos, size_type n) const
{
- if (pos + n > size())
+ if (pos + n > this->size())
return npos;
else {
- pointer finish = this->priv_addr() + this->priv_size();
+ const pointer addr = this->priv_addr();
+ pointer finish = addr + this->priv_size();
const const_iterator result =
- std::search(container_detail::to_raw_pointer(this->priv_addr() + pos),
+ std::search(container_detail::to_raw_pointer(addr + pos),
container_detail::to_raw_pointer(finish),
s, s + n, Eq_traits<Traits>());
return result != finish ? result - begin() : npos;
@@ -1721,19 +1961,21 @@ class basic_string
//!
//! <b>Returns</b>: find(basic_string(s), pos).
size_type find(const CharT* s, size_type pos = 0) const
- { return find(s, pos, Traits::length(s)); }
+ { return this->find(s, pos, Traits::length(s)); }
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
size_type find(CharT c, size_type pos = 0) const
{
- if (pos >= size())
+ const size_type sz = this->size();
+ if (pos >= sz)
return npos;
else {
- pointer finish = this->priv_addr() + this->priv_size();
+ const pointer addr = this->priv_addr();
+ pointer finish = addr + sz;
const const_iterator result =
- std::find_if(this->priv_addr() + pos, finish,
+ std::find_if(addr + pos, finish,
std::bind2nd(Eq_traits<Traits>(), c));
return result != finish ? result - begin() : npos;
}
@@ -1757,7 +1999,7 @@ class basic_string
//! <b>Returns</b>: rfind(basic_string(s, n), pos).
size_type rfind(const CharT* s, size_type pos, size_type n) const
{
- const size_type len = size();
+ const size_type len = this->size();
if (n > len)
return npos;
@@ -1786,7 +2028,7 @@ class basic_string
//! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
size_type rfind(CharT c, size_type pos = npos) const
{
- const size_type len = size();
+ const size_type len = this->size();
if (len < 1)
return npos;
@@ -1816,14 +2058,15 @@ class basic_string
//! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
size_type find_first_of(const CharT* s, size_type pos, size_type n) const
{
- if (pos >= size())
+ const size_type sz = this->size();
+ if (pos >= sz)
return npos;
else {
- pointer finish = this->priv_addr() + this->priv_size();
- const_iterator result = std::find_first_of(this->priv_addr() + pos, finish,
- s, s + n,
- Eq_traits<Traits>());
- return result != finish ? result - begin() : npos;
+ const pointer addr = this->priv_addr();
+ pointer finish = addr + sz;
+ const_iterator result = std::find_first_of
+ (addr + pos, finish, s, s + n, Eq_traits<Traits>());
+ return result != finish ? result - this->begin() : npos;
}
}
@@ -1860,17 +2103,17 @@ class basic_string
//! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
size_type find_last_of(const CharT* s, size_type pos, size_type n) const
{
- const size_type len = size();
+ const size_type len = this->size();
if (len < 1)
return npos;
else {
- const const_iterator last = this->priv_addr() + container_detail::min_value(len - 1, pos) + 1;
+ const pointer addr = this->priv_addr();
+ const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
const const_reverse_iterator rresult =
std::find_first_of(const_reverse_iterator(last), rend(),
- s, s + n,
- Eq_traits<Traits>());
- return rresult != rend() ? (rresult.base() - 1) - this->priv_addr() : npos;
+ s, s + n, Eq_traits<Traits>());
+ return rresult != rend() ? (rresult.base() - 1) - addr : npos;
}
}
@@ -1906,13 +2149,14 @@ class basic_string
//! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
{
- if (pos > size())
+ if (pos > this->size())
return npos;
else {
- pointer finish = this->priv_addr() + this->priv_size();
- const_iterator result = std::find_if(this->priv_addr() + pos, finish,
- Not_within_traits<Traits>(s, s + n));
- return result != finish ? result - this->priv_addr() : npos;
+ const pointer addr = this->priv_addr();
+ const pointer finish = addr + this->priv_size();
+ const const_iterator result = std::find_if
+ (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
+ return result != finish ? result - addr : npos;
}
}
@@ -1929,12 +2173,13 @@ class basic_string
//! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
size_type find_first_not_of(CharT c, size_type pos = 0) const
{
- if (pos > size())
+ if (pos > this->size())
return npos;
else {
- pointer finish = this->priv_addr() + this->priv_size();
- const_iterator result
- = std::find_if(this->priv_addr() + pos, finish,
+ const pointer addr = this->priv_addr();
+ const pointer finish = addr + this->priv_size();
+ const const_iterator result
+ = std::find_if(addr + pos, finish,
std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
return result != finish ? result - begin() : npos;
}
@@ -1957,7 +2202,7 @@ class basic_string
//! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
{
- const size_type len = size();
+ const size_type len = this->size();
if (len < 1)
return npos;
@@ -1983,13 +2228,13 @@ class basic_string
//! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
size_type find_last_not_of(CharT c, size_type pos = npos) const
{
- const size_type len = size();
+ const size_type len = this->size();
if (len < 1)
return npos;
else {
const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
- const_reverse_iterator rresult =
+ const const_reverse_iterator rresult =
std::find_if(const_reverse_iterator(last), rend(),
std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
@@ -2006,10 +2251,11 @@ class basic_string
//! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
basic_string substr(size_type pos = 0, size_type n = npos) const
{
- if (pos > size())
- this->throw_out_of_range();
- return basic_string(this->priv_addr() + pos,
- this->priv_addr() + pos + container_detail::min_value(n, size() - pos), this->alloc());
+ if (pos > this->size())
+ throw_out_of_range("basic_string::substr out of range position");
+ const pointer addr = this->priv_addr();
+ return basic_string(addr + pos,
+ addr + pos + container_detail::min_value(n, size() - pos), this->alloc());
}
//! <b>Effects</b>: Determines the effective length rlen of the string to copy as
@@ -2022,7 +2268,11 @@ class basic_string
//! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
//! and value > 0 if size() > str.size()
int compare(const basic_string& str) const
- { return s_compare(this->priv_addr(), this->priv_addr() + this->priv_size(), str.priv_addr(), str.priv_addr() + str.priv_size()); }
+ {
+ const pointer addr = this->priv_addr();
+ const pointer str_addr = str.priv_addr();
+ return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
+ }
//! <b>Requires</b>: pos1 <= size()
//!
@@ -2034,11 +2284,13 @@ class basic_string
//! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
int compare(size_type pos1, size_type n1, const basic_string& str) const
{
- if (pos1 > size())
- this->throw_out_of_range();
- return s_compare(this->priv_addr() + pos1,
- this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1),
- str.priv_addr(), str.priv_addr() + str.priv_size());
+ if (pos1 > this->size())
+ throw_out_of_range("basic_string::compare out of range position");
+ const pointer addr = this->priv_addr();
+ const pointer str_addr = str.priv_addr();
+ return s_compare(addr + pos1,
+ addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
+ str_addr, str_addr + str.priv_size());
}
//! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
@@ -2049,21 +2301,26 @@ class basic_string
//! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
//!
//! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
- int compare(size_type pos1, size_type n1,
- const basic_string& str, size_type pos2, size_type n2) const {
- if (pos1 > size() || pos2 > str.size())
- this->throw_out_of_range();
- return s_compare(this->priv_addr() + pos1,
- this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1),
- str.priv_addr() + pos2,
- str.priv_addr() + pos2 + container_detail::min_value(n2, size() - pos2));
+ int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2) const
+ {
+ if (pos1 > this->size() || pos2 > str.size())
+ throw_out_of_range("basic_string::compare out of range position");
+ const pointer addr = this->priv_addr();
+ const pointer str_addr = str.priv_addr();
+ return s_compare(addr + pos1,
+ addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
+ str_addr + pos2,
+ str_addr + pos2 + container_detail::min_value(n2, str.size() - pos2));
}
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: compare(basic_string(s)).
int compare(const CharT* s) const
- { return s_compare(this->priv_addr(), this->priv_addr() + this->priv_size(), s, s + Traits::length(s)); }
+ {
+ const pointer addr = this->priv_addr();
+ return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
+ }
//! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
@@ -2071,13 +2328,13 @@ class basic_string
//! <b>Throws</b>: out_of_range if pos1 > size()
//!
//! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
- int compare(size_type pos1, size_type n1,
- const CharT* s, size_type n2) const
+ int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
{
- if (pos1 > size())
- this->throw_out_of_range();
- return s_compare(this->priv_addr() + pos1,
- this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1),
+ if (pos1 > this->size())
+ throw_out_of_range("basic_string::compare out of range position");
+ const pointer addr = this->priv_addr();
+ return s_compare( addr + pos1,
+ addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
s, s + n2);
}
@@ -2089,8 +2346,35 @@ class basic_string
int compare(size_type pos1, size_type n1, const CharT* s) const
{ return this->compare(pos1, n1, s, Traits::length(s)); }
- /// @cond
+ #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
+ void priv_reserve(size_type res_arg, const bool null_terminate = true)
+ {
+ if (res_arg > this->max_size()){
+ throw_length_error("basic_string::reserve max_size() exceeded");
+ }
+
+ if (this->capacity() < res_arg){
+ size_type n = container_detail::max_value(res_arg, this->size()) + 1;
+ size_type new_cap = this->next_capacity(n);
+ pointer new_start = this->allocation_command
+ (allocate_new, n, new_cap, new_cap).first;
+ size_type new_length = 0;
+
+ const pointer addr = this->priv_addr();
+ new_length += priv_uninitialized_copy
+ (addr, addr + this->priv_size(), new_start);
+ if(null_terminate){
+ this->priv_construct_null(new_start + new_length);
+ }
+ this->deallocate_block();
+ this->is_short(false);
+ this->priv_long_addr(new_start);
+ this->priv_long_size(new_length);
+ this->priv_storage(new_cap);
+ }
+ }
+
static int s_compare(const_pointer f1, const_pointer l1,
const_pointer f2, const_pointer l2)
{
@@ -2109,12 +2393,12 @@ class basic_string
{
//Allocate a new buffer.
size_type real_cap = 0;
- pointer long_addr = this->priv_long_addr();
- size_type long_size = this->priv_long_size();
- size_type long_storage = this->priv_long_storage();
+ const pointer long_addr = this->priv_long_addr();
+ const size_type long_size = this->priv_long_size();
+ const size_type long_storage = this->priv_long_storage();
//We can make this nothrow as chars are always NoThrowCopyables
- try{
- std::pair<pointer, bool> ret = this->allocation_command
+ BOOST_TRY{
+ const std::pair<pointer, bool> ret = this->allocation_command
(allocate_new, long_size+1, long_size+1, real_cap, long_addr);
//Copy and update
Traits::copy( container_detail::to_raw_pointer(ret.first)
@@ -2125,9 +2409,10 @@ class basic_string
//And release old buffer
this->alloc().deallocate(long_addr, long_storage);
}
- catch(...){
+ BOOST_CATCH(...){
return;
}
+ BOOST_CATCH_END
}
template<class AllocVersion>
@@ -2145,55 +2430,12 @@ class basic_string
}
void priv_construct_null(pointer p)
- { this->construct(p, 0); }
-
- static CharT priv_null()
- { return (CharT) 0; }
+ { this->construct(p, CharT(0)); }
// Helper functions used by constructors. It is a severe error for
// any of them to be called anywhere except from within constructors.
void priv_terminate_string()
- { this->priv_construct_null(this->priv_addr() + this->priv_size()); }
-
- template <class InputIter>
- void priv_range_initialize(InputIter f, InputIter l,
- std::input_iterator_tag)
- {
- this->allocate_initial_block(InternalBufferChars);
- this->priv_construct_null(this->priv_addr() + this->priv_size());
- this->append(f, l);
- }
-
- template <class ForwardIter>
- void priv_range_initialize(ForwardIter f, ForwardIter l,
- std::forward_iterator_tag)
- {
- difference_type n = std::distance(f, l);
- this->allocate_initial_block(container_detail::max_value<difference_type>(n+1, InternalBufferChars));
- priv_uninitialized_copy(f, l, this->priv_addr());
- this->priv_size(n);
- this->priv_terminate_string();
- }
-
- template <class InputIter>
- void priv_range_initialize(InputIter f, InputIter l)
- {
- typedef typename std::iterator_traits<InputIter>::iterator_category Category;
- this->priv_range_initialize(f, l, Category());
- }
-
- template <class Integer>
- void priv_initialize_dispatch(Integer n, Integer x, container_detail::true_)
- {
- this->allocate_initial_block(container_detail::max_value<difference_type>(n+1, InternalBufferChars));
- priv_uninitialized_fill_n(this->priv_addr(), n, x);
- this->priv_size(n);
- this->priv_terminate_string();
- }
-
- template <class InputIter>
- void priv_initialize_dispatch(InputIter f, InputIter l, container_detail::false_)
- { this->priv_range_initialize(f, l); }
+ { this->priv_construct_null(this->priv_end_addr()); }
template<class FwdIt, class Count> inline
void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
@@ -2241,154 +2483,6 @@ class basic_string
return (constructed);
}
- template <class Integer>
- basic_string& priv_assign_dispatch(Integer n, Integer x, container_detail::true_)
- { return this->assign((size_type) n, (CharT) x); }
-
- template <class InputIter>
- basic_string& priv_assign_dispatch(InputIter f, InputIter l,
- container_detail::false_)
- {
- size_type cur = 0;
- CharT *ptr = container_detail::to_raw_pointer(this->priv_addr());
- while (f != l && cur != this->priv_size()) {
- Traits::assign(*ptr, *f);
- ++f;
- ++cur;
- ++ptr;
- }
- if (f == l)
- this->erase(this->priv_addr() + cur, this->priv_addr() + this->priv_size());
- else
- this->append(f, l);
- return *this;
- }
-
- template <class InputIter>
- void priv_insert(const_iterator p, InputIter first, InputIter last, std::input_iterator_tag)
- {
- for ( ; first != last; ++first, ++p) {
- p = this->insert(p, *first);
- }
- }
-
- template <class ForwardIter>
- void priv_insert(const_iterator position, ForwardIter first,
- ForwardIter last, std::forward_iterator_tag)
- {
- if (first != last) {
- size_type n = std::distance(first, last);
- size_type remaining = this->capacity() - this->priv_size();
- const size_type old_size = this->size();
- pointer old_start = this->priv_addr();
- bool enough_capacity = false;
- std::pair<pointer, bool> allocation_ret;
- size_type new_cap = 0;
-
- //Check if we have enough capacity
- if (remaining >= n){
- enough_capacity = true;
- }
- else {
- //Otherwise expand current buffer or allocate new storage
- new_cap = this->next_capacity(n);
- allocation_ret = this->allocation_command
- (allocate_new | expand_fwd | expand_bwd, old_size + n + 1,
- new_cap, new_cap, old_start);
-
- //Check forward expansion
- if(old_start == allocation_ret.first){
- enough_capacity = true;
- this->priv_storage(new_cap);
- }
- }
-
- //Reuse same buffer
- if(enough_capacity){
- const size_type elems_after =
- this->priv_size() - (position - this->priv_addr());
- size_type old_length = this->priv_size();
- if (elems_after >= n) {
- pointer pointer_past_last = this->priv_addr() + this->priv_size() + 1;
- priv_uninitialized_copy(this->priv_addr() + (this->priv_size() - n + 1),
- pointer_past_last, pointer_past_last);
-
- this->priv_size(this->priv_size()+n);
- Traits::move(const_cast<CharT*>(container_detail::to_raw_pointer(position + n)),
- container_detail::to_raw_pointer(position),
- (elems_after - n) + 1);
- this->priv_copy(first, last, const_cast<CharT*>(container_detail::to_raw_pointer(position)));
- }
- else {
- ForwardIter mid = first;
- std::advance(mid, elems_after + 1);
-
- priv_uninitialized_copy(mid, last, this->priv_addr() + this->priv_size() + 1);
- this->priv_size(this->priv_size() + (n - elems_after));
- priv_uninitialized_copy
- (position, const_iterator(this->priv_addr() + old_length + 1),
- this->priv_addr() + this->priv_size());
- this->priv_size(this->priv_size() + elems_after);
- this->priv_copy(first, mid, const_cast<CharT*>(container_detail::to_raw_pointer(position)));
- }
- }
- else{
- pointer new_start = allocation_ret.first;
- if(!allocation_ret.second){
- //Copy data to new buffer
- size_type new_length = 0;
- //This can't throw, since characters are POD
- new_length += priv_uninitialized_copy
- (const_iterator(this->priv_addr()), position, new_start);
- new_length += priv_uninitialized_copy
- (first, last, new_start + new_length);
- new_length += priv_uninitialized_copy
- (position, const_iterator(this->priv_addr() + this->priv_size()),
- new_start + new_length);
- this->priv_construct_null(new_start + new_length);
-
- this->deallocate_block();
- this->is_short(false);
- this->priv_long_addr(new_start);
- this->priv_long_size(new_length);
- this->priv_long_storage(new_cap);
- }
- else{
- //value_type is POD, so backwards expansion is much easier
- //than with vector<T>
- value_type *oldbuf = container_detail::to_raw_pointer(old_start);
- value_type *newbuf = container_detail::to_raw_pointer(new_start);
- const value_type *pos = container_detail::to_raw_pointer(position);
- size_type before = pos - oldbuf;
-
- //First move old data
- Traits::move(newbuf, oldbuf, before);
- Traits::move(newbuf + before + n, pos, old_size - before);
- //Now initialize the new data
- priv_uninitialized_copy(first, last, new_start + before);
- this->priv_construct_null(new_start + (old_size + n));
- this->is_short(false);
- this->priv_long_addr(new_start);
- this->priv_long_size(old_size + n);
- this->priv_long_storage(new_cap);
- }
- }
- }
- }
-
- template <class Integer>
- void priv_insert_dispatch(const_iterator p, Integer n, Integer x,
- container_detail::true_)
- { insert(p, (size_type) n, (CharT) x); }
-
- template <class InputIter>
- void priv_insert_dispatch(const_iterator p, InputIter first, InputIter last,
- container_detail::false_)
- {
- typedef typename std::iterator_traits<InputIter>::iterator_category Category;
- priv_insert(p, first, last, Category());
- }
-
template <class InputIterator, class OutIterator>
void priv_copy(InputIterator first, InputIterator last, OutIterator result)
{
@@ -2414,43 +2508,11 @@ class basic_string
return this->priv_replace(first, last, f, l, Category());
}
-
- template <class InputIter>
- basic_string& priv_replace(const_iterator first, const_iterator last,
- InputIter f, InputIter l, std::input_iterator_tag)
- {
- for ( ; first != last && f != l; ++first, ++f)
- Traits::assign(*first, *f);
-
- if (f == l)
- this->erase(first, last);
- else
- this->insert(last, f, l);
- return *this;
- }
-
- template <class ForwardIter>
- basic_string& priv_replace(const_iterator first, const_iterator last,
- ForwardIter f, ForwardIter l,
- std::forward_iterator_tag)
- {
- difference_type n = std::distance(f, l);
- const difference_type len = last - first;
- if (len >= n) {
- this->priv_copy(f, l, const_cast<CharT*>(container_detail::to_raw_pointer(first)));
- this->erase(first + n, last);
- }
- else {
- ForwardIter m = f;
- std::advance(m, len);
- this->priv_copy(f, m, const_cast<CharT*>(container_detail::to_raw_pointer(first)));
- this->insert(last, m, l);
- }
- return *this;
- }
- /// @endcond
+ #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
};
+#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
+
//!Typedef for a basic_string of
//!narrow characters
typedef basic_string
@@ -2467,291 +2529,223 @@ typedef basic_string
,std::allocator<wchar_t> >
wstring;
-/// @cond
-
-template <class CharT, class Traits, class A>
-const typename basic_string<CharT,Traits,A>::size_type
-basic_string<CharT,Traits,A>::npos
- = (typename basic_string<CharT,Traits,A>::size_type) -1;
-
-/// @endcond
+#endif
// ------------------------------------------------------------
// Non-member functions.
// Operator+
-template <class CharT, class Traits, class A>
-inline basic_string<CharT,Traits,A>
-operator+(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT,Traits,Allocator>
+ operator+(const basic_string<CharT,Traits,Allocator>& x
+ ,const basic_string<CharT,Traits,Allocator>& y)
{
- typedef basic_string<CharT,Traits,A> str_t;
+ typedef basic_string<CharT,Traits,Allocator> str_t;
typedef typename str_t::reserve_t reserve_t;
reserve_t reserve;
str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
result.append(x);
result.append(y);
- return boost::move(result);
+ return result;
}
-template <class CharT, class Traits, class A> inline
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
- operator+(
- BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) mx
- , BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) my)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT, Traits, Allocator> operator+
+ ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END mx
+ , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END my)
{
mx += my;
return boost::move(mx);
}
-template <class CharT, class Traits, class A> inline
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
- operator+(
- BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) mx
- , const basic_string<CharT,Traits,A>& y)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT, Traits, Allocator> operator+
+ ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END mx
+ , const basic_string<CharT,Traits,Allocator>& y)
{
mx += y;
return boost::move(mx);
}
-template <class CharT, class Traits, class A> inline
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
- operator+(const basic_string<CharT,Traits,A>& x,
- BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) my)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT, Traits, Allocator> operator+
+ (const basic_string<CharT,Traits,Allocator>& x
+ ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END my)
{
- typedef typename basic_string<CharT,Traits,A>::size_type size_type;
- my.replace(size_type(0), size_type(0), x);
+ my.insert(my.begin(), x.begin(), x.end());
return boost::move(my);
}
-template <class CharT, class Traits, class A>
-inline basic_string<CharT,Traits,A>
-operator+(const CharT* s, const basic_string<CharT,Traits,A>& y)
-{
- typedef basic_string<CharT, Traits, A> str_t;
- typedef typename str_t::reserve_t reserve_t;
- reserve_t reserve;
- const typename str_t::size_type n = Traits::length(s);
- str_t result(reserve, n + y.size());
- result.append(s, s + n);
- result.append(y);
- return boost::move(result);
-}
-
-template <class CharT, class Traits, class A> inline
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
-operator+(const CharT* s,
- BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) my)
-{
- typedef typename basic_string<CharT,Traits,A>::size_type size_type;
- return boost::move(my.replace(size_type(0), size_type(0), s));
-}
-
-template <class CharT, class Traits, class A>
-inline basic_string<CharT,Traits,A>
-operator+(CharT c, const basic_string<CharT,Traits,A>& y)
-{
- typedef basic_string<CharT,Traits,A> str_t;
- typedef typename str_t::reserve_t reserve_t;
- reserve_t reserve;
- str_t result(reserve, 1 + y.size());
- result.push_back(c);
- result.append(y);
- return boost::move(result);
-}
-
-template <class CharT, class Traits, class A> inline
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
-operator+(CharT c,
- BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) my)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT, Traits, Allocator> operator+
+ (const CharT* s, basic_string<CharT, Traits, Allocator> y)
{
- typedef typename basic_string<CharT,Traits,A>::size_type size_type;
- return boost::move(my.replace(size_type(0), size_type(0), &c, &c + 1));
+ y.insert(y.begin(), s, s + Traits::length(s));
+ return y;
}
-template <class CharT, class Traits, class A>
-inline basic_string<CharT,Traits,A>
-operator+(const basic_string<CharT,Traits,A>& x, const CharT* s)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT,Traits,Allocator> operator+
+ (basic_string<CharT,Traits,Allocator> x, const CharT* s)
{
- typedef basic_string<CharT,Traits,A> str_t;
- typedef typename str_t::reserve_t reserve_t;
- reserve_t reserve;
- const typename str_t::size_type n = Traits::length(s);
- str_t result(reserve, x.size() + n, x.get_stored_allocator());
- result.append(x);
- result.append(s, s + n);
- return boost::move(result);
+ x += s;
+ return x;
}
-template <class CharT, class Traits, class A>
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
-operator+(BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) mx
- , const CharT* s)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT,Traits,Allocator> operator+
+ (CharT c, basic_string<CharT,Traits,Allocator> y)
{
- mx += s;
- return boost::move(mx);
-}
-
-template <class CharT, class Traits, class A>
-inline basic_string<CharT,Traits,A>
-operator+(const basic_string<CharT,Traits,A>& x, const CharT c)
-{
- typedef basic_string<CharT,Traits,A> str_t;
- typedef typename str_t::reserve_t reserve_t;
- reserve_t reserve;
- str_t result(reserve, x.size() + 1, x.get_stored_allocator());
- result.append(x);
- result.push_back(c);
- return boost::move(result);
+ y.insert(y.begin(), c);
+ return y;
}
-template <class CharT, class Traits, class A>
-BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A)
-operator+( BOOST_RV_REF_3_TEMPL_ARGS(basic_string, CharT, Traits, A) mx
- , const CharT c)
+template <class CharT, class Traits, class Allocator> inline
+ basic_string<CharT,Traits,Allocator> operator+
+ (basic_string<CharT,Traits,Allocator> x, const CharT c)
{
- mx += c;
- return boost::move(mx);
+ x += c;
+ return x;
}
// Operator== and operator!=
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator==(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y)
+operator==(const basic_string<CharT,Traits,Allocator>& x,
+ const basic_string<CharT,Traits,Allocator>& y)
{
return x.size() == y.size() &&
Traits::compare(x.data(), y.data(), x.size()) == 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator==(const CharT* s, const basic_string<CharT,Traits,A>& y)
+operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
{
- typename basic_string<CharT,Traits,A>::size_type n = Traits::length(s);
+ typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
return n == y.size() && Traits::compare(s, y.data(), n) == 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator==(const basic_string<CharT,Traits,A>& x, const CharT* s)
+operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
{
- typename basic_string<CharT,Traits,A>::size_type n = Traits::length(s);
+ typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
return x.size() == n && Traits::compare(x.data(), s, n) == 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator!=(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y)
+operator!=(const basic_string<CharT,Traits,Allocator>& x,
+ const basic_string<CharT,Traits,Allocator>& y)
{ return !(x == y); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator!=(const CharT* s, const basic_string<CharT,Traits,A>& y)
+operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
{ return !(s == y); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator!=(const basic_string<CharT,Traits,A>& x, const CharT* s)
+operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
{ return !(x == s); }
// Operator< (and also >, <=, and >=).
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<(const basic_string<CharT,Traits,A>& x, const basic_string<CharT,Traits,A>& y)
+operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
{
return x.compare(y) < 0;
-// return basic_string<CharT,Traits,A>
+// return basic_string<CharT,Traits,Allocator>
// ::s_compare(x.begin(), x.end(), y.begin(), y.end()) < 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<(const CharT* s, const basic_string<CharT,Traits,A>& y)
+operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
{
return y.compare(s) > 0;
-// basic_string<CharT,Traits,A>::size_type n = Traits::length(s);
-// return basic_string<CharT,Traits,A>
+// basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
+// return basic_string<CharT,Traits,Allocator>
// ::s_compare(s, s + n, y.begin(), y.end()) < 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<(const basic_string<CharT,Traits,A>& x,
+operator<(const basic_string<CharT,Traits,Allocator>& x,
const CharT* s)
{
return x.compare(s) < 0;
-// basic_string<CharT,Traits,A>::size_type n = Traits::length(s);
-// return basic_string<CharT,Traits,A>
+// basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
+// return basic_string<CharT,Traits,Allocator>
// ::s_compare(x.begin(), x.end(), s, s + n) < 0;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y) {
+operator>(const basic_string<CharT,Traits,Allocator>& x,
+ const basic_string<CharT,Traits,Allocator>& y) {
return y < x;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>(const CharT* s, const basic_string<CharT,Traits,A>& y) {
+operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
return y < s;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>(const basic_string<CharT,Traits,A>& x, const CharT* s)
+operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
{
return s < x;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<=(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y)
+operator<=(const basic_string<CharT,Traits,Allocator>& x,
+ const basic_string<CharT,Traits,Allocator>& y)
{
return !(y < x);
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<=(const CharT* s, const basic_string<CharT,Traits,A>& y)
+operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
{ return !(y < s); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator<=(const basic_string<CharT,Traits,A>& x, const CharT* s)
+operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
{ return !(s < x); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>=(const basic_string<CharT,Traits,A>& x,
- const basic_string<CharT,Traits,A>& y)
+operator>=(const basic_string<CharT,Traits,Allocator>& x,
+ const basic_string<CharT,Traits,Allocator>& y)
{ return !(x < y); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>=(const CharT* s, const basic_string<CharT,Traits,A>& y)
+operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
{ return !(s < y); }
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline bool
-operator>=(const basic_string<CharT,Traits,A>& x, const CharT* s)
+operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
{ return !(x < s); }
// Swap.
-template <class CharT, class Traits, class A>
-inline void swap(basic_string<CharT,Traits,A>& x, basic_string<CharT,Traits,A>& y)
+template <class CharT, class Traits, class Allocator>
+inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
{ x.swap(y); }
-/// @cond
-// I/O.
+#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
+// I/O.
namespace container_detail {
template <class CharT, class Traits>
@@ -2770,28 +2764,28 @@ string_fill(std::basic_ostream<CharT, Traits>& os,
}
} //namespace container_detail {
-/// @endcond
+#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
-operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,A>& s)
+operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
{
typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
bool ok = false;
if (sentry) {
ok = true;
- typename basic_string<CharT,Traits,A>::size_type n = s.size();
- typename basic_string<CharT,Traits,A>::size_type pad_len = 0;
+ typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
+ typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
const bool left = (os.flags() & std::ios::left) != 0;
const std::size_t w = os.width(0);
std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
if (w != 0 && n < w)
pad_len = w - n;
-
+
if (!left)
- ok = container_detail::string_fill(os, buf, pad_len);
+ ok = container_detail::string_fill(os, buf, pad_len);
ok = ok &&
buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
@@ -2807,9 +2801,9 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Trait
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
std::basic_istream<CharT, Traits>&
-operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,A>& s)
+operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
{
typename std::basic_istream<CharT, Traits>::sentry sentry(is);
@@ -2843,7 +2837,7 @@ operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,A>&
s.push_back(c);
}
}
-
+
// If we have read no characters, then set failbit.
if (s.size() == 0)
is.setstate(std::ios_base::failbit);
@@ -2854,11 +2848,11 @@ operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,A>&
return is;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
std::basic_istream<CharT, Traits>&
-getline(std::istream& is, basic_string<CharT,Traits,A>& s,CharT delim)
+getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
{
- typename basic_string<CharT,Traits,A>::size_type nread = 0;
+ typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
if (sentry) {
std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
@@ -2886,36 +2880,38 @@ getline(std::istream& is, basic_string<CharT,Traits,A>& s,CharT delim)
return is;
}
-template <class CharT, class Traits, class A>
+template <class CharT, class Traits, class Allocator>
inline std::basic_istream<CharT, Traits>&
-getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,A>& s)
+getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
{
return getline(is, s, '\n');
}
-template <class Ch, class A>
-inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, A> const& v)
+template <class Ch, class Allocator>
+inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, Allocator> const& v)
{
return hash_range(v.begin(), v.end());
}
}}
-/// @cond
+#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
namespace boost {
-/*
+
+template <class T>
+struct has_trivial_destructor_after_move;
+
//!has_trivial_destructor_after_move<> == true_type
//!specialization for optimizations
-template <class C, class T, class A>
-struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, A> >
-{
- static const bool value = has_trivial_destructor<A>::value;
-};
-*/
+template <class C, class T, class Allocator>
+struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
+ : public ::boost::has_trivial_destructor_after_move<Allocator>
+{};
+
}
-/// @endcond
+#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
#include <boost/container/detail/config_end.hpp>