Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@101: // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/container for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_CONTAINER_STRING_HPP Chris@16: #define BOOST_CONTAINER_STRING_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: // container Chris@101: #include Chris@101: #include //new_allocator Chris@101: #include Chris@101: // container/detail Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: Chris@101: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: Chris@101: Chris@16: #include Chris@101: #include //bind2nd, etc. Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace container { Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: namespace container_detail { Chris@16: // ------------------------------------------------------------ Chris@101: // Class basic_string_base. Chris@16: Chris@16: // basic_string_base is a helper class that makes it it easier to write Chris@16: // an exception-safe version of basic_string. The constructor allocates, Chris@16: // but does not initialize, a block of memory. The destructor Chris@16: // deallocates, but does not destroy elements within, a block of Chris@16: // memory. The destructor assumes that the memory either is the internal buffer, Chris@16: // or else points to a block of memory that was allocated using string_base's Chris@16: // allocator and whose size is this->m_storage. Chris@16: template Chris@16: class basic_string_base Chris@16: { Chris@101: basic_string_base & operator=(const basic_string_base &); Chris@101: basic_string_base(const basic_string_base &); Chris@16: Chris@16: typedef allocator_traits allocator_traits_type; Chris@16: public: Chris@101: typedef Allocator allocator_type; Chris@16: typedef allocator_type stored_allocator_type; Chris@16: typedef typename allocator_traits_type::pointer pointer; Chris@16: typedef typename allocator_traits_type::value_type value_type; Chris@16: typedef typename allocator_traits_type::size_type size_type; Chris@16: typedef ::boost::intrusive::pointer_traits pointer_traits; Chris@16: Chris@16: basic_string_base() Chris@16: : members_() Chris@16: { init(); } Chris@16: Chris@16: basic_string_base(const allocator_type& a) Chris@16: : members_(a) Chris@16: { init(); } Chris@16: Chris@101: basic_string_base(BOOST_RV_REF(allocator_type) a) Chris@101: : members_(boost::move(a)) Chris@101: { this->init(); } Chris@101: Chris@16: basic_string_base(const allocator_type& a, size_type n) Chris@16: : members_(a) Chris@101: { Chris@16: this->init(); Chris@16: this->allocate_initial_block(n); Chris@16: } Chris@16: Chris@16: ~basic_string_base() Chris@101: { Chris@16: if(!this->is_short()){ Chris@16: this->deallocate_block(); Chris@16: this->is_short(true); Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: //This is the structure controlling a long string Chris@16: struct long_t Chris@16: { Chris@16: size_type is_short : 1; Chris@16: size_type length : (sizeof(size_type)*CHAR_BIT - 1); Chris@16: size_type storage; Chris@16: pointer start; Chris@16: Chris@16: long_t() Chris@16: {} Chris@16: Chris@16: long_t(const long_t &other) Chris@16: { Chris@16: this->is_short = other.is_short; Chris@16: length = other.length; Chris@16: storage = other.storage; Chris@16: start = other.start; Chris@16: } Chris@16: Chris@16: long_t &operator =(const long_t &other) Chris@16: { Chris@16: this->is_short = other.is_short; Chris@16: length = other.length; Chris@16: storage = other.storage; Chris@16: start = other.start; Chris@16: return *this; Chris@16: } Chris@16: }; Chris@16: Chris@16: //This type is the first part of the structure controlling a short string Chris@16: //The "data" member stores Chris@16: struct short_header Chris@16: { Chris@16: unsigned char is_short : 1; Chris@16: unsigned char length : (CHAR_BIT - 1); Chris@16: }; Chris@16: Chris@16: //This type has the same alignment and size as long_t but it's POD Chris@16: //so, unlike long_t, it can be placed in a union Chris@101: Chris@101: typedef typename container_detail::aligned_storage Chris@101: ::value>::type long_raw_t; Chris@16: Chris@16: protected: Chris@16: static const size_type MinInternalBufferChars = 8; Chris@16: static const size_type AlignmentOfValueType = Chris@16: alignment_of::value; Chris@16: static const size_type ShortDataOffset = Chris@16: container_detail::ct_rounded_size::value; Chris@16: static const size_type ZeroCostInternalBufferChars = Chris@16: (sizeof(long_t) - ShortDataOffset)/sizeof(value_type); Chris@16: static const size_type UnalignedFinalInternalBufferChars = Chris@16: (ZeroCostInternalBufferChars > MinInternalBufferChars) ? Chris@16: ZeroCostInternalBufferChars : MinInternalBufferChars; Chris@16: Chris@16: struct short_t Chris@16: { Chris@16: short_header h; Chris@16: value_type data[UnalignedFinalInternalBufferChars]; Chris@16: }; Chris@16: Chris@16: union repr_t Chris@16: { Chris@16: long_raw_t r; Chris@16: short_t s; Chris@16: Chris@16: const short_t &short_repr() const Chris@16: { return s; } Chris@16: Chris@16: const long_t &long_repr() const Chris@16: { return *static_cast(static_cast(&r)); } Chris@16: Chris@16: short_t &short_repr() Chris@16: { return s; } Chris@16: Chris@16: long_t &long_repr() Chris@16: { return *static_cast(static_cast(&r)); } Chris@16: }; Chris@16: Chris@16: struct members_holder Chris@16: : public Allocator Chris@16: { Chris@16: members_holder() Chris@16: : Allocator() Chris@16: {} Chris@16: Chris@16: template Chris@16: explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a) Chris@16: : Allocator(boost::forward(a)) Chris@16: {} Chris@16: Chris@16: repr_t m_repr; Chris@16: } members_; Chris@16: Chris@16: const Allocator &alloc() const Chris@16: { return members_; } Chris@16: Chris@16: Allocator &alloc() Chris@16: { return members_; } Chris@16: Chris@16: static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type); Chris@16: Chris@16: private: Chris@16: Chris@16: static const size_type MinAllocation = InternalBufferChars*2; Chris@16: Chris@16: protected: Chris@16: bool is_short() const Chris@16: { return static_cast(this->members_.m_repr.s.h.is_short != 0); } Chris@16: Chris@16: void is_short(bool yes) Chris@16: { Chris@16: const bool was_short = this->is_short(); Chris@16: if(yes && !was_short){ Chris@16: allocator_traits_type::destroy Chris@16: ( this->alloc() Chris@16: , static_cast(static_cast(&this->members_.m_repr.r)) Chris@16: ); Chris@16: this->members_.m_repr.s.h.is_short = true; Chris@16: } Chris@16: else if(!yes && was_short){ Chris@16: allocator_traits_type::construct Chris@16: ( this->alloc() Chris@16: , static_cast(static_cast(&this->members_.m_repr.r)) Chris@16: ); Chris@16: this->members_.m_repr.s.h.is_short = false; Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: void init() Chris@16: { Chris@16: this->members_.m_repr.s.h.is_short = 1; Chris@16: this->members_.m_repr.s.h.length = 0; Chris@16: } Chris@16: Chris@16: protected: Chris@16: Chris@16: typedef container_detail::integral_constant::value> alloc_version; Chris@16: Chris@101: pointer allocation_command(allocation_type command, Chris@16: size_type limit_size, Chris@101: size_type &prefer_in_recvd_out_size, Chris@101: pointer &reuse) Chris@16: { Chris@16: if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){ Chris@101: reuse = 0; Chris@16: command &= ~(expand_fwd | expand_bwd); Chris@16: } Chris@16: return container_detail::allocator_version_traits::allocation_command Chris@101: (this->alloc(), command, limit_size, prefer_in_recvd_out_size, reuse); Chris@16: } Chris@16: Chris@16: size_type next_capacity(size_type additional_objects) const Chris@101: { Chris@101: return next_capacity_calculator Chris@101: :: Chris@101: get( allocator_traits_type::max_size(this->alloc()) Chris@101: , this->priv_storage(), additional_objects ); Chris@101: } Chris@16: Chris@16: void deallocate(pointer p, size_type n) Chris@101: { Chris@16: if (p && (n > InternalBufferChars)) Chris@16: this->alloc().deallocate(p, n); Chris@16: } Chris@16: Chris@16: void construct(pointer p, const value_type &value = value_type()) Chris@16: { Chris@16: allocator_traits_type::construct Chris@16: ( this->alloc() Chris@16: , container_detail::to_raw_pointer(p) Chris@16: , value Chris@16: ); Chris@16: } Chris@16: Chris@16: void destroy(pointer p, size_type n) Chris@16: { Chris@16: value_type *raw_p = container_detail::to_raw_pointer(p); Chris@16: for(; n--; ++raw_p){ Chris@16: allocator_traits_type::destroy( this->alloc(), raw_p); Chris@16: } Chris@16: } Chris@16: Chris@16: void destroy(pointer p) Chris@16: { Chris@16: allocator_traits_type::destroy Chris@16: ( this->alloc() Chris@16: , container_detail::to_raw_pointer(p) Chris@16: ); Chris@16: } Chris@16: Chris@16: void allocate_initial_block(size_type n) Chris@16: { Chris@16: if (n <= this->max_size()) { Chris@16: if(n > InternalBufferChars){ Chris@16: size_type new_cap = this->next_capacity(n); Chris@101: pointer reuse = 0; Chris@101: pointer p = this->allocation_command(allocate_new, n, new_cap, reuse); Chris@16: this->is_short(false); Chris@16: this->priv_long_addr(p); Chris@16: this->priv_long_size(0); Chris@16: this->priv_storage(new_cap); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: throw_length_error("basic_string::allocate_initial_block max_size() exceeded"); Chris@16: } Chris@16: } Chris@16: Chris@16: void deallocate_block() Chris@16: { this->deallocate(this->priv_addr(), this->priv_storage()); } Chris@101: Chris@16: size_type max_size() const Chris@16: { return allocator_traits_type::max_size(this->alloc()) - 1; } Chris@16: Chris@16: protected: Chris@16: size_type priv_capacity() const Chris@16: { return this->priv_storage() - 1; } Chris@16: Chris@16: pointer priv_short_addr() const Chris@16: { return pointer_traits::pointer_to(const_cast(this->members_.m_repr.short_repr().data[0])); } Chris@16: Chris@16: pointer priv_long_addr() const Chris@16: { return this->members_.m_repr.long_repr().start; } Chris@16: Chris@16: pointer priv_addr() const Chris@16: { Chris@16: return this->is_short() Chris@16: ? priv_short_addr() Chris@16: : priv_long_addr() Chris@16: ; Chris@16: } Chris@16: Chris@16: pointer priv_end_addr() const Chris@16: { Chris@16: return this->is_short() Chris@16: ? this->priv_short_addr() + this->priv_short_size() Chris@16: : this->priv_long_addr() + this->priv_long_size() Chris@16: ; Chris@16: } Chris@16: Chris@16: void priv_long_addr(pointer addr) Chris@16: { this->members_.m_repr.long_repr().start = addr; } Chris@16: Chris@16: size_type priv_storage() const Chris@16: { return this->is_short() ? priv_short_storage() : priv_long_storage(); } Chris@16: Chris@16: size_type priv_short_storage() const Chris@16: { return InternalBufferChars; } Chris@16: Chris@16: size_type priv_long_storage() const Chris@16: { return this->members_.m_repr.long_repr().storage; } Chris@16: Chris@16: void priv_storage(size_type storage) Chris@101: { Chris@16: if(!this->is_short()) Chris@16: this->priv_long_storage(storage); Chris@16: } Chris@16: Chris@16: void priv_long_storage(size_type storage) Chris@101: { Chris@16: this->members_.m_repr.long_repr().storage = storage; Chris@16: } Chris@16: Chris@16: size_type priv_size() const Chris@16: { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); } Chris@16: Chris@16: size_type priv_short_size() const Chris@16: { return this->members_.m_repr.short_repr().h.length; } Chris@16: Chris@16: size_type priv_long_size() const Chris@16: { return this->members_.m_repr.long_repr().length; } Chris@16: Chris@16: void priv_size(size_type sz) Chris@101: { Chris@16: if(this->is_short()) Chris@16: this->priv_short_size(sz); Chris@16: else Chris@16: this->priv_long_size(sz); Chris@16: } Chris@16: Chris@16: void priv_short_size(size_type sz) Chris@101: { Chris@16: this->members_.m_repr.s.h.length = (unsigned char)sz; Chris@16: } Chris@16: Chris@16: void priv_long_size(size_type sz) Chris@101: { Chris@16: this->members_.m_repr.long_repr().length = sz; Chris@16: } Chris@16: Chris@16: void swap_data(basic_string_base& other) Chris@16: { Chris@16: if(this->is_short()){ Chris@16: if(other.is_short()){ Chris@101: repr_t tmp(this->members_.m_repr); Chris@101: this->members_.m_repr = other.members_.m_repr; Chris@101: other.members_.m_repr = tmp; Chris@16: } Chris@16: else{ Chris@16: short_t short_backup(this->members_.m_repr.short_repr()); Chris@16: long_t long_backup (other.members_.m_repr.long_repr()); Chris@16: other.members_.m_repr.long_repr().~long_t(); Chris@16: ::new(&this->members_.m_repr.long_repr()) long_t; Chris@16: this->members_.m_repr.long_repr() = long_backup; Chris@16: other.members_.m_repr.short_repr() = short_backup; Chris@16: } Chris@16: } Chris@16: else{ Chris@16: if(other.is_short()){ Chris@16: short_t short_backup(other.members_.m_repr.short_repr()); Chris@16: long_t long_backup (this->members_.m_repr.long_repr()); Chris@16: this->members_.m_repr.long_repr().~long_t(); Chris@16: ::new(&other.members_.m_repr.long_repr()) long_t; Chris@16: other.members_.m_repr.long_repr() = long_backup; Chris@16: this->members_.m_repr.short_repr() = short_backup; Chris@16: } Chris@16: else{ Chris@101: boost::adl_move_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr()); Chris@16: } Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: } //namespace container_detail { Chris@16: Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: //! The basic_string class represents a Sequence of characters. It contains all the Chris@16: //! usual operations of a Sequence, and, additionally, it contains standard string Chris@16: //! operations such as search and concatenation. Chris@16: //! Chris@16: //! The basic_string class is parameterized by character type, and by that type's Chris@16: //! Character Traits. Chris@16: //! Chris@16: //! This class has performance characteristics very much like vector<>, meaning, Chris@16: //! for example, that it does not perform reference-count or copy-on-write, and that Chris@16: //! concatenation of two strings is an O(N) operation. Chris@16: //! Chris@16: //! Some of basic_string's member functions use an unusual method of specifying positions Chris@16: //! and ranges. In addition to the conventional method using iterators, many of Chris@16: //! basic_string's member functions use a single value pos of type size_type to represent a Chris@16: //! position (in which case the position is begin() + pos, and many of basic_string's Chris@16: //! member functions use two values, pos and n, to represent a range. In that case pos is Chris@16: //! the beginning of the range and n is its size. That is, the range is Chris@16: //! [begin() + pos, begin() + pos + n). Chris@16: //! Chris@16: //! Note that the C++ standard does not specify the complexity of basic_string operations. Chris@16: //! In this implementation, basic_string has performance characteristics very similar to Chris@16: //! those of vector: access to a single character is O(1), while copy and concatenation Chris@16: //! are O(N). Chris@16: //! Chris@16: //! In this implementation, begin(), Chris@16: //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators. Chris@16: //! In this implementation, iterators are only invalidated by member functions that Chris@16: //! explicitly change the string's contents. Chris@101: //! Chris@101: //! \tparam CharT The type of character it contains. Chris@101: //! \tparam Traits The Character Traits type, which encapsulates basic character operations Chris@101: //! \tparam Allocator The allocator, used for internal memory management. Chris@16: #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@101: template , class Allocator = new_allocator > Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class basic_string Chris@16: : private container_detail::basic_string_base Chris@16: { Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: private: Chris@16: typedef allocator_traits allocator_traits_type; Chris@16: BOOST_COPYABLE_AND_MOVABLE(basic_string) Chris@16: typedef container_detail::basic_string_base base_t; Chris@16: static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars; Chris@16: Chris@16: protected: Chris@16: // Allocator helper class to use a char_traits as a function object. Chris@16: Chris@16: template Chris@16: struct Eq_traits Chris@16: { Chris@101: //Compatibility with std::binary_function Chris@101: typedef typename Tr::char_type first_argument_type; Chris@101: typedef typename Tr::char_type second_argument_type; Chris@101: typedef bool result_type; Chris@101: Chris@101: bool operator()(const first_argument_type& x, const second_argument_type& y) const Chris@16: { return Tr::eq(x, y); } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct Not_within_traits Chris@16: { Chris@101: typedef typename Tr::char_type argument_type; Chris@101: typedef bool result_type; Chris@101: Chris@16: typedef const typename Tr::char_type* Pointer; Chris@16: const Pointer m_first; Chris@16: const Pointer m_last; Chris@16: Chris@16: Not_within_traits(Pointer f, Pointer l) Chris@16: : m_first(f), m_last(l) {} Chris@16: Chris@16: bool operator()(const typename Tr::char_type& x) const Chris@16: { Chris@16: return std::find_if(m_first, m_last, Chris@16: std::bind1st(Eq_traits(), x)) == m_last; Chris@16: } Chris@16: }; Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // types Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: typedef Traits traits_type; Chris@16: typedef CharT value_type; Chris@16: typedef typename ::boost::container::allocator_traits::pointer pointer; Chris@16: typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; Chris@16: typedef typename ::boost::container::allocator_traits::reference reference; Chris@16: typedef typename ::boost::container::allocator_traits::const_reference const_reference; Chris@16: typedef typename ::boost::container::allocator_traits::size_type size_type; Chris@16: typedef typename ::boost::container::allocator_traits::difference_type difference_type; Chris@16: typedef Allocator allocator_type; Chris@16: typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type; Chris@16: typedef BOOST_CONTAINER_IMPDEF(pointer) iterator; Chris@16: typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator; Chris@101: typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator) reverse_iterator; Chris@101: typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator) const_reverse_iterator; Chris@16: static const size_type npos = size_type(-1); Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: private: Chris@16: typedef constant_iterator cvalue_iterator; Chris@16: typedef typename base_t::alloc_version alloc_version; Chris@16: typedef ::boost::intrusive::pointer_traits pointer_traits; Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: public: // Constructor, destructor, assignment. Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // construct/copy/destroy Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: struct reserve_t {}; Chris@16: Chris@16: basic_string(reserve_t, size_type n, Chris@16: const allocator_type& a = allocator_type()) Chris@16: //Select allocator as in copy constructor as reserve_t-based constructors Chris@16: //are two step copies optimized for capacity Chris@16: : base_t( allocator_traits_type::select_on_container_copy_construction(a) Chris@16: , n + 1) Chris@16: { this->priv_terminate_string(); } Chris@16: Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: //! Effects: Default constructs a basic_string. Chris@16: //! Chris@16: //! Throws: If allocator_type's default constructor throws. Chris@16: basic_string() Chris@16: : base_t() Chris@16: { this->priv_terminate_string(); } Chris@16: Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter. Chris@16: //! Chris@16: //! Throws: Nothing Chris@101: explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: : base_t(a) Chris@16: { this->priv_terminate_string(); } Chris@16: Chris@16: //! Effects: Copy constructs a basic_string. Chris@16: //! Chris@16: //! Postcondition: x == *this. Chris@16: //! Chris@101: //! Throws: If allocator_type's default constructor or allocation throws. Chris@16: basic_string(const basic_string& s) Chris@16: : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc())) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(s.begin(), s.end()); Chris@16: } Chris@16: Chris@16: //! Effects: Move constructor. Moves s's resources to *this. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW Chris@101: : base_t(boost::move(s.alloc())) Chris@101: { Chris@101: if(s.alloc() == this->alloc()){ Chris@101: this->swap_data(s); Chris@101: } Chris@101: else{ Chris@101: this->assign(s.begin(), s.end()); Chris@101: } Chris@101: } Chris@16: Chris@16: //! Effects: Copy constructs a basic_string using the specified allocator. Chris@16: //! Chris@16: //! Postcondition: x == *this. Chris@16: //! Chris@16: //! Throws: If allocation throws. Chris@16: basic_string(const basic_string& s, const allocator_type &a) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(s.begin(), s.end()); Chris@16: } Chris@16: Chris@16: //! Effects: Move constructor using the specified allocator. Chris@16: //! Moves s's resources to *this. Chris@16: //! Chris@16: //! Throws: If allocation throws. Chris@16: //! Chris@16: //! Complexity: Constant if a == s.get_allocator(), linear otherwise. Chris@16: basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: if(a == this->alloc()){ Chris@16: this->swap_data(s); Chris@16: } Chris@16: else{ Chris@16: this->assign(s.begin(), s.end()); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@16: //! and is initialized by a specific number of characters of the s string. Chris@16: basic_string(const basic_string& s, size_type pos, size_type n = npos, Chris@16: const allocator_type& a = allocator_type()) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: if (pos > s.size()) Chris@16: throw_out_of_range("basic_string::basic_string out of range position"); Chris@16: else Chris@16: this->assign Chris@16: (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos)); Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@16: //! and is initialized by a specific number of characters of the s c-string. Chris@16: basic_string(const CharT* s, size_type n, const allocator_type& a = allocator_type()) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(s, s + n); Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@16: //! and is initialized by the null-terminated s c-string. Chris@16: basic_string(const CharT* s, const allocator_type& a = allocator_type()) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(s, s + Traits::length(s)); Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@16: //! and is initialized by n copies of c. Chris@16: basic_string(size_type n, CharT c, const allocator_type& a = allocator_type()) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(n, c); Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@101: //! and is initialized by n default-initialized characters. Chris@101: basic_string(size_type n, default_init_t, const allocator_type& a = allocator_type()) Chris@101: : base_t(a, n + 1) Chris@101: { Chris@101: this->priv_size(n); Chris@101: this->priv_terminate_string(); Chris@101: } Chris@101: Chris@101: //! Effects: Constructs a basic_string taking the allocator as parameter, Chris@16: //! and a range of iterators. Chris@16: template Chris@16: basic_string(InputIterator f, InputIterator l, const allocator_type& a = allocator_type()) Chris@16: : base_t(a) Chris@16: { Chris@16: this->priv_terminate_string(); Chris@16: this->assign(f, l); Chris@16: } Chris@16: Chris@16: //! Effects: Destroys the basic_string. All used memory is deallocated. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: ~basic_string() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: {} Chris@101: Chris@16: //! Effects: Copy constructs a string. Chris@16: //! Chris@16: //! Postcondition: x == *this. Chris@16: //! Chris@16: //! Complexity: Linear to the elements x contains. Chris@16: basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x) Chris@16: { Chris@16: if (&x != this){ Chris@16: allocator_type &this_alloc = this->alloc(); Chris@16: const allocator_type &x_alloc = x.alloc(); Chris@16: container_detail::bool_ flag; Chris@16: if(flag && this_alloc != x_alloc){ Chris@16: if(!this->is_short()){ Chris@16: this->deallocate_block(); Chris@16: this->is_short(true); Chris@16: Traits::assign(*this->priv_addr(), CharT(0)); Chris@16: this->priv_short_size(0); Chris@16: } Chris@16: } Chris@16: container_detail::assign_alloc(this->alloc(), x.alloc(), flag); Chris@16: this->assign(x.begin(), x.end()); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@101: //! Effects: Move constructor. Moves x's resources to *this. Chris@16: //! Chris@101: //! Throws: If allocator_traits_type::propagate_on_container_move_assignment Chris@101: //! is false and allocation throws Chris@16: //! Chris@101: //! Complexity: Constant if allocator_traits_type:: Chris@101: //! propagate_on_container_move_assignment is true or Chris@101: //! this->get>allocator() == x.get_allocator(). Linear otherwise. Chris@101: basic_string& operator=(BOOST_RV_REF(basic_string) x) Chris@101: BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value Chris@101: || allocator_traits_type::is_always_equal::value) Chris@16: { Chris@101: //for move constructor, no aliasing (&x != this) is assummed. Chris@101: BOOST_ASSERT(this != &x); Chris@101: allocator_type &this_alloc = this->alloc(); Chris@101: allocator_type &x_alloc = x.alloc(); Chris@101: const bool propagate_alloc = allocator_traits_type:: Chris@101: propagate_on_container_move_assignment::value; Chris@101: container_detail::bool_ flag; Chris@101: const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; Chris@101: //Resources can be transferred if both allocators are Chris@101: //going to be equal after this function (either propagated or already equal) Chris@101: if(propagate_alloc || allocators_equal){ Chris@101: //Destroy objects but retain memory in case x reuses it in the future Chris@101: this->clear(); Chris@101: //Move allocator if needed Chris@101: container_detail::move_alloc(this_alloc, x_alloc, flag); Chris@101: //Nothrow swap Chris@101: this->swap_data(x); Chris@101: } Chris@101: //Else do a one by one move Chris@101: else{ Chris@101: this->assign( x.begin(), x.end()); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Effects: Assignment from a null-terminated c-string. Chris@16: basic_string& operator=(const CharT* s) Chris@16: { return this->assign(s, s + Traits::length(s)); } Chris@16: Chris@16: //! Effects: Assignment from character. Chris@16: basic_string& operator=(CharT c) Chris@16: { return this->assign(static_cast(1), c); } Chris@16: Chris@16: //! Effects: Returns a copy of the internal allocator. Chris@16: //! Chris@16: //! Throws: If allocator's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->alloc(); } Chris@16: Chris@16: //! Effects: Returns a reference to the internal allocator. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Non-standard extension. Chris@101: stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->alloc(); } Chris@16: Chris@16: //! Effects: Returns a reference to the internal allocator. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Non-standard extension. Chris@101: const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->alloc(); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // iterators Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Effects: Returns an iterator to the first element contained in the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: iterator begin() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_addr(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the first element contained in the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_addr(); } Chris@16: Chris@16: //! Effects: Returns an iterator to the end of the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: iterator end() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_end_addr(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the end of the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_end_addr(); } Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the beginning Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return reverse_iterator(this->priv_end_addr()); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->crbegin(); } Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the end Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return reverse_iterator(this->priv_addr()); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->crend(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the first element contained in the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_addr(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the end of the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_end_addr(); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_reverse_iterator(this->priv_end_addr()); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_reverse_iterator(this->priv_addr()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // capacity Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Effects: Returns true if the vector contains no elements. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: bool empty() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return !this->priv_size(); } Chris@16: Chris@16: //! Effects: Returns the number of the elements contained in the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: size_type size() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_size(); } Chris@16: Chris@16: //! Effects: Returns the number of the elements contained in the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: size_type length() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->size(); } Chris@16: Chris@16: //! Effects: Returns the largest possible size of the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return base_t::max_size(); } Chris@16: Chris@16: //! Effects: Inserts or erases elements at the end such that Chris@16: //! the size becomes n. New elements are copy constructed from x. Chris@16: //! Chris@16: //! Throws: If memory allocation throws Chris@16: //! Chris@16: //! Complexity: Linear to the difference between size() and new_size. Chris@16: void resize(size_type n, CharT c) Chris@16: { Chris@16: if (n <= this->size()) Chris@16: this->erase(this->begin() + n, this->end()); Chris@16: else Chris@16: this->append(n - this->size(), c); Chris@16: } Chris@16: Chris@16: //! Effects: Inserts or erases elements at the end such that Chris@16: //! the size becomes n. New elements are value initialized. Chris@16: //! Chris@16: //! Throws: If memory allocation throws Chris@16: //! Chris@16: //! Complexity: Linear to the difference between size() and new_size. Chris@16: void resize(size_type n) Chris@16: { resize(n, CharT()); } Chris@16: Chris@101: Chris@101: //! Effects: Inserts or erases elements at the end such that Chris@101: //! the size becomes n. New elements are uninitialized. Chris@101: //! Chris@101: //! Throws: If memory allocation throws Chris@101: //! Chris@101: //! Complexity: Linear to the difference between size() and new_size. Chris@101: //! Chris@101: //! Note: Non-standard extension Chris@101: void resize(size_type n, default_init_t) Chris@101: { Chris@101: if (n <= this->size()) Chris@101: this->erase(this->begin() + n, this->end()); Chris@101: else{ Chris@101: this->priv_reserve(n, false); Chris@101: this->priv_size(n); Chris@101: this->priv_terminate_string(); Chris@101: } Chris@101: } Chris@101: Chris@16: //! Effects: Number of elements for which memory has been allocated. Chris@16: //! capacity() is always greater than or equal to size(). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->priv_capacity(); } Chris@16: Chris@16: //! Effects: If n is less than or equal to capacity(), this call has no Chris@16: //! effect. Otherwise, it is a request for allocation of additional memory. Chris@16: //! If the request is successful, then capacity() is greater than or equal to Chris@16: //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. Chris@16: //! Chris@16: //! Throws: If memory allocation allocation throws Chris@16: void reserve(size_type res_arg) Chris@101: { this->priv_reserve(res_arg); } Chris@16: Chris@16: //! Effects: Tries to deallocate the excess of memory created Chris@16: //! with previous allocations. The size of the string is unchanged Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Linear to size(). Chris@16: void shrink_to_fit() Chris@16: { Chris@16: //Check if shrinking is possible Chris@16: if(this->priv_storage() > InternalBufferChars){ Chris@16: //Check if we should pass from dynamically allocated buffer Chris@16: //to the internal storage Chris@16: if(this->priv_size() < InternalBufferChars){ Chris@16: //Dynamically allocated buffer attributes Chris@16: pointer long_addr = this->priv_long_addr(); Chris@16: size_type long_storage = this->priv_long_storage(); Chris@16: size_type long_size = this->priv_long_size(); Chris@16: //Shrink from allocated buffer to the internal one, including trailing null Chris@16: Traits::copy( container_detail::to_raw_pointer(this->priv_short_addr()) Chris@16: , container_detail::to_raw_pointer(long_addr) Chris@16: , long_size+1); Chris@16: this->is_short(true); Chris@16: this->alloc().deallocate(long_addr, long_storage); Chris@16: } Chris@16: else{ Chris@16: //Shrinking in dynamic buffer Chris@16: this->priv_shrink_to_fit_dynamic_buffer(alloc_version()); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // element access Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Requires: size() > n. Chris@16: //! Chris@16: //! Effects: Returns a reference to the nth element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *(this->priv_addr() + n); } Chris@16: Chris@16: //! Requires: size() > n. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the nth element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *(this->priv_addr() + n); } Chris@16: Chris@16: //! Requires: size() > n. Chris@16: //! Chris@16: //! Effects: Returns a reference to the nth element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: std::range_error if n >= size() Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: reference at(size_type n) Chris@16: { Chris@16: if (n >= this->size()) Chris@16: throw_out_of_range("basic_string::at invalid subscript"); Chris@16: return *(this->priv_addr() + n); Chris@16: } Chris@16: Chris@16: //! Requires: size() > n. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the nth element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: std::range_error if n >= size() Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reference at(size_type n) const { Chris@16: if (n >= this->size()) Chris@16: throw_out_of_range("basic_string::at invalid subscript"); Chris@16: return *(this->priv_addr() + n); Chris@16: } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // modifiers Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Effects: Calls append(str.data, str.size()). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& operator+=(const basic_string& s) Chris@16: { return this->append(s); } Chris@16: Chris@16: //! Effects: Calls append(s). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& operator+=(const CharT* s) Chris@16: { return this->append(s); } Chris@16: Chris@16: //! Effects: Calls append(1, c). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& operator+=(CharT c) Chris@16: { this->push_back(c); return *this; } Chris@16: Chris@16: //! Effects: Calls append(str.data(), str.size()). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& append(const basic_string& s) Chris@16: { return this->append(s.begin(), s.end()); } Chris@16: Chris@16: //! Requires: pos <= str.size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to append Chris@16: //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen). Chris@16: //! Chris@16: //! Throws: If memory allocation throws and out_of_range if pos > str.size() Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& append(const basic_string& s, size_type pos, size_type n) Chris@16: { Chris@16: if (pos > s.size()) Chris@16: throw_out_of_range("basic_string::append out of range position"); Chris@16: return this->append(s.begin() + pos, Chris@16: s.begin() + pos + container_detail::min_value(n, s.size() - pos)); Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Effects: The function replaces the string controlled by *this with Chris@16: //! a string of length size() + n whose irst size() elements are a copy of the Chris@16: //! original string controlled by *this and whose remaining Chris@16: //! elements are a copy of the initial n elements of s. Chris@16: //! Chris@16: //! Throws: If memory allocation throws length_error if size() + n > max_size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& append(const CharT* s, size_type n) Chris@16: { return this->append(s, s + n); } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Effects: Calls append(s, traits::length(s)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& append(const CharT* s) Chris@16: { return this->append(s, s + Traits::length(s)); } Chris@16: Chris@16: //! Effects: Equivalent to append(basic_string(n, c)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& append(size_type n, CharT c) Chris@16: { return this->append(cvalue_iterator(c, n), cvalue_iterator()); } Chris@16: Chris@16: //! Requires: [first,last) is a valid range. Chris@16: //! Chris@16: //! Effects: Equivalent to append(basic_string(first, last)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: template Chris@16: basic_string& append(InputIter first, InputIter last) Chris@16: { this->insert(this->end(), first, last); return *this; } Chris@16: Chris@16: //! Effects: Equivalent to append(static_cast(1), c). Chris@16: void push_back(CharT c) Chris@16: { Chris@16: const size_type old_size = this->priv_size(); Chris@16: if (old_size < this->capacity()){ Chris@16: const pointer addr = this->priv_addr(); Chris@16: this->priv_construct_null(addr + old_size + 1); Chris@16: Traits::assign(addr[old_size], c); Chris@16: this->priv_size(old_size+1); Chris@16: } Chris@16: else{ Chris@16: //No enough memory, insert a new object at the end Chris@16: this->append(size_type(1), c); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Equivalent to assign(str, 0, npos). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& assign(const basic_string& s) Chris@16: { return this->operator=(s); } Chris@16: Chris@16: //! Effects: The function replaces the string controlled by *this Chris@16: //! with a string of length str.size() whose elements are a copy of the string Chris@16: //! controlled by str. Leaves str in a valid but unspecified state. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: *this Chris@101: basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->swap_data(ms), *this; } Chris@16: Chris@16: //! Requires: pos <= str.size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to assign as Chris@16: //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen). Chris@16: //! Chris@16: //! Throws: If memory allocation throws or out_of_range if pos > str.size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& assign(const basic_string& s, size_type pos, size_type n) Chris@16: { Chris@16: if (pos > s.size()) Chris@16: throw_out_of_range("basic_string::assign out of range position"); Chris@16: return this->assign(s.begin() + pos, Chris@16: s.begin() + pos + container_detail::min_value(n, s.size() - pos)); Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Effects: Replaces the string controlled by *this with a string of Chris@16: //! length n whose elements are a copy of those pointed to by s. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or length_error if n > max_size(). Chris@101: //! Chris@16: //! Returns: *this Chris@16: basic_string& assign(const CharT* s, size_type n) Chris@16: { return this->assign(s, s + n); } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Effects: Calls assign(s, traits::length(s)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& assign(const CharT* s) Chris@16: { return this->assign(s, s + Traits::length(s)); } Chris@16: Chris@16: //! Effects: Equivalent to assign(basic_string(n, c)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& assign(size_type n, CharT c) Chris@16: { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); } Chris@16: Chris@16: //! Effects: Equivalent to assign(basic_string(first, last)). Chris@101: //! Chris@101: //! Returns: *this Chris@101: basic_string& assign(const CharT* first, const CharT* last) Chris@101: { Chris@101: size_type n = static_cast(last - first); Chris@101: this->reserve(n); Chris@101: CharT* ptr = container_detail::to_raw_pointer(this->priv_addr()); Chris@101: Traits::copy(ptr, first, n); Chris@101: this->priv_construct_null(ptr + n); Chris@101: this->priv_size(n); Chris@101: return *this; Chris@101: } Chris@101: Chris@101: //! Effects: Equivalent to assign(basic_string(first, last)). Chris@16: //! Chris@16: //! Returns: *this Chris@16: template Chris@16: basic_string& assign(InputIter first, InputIter last Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: >::type * = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: size_type cur = 0; Chris@16: const pointer addr = this->priv_addr(); Chris@16: CharT *ptr = container_detail::to_raw_pointer(addr); Chris@16: const size_type old_size = this->priv_size(); Chris@16: while (first != last && cur != old_size) { Chris@16: Traits::assign(*ptr, *first); Chris@16: ++first; Chris@16: ++cur; Chris@16: ++ptr; Chris@16: } Chris@16: if (first == last) Chris@16: this->erase(addr + cur, addr + old_size); Chris@16: else Chris@16: this->append(first, last); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: pos <= size(). Chris@16: //! Chris@16: //! Effects: Calls insert(pos, str.data(), str.size()). Chris@16: //! Chris@16: //! Throws: If memory allocation throws or out_of_range if pos > size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& insert(size_type pos, const basic_string& s) Chris@16: { Chris@16: const size_type sz = this->size(); Chris@16: if (pos > sz) Chris@16: throw_out_of_range("basic_string::insert out of range position"); Chris@16: if (sz > this->max_size() - s.size()) Chris@16: throw_length_error("basic_string::insert max_size() exceeded"); Chris@16: this->insert(this->priv_addr() + pos, s.begin(), s.end()); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() and pos2 <= str.size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to insert as Chris@16: //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen). Chris@16: //! Chris@16: //! Throws: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n) Chris@16: { Chris@16: const size_type sz = this->size(); Chris@16: const size_type str_size = s.size(); Chris@16: if (pos1 > sz || pos2 > str_size) Chris@16: throw_out_of_range("basic_string::insert out of range position"); Chris@16: size_type len = container_detail::min_value(n, str_size - pos2); Chris@16: if (sz > this->max_size() - len) Chris@16: throw_length_error("basic_string::insert max_size() exceeded"); Chris@16: const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2; Chris@16: const CharT *end_ptr = beg_ptr + len; Chris@16: this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT and pos <= size(). Chris@16: //! Chris@16: //! Effects: Replaces the string controlled by *this with a string of length size() + n Chris@16: //! whose first pos elements are a copy of the initial elements of the original string Chris@16: //! controlled by *this and whose next n elements are a copy of the elements in s and whose Chris@16: //! remaining elements are a copy of the remaining elements of the original string controlled by *this. Chris@16: //! Chris@16: //! Throws: If memory allocation throws, out_of_range if pos > size() or Chris@16: //! length_error if size() + n > max_size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& insert(size_type pos, const CharT* s, size_type n) Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::insert out of range position"); Chris@16: if (this->size() > this->max_size() - n) Chris@16: throw_length_error("basic_string::insert max_size() exceeded"); Chris@16: this->insert(this->priv_addr() + pos, s, s + n); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT Chris@16: //! Chris@16: //! Effects: Calls insert(pos, s, traits::length(s)). Chris@16: //! Chris@16: //! Throws: If memory allocation throws, out_of_range if pos > size() Chris@16: //! length_error if size() > max_size() - Traits::length(s) Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& insert(size_type pos, const CharT* s) Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::insert out of range position"); Chris@16: size_type len = Traits::length(s); Chris@16: if (this->size() > this->max_size() - len) Chris@16: throw_length_error("basic_string::insert max_size() exceeded"); Chris@16: this->insert(this->priv_addr() + pos, s, s + len); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Effects: Equivalent to insert(pos, basic_string(n, c)). Chris@16: //! Chris@16: //! Throws: If memory allocation throws, out_of_range if pos > size() Chris@16: //! length_error if size() > max_size() - n Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& insert(size_type pos, size_type n, CharT c) Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::insert out of range position"); Chris@16: if (this->size() > this->max_size() - n) Chris@16: throw_length_error("basic_string::insert max_size() exceeded"); Chris@16: this->insert(const_iterator(this->priv_addr() + pos), n, c); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: p is a valid iterator on *this. Chris@16: //! Chris@16: //! Effects: inserts a copy of c before the character referred to by p. Chris@16: //! Chris@16: //! Returns: An iterator which refers to the copy of the inserted character. Chris@16: iterator insert(const_iterator p, CharT c) Chris@16: { Chris@16: size_type new_offset = p - this->priv_addr(); Chris@16: this->insert(p, cvalue_iterator(c, 1), cvalue_iterator()); Chris@16: return this->priv_addr() + new_offset; Chris@16: } Chris@16: Chris@16: Chris@16: //! Requires: p is a valid iterator on *this. Chris@16: //! Chris@16: //! Effects: Inserts n copies of c before the character referred to by p. Chris@16: //! Chris@16: //! Returns: an iterator to the first inserted element or p if n is 0. Chris@16: iterator insert(const_iterator p, size_type n, CharT c) Chris@16: { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); } Chris@16: Chris@16: //! Requires: p is a valid iterator on *this. [first,last) is a valid range. Chris@16: //! Chris@16: //! Effects: Equivalent to insert(p - begin(), basic_string(first, last)). Chris@16: //! Chris@16: //! Returns: an iterator to the first inserted element or p if first == last. Chris@16: template Chris@16: iterator insert(const_iterator p, InputIter first, InputIter last Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && container_detail::is_input_iterator::value Chris@16: >::type * = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: const size_type n_pos = p - this->cbegin(); Chris@16: for ( ; first != last; ++first, ++p) { Chris@16: p = this->insert(p, *first); Chris@16: } Chris@101: return this->begin() + n_pos; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: template Chris@16: iterator insert(const_iterator p, ForwardIter first, ForwardIter last Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && !container_detail::is_input_iterator::value Chris@16: >::type * = 0 Chris@16: ) Chris@16: { Chris@16: const size_type n_pos = p - this->cbegin(); Chris@16: if (first != last) { Chris@101: const size_type n = boost::container::iterator_distance(first, last); Chris@16: const size_type old_size = this->priv_size(); Chris@16: const size_type remaining = this->capacity() - old_size; Chris@16: const pointer old_start = this->priv_addr(); Chris@16: bool enough_capacity = false; Chris@16: size_type new_cap = 0; Chris@16: Chris@16: //Check if we have enough capacity Chris@101: pointer hint = pointer(); Chris@101: pointer allocation_ret = pointer(); Chris@16: if (remaining >= n){ Chris@101: enough_capacity = true; Chris@16: } Chris@16: else { Chris@16: //Otherwise expand current buffer or allocate new storage Chris@16: new_cap = this->next_capacity(n); Chris@101: hint = old_start; Chris@16: allocation_ret = this->allocation_command Chris@101: (allocate_new | expand_fwd | expand_bwd, old_size + n + 1, new_cap, hint); Chris@16: Chris@16: //Check forward expansion Chris@101: if(old_start == allocation_ret){ Chris@16: enough_capacity = true; Chris@16: this->priv_storage(new_cap); Chris@16: } Chris@16: } Chris@16: Chris@16: //Reuse same buffer Chris@16: if(enough_capacity){ Chris@16: const size_type elems_after = old_size - (p - old_start); Chris@16: const size_type old_length = old_size; Chris@16: if (elems_after >= n) { Chris@16: const pointer pointer_past_last = old_start + old_size + 1; Chris@16: priv_uninitialized_copy(old_start + (old_size - n + 1), Chris@16: pointer_past_last, pointer_past_last); Chris@16: Chris@16: this->priv_size(old_size+n); Chris@16: Traits::move(const_cast(container_detail::to_raw_pointer(p + n)), Chris@16: container_detail::to_raw_pointer(p), Chris@16: (elems_after - n) + 1); Chris@16: this->priv_copy(first, last, const_cast(container_detail::to_raw_pointer(p))); Chris@16: } Chris@16: else { Chris@16: ForwardIter mid = first; Chris@101: boost::container::iterator_advance(mid, elems_after + 1); Chris@16: Chris@16: priv_uninitialized_copy(mid, last, old_start + old_size + 1); Chris@16: const size_type newer_size = old_size + (n - elems_after); Chris@16: this->priv_size(newer_size); Chris@16: priv_uninitialized_copy Chris@16: (p, const_iterator(old_start + old_length + 1), Chris@16: old_start + newer_size); Chris@16: this->priv_size(newer_size + elems_after); Chris@16: this->priv_copy(first, mid, const_cast(container_detail::to_raw_pointer(p))); Chris@16: } Chris@16: } Chris@16: else{ Chris@101: pointer new_start = allocation_ret; Chris@101: if(!hint){ Chris@16: //Copy data to new buffer Chris@16: size_type new_length = 0; Chris@16: //This can't throw, since characters are POD Chris@16: new_length += priv_uninitialized_copy Chris@16: (const_iterator(old_start), p, new_start); Chris@16: new_length += priv_uninitialized_copy Chris@16: (first, last, new_start + new_length); Chris@16: new_length += priv_uninitialized_copy Chris@16: (p, const_iterator(old_start + old_size), Chris@16: new_start + new_length); Chris@16: this->priv_construct_null(new_start + new_length); Chris@16: Chris@16: this->deallocate_block(); Chris@16: this->is_short(false); Chris@16: this->priv_long_addr(new_start); Chris@16: this->priv_long_size(new_length); Chris@16: this->priv_long_storage(new_cap); Chris@16: } Chris@16: else{ Chris@16: //value_type is POD, so backwards expansion is much easier Chris@16: //than with vector Chris@16: value_type * const oldbuf = container_detail::to_raw_pointer(old_start); Chris@16: value_type * const newbuf = container_detail::to_raw_pointer(new_start); Chris@16: const value_type *const pos = container_detail::to_raw_pointer(p); Chris@16: const size_type before = pos - oldbuf; Chris@16: Chris@16: //First move old data Chris@16: Traits::move(newbuf, oldbuf, before); Chris@16: Traits::move(newbuf + before + n, pos, old_size - before); Chris@16: //Now initialize the new data Chris@16: priv_uninitialized_copy(first, last, new_start + before); Chris@16: this->priv_construct_null(new_start + (old_size + n)); Chris@16: this->is_short(false); Chris@16: this->priv_long_addr(new_start); Chris@16: this->priv_long_size(old_size + n); Chris@16: this->priv_long_storage(new_cap); Chris@16: } Chris@16: } Chris@16: } Chris@16: return this->begin() + n_pos; Chris@16: } Chris@16: #endif Chris@16: Chris@16: //! Requires: pos <= size() Chris@16: //! Chris@16: //! Effects: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos. Chris@16: //! The function then replaces the string controlled by *this with a string of length size() - xlen Chris@16: //! whose first pos elements are a copy of the initial elements of the original string controlled by *this, Chris@16: //! and whose remaining elements are a copy of the elements of the original string controlled by *this Chris@16: //! beginning at position pos + xlen. Chris@16: //! Chris@16: //! Throws: out_of_range if pos > size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& erase(size_type pos = 0, size_type n = npos) Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::erase out of range position"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos)); Chris@16: return *this; Chris@101: } Chris@16: Chris@16: //! Effects: Removes the character referred to by p. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: An iterator which points to the element immediately following p prior to the element being Chris@16: //! erased. If no such element exists, end() is returned. Chris@101: iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: // The move includes the terminating null. Chris@16: CharT * const ptr = const_cast(container_detail::to_raw_pointer(p)); Chris@16: const size_type old_size = this->priv_size(); Chris@16: Traits::move(ptr, Chris@16: container_detail::to_raw_pointer(p + 1), Chris@16: old_size - (p - this->priv_addr())); Chris@16: this->priv_size(old_size-1); Chris@16: return iterator(ptr); Chris@16: } Chris@16: Chris@16: //! Requires: first and last are valid iterators on *this, defining a range [first,last). Chris@16: //! Chris@16: //! Effects: Removes the characters in the range [first,last). Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: An iterator which points to the element pointed to by last prior to Chris@16: //! the other elements being erased. If no such element exists, end() is returned. Chris@101: iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: CharT * f = const_cast(container_detail::to_raw_pointer(first)); Chris@16: if (first != last) { // The move includes the terminating null. Chris@16: const size_type num_erased = last - first; Chris@16: const size_type old_size = this->priv_size(); Chris@16: Traits::move(f, Chris@16: container_detail::to_raw_pointer(last), Chris@16: (old_size + 1)-(last - this->priv_addr())); Chris@16: const size_type new_length = old_size - num_erased; Chris@16: this->priv_size(new_length); Chris@16: } Chris@16: return iterator(f); Chris@16: } Chris@16: Chris@16: //! Requires: !empty() Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Effects: Equivalent to erase(size() - 1, 1). Chris@101: void pop_back() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: const size_type old_size = this->priv_size(); Chris@16: Traits::assign(this->priv_addr()[old_size-1], CharT(0)); Chris@16: this->priv_size(old_size-1);; Chris@16: } Chris@16: Chris@16: //! Effects: Erases all the elements of the vector. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements in the vector. Chris@101: void clear() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: if (!this->empty()) { Chris@16: Traits::assign(*this->priv_addr(), CharT(0)); Chris@16: this->priv_size(0); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size(). Chris@16: //! Chris@16: //! Effects: Calls replace(pos1, n1, str.data(), str.size()). Chris@16: //! Chris@16: //! Throws: if memory allocation throws or out_of_range if pos1 > size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(size_type pos1, size_type n1, const basic_string& str) Chris@16: { Chris@16: if (pos1 > this->size()) Chris@16: throw_out_of_range("basic_string::replace out of range position"); Chris@16: const size_type len = container_detail::min_value(n1, this->size() - pos1); Chris@16: if (this->size() - len >= this->max_size() - str.size()) Chris@16: throw_length_error("basic_string::replace max_size() exceeded"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return this->replace( const_iterator(addr + pos1) Chris@16: , const_iterator(addr + pos1 + len) Chris@16: , str.begin(), str.end()); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() and pos2 <= str.size(). Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to be Chris@16: //! inserted as the smaller of n2 and str.size() - pos2 and calls Chris@16: //! replace(pos1, n1, str.data() + pos2, rlen). Chris@16: //! Chris@16: //! Throws: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size(). Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(size_type pos1, size_type n1, Chris@16: const basic_string& str, size_type pos2, size_type n2) Chris@16: { Chris@16: if (pos1 > this->size() || pos2 > str.size()) Chris@16: throw_out_of_range("basic_string::replace out of range position"); Chris@16: const size_type len1 = container_detail::min_value(n1, this->size() - pos1); Chris@16: const size_type len2 = container_detail::min_value(n2, str.size() - pos2); Chris@16: if (this->size() - len1 >= this->max_size() - len2) Chris@16: throw_length_error("basic_string::replace max_size() exceeded"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer straddr = str.priv_addr(); Chris@16: return this->replace(addr + pos1, addr + pos1 + len1, Chris@16: straddr + pos2, straddr + pos2 + len2); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() and s points to an array of at least n2 elements of CharT. Chris@16: //! Chris@16: //! Effects: Determines the effective length xlen of the string to be removed as the Chris@16: //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Chris@16: //! Otherwise, the function replaces the string controlled by *this with a string of Chris@16: //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements Chris@16: //! of the original string controlled by *this, whose next n2 elements are a copy of the Chris@16: //! initial n2 elements of s, and whose remaining elements are a copy of the elements of Chris@16: //! the original string controlled by *this beginning at position pos + xlen. Chris@16: //! Chris@16: //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error Chris@16: //! if the length of the resulting string would exceed max_size() Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2) Chris@16: { Chris@16: if (pos1 > this->size()) Chris@16: throw_out_of_range("basic_string::replace out of range position"); Chris@16: const size_type len = container_detail::min_value(n1, this->size() - pos1); Chris@16: if (n2 > this->max_size() || size() - len >= this->max_size() - n2) Chris@16: throw_length_error("basic_string::replace max_size() exceeded"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return this->replace(addr + pos1, addr + pos1 + len, s, s + n2); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() and s points to an array of at least n2 elements of CharT. Chris@16: //! Chris@16: //! Effects: Determines the effective length xlen of the string to be removed as the smaller Chris@16: //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise, Chris@16: //! the function replaces the string controlled by *this with a string of length size() - xlen + n2 Chris@16: //! whose first pos1 elements are a copy of the initial elements of the original string controlled Chris@16: //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose Chris@16: //! remaining elements are a copy of the elements of the original string controlled by *this Chris@16: //! beginning at position pos + xlen. Chris@16: //! Chris@16: //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error Chris@16: //! if the length of the resulting string would exceed max_size() Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(size_type pos, size_type n1, const CharT* s) Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::replace out of range position"); Chris@16: const size_type len = container_detail::min_value(n1, this->size() - pos); Chris@16: const size_type n2 = Traits::length(s); Chris@16: if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2) Chris@16: throw_length_error("basic_string::replace max_size() exceeded"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return this->replace(addr + pos, addr + pos + len, Chris@16: s, s + Traits::length(s)); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size(). Chris@16: //! Chris@16: //! Effects: Equivalent to replace(pos1, n1, basic_string(n2, c)). Chris@16: //! Chris@16: //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error Chris@16: //! if the length of the resulting string would exceed max_size() Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c) Chris@16: { Chris@16: if (pos1 > this->size()) Chris@16: throw_out_of_range("basic_string::replace out of range position"); Chris@16: const size_type len = container_detail::min_value(n1, this->size() - pos1); Chris@16: if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2) Chris@16: throw_length_error("basic_string::replace max_size() exceeded"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return this->replace(addr + pos1, addr + pos1 + len, n2, c); Chris@16: } Chris@16: Chris@16: //! Requires: [begin(),i1) and [i1,i2) are valid ranges. Chris@16: //! Chris@16: //! Effects: Calls replace(i1 - begin(), i2 - i1, str). Chris@16: //! Chris@16: //! Throws: if memory allocation throws Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str) Chris@16: { return this->replace(i1, i2, str.begin(), str.end()); } Chris@16: Chris@16: //! Requires: [begin(),i1) and [i1,i2) are valid ranges and Chris@16: //! s points to an array of at least n elements Chris@16: //! Chris@16: //! Effects: Calls replace(i1 - begin(), i2 - i1, s, n). Chris@16: //! Chris@16: //! Throws: if memory allocation throws Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n) Chris@16: { return this->replace(i1, i2, s, s + n); } Chris@16: Chris@16: //! Requires: [begin(),i1) and [i1,i2) are valid ranges and s points to an Chris@16: //! array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Effects: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)). Chris@16: //! Chris@16: //! Throws: if memory allocation throws Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s) Chris@16: { return this->replace(i1, i2, s, s + Traits::length(s)); } Chris@16: Chris@16: //! Requires: [begin(),i1) and [i1,i2) are valid ranges. Chris@16: //! Chris@16: //! Effects: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)). Chris@16: //! Chris@16: //! Throws: if memory allocation throws Chris@16: //! Chris@16: //! Returns: *this Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c) Chris@16: { Chris@16: const size_type len = static_cast(i2 - i1); Chris@16: if (len >= n) { Chris@16: Traits::assign(const_cast(container_detail::to_raw_pointer(i1)), n, c); Chris@16: erase(i1 + n, i2); Chris@16: } Chris@16: else { Chris@16: Traits::assign(const_cast(container_detail::to_raw_pointer(i1)), len, c); Chris@16: insert(i2, n - len, c); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Requires: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges. Chris@16: //! Chris@16: //! Effects: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)). Chris@16: //! Chris@16: //! Throws: if memory allocation throws Chris@16: //! Chris@16: //! Returns: *this Chris@16: template Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2 Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && container_detail::is_input_iterator::value Chris@16: >::type * = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){ Chris@16: Traits::assign(*const_cast(container_detail::to_raw_pointer(i1)), *j1); Chris@16: } Chris@16: Chris@16: if (j1 == j2) Chris@16: this->erase(i1, i2); Chris@16: else Chris@16: this->insert(i2, j1, j2); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: template Chris@16: basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2 Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && !container_detail::is_input_iterator::value Chris@16: >::type * = 0 Chris@16: ) Chris@16: { Chris@101: difference_type n = boost::container::iterator_distance(j1, j2); Chris@16: const difference_type len = i2 - i1; Chris@16: if (len >= n) { Chris@16: this->priv_copy(j1, j2, const_cast(container_detail::to_raw_pointer(i1))); Chris@16: this->erase(i1 + n, i2); Chris@16: } Chris@16: else { Chris@16: ForwardIter m = j1; Chris@101: boost::container::iterator_advance(m, len); Chris@16: this->priv_copy(j1, m, const_cast(container_detail::to_raw_pointer(i1))); Chris@16: this->insert(i2, m, j2); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: Chris@16: //! Requires: pos <= size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to copy as the Chris@16: //! smaller of n and size() - pos. s shall designate an array of at least rlen elements. Chris@16: //! The function then replaces the string designated by s with a string of length rlen Chris@16: //! whose elements are a copy of the string controlled by *this beginning at position pos. Chris@16: //! The function does not append a null object to the string designated by s. Chris@16: //! Chris@16: //! Throws: if memory allocation throws, out_of_range if pos > size(). Chris@16: //! Chris@16: //! Returns: rlen Chris@16: size_type copy(CharT* s, size_type n, size_type pos = 0) const Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::copy out of range position"); Chris@16: const size_type len = container_detail::min_value(n, this->size() - pos); Chris@16: Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len); Chris@16: return len; Chris@16: } Chris@16: Chris@16: //! Effects: *this contains the same sequence of characters that was in s, Chris@16: //! s contains the same sequence of characters that was in *this. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: void swap(basic_string& x) Chris@101: BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value Chris@101: || allocator_traits_type::is_always_equal::value) Chris@16: { Chris@16: this->base_t::swap_data(x); Chris@16: container_detail::bool_ flag; Chris@16: container_detail::swap_alloc(this->alloc(), x.alloc(), flag); Chris@16: } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // data access Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Requires: The program shall not alter any of the values stored in the character array. Chris@16: //! Chris@101: //! Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()]. Chris@16: //! Chris@16: //! Complexity: constant time. Chris@101: const CharT* c_str() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return container_detail::to_raw_pointer(this->priv_addr()); } Chris@16: Chris@16: //! Requires: The program shall not alter any of the values stored in the character array. Chris@16: //! Chris@101: //! Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()]. Chris@16: //! Chris@16: //! Complexity: constant time. Chris@101: const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return container_detail::to_raw_pointer(this->priv_addr()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // string operations Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Effects: Determines the lowest position xpos, if possible, such that both Chris@16: //! of the following conditions obtain: 19 pos <= xpos and xpos + str.size() <= size(); Chris@16: //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type find(const basic_string& s, size_type pos = 0) const Chris@16: { return find(s.c_str(), pos, s.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find(basic_string(s,n),pos). Chris@16: size_type find(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: if (pos + n > this->size()) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: pointer finish = addr + this->priv_size(); Chris@16: const const_iterator result = Chris@16: std::search(container_detail::to_raw_pointer(addr + pos), Chris@16: container_detail::to_raw_pointer(finish), Chris@16: s, s + n, Eq_traits()); Chris@16: return result != finish ? result - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find(basic_string(s), pos). Chris@16: size_type find(const CharT* s, size_type pos = 0) const Chris@16: { return this->find(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find(basic_string(1,c), pos). Chris@16: size_type find(CharT c, size_type pos = 0) const Chris@16: { Chris@16: const size_type sz = this->size(); Chris@16: if (pos >= sz) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: pointer finish = addr + sz; Chris@16: const const_iterator result = Chris@16: std::find_if(addr + pos, finish, Chris@16: std::bind2nd(Eq_traits(), c)); Chris@16: return result != finish ? result - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Determines the highest position xpos, if possible, such Chris@16: //! that both of the following conditions obtain: Chris@16: //! a) xpos <= pos and xpos + str.size() <= size(); Chris@16: //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type rfind(const basic_string& str, size_type pos = npos) const Chris@16: { return rfind(str.c_str(), pos, str.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: rfind(basic_string(s, n), pos). Chris@16: size_type rfind(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: Chris@16: if (n > len) Chris@16: return npos; Chris@16: else if (n == 0) Chris@16: return container_detail::min_value(len, pos); Chris@16: else { Chris@16: const const_iterator last = begin() + container_detail::min_value(len - n, pos) + n; Chris@16: const const_iterator result = find_end(begin(), last, Chris@16: s, s + n, Chris@16: Eq_traits()); Chris@16: return result != last ? result - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: pos <= size() and s points to an array of at least Chris@16: //! traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: rfind(basic_string(s), pos). Chris@16: size_type rfind(const CharT* s, size_type pos = npos) const Chris@16: { return rfind(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: rfind(basic_string(1,c),pos). Chris@16: size_type rfind(CharT c, size_type pos = npos) const Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: Chris@16: if (len < 1) Chris@16: return npos; Chris@16: else { Chris@16: const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; Chris@16: const_reverse_iterator rresult = Chris@16: std::find_if(const_reverse_iterator(last), rend(), Chris@16: std::bind2nd(Eq_traits(), c)); Chris@16: return rresult != rend() ? (rresult.base() - 1) - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Determines the lowest position xpos, if possible, such that both of the Chris@16: //! following conditions obtain: a) pos <= xpos and xpos < size(); Chris@16: //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type find_first_of(const basic_string& s, size_type pos = 0) const Chris@16: { return find_first_of(s.c_str(), pos, s.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_of(basic_string(s, n), pos). Chris@16: size_type find_first_of(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: const size_type sz = this->size(); Chris@16: if (pos >= sz) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: pointer finish = addr + sz; Chris@16: const_iterator result = std::find_first_of Chris@16: (addr + pos, finish, s, s + n, Eq_traits()); Chris@16: return result != finish ? result - this->begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_of(basic_string(s), pos). Chris@16: size_type find_first_of(const CharT* s, size_type pos = 0) const Chris@16: { return find_first_of(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_of(basic_string(1,c), pos). Chris@16: size_type find_first_of(CharT c, size_type pos = 0) const Chris@16: { return find(c, pos); } Chris@16: Chris@16: //! Effects: Determines the highest position xpos, if possible, such that both of Chris@16: //! the following conditions obtain: a) xpos <= pos and xpos < size(); b) Chris@16: //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type find_last_of(const basic_string& str, size_type pos = npos) const Chris@16: { return find_last_of(str.c_str(), pos, str.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_of(basic_string(s, n), pos). Chris@16: size_type find_last_of(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: Chris@16: if (len < 1) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1; Chris@16: const const_reverse_iterator rresult = Chris@16: std::find_first_of(const_reverse_iterator(last), rend(), Chris@16: s, s + n, Eq_traits()); Chris@16: return rresult != rend() ? (rresult.base() - 1) - addr : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_of(basic_string(1,c),pos). Chris@16: size_type find_last_of(const CharT* s, size_type pos = npos) const Chris@16: { return find_last_of(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_of(basic_string(s), pos). Chris@16: size_type find_last_of(CharT c, size_type pos = npos) const Chris@16: { return rfind(c, pos); } Chris@16: Chris@16: //! Effects: Determines the lowest position xpos, if possible, such that Chris@16: //! both of the following conditions obtain: Chris@16: //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no Chris@16: //! element I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type find_first_not_of(const basic_string& str, size_type pos = 0) const Chris@16: { return find_first_not_of(str.c_str(), pos, str.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_not_of(basic_string(s, n), pos). Chris@16: size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: if (pos > this->size()) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer finish = addr + this->priv_size(); Chris@16: const const_iterator result = std::find_if Chris@16: (addr + pos, finish, Not_within_traits(s, s + n)); Chris@16: return result != finish ? result - addr : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_not_of(basic_string(s), pos). Chris@16: size_type find_first_not_of(const CharT* s, size_type pos = 0) const Chris@16: { return find_first_not_of(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_first_not_of(basic_string(1, c), pos). Chris@16: size_type find_first_not_of(CharT c, size_type pos = 0) const Chris@16: { Chris@16: if (pos > this->size()) Chris@16: return npos; Chris@16: else { Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer finish = addr + this->priv_size(); Chris@16: const const_iterator result Chris@16: = std::find_if(addr + pos, finish, Chris@16: std::not1(std::bind2nd(Eq_traits(), c))); Chris@16: return result != finish ? result - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Determines the highest position xpos, if possible, such that Chris@16: //! both of the following conditions obtain: a) xpos <= pos and xpos < size(); Chris@16: //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. Chris@16: size_type find_last_not_of(const basic_string& str, size_type pos = npos) const Chris@16: { return find_last_not_of(str.c_str(), pos, str.size()); } Chris@16: Chris@16: //! Requires: s points to an array of at least n elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_not_of(basic_string(s, n), pos). Chris@16: size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: Chris@16: if (len < 1) Chris@16: return npos; Chris@16: else { Chris@16: const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; Chris@16: const const_reverse_iterator rresult = Chris@16: std::find_if(const_reverse_iterator(last), rend(), Chris@16: Not_within_traits(s, s + n)); Chris@16: return rresult != rend() ? (rresult.base() - 1) - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_not_of(basic_string(s), pos). Chris@16: size_type find_last_not_of(const CharT* s, size_type pos = npos) const Chris@16: { return find_last_not_of(s, pos, Traits::length(s)); } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: find_last_not_of(basic_string(1, c), pos). Chris@16: size_type find_last_not_of(CharT c, size_type pos = npos) const Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: Chris@16: if (len < 1) Chris@16: return npos; Chris@16: else { Chris@16: const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; Chris@16: const const_reverse_iterator rresult = Chris@16: std::find_if(const_reverse_iterator(last), rend(), Chris@16: std::not1(std::bind2nd(Eq_traits(), c))); Chris@16: return rresult != rend() ? (rresult.base() - 1) - begin() : npos; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Requires: pos <= size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to copy as Chris@16: //! the smaller of n and size() - pos. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or out_of_range if pos > size(). Chris@16: //! Chris@16: //! Returns: basic_string(data()+pos,rlen). Chris@16: basic_string substr(size_type pos = 0, size_type n = npos) const Chris@16: { Chris@16: if (pos > this->size()) Chris@16: throw_out_of_range("basic_string::substr out of range position"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return basic_string(addr + pos, Chris@16: addr + pos + container_detail::min_value(n, size() - pos), this->alloc()); Chris@16: } Chris@16: Chris@16: //! Effects: Determines the effective length rlen of the string to copy as Chris@16: //! the smaller of size() and str.size(). The function then compares the two strings by Chris@16: //! calling traits::compare(data(), str.data(), rlen). Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: The nonzero result if the result of the comparison is nonzero. Chris@16: //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(), Chris@16: //! and value > 0 if size() > str.size() Chris@16: int compare(const basic_string& str) const Chris@16: { Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer str_addr = str.priv_addr(); Chris@16: return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size()); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to copy as Chris@16: //! the smaller of Chris@16: //! Chris@16: //! Throws: out_of_range if pos1 > size() Chris@16: //! Chris@16: //! Returns:basic_string(*this,pos1,n1).compare(str). Chris@16: int compare(size_type pos1, size_type n1, const basic_string& str) const Chris@16: { Chris@16: if (pos1 > this->size()) Chris@16: throw_out_of_range("basic_string::compare out of range position"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer str_addr = str.priv_addr(); Chris@16: return s_compare(addr + pos1, Chris@16: addr + pos1 + container_detail::min_value(n1, this->size() - pos1), Chris@16: str_addr, str_addr + str.priv_size()); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 <= size() and pos2 <= str.size() Chris@16: //! Chris@16: //! Effects: Determines the effective length rlen of the string to copy as Chris@16: //! the smaller of Chris@16: //! Chris@16: //! Throws: out_of_range if pos1 > size() or pos2 > str.size() Chris@16: //! Chris@16: //! Returns: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)). Chris@16: int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2) const Chris@16: { Chris@16: if (pos1 > this->size() || pos2 > str.size()) Chris@16: throw_out_of_range("basic_string::compare out of range position"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: const pointer str_addr = str.priv_addr(); Chris@16: return s_compare(addr + pos1, Chris@16: addr + pos1 + container_detail::min_value(n1, this->size() - pos1), Chris@16: str_addr + pos2, Chris@16: str_addr + pos2 + container_detail::min_value(n2, str.size() - pos2)); Chris@16: } Chris@16: Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Returns: compare(basic_string(s)). Chris@16: int compare(const CharT* s) const Chris@16: { Chris@16: const pointer addr = this->priv_addr(); Chris@16: return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s)); Chris@16: } Chris@16: Chris@16: Chris@16: //! Requires: pos1 > size() and s points to an array of at least n2 elements of CharT. Chris@16: //! Chris@16: //! Throws: out_of_range if pos1 > size() Chris@16: //! Chris@16: //! Returns: basic_string(*this, pos, n1).compare(basic_string(s, n2)). Chris@16: int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const Chris@16: { Chris@16: if (pos1 > this->size()) Chris@16: throw_out_of_range("basic_string::compare out of range position"); Chris@16: const pointer addr = this->priv_addr(); Chris@16: return s_compare( addr + pos1, Chris@16: addr + pos1 + container_detail::min_value(n1, this->size() - pos1), Chris@16: s, s + n2); Chris@16: } Chris@16: Chris@16: //! Requires: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT. Chris@16: //! Chris@16: //! Throws: out_of_range if pos1 > size() Chris@16: //! Chris@16: //! Returns: basic_string(*this, pos, n1).compare(basic_string(s, n2)). Chris@16: int compare(size_type pos1, size_type n1, const CharT* s) const Chris@16: { return this->compare(pos1, n1, s, Traits::length(s)); } Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: private: Chris@101: void priv_reserve(size_type res_arg, const bool null_terminate = true) Chris@101: { Chris@101: if (res_arg > this->max_size()){ Chris@101: throw_length_error("basic_string::reserve max_size() exceeded"); Chris@101: } Chris@101: Chris@101: if (this->capacity() < res_arg){ Chris@101: size_type n = container_detail::max_value(res_arg, this->size()) + 1; Chris@101: size_type new_cap = this->next_capacity(n); Chris@101: pointer reuse = 0; Chris@101: pointer new_start = this->allocation_command(allocate_new, n, new_cap, reuse); Chris@101: size_type new_length = 0; Chris@101: Chris@101: const pointer addr = this->priv_addr(); Chris@101: new_length += priv_uninitialized_copy Chris@101: (addr, addr + this->priv_size(), new_start); Chris@101: if(null_terminate){ Chris@101: this->priv_construct_null(new_start + new_length); Chris@101: } Chris@101: this->deallocate_block(); Chris@101: this->is_short(false); Chris@101: this->priv_long_addr(new_start); Chris@101: this->priv_long_size(new_length); Chris@101: this->priv_storage(new_cap); Chris@101: } Chris@101: } Chris@101: Chris@16: static int s_compare(const_pointer f1, const_pointer l1, Chris@16: const_pointer f2, const_pointer l2) Chris@16: { Chris@16: const difference_type n1 = l1 - f1; Chris@16: const difference_type n2 = l2 - f2; Chris@16: const int cmp = Traits::compare(container_detail::to_raw_pointer(f1), Chris@16: container_detail::to_raw_pointer(f2), Chris@16: container_detail::min_value(n1, n2)); Chris@16: return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0)); Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_shrink_to_fit_dynamic_buffer Chris@16: ( AllocVersion Chris@101: , typename container_detail::enable_if >::type* = 0) Chris@16: { Chris@16: //Allocate a new buffer. Chris@16: size_type real_cap = 0; Chris@16: const pointer long_addr = this->priv_long_addr(); Chris@16: const size_type long_size = this->priv_long_size(); Chris@16: const size_type long_storage = this->priv_long_storage(); Chris@16: //We can make this nothrow as chars are always NoThrowCopyables Chris@16: BOOST_TRY{ Chris@101: pointer reuse = 0; Chris@101: real_cap = long_size+1; Chris@101: const pointer ret = this->allocation_command(allocate_new, long_size+1, real_cap, reuse); Chris@16: //Copy and update Chris@101: Traits::copy( container_detail::to_raw_pointer(ret) Chris@16: , container_detail::to_raw_pointer(this->priv_long_addr()) Chris@16: , long_size+1); Chris@101: this->priv_long_addr(ret); Chris@16: this->priv_storage(real_cap); Chris@16: //And release old buffer Chris@16: this->alloc().deallocate(long_addr, long_storage); Chris@16: } Chris@16: BOOST_CATCH(...){ Chris@16: return; Chris@16: } Chris@16: BOOST_CATCH_END Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_shrink_to_fit_dynamic_buffer Chris@16: ( AllocVersion Chris@101: , typename container_detail::enable_if >::type* = 0) Chris@16: { Chris@101: size_type received_size = this->priv_long_size()+1; Chris@101: pointer hint = this->priv_long_addr(); Chris@16: if(this->alloc().allocation_command Chris@101: ( shrink_in_place | nothrow_allocation, this->priv_long_storage(), received_size, hint)){ Chris@16: this->priv_storage(received_size); Chris@16: } Chris@16: } Chris@16: Chris@16: void priv_construct_null(pointer p) Chris@16: { this->construct(p, CharT(0)); } Chris@16: Chris@16: // Helper functions used by constructors. It is a severe error for Chris@16: // any of them to be called anywhere except from within constructors. Chris@16: void priv_terminate_string() Chris@16: { this->priv_construct_null(this->priv_end_addr()); } Chris@16: Chris@16: template inline Chris@16: void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val) Chris@16: { Chris@16: //Save initial position Chris@16: FwdIt init = first; Chris@16: Chris@16: BOOST_TRY{ Chris@16: //Construct objects Chris@16: for (; count--; ++first){ Chris@16: this->construct(first, val); Chris@16: } Chris@16: } Chris@16: BOOST_CATCH(...){ Chris@16: //Call destructors Chris@16: for (; init != first; ++init){ Chris@16: this->destroy(init); Chris@16: } Chris@16: BOOST_RETHROW Chris@16: } Chris@16: BOOST_CATCH_END Chris@16: } Chris@16: Chris@16: template inline Chris@16: size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest) Chris@16: { Chris@16: //Save initial destination position Chris@16: FwdIt dest_init = dest; Chris@16: size_type constructed = 0; Chris@16: Chris@16: BOOST_TRY{ Chris@16: //Try to build objects Chris@16: for (; first != last; ++dest, ++first, ++constructed){ Chris@16: this->construct(dest, *first); Chris@16: } Chris@16: } Chris@16: BOOST_CATCH(...){ Chris@16: //Call destructors Chris@16: for (; constructed--; ++dest_init){ Chris@16: this->destroy(dest_init); Chris@16: } Chris@16: BOOST_RETHROW Chris@16: } Chris@16: BOOST_CATCH_END Chris@16: return (constructed); Chris@16: } Chris@16: Chris@16: template Chris@16: void priv_copy(InputIterator first, InputIterator last, OutIterator result) Chris@16: { Chris@16: for ( ; first != last; ++first, ++result) Chris@16: Traits::assign(*result, *first); Chris@16: } Chris@16: Chris@16: void priv_copy(const CharT* first, const CharT* last, CharT* result) Chris@16: { Traits::copy(result, first, last - first); } Chris@16: Chris@16: template Chris@16: basic_string& priv_replace_dispatch(const_iterator first, const_iterator last, Chris@16: Integer n, Integer x, Chris@16: container_detail::true_) Chris@16: { return this->replace(first, last, (size_type) n, (CharT) x); } Chris@16: Chris@16: template Chris@16: basic_string& priv_replace_dispatch(const_iterator first, const_iterator last, Chris@16: InputIter f, InputIter l, Chris@16: container_detail::false_) Chris@16: { Chris@101: typedef typename boost::container::iterator_traits::iterator_category Category; Chris@16: return this->priv_replace(first, last, f, l, Category()); Chris@16: } Chris@16: Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@101: #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@101: Chris@16: //!Typedef for a basic_string of Chris@16: //!narrow characters Chris@16: typedef basic_string Chris@16: Chris@101: ,new_allocator > Chris@16: string; Chris@16: Chris@16: //!Typedef for a basic_string of Chris@16: //!narrow characters Chris@16: typedef basic_string Chris@16: Chris@101: ,new_allocator > Chris@16: wstring; Chris@16: Chris@101: #endif Chris@101: Chris@16: // ------------------------------------------------------------ Chris@16: // Non-member functions. Chris@16: Chris@16: // Operator+ Chris@16: Chris@101: template inline Chris@16: basic_string Chris@16: operator+(const basic_string& x Chris@16: ,const basic_string& y) Chris@16: { Chris@16: typedef basic_string str_t; Chris@16: typedef typename str_t::reserve_t reserve_t; Chris@16: reserve_t reserve; Chris@16: str_t result(reserve, x.size() + y.size(), x.get_stored_allocator()); Chris@16: result.append(x); Chris@16: result.append(y); Chris@16: return result; Chris@16: } Chris@16: Chris@16: template inline Chris@16: basic_string operator+ Chris@101: ( BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END x Chris@101: , BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END y) Chris@16: { Chris@101: x += y; Chris@101: return boost::move(x); Chris@16: } Chris@16: Chris@16: template inline Chris@16: basic_string operator+ Chris@101: ( BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END x Chris@16: , const basic_string& y) Chris@16: { Chris@101: x += y; Chris@101: return boost::move(x); Chris@16: } Chris@16: Chris@16: template inline Chris@16: basic_string operator+ Chris@16: (const basic_string& x Chris@101: ,BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END y) Chris@16: { Chris@101: y.insert(y.begin(), x.begin(), x.end()); Chris@101: return boost::move(y); Chris@16: } Chris@16: Chris@16: template inline Chris@16: basic_string operator+ Chris@16: (const CharT* s, basic_string y) Chris@16: { Chris@16: y.insert(y.begin(), s, s + Traits::length(s)); Chris@16: return y; Chris@16: } Chris@16: Chris@101: template inline Chris@16: basic_string operator+ Chris@16: (basic_string x, const CharT* s) Chris@16: { Chris@16: x += s; Chris@16: return x; Chris@16: } Chris@16: Chris@16: template inline Chris@16: basic_string operator+ Chris@16: (CharT c, basic_string y) Chris@16: { Chris@16: y.insert(y.begin(), c); Chris@16: return y; Chris@16: } Chris@16: Chris@101: template inline Chris@16: basic_string operator+ Chris@16: (basic_string x, const CharT c) Chris@16: { Chris@16: x += c; Chris@16: return x; Chris@16: } Chris@16: Chris@16: // Operator== and operator!= Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator==(const basic_string& x, Chris@16: const basic_string& y) Chris@16: { Chris@16: return x.size() == y.size() && Chris@16: Traits::compare(x.data(), y.data(), x.size()) == 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator==(const CharT* s, const basic_string& y) Chris@16: { Chris@16: typename basic_string::size_type n = Traits::length(s); Chris@16: return n == y.size() && Traits::compare(s, y.data(), n) == 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator==(const basic_string& x, const CharT* s) Chris@16: { Chris@16: typename basic_string::size_type n = Traits::length(s); Chris@16: return x.size() == n && Traits::compare(x.data(), s, n) == 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator!=(const basic_string& x, Chris@16: const basic_string& y) Chris@16: { return !(x == y); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator!=(const CharT* s, const basic_string& y) Chris@16: { return !(s == y); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator!=(const basic_string& x, const CharT* s) Chris@16: { return !(x == s); } Chris@16: Chris@16: Chris@16: // Operator< (and also >, <=, and >=). Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<(const basic_string& x, const basic_string& y) Chris@16: { Chris@16: return x.compare(y) < 0; Chris@16: // return basic_string Chris@16: // ::s_compare(x.begin(), x.end(), y.begin(), y.end()) < 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<(const CharT* s, const basic_string& y) Chris@16: { Chris@16: return y.compare(s) > 0; Chris@16: // basic_string::size_type n = Traits::length(s); Chris@16: // return basic_string Chris@16: // ::s_compare(s, s + n, y.begin(), y.end()) < 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<(const basic_string& x, Chris@16: const CharT* s) Chris@16: { Chris@16: return x.compare(s) < 0; Chris@16: // basic_string::size_type n = Traits::length(s); Chris@16: // return basic_string Chris@16: // ::s_compare(x.begin(), x.end(), s, s + n) < 0; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>(const basic_string& x, Chris@16: const basic_string& y) { Chris@16: return y < x; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>(const CharT* s, const basic_string& y) { Chris@16: return y < s; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>(const basic_string& x, const CharT* s) Chris@16: { Chris@16: return s < x; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<=(const basic_string& x, Chris@16: const basic_string& y) Chris@16: { Chris@16: return !(y < x); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<=(const CharT* s, const basic_string& y) Chris@16: { return !(y < s); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator<=(const basic_string& x, const CharT* s) Chris@16: { return !(s < x); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>=(const basic_string& x, Chris@16: const basic_string& y) Chris@16: { return !(x < y); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>=(const CharT* s, const basic_string& y) Chris@16: { return !(s < y); } Chris@16: Chris@16: template Chris@16: inline bool Chris@16: operator>=(const basic_string& x, const CharT* s) Chris@16: { return !(x < s); } Chris@16: Chris@16: // Swap. Chris@16: template Chris@16: inline void swap(basic_string& x, basic_string& y) Chris@16: { x.swap(y); } Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@101: // I/O. Chris@16: namespace container_detail { Chris@16: Chris@16: template Chris@16: inline bool Chris@16: string_fill(std::basic_ostream& os, Chris@16: std::basic_streambuf* buf, Chris@16: std::size_t n) Chris@16: { Chris@16: CharT f = os.fill(); Chris@16: std::size_t i; Chris@16: bool ok = true; Chris@16: Chris@16: for (i = 0; i < n; i++) Chris@16: ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof()); Chris@16: return ok; Chris@16: } Chris@16: Chris@16: } //namespace container_detail { Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: template Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const basic_string& s) Chris@16: { Chris@16: typename std::basic_ostream::sentry sentry(os); Chris@16: bool ok = false; Chris@16: Chris@16: if (sentry) { Chris@16: ok = true; Chris@16: typename basic_string::size_type n = s.size(); Chris@16: typename basic_string::size_type pad_len = 0; Chris@16: const bool left = (os.flags() & std::ios::left) != 0; Chris@16: const std::size_t w = os.width(0); Chris@16: std::basic_streambuf* buf = os.rdbuf(); Chris@16: Chris@16: if (w != 0 && n < w) Chris@16: pad_len = w - n; Chris@101: Chris@16: if (!left) Chris@101: ok = container_detail::string_fill(os, buf, pad_len); Chris@16: Chris@16: ok = ok && Chris@16: buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n); Chris@16: Chris@16: if (left) Chris@16: ok = ok && container_detail::string_fill(os, buf, pad_len); Chris@16: } Chris@16: Chris@16: if (!ok) Chris@16: os.setstate(std::ios_base::failbit); Chris@16: Chris@16: return os; Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: std::basic_istream& Chris@16: operator>>(std::basic_istream& is, basic_string& s) Chris@16: { Chris@16: typename std::basic_istream::sentry sentry(is); Chris@16: Chris@16: if (sentry) { Chris@16: std::basic_streambuf* buf = is.rdbuf(); Chris@16: const std::ctype& ctype = std::use_facet >(is.getloc()); Chris@16: Chris@16: s.clear(); Chris@16: std::size_t n = is.width(0); Chris@16: if (n == 0) Chris@16: n = static_cast(-1); Chris@16: else Chris@16: s.reserve(n); Chris@16: Chris@16: while (n-- > 0) { Chris@16: typename Traits::int_type c1 = buf->sbumpc(); Chris@16: Chris@16: if (Traits::eq_int_type(c1, Traits::eof())) { Chris@16: is.setstate(std::ios_base::eofbit); Chris@16: break; Chris@16: } Chris@16: else { Chris@16: CharT c = Traits::to_char_type(c1); Chris@16: Chris@16: if (ctype.is(std::ctype::space, c)) { Chris@16: if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof())) Chris@16: is.setstate(std::ios_base::failbit); Chris@16: break; Chris@16: } Chris@16: else Chris@16: s.push_back(c); Chris@16: } Chris@16: } Chris@101: Chris@16: // If we have read no characters, then set failbit. Chris@16: if (s.size() == 0) Chris@16: is.setstate(std::ios_base::failbit); Chris@16: } Chris@16: else Chris@16: is.setstate(std::ios_base::failbit); Chris@16: Chris@16: return is; Chris@16: } Chris@16: Chris@101: template Chris@16: std::basic_istream& Chris@16: getline(std::istream& is, basic_string& s,CharT delim) Chris@16: { Chris@16: typename basic_string::size_type nread = 0; Chris@16: typename std::basic_istream::sentry sentry(is, true); Chris@16: if (sentry) { Chris@16: std::basic_streambuf* buf = is.rdbuf(); Chris@16: s.clear(); Chris@16: Chris@16: while (nread < s.max_size()) { Chris@16: int c1 = buf->sbumpc(); Chris@16: if (Traits::eq_int_type(c1, Traits::eof())) { Chris@16: is.setstate(std::ios_base::eofbit); Chris@16: break; Chris@16: } Chris@16: else { Chris@16: ++nread; Chris@16: CharT c = Traits::to_char_type(c1); Chris@16: if (!Traits::eq(c, delim)) Chris@16: s.push_back(c); Chris@16: else Chris@16: break; // Character is extracted but not appended. Chris@16: } Chris@16: } Chris@16: } Chris@16: if (nread == 0 || nread >= s.max_size()) Chris@16: is.setstate(std::ios_base::failbit); Chris@16: Chris@16: return is; Chris@16: } Chris@16: Chris@101: template Chris@16: inline std::basic_istream& Chris@16: getline(std::basic_istream& is, basic_string& s) Chris@16: { Chris@16: return getline(is, s, '\n'); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::size_t hash_value(basic_string, Allocator> const& v) Chris@16: { Chris@16: return hash_range(v.begin(), v.end()); Chris@16: } Chris@16: Chris@16: }} Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: //!has_trivial_destructor_after_move<> == true_type Chris@16: //!specialization for optimizations Chris@16: template Chris@16: struct has_trivial_destructor_after_move > Chris@101: { Chris@101: typedef typename ::boost::container::allocator_traits::pointer pointer; Chris@101: static const bool value = ::boost::has_trivial_destructor_after_move::value && Chris@101: ::boost::has_trivial_destructor_after_move::value; Chris@101: }; Chris@16: Chris@16: } Chris@16: Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_CONTAINER_STRING_HPP