1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_STRING_HPP
12 #define BOOST_CONTAINER_STRING_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/container_fwd.hpp>
26 #include <boost/container/allocator_traits.hpp>
27 #include <boost/container/new_allocator.hpp> //new_allocator
28 #include <boost/container/throw_exception.hpp>
30 #include <boost/container/detail/alloc_helpers.hpp>
31 #include <boost/container/detail/allocator_version_traits.hpp>
32 #include <boost/container/detail/allocation_type.hpp>
33 #include <boost/container/detail/iterator.hpp>
34 #include <boost/container/detail/iterators.hpp>
35 #include <boost/container/detail/min_max.hpp>
36 #include <boost/container/detail/mpl.hpp>
37 #include <boost/container/detail/next_capacity.hpp>
38 #include <boost/move/detail/to_raw_pointer.hpp>
39 #include <boost/container/detail/version_type.hpp>
40 #include <boost/container/detail/type_traits.hpp>
41 #include <boost/container/detail/minimal_char_traits_header.hpp>
42 #include <boost/container/detail/algorithm.hpp>
44 #include <boost/intrusive/pointer_traits.hpp>
46 #include <boost/move/utility_core.hpp>
47 #include <boost/move/adl_move_swap.hpp>
48 #include <boost/move/traits.hpp>
50 #include <boost/static_assert.hpp>
51 #include <boost/core/no_exceptions_support.hpp>
52 #include <boost/functional/hash.hpp>
64 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
65 #include <initializer_list> //for std::initializer_list
72 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
73 namespace container_detail {
74 // ------------------------------------------------------------
75 // Class basic_string_base.
77 // basic_string_base is a helper class that makes it it easier to write
78 // an exception-safe version of basic_string. The constructor allocates,
79 // but does not initialize, a block of memory. The destructor
80 // deallocates, but does not destroy elements within, a block of
81 // memory. The destructor assumes that the memory either is the internal buffer,
82 // or else points to a block of memory that was allocated using string_base's
83 // allocator and whose size is this->m_storage.
84 template <class Allocator>
85 class basic_string_base
87 basic_string_base & operator=(const basic_string_base &);
88 basic_string_base(const basic_string_base &);
90 typedef allocator_traits<Allocator> allocator_traits_type;
92 typedef Allocator allocator_type;
93 typedef allocator_type stored_allocator_type;
94 typedef typename allocator_traits_type::pointer pointer;
95 typedef typename allocator_traits_type::value_type value_type;
96 typedef typename allocator_traits_type::size_type size_type;
97 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
103 explicit basic_string_base(const allocator_type& a)
107 explicit basic_string_base(BOOST_RV_REF(allocator_type) a)
108 : members_(boost::move(a))
111 basic_string_base(const allocator_type& a, size_type n)
115 this->allocate_initial_block(n);
118 explicit basic_string_base(size_type n)
122 this->allocate_initial_block(n);
127 if(!this->is_short()){
128 this->deallocate(this->priv_long_addr(), this->priv_long_storage());
134 //This is the structure controlling a long string
137 size_type is_short : 1;
138 size_type length : (sizeof(size_type)*CHAR_BIT - 1);
145 long_t(const long_t &other)
147 this->is_short = false;
148 length = other.length;
149 storage = other.storage;
153 long_t &operator= (const long_t &other)
155 length = other.length;
156 storage = other.storage;
162 //This type is the first part of the structure controlling a short string
163 //The "data" member stores
166 unsigned char is_short : 1;
167 unsigned char length : (CHAR_BIT - 1);
170 //This type has the same alignment and size as long_t but it's POD
171 //so, unlike long_t, it can be placed in a union
173 typedef typename container_detail::aligned_storage
174 <sizeof(long_t), container_detail::alignment_of<long_t>::value>::type long_raw_t;
177 static const size_type MinInternalBufferChars = 8;
178 static const size_type AlignmentOfValueType =
179 alignment_of<value_type>::value;
180 static const size_type ShortDataOffset = ((sizeof(short_header)-1)/AlignmentOfValueType+1)*AlignmentOfValueType;
181 static const size_type ZeroCostInternalBufferChars =
182 (sizeof(long_t) - ShortDataOffset)/sizeof(value_type);
183 static const size_type UnalignedFinalInternalBufferChars =
184 (ZeroCostInternalBufferChars > MinInternalBufferChars) ?
185 ZeroCostInternalBufferChars : MinInternalBufferChars;
190 value_type data[UnalignedFinalInternalBufferChars];
198 const short_t &short_repr() const
201 const long_t &long_repr() const
202 { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }
204 short_t &short_repr()
208 { return *static_cast<long_t*>(static_cast<void*>(&r)); }
211 struct members_holder
218 template<class AllocatorConvertible>
219 explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
220 : Allocator(boost::forward<AllocatorConvertible>(a))
226 const Allocator &alloc() const
232 static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
236 static const size_type MinAllocation = InternalBufferChars*2;
239 bool is_short() const
241 //Access and copy (to avoid UB) the first byte of the union to know if the
242 //active representation is short or long
244 BOOST_STATIC_ASSERT((sizeof(short_header) == 1));
245 *(unsigned char*)&hdr = *(unsigned char*)&this->members_.m_repr;
246 return hdr.is_short != 0;
249 void is_short(bool yes)
251 const bool was_short = this->is_short();
252 if(yes && !was_short){
253 allocator_traits_type::destroy
255 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
257 this->members_.m_repr.s.h.is_short = true;
259 else if(!yes && was_short){
260 allocator_traits_type::construct
262 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
264 this->members_.m_repr.s.h.is_short = false;
271 this->members_.m_repr.s.h.is_short = 1;
272 this->members_.m_repr.s.h.length = 0;
277 typedef container_detail::integral_constant<unsigned,
278 boost::container::container_detail::version<Allocator>::value> alloc_version;
280 pointer allocation_command(allocation_type command,
281 size_type limit_size,
282 size_type &prefer_in_recvd_out_size,
285 if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
287 command &= ~(expand_fwd | expand_bwd);
289 return container_detail::allocator_version_traits<Allocator>::allocation_command
290 (this->alloc(), command, limit_size, prefer_in_recvd_out_size, reuse);
293 size_type next_capacity(size_type additional_objects) const
295 return next_capacity_calculator
296 <size_type, NextCapacityDouble /*NextCapacity60Percent*/>::
297 get( allocator_traits_type::max_size(this->alloc())
298 , this->priv_storage(), additional_objects );
301 void deallocate(pointer p, size_type n)
303 if (p && (n > InternalBufferChars))
304 this->alloc().deallocate(p, n);
307 void construct(pointer p, const value_type &value = value_type())
309 allocator_traits_type::construct
311 , boost::movelib::to_raw_pointer(p)
316 void destroy(pointer p, size_type n)
318 value_type *raw_p = boost::movelib::to_raw_pointer(p);
320 allocator_traits_type::destroy( this->alloc(), raw_p);
324 void destroy(pointer p)
326 allocator_traits_type::destroy
328 , boost::movelib::to_raw_pointer(p)
332 void allocate_initial_block(size_type n)
334 if (n <= this->max_size()) {
335 if(n > InternalBufferChars){
336 size_type new_cap = this->next_capacity(n);
338 pointer p = this->allocation_command(allocate_new, n, new_cap, reuse);
339 this->is_short(false);
340 this->priv_long_addr(p);
341 this->priv_long_size(0);
342 this->priv_storage(new_cap);
346 throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
350 void deallocate_block()
351 { this->deallocate(this->priv_addr(), this->priv_storage()); }
353 size_type max_size() const
354 { return allocator_traits_type::max_size(this->alloc()) - 1; }
357 size_type priv_capacity() const
358 { return this->priv_storage() - 1; }
360 pointer priv_short_addr() const
361 { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
363 pointer priv_long_addr() const
364 { return this->members_.m_repr.long_repr().start; }
366 pointer priv_addr() const
368 return this->is_short()
374 pointer priv_end_addr() const
376 return this->is_short()
377 ? this->priv_short_addr() + this->priv_short_size()
378 : this->priv_long_addr() + this->priv_long_size()
382 void priv_long_addr(pointer addr)
383 { this->members_.m_repr.long_repr().start = addr; }
385 size_type priv_storage() const
386 { return this->is_short() ? priv_short_storage() : priv_long_storage(); }
388 size_type priv_short_storage() const
389 { return InternalBufferChars; }
391 size_type priv_long_storage() const
392 { return this->members_.m_repr.long_repr().storage; }
394 void priv_storage(size_type storage)
396 if(!this->is_short())
397 this->priv_long_storage(storage);
400 void priv_long_storage(size_type storage)
402 this->members_.m_repr.long_repr().storage = storage;
405 size_type priv_size() const
406 { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
408 size_type priv_short_size() const
409 { return this->members_.m_repr.short_repr().h.length; }
411 size_type priv_long_size() const
412 { return this->members_.m_repr.long_repr().length; }
414 void priv_size(size_type sz)
417 this->priv_short_size(sz);
419 this->priv_long_size(sz);
422 void priv_short_size(size_type sz)
424 this->members_.m_repr.s.h.length = (unsigned char)sz;
427 void priv_long_size(size_type sz)
429 this->members_.m_repr.long_repr().length = sz;
432 void swap_data(basic_string_base& other)
434 if(this->is_short()){
435 if(other.is_short()){
436 repr_t tmp(this->members_.m_repr);
437 this->members_.m_repr = other.members_.m_repr;
438 other.members_.m_repr = tmp;
441 short_t short_backup(this->members_.m_repr.short_repr());
442 this->members_.m_repr.short_repr().~short_t();
443 ::new(&this->members_.m_repr.long_repr()) long_t(other.members_.m_repr.long_repr());
444 other.members_.m_repr.long_repr().~long_t();
445 ::new(&other.members_.m_repr.short_repr()) short_t(short_backup);
449 if(other.is_short()){
450 short_t short_backup(other.members_.m_repr.short_repr());
451 other.members_.m_repr.short_repr().~short_t();
452 ::new(&other.members_.m_repr.long_repr()) long_t(this->members_.m_repr.long_repr());
453 this->members_.m_repr.long_repr().~long_t();
454 ::new(&this->members_.m_repr.short_repr()) short_t(short_backup);
457 boost::adl_move_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
463 } //namespace container_detail {
465 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
467 //! The basic_string class represents a Sequence of characters. It contains all the
468 //! usual operations of a Sequence, and, additionally, it contains standard string
469 //! operations such as search and concatenation.
471 //! The basic_string class is parameterized by character type, and by that type's
472 //! Character Traits.
474 //! This class has performance characteristics very much like vector<>, meaning,
475 //! for example, that it does not perform reference-count or copy-on-write, and that
476 //! concatenation of two strings is an O(N) operation.
478 //! Some of basic_string's member functions use an unusual method of specifying positions
479 //! and ranges. In addition to the conventional method using iterators, many of
480 //! basic_string's member functions use a single value pos of type size_type to represent a
481 //! position (in which case the position is begin() + pos, and many of basic_string's
482 //! member functions use two values, pos and n, to represent a range. In that case pos is
483 //! the beginning of the range and n is its size. That is, the range is
484 //! [begin() + pos, begin() + pos + n).
486 //! Note that the C++ standard does not specify the complexity of basic_string operations.
487 //! In this implementation, basic_string has performance characteristics very similar to
488 //! those of vector: access to a single character is O(1), while copy and concatenation
491 //! In this implementation, begin(),
492 //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
493 //! In this implementation, iterators are only invalidated by member functions that
494 //! explicitly change the string's contents.
496 //! \tparam CharT The type of character it contains.
497 //! \tparam Traits The Character Traits type, which encapsulates basic character operations
498 //! \tparam Allocator The allocator, used for internal memory management.
499 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
500 template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = new_allocator<CharT> >
502 template <class CharT, class Traits, class Allocator>
505 : private container_detail::basic_string_base<Allocator>
507 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
509 typedef allocator_traits<Allocator> allocator_traits_type;
510 BOOST_COPYABLE_AND_MOVABLE(basic_string)
511 typedef container_detail::basic_string_base<Allocator> base_t;
512 static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
515 // Allocator helper class to use a char_traits as a function object.
520 //Compatibility with std::binary_function
521 typedef typename Tr::char_type first_argument_type;
522 typedef typename Tr::char_type second_argument_type;
523 typedef bool result_type;
525 bool operator()(const first_argument_type& x, const second_argument_type& y) const
526 { return Tr::eq(x, y); }
530 struct Not_within_traits
532 typedef typename Tr::char_type argument_type;
533 typedef bool result_type;
535 typedef const typename Tr::char_type* Pointer;
536 const Pointer m_first;
537 const Pointer m_last;
539 Not_within_traits(Pointer f, Pointer l)
540 : m_first(f), m_last(l) {}
542 bool operator()(const typename Tr::char_type& x) const
544 return boost::container::find_if(m_first, m_last,
545 boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
548 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
551 //////////////////////////////////////////////
555 //////////////////////////////////////////////
556 typedef Traits traits_type;
557 typedef CharT value_type;
558 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
559 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
560 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
561 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
562 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
563 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
564 typedef Allocator allocator_type;
565 typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
566 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
567 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
568 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
569 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
570 static const size_type npos = size_type(-1);
572 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
574 typedef constant_iterator<CharT, difference_type> cvalue_iterator;
575 typedef typename base_t::alloc_version alloc_version;
576 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
577 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
579 public: // Constructor, destructor, assignment.
580 //////////////////////////////////////////////
582 // construct/copy/destroy
584 //////////////////////////////////////////////
585 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
588 basic_string(reserve_t, size_type n,
589 const allocator_type& a = allocator_type())
590 //Select allocator as in copy constructor as reserve_t-based constructors
591 //are two step copies optimized for capacity
592 : base_t( allocator_traits_type::select_on_container_copy_construction(a)
594 { this->priv_terminate_string(); }
596 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
598 //! <b>Effects</b>: Default constructs a basic_string.
600 //! <b>Throws</b>: If allocator_type's default constructor throws.
601 basic_string() BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
603 { this->priv_terminate_string(); }
606 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
608 //! <b>Throws</b>: Nothing
609 explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
611 { this->priv_terminate_string(); }
613 //! <b>Effects</b>: Copy constructs a basic_string.
615 //! <b>Postcondition</b>: x == *this.
617 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
618 basic_string(const basic_string& s)
619 : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
621 this->priv_terminate_string();
622 this->assign(s.begin(), s.end());
625 //! <b>Effects</b>: Same as basic_string(sv.data(), sv.size(), a).
627 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
628 template<template <class, class> class BasicStringView>
629 explicit basic_string(BasicStringView<CharT, Traits> sv, const Allocator& a = Allocator())
630 : base_t(allocator_traits_type::select_on_container_copy_construction(a))
632 this->priv_terminate_string();
636 //! <b>Effects</b>: Move constructor. Moves s's resources to *this.
638 //! <b>Throws</b>: Nothing.
640 //! <b>Complexity</b>: Constant.
641 basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW
642 : base_t(boost::move(s.alloc()))
644 if(s.alloc() == this->alloc()){
648 this->assign(s.begin(), s.end());
652 //! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
654 //! <b>Postcondition</b>: x == *this.
656 //! <b>Throws</b>: If allocation throws.
657 basic_string(const basic_string& s, const allocator_type &a)
660 this->priv_terminate_string();
661 this->assign(s.begin(), s.end());
664 //! <b>Effects</b>: Move constructor using the specified allocator.
665 //! Moves s's resources to *this.
667 //! <b>Throws</b>: If allocation throws.
669 //! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
670 basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
673 this->priv_terminate_string();
674 if(a == this->alloc()){
678 this->assign(s.begin(), s.end());
682 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
683 //! and is initialized by a specific number of characters of the s string.
684 basic_string(const basic_string& s, size_type pos, size_type n = npos)
687 this->priv_terminate_string();
689 throw_out_of_range("basic_string::basic_string out of range position");
692 (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
695 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
696 //! and is initialized by a specific number of characters of the s string.
697 basic_string(const basic_string& s, size_type pos, size_type n, const allocator_type& a)
700 this->priv_terminate_string();
702 throw_out_of_range("basic_string::basic_string out of range position");
705 (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
708 //! <b>Effects</b>: Constructs a basic_string taking a default-constructed allocator,
709 //! and is initialized by a specific number of characters of the s c-string.
710 basic_string(const CharT* s, size_type n)
713 this->priv_terminate_string();
714 this->assign(s, s + n);
717 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
718 //! and is initialized by a specific number of characters of the s c-string.
719 basic_string(const CharT* s, size_type n, const allocator_type& a)
722 this->priv_terminate_string();
723 this->assign(s, s + n);
726 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
727 //! and is initialized by the null-terminated s c-string.
728 basic_string(const CharT* s)
731 this->priv_terminate_string();
732 this->assign(s, s + Traits::length(s));
735 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
736 //! and is initialized by the null-terminated s c-string.
737 basic_string(const CharT* s, const allocator_type& a)
740 this->priv_terminate_string();
741 this->assign(s, s + Traits::length(s));
745 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
746 //! and is initialized by n copies of c.
747 basic_string(size_type n, CharT c)
750 this->priv_terminate_string();
754 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
755 //! and is initialized by n copies of c.
756 basic_string(size_type n, CharT c, const allocator_type& a)
759 this->priv_terminate_string();
763 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
764 //! and is initialized by n default-initialized characters.
765 basic_string(size_type n, default_init_t)
769 this->priv_terminate_string();
772 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
773 //! and is initialized by n default-initialized characters.
774 basic_string(size_type n, default_init_t, const allocator_type& a)
778 this->priv_terminate_string();
781 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
782 //! and a range of iterators.
783 template <class InputIterator>
784 basic_string(InputIterator f, InputIterator l)
787 this->priv_terminate_string();
791 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
792 //! and a range of iterators.
793 template <class InputIterator>
794 basic_string(InputIterator f, InputIterator l, const allocator_type& a)
797 this->priv_terminate_string();
801 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
802 //! <b>Effects</b>: Same as basic_string(il.begin(), il.end(), a).
804 basic_string(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
807 this->priv_terminate_string();
808 this->assign(il.begin(), il.end());
812 //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
814 //! <b>Throws</b>: Nothing.
816 //! <b>Complexity</b>: Constant.
817 ~basic_string() BOOST_NOEXCEPT_OR_NOTHROW
820 //! <b>Effects</b>: Copy constructs a string.
822 //! <b>Postcondition</b>: x == *this.
824 //! <b>Complexity</b>: Linear to the elements x contains.
825 basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
828 allocator_type &this_alloc = this->alloc();
829 const allocator_type &x_alloc = x.alloc();
830 container_detail::bool_<allocator_traits_type::
831 propagate_on_container_copy_assignment::value> flag;
832 if(flag && this_alloc != x_alloc){
833 if(!this->is_short()){
834 this->deallocate_block();
835 this->is_short(true);
836 Traits::assign(*this->priv_addr(), CharT(0));
837 this->priv_short_size(0);
840 container_detail::assign_alloc(this->alloc(), x.alloc(), flag);
841 this->assign(x.begin(), x.end());
846 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
848 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
849 //! is false and allocation throws
851 //! <b>Complexity</b>: Constant if allocator_traits_type::
852 //! propagate_on_container_move_assignment is true or
853 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
854 basic_string& operator=(BOOST_RV_REF(basic_string) x)
855 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
856 || allocator_traits_type::is_always_equal::value)
858 //for move constructor, no aliasing (&x != this) is assummed.
859 BOOST_ASSERT(this != &x);
860 allocator_type &this_alloc = this->alloc();
861 allocator_type &x_alloc = x.alloc();
862 const bool propagate_alloc = allocator_traits_type::
863 propagate_on_container_move_assignment::value;
864 container_detail::bool_<propagate_alloc> flag;
865 const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
866 //Resources can be transferred if both allocators are
867 //going to be equal after this function (either propagated or already equal)
868 if(propagate_alloc || allocators_equal){
869 //Destroy objects but retain memory in case x reuses it in the future
871 //Move allocator if needed
872 container_detail::move_alloc(this_alloc, x_alloc, flag);
876 //Else do a one by one move
878 this->assign( x.begin(), x.end());
883 //! <b>Effects</b>: Assignment from a null-terminated c-string.
885 basic_string& operator=(const CharT* s)
886 { return this->assign(s, s + Traits::length(s)); }
888 //! <b>Effects</b>: Returns *this = basic_string(1, c).
890 basic_string& operator=(CharT c)
891 { return this->assign(static_cast<size_type>(1), c); }
893 //! <b>Effects</b>: Equivalent to return assign(sv).
895 template<template <class, class> class BasicStringView>
896 basic_string& operator=(BasicStringView<CharT, Traits> sv)
897 { return this->assign(sv.data(), sv.size()); }
899 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
900 //! <b>Effects</b>: Returns *this = basic_string(il);
902 basic_string& operator=(std::initializer_list<CharT> il)
904 return this->assign(il.begin(), il.end());
908 //! <b>Effects</b>: Returns a copy of the internal allocator.
910 //! <b>Throws</b>: If allocator's copy constructor throws.
912 //! <b>Complexity</b>: Constant.
913 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
914 { return this->alloc(); }
916 //! <b>Effects</b>: Returns a reference to the internal allocator.
918 //! <b>Throws</b>: Nothing
920 //! <b>Complexity</b>: Constant.
922 //! <b>Note</b>: Non-standard extension.
923 stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
924 { return this->alloc(); }
926 //! <b>Effects</b>: Returns a reference to the internal allocator.
928 //! <b>Throws</b>: Nothing
930 //! <b>Complexity</b>: Constant.
932 //! <b>Note</b>: Non-standard extension.
933 const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
934 { return this->alloc(); }
936 //////////////////////////////////////////////
940 //////////////////////////////////////////////
942 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
944 //! <b>Throws</b>: Nothing.
946 //! <b>Complexity</b>: Constant.
947 iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
948 { return this->priv_addr(); }
950 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
952 //! <b>Throws</b>: Nothing.
954 //! <b>Complexity</b>: Constant.
955 const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
956 { return this->priv_addr(); }
958 //! <b>Effects</b>: Returns an iterator to the end of the vector.
960 //! <b>Throws</b>: Nothing.
962 //! <b>Complexity</b>: Constant.
963 iterator end() BOOST_NOEXCEPT_OR_NOTHROW
964 { return this->priv_end_addr(); }
966 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
968 //! <b>Throws</b>: Nothing.
970 //! <b>Complexity</b>: Constant.
971 const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
972 { return this->priv_end_addr(); }
974 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
975 //! of the reversed vector.
977 //! <b>Throws</b>: Nothing.
979 //! <b>Complexity</b>: Constant.
980 reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
981 { return reverse_iterator(this->priv_end_addr()); }
983 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
984 //! of the reversed vector.
986 //! <b>Throws</b>: Nothing.
988 //! <b>Complexity</b>: Constant.
989 const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
990 { return this->crbegin(); }
992 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
993 //! of the reversed vector.
995 //! <b>Throws</b>: Nothing.
997 //! <b>Complexity</b>: Constant.
998 reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
999 { return reverse_iterator(this->priv_addr()); }
1001 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1002 //! of the reversed vector.
1004 //! <b>Throws</b>: Nothing.
1006 //! <b>Complexity</b>: Constant.
1007 const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1008 { return this->crend(); }
1010 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1012 //! <b>Throws</b>: Nothing.
1014 //! <b>Complexity</b>: Constant.
1015 const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1016 { return this->priv_addr(); }
1018 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1020 //! <b>Throws</b>: Nothing.
1022 //! <b>Complexity</b>: Constant.
1023 const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1024 { return this->priv_end_addr(); }
1026 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1027 //! of the reversed vector.
1029 //! <b>Throws</b>: Nothing.
1031 //! <b>Complexity</b>: Constant.
1032 const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1033 { return const_reverse_iterator(this->priv_end_addr()); }
1035 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1036 //! of the reversed vector.
1038 //! <b>Throws</b>: Nothing.
1040 //! <b>Complexity</b>: Constant.
1041 const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1042 { return const_reverse_iterator(this->priv_addr()); }
1044 //////////////////////////////////////////////
1048 //////////////////////////////////////////////
1050 //! <b>Effects</b>: Returns true if the vector contains no elements.
1052 //! <b>Throws</b>: Nothing.
1054 //! <b>Complexity</b>: Constant.
1055 bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1056 { return !this->priv_size(); }
1058 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1060 //! <b>Throws</b>: Nothing.
1062 //! <b>Complexity</b>: Constant.
1063 size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1064 { return this->priv_size(); }
1066 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1068 //! <b>Throws</b>: Nothing.
1070 //! <b>Complexity</b>: Constant.
1071 size_type length() const BOOST_NOEXCEPT_OR_NOTHROW
1072 { return this->size(); }
1074 //! <b>Effects</b>: Returns the largest possible size of the vector.
1076 //! <b>Throws</b>: Nothing.
1078 //! <b>Complexity</b>: Constant.
1079 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1080 { return base_t::max_size(); }
1082 //! <b>Effects</b>: Inserts or erases elements at the end such that
1083 //! the size becomes n. New elements are copy constructed from x.
1085 //! <b>Throws</b>: If memory allocation throws
1087 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1088 void resize(size_type n, CharT c)
1090 if (n <= this->size())
1091 this->erase(this->begin() + n, this->end());
1093 this->append(n - this->size(), c);
1096 //! <b>Effects</b>: Inserts or erases elements at the end such that
1097 //! the size becomes n. New elements are value initialized.
1099 //! <b>Throws</b>: If memory allocation throws
1101 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1102 void resize(size_type n)
1103 { resize(n, CharT()); }
1106 //! <b>Effects</b>: Inserts or erases elements at the end such that
1107 //! the size becomes n. New elements are uninitialized.
1109 //! <b>Throws</b>: If memory allocation throws
1111 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1113 //! <b>Note</b>: Non-standard extension
1114 void resize(size_type n, default_init_t)
1116 if (n <= this->size())
1117 this->erase(this->begin() + n, this->end());
1119 this->priv_reserve(n, false);
1121 this->priv_terminate_string();
1125 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1126 //! capacity() is always greater than or equal to size().
1128 //! <b>Throws</b>: Nothing.
1130 //! <b>Complexity</b>: Constant.
1131 size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1132 { return this->priv_capacity(); }
1134 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1135 //! effect. Otherwise, it is a request for allocation of additional memory.
1136 //! If the request is successful, then capacity() is greater than or equal to
1137 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1139 //! <b>Throws</b>: If memory allocation allocation throws
1140 void reserve(size_type res_arg)
1141 { this->priv_reserve(res_arg); }
1143 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1144 //! with previous allocations. The size of the string is unchanged
1146 //! <b>Throws</b>: Nothing
1148 //! <b>Complexity</b>: Linear to size().
1149 void shrink_to_fit()
1151 //Check if shrinking is possible
1152 if(this->priv_storage() > InternalBufferChars){
1153 //Check if we should pass from dynamically allocated buffer
1154 //to the internal storage
1155 if(this->priv_size() < InternalBufferChars){
1156 //Dynamically allocated buffer attributes
1157 pointer long_addr = this->priv_long_addr();
1158 size_type long_storage = this->priv_long_storage();
1159 size_type long_size = this->priv_long_size();
1160 //Shrink from allocated buffer to the internal one, including trailing null
1161 Traits::copy( boost::movelib::to_raw_pointer(this->priv_short_addr())
1162 , boost::movelib::to_raw_pointer(long_addr)
1164 this->is_short(true);
1165 this->alloc().deallocate(long_addr, long_storage);
1168 //Shrinking in dynamic buffer
1169 this->priv_shrink_to_fit_dynamic_buffer(alloc_version());
1174 //////////////////////////////////////////////
1178 //////////////////////////////////////////////
1180 //! <b>Requires</b>: !empty()
1182 //! <b>Effects</b>: Returns a reference to the first
1183 //! element of the container.
1185 //! <b>Throws</b>: Nothing.
1187 //! <b>Complexity</b>: Constant.
1188 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1190 BOOST_ASSERT(!this->empty());
1191 return *this->priv_addr();
1194 //! <b>Requires</b>: !empty()
1196 //! <b>Effects</b>: Returns a const reference to the first
1197 //! element of the container.
1199 //! <b>Throws</b>: Nothing.
1201 //! <b>Complexity</b>: Constant.
1202 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1204 BOOST_ASSERT(!this->empty());
1205 return *this->priv_addr();
1208 //! <b>Requires</b>: !empty()
1210 //! <b>Effects</b>: Returns a reference to the last
1211 //! element of the container.
1213 //! <b>Throws</b>: Nothing.
1215 //! <b>Complexity</b>: Constant.
1216 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1218 BOOST_ASSERT(!this->empty());
1219 return *(this->priv_addr() + (this->size() - 1u) );
1222 //! <b>Requires</b>: !empty()
1224 //! <b>Effects</b>: Returns a const reference to the last
1225 //! element of the container.
1227 //! <b>Throws</b>: Nothing.
1229 //! <b>Complexity</b>: Constant.
1230 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1232 BOOST_ASSERT(!this->empty());
1233 return *(this->priv_addr() + (this->size() - 1u) );
1236 //! <b>Requires</b>: size() > n.
1238 //! <b>Effects</b>: Returns a reference to the nth element
1239 //! from the beginning of the container.
1241 //! <b>Throws</b>: Nothing.
1243 //! <b>Complexity</b>: Constant.
1244 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1246 BOOST_ASSERT(this->size() > n);
1247 return *(this->priv_addr() + n);
1250 //! <b>Requires</b>: size() > n.
1252 //! <b>Effects</b>: Returns a const reference to the nth element
1253 //! from the beginning of the container.
1255 //! <b>Throws</b>: Nothing.
1257 //! <b>Complexity</b>: Constant.
1258 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1260 BOOST_ASSERT(this->size() > n);
1261 return *(this->priv_addr() + n);
1264 //! <b>Requires</b>: size() > n.
1266 //! <b>Effects</b>: Returns a reference to the nth element
1267 //! from the beginning of the container.
1269 //! <b>Throws</b>: std::range_error if n >= size()
1271 //! <b>Complexity</b>: Constant.
1272 reference at(size_type n)
1274 if (n >= this->size())
1275 throw_out_of_range("basic_string::at invalid subscript");
1276 return *(this->priv_addr() + n);
1279 //! <b>Requires</b>: size() > n.
1281 //! <b>Effects</b>: Returns a const reference to the nth element
1282 //! from the beginning of the container.
1284 //! <b>Throws</b>: std::range_error if n >= size()
1286 //! <b>Complexity</b>: Constant.
1287 const_reference at(size_type n) const {
1288 if (n >= this->size())
1289 throw_out_of_range("basic_string::at invalid subscript");
1290 return *(this->priv_addr() + n);
1293 //////////////////////////////////////////////
1297 //////////////////////////////////////////////
1299 //! <b>Effects</b>: Calls append(str.data, str.size()).
1301 //! <b>Returns</b>: *this
1302 basic_string& operator+=(const basic_string& s)
1303 { return this->append(s); }
1305 //! <b>Effects</b>: Same as `return append(sv)`.
1307 template<template<class, class> class BasicStringView>
1308 basic_string& operator+=(BasicStringView<CharT, Traits> sv)
1310 return this->append(sv);
1313 //! <b>Effects</b>: Calls append(s).
1315 //! <b>Returns</b>: *this
1316 basic_string& operator+=(const CharT* s)
1317 { return this->append(s); }
1319 //! <b>Effects</b>: Calls append(1, c).
1321 //! <b>Returns</b>: *this
1322 basic_string& operator+=(CharT c)
1323 { this->push_back(c); return *this; }
1325 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1326 //! <b>Effects</b>: Returns append(il)
1328 basic_string& operator+=(std::initializer_list<CharT> il)
1330 return this->append(il);
1334 //! <b>Effects</b>: Calls append(str.data(), str.size()).
1336 //! <b>Returns</b>: *this
1337 basic_string& append(const basic_string& s)
1338 { return this->append(s.begin(), s.end()); }
1340 //! <b>Effects</b>: Same as return append(sv.data(), sv.size()).
1342 template<template<class, class> class BasicStringView>
1343 basic_string& append(BasicStringView<CharT, Traits> sv)
1344 { return this->append(sv.data(), sv.size()); }
1346 //! <b>Requires</b>: pos <= str.size()
1348 //! <b>Effects</b>: Determines the effective length rlen of the string to append
1349 //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
1351 //! <b>Throws</b>: If memory allocation throws and out_of_range if pos > str.size()
1353 //! <b>Returns</b>: *this
1354 basic_string& append(const basic_string& s, size_type pos, size_type n = npos)
1357 throw_out_of_range("basic_string::append out of range position");
1358 return this->append(s.begin() + pos,
1359 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
1362 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1364 //! <b>Effects</b>: The function replaces the string controlled by *this with
1365 //! a string of length size() + n whose irst size() elements are a copy of the
1366 //! original string controlled by *this and whose remaining
1367 //! elements are a copy of the initial n elements of s.
1369 //! <b>Throws</b>: If memory allocation throws length_error if size() + n > max_size().
1371 //! <b>Returns</b>: *this
1372 basic_string& append(const CharT* s, size_type n)
1373 { return this->append(s, s + n); }
1375 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1377 //! <b>Effects</b>: Calls append(s, traits::length(s)).
1379 //! <b>Returns</b>: *this
1380 basic_string& append(const CharT* s)
1381 { return this->append(s, s + Traits::length(s)); }
1383 //! <b>Effects</b>: Equivalent to append(basic_string(n, c)).
1385 //! <b>Returns</b>: *this
1386 basic_string& append(size_type n, CharT c)
1387 { return this->append(cvalue_iterator(c, n), cvalue_iterator()); }
1389 //! <b>Requires</b>: [first,last) is a valid range.
1391 //! <b>Effects</b>: Equivalent to append(basic_string(first, last)).
1393 //! <b>Returns</b>: *this
1394 template <class InputIter>
1395 basic_string& append(InputIter first, InputIter last)
1396 { this->insert(this->end(), first, last); return *this; }
1398 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1399 //! <b>Effects</b>: Returns append(il.begin(), il.size()).
1401 basic_string& append(std::initializer_list<CharT> il)
1403 return this->append(il.begin(), il.size());
1407 //! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
1409 void push_back(CharT c)
1411 const size_type old_size = this->priv_size();
1412 if (old_size < this->capacity()){
1413 const pointer addr = this->priv_addr();
1414 this->priv_construct_null(addr + old_size + 1);
1415 Traits::assign(addr[old_size], c);
1416 this->priv_size(old_size+1);
1419 //No enough memory, insert a new object at the end
1420 this->append(size_type(1), c);
1424 //! <b>Effects</b>: Equivalent to assign(str, 0, npos).
1426 //! <b>Returns</b>: *this
1427 basic_string& assign(const basic_string& s)
1428 { return this->operator=(s); }
1430 //! <b>Effects</b>: Equivalent to return assign(sv.data(), sv.size()).
1432 //! <b>Returns</b>: *this
1433 template<template <class, class> class BasicStringView>
1434 basic_string& assign(BasicStringView<CharT, Traits> sv)
1435 { return this->operator=(sv); }
1437 //! <b>Effects</b>: The function replaces the string controlled by *this
1438 //! with a string of length str.size() whose elements are a copy of the string
1439 //! controlled by str. Leaves str in a valid but unspecified state.
1441 //! <b>Throws</b>: Nothing
1443 //! <b>Returns</b>: *this
1444 basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_NOEXCEPT_OR_NOTHROW
1445 { return this->swap_data(ms), *this; }
1447 //! <b>Requires</b>: pos <= str.size()
1449 //! <b>Effects</b>: Determines the effective length rlen of the string to assign as
1450 //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).
1452 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
1454 //! <b>Returns</b>: *this
1455 basic_string& assign(const basic_string& s, size_type pos, size_type n)
1458 throw_out_of_range("basic_string::assign out of range position");
1459 return this->assign(s.begin() + pos,
1460 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
1463 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1465 //! <b>Effects</b>: Replaces the string controlled by *this with a string of
1466 //! length n whose elements are a copy of those pointed to by s.
1468 //! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
1470 //! <b>Returns</b>: *this
1471 basic_string& assign(const CharT* s, size_type n)
1472 { return this->assign(s, s + n); }
1474 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1476 //! <b>Effects</b>: Calls assign(s, traits::length(s)).
1478 //! <b>Returns</b>: *this
1479 basic_string& assign(const CharT* s)
1480 { return this->assign(s, s + Traits::length(s)); }
1482 //! <b>Effects</b>: Equivalent to assign(basic_string(n, c)).
1484 //! <b>Returns</b>: *this
1485 basic_string& assign(size_type n, CharT c)
1486 { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
1488 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1490 //! <b>Returns</b>: *this
1491 basic_string& assign(const CharT* first, const CharT* last)
1493 size_type n = static_cast<size_type>(last - first);
1495 CharT* ptr = boost::movelib::to_raw_pointer(this->priv_addr());
1496 Traits::copy(ptr, first, n);
1497 this->priv_construct_null(ptr + n);
1502 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1504 //! <b>Returns</b>: *this
1505 template <class InputIter>
1506 basic_string& assign(InputIter first, InputIter last
1507 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1508 , typename container_detail::disable_if_convertible<InputIter, size_type>::type * = 0
1513 const pointer addr = this->priv_addr();
1514 CharT *ptr = boost::movelib::to_raw_pointer(addr);
1515 const size_type old_size = this->priv_size();
1516 while (first != last && cur != old_size) {
1517 Traits::assign(*ptr, *first);
1523 this->erase(addr + cur, addr + old_size);
1525 this->append(first, last);
1529 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1530 //! <b>Effects</b>: Returns assign(il.begin(), il.size()).
1532 basic_string& assign(std::initializer_list<CharT> il)
1534 return this->assign(il.begin(), il.size());
1538 //! <b>Requires</b>: pos <= size().
1540 //! <b>Effects</b>: Calls insert(pos, str.data(), str.size()).
1542 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
1544 //! <b>Returns</b>: *this
1545 basic_string& insert(size_type pos, const basic_string& s)
1547 const size_type sz = this->size();
1549 throw_out_of_range("basic_string::insert out of range position");
1550 if (sz > this->max_size() - s.size())
1551 throw_length_error("basic_string::insert max_size() exceeded");
1552 this->insert(this->priv_addr() + pos, s.begin(), s.end());
1556 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
1558 //! <b>Effects</b>: Determines the effective length rlen of the string to insert as
1559 //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
1561 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
1563 //! <b>Returns</b>: *this
1564 basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n = npos)
1566 const size_type sz = this->size();
1567 const size_type str_size = s.size();
1568 if (pos1 > sz || pos2 > str_size)
1569 throw_out_of_range("basic_string::insert out of range position");
1570 size_type len = container_detail::min_value(n, str_size - pos2);
1571 if (sz > this->max_size() - len)
1572 throw_length_error("basic_string::insert max_size() exceeded");
1573 const CharT *beg_ptr = boost::movelib::to_raw_pointer(s.begin()) + pos2;
1574 const CharT *end_ptr = beg_ptr + len;
1575 this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
1579 //! <b>Requires</b>: s points to an array of at least n elements of CharT and pos <= size().
1581 //! <b>Effects</b>: Replaces the string controlled by *this with a string of length size() + n
1582 //! whose first pos elements are a copy of the initial elements of the original string
1583 //! controlled by *this and whose next n elements are a copy of the elements in s and whose
1584 //! remaining elements are a copy of the remaining elements of the original string controlled by *this.
1586 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size() or
1587 //! length_error if size() + n > max_size().
1589 //! <b>Returns</b>: *this
1590 basic_string& insert(size_type pos, const CharT* s, size_type n)
1592 if (pos > this->size())
1593 throw_out_of_range("basic_string::insert out of range position");
1594 if (this->size() > this->max_size() - n)
1595 throw_length_error("basic_string::insert max_size() exceeded");
1596 this->insert(this->priv_addr() + pos, s, s + n);
1600 //! <b>Requires</b>: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT
1602 //! <b>Effects</b>: Calls insert(pos, s, traits::length(s)).
1604 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1605 //! length_error if size() > max_size() - Traits::length(s)
1607 //! <b>Returns</b>: *this
1608 basic_string& insert(size_type pos, const CharT* s)
1610 if (pos > this->size())
1611 throw_out_of_range("basic_string::insert out of range position");
1612 size_type len = Traits::length(s);
1613 if (this->size() > this->max_size() - len)
1614 throw_length_error("basic_string::insert max_size() exceeded");
1615 this->insert(this->priv_addr() + pos, s, s + len);
1619 //! <b>Effects</b>: Equivalent to insert(pos, basic_string(n, c)).
1621 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1622 //! length_error if size() > max_size() - n
1624 //! <b>Returns</b>: *this
1625 basic_string& insert(size_type pos, size_type n, CharT c)
1627 if (pos > this->size())
1628 throw_out_of_range("basic_string::insert out of range position");
1629 if (this->size() > this->max_size() - n)
1630 throw_length_error("basic_string::insert max_size() exceeded");
1631 this->insert(const_iterator(this->priv_addr() + pos), n, c);
1635 //! <b>Effects</b>: Same as `return insert(pos, sv.data(), sv.size())`.
1637 template<template<class, class> class BasicStringView>
1638 basic_string& insert(size_type pos, BasicStringView<CharT, Traits> sv)
1639 { return this->insert(pos, sv.data(), sv.size()); }
1641 //! <b>Requires</b>: p is a valid iterator on *this.
1643 //! <b>Effects</b>: inserts a copy of c before the character referred to by p.
1645 //! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
1646 iterator insert(const_iterator p, CharT c)
1648 size_type new_offset = p - this->priv_addr();
1649 this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
1650 return this->priv_addr() + new_offset;
1653 //! <b>Requires</b>: p is a valid iterator on *this.
1655 //! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
1657 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1658 iterator insert(const_iterator p, size_type n, CharT c)
1659 { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
1661 //! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
1663 //! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
1665 //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
1666 template <class InputIter>
1667 iterator insert(const_iterator p, InputIter first, InputIter last
1668 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1669 , typename container_detail::disable_if_or
1671 , container_detail::is_convertible<InputIter, size_type>
1672 , container_detail::is_not_input_iterator<InputIter>
1677 const size_type n_pos = p - this->cbegin();
1678 for ( ; first != last; ++first, ++p) {
1679 p = this->insert(p, *first);
1681 return this->begin() + n_pos;
1684 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1685 template <class ForwardIter>
1686 iterator insert(const_iterator p, ForwardIter first, ForwardIter last
1687 , typename container_detail::disable_if_or
1689 , container_detail::is_convertible<ForwardIter, size_type>
1690 , container_detail::is_input_iterator<ForwardIter>
1694 const size_type n_pos = p - this->cbegin();
1695 if (first != last) {
1696 const size_type n = boost::container::iterator_distance(first, last);
1697 const size_type old_size = this->priv_size();
1698 const size_type remaining = this->capacity() - old_size;
1699 const pointer old_start = this->priv_addr();
1700 bool enough_capacity = false;
1701 size_type new_cap = 0;
1703 //Check if we have enough capacity
1704 pointer hint = pointer();
1705 pointer allocation_ret = pointer();
1706 if (remaining >= n){
1707 enough_capacity = true;
1710 //Otherwise expand current buffer or allocate new storage
1711 new_cap = this->next_capacity(n);
1713 allocation_ret = this->allocation_command
1714 (allocate_new | expand_fwd | expand_bwd, old_size + n + 1, new_cap, hint);
1716 //Check forward expansion
1717 if(old_start == allocation_ret){
1718 enough_capacity = true;
1719 this->priv_storage(new_cap);
1724 if(enough_capacity){
1725 const size_type elems_after = old_size - (p - old_start);
1726 const size_type old_length = old_size;
1727 if (elems_after >= n) {
1728 const pointer pointer_past_last = old_start + old_size + 1;
1729 priv_uninitialized_copy(old_start + (old_size - n + 1),
1730 pointer_past_last, pointer_past_last);
1732 this->priv_size(old_size+n);
1733 Traits::move(const_cast<CharT*>(boost::movelib::to_raw_pointer(p + n)),
1734 boost::movelib::to_raw_pointer(p),
1735 (elems_after - n) + 1);
1736 this->priv_copy(first, last, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1739 ForwardIter mid = first;
1740 boost::container::iterator_advance(mid, elems_after + 1);
1742 priv_uninitialized_copy(mid, last, old_start + old_size + 1);
1743 const size_type newer_size = old_size + (n - elems_after);
1744 this->priv_size(newer_size);
1745 priv_uninitialized_copy
1746 (p, const_iterator(old_start + old_length + 1),
1747 old_start + newer_size);
1748 this->priv_size(newer_size + elems_after);
1749 this->priv_copy(first, mid, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1753 pointer new_start = allocation_ret;
1755 //Copy data to new buffer
1756 size_type new_length = 0;
1757 //This can't throw, since characters are POD
1758 new_length += priv_uninitialized_copy
1759 (const_iterator(old_start), p, new_start);
1760 new_length += priv_uninitialized_copy
1761 (first, last, new_start + new_length);
1762 new_length += priv_uninitialized_copy
1763 (p, const_iterator(old_start + old_size),
1764 new_start + new_length);
1765 this->priv_construct_null(new_start + new_length);
1767 this->deallocate_block();
1768 this->is_short(false);
1769 this->priv_long_addr(new_start);
1770 this->priv_long_size(new_length);
1771 this->priv_long_storage(new_cap);
1774 //value_type is POD, so backwards expansion is much easier
1775 //than with vector<T>
1776 value_type * const oldbuf = boost::movelib::to_raw_pointer(old_start);
1777 value_type * const newbuf = boost::movelib::to_raw_pointer(new_start);
1778 const value_type *const pos = boost::movelib::to_raw_pointer(p);
1779 const size_type before = pos - oldbuf;
1781 //First move old data
1782 Traits::move(newbuf, oldbuf, before);
1783 Traits::move(newbuf + before + n, pos, old_size - before);
1784 //Now initialize the new data
1785 priv_uninitialized_copy(first, last, new_start + before);
1786 this->priv_construct_null(new_start + (old_size + n));
1787 this->is_short(false);
1788 this->priv_long_addr(new_start);
1789 this->priv_long_size(old_size + n);
1790 this->priv_long_storage(new_cap);
1794 return this->begin() + n_pos;
1798 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1799 //! <b>Effects</b>: As if by insert(p, il.begin(), il.end()).
1801 //! <b>Returns</b>: An iterator which refers to the copy of the first inserted
1802 //! character, or p if i1 is empty.
1803 iterator insert(const_iterator p, std::initializer_list<CharT> il)
1805 return this->insert(p, il.begin(), il.end());
1809 //! <b>Effects</b>: Removes the last element from the container.
1811 //! <b>Throws</b>: Nothing.
1813 //! <b>Complexity</b>: Constant time.
1814 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1816 BOOST_ASSERT(!this->empty());
1817 iterator p = this->end();
1821 //! <b>Requires</b>: pos <= size()
1823 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
1824 //! The function then replaces the string controlled by *this with a string of length size() - xlen
1825 //! whose first pos elements are a copy of the initial elements of the original string controlled by *this,
1826 //! and whose remaining elements are a copy of the elements of the original string controlled by *this
1827 //! beginning at position pos + xlen.
1829 //! <b>Throws</b>: out_of_range if pos > size().
1831 //! <b>Returns</b>: *this
1832 basic_string& erase(size_type pos = 0, size_type n = npos)
1834 if (pos > this->size())
1835 throw_out_of_range("basic_string::erase out of range position");
1836 const pointer addr = this->priv_addr();
1837 erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos));
1841 //! <b>Effects</b>: Removes the character referred to by p.
1843 //! <b>Throws</b>: Nothing
1845 //! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
1846 //! erased. If no such element exists, end() is returned.
1847 iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1849 // The move includes the terminating null.
1850 CharT * const ptr = const_cast<CharT*>(boost::movelib::to_raw_pointer(p));
1851 const size_type old_size = this->priv_size();
1853 boost::movelib::to_raw_pointer(p + 1),
1854 old_size - (p - this->priv_addr()));
1855 this->priv_size(old_size-1);
1856 return iterator(ptr);
1859 //! <b>Requires</b>: first and last are valid iterators on *this, defining a range [first,last).
1861 //! <b>Effects</b>: Removes the characters in the range [first,last).
1863 //! <b>Throws</b>: Nothing
1865 //! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
1866 //! the other elements being erased. If no such element exists, end() is returned.
1867 iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
1869 CharT * f = const_cast<CharT*>(boost::movelib::to_raw_pointer(first));
1870 if (first != last) { // The move includes the terminating null.
1871 const size_type num_erased = last - first;
1872 const size_type old_size = this->priv_size();
1874 boost::movelib::to_raw_pointer(last),
1875 (old_size + 1)-(last - this->priv_addr()));
1876 const size_type new_length = old_size - num_erased;
1877 this->priv_size(new_length);
1882 //! <b>Effects</b>: Erases all the elements of the vector.
1884 //! <b>Throws</b>: Nothing.
1886 //! <b>Complexity</b>: Linear to the number of elements in the vector.
1887 void clear() BOOST_NOEXCEPT_OR_NOTHROW
1889 if (!this->empty()) {
1890 Traits::assign(*this->priv_addr(), CharT(0));
1895 //! <b>Requires</b>: pos1 <= size().
1897 //! <b>Effects</b>: Calls replace(pos1, n1, str.data(), str.size()).
1899 //! <b>Throws</b>: if memory allocation throws or out_of_range if pos1 > size().
1901 //! <b>Returns</b>: *this
1902 basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
1904 if (pos1 > this->size())
1905 throw_out_of_range("basic_string::replace out of range position");
1906 const size_type len = container_detail::min_value(n1, this->size() - pos1);
1907 if (this->size() - len >= this->max_size() - str.size())
1908 throw_length_error("basic_string::replace max_size() exceeded");
1909 const pointer addr = this->priv_addr();
1910 return this->replace( const_iterator(addr + pos1)
1911 , const_iterator(addr + pos1 + len)
1912 , str.begin(), str.end());
1915 //! <b>Effects</b>: Calls `return replace(pos1, n1, sv.data(), sv.size());`.
1917 template<template<class, class> class BasicStringView>
1918 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv)
1920 return this->replace(pos1, n1, sv.data(), sv.size());
1923 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
1925 //! <b>Effects</b>: Determines the effective length rlen of the string to be
1926 //! inserted as the smaller of n2 and str.size() - pos2 and calls
1927 //! replace(pos1, n1, str.data() + pos2, rlen).
1929 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size().
1931 //! <b>Returns</b>: *this
1932 basic_string& replace(size_type pos1, size_type n1,
1933 const basic_string& str, size_type pos2, size_type n2 = npos)
1935 if (pos2 > str.size())
1936 throw_out_of_range("basic_string::replace out of range position");
1937 return this->replace(pos1, n1, str.data()+pos2, container_detail::min_value(n2, str.size() - pos2));
1940 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size().
1942 //! <b>Effects</b>: Determines the effective length rlen of the string to be inserted as the
1943 //! smaller of n2 and sv.size() - pos2 and calls `replace(pos1, n1, sv.data() + pos2, rlen)`.
1945 //! <b>Returns</b>: *this.
1946 template<template<class, class> class BasicStringView>
1947 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv,
1948 size_type pos2, size_type n2 = npos)
1950 if (pos2 > sv.size())
1951 throw_out_of_range("basic_string::replace out of range position");
1952 return this->replace(pos1, n1, sv.data()+pos2, container_detail::min_value(n2, sv.size() - pos2));
1955 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1957 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the
1958 //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error.
1959 //! Otherwise, the function replaces the string controlled by *this with a string of
1960 //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements
1961 //! of the original string controlled by *this, whose next n2 elements are a copy of the
1962 //! initial n2 elements of s, and whose remaining elements are a copy of the elements of
1963 //! the original string controlled by *this beginning at position pos + xlen.
1965 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1966 //! if the length of the resulting string would exceed max_size()
1968 //! <b>Returns</b>: *this
1969 basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
1971 if (pos1 > this->size())
1972 throw_out_of_range("basic_string::replace out of range position");
1973 const size_type len = container_detail::min_value(n1, this->size() - pos1);
1974 const size_type max_size = this->max_size();
1975 if (n2 > max_size || (this->size() - len) >= (max_size - n2))
1976 throw_length_error("basic_string::replace max_size() exceeded");
1977 const pointer addr = this->priv_addr() + pos1;
1978 return this->replace(addr, addr + len, s, s + n2);
1981 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1983 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller
1984 //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise,
1985 //! the function replaces the string controlled by *this with a string of length size() - xlen + n2
1986 //! whose first pos1 elements are a copy of the initial elements of the original string controlled
1987 //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose
1988 //! remaining elements are a copy of the elements of the original string controlled by *this
1989 //! beginning at position pos + xlen.
1991 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1992 //! if the length of the resulting string would exceed max_size()
1994 //! <b>Returns</b>: *this
1995 basic_string& replace(size_type pos, size_type n1, const CharT* s)
1997 return this->replace(pos, n1, s, Traits::length(s));
2000 //! <b>Requires</b>: pos1 <= size().
2002 //! <b>Effects</b>: Equivalent to replace(pos1, n1, basic_string(n2, c)).
2004 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
2005 //! if the length of the resulting string would exceed max_size()
2007 //! <b>Returns</b>: *this
2008 basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
2010 if (pos1 > this->size())
2011 throw_out_of_range("basic_string::replace out of range position");
2012 const size_type len = container_detail::min_value(n1, this->size() - pos1);
2013 if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
2014 throw_length_error("basic_string::replace max_size() exceeded");
2015 const pointer addr = this->priv_addr();
2016 return this->replace(addr + pos1, addr + pos1 + len, n2, c);
2019 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2021 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, str).
2023 //! <b>Throws</b>: if memory allocation throws
2025 //! <b>Returns</b>: *this
2026 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
2027 { return this->replace(i1, i2, str.data(), str.data()+str.size()); }
2029 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and
2030 //! s points to an array of at least n elements
2032 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, n).
2034 //! <b>Throws</b>: if memory allocation throws
2036 //! <b>Returns</b>: *this
2037 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n)
2038 { return this->replace(i1, i2, s, s + n); }
2040 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and s points to an
2041 //! array of at least traits::length(s) + 1 elements of CharT.
2043 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)).
2045 //! <b>Throws</b>: if memory allocation throws
2047 //! <b>Returns</b>: *this
2048 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s)
2049 { return this->replace(i1, i2, s, s + Traits::length(s)); }
2051 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2053 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)).
2055 //! <b>Throws</b>: if memory allocation throws
2057 //! <b>Returns</b>: *this
2058 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c)
2060 const size_type len = static_cast<size_type>(i2 - i1);
2062 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), n, c);
2066 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), len, c);
2067 insert(i2, n - len, c);
2072 //! <b>Requires</b>: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges.
2074 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)).
2076 //! <b>Throws</b>: if memory allocation throws
2078 //! <b>Returns</b>: *this
2079 template <class InputIter>
2080 basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
2081 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2082 , typename container_detail::disable_if_or
2084 , container_detail::is_convertible<InputIter, size_type>
2085 , container_detail::is_input_iterator<InputIter>
2090 for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
2091 Traits::assign(*const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), *j1);
2095 this->erase(i1, i2);
2097 this->insert(i2, j1, j2);
2101 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2102 template <class ForwardIter>
2103 basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
2104 , typename container_detail::disable_if_or
2106 , container_detail::is_convertible<ForwardIter, size_type>
2107 , container_detail::is_not_input_iterator<ForwardIter>
2111 difference_type n = boost::container::iterator_distance(j1, j2);
2112 const difference_type len = i2 - i1;
2114 this->priv_copy(j1, j2, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2115 this->erase(i1 + n, i2);
2119 boost::container::iterator_advance(m, len);
2120 this->priv_copy(j1, m, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2121 this->insert(i2, m, j2);
2127 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2129 //! <b>Effects</b>: Calls `replace(i1 - begin(), i2 - i1, sv).`.
2131 //! <b>Returns</b>: *this.
2132 template<template <class, class> class BasicStringView>
2133 basic_string& replace(const_iterator i1, const_iterator i2, BasicStringView<CharT, Traits> sv)
2135 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2136 , static_cast<size_type>(i2 - i1), sv);
2139 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
2140 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2142 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, il.begin(), il.size()).
2144 //! <b>Returns</b>: *this.
2145 basic_string& replace(const_iterator i1, const_iterator i2, std::initializer_list<CharT> il)
2147 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2148 , static_cast<size_type>(i2 - i1)
2149 , il.begin(), il.size());
2153 //! <b>Requires</b>: pos <= size()
2155 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as the
2156 //! smaller of n and size() - pos. s shall designate an array of at least rlen elements.
2157 //! The function then replaces the string designated by s with a string of length rlen
2158 //! whose elements are a copy of the string controlled by *this beginning at position pos.
2159 //! The function does not append a null object to the string designated by s.
2161 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos > size().
2163 //! <b>Returns</b>: rlen
2164 size_type copy(CharT* s, size_type n, size_type pos = 0) const
2166 if (pos > this->size())
2167 throw_out_of_range("basic_string::copy out of range position");
2168 const size_type len = container_detail::min_value(n, this->size() - pos);
2169 Traits::copy(s, boost::movelib::to_raw_pointer(this->priv_addr() + pos), len);
2173 //! <b>Effects</b>: *this contains the same sequence of characters that was in s,
2174 //! s contains the same sequence of characters that was in *this.
2176 //! <b>Throws</b>: Nothing
2177 void swap(basic_string& x)
2178 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
2179 || allocator_traits_type::is_always_equal::value)
2181 this->base_t::swap_data(x);
2182 container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
2183 container_detail::swap_alloc(this->alloc(), x.alloc(), flag);
2186 //////////////////////////////////////////////
2190 //////////////////////////////////////////////
2192 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2194 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2196 //! <b>Complexity</b>: constant time.
2197 const CharT* c_str() const BOOST_NOEXCEPT_OR_NOTHROW
2198 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2200 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2202 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2204 //! <b>Complexity</b>: constant time.
2205 const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW
2206 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2208 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2210 //! <b>Complexity</b>: constant time.
2211 CharT* data() BOOST_NOEXCEPT_OR_NOTHROW
2212 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2214 #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
2215 //! <b>Returns</b>: a string_view to the characters in the string.
2217 //! <b>Complexity</b>: constant time.
2218 template<template <class, class> class BasicStringView>
2219 operator BasicStringView<CharT, Traits>() const BOOST_NOEXCEPT_OR_NOTHROW
2220 { return this->to_view< BasicStringView<CharT, Traits> >(); }
2223 //! <b>Returns</b>: a string_view to the characters in the string.
2225 //! <b>Complexity</b>: constant time.
2227 //! <b>Note</b>: This function is available to write portable code for compilers
2228 //! that don't support templated conversion operators.
2229 template<class BasicStringView>
2230 BasicStringView to_view() const BOOST_NOEXCEPT_OR_NOTHROW
2231 { return BasicStringView(this->data(), this->size()); }
2233 //////////////////////////////////////////////
2235 // string operations
2237 //////////////////////////////////////////////
2239 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2240 //! of the following conditions hold:
2241 //! 1) pos <= xpos and xpos + str.size() <= size();
2242 //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2244 //! <b>Throws</b>: Nothing
2246 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2247 size_type find(const basic_string& s, size_type pos = 0) const
2248 { return find(s.c_str(), pos, s.size()); }
2250 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2251 //! of the following conditions hold:
2252 //! 1) pos <= xpos and xpos + sv.size() <= size();
2253 //! 2) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2255 //! <b>Throws</b>: Nothing
2257 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2258 template<template <class, class> class BasicStringView>
2259 size_type find(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2260 { return find(sv.data(), pos, sv.size()); }
2262 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2264 //! <b>Throws</b>: Nothing
2266 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
2267 size_type find(const CharT* s, size_type pos, size_type n) const
2269 if (pos + n > this->size())
2272 const pointer addr = this->priv_addr();
2273 pointer finish = addr + this->priv_size();
2274 const const_iterator result =
2275 boost::container::search(boost::movelib::to_raw_pointer(addr + pos),
2276 boost::movelib::to_raw_pointer(finish),
2277 s, s + n, Eq_traits<Traits>());
2278 return result != finish ? result - begin() : npos;
2282 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2284 //! <b>Throws</b>: Nothing
2286 //! <b>Returns</b>: find(basic_string(s), pos).
2287 size_type find(const CharT* s, size_type pos = 0) const
2288 { return this->find(s, pos, Traits::length(s)); }
2290 //! <b>Throws</b>: Nothing
2292 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
2293 size_type find(CharT c, size_type pos = 0) const
2295 const size_type sz = this->size();
2299 const pointer addr = this->priv_addr();
2300 pointer finish = addr + sz;
2301 const const_iterator result =
2302 boost::container::find_if(addr + pos, finish,
2303 boost::container::bind2nd(Eq_traits<Traits>(), c));
2304 return result != finish ? result - begin() : npos;
2308 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2309 //! that both of the following conditions obtain:
2310 //! a) xpos <= pos and xpos + str.size() <= size();
2311 //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2313 //! <b>Throws</b>: Nothing
2315 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2316 size_type rfind(const basic_string& str, size_type pos = npos) const
2317 { return rfind(str.c_str(), pos, str.size()); }
2319 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2320 //! that both of the following conditions obtain:
2321 //! a) xpos <= pos and xpos + sv.size() <= size();
2322 //! b) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2324 //! <b>Throws</b>: Nothing
2326 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2327 template<template <class, class> class BasicStringView>
2328 size_type rfind(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2329 { return rfind(sv.data(), pos, sv.size()); }
2331 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2333 //! <b>Throws</b>: Nothing
2335 //! <b>Returns</b>: rfind(basic_string(s, n), pos).
2336 size_type rfind(const CharT* s, size_type pos, size_type n) const
2338 const size_type len = this->size();
2343 return container_detail::min_value(len, pos);
2345 const const_iterator last = begin() + container_detail::min_value(len - n, pos) + n;
2346 const const_iterator result = find_end(begin(), last,
2348 Eq_traits<Traits>());
2349 return result != last ? result - begin() : npos;
2353 //! <b>Requires</b>: pos <= size() and s points to an array of at least
2354 //! traits::length(s) + 1 elements of CharT.
2356 //! <b>Throws</b>: Nothing
2358 //! <b>Returns</b>: rfind(basic_string(s), pos).
2359 size_type rfind(const CharT* s, size_type pos = npos) const
2360 { return rfind(s, pos, Traits::length(s)); }
2362 //! <b>Throws</b>: Nothing
2364 //! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
2365 size_type rfind(CharT c, size_type pos = npos) const
2367 const size_type len = this->size();
2372 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2373 const_reverse_iterator rresult =
2374 boost::container::find_if(const_reverse_iterator(last), rend(),
2375 boost::container::bind2nd(Eq_traits<Traits>(), c));
2376 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2380 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2381 //! following conditions obtain: a) pos <= xpos and xpos < size();
2382 //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2384 //! <b>Throws</b>: Nothing
2386 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2387 size_type find_first_of(const basic_string& str, size_type pos = 0) const
2388 { return this->find_first_of(str.c_str(), pos, str.size()); }
2390 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2391 //! following conditions obtain: a) pos <= xpos and xpos < size();
2392 //! b) traits::eq(at(xpos), sv.at(I)) for some element I of the string controlled by sv.
2394 //! <b>Throws</b>: Nothing
2396 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2397 template<template <class, class> class BasicStringView>
2398 size_type find_first_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2399 { return this->find_first_of(sv.data(), pos, sv.size()); }
2401 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2403 //! <b>Throws</b>: Nothing
2405 //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
2406 size_type find_first_of(const CharT* s, size_type pos, size_type n) const
2408 const size_type sz = this->size();
2412 const pointer addr = this->priv_addr();
2413 pointer finish = addr + sz;
2414 const_iterator result = boost::container::find_first_of
2415 (addr + pos, finish, s, s + n, Eq_traits<Traits>());
2416 return result != finish ? result - this->begin() : npos;
2420 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2422 //! <b>Throws</b>: Nothing
2424 //! <b>Returns</b>: find_first_of(basic_string(s), pos).
2425 size_type find_first_of(const CharT* s, size_type pos = 0) const
2426 { return this->find_first_of(s, pos, Traits::length(s)); }
2428 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2430 //! <b>Throws</b>: Nothing
2432 //! <b>Returns</b>: find_first_of(basic_string<CharT,traits,Allocator>(1,c), pos).
2433 size_type find_first_of(CharT c, size_type pos = 0) const
2434 { return this->find(c, pos); }
2436 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2437 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2438 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2440 //! <b>Throws</b>: Nothing
2442 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2443 size_type find_last_of(const basic_string& str, size_type pos = npos) const
2444 { return this->find_last_of(str.c_str(), pos, str.size()); }
2446 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2447 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2448 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2450 //! <b>Throws</b>: Nothing
2452 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2453 template<template <class, class> class BasicStringView>
2454 size_type find_last_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2455 { return this->find_last_of(sv.data(), pos, sv.size()); }
2457 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2459 //! <b>Throws</b>: Nothing
2461 //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
2462 size_type find_last_of(const CharT* s, size_type pos, size_type n) const
2464 const size_type len = this->size();
2469 const pointer addr = this->priv_addr();
2470 const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
2471 const const_reverse_iterator rresult =
2472 boost::container::find_first_of(const_reverse_iterator(last), rend(),
2473 s, s + n, Eq_traits<Traits>());
2474 return rresult != rend() ? (rresult.base() - 1) - addr : npos;
2478 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2480 //! <b>Throws</b>: Nothing
2482 //! <b>Returns</b>: find_last_of(basic_string<CharT,traits,Allocator>(1,c),pos).
2483 size_type find_last_of(const CharT* s, size_type pos = npos) const
2484 { return find_last_of(s, pos, Traits::length(s)); }
2486 //! <b>Throws</b>: Nothing
2488 //! <b>Returns</b>: find_last_of(basic_string(s), pos).
2489 size_type find_last_of(CharT c, size_type pos = npos) const
2490 { return rfind(c, pos); }
2492 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2493 //! both of the following conditions obtain:
2494 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no
2495 //! element I of the string controlled by str.
2497 //! <b>Throws</b>: Nothing
2499 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2500 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
2501 { return find_first_not_of(str.c_str(), pos, str.size()); }
2503 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2504 //! both of the following conditions obtain:
2505 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), sv.at(I)) for no
2506 //! element I of the string controlled by sv.
2508 //! <b>Throws</b>: Nothing
2510 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2511 template<template <class, class> class BasicStringView>
2512 size_type find_first_not_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2513 { return find_first_not_of(sv.data(), pos, sv.size()); }
2515 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2517 //! <b>Throws</b>: Nothing
2519 //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
2520 size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
2522 if (pos > this->size())
2525 const pointer addr = this->priv_addr();
2526 const pointer finish = addr + this->priv_size();
2527 const const_iterator result = boost::container::find_if
2528 (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
2529 return result != finish ? result - addr : npos;
2533 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2535 //! <b>Throws</b>: Nothing
2537 //! <b>Returns</b>: find_first_not_of(basic_string(s), pos).
2538 size_type find_first_not_of(const CharT* s, size_type pos = 0) const
2539 { return find_first_not_of(s, pos, Traits::length(s)); }
2541 //! <b>Throws</b>: Nothing
2543 //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
2544 size_type find_first_not_of(CharT c, size_type pos = 0) const
2546 if (pos > this->size())
2549 const pointer addr = this->priv_addr();
2550 const pointer finish = addr + this->priv_size();
2551 const const_iterator result
2552 = boost::container::find_if(addr + pos, finish,
2553 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2554 return result != finish ? result - begin() : npos;
2558 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2559 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2560 //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.
2562 //! <b>Throws</b>: Nothing
2564 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2565 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
2566 { return find_last_not_of(str.c_str(), pos, str.size()); }
2568 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2569 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2570 //! b) traits::eq(at(xpos), sv.at(I)) for no element I of the string controlled by sv.
2572 //! <b>Throws</b>: Nothing
2574 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2575 template<template <class, class> class BasicStringView>
2576 size_type find_last_not_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2577 { return find_last_not_of(sv.data(), pos, sv.size()); }
2579 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2581 //! <b>Throws</b>: Nothing
2583 //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
2584 size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
2586 const size_type len = this->size();
2591 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2592 const const_reverse_iterator rresult =
2593 boost::container::find_if(const_reverse_iterator(last), rend(),
2594 Not_within_traits<Traits>(s, s + n));
2595 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2599 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2601 //! <b>Throws</b>: Nothing
2603 //! <b>Returns</b>: find_last_not_of(basic_string(s), pos).
2604 size_type find_last_not_of(const CharT* s, size_type pos = npos) const
2605 { return find_last_not_of(s, pos, Traits::length(s)); }
2607 //! <b>Throws</b>: Nothing
2609 //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
2610 size_type find_last_not_of(CharT c, size_type pos = npos) const
2612 const size_type len = this->size();
2617 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2618 const const_reverse_iterator rresult =
2619 boost::container::find_if(const_reverse_iterator(last), rend(),
2620 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2621 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2625 //! <b>Requires</b>: Requires: pos <= size()
2627 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2628 //! the smaller of n and size() - pos.
2630 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
2632 //! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
2633 basic_string substr(size_type pos = 0, size_type n = npos) const
2635 if (pos > this->size())
2636 throw_out_of_range("basic_string::substr out of range position");
2637 const pointer addr = this->priv_addr();
2638 return basic_string(addr + pos,
2639 addr + pos + container_detail::min_value(n, size() - pos), this->alloc());
2642 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2643 //! the smaller of size() and str.size(). The function then compares the two strings by
2644 //! calling traits::compare(data(), str.data(), rlen).
2646 //! <b>Throws</b>: Nothing
2648 //! <b>Returns</b>: The nonzero result if the result of the comparison is nonzero.
2649 //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
2650 //! and value > 0 if size() > str.size()
2651 int compare(const basic_string& str) const
2653 const pointer addr = this->priv_addr();
2654 const pointer str_addr = str.priv_addr();
2655 return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
2658 //! <b>Throws</b>: Nothing
2660 //! <b>Returns</b>: compare(basic_string(sv)).
2661 template<template <class, class> class BasicStringView>
2662 int compare(BasicStringView<CharT,Traits> sv) const
2664 const pointer addr = this->priv_addr();
2665 return s_compare(addr, addr + this->priv_size(), sv.data(), sv.data() + sv.size());
2668 //! <b>Requires</b>: pos1 <= size()
2670 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2671 //! the smaller of (this->size() - pos1), n1 and str.size(). The function then compares the two strings by
2672 //! calling traits::compare(data()+pos1, str.data(), rlen).
2674 //! <b>Throws</b>: out_of_range if pos1 > size()
2676 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
2677 int compare(size_type pos1, size_type n1, const basic_string& str) const
2679 if (pos1 > this->size())
2680 throw_out_of_range("basic_string::compare out of range position");
2681 const pointer addr = this->priv_addr();
2682 const pointer str_addr = str.priv_addr();
2683 return s_compare(addr + pos1,
2684 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
2685 str_addr, str_addr + str.priv_size());
2688 //! <b>Requires</b>: pos1 <= size()
2690 //! <b>Throws</b>: out_of_range if pos1 > size()
2692 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(sv).
2693 template<template <class, class> class BasicStringView>
2694 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv) const
2696 if (pos1 > this->size())
2697 throw_out_of_range("basic_string::compare out of range position");
2698 const pointer addr = this->priv_addr() + pos1;
2699 const CharT* str_addr = sv.data();
2700 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2701 str_addr, str_addr + sv.size());
2704 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2706 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2709 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
2711 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
2712 int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const
2714 if (pos1 > this->size() || pos2 > str.size())
2715 throw_out_of_range("basic_string::compare out of range position");
2716 const pointer addr = this->priv_addr() + pos1;
2717 const pointer str_addr = str.priv_addr() + pos2;
2718 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2719 str_addr, str_addr + container_detail::min_value(n2, str.size() - pos2));
2722 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2724 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2727 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size()
2729 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(BasicStringView<CharT, Traits>(sv, pos2, n2)).
2730 template<template <class, class> class BasicStringView>
2731 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv, size_type pos2, size_type n2) const
2733 if (pos1 > this->size() || pos2 > sv.size())
2734 throw_out_of_range("basic_string::compare out of range position");
2735 const pointer addr = this->priv_addr() + pos1;
2736 const CharT * str_addr = sv.data() + pos2;
2737 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2738 str_addr, str_addr + container_detail::min_value(n2, sv.size() - pos2));
2741 //! <b>Throws</b>: Nothing
2743 //! <b>Returns</b>: compare(basic_string(s)).
2744 int compare(const CharT* s) const
2746 const pointer addr = this->priv_addr();
2747 return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
2750 //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
2752 //! <b>Throws</b>: out_of_range if pos1 > size()
2754 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2755 int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
2757 if (pos1 > this->size())
2758 throw_out_of_range("basic_string::compare out of range position");
2759 const pointer addr = this->priv_addr();
2760 return s_compare( addr + pos1,
2761 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
2765 //! <b>Requires</b>: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT.
2767 //! <b>Throws</b>: out_of_range if pos1 > size()
2769 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2770 int compare(size_type pos1, size_type n1, const CharT* s) const
2771 { return this->compare(pos1, n1, s, Traits::length(s)); }
2773 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2775 void priv_reserve(size_type res_arg, const bool null_terminate = true)
2777 if (res_arg > this->max_size()){
2778 throw_length_error("basic_string::reserve max_size() exceeded");
2781 if (this->capacity() < res_arg){
2782 size_type n = container_detail::max_value(res_arg, this->size()) + 1;
2783 size_type new_cap = this->next_capacity(n);
2785 pointer new_start = this->allocation_command(allocate_new, n, new_cap, reuse);
2786 size_type new_length = 0;
2788 const pointer addr = this->priv_addr();
2789 new_length += priv_uninitialized_copy
2790 (addr, addr + this->priv_size(), new_start);
2792 this->priv_construct_null(new_start + new_length);
2794 this->deallocate_block();
2795 this->is_short(false);
2796 this->priv_long_addr(new_start);
2797 this->priv_long_size(new_length);
2798 this->priv_storage(new_cap);
2802 template<class It1, class It2>
2803 static int s_compare(It1 f1, It1 l1, It2 f2, It2 l2)
2805 const difference_type n1 = l1 - f1;
2806 const difference_type n2 = l2 - f2;
2807 const int cmp = Traits::compare(boost::movelib::to_raw_pointer(f1),
2808 boost::movelib::to_raw_pointer(f2),
2809 container_detail::min_value(n1, n2));
2810 return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0));
2813 template<class AllocVersion>
2814 void priv_shrink_to_fit_dynamic_buffer
2816 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, version_1> >::type* = 0)
2818 //Allocate a new buffer.
2819 size_type real_cap = 0;
2820 const pointer long_addr = this->priv_long_addr();
2821 const size_type long_size = this->priv_long_size();
2822 const size_type long_storage = this->priv_long_storage();
2823 //We can make this nothrow as chars are always NoThrowCopyables
2826 real_cap = long_size+1;
2827 const pointer ret = this->allocation_command(allocate_new, long_size+1, real_cap, reuse);
2829 Traits::copy( boost::movelib::to_raw_pointer(ret)
2830 , boost::movelib::to_raw_pointer(this->priv_long_addr())
2832 this->priv_long_addr(ret);
2833 this->priv_storage(real_cap);
2834 //And release old buffer
2835 this->alloc().deallocate(long_addr, long_storage);
2843 template<class AllocVersion>
2844 void priv_shrink_to_fit_dynamic_buffer
2846 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, version_2> >::type* = 0)
2848 size_type received_size = this->priv_long_size()+1;
2849 pointer hint = this->priv_long_addr();
2850 if(this->alloc().allocation_command
2851 ( shrink_in_place | nothrow_allocation, this->priv_long_storage(), received_size, hint)){
2852 this->priv_storage(received_size);
2856 void priv_construct_null(pointer p)
2857 { this->construct(p, CharT(0)); }
2859 // Helper functions used by constructors. It is a severe error for
2860 // any of them to be called anywhere except from within constructors.
2861 void priv_terminate_string()
2862 { this->priv_construct_null(this->priv_end_addr()); }
2864 template<class FwdIt, class Count> inline
2865 void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
2867 //Save initial position
2872 for (; count--; ++first){
2873 this->construct(first, val);
2878 for (; init != first; ++init){
2879 this->destroy(init);
2886 template<class InpIt, class FwdIt> inline
2887 size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest)
2889 //Save initial destination position
2890 FwdIt dest_init = dest;
2891 size_type constructed = 0;
2894 //Try to build objects
2895 for (; first != last; ++dest, ++first, ++constructed){
2896 this->construct(dest, *first);
2901 for (; constructed--; ++dest_init){
2902 this->destroy(dest_init);
2907 return (constructed);
2910 template <class InputIterator, class OutIterator>
2911 void priv_copy(InputIterator first, InputIterator last, OutIterator result)
2913 for ( ; first != last; ++first, ++result)
2914 Traits::assign(*result, *first);
2917 void priv_copy(const CharT* first, const CharT* last, CharT* result)
2918 { Traits::copy(result, first, last - first); }
2920 template <class Integer>
2921 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2922 Integer n, Integer x,
2923 container_detail::true_)
2924 { return this->replace(first, last, (size_type) n, (CharT) x); }
2926 template <class InputIter>
2927 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2928 InputIter f, InputIter l,
2929 container_detail::false_)
2931 typedef typename boost::container::iterator_traits<InputIter>::iterator_category Category;
2932 return this->priv_replace(first, last, f, l, Category());
2935 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2938 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
2940 //!Typedef for a basic_string of
2941 //!narrow characters
2942 typedef basic_string
2944 ,std::char_traits<char>
2945 ,new_allocator<char> >
2948 //!Typedef for a basic_string of
2949 //!narrow characters
2950 typedef basic_string
2952 ,std::char_traits<wchar_t>
2953 ,new_allocator<wchar_t> >
2958 template <class CharT, class Traits, class Allocator>
2959 const typename basic_string<CharT,Traits,Allocator>::size_type
2960 basic_string<CharT,Traits,Allocator>::npos;
2965 static const bool value = false;
2968 template<class C, class T, class A>
2969 struct is_string< basic_string<C, T, A> >
2971 static const bool value = true;
2976 // ------------------------------------------------------------
2977 // Non-member functions.
2981 template <class CharT, class Traits, class Allocator> inline
2982 basic_string<CharT,Traits,Allocator>
2983 operator+(const basic_string<CharT,Traits,Allocator>& x
2984 ,const basic_string<CharT,Traits,Allocator>& y)
2986 typedef basic_string<CharT,Traits,Allocator> str_t;
2987 typedef typename str_t::reserve_t reserve_t;
2989 str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
2995 template <class CharT, class Traits, class Allocator> inline
2996 basic_string<CharT, Traits, Allocator> operator+
2997 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
2998 , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
3001 return boost::move(x);
3004 template <class CharT, class Traits, class Allocator> inline
3005 basic_string<CharT, Traits, Allocator> operator+
3006 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
3007 , const basic_string<CharT,Traits,Allocator>& y)
3010 return boost::move(x);
3013 template <class CharT, class Traits, class Allocator> inline
3014 basic_string<CharT, Traits, Allocator> operator+
3015 (const basic_string<CharT,Traits,Allocator>& x
3016 ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
3018 y.insert(y.begin(), x.begin(), x.end());
3019 return boost::move(y);
3022 template <class CharT, class Traits, class Allocator> inline
3023 basic_string<CharT, Traits, Allocator> operator+
3024 (const CharT* s, basic_string<CharT, Traits, Allocator> y)
3026 y.insert(y.begin(), s, s + Traits::length(s));
3030 template <class CharT, class Traits, class Allocator> inline
3031 basic_string<CharT,Traits,Allocator> operator+
3032 (basic_string<CharT,Traits,Allocator> x, const CharT* s)
3038 template <class CharT, class Traits, class Allocator> inline
3039 basic_string<CharT,Traits,Allocator> operator+
3040 (CharT c, basic_string<CharT,Traits,Allocator> y)
3042 y.insert(y.begin(), c);
3046 template <class CharT, class Traits, class Allocator> inline
3047 basic_string<CharT,Traits,Allocator> operator+
3048 (basic_string<CharT,Traits,Allocator> x, const CharT c)
3054 // Operator== and operator!=
3056 template <class CharT, class Traits, class Allocator>
3058 operator==(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3060 return x.size() == y.size() &&
3061 Traits::compare(x.data(), y.data(), x.size()) == 0;
3064 template <class CharT, class Traits, class Allocator>
3066 operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3068 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3069 return n == y.size() && Traits::compare(s, y.data(), n) == 0;
3072 template <class CharT, class Traits, class Allocator>
3074 operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3076 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3077 return x.size() == n && Traits::compare(x.data(), s, n) == 0;
3080 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3082 BOOST_CONTAINER_DOC1ST( bool,
3083 typename container_detail::disable_if
3084 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3085 operator==( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3087 return x.size() == y.size() &&
3088 Traits::compare(x.data(), y.data(), x.size()) == 0;
3091 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3093 BOOST_CONTAINER_DOC1ST( bool,
3094 typename container_detail::disable_if
3095 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3096 operator==( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3098 return x.size() == y.size() &&
3099 Traits::compare(x.data(), y.data(), x.size()) == 0;
3102 template <class CharT, class Traits, class Allocator>
3104 operator!=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3105 { return !(x == y); }
3107 template <class CharT, class Traits, class Allocator>
3109 operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3110 { return !(s == y); }
3112 template <class CharT, class Traits, class Allocator>
3114 operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3115 { return !(x == s); }
3118 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3120 BOOST_CONTAINER_DOC1ST( bool,
3121 typename container_detail::disable_if
3122 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3123 operator!=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3124 { return !(x == y); }
3126 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3128 BOOST_CONTAINER_DOC1ST( bool,
3129 typename container_detail::disable_if
3130 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3131 operator!=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3132 { return !(x == y); }
3134 // Operator< (and also >, <=, and >=).
3135 template <class CharT, class Traits, class Allocator>
3137 operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3139 return x.compare(y) < 0;
3142 template <class CharT, class Traits, class Allocator>
3144 operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3146 return y.compare(s) > 0;
3149 template <class CharT, class Traits, class Allocator>
3151 operator<(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3153 return x.compare(s) < 0;
3156 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3158 BOOST_CONTAINER_DOC1ST( bool,
3159 typename container_detail::disable_if
3160 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3161 operator<( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3162 { return y.compare(x) > 0; }
3164 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3166 BOOST_CONTAINER_DOC1ST( bool,
3167 typename container_detail::disable_if
3168 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3169 operator<( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3170 { return x.compare(y) < 0; }
3172 template <class CharT, class Traits, class Allocator>
3174 operator>(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y) {
3178 template <class CharT, class Traits, class Allocator>
3180 operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
3184 template <class CharT, class Traits, class Allocator>
3186 operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3191 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3193 BOOST_CONTAINER_DOC1ST( bool,
3194 typename container_detail::disable_if
3195 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3196 operator>( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3199 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3201 BOOST_CONTAINER_DOC1ST( bool,
3202 typename container_detail::disable_if
3203 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3204 operator>( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3207 template <class CharT, class Traits, class Allocator>
3209 operator<=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3214 template <class CharT, class Traits, class Allocator>
3216 operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3217 { return !(y < s); }
3219 template <class CharT, class Traits, class Allocator>
3221 operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3222 { return !(s < x); }
3225 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3227 BOOST_CONTAINER_DOC1ST( bool,
3228 typename container_detail::disable_if
3229 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3230 operator<=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3231 { return !(y < x); }
3233 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3235 BOOST_CONTAINER_DOC1ST( bool,
3236 typename container_detail::disable_if
3237 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3238 operator<=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3239 { return !(y < x); }
3241 template <class CharT, class Traits, class Allocator>
3243 operator>=(const basic_string<CharT,Traits,Allocator>& x,
3244 const basic_string<CharT,Traits,Allocator>& y)
3245 { return !(x < y); }
3247 template <class CharT, class Traits, class Allocator>
3249 operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3250 { return !(s < y); }
3252 template <class CharT, class Traits, class Allocator>
3254 operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3255 { return !(x < s); }
3257 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3259 BOOST_CONTAINER_DOC1ST( bool,
3260 typename container_detail::disable_if
3261 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3262 operator>=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3263 { return !(x < y); }
3265 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3267 BOOST_CONTAINER_DOC1ST( bool,
3268 typename container_detail::disable_if
3269 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3270 operator>=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3271 { return !(x < y); }
3274 template <class CharT, class Traits, class Allocator>
3275 inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
3278 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3280 namespace container_detail {
3282 template <class CharT, class Traits>
3284 string_fill(std::basic_ostream<CharT, Traits>& os,
3285 std::basic_streambuf<CharT, Traits>* buf,
3288 CharT f = os.fill();
3292 for (i = 0; i < n; i++)
3293 ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof());
3297 } //namespace container_detail {
3298 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3300 template <class CharT, class Traits, class Allocator>
3301 std::basic_ostream<CharT, Traits>&
3302 operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
3304 typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
3309 typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
3310 typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
3311 const bool left = (os.flags() & std::ios::left) != 0;
3312 const std::size_t w = os.width(0);
3313 std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
3315 if (w != 0 && n < w)
3319 ok = container_detail::string_fill(os, buf, pad_len);
3322 buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
3325 ok = ok && container_detail::string_fill(os, buf, pad_len);
3329 os.setstate(std::ios_base::failbit);
3335 template <class CharT, class Traits, class Allocator>
3336 std::basic_istream<CharT, Traits>&
3337 operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3339 typename std::basic_istream<CharT, Traits>::sentry sentry(is);
3342 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3343 const std::ctype<CharT>& ctype = std::use_facet<std::ctype<CharT> >(is.getloc());
3346 std::size_t n = is.width(0);
3348 n = static_cast<std::size_t>(-1);
3353 typename Traits::int_type c1 = buf->sbumpc();
3355 if (Traits::eq_int_type(c1, Traits::eof())) {
3356 is.setstate(std::ios_base::eofbit);
3360 CharT c = Traits::to_char_type(c1);
3362 if (ctype.is(std::ctype<CharT>::space, c)) {
3363 if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof()))
3364 is.setstate(std::ios_base::failbit);
3372 // If we have read no characters, then set failbit.
3374 is.setstate(std::ios_base::failbit);
3377 is.setstate(std::ios_base::failbit);
3382 template <class CharT, class Traits, class Allocator>
3383 std::basic_istream<CharT, Traits>&
3384 getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
3386 typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
3387 typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
3389 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3392 while (nread < s.max_size()) {
3393 int c1 = buf->sbumpc();
3394 if (Traits::eq_int_type(c1, Traits::eof())) {
3395 is.setstate(std::ios_base::eofbit);
3400 CharT c = Traits::to_char_type(c1);
3401 if (!Traits::eq(c, delim))
3404 break; // Character is extracted but not appended.
3408 if (nread == 0 || nread >= s.max_size())
3409 is.setstate(std::ios_base::failbit);
3414 template <class CharT, class Traits, class Allocator>
3415 inline std::basic_istream<CharT, Traits>&
3416 getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3418 return getline(is, s, '\n');
3421 template <class Ch, class Allocator>
3422 inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, Allocator> const& v)
3424 return hash_range(v.begin(), v.end());
3429 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3433 //!has_trivial_destructor_after_move<> == true_type
3434 //!specialization for optimizations
3435 template <class C, class T, class Allocator>
3436 struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
3438 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3439 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3440 ::boost::has_trivial_destructor_after_move<pointer>::value;
3445 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3447 #include <boost/container/detail/config_end.hpp>
3449 #endif // BOOST_CONTAINER_STRING_HPP