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
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 dtl::aligned_storage
174 <sizeof(long_t), dtl::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.data)); }
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 dtl::integral_constant<unsigned,
278 boost::container::dtl::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 dtl::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 growth_factor_100()
296 ( this->priv_storage(), additional_objects, allocator_traits_type::max_size(this->alloc()));
299 void deallocate(pointer p, size_type n)
301 if (p && (n > InternalBufferChars))
302 this->alloc().deallocate(p, n);
305 void construct(pointer p, const value_type &value = value_type())
307 allocator_traits_type::construct
309 , boost::movelib::to_raw_pointer(p)
314 void destroy(pointer p, size_type n)
316 value_type *raw_p = boost::movelib::to_raw_pointer(p);
318 allocator_traits_type::destroy( this->alloc(), raw_p);
322 void destroy(pointer p)
324 allocator_traits_type::destroy
326 , boost::movelib::to_raw_pointer(p)
330 void allocate_initial_block(size_type n)
332 if (n <= this->max_size()) {
333 if(n > InternalBufferChars){
334 size_type new_cap = this->next_capacity(n);
336 pointer p = this->allocation_command(allocate_new, n, new_cap, reuse);
337 this->is_short(false);
338 this->priv_long_addr(p);
339 this->priv_long_size(0);
340 this->priv_storage(new_cap);
344 throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
348 void deallocate_block()
349 { this->deallocate(this->priv_addr(), this->priv_storage()); }
351 size_type max_size() const
352 { return allocator_traits_type::max_size(this->alloc()) - 1; }
355 size_type priv_capacity() const
356 { return this->priv_storage() - 1; }
358 pointer priv_short_addr() const
359 { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
361 pointer priv_long_addr() const
362 { return this->members_.m_repr.long_repr().start; }
364 pointer priv_addr() const
366 return this->is_short()
372 pointer priv_end_addr() const
374 return this->is_short()
375 ? this->priv_short_addr() + this->priv_short_size()
376 : this->priv_long_addr() + this->priv_long_size()
380 void priv_long_addr(pointer addr)
381 { this->members_.m_repr.long_repr().start = addr; }
383 size_type priv_storage() const
384 { return this->is_short() ? priv_short_storage() : priv_long_storage(); }
386 size_type priv_short_storage() const
387 { return InternalBufferChars; }
389 size_type priv_long_storage() const
390 { return this->members_.m_repr.long_repr().storage; }
392 void priv_storage(size_type storage)
394 if(!this->is_short())
395 this->priv_long_storage(storage);
398 void priv_long_storage(size_type storage)
400 this->members_.m_repr.long_repr().storage = storage;
403 size_type priv_size() const
404 { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
406 size_type priv_short_size() const
407 { return this->members_.m_repr.short_repr().h.length; }
409 size_type priv_long_size() const
410 { return this->members_.m_repr.long_repr().length; }
412 void priv_size(size_type sz)
415 this->priv_short_size(sz);
417 this->priv_long_size(sz);
420 void priv_short_size(size_type sz)
422 this->members_.m_repr.s.h.length = (unsigned char)sz;
425 void priv_long_size(size_type sz)
427 this->members_.m_repr.long_repr().length = sz;
430 void swap_data(basic_string_base& other)
432 if(this->is_short()){
433 if(other.is_short()){
434 repr_t tmp(this->members_.m_repr);
435 this->members_.m_repr = other.members_.m_repr;
436 other.members_.m_repr = tmp;
439 short_t short_backup(this->members_.m_repr.short_repr());
440 this->members_.m_repr.short_repr().~short_t();
441 ::new(&this->members_.m_repr.long_repr()) long_t(other.members_.m_repr.long_repr());
442 other.members_.m_repr.long_repr().~long_t();
443 ::new(&other.members_.m_repr.short_repr()) short_t(short_backup);
447 if(other.is_short()){
448 short_t short_backup(other.members_.m_repr.short_repr());
449 other.members_.m_repr.short_repr().~short_t();
450 ::new(&other.members_.m_repr.long_repr()) long_t(this->members_.m_repr.long_repr());
451 this->members_.m_repr.long_repr().~long_t();
452 ::new(&this->members_.m_repr.short_repr()) short_t(short_backup);
455 boost::adl_move_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
463 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
465 //! The basic_string class represents a Sequence of characters. It contains all the
466 //! usual operations of a Sequence, and, additionally, it contains standard string
467 //! operations such as search and concatenation.
469 //! The basic_string class is parameterized by character type, and by that type's
470 //! Character Traits.
472 //! This class has performance characteristics very much like vector<>, meaning,
473 //! for example, that it does not perform reference-count or copy-on-write, and that
474 //! concatenation of two strings is an O(N) operation.
476 //! Some of basic_string's member functions use an unusual method of specifying positions
477 //! and ranges. In addition to the conventional method using iterators, many of
478 //! basic_string's member functions use a single value pos of type size_type to represent a
479 //! position (in which case the position is begin() + pos, and many of basic_string's
480 //! member functions use two values, pos and n, to represent a range. In that case pos is
481 //! the beginning of the range and n is its size. That is, the range is
482 //! [begin() + pos, begin() + pos + n).
484 //! Note that the C++ standard does not specify the complexity of basic_string operations.
485 //! In this implementation, basic_string has performance characteristics very similar to
486 //! those of vector: access to a single character is O(1), while copy and concatenation
489 //! In this implementation, begin(),
490 //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
491 //! In this implementation, iterators are only invalidated by member functions that
492 //! explicitly change the string's contents.
494 //! \tparam CharT The type of character it contains.
495 //! \tparam Traits The Character Traits type, which encapsulates basic character operations
496 //! \tparam Allocator The allocator, used for internal memory management.
497 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
498 template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = new_allocator<CharT> >
500 template <class CharT, class Traits, class Allocator>
503 : private dtl::basic_string_base<Allocator>
505 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
507 typedef allocator_traits<Allocator> allocator_traits_type;
508 BOOST_COPYABLE_AND_MOVABLE(basic_string)
509 typedef dtl::basic_string_base<Allocator> base_t;
510 static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
513 // Allocator helper class to use a char_traits as a function object.
518 //Compatibility with std::binary_function
519 typedef typename Tr::char_type first_argument_type;
520 typedef typename Tr::char_type second_argument_type;
521 typedef bool result_type;
523 bool operator()(const first_argument_type& x, const second_argument_type& y) const
524 { return Tr::eq(x, y); }
528 struct Not_within_traits
530 typedef typename Tr::char_type argument_type;
531 typedef bool result_type;
533 typedef const typename Tr::char_type* Pointer;
534 const Pointer m_first;
535 const Pointer m_last;
537 Not_within_traits(Pointer f, Pointer l)
538 : m_first(f), m_last(l) {}
540 bool operator()(const typename Tr::char_type& x) const
542 return boost::container::find_if(m_first, m_last,
543 boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
546 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
549 //////////////////////////////////////////////
553 //////////////////////////////////////////////
554 typedef Traits traits_type;
555 typedef CharT value_type;
556 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
557 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
558 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
559 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
560 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
561 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
562 typedef Allocator allocator_type;
563 typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
564 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
565 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
566 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
567 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
568 static const size_type npos = size_type(-1);
570 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
572 typedef constant_iterator<CharT, difference_type> cvalue_iterator;
573 typedef typename base_t::alloc_version alloc_version;
574 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
575 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
577 public: // Constructor, destructor, assignment.
578 //////////////////////////////////////////////
580 // construct/copy/destroy
582 //////////////////////////////////////////////
583 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
586 basic_string(reserve_t, size_type n,
587 const allocator_type& a = allocator_type())
588 //Select allocator as in copy constructor as reserve_t-based constructors
589 //are two step copies optimized for capacity
590 : base_t( allocator_traits_type::select_on_container_copy_construction(a)
592 { this->priv_terminate_string(); }
594 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
596 //! <b>Effects</b>: Default constructs a basic_string.
598 //! <b>Throws</b>: If allocator_type's default constructor throws.
599 basic_string() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
601 { this->priv_terminate_string(); }
604 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
606 //! <b>Throws</b>: Nothing
607 explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
609 { this->priv_terminate_string(); }
611 //! <b>Effects</b>: Copy constructs a basic_string.
613 //! <b>Postcondition</b>: x == *this.
615 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
616 basic_string(const basic_string& s)
617 : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
619 this->priv_terminate_string();
620 this->assign(s.begin(), s.end());
623 //! <b>Effects</b>: Same as basic_string(sv.data(), sv.size(), a).
625 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
626 template<template <class, class> class BasicStringView>
627 explicit basic_string(BasicStringView<CharT, Traits> sv, const Allocator& a = Allocator())
628 : base_t(allocator_traits_type::select_on_container_copy_construction(a))
630 this->priv_terminate_string();
634 //! <b>Effects</b>: Move constructor. Moves s's resources to *this.
636 //! <b>Throws</b>: Nothing.
638 //! <b>Complexity</b>: Constant.
639 basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW
640 : base_t(boost::move(s.alloc()))
642 if(s.alloc() == this->alloc()){
646 this->assign(s.begin(), s.end());
650 //! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
652 //! <b>Postcondition</b>: x == *this.
654 //! <b>Throws</b>: If allocation throws.
655 basic_string(const basic_string& s, const allocator_type &a)
658 this->priv_terminate_string();
659 this->assign(s.begin(), s.end());
662 //! <b>Effects</b>: Move constructor using the specified allocator.
663 //! Moves s's resources to *this.
665 //! <b>Throws</b>: If allocation throws.
667 //! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
668 basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
671 this->priv_terminate_string();
672 if(a == this->alloc()){
676 this->assign(s.begin(), s.end());
680 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
681 //! and is initialized by a specific number of characters of the s string.
682 basic_string(const basic_string& s, size_type pos, size_type n = npos)
685 this->priv_terminate_string();
687 throw_out_of_range("basic_string::basic_string out of range position");
690 (s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
693 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
694 //! and is initialized by a specific number of characters of the s string.
695 basic_string(const basic_string& s, size_type pos, size_type n, const allocator_type& a)
698 this->priv_terminate_string();
700 throw_out_of_range("basic_string::basic_string out of range position");
703 (s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
706 //! <b>Effects</b>: Constructs a basic_string taking a default-constructed allocator,
707 //! and is initialized by a specific number of characters of the s c-string.
708 basic_string(const CharT* s, size_type n)
711 this->priv_terminate_string();
712 this->assign(s, s + n);
715 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
716 //! and is initialized by a specific number of characters of the s c-string.
717 basic_string(const CharT* s, size_type n, const allocator_type& a)
720 this->priv_terminate_string();
721 this->assign(s, s + n);
724 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
725 //! and is initialized by the null-terminated s c-string.
726 basic_string(const CharT* s)
729 this->priv_terminate_string();
730 this->assign(s, s + Traits::length(s));
733 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
734 //! and is initialized by the null-terminated s c-string.
735 basic_string(const CharT* s, const allocator_type& a)
738 this->priv_terminate_string();
739 this->assign(s, s + Traits::length(s));
743 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
744 //! and is initialized by n copies of c.
745 basic_string(size_type n, CharT c)
748 this->priv_terminate_string();
752 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
753 //! and is initialized by n copies of c.
754 basic_string(size_type n, CharT c, const allocator_type& a)
757 this->priv_terminate_string();
761 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
762 //! and is initialized by n default-initialized characters.
763 basic_string(size_type n, default_init_t)
767 this->priv_terminate_string();
770 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
771 //! and is initialized by n default-initialized characters.
772 basic_string(size_type n, default_init_t, const allocator_type& a)
776 this->priv_terminate_string();
779 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
780 //! and a range of iterators.
781 template <class InputIterator>
782 basic_string(InputIterator f, InputIterator l)
785 this->priv_terminate_string();
789 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
790 //! and a range of iterators.
791 template <class InputIterator>
792 basic_string(InputIterator f, InputIterator l, const allocator_type& a)
795 this->priv_terminate_string();
799 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
800 //! <b>Effects</b>: Same as basic_string(il.begin(), il.end(), a).
802 basic_string(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
805 this->priv_terminate_string();
806 this->assign(il.begin(), il.end());
810 //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
812 //! <b>Throws</b>: Nothing.
814 //! <b>Complexity</b>: Constant.
815 ~basic_string() BOOST_NOEXCEPT_OR_NOTHROW
818 //! <b>Effects</b>: Copy constructs a string.
820 //! <b>Postcondition</b>: x == *this.
822 //! <b>Complexity</b>: Linear to the elements x contains.
823 basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
826 allocator_type &this_alloc = this->alloc();
827 const allocator_type &x_alloc = x.alloc();
828 dtl::bool_<allocator_traits_type::
829 propagate_on_container_copy_assignment::value> flag;
830 if(flag && this_alloc != x_alloc){
831 if(!this->is_short()){
832 this->deallocate_block();
833 this->is_short(true);
834 Traits::assign(*this->priv_addr(), CharT(0));
835 this->priv_short_size(0);
838 dtl::assign_alloc(this->alloc(), x.alloc(), flag);
839 this->assign(x.begin(), x.end());
844 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
846 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
847 //! is false and allocation throws
849 //! <b>Complexity</b>: Constant if allocator_traits_type::
850 //! propagate_on_container_move_assignment is true or
851 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
852 basic_string& operator=(BOOST_RV_REF(basic_string) x)
853 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
854 || allocator_traits_type::is_always_equal::value)
856 //for move constructor, no aliasing (&x != this) is assummed.
857 BOOST_ASSERT(this != &x);
858 allocator_type &this_alloc = this->alloc();
859 allocator_type &x_alloc = x.alloc();
860 const bool propagate_alloc = allocator_traits_type::
861 propagate_on_container_move_assignment::value;
862 dtl::bool_<propagate_alloc> flag;
863 const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
864 //Resources can be transferred if both allocators are
865 //going to be equal after this function (either propagated or already equal)
866 if(propagate_alloc || allocators_equal){
867 //Destroy objects but retain memory in case x reuses it in the future
869 //Move allocator if needed
870 dtl::move_alloc(this_alloc, x_alloc, flag);
874 //Else do a one by one move
876 this->assign( x.begin(), x.end());
881 //! <b>Effects</b>: Assignment from a null-terminated c-string.
883 basic_string& operator=(const CharT* s)
884 { return this->assign(s, s + Traits::length(s)); }
886 //! <b>Effects</b>: Returns *this = basic_string(1, c).
888 basic_string& operator=(CharT c)
889 { return this->assign(static_cast<size_type>(1), c); }
891 //! <b>Effects</b>: Equivalent to return assign(sv).
893 template<template <class, class> class BasicStringView>
894 basic_string& operator=(BasicStringView<CharT, Traits> sv)
895 { return this->assign(sv.data(), sv.size()); }
897 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
898 //! <b>Effects</b>: Returns *this = basic_string(il);
900 basic_string& operator=(std::initializer_list<CharT> il)
902 return this->assign(il.begin(), il.end());
906 //! <b>Effects</b>: Returns a copy of the internal allocator.
908 //! <b>Throws</b>: If allocator's copy constructor throws.
910 //! <b>Complexity</b>: Constant.
911 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
912 { return this->alloc(); }
914 //! <b>Effects</b>: Returns a reference to the internal allocator.
916 //! <b>Throws</b>: Nothing
918 //! <b>Complexity</b>: Constant.
920 //! <b>Note</b>: Non-standard extension.
921 stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
922 { return this->alloc(); }
924 //! <b>Effects</b>: Returns a reference to the internal allocator.
926 //! <b>Throws</b>: Nothing
928 //! <b>Complexity</b>: Constant.
930 //! <b>Note</b>: Non-standard extension.
931 const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
932 { return this->alloc(); }
934 //////////////////////////////////////////////
938 //////////////////////////////////////////////
940 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
942 //! <b>Throws</b>: Nothing.
944 //! <b>Complexity</b>: Constant.
945 iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
946 { return this->priv_addr(); }
948 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
950 //! <b>Throws</b>: Nothing.
952 //! <b>Complexity</b>: Constant.
953 const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
954 { return this->priv_addr(); }
956 //! <b>Effects</b>: Returns an iterator to the end of the vector.
958 //! <b>Throws</b>: Nothing.
960 //! <b>Complexity</b>: Constant.
961 iterator end() BOOST_NOEXCEPT_OR_NOTHROW
962 { return this->priv_end_addr(); }
964 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
966 //! <b>Throws</b>: Nothing.
968 //! <b>Complexity</b>: Constant.
969 const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
970 { return this->priv_end_addr(); }
972 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
973 //! of the reversed vector.
975 //! <b>Throws</b>: Nothing.
977 //! <b>Complexity</b>: Constant.
978 reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
979 { return reverse_iterator(this->priv_end_addr()); }
981 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
982 //! of the reversed vector.
984 //! <b>Throws</b>: Nothing.
986 //! <b>Complexity</b>: Constant.
987 const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
988 { return this->crbegin(); }
990 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
991 //! of the reversed vector.
993 //! <b>Throws</b>: Nothing.
995 //! <b>Complexity</b>: Constant.
996 reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
997 { return reverse_iterator(this->priv_addr()); }
999 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1000 //! of the reversed vector.
1002 //! <b>Throws</b>: Nothing.
1004 //! <b>Complexity</b>: Constant.
1005 const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1006 { return this->crend(); }
1008 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1010 //! <b>Throws</b>: Nothing.
1012 //! <b>Complexity</b>: Constant.
1013 const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1014 { return this->priv_addr(); }
1016 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1018 //! <b>Throws</b>: Nothing.
1020 //! <b>Complexity</b>: Constant.
1021 const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1022 { return this->priv_end_addr(); }
1024 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1025 //! of the reversed vector.
1027 //! <b>Throws</b>: Nothing.
1029 //! <b>Complexity</b>: Constant.
1030 const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1031 { return const_reverse_iterator(this->priv_end_addr()); }
1033 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1034 //! of the reversed vector.
1036 //! <b>Throws</b>: Nothing.
1038 //! <b>Complexity</b>: Constant.
1039 const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1040 { return const_reverse_iterator(this->priv_addr()); }
1042 //////////////////////////////////////////////
1046 //////////////////////////////////////////////
1048 //! <b>Effects</b>: Returns true if the vector contains no elements.
1050 //! <b>Throws</b>: Nothing.
1052 //! <b>Complexity</b>: Constant.
1053 bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1054 { return !this->priv_size(); }
1056 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1058 //! <b>Throws</b>: Nothing.
1060 //! <b>Complexity</b>: Constant.
1061 size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1062 { return this->priv_size(); }
1064 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1066 //! <b>Throws</b>: Nothing.
1068 //! <b>Complexity</b>: Constant.
1069 size_type length() const BOOST_NOEXCEPT_OR_NOTHROW
1070 { return this->size(); }
1072 //! <b>Effects</b>: Returns the largest possible size of the vector.
1074 //! <b>Throws</b>: Nothing.
1076 //! <b>Complexity</b>: Constant.
1077 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1078 { return base_t::max_size(); }
1080 //! <b>Effects</b>: Inserts or erases elements at the end such that
1081 //! the size becomes n. New elements are copy constructed from x.
1083 //! <b>Throws</b>: If memory allocation throws
1085 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1086 void resize(size_type n, CharT c)
1088 if (n <= this->size())
1089 this->erase(this->begin() + n, this->end());
1091 this->append(n - this->size(), c);
1094 //! <b>Effects</b>: Inserts or erases elements at the end such that
1095 //! the size becomes n. New elements are value initialized.
1097 //! <b>Throws</b>: If memory allocation throws
1099 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1100 void resize(size_type n)
1101 { resize(n, CharT()); }
1104 //! <b>Effects</b>: Inserts or erases elements at the end such that
1105 //! the size becomes n. New elements are uninitialized.
1107 //! <b>Throws</b>: If memory allocation throws
1109 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1111 //! <b>Note</b>: Non-standard extension
1112 void resize(size_type n, default_init_t)
1114 if (n <= this->size())
1115 this->erase(this->begin() + n, this->end());
1117 this->priv_reserve(n, false);
1119 this->priv_terminate_string();
1123 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1124 //! capacity() is always greater than or equal to size().
1126 //! <b>Throws</b>: Nothing.
1128 //! <b>Complexity</b>: Constant.
1129 size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1130 { return this->priv_capacity(); }
1132 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1133 //! effect. Otherwise, it is a request for allocation of additional memory.
1134 //! If the request is successful, then capacity() is greater than or equal to
1135 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1137 //! <b>Throws</b>: If memory allocation allocation throws
1138 void reserve(size_type res_arg)
1139 { this->priv_reserve(res_arg); }
1141 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1142 //! with previous allocations. The size of the string is unchanged
1144 //! <b>Throws</b>: Nothing
1146 //! <b>Complexity</b>: Linear to size().
1147 void shrink_to_fit()
1149 //Check if shrinking is possible
1150 if(this->priv_storage() > InternalBufferChars){
1151 //Check if we should pass from dynamically allocated buffer
1152 //to the internal storage
1153 if(this->priv_size() < InternalBufferChars){
1154 //Dynamically allocated buffer attributes
1155 pointer long_addr = this->priv_long_addr();
1156 size_type long_storage = this->priv_long_storage();
1157 size_type long_size = this->priv_long_size();
1158 //Shrink from allocated buffer to the internal one, including trailing null
1159 Traits::copy( boost::movelib::to_raw_pointer(this->priv_short_addr())
1160 , boost::movelib::to_raw_pointer(long_addr)
1162 this->is_short(true);
1163 this->alloc().deallocate(long_addr, long_storage);
1166 //Shrinking in dynamic buffer
1167 this->priv_shrink_to_fit_dynamic_buffer(alloc_version());
1172 //////////////////////////////////////////////
1176 //////////////////////////////////////////////
1178 //! <b>Requires</b>: !empty()
1180 //! <b>Effects</b>: Returns a reference to the first
1181 //! element of the container.
1183 //! <b>Throws</b>: Nothing.
1185 //! <b>Complexity</b>: Constant.
1186 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1188 BOOST_ASSERT(!this->empty());
1189 return *this->priv_addr();
1192 //! <b>Requires</b>: !empty()
1194 //! <b>Effects</b>: Returns a const reference to the first
1195 //! element of the container.
1197 //! <b>Throws</b>: Nothing.
1199 //! <b>Complexity</b>: Constant.
1200 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1202 BOOST_ASSERT(!this->empty());
1203 return *this->priv_addr();
1206 //! <b>Requires</b>: !empty()
1208 //! <b>Effects</b>: Returns a reference to the last
1209 //! element of the container.
1211 //! <b>Throws</b>: Nothing.
1213 //! <b>Complexity</b>: Constant.
1214 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1216 BOOST_ASSERT(!this->empty());
1217 return *(this->priv_addr() + (this->size() - 1u) );
1220 //! <b>Requires</b>: !empty()
1222 //! <b>Effects</b>: Returns a const reference to the last
1223 //! element of the container.
1225 //! <b>Throws</b>: Nothing.
1227 //! <b>Complexity</b>: Constant.
1228 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1230 BOOST_ASSERT(!this->empty());
1231 return *(this->priv_addr() + (this->size() - 1u) );
1234 //! <b>Requires</b>: size() > n.
1236 //! <b>Effects</b>: Returns a reference to the nth element
1237 //! from the beginning of the container.
1239 //! <b>Throws</b>: Nothing.
1241 //! <b>Complexity</b>: Constant.
1242 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1244 BOOST_ASSERT(this->size() > n);
1245 return *(this->priv_addr() + n);
1248 //! <b>Requires</b>: size() > n.
1250 //! <b>Effects</b>: Returns a const reference to the nth element
1251 //! from the beginning of the container.
1253 //! <b>Throws</b>: Nothing.
1255 //! <b>Complexity</b>: Constant.
1256 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1258 BOOST_ASSERT(this->size() > n);
1259 return *(this->priv_addr() + n);
1262 //! <b>Requires</b>: size() > n.
1264 //! <b>Effects</b>: Returns a reference to the nth element
1265 //! from the beginning of the container.
1267 //! <b>Throws</b>: std::range_error if n >= size()
1269 //! <b>Complexity</b>: Constant.
1270 reference at(size_type n)
1272 if (n >= this->size())
1273 throw_out_of_range("basic_string::at invalid subscript");
1274 return *(this->priv_addr() + n);
1277 //! <b>Requires</b>: size() > n.
1279 //! <b>Effects</b>: Returns a const reference to the nth element
1280 //! from the beginning of the container.
1282 //! <b>Throws</b>: std::range_error if n >= size()
1284 //! <b>Complexity</b>: Constant.
1285 const_reference at(size_type n) const {
1286 if (n >= this->size())
1287 throw_out_of_range("basic_string::at invalid subscript");
1288 return *(this->priv_addr() + n);
1291 //////////////////////////////////////////////
1295 //////////////////////////////////////////////
1297 //! <b>Effects</b>: Calls append(str.data, str.size()).
1299 //! <b>Returns</b>: *this
1300 basic_string& operator+=(const basic_string& s)
1301 { return this->append(s); }
1303 //! <b>Effects</b>: Same as `return append(sv)`.
1305 template<template<class, class> class BasicStringView>
1306 basic_string& operator+=(BasicStringView<CharT, Traits> sv)
1308 return this->append(sv);
1311 //! <b>Effects</b>: Calls append(s).
1313 //! <b>Returns</b>: *this
1314 basic_string& operator+=(const CharT* s)
1315 { return this->append(s); }
1317 //! <b>Effects</b>: Calls append(1, c).
1319 //! <b>Returns</b>: *this
1320 basic_string& operator+=(CharT c)
1321 { this->push_back(c); return *this; }
1323 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1324 //! <b>Effects</b>: Returns append(il)
1326 basic_string& operator+=(std::initializer_list<CharT> il)
1328 return this->append(il);
1332 //! <b>Effects</b>: Calls append(str.data(), str.size()).
1334 //! <b>Returns</b>: *this
1335 basic_string& append(const basic_string& s)
1336 { return this->append(s.begin(), s.end()); }
1338 //! <b>Effects</b>: Same as return append(sv.data(), sv.size()).
1340 template<template<class, class> class BasicStringView>
1341 basic_string& append(BasicStringView<CharT, Traits> sv)
1342 { return this->append(sv.data(), sv.size()); }
1344 //! <b>Requires</b>: pos <= str.size()
1346 //! <b>Effects</b>: Determines the effective length rlen of the string to append
1347 //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
1349 //! <b>Throws</b>: If memory allocation throws and out_of_range if pos > str.size()
1351 //! <b>Returns</b>: *this
1352 basic_string& append(const basic_string& s, size_type pos, size_type n = npos)
1355 throw_out_of_range("basic_string::append out of range position");
1356 return this->append(s.begin() + pos,
1357 s.begin() + pos + dtl::min_value(n, s.size() - pos));
1360 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1362 //! <b>Effects</b>: The function replaces the string controlled by *this with
1363 //! a string of length size() + n whose irst size() elements are a copy of the
1364 //! original string controlled by *this and whose remaining
1365 //! elements are a copy of the initial n elements of s.
1367 //! <b>Throws</b>: If memory allocation throws length_error if size() + n > max_size().
1369 //! <b>Returns</b>: *this
1370 basic_string& append(const CharT* s, size_type n)
1371 { return this->append(s, s + n); }
1373 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1375 //! <b>Effects</b>: Calls append(s, traits::length(s)).
1377 //! <b>Returns</b>: *this
1378 basic_string& append(const CharT* s)
1379 { return this->append(s, s + Traits::length(s)); }
1381 //! <b>Effects</b>: Equivalent to append(basic_string(n, c)).
1383 //! <b>Returns</b>: *this
1384 basic_string& append(size_type n, CharT c)
1385 { return this->append(cvalue_iterator(c, n), cvalue_iterator()); }
1387 //! <b>Requires</b>: [first,last) is a valid range.
1389 //! <b>Effects</b>: Equivalent to append(basic_string(first, last)).
1391 //! <b>Returns</b>: *this
1392 template <class InputIter>
1393 basic_string& append(InputIter first, InputIter last)
1394 { this->insert(this->end(), first, last); return *this; }
1396 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1397 //! <b>Effects</b>: Returns append(il.begin(), il.size()).
1399 basic_string& append(std::initializer_list<CharT> il)
1401 return this->append(il.begin(), il.size());
1405 //! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
1407 void push_back(CharT c)
1409 const size_type old_size = this->priv_size();
1410 if (old_size < this->capacity()){
1411 const pointer addr = this->priv_addr();
1412 this->priv_construct_null(addr + old_size + 1);
1413 Traits::assign(addr[old_size], c);
1414 this->priv_size(old_size+1);
1417 //No enough memory, insert a new object at the end
1418 this->append(size_type(1), c);
1422 //! <b>Effects</b>: Equivalent to assign(str, 0, npos).
1424 //! <b>Returns</b>: *this
1425 basic_string& assign(const basic_string& s)
1426 { return this->operator=(s); }
1428 //! <b>Effects</b>: Equivalent to return assign(sv.data(), sv.size()).
1430 //! <b>Returns</b>: *this
1431 template<template <class, class> class BasicStringView>
1432 basic_string& assign(BasicStringView<CharT, Traits> sv)
1433 { return this->operator=(sv); }
1435 //! <b>Effects</b>: The function replaces the string controlled by *this
1436 //! with a string of length str.size() whose elements are a copy of the string
1437 //! controlled by str. Leaves str in a valid but unspecified state.
1439 //! <b>Throws</b>: Nothing
1441 //! <b>Returns</b>: *this
1442 basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_NOEXCEPT_OR_NOTHROW
1443 { return this->swap_data(ms), *this; }
1445 //! <b>Requires</b>: pos <= str.size()
1447 //! <b>Effects</b>: Determines the effective length rlen of the string to assign as
1448 //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).
1450 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
1452 //! <b>Returns</b>: *this
1453 basic_string& assign(const basic_string& s, size_type pos, size_type n)
1456 throw_out_of_range("basic_string::assign out of range position");
1457 return this->assign(s.begin() + pos,
1458 s.begin() + pos + dtl::min_value(n, s.size() - pos));
1461 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1463 //! <b>Effects</b>: Replaces the string controlled by *this with a string of
1464 //! length n whose elements are a copy of those pointed to by s.
1466 //! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
1468 //! <b>Returns</b>: *this
1469 basic_string& assign(const CharT* s, size_type n)
1470 { return this->assign(s, s + n); }
1472 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1474 //! <b>Effects</b>: Calls assign(s, traits::length(s)).
1476 //! <b>Returns</b>: *this
1477 basic_string& assign(const CharT* s)
1478 { return this->assign(s, s + Traits::length(s)); }
1480 //! <b>Effects</b>: Equivalent to assign(basic_string(n, c)).
1482 //! <b>Returns</b>: *this
1483 basic_string& assign(size_type n, CharT c)
1484 { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
1486 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1488 //! <b>Returns</b>: *this
1489 basic_string& assign(const CharT* first, const CharT* last)
1491 size_type n = static_cast<size_type>(last - first);
1493 CharT* ptr = boost::movelib::to_raw_pointer(this->priv_addr());
1494 Traits::copy(ptr, first, n);
1495 this->priv_construct_null(ptr + n);
1500 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1502 //! <b>Returns</b>: *this
1503 template <class InputIter>
1504 basic_string& assign(InputIter first, InputIter last
1505 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1506 , typename dtl::disable_if_convertible<InputIter, size_type>::type * = 0
1511 const pointer addr = this->priv_addr();
1512 CharT *ptr = boost::movelib::to_raw_pointer(addr);
1513 const size_type old_size = this->priv_size();
1514 while (first != last && cur != old_size) {
1515 Traits::assign(*ptr, *first);
1521 this->erase(addr + cur, addr + old_size);
1523 this->append(first, last);
1527 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1528 //! <b>Effects</b>: Returns assign(il.begin(), il.size()).
1530 basic_string& assign(std::initializer_list<CharT> il)
1532 return this->assign(il.begin(), il.size());
1536 //! <b>Requires</b>: pos <= size().
1538 //! <b>Effects</b>: Calls insert(pos, str.data(), str.size()).
1540 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
1542 //! <b>Returns</b>: *this
1543 basic_string& insert(size_type pos, const basic_string& s)
1545 const size_type sz = this->size();
1547 throw_out_of_range("basic_string::insert out of range position");
1548 if (sz > this->max_size() - s.size())
1549 throw_length_error("basic_string::insert max_size() exceeded");
1550 this->insert(this->priv_addr() + pos, s.begin(), s.end());
1554 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
1556 //! <b>Effects</b>: Determines the effective length rlen of the string to insert as
1557 //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
1559 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
1561 //! <b>Returns</b>: *this
1562 basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n = npos)
1564 const size_type sz = this->size();
1565 const size_type str_size = s.size();
1566 if (pos1 > sz || pos2 > str_size)
1567 throw_out_of_range("basic_string::insert out of range position");
1568 size_type len = dtl::min_value(n, str_size - pos2);
1569 if (sz > this->max_size() - len)
1570 throw_length_error("basic_string::insert max_size() exceeded");
1571 const CharT *beg_ptr = boost::movelib::to_raw_pointer(s.begin()) + pos2;
1572 const CharT *end_ptr = beg_ptr + len;
1573 this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
1577 //! <b>Requires</b>: s points to an array of at least n elements of CharT and pos <= size().
1579 //! <b>Effects</b>: Replaces the string controlled by *this with a string of length size() + n
1580 //! whose first pos elements are a copy of the initial elements of the original string
1581 //! controlled by *this and whose next n elements are a copy of the elements in s and whose
1582 //! remaining elements are a copy of the remaining elements of the original string controlled by *this.
1584 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size() or
1585 //! length_error if size() + n > max_size().
1587 //! <b>Returns</b>: *this
1588 basic_string& insert(size_type pos, const CharT* s, size_type n)
1590 if (pos > this->size())
1591 throw_out_of_range("basic_string::insert out of range position");
1592 if (this->size() > this->max_size() - n)
1593 throw_length_error("basic_string::insert max_size() exceeded");
1594 this->insert(this->priv_addr() + pos, s, s + n);
1598 //! <b>Requires</b>: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT
1600 //! <b>Effects</b>: Calls insert(pos, s, traits::length(s)).
1602 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1603 //! length_error if size() > max_size() - Traits::length(s)
1605 //! <b>Returns</b>: *this
1606 basic_string& insert(size_type pos, const CharT* s)
1608 if (pos > this->size())
1609 throw_out_of_range("basic_string::insert out of range position");
1610 size_type len = Traits::length(s);
1611 if (this->size() > this->max_size() - len)
1612 throw_length_error("basic_string::insert max_size() exceeded");
1613 this->insert(this->priv_addr() + pos, s, s + len);
1617 //! <b>Effects</b>: Equivalent to insert(pos, basic_string(n, c)).
1619 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1620 //! length_error if size() > max_size() - n
1622 //! <b>Returns</b>: *this
1623 basic_string& insert(size_type pos, size_type n, CharT c)
1625 if (pos > this->size())
1626 throw_out_of_range("basic_string::insert out of range position");
1627 if (this->size() > this->max_size() - n)
1628 throw_length_error("basic_string::insert max_size() exceeded");
1629 this->insert(const_iterator(this->priv_addr() + pos), n, c);
1633 //! <b>Effects</b>: Same as `return insert(pos, sv.data(), sv.size())`.
1635 template<template<class, class> class BasicStringView>
1636 basic_string& insert(size_type pos, BasicStringView<CharT, Traits> sv)
1637 { return this->insert(pos, sv.data(), sv.size()); }
1639 //! <b>Requires</b>: p is a valid iterator on *this.
1641 //! <b>Effects</b>: inserts a copy of c before the character referred to by p.
1643 //! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
1644 iterator insert(const_iterator p, CharT c)
1646 size_type new_offset = p - this->priv_addr();
1647 this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
1648 return this->priv_addr() + new_offset;
1651 //! <b>Requires</b>: p is a valid iterator on *this.
1653 //! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
1655 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1656 iterator insert(const_iterator p, size_type n, CharT c)
1657 { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
1659 //! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
1661 //! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
1663 //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
1664 template <class InputIter>
1665 iterator insert(const_iterator p, InputIter first, InputIter last
1666 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1667 , typename dtl::disable_if_or
1669 , dtl::is_convertible<InputIter, size_type>
1670 , dtl::is_not_input_iterator<InputIter>
1675 const size_type n_pos = p - this->cbegin();
1676 for ( ; first != last; ++first, ++p) {
1677 p = this->insert(p, *first);
1679 return this->begin() + n_pos;
1682 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1683 template <class ForwardIter>
1684 iterator insert(const_iterator p, ForwardIter first, ForwardIter last
1685 , typename dtl::disable_if_or
1687 , dtl::is_convertible<ForwardIter, size_type>
1688 , dtl::is_input_iterator<ForwardIter>
1692 const size_type n_pos = p - this->cbegin();
1693 if (first != last) {
1694 const size_type n = boost::container::iterator_distance(first, last);
1695 const size_type old_size = this->priv_size();
1696 const size_type remaining = this->capacity() - old_size;
1697 const pointer old_start = this->priv_addr();
1698 bool enough_capacity = false;
1699 size_type new_cap = 0;
1701 //Check if we have enough capacity
1702 pointer hint = pointer();
1703 pointer allocation_ret = pointer();
1704 if (remaining >= n){
1705 enough_capacity = true;
1708 //Otherwise expand current buffer or allocate new storage
1709 new_cap = this->next_capacity(n);
1711 allocation_ret = this->allocation_command
1712 (allocate_new | expand_fwd | expand_bwd, old_size + n + 1, new_cap, hint);
1714 //Check forward expansion
1715 if(old_start == allocation_ret){
1716 enough_capacity = true;
1717 this->priv_storage(new_cap);
1722 if(enough_capacity){
1723 const size_type elems_after = old_size - (p - old_start);
1724 const size_type old_length = old_size;
1725 if (elems_after >= n) {
1726 const pointer pointer_past_last = old_start + old_size + 1;
1727 priv_uninitialized_copy(old_start + (old_size - n + 1),
1728 pointer_past_last, pointer_past_last);
1730 this->priv_size(old_size+n);
1731 Traits::move(const_cast<CharT*>(boost::movelib::to_raw_pointer(p + n)),
1732 boost::movelib::to_raw_pointer(p),
1733 (elems_after - n) + 1);
1734 this->priv_copy(first, last, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1737 ForwardIter mid = first;
1738 boost::container::iterator_advance(mid, elems_after + 1);
1740 priv_uninitialized_copy(mid, last, old_start + old_size + 1);
1741 const size_type newer_size = old_size + (n - elems_after);
1742 this->priv_size(newer_size);
1743 priv_uninitialized_copy
1744 (p, const_iterator(old_start + old_length + 1),
1745 old_start + newer_size);
1746 this->priv_size(newer_size + elems_after);
1747 this->priv_copy(first, mid, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1751 pointer new_start = allocation_ret;
1753 //Copy data to new buffer
1754 size_type new_length = 0;
1755 //This can't throw, since characters are POD
1756 new_length += priv_uninitialized_copy
1757 (const_iterator(old_start), p, new_start);
1758 new_length += priv_uninitialized_copy
1759 (first, last, new_start + new_length);
1760 new_length += priv_uninitialized_copy
1761 (p, const_iterator(old_start + old_size),
1762 new_start + new_length);
1763 this->priv_construct_null(new_start + new_length);
1765 this->deallocate_block();
1766 this->is_short(false);
1767 this->priv_long_addr(new_start);
1768 this->priv_long_size(new_length);
1769 this->priv_long_storage(new_cap);
1772 //value_type is POD, so backwards expansion is much easier
1773 //than with vector<T>
1774 value_type * const oldbuf = boost::movelib::to_raw_pointer(old_start);
1775 value_type * const newbuf = boost::movelib::to_raw_pointer(new_start);
1776 const value_type *const pos = boost::movelib::to_raw_pointer(p);
1777 const size_type before = pos - oldbuf;
1779 //First move old data
1780 Traits::move(newbuf, oldbuf, before);
1781 Traits::move(newbuf + before + n, pos, old_size - before);
1782 //Now initialize the new data
1783 priv_uninitialized_copy(first, last, new_start + before);
1784 this->priv_construct_null(new_start + (old_size + n));
1785 this->is_short(false);
1786 this->priv_long_addr(new_start);
1787 this->priv_long_size(old_size + n);
1788 this->priv_long_storage(new_cap);
1792 return this->begin() + n_pos;
1796 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1797 //! <b>Effects</b>: As if by insert(p, il.begin(), il.end()).
1799 //! <b>Returns</b>: An iterator which refers to the copy of the first inserted
1800 //! character, or p if i1 is empty.
1801 iterator insert(const_iterator p, std::initializer_list<CharT> il)
1803 return this->insert(p, il.begin(), il.end());
1807 //! <b>Effects</b>: Removes the last element from the container.
1809 //! <b>Throws</b>: Nothing.
1811 //! <b>Complexity</b>: Constant time.
1812 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1814 BOOST_ASSERT(!this->empty());
1815 iterator p = this->end();
1819 //! <b>Requires</b>: pos <= size()
1821 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
1822 //! The function then replaces the string controlled by *this with a string of length size() - xlen
1823 //! whose first pos elements are a copy of the initial elements of the original string controlled by *this,
1824 //! and whose remaining elements are a copy of the elements of the original string controlled by *this
1825 //! beginning at position pos + xlen.
1827 //! <b>Throws</b>: out_of_range if pos > size().
1829 //! <b>Returns</b>: *this
1830 basic_string& erase(size_type pos = 0, size_type n = npos)
1832 if (pos > this->size())
1833 throw_out_of_range("basic_string::erase out of range position");
1834 const pointer addr = this->priv_addr();
1835 erase(addr + pos, addr + pos + dtl::min_value(n, this->size() - pos));
1839 //! <b>Effects</b>: Removes the character referred to by p.
1841 //! <b>Throws</b>: Nothing
1843 //! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
1844 //! erased. If no such element exists, end() is returned.
1845 iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1847 // The move includes the terminating null.
1848 CharT * const ptr = const_cast<CharT*>(boost::movelib::to_raw_pointer(p));
1849 const size_type old_size = this->priv_size();
1851 boost::movelib::to_raw_pointer(p + 1),
1852 old_size - (p - this->priv_addr()));
1853 this->priv_size(old_size-1);
1854 return iterator(ptr);
1857 //! <b>Requires</b>: first and last are valid iterators on *this, defining a range [first,last).
1859 //! <b>Effects</b>: Removes the characters in the range [first,last).
1861 //! <b>Throws</b>: Nothing
1863 //! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
1864 //! the other elements being erased. If no such element exists, end() is returned.
1865 iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
1867 CharT * f = const_cast<CharT*>(boost::movelib::to_raw_pointer(first));
1868 if (first != last) { // The move includes the terminating null.
1869 const size_type num_erased = last - first;
1870 const size_type old_size = this->priv_size();
1872 boost::movelib::to_raw_pointer(last),
1873 (old_size + 1)-(last - this->priv_addr()));
1874 const size_type new_length = old_size - num_erased;
1875 this->priv_size(new_length);
1880 //! <b>Effects</b>: Erases all the elements of the vector.
1882 //! <b>Throws</b>: Nothing.
1884 //! <b>Complexity</b>: Linear to the number of elements in the vector.
1885 void clear() BOOST_NOEXCEPT_OR_NOTHROW
1887 if (!this->empty()) {
1888 Traits::assign(*this->priv_addr(), CharT(0));
1893 //! <b>Requires</b>: pos1 <= size().
1895 //! <b>Effects</b>: Calls replace(pos1, n1, str.data(), str.size()).
1897 //! <b>Throws</b>: if memory allocation throws or out_of_range if pos1 > size().
1899 //! <b>Returns</b>: *this
1900 basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
1902 if (pos1 > this->size())
1903 throw_out_of_range("basic_string::replace out of range position");
1904 const size_type len = dtl::min_value(n1, this->size() - pos1);
1905 if (this->size() - len >= this->max_size() - str.size())
1906 throw_length_error("basic_string::replace max_size() exceeded");
1907 const pointer addr = this->priv_addr();
1908 return this->replace( const_iterator(addr + pos1)
1909 , const_iterator(addr + pos1 + len)
1910 , str.begin(), str.end());
1913 //! <b>Effects</b>: Calls `return replace(pos1, n1, sv.data(), sv.size());`.
1915 template<template<class, class> class BasicStringView>
1916 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv)
1918 return this->replace(pos1, n1, sv.data(), sv.size());
1921 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
1923 //! <b>Effects</b>: Determines the effective length rlen of the string to be
1924 //! inserted as the smaller of n2 and str.size() - pos2 and calls
1925 //! replace(pos1, n1, str.data() + pos2, rlen).
1927 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size().
1929 //! <b>Returns</b>: *this
1930 basic_string& replace(size_type pos1, size_type n1,
1931 const basic_string& str, size_type pos2, size_type n2 = npos)
1933 if (pos2 > str.size())
1934 throw_out_of_range("basic_string::replace out of range position");
1935 return this->replace(pos1, n1, str.data()+pos2, dtl::min_value(n2, str.size() - pos2));
1938 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size().
1940 //! <b>Effects</b>: Determines the effective length rlen of the string to be inserted as the
1941 //! smaller of n2 and sv.size() - pos2 and calls `replace(pos1, n1, sv.data() + pos2, rlen)`.
1943 //! <b>Returns</b>: *this.
1944 template<template<class, class> class BasicStringView>
1945 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv,
1946 size_type pos2, size_type n2 = npos)
1948 if (pos2 > sv.size())
1949 throw_out_of_range("basic_string::replace out of range position");
1950 return this->replace(pos1, n1, sv.data()+pos2, dtl::min_value(n2, sv.size() - pos2));
1953 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1955 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the
1956 //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error.
1957 //! Otherwise, the function replaces the string controlled by *this with a string of
1958 //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements
1959 //! of the original string controlled by *this, whose next n2 elements are a copy of the
1960 //! initial n2 elements of s, and whose remaining elements are a copy of the elements of
1961 //! the original string controlled by *this beginning at position pos + xlen.
1963 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1964 //! if the length of the resulting string would exceed max_size()
1966 //! <b>Returns</b>: *this
1967 basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
1969 if (pos1 > this->size())
1970 throw_out_of_range("basic_string::replace out of range position");
1971 const size_type len = dtl::min_value(n1, this->size() - pos1);
1972 const size_type max_size = this->max_size();
1973 if (n2 > max_size || (this->size() - len) >= (max_size - n2))
1974 throw_length_error("basic_string::replace max_size() exceeded");
1975 const pointer addr = this->priv_addr() + pos1;
1976 return this->replace(addr, addr + len, s, s + n2);
1979 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1981 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller
1982 //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise,
1983 //! the function replaces the string controlled by *this with a string of length size() - xlen + n2
1984 //! whose first pos1 elements are a copy of the initial elements of the original string controlled
1985 //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose
1986 //! remaining elements are a copy of the elements of the original string controlled by *this
1987 //! beginning at position pos + xlen.
1989 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1990 //! if the length of the resulting string would exceed max_size()
1992 //! <b>Returns</b>: *this
1993 basic_string& replace(size_type pos, size_type n1, const CharT* s)
1995 return this->replace(pos, n1, s, Traits::length(s));
1998 //! <b>Requires</b>: pos1 <= size().
2000 //! <b>Effects</b>: Equivalent to replace(pos1, n1, basic_string(n2, c)).
2002 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
2003 //! if the length of the resulting string would exceed max_size()
2005 //! <b>Returns</b>: *this
2006 basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
2008 if (pos1 > this->size())
2009 throw_out_of_range("basic_string::replace out of range position");
2010 const size_type len = dtl::min_value(n1, this->size() - pos1);
2011 if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
2012 throw_length_error("basic_string::replace max_size() exceeded");
2013 const pointer addr = this->priv_addr();
2014 return this->replace(addr + pos1, addr + pos1 + len, n2, c);
2017 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2019 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, str).
2021 //! <b>Throws</b>: if memory allocation throws
2023 //! <b>Returns</b>: *this
2024 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
2025 { return this->replace(i1, i2, str.data(), str.data()+str.size()); }
2027 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and
2028 //! s points to an array of at least n elements
2030 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, n).
2032 //! <b>Throws</b>: if memory allocation throws
2034 //! <b>Returns</b>: *this
2035 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n)
2036 { return this->replace(i1, i2, s, s + n); }
2038 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and s points to an
2039 //! array of at least traits::length(s) + 1 elements of CharT.
2041 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)).
2043 //! <b>Throws</b>: if memory allocation throws
2045 //! <b>Returns</b>: *this
2046 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s)
2047 { return this->replace(i1, i2, s, s + Traits::length(s)); }
2049 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2051 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)).
2053 //! <b>Throws</b>: if memory allocation throws
2055 //! <b>Returns</b>: *this
2056 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c)
2058 const size_type len = static_cast<size_type>(i2 - i1);
2060 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), n, c);
2064 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), len, c);
2065 insert(i2, n - len, c);
2070 //! <b>Requires</b>: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges.
2072 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)).
2074 //! <b>Throws</b>: if memory allocation throws
2076 //! <b>Returns</b>: *this
2077 template <class InputIter>
2078 basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
2079 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2080 , typename dtl::disable_if_or
2082 , dtl::is_convertible<InputIter, size_type>
2083 , dtl::is_input_iterator<InputIter>
2088 for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
2089 Traits::assign(*const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), *j1);
2093 this->erase(i1, i2);
2095 this->insert(i2, j1, j2);
2099 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2100 template <class ForwardIter>
2101 basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
2102 , typename dtl::disable_if_or
2104 , dtl::is_convertible<ForwardIter, size_type>
2105 , dtl::is_not_input_iterator<ForwardIter>
2109 difference_type n = boost::container::iterator_distance(j1, j2);
2110 const difference_type len = i2 - i1;
2112 this->priv_copy(j1, j2, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2113 this->erase(i1 + n, i2);
2117 boost::container::iterator_advance(m, len);
2118 this->priv_copy(j1, m, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2119 this->insert(i2, m, j2);
2125 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2127 //! <b>Effects</b>: Calls `replace(i1 - begin(), i2 - i1, sv).`.
2129 //! <b>Returns</b>: *this.
2130 template<template <class, class> class BasicStringView>
2131 basic_string& replace(const_iterator i1, const_iterator i2, BasicStringView<CharT, Traits> sv)
2133 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2134 , static_cast<size_type>(i2 - i1), sv);
2137 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
2138 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2140 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, il.begin(), il.size()).
2142 //! <b>Returns</b>: *this.
2143 basic_string& replace(const_iterator i1, const_iterator i2, std::initializer_list<CharT> il)
2145 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2146 , static_cast<size_type>(i2 - i1)
2147 , il.begin(), il.size());
2151 //! <b>Requires</b>: pos <= size()
2153 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as the
2154 //! smaller of n and size() - pos. s shall designate an array of at least rlen elements.
2155 //! The function then replaces the string designated by s with a string of length rlen
2156 //! whose elements are a copy of the string controlled by *this beginning at position pos.
2157 //! The function does not append a null object to the string designated by s.
2159 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos > size().
2161 //! <b>Returns</b>: rlen
2162 size_type copy(CharT* s, size_type n, size_type pos = 0) const
2164 if (pos > this->size())
2165 throw_out_of_range("basic_string::copy out of range position");
2166 const size_type len = dtl::min_value(n, this->size() - pos);
2167 Traits::copy(s, boost::movelib::to_raw_pointer(this->priv_addr() + pos), len);
2171 //! <b>Effects</b>: *this contains the same sequence of characters that was in s,
2172 //! s contains the same sequence of characters that was in *this.
2174 //! <b>Throws</b>: Nothing
2175 void swap(basic_string& x)
2176 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
2177 || allocator_traits_type::is_always_equal::value)
2179 this->base_t::swap_data(x);
2180 dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
2181 dtl::swap_alloc(this->alloc(), x.alloc(), flag);
2184 //////////////////////////////////////////////
2188 //////////////////////////////////////////////
2190 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2192 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2194 //! <b>Complexity</b>: constant time.
2195 const CharT* c_str() const BOOST_NOEXCEPT_OR_NOTHROW
2196 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2198 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2200 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2202 //! <b>Complexity</b>: constant time.
2203 const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW
2204 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2206 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2208 //! <b>Complexity</b>: constant time.
2209 CharT* data() BOOST_NOEXCEPT_OR_NOTHROW
2210 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2212 #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
2213 //! <b>Returns</b>: a string_view to the characters in the string.
2215 //! <b>Complexity</b>: constant time.
2216 template<template <class, class> class BasicStringView>
2217 operator BasicStringView<CharT, Traits>() const BOOST_NOEXCEPT_OR_NOTHROW
2218 { return this->to_view< BasicStringView<CharT, Traits> >(); }
2221 //! <b>Returns</b>: a string_view to the characters in the string.
2223 //! <b>Complexity</b>: constant time.
2225 //! <b>Note</b>: This function is available to write portable code for compilers
2226 //! that don't support templated conversion operators.
2227 template<class BasicStringView>
2228 BasicStringView to_view() const BOOST_NOEXCEPT_OR_NOTHROW
2229 { return BasicStringView(this->data(), this->size()); }
2231 //////////////////////////////////////////////
2233 // string operations
2235 //////////////////////////////////////////////
2237 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2238 //! of the following conditions hold:
2239 //! 1) pos <= xpos and xpos + str.size() <= size();
2240 //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2242 //! <b>Throws</b>: Nothing
2244 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2245 size_type find(const basic_string& s, size_type pos = 0) const
2246 { return find(s.c_str(), pos, s.size()); }
2248 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2249 //! of the following conditions hold:
2250 //! 1) pos <= xpos and xpos + sv.size() <= size();
2251 //! 2) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2253 //! <b>Throws</b>: Nothing
2255 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2256 template<template <class, class> class BasicStringView>
2257 size_type find(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2258 { return find(sv.data(), pos, sv.size()); }
2260 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2262 //! <b>Throws</b>: Nothing
2264 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
2265 size_type find(const CharT* s, size_type pos, size_type n) const
2267 if (pos + n > this->size())
2270 const pointer addr = this->priv_addr();
2271 pointer finish = addr + this->priv_size();
2272 const const_iterator result =
2273 boost::container::search(boost::movelib::to_raw_pointer(addr + pos),
2274 boost::movelib::to_raw_pointer(finish),
2275 s, s + n, Eq_traits<Traits>());
2276 return result != finish ? result - begin() : npos;
2280 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2282 //! <b>Throws</b>: Nothing
2284 //! <b>Returns</b>: find(basic_string(s), pos).
2285 size_type find(const CharT* s, size_type pos = 0) const
2286 { return this->find(s, pos, Traits::length(s)); }
2288 //! <b>Throws</b>: Nothing
2290 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
2291 size_type find(CharT c, size_type pos = 0) const
2293 const size_type sz = this->size();
2297 const pointer addr = this->priv_addr();
2298 pointer finish = addr + sz;
2299 const const_iterator result =
2300 boost::container::find_if(addr + pos, finish,
2301 boost::container::bind2nd(Eq_traits<Traits>(), c));
2302 return result != finish ? result - begin() : npos;
2306 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2307 //! that both of the following conditions obtain:
2308 //! a) xpos <= pos and xpos + str.size() <= size();
2309 //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2311 //! <b>Throws</b>: Nothing
2313 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2314 size_type rfind(const basic_string& str, size_type pos = npos) const
2315 { return rfind(str.c_str(), pos, str.size()); }
2317 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2318 //! that both of the following conditions obtain:
2319 //! a) xpos <= pos and xpos + sv.size() <= size();
2320 //! b) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2322 //! <b>Throws</b>: Nothing
2324 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2325 template<template <class, class> class BasicStringView>
2326 size_type rfind(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2327 { return rfind(sv.data(), pos, sv.size()); }
2329 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2331 //! <b>Throws</b>: Nothing
2333 //! <b>Returns</b>: rfind(basic_string(s, n), pos).
2334 size_type rfind(const CharT* s, size_type pos, size_type n) const
2336 const size_type len = this->size();
2341 return dtl::min_value(len, pos);
2343 const const_iterator last = begin() + dtl::min_value(len - n, pos) + n;
2344 const const_iterator result = find_end(begin(), last,
2346 Eq_traits<Traits>());
2347 return result != last ? result - begin() : npos;
2351 //! <b>Requires</b>: pos <= size() and s points to an array of at least
2352 //! traits::length(s) + 1 elements of CharT.
2354 //! <b>Throws</b>: Nothing
2356 //! <b>Returns</b>: rfind(basic_string(s), pos).
2357 size_type rfind(const CharT* s, size_type pos = npos) const
2358 { return rfind(s, pos, Traits::length(s)); }
2360 //! <b>Throws</b>: Nothing
2362 //! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
2363 size_type rfind(CharT c, size_type pos = npos) const
2365 const size_type len = this->size();
2370 const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
2371 const_reverse_iterator rresult =
2372 boost::container::find_if(const_reverse_iterator(last), rend(),
2373 boost::container::bind2nd(Eq_traits<Traits>(), c));
2374 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2378 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2379 //! following conditions obtain: a) pos <= xpos and xpos < size();
2380 //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2382 //! <b>Throws</b>: Nothing
2384 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2385 size_type find_first_of(const basic_string& str, size_type pos = 0) const
2386 { return this->find_first_of(str.c_str(), pos, str.size()); }
2388 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2389 //! following conditions obtain: a) pos <= xpos and xpos < size();
2390 //! b) traits::eq(at(xpos), sv.at(I)) for some element I of the string controlled by sv.
2392 //! <b>Throws</b>: Nothing
2394 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2395 template<template <class, class> class BasicStringView>
2396 size_type find_first_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2397 { return this->find_first_of(sv.data(), pos, sv.size()); }
2399 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2401 //! <b>Throws</b>: Nothing
2403 //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
2404 size_type find_first_of(const CharT* s, size_type pos, size_type n) const
2406 const size_type sz = this->size();
2410 const pointer addr = this->priv_addr();
2411 pointer finish = addr + sz;
2412 const_iterator result = boost::container::find_first_of
2413 (addr + pos, finish, s, s + n, Eq_traits<Traits>());
2414 return result != finish ? result - this->begin() : npos;
2418 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2420 //! <b>Throws</b>: Nothing
2422 //! <b>Returns</b>: find_first_of(basic_string(s), pos).
2423 size_type find_first_of(const CharT* s, size_type pos = 0) const
2424 { return this->find_first_of(s, pos, Traits::length(s)); }
2426 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2428 //! <b>Throws</b>: Nothing
2430 //! <b>Returns</b>: find_first_of(basic_string<CharT,traits,Allocator>(1,c), pos).
2431 size_type find_first_of(CharT c, size_type pos = 0) const
2432 { return this->find(c, pos); }
2434 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2435 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2436 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2438 //! <b>Throws</b>: Nothing
2440 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2441 size_type find_last_of(const basic_string& str, size_type pos = npos) const
2442 { return this->find_last_of(str.c_str(), pos, str.size()); }
2444 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2445 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2446 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2448 //! <b>Throws</b>: Nothing
2450 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2451 template<template <class, class> class BasicStringView>
2452 size_type find_last_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2453 { return this->find_last_of(sv.data(), pos, sv.size()); }
2455 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2457 //! <b>Throws</b>: Nothing
2459 //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
2460 size_type find_last_of(const CharT* s, size_type pos, size_type n) const
2462 const size_type len = this->size();
2467 const pointer addr = this->priv_addr();
2468 const const_iterator last = addr + dtl::min_value(len - 1, pos) + 1;
2469 const const_reverse_iterator rresult =
2470 boost::container::find_first_of(const_reverse_iterator(last), rend(),
2471 s, s + n, Eq_traits<Traits>());
2472 return rresult != rend() ? (rresult.base() - 1) - addr : npos;
2476 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2478 //! <b>Throws</b>: Nothing
2480 //! <b>Returns</b>: find_last_of(basic_string<CharT,traits,Allocator>(1,c),pos).
2481 size_type find_last_of(const CharT* s, size_type pos = npos) const
2482 { return find_last_of(s, pos, Traits::length(s)); }
2484 //! <b>Throws</b>: Nothing
2486 //! <b>Returns</b>: find_last_of(basic_string(s), pos).
2487 size_type find_last_of(CharT c, size_type pos = npos) const
2488 { return rfind(c, pos); }
2490 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2491 //! both of the following conditions obtain:
2492 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no
2493 //! element I of the string controlled by str.
2495 //! <b>Throws</b>: Nothing
2497 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2498 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
2499 { return find_first_not_of(str.c_str(), pos, str.size()); }
2501 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2502 //! both of the following conditions obtain:
2503 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), sv.at(I)) for no
2504 //! element I of the string controlled by sv.
2506 //! <b>Throws</b>: Nothing
2508 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2509 template<template <class, class> class BasicStringView>
2510 size_type find_first_not_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2511 { return find_first_not_of(sv.data(), pos, sv.size()); }
2513 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2515 //! <b>Throws</b>: Nothing
2517 //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
2518 size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
2520 if (pos > this->size())
2523 const pointer addr = this->priv_addr();
2524 const pointer finish = addr + this->priv_size();
2525 const const_iterator result = boost::container::find_if
2526 (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
2527 return result != finish ? result - addr : npos;
2531 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2533 //! <b>Throws</b>: Nothing
2535 //! <b>Returns</b>: find_first_not_of(basic_string(s), pos).
2536 size_type find_first_not_of(const CharT* s, size_type pos = 0) const
2537 { return find_first_not_of(s, pos, Traits::length(s)); }
2539 //! <b>Throws</b>: Nothing
2541 //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
2542 size_type find_first_not_of(CharT c, size_type pos = 0) const
2544 if (pos > this->size())
2547 const pointer addr = this->priv_addr();
2548 const pointer finish = addr + this->priv_size();
2549 const const_iterator result
2550 = boost::container::find_if(addr + pos, finish,
2551 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2552 return result != finish ? result - begin() : npos;
2556 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2557 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2558 //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.
2560 //! <b>Throws</b>: Nothing
2562 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2563 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
2564 { return find_last_not_of(str.c_str(), pos, str.size()); }
2566 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2567 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2568 //! b) traits::eq(at(xpos), sv.at(I)) for no element I of the string controlled by sv.
2570 //! <b>Throws</b>: Nothing
2572 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2573 template<template <class, class> class BasicStringView>
2574 size_type find_last_not_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2575 { return find_last_not_of(sv.data(), pos, sv.size()); }
2577 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2579 //! <b>Throws</b>: Nothing
2581 //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
2582 size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
2584 const size_type len = this->size();
2589 const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
2590 const const_reverse_iterator rresult =
2591 boost::container::find_if(const_reverse_iterator(last), rend(),
2592 Not_within_traits<Traits>(s, s + n));
2593 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2597 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2599 //! <b>Throws</b>: Nothing
2601 //! <b>Returns</b>: find_last_not_of(basic_string(s), pos).
2602 size_type find_last_not_of(const CharT* s, size_type pos = npos) const
2603 { return find_last_not_of(s, pos, Traits::length(s)); }
2605 //! <b>Throws</b>: Nothing
2607 //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
2608 size_type find_last_not_of(CharT c, size_type pos = npos) const
2610 const size_type len = this->size();
2615 const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
2616 const const_reverse_iterator rresult =
2617 boost::container::find_if(const_reverse_iterator(last), rend(),
2618 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2619 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2623 //! <b>Requires</b>: Requires: pos <= size()
2625 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2626 //! the smaller of n and size() - pos.
2628 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
2630 //! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
2631 basic_string substr(size_type pos = 0, size_type n = npos) const
2633 if (pos > this->size())
2634 throw_out_of_range("basic_string::substr out of range position");
2635 const pointer addr = this->priv_addr();
2636 return basic_string(addr + pos,
2637 addr + pos + dtl::min_value(n, size() - pos), this->alloc());
2640 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2641 //! the smaller of size() and str.size(). The function then compares the two strings by
2642 //! calling traits::compare(data(), str.data(), rlen).
2644 //! <b>Throws</b>: Nothing
2646 //! <b>Returns</b>: The nonzero result if the result of the comparison is nonzero.
2647 //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
2648 //! and value > 0 if size() > str.size()
2649 int compare(const basic_string& str) const
2651 const pointer addr = this->priv_addr();
2652 const pointer str_addr = str.priv_addr();
2653 return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
2656 //! <b>Throws</b>: Nothing
2658 //! <b>Returns</b>: compare(basic_string(sv)).
2659 template<template <class, class> class BasicStringView>
2660 int compare(BasicStringView<CharT,Traits> sv) const
2662 const pointer addr = this->priv_addr();
2663 return s_compare(addr, addr + this->priv_size(), sv.data(), sv.data() + sv.size());
2666 //! <b>Requires</b>: pos1 <= size()
2668 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2669 //! the smaller of (this->size() - pos1), n1 and str.size(). The function then compares the two strings by
2670 //! calling traits::compare(data()+pos1, str.data(), rlen).
2672 //! <b>Throws</b>: out_of_range if pos1 > size()
2674 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
2675 int compare(size_type pos1, size_type n1, const basic_string& str) const
2677 if (pos1 > this->size())
2678 throw_out_of_range("basic_string::compare out of range position");
2679 const pointer addr = this->priv_addr();
2680 const pointer str_addr = str.priv_addr();
2681 return s_compare(addr + pos1,
2682 addr + pos1 + dtl::min_value(n1, this->size() - pos1),
2683 str_addr, str_addr + str.priv_size());
2686 //! <b>Requires</b>: pos1 <= size()
2688 //! <b>Throws</b>: out_of_range if pos1 > size()
2690 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(sv).
2691 template<template <class, class> class BasicStringView>
2692 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv) const
2694 if (pos1 > this->size())
2695 throw_out_of_range("basic_string::compare out of range position");
2696 const pointer addr = this->priv_addr() + pos1;
2697 const CharT* str_addr = sv.data();
2698 return s_compare(addr, addr + dtl::min_value(n1, this->size() - pos1),
2699 str_addr, str_addr + sv.size());
2702 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2704 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2707 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
2709 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
2710 int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const
2712 if (pos1 > this->size() || pos2 > str.size())
2713 throw_out_of_range("basic_string::compare out of range position");
2714 const pointer addr = this->priv_addr() + pos1;
2715 const pointer str_addr = str.priv_addr() + pos2;
2716 return s_compare(addr, addr + dtl::min_value(n1, this->size() - pos1),
2717 str_addr, str_addr + dtl::min_value(n2, str.size() - pos2));
2720 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2722 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2725 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size()
2727 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(BasicStringView<CharT, Traits>(sv, pos2, n2)).
2728 template<template <class, class> class BasicStringView>
2729 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv, size_type pos2, size_type n2) const
2731 if (pos1 > this->size() || pos2 > sv.size())
2732 throw_out_of_range("basic_string::compare out of range position");
2733 const pointer addr = this->priv_addr() + pos1;
2734 const CharT * str_addr = sv.data() + pos2;
2735 return s_compare(addr, addr + dtl::min_value(n1, this->size() - pos1),
2736 str_addr, str_addr + dtl::min_value(n2, sv.size() - pos2));
2739 //! <b>Throws</b>: Nothing
2741 //! <b>Returns</b>: compare(basic_string(s)).
2742 int compare(const CharT* s) const
2744 const pointer addr = this->priv_addr();
2745 return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
2748 //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
2750 //! <b>Throws</b>: out_of_range if pos1 > size()
2752 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2753 int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
2755 if (pos1 > this->size())
2756 throw_out_of_range("basic_string::compare out of range position");
2757 const pointer addr = this->priv_addr();
2758 return s_compare( addr + pos1,
2759 addr + pos1 + dtl::min_value(n1, this->size() - pos1),
2763 //! <b>Requires</b>: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT.
2765 //! <b>Throws</b>: out_of_range if pos1 > size()
2767 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2768 int compare(size_type pos1, size_type n1, const CharT* s) const
2769 { return this->compare(pos1, n1, s, Traits::length(s)); }
2771 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2773 void priv_reserve(size_type res_arg, const bool null_terminate = true)
2775 if (res_arg > this->max_size()){
2776 throw_length_error("basic_string::reserve max_size() exceeded");
2779 if (this->capacity() < res_arg){
2780 size_type n = dtl::max_value(res_arg, this->size()) + 1;
2781 size_type new_cap = this->next_capacity(n);
2783 pointer new_start = this->allocation_command(allocate_new, n, new_cap, reuse);
2784 size_type new_length = 0;
2786 const pointer addr = this->priv_addr();
2787 new_length += priv_uninitialized_copy
2788 (addr, addr + this->priv_size(), new_start);
2790 this->priv_construct_null(new_start + new_length);
2792 this->deallocate_block();
2793 this->is_short(false);
2794 this->priv_long_addr(new_start);
2795 this->priv_long_size(new_length);
2796 this->priv_storage(new_cap);
2800 template<class It1, class It2>
2801 static int s_compare(It1 f1, It1 l1, It2 f2, It2 l2)
2803 const difference_type n1 = l1 - f1;
2804 const difference_type n2 = l2 - f2;
2805 const int cmp = Traits::compare(boost::movelib::to_raw_pointer(f1),
2806 boost::movelib::to_raw_pointer(f2),
2807 dtl::min_value(n1, n2));
2808 return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0));
2811 template<class AllocVersion>
2812 void priv_shrink_to_fit_dynamic_buffer
2814 , typename dtl::enable_if<dtl::is_same<AllocVersion, version_1> >::type* = 0)
2816 //Allocate a new buffer.
2817 size_type real_cap = 0;
2818 const pointer long_addr = this->priv_long_addr();
2819 const size_type long_size = this->priv_long_size();
2820 const size_type long_storage = this->priv_long_storage();
2821 //We can make this nothrow as chars are always NoThrowCopyables
2824 real_cap = long_size+1;
2825 const pointer ret = this->allocation_command(allocate_new, long_size+1, real_cap, reuse);
2827 Traits::copy( boost::movelib::to_raw_pointer(ret)
2828 , boost::movelib::to_raw_pointer(this->priv_long_addr())
2830 this->priv_long_addr(ret);
2831 this->priv_storage(real_cap);
2832 //And release old buffer
2833 this->alloc().deallocate(long_addr, long_storage);
2841 template<class AllocVersion>
2842 void priv_shrink_to_fit_dynamic_buffer
2844 , typename dtl::enable_if<dtl::is_same<AllocVersion, version_2> >::type* = 0)
2846 size_type received_size = this->priv_long_size()+1;
2847 pointer hint = this->priv_long_addr();
2848 if(this->alloc().allocation_command
2849 ( shrink_in_place | nothrow_allocation, this->priv_long_storage(), received_size, hint)){
2850 this->priv_storage(received_size);
2854 void priv_construct_null(pointer p)
2855 { this->construct(p, CharT(0)); }
2857 // Helper functions used by constructors. It is a severe error for
2858 // any of them to be called anywhere except from within constructors.
2859 void priv_terminate_string()
2860 { this->priv_construct_null(this->priv_end_addr()); }
2862 template<class FwdIt, class Count> inline
2863 void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
2865 //Save initial position
2870 for (; count--; ++first){
2871 this->construct(first, val);
2876 for (; init != first; ++init){
2877 this->destroy(init);
2884 template<class InpIt, class FwdIt> inline
2885 size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest)
2887 //Save initial destination position
2888 FwdIt dest_init = dest;
2889 size_type constructed = 0;
2892 //Try to build objects
2893 for (; first != last; ++dest, ++first, ++constructed){
2894 this->construct(dest, *first);
2899 for (; constructed--; ++dest_init){
2900 this->destroy(dest_init);
2905 return (constructed);
2908 template <class InputIterator, class OutIterator>
2909 void priv_copy(InputIterator first, InputIterator last, OutIterator result)
2911 for ( ; first != last; ++first, ++result)
2912 Traits::assign(*result, *first);
2915 void priv_copy(const CharT* first, const CharT* last, CharT* result)
2916 { Traits::copy(result, first, last - first); }
2918 template <class Integer>
2919 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2920 Integer n, Integer x,
2922 { return this->replace(first, last, (size_type) n, (CharT) x); }
2924 template <class InputIter>
2925 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2926 InputIter f, InputIter l,
2929 typedef typename boost::container::iterator_traits<InputIter>::iterator_category Category;
2930 return this->priv_replace(first, last, f, l, Category());
2933 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2936 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
2938 //!Typedef for a basic_string of
2939 //!narrow characters
2940 typedef basic_string
2942 ,std::char_traits<char>
2943 ,new_allocator<char> >
2946 //!Typedef for a basic_string of
2947 //!narrow characters
2948 typedef basic_string
2950 ,std::char_traits<wchar_t>
2951 ,new_allocator<wchar_t> >
2956 template <class CharT, class Traits, class Allocator>
2957 const typename basic_string<CharT,Traits,Allocator>::size_type
2958 basic_string<CharT,Traits,Allocator>::npos;
2963 static const bool value = false;
2966 template<class C, class T, class A>
2967 struct is_string< basic_string<C, T, A> >
2969 static const bool value = true;
2974 // ------------------------------------------------------------
2975 // Non-member functions.
2979 template <class CharT, class Traits, class Allocator> inline
2980 basic_string<CharT,Traits,Allocator>
2981 operator+(const basic_string<CharT,Traits,Allocator>& x
2982 ,const basic_string<CharT,Traits,Allocator>& y)
2984 typedef basic_string<CharT,Traits,Allocator> str_t;
2985 typedef typename str_t::reserve_t reserve_t;
2987 str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
2993 template <class CharT, class Traits, class Allocator> inline
2994 basic_string<CharT, Traits, Allocator> operator+
2995 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
2996 , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
2999 return boost::move(x);
3002 template <class CharT, class Traits, class Allocator> inline
3003 basic_string<CharT, Traits, Allocator> operator+
3004 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
3005 , const basic_string<CharT,Traits,Allocator>& y)
3008 return boost::move(x);
3011 template <class CharT, class Traits, class Allocator> inline
3012 basic_string<CharT, Traits, Allocator> operator+
3013 (const basic_string<CharT,Traits,Allocator>& x
3014 ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
3016 y.insert(y.begin(), x.begin(), x.end());
3017 return boost::move(y);
3020 template <class CharT, class Traits, class Allocator> inline
3021 basic_string<CharT, Traits, Allocator> operator+
3022 (const CharT* s, basic_string<CharT, Traits, Allocator> y)
3024 y.insert(y.begin(), s, s + Traits::length(s));
3028 template <class CharT, class Traits, class Allocator> inline
3029 basic_string<CharT,Traits,Allocator> operator+
3030 (basic_string<CharT,Traits,Allocator> x, const CharT* s)
3036 template <class CharT, class Traits, class Allocator> inline
3037 basic_string<CharT,Traits,Allocator> operator+
3038 (CharT c, basic_string<CharT,Traits,Allocator> y)
3040 y.insert(y.begin(), c);
3044 template <class CharT, class Traits, class Allocator> inline
3045 basic_string<CharT,Traits,Allocator> operator+
3046 (basic_string<CharT,Traits,Allocator> x, const CharT c)
3052 // Operator== and operator!=
3054 template <class CharT, class Traits, class Allocator>
3056 operator==(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3058 return x.size() == y.size() &&
3059 Traits::compare(x.data(), y.data(), x.size()) == 0;
3062 template <class CharT, class Traits, class Allocator>
3064 operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3066 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3067 return n == y.size() && Traits::compare(s, y.data(), n) == 0;
3070 template <class CharT, class Traits, class Allocator>
3072 operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3074 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3075 return x.size() == n && Traits::compare(x.data(), s, n) == 0;
3078 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3080 BOOST_CONTAINER_DOC1ST( bool,
3081 typename dtl::disable_if
3082 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3083 operator==( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3085 return x.size() == y.size() &&
3086 Traits::compare(x.data(), y.data(), x.size()) == 0;
3089 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3091 BOOST_CONTAINER_DOC1ST( bool,
3092 typename dtl::disable_if
3093 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3094 operator==( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3096 return x.size() == y.size() &&
3097 Traits::compare(x.data(), y.data(), x.size()) == 0;
3100 template <class CharT, class Traits, class Allocator>
3102 operator!=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3103 { return !(x == y); }
3105 template <class CharT, class Traits, class Allocator>
3107 operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3108 { return !(s == y); }
3110 template <class CharT, class Traits, class Allocator>
3112 operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3113 { return !(x == s); }
3116 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3118 BOOST_CONTAINER_DOC1ST( bool,
3119 typename dtl::disable_if
3120 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3121 operator!=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3122 { return !(x == y); }
3124 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3126 BOOST_CONTAINER_DOC1ST( bool,
3127 typename dtl::disable_if
3128 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3129 operator!=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3130 { return !(x == y); }
3132 // Operator< (and also >, <=, and >=).
3133 template <class CharT, class Traits, class Allocator>
3135 operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3137 return x.compare(y) < 0;
3140 template <class CharT, class Traits, class Allocator>
3142 operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3144 return y.compare(s) > 0;
3147 template <class CharT, class Traits, class Allocator>
3149 operator<(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3151 return x.compare(s) < 0;
3154 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3156 BOOST_CONTAINER_DOC1ST( bool,
3157 typename dtl::disable_if
3158 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3159 operator<( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3160 { return y.compare(x) > 0; }
3162 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3164 BOOST_CONTAINER_DOC1ST( bool,
3165 typename dtl::disable_if
3166 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3167 operator<( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3168 { return x.compare(y) < 0; }
3170 template <class CharT, class Traits, class Allocator>
3172 operator>(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y) {
3176 template <class CharT, class Traits, class Allocator>
3178 operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
3182 template <class CharT, class Traits, class Allocator>
3184 operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3189 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3191 BOOST_CONTAINER_DOC1ST( bool,
3192 typename dtl::disable_if
3193 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3194 operator>( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3197 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3199 BOOST_CONTAINER_DOC1ST( bool,
3200 typename dtl::disable_if
3201 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3202 operator>( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3205 template <class CharT, class Traits, class Allocator>
3207 operator<=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3212 template <class CharT, class Traits, class Allocator>
3214 operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3215 { return !(y < s); }
3217 template <class CharT, class Traits, class Allocator>
3219 operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3220 { return !(s < x); }
3223 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3225 BOOST_CONTAINER_DOC1ST( bool,
3226 typename dtl::disable_if
3227 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3228 operator<=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3229 { return !(y < x); }
3231 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3233 BOOST_CONTAINER_DOC1ST( bool,
3234 typename dtl::disable_if
3235 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3236 operator<=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3237 { return !(y < x); }
3239 template <class CharT, class Traits, class Allocator>
3241 operator>=(const basic_string<CharT,Traits,Allocator>& x,
3242 const basic_string<CharT,Traits,Allocator>& y)
3243 { return !(x < y); }
3245 template <class CharT, class Traits, class Allocator>
3247 operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3248 { return !(s < y); }
3250 template <class CharT, class Traits, class Allocator>
3252 operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3253 { return !(x < s); }
3255 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3257 BOOST_CONTAINER_DOC1ST( bool,
3258 typename dtl::disable_if
3259 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3260 operator>=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3261 { return !(x < y); }
3263 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3265 BOOST_CONTAINER_DOC1ST( bool,
3266 typename dtl::disable_if
3267 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3268 operator>=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3269 { return !(x < y); }
3272 template <class CharT, class Traits, class Allocator>
3273 inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
3276 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3280 template <class CharT, class Traits>
3282 string_fill(std::basic_ostream<CharT, Traits>& os,
3283 std::basic_streambuf<CharT, Traits>* buf,
3286 CharT f = os.fill();
3290 for (i = 0; i < n; i++)
3291 ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof());
3296 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3298 template <class CharT, class Traits, class Allocator>
3299 std::basic_ostream<CharT, Traits>&
3300 operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
3302 typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
3307 typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
3308 typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
3309 const bool left = (os.flags() & std::ios::left) != 0;
3310 const std::size_t w = os.width(0);
3311 std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
3313 if (w != 0 && n < w)
3317 ok = dtl::string_fill(os, buf, pad_len);
3320 buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
3323 ok = ok && dtl::string_fill(os, buf, pad_len);
3327 os.setstate(std::ios_base::failbit);
3333 template <class CharT, class Traits, class Allocator>
3334 std::basic_istream<CharT, Traits>&
3335 operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3337 typename std::basic_istream<CharT, Traits>::sentry sentry(is);
3340 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3341 const std::ctype<CharT>& ctype = std::use_facet<std::ctype<CharT> >(is.getloc());
3344 std::size_t n = is.width(0);
3346 n = static_cast<std::size_t>(-1);
3351 typename Traits::int_type c1 = buf->sbumpc();
3353 if (Traits::eq_int_type(c1, Traits::eof())) {
3354 is.setstate(std::ios_base::eofbit);
3358 CharT c = Traits::to_char_type(c1);
3360 if (ctype.is(std::ctype<CharT>::space, c)) {
3361 if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof()))
3362 is.setstate(std::ios_base::failbit);
3370 // If we have read no characters, then set failbit.
3372 is.setstate(std::ios_base::failbit);
3375 is.setstate(std::ios_base::failbit);
3380 template <class CharT, class Traits, class Allocator>
3381 std::basic_istream<CharT, Traits>&
3382 getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
3384 typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
3385 typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
3387 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3390 while (nread < s.max_size()) {
3391 int c1 = buf->sbumpc();
3392 if (Traits::eq_int_type(c1, Traits::eof())) {
3393 is.setstate(std::ios_base::eofbit);
3398 CharT c = Traits::to_char_type(c1);
3399 if (!Traits::eq(c, delim))
3402 break; // Character is extracted but not appended.
3406 if (nread == 0 || nread >= s.max_size())
3407 is.setstate(std::ios_base::failbit);
3412 template <class CharT, class Traits, class Allocator>
3413 inline std::basic_istream<CharT, Traits>&
3414 getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3416 return getline(is, s, '\n');
3419 template <class Ch, class Allocator>
3420 inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, Allocator> const& v)
3422 return hash_range(v.begin(), v.end());
3427 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3431 //!has_trivial_destructor_after_move<> == true_type
3432 //!specialization for optimizations
3433 template <class C, class T, class Allocator>
3434 struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
3436 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3437 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3438 ::boost::has_trivial_destructor_after_move<pointer>::value;
3443 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3445 #include <boost/container/detail/config_end.hpp>
3447 #endif // BOOST_CONTAINER_STRING_HPP