]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/container/string.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / container / string.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_CONTAINER_STRING_HPP
12 #define BOOST_CONTAINER_STRING_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/container_fwd.hpp>
25 // container
26 #include <boost/container/allocator_traits.hpp>
27 #include <boost/container/new_allocator.hpp> //new_allocator
28 #include <boost/container/throw_exception.hpp>
29 // container/detail
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>
43
44 #include <boost/intrusive/pointer_traits.hpp>
45
46 #include <boost/move/utility_core.hpp>
47 #include <boost/move/adl_move_swap.hpp>
48 #include <boost/move/traits.hpp>
49
50 #include <boost/static_assert.hpp>
51 #include <boost/core/no_exceptions_support.hpp>
52 #include <boost/functional/hash.hpp>
53
54 #include <algorithm>
55 #include <iosfwd>
56 #include <istream>
57 #include <ostream>
58 #include <ios>
59 #include <locale>
60 #include <cstddef>
61 #include <climits>
62
63 //std
64 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
65 #include <initializer_list> //for std::initializer_list
66 #endif
67
68
69 namespace boost {
70 namespace container {
71
72 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
73 namespace container_detail {
74 // ------------------------------------------------------------
75 // Class basic_string_base.
76
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
86 {
87 basic_string_base & operator=(const basic_string_base &);
88 basic_string_base(const basic_string_base &);
89
90 typedef allocator_traits<Allocator> allocator_traits_type;
91 public:
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;
98
99 basic_string_base()
100 : members_()
101 { init(); }
102
103 explicit basic_string_base(const allocator_type& a)
104 : members_(a)
105 { init(); }
106
107 explicit basic_string_base(BOOST_RV_REF(allocator_type) a)
108 : members_(boost::move(a))
109 { this->init(); }
110
111 basic_string_base(const allocator_type& a, size_type n)
112 : members_(a)
113 {
114 this->init();
115 this->allocate_initial_block(n);
116 }
117
118 explicit basic_string_base(size_type n)
119 : members_()
120 {
121 this->init();
122 this->allocate_initial_block(n);
123 }
124
125 ~basic_string_base()
126 {
127 if(!this->is_short()){
128 this->deallocate(this->priv_long_addr(), this->priv_long_storage());
129 }
130 }
131
132 private:
133
134 //This is the structure controlling a long string
135 struct long_t
136 {
137 size_type is_short : 1;
138 size_type length : (sizeof(size_type)*CHAR_BIT - 1);
139 size_type storage;
140 pointer start;
141
142 long_t()
143 {}
144
145 long_t(const long_t &other)
146 {
147 this->is_short = false;
148 length = other.length;
149 storage = other.storage;
150 start = other.start;
151 }
152
153 long_t &operator= (const long_t &other)
154 {
155 length = other.length;
156 storage = other.storage;
157 start = other.start;
158 return *this;
159 }
160 };
161
162 //This type is the first part of the structure controlling a short string
163 //The "data" member stores
164 struct short_header
165 {
166 unsigned char is_short : 1;
167 unsigned char length : (CHAR_BIT - 1);
168 };
169
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
172
173 typedef typename container_detail::aligned_storage
174 <sizeof(long_t), container_detail::alignment_of<long_t>::value>::type long_raw_t;
175
176 protected:
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;
186
187 struct short_t
188 {
189 short_header h;
190 value_type data[UnalignedFinalInternalBufferChars];
191 };
192
193 union repr_t
194 {
195 long_raw_t r;
196 short_t s;
197
198 const short_t &short_repr() const
199 { return s; }
200
201 const long_t &long_repr() const
202 { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }
203
204 short_t &short_repr()
205 { return s; }
206
207 long_t &long_repr()
208 { return *static_cast<long_t*>(static_cast<void*>(&r)); }
209 };
210
211 struct members_holder
212 : public Allocator
213 {
214 members_holder()
215 : Allocator()
216 {}
217
218 template<class AllocatorConvertible>
219 explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
220 : Allocator(boost::forward<AllocatorConvertible>(a))
221 {}
222
223 repr_t m_repr;
224 } members_;
225
226 const Allocator &alloc() const
227 { return members_; }
228
229 Allocator &alloc()
230 { return members_; }
231
232 static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
233
234 private:
235
236 static const size_type MinAllocation = InternalBufferChars*2;
237
238 protected:
239 bool is_short() const
240 {
241 //Access and copy (to avoid UB) the first byte of the union to know if the
242 //active representation is short or long
243 short_header hdr;
244 BOOST_STATIC_ASSERT((sizeof(short_header) == 1));
245 *(unsigned char*)&hdr = *(unsigned char*)&this->members_.m_repr;
246 return hdr.is_short != 0;
247 }
248
249 void is_short(bool yes)
250 {
251 const bool was_short = this->is_short();
252 if(yes && !was_short){
253 allocator_traits_type::destroy
254 ( this->alloc()
255 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
256 );
257 this->members_.m_repr.s.h.is_short = true;
258 }
259 else if(!yes && was_short){
260 allocator_traits_type::construct
261 ( this->alloc()
262 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
263 );
264 this->members_.m_repr.s.h.is_short = false;
265 }
266 }
267
268 private:
269 void init()
270 {
271 this->members_.m_repr.s.h.is_short = 1;
272 this->members_.m_repr.s.h.length = 0;
273 }
274
275 protected:
276
277 typedef container_detail::integral_constant<unsigned,
278 boost::container::container_detail::version<Allocator>::value> alloc_version;
279
280 pointer allocation_command(allocation_type command,
281 size_type limit_size,
282 size_type &prefer_in_recvd_out_size,
283 pointer &reuse)
284 {
285 if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
286 reuse = 0;
287 command &= ~(expand_fwd | expand_bwd);
288 }
289 return container_detail::allocator_version_traits<Allocator>::allocation_command
290 (this->alloc(), command, limit_size, prefer_in_recvd_out_size, reuse);
291 }
292
293 size_type next_capacity(size_type additional_objects) const
294 {
295 return next_capacity_calculator
296 <size_type, NextCapacityDouble /*NextCapacity60Percent*/>::
297 get( allocator_traits_type::max_size(this->alloc())
298 , this->priv_storage(), additional_objects );
299 }
300
301 void deallocate(pointer p, size_type n)
302 {
303 if (p && (n > InternalBufferChars))
304 this->alloc().deallocate(p, n);
305 }
306
307 void construct(pointer p, const value_type &value = value_type())
308 {
309 allocator_traits_type::construct
310 ( this->alloc()
311 , boost::movelib::to_raw_pointer(p)
312 , value
313 );
314 }
315
316 void destroy(pointer p, size_type n)
317 {
318 value_type *raw_p = boost::movelib::to_raw_pointer(p);
319 for(; n--; ++raw_p){
320 allocator_traits_type::destroy( this->alloc(), raw_p);
321 }
322 }
323
324 void destroy(pointer p)
325 {
326 allocator_traits_type::destroy
327 ( this->alloc()
328 , boost::movelib::to_raw_pointer(p)
329 );
330 }
331
332 void allocate_initial_block(size_type n)
333 {
334 if (n <= this->max_size()) {
335 if(n > InternalBufferChars){
336 size_type new_cap = this->next_capacity(n);
337 pointer reuse = 0;
338 pointer p = this->allocation_command(allocate_new, n, new_cap, reuse);
339 this->is_short(false);
340 this->priv_long_addr(p);
341 this->priv_long_size(0);
342 this->priv_storage(new_cap);
343 }
344 }
345 else{
346 throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
347 }
348 }
349
350 void deallocate_block()
351 { this->deallocate(this->priv_addr(), this->priv_storage()); }
352
353 size_type max_size() const
354 { return allocator_traits_type::max_size(this->alloc()) - 1; }
355
356 protected:
357 size_type priv_capacity() const
358 { return this->priv_storage() - 1; }
359
360 pointer priv_short_addr() const
361 { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
362
363 pointer priv_long_addr() const
364 { return this->members_.m_repr.long_repr().start; }
365
366 pointer priv_addr() const
367 {
368 return this->is_short()
369 ? priv_short_addr()
370 : priv_long_addr()
371 ;
372 }
373
374 pointer priv_end_addr() const
375 {
376 return this->is_short()
377 ? this->priv_short_addr() + this->priv_short_size()
378 : this->priv_long_addr() + this->priv_long_size()
379 ;
380 }
381
382 void priv_long_addr(pointer addr)
383 { this->members_.m_repr.long_repr().start = addr; }
384
385 size_type priv_storage() const
386 { return this->is_short() ? priv_short_storage() : priv_long_storage(); }
387
388 size_type priv_short_storage() const
389 { return InternalBufferChars; }
390
391 size_type priv_long_storage() const
392 { return this->members_.m_repr.long_repr().storage; }
393
394 void priv_storage(size_type storage)
395 {
396 if(!this->is_short())
397 this->priv_long_storage(storage);
398 }
399
400 void priv_long_storage(size_type storage)
401 {
402 this->members_.m_repr.long_repr().storage = storage;
403 }
404
405 size_type priv_size() const
406 { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
407
408 size_type priv_short_size() const
409 { return this->members_.m_repr.short_repr().h.length; }
410
411 size_type priv_long_size() const
412 { return this->members_.m_repr.long_repr().length; }
413
414 void priv_size(size_type sz)
415 {
416 if(this->is_short())
417 this->priv_short_size(sz);
418 else
419 this->priv_long_size(sz);
420 }
421
422 void priv_short_size(size_type sz)
423 {
424 this->members_.m_repr.s.h.length = (unsigned char)sz;
425 }
426
427 void priv_long_size(size_type sz)
428 {
429 this->members_.m_repr.long_repr().length = sz;
430 }
431
432 void swap_data(basic_string_base& other)
433 {
434 if(this->is_short()){
435 if(other.is_short()){
436 repr_t tmp(this->members_.m_repr);
437 this->members_.m_repr = other.members_.m_repr;
438 other.members_.m_repr = tmp;
439 }
440 else{
441 short_t short_backup(this->members_.m_repr.short_repr());
442 this->members_.m_repr.short_repr().~short_t();
443 ::new(&this->members_.m_repr.long_repr()) long_t(other.members_.m_repr.long_repr());
444 other.members_.m_repr.long_repr().~long_t();
445 ::new(&other.members_.m_repr.short_repr()) short_t(short_backup);
446 }
447 }
448 else{
449 if(other.is_short()){
450 short_t short_backup(other.members_.m_repr.short_repr());
451 other.members_.m_repr.short_repr().~short_t();
452 ::new(&other.members_.m_repr.long_repr()) long_t(this->members_.m_repr.long_repr());
453 this->members_.m_repr.long_repr().~long_t();
454 ::new(&this->members_.m_repr.short_repr()) short_t(short_backup);
455 }
456 else{
457 boost::adl_move_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
458 }
459 }
460 }
461 };
462
463 } //namespace container_detail {
464
465 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
466
467 //! The basic_string class represents a Sequence of characters. It contains all the
468 //! usual operations of a Sequence, and, additionally, it contains standard string
469 //! operations such as search and concatenation.
470 //!
471 //! The basic_string class is parameterized by character type, and by that type's
472 //! Character Traits.
473 //!
474 //! This class has performance characteristics very much like vector<>, meaning,
475 //! for example, that it does not perform reference-count or copy-on-write, and that
476 //! concatenation of two strings is an O(N) operation.
477 //!
478 //! Some of basic_string's member functions use an unusual method of specifying positions
479 //! and ranges. In addition to the conventional method using iterators, many of
480 //! basic_string's member functions use a single value pos of type size_type to represent a
481 //! position (in which case the position is begin() + pos, and many of basic_string's
482 //! member functions use two values, pos and n, to represent a range. In that case pos is
483 //! the beginning of the range and n is its size. That is, the range is
484 //! [begin() + pos, begin() + pos + n).
485 //!
486 //! Note that the C++ standard does not specify the complexity of basic_string operations.
487 //! In this implementation, basic_string has performance characteristics very similar to
488 //! those of vector: access to a single character is O(1), while copy and concatenation
489 //! are O(N).
490 //!
491 //! In this implementation, begin(),
492 //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
493 //! In this implementation, iterators are only invalidated by member functions that
494 //! explicitly change the string's contents.
495 //!
496 //! \tparam CharT The type of character it contains.
497 //! \tparam Traits The Character Traits type, which encapsulates basic character operations
498 //! \tparam Allocator The allocator, used for internal memory management.
499 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
500 template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = new_allocator<CharT> >
501 #else
502 template <class CharT, class Traits, class Allocator>
503 #endif
504 class basic_string
505 : private container_detail::basic_string_base<Allocator>
506 {
507 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
508 private:
509 typedef allocator_traits<Allocator> allocator_traits_type;
510 BOOST_COPYABLE_AND_MOVABLE(basic_string)
511 typedef container_detail::basic_string_base<Allocator> base_t;
512 static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
513
514 protected:
515 // Allocator helper class to use a char_traits as a function object.
516
517 template <class Tr>
518 struct Eq_traits
519 {
520 //Compatibility with std::binary_function
521 typedef typename Tr::char_type first_argument_type;
522 typedef typename Tr::char_type second_argument_type;
523 typedef bool result_type;
524
525 bool operator()(const first_argument_type& x, const second_argument_type& y) const
526 { return Tr::eq(x, y); }
527 };
528
529 template <class Tr>
530 struct Not_within_traits
531 {
532 typedef typename Tr::char_type argument_type;
533 typedef bool result_type;
534
535 typedef const typename Tr::char_type* Pointer;
536 const Pointer m_first;
537 const Pointer m_last;
538
539 Not_within_traits(Pointer f, Pointer l)
540 : m_first(f), m_last(l) {}
541
542 bool operator()(const typename Tr::char_type& x) const
543 {
544 return boost::container::find_if(m_first, m_last,
545 boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
546 }
547 };
548 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
549
550 public:
551 //////////////////////////////////////////////
552 //
553 // types
554 //
555 //////////////////////////////////////////////
556 typedef Traits traits_type;
557 typedef CharT value_type;
558 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
559 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
560 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
561 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
562 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
563 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
564 typedef Allocator allocator_type;
565 typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
566 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
567 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
568 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
569 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
570 static const size_type npos = size_type(-1);
571
572 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
573 private:
574 typedef constant_iterator<CharT, difference_type> cvalue_iterator;
575 typedef typename base_t::alloc_version alloc_version;
576 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
577 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
578
579 public: // Constructor, destructor, assignment.
580 //////////////////////////////////////////////
581 //
582 // construct/copy/destroy
583 //
584 //////////////////////////////////////////////
585 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
586 struct reserve_t {};
587
588 basic_string(reserve_t, size_type n,
589 const allocator_type& a = allocator_type())
590 //Select allocator as in copy constructor as reserve_t-based constructors
591 //are two step copies optimized for capacity
592 : base_t( allocator_traits_type::select_on_container_copy_construction(a)
593 , n + 1)
594 { this->priv_terminate_string(); }
595
596 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
597
598 //! <b>Effects</b>: Default constructs a basic_string.
599 //!
600 //! <b>Throws</b>: If allocator_type's default constructor throws.
601 basic_string() BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
602 : base_t()
603 { this->priv_terminate_string(); }
604
605
606 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
607 //!
608 //! <b>Throws</b>: Nothing
609 explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
610 : base_t(a)
611 { this->priv_terminate_string(); }
612
613 //! <b>Effects</b>: Copy constructs a basic_string.
614 //!
615 //! <b>Postcondition</b>: x == *this.
616 //!
617 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
618 basic_string(const basic_string& s)
619 : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
620 {
621 this->priv_terminate_string();
622 this->assign(s.begin(), s.end());
623 }
624
625 //! <b>Effects</b>: Same as basic_string(sv.data(), sv.size(), a).
626 //!
627 //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
628 template<template <class, class> class BasicStringView>
629 explicit basic_string(BasicStringView<CharT, Traits> sv, const Allocator& a = Allocator())
630 : base_t(allocator_traits_type::select_on_container_copy_construction(a))
631 {
632 this->priv_terminate_string();
633 this->assign(sv);
634 }
635
636 //! <b>Effects</b>: Move constructor. Moves s's resources to *this.
637 //!
638 //! <b>Throws</b>: Nothing.
639 //!
640 //! <b>Complexity</b>: Constant.
641 basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW
642 : base_t(boost::move(s.alloc()))
643 {
644 if(s.alloc() == this->alloc()){
645 this->swap_data(s);
646 }
647 else{
648 this->assign(s.begin(), s.end());
649 }
650 }
651
652 //! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
653 //!
654 //! <b>Postcondition</b>: x == *this.
655 //!
656 //! <b>Throws</b>: If allocation throws.
657 basic_string(const basic_string& s, const allocator_type &a)
658 : base_t(a)
659 {
660 this->priv_terminate_string();
661 this->assign(s.begin(), s.end());
662 }
663
664 //! <b>Effects</b>: Move constructor using the specified allocator.
665 //! Moves s's resources to *this.
666 //!
667 //! <b>Throws</b>: If allocation throws.
668 //!
669 //! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
670 basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
671 : base_t(a)
672 {
673 this->priv_terminate_string();
674 if(a == this->alloc()){
675 this->swap_data(s);
676 }
677 else{
678 this->assign(s.begin(), s.end());
679 }
680 }
681
682 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
683 //! and is initialized by a specific number of characters of the s string.
684 basic_string(const basic_string& s, size_type pos, size_type n = npos)
685 : base_t()
686 {
687 this->priv_terminate_string();
688 if (pos > s.size())
689 throw_out_of_range("basic_string::basic_string out of range position");
690 else
691 this->assign
692 (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
693 }
694
695 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
696 //! and is initialized by a specific number of characters of the s string.
697 basic_string(const basic_string& s, size_type pos, size_type n, const allocator_type& a)
698 : base_t(a)
699 {
700 this->priv_terminate_string();
701 if (pos > s.size())
702 throw_out_of_range("basic_string::basic_string out of range position");
703 else
704 this->assign
705 (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
706 }
707
708 //! <b>Effects</b>: Constructs a basic_string taking a default-constructed allocator,
709 //! and is initialized by a specific number of characters of the s c-string.
710 basic_string(const CharT* s, size_type n)
711 : base_t()
712 {
713 this->priv_terminate_string();
714 this->assign(s, s + n);
715 }
716
717 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
718 //! and is initialized by a specific number of characters of the s c-string.
719 basic_string(const CharT* s, size_type n, const allocator_type& a)
720 : base_t(a)
721 {
722 this->priv_terminate_string();
723 this->assign(s, s + n);
724 }
725
726 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
727 //! and is initialized by the null-terminated s c-string.
728 basic_string(const CharT* s)
729 : base_t()
730 {
731 this->priv_terminate_string();
732 this->assign(s, s + Traits::length(s));
733 }
734
735 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
736 //! and is initialized by the null-terminated s c-string.
737 basic_string(const CharT* s, const allocator_type& a)
738 : base_t(a)
739 {
740 this->priv_terminate_string();
741 this->assign(s, s + Traits::length(s));
742 }
743
744
745 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
746 //! and is initialized by n copies of c.
747 basic_string(size_type n, CharT c)
748 : base_t()
749 {
750 this->priv_terminate_string();
751 this->assign(n, c);
752 }
753
754 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
755 //! and is initialized by n copies of c.
756 basic_string(size_type n, CharT c, const allocator_type& a)
757 : base_t(a)
758 {
759 this->priv_terminate_string();
760 this->assign(n, c);
761 }
762
763 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
764 //! and is initialized by n default-initialized characters.
765 basic_string(size_type n, default_init_t)
766 : base_t(n + 1)
767 {
768 this->priv_size(n);
769 this->priv_terminate_string();
770 }
771
772 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
773 //! and is initialized by n default-initialized characters.
774 basic_string(size_type n, default_init_t, const allocator_type& a)
775 : base_t(a, n + 1)
776 {
777 this->priv_size(n);
778 this->priv_terminate_string();
779 }
780
781 //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
782 //! and a range of iterators.
783 template <class InputIterator>
784 basic_string(InputIterator f, InputIterator l)
785 : base_t()
786 {
787 this->priv_terminate_string();
788 this->assign(f, l);
789 }
790
791 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
792 //! and a range of iterators.
793 template <class InputIterator>
794 basic_string(InputIterator f, InputIterator l, const allocator_type& a)
795 : base_t(a)
796 {
797 this->priv_terminate_string();
798 this->assign(f, l);
799 }
800
801 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
802 //! <b>Effects</b>: Same as basic_string(il.begin(), il.end(), a).
803 //!
804 basic_string(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
805 : base_t(a)
806 {
807 this->priv_terminate_string();
808 this->assign(il.begin(), il.end());
809 }
810 #endif
811
812 //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
813 //!
814 //! <b>Throws</b>: Nothing.
815 //!
816 //! <b>Complexity</b>: Constant.
817 ~basic_string() BOOST_NOEXCEPT_OR_NOTHROW
818 {}
819
820 //! <b>Effects</b>: Copy constructs a string.
821 //!
822 //! <b>Postcondition</b>: x == *this.
823 //!
824 //! <b>Complexity</b>: Linear to the elements x contains.
825 basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
826 {
827 if (&x != this){
828 allocator_type &this_alloc = this->alloc();
829 const allocator_type &x_alloc = x.alloc();
830 container_detail::bool_<allocator_traits_type::
831 propagate_on_container_copy_assignment::value> flag;
832 if(flag && this_alloc != x_alloc){
833 if(!this->is_short()){
834 this->deallocate_block();
835 this->is_short(true);
836 Traits::assign(*this->priv_addr(), CharT(0));
837 this->priv_short_size(0);
838 }
839 }
840 container_detail::assign_alloc(this->alloc(), x.alloc(), flag);
841 this->assign(x.begin(), x.end());
842 }
843 return *this;
844 }
845
846 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
847 //!
848 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
849 //! is false and allocation throws
850 //!
851 //! <b>Complexity</b>: Constant if allocator_traits_type::
852 //! propagate_on_container_move_assignment is true or
853 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
854 basic_string& operator=(BOOST_RV_REF(basic_string) x)
855 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
856 || allocator_traits_type::is_always_equal::value)
857 {
858 //for move constructor, no aliasing (&x != this) is assummed.
859 BOOST_ASSERT(this != &x);
860 allocator_type &this_alloc = this->alloc();
861 allocator_type &x_alloc = x.alloc();
862 const bool propagate_alloc = allocator_traits_type::
863 propagate_on_container_move_assignment::value;
864 container_detail::bool_<propagate_alloc> flag;
865 const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
866 //Resources can be transferred if both allocators are
867 //going to be equal after this function (either propagated or already equal)
868 if(propagate_alloc || allocators_equal){
869 //Destroy objects but retain memory in case x reuses it in the future
870 this->clear();
871 //Move allocator if needed
872 container_detail::move_alloc(this_alloc, x_alloc, flag);
873 //Nothrow swap
874 this->swap_data(x);
875 }
876 //Else do a one by one move
877 else{
878 this->assign( x.begin(), x.end());
879 }
880 return *this;
881 }
882
883 //! <b>Effects</b>: Assignment from a null-terminated c-string.
884 //!
885 basic_string& operator=(const CharT* s)
886 { return this->assign(s, s + Traits::length(s)); }
887
888 //! <b>Effects</b>: Returns *this = basic_string(1, c).
889 //!
890 basic_string& operator=(CharT c)
891 { return this->assign(static_cast<size_type>(1), c); }
892
893 //! <b>Effects</b>: Equivalent to return assign(sv).
894 //!
895 template<template <class, class> class BasicStringView>
896 basic_string& operator=(BasicStringView<CharT, Traits> sv)
897 { return this->assign(sv.data(), sv.size()); }
898
899 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
900 //! <b>Effects</b>: Returns *this = basic_string(il);
901 //!
902 basic_string& operator=(std::initializer_list<CharT> il)
903 {
904 return this->assign(il.begin(), il.end());
905 }
906 #endif
907
908 //! <b>Effects</b>: Returns a copy of the internal allocator.
909 //!
910 //! <b>Throws</b>: If allocator's copy constructor throws.
911 //!
912 //! <b>Complexity</b>: Constant.
913 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
914 { return this->alloc(); }
915
916 //! <b>Effects</b>: Returns a reference to the internal allocator.
917 //!
918 //! <b>Throws</b>: Nothing
919 //!
920 //! <b>Complexity</b>: Constant.
921 //!
922 //! <b>Note</b>: Non-standard extension.
923 stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
924 { return this->alloc(); }
925
926 //! <b>Effects</b>: Returns a reference to the internal allocator.
927 //!
928 //! <b>Throws</b>: Nothing
929 //!
930 //! <b>Complexity</b>: Constant.
931 //!
932 //! <b>Note</b>: Non-standard extension.
933 const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
934 { return this->alloc(); }
935
936 //////////////////////////////////////////////
937 //
938 // iterators
939 //
940 //////////////////////////////////////////////
941
942 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
943 //!
944 //! <b>Throws</b>: Nothing.
945 //!
946 //! <b>Complexity</b>: Constant.
947 iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
948 { return this->priv_addr(); }
949
950 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
951 //!
952 //! <b>Throws</b>: Nothing.
953 //!
954 //! <b>Complexity</b>: Constant.
955 const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
956 { return this->priv_addr(); }
957
958 //! <b>Effects</b>: Returns an iterator to the end of the vector.
959 //!
960 //! <b>Throws</b>: Nothing.
961 //!
962 //! <b>Complexity</b>: Constant.
963 iterator end() BOOST_NOEXCEPT_OR_NOTHROW
964 { return this->priv_end_addr(); }
965
966 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
967 //!
968 //! <b>Throws</b>: Nothing.
969 //!
970 //! <b>Complexity</b>: Constant.
971 const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
972 { return this->priv_end_addr(); }
973
974 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
975 //! of the reversed vector.
976 //!
977 //! <b>Throws</b>: Nothing.
978 //!
979 //! <b>Complexity</b>: Constant.
980 reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
981 { return reverse_iterator(this->priv_end_addr()); }
982
983 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
984 //! of the reversed vector.
985 //!
986 //! <b>Throws</b>: Nothing.
987 //!
988 //! <b>Complexity</b>: Constant.
989 const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
990 { return this->crbegin(); }
991
992 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
993 //! of the reversed vector.
994 //!
995 //! <b>Throws</b>: Nothing.
996 //!
997 //! <b>Complexity</b>: Constant.
998 reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
999 { return reverse_iterator(this->priv_addr()); }
1000
1001 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1002 //! of the reversed vector.
1003 //!
1004 //! <b>Throws</b>: Nothing.
1005 //!
1006 //! <b>Complexity</b>: Constant.
1007 const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1008 { return this->crend(); }
1009
1010 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1011 //!
1012 //! <b>Throws</b>: Nothing.
1013 //!
1014 //! <b>Complexity</b>: Constant.
1015 const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1016 { return this->priv_addr(); }
1017
1018 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1019 //!
1020 //! <b>Throws</b>: Nothing.
1021 //!
1022 //! <b>Complexity</b>: Constant.
1023 const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1024 { return this->priv_end_addr(); }
1025
1026 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1027 //! of the reversed vector.
1028 //!
1029 //! <b>Throws</b>: Nothing.
1030 //!
1031 //! <b>Complexity</b>: Constant.
1032 const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1033 { return const_reverse_iterator(this->priv_end_addr()); }
1034
1035 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1036 //! of the reversed vector.
1037 //!
1038 //! <b>Throws</b>: Nothing.
1039 //!
1040 //! <b>Complexity</b>: Constant.
1041 const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1042 { return const_reverse_iterator(this->priv_addr()); }
1043
1044 //////////////////////////////////////////////
1045 //
1046 // capacity
1047 //
1048 //////////////////////////////////////////////
1049
1050 //! <b>Effects</b>: Returns true if the vector contains no elements.
1051 //!
1052 //! <b>Throws</b>: Nothing.
1053 //!
1054 //! <b>Complexity</b>: Constant.
1055 bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1056 { return !this->priv_size(); }
1057
1058 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1059 //!
1060 //! <b>Throws</b>: Nothing.
1061 //!
1062 //! <b>Complexity</b>: Constant.
1063 size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1064 { return this->priv_size(); }
1065
1066 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1067 //!
1068 //! <b>Throws</b>: Nothing.
1069 //!
1070 //! <b>Complexity</b>: Constant.
1071 size_type length() const BOOST_NOEXCEPT_OR_NOTHROW
1072 { return this->size(); }
1073
1074 //! <b>Effects</b>: Returns the largest possible size of the vector.
1075 //!
1076 //! <b>Throws</b>: Nothing.
1077 //!
1078 //! <b>Complexity</b>: Constant.
1079 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1080 { return base_t::max_size(); }
1081
1082 //! <b>Effects</b>: Inserts or erases elements at the end such that
1083 //! the size becomes n. New elements are copy constructed from x.
1084 //!
1085 //! <b>Throws</b>: If memory allocation throws
1086 //!
1087 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1088 void resize(size_type n, CharT c)
1089 {
1090 if (n <= this->size())
1091 this->erase(this->begin() + n, this->end());
1092 else
1093 this->append(n - this->size(), c);
1094 }
1095
1096 //! <b>Effects</b>: Inserts or erases elements at the end such that
1097 //! the size becomes n. New elements are value initialized.
1098 //!
1099 //! <b>Throws</b>: If memory allocation throws
1100 //!
1101 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1102 void resize(size_type n)
1103 { resize(n, CharT()); }
1104
1105
1106 //! <b>Effects</b>: Inserts or erases elements at the end such that
1107 //! the size becomes n. New elements are uninitialized.
1108 //!
1109 //! <b>Throws</b>: If memory allocation throws
1110 //!
1111 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1112 //!
1113 //! <b>Note</b>: Non-standard extension
1114 void resize(size_type n, default_init_t)
1115 {
1116 if (n <= this->size())
1117 this->erase(this->begin() + n, this->end());
1118 else{
1119 this->priv_reserve(n, false);
1120 this->priv_size(n);
1121 this->priv_terminate_string();
1122 }
1123 }
1124
1125 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1126 //! capacity() is always greater than or equal to size().
1127 //!
1128 //! <b>Throws</b>: Nothing.
1129 //!
1130 //! <b>Complexity</b>: Constant.
1131 size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1132 { return this->priv_capacity(); }
1133
1134 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1135 //! effect. Otherwise, it is a request for allocation of additional memory.
1136 //! If the request is successful, then capacity() is greater than or equal to
1137 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1138 //!
1139 //! <b>Throws</b>: If memory allocation allocation throws
1140 void reserve(size_type res_arg)
1141 { this->priv_reserve(res_arg); }
1142
1143 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1144 //! with previous allocations. The size of the string is unchanged
1145 //!
1146 //! <b>Throws</b>: Nothing
1147 //!
1148 //! <b>Complexity</b>: Linear to size().
1149 void shrink_to_fit()
1150 {
1151 //Check if shrinking is possible
1152 if(this->priv_storage() > InternalBufferChars){
1153 //Check if we should pass from dynamically allocated buffer
1154 //to the internal storage
1155 if(this->priv_size() < InternalBufferChars){
1156 //Dynamically allocated buffer attributes
1157 pointer long_addr = this->priv_long_addr();
1158 size_type long_storage = this->priv_long_storage();
1159 size_type long_size = this->priv_long_size();
1160 //Shrink from allocated buffer to the internal one, including trailing null
1161 Traits::copy( boost::movelib::to_raw_pointer(this->priv_short_addr())
1162 , boost::movelib::to_raw_pointer(long_addr)
1163 , long_size+1);
1164 this->is_short(true);
1165 this->alloc().deallocate(long_addr, long_storage);
1166 }
1167 else{
1168 //Shrinking in dynamic buffer
1169 this->priv_shrink_to_fit_dynamic_buffer(alloc_version());
1170 }
1171 }
1172 }
1173
1174 //////////////////////////////////////////////
1175 //
1176 // element access
1177 //
1178 //////////////////////////////////////////////
1179
1180 //! <b>Requires</b>: !empty()
1181 //!
1182 //! <b>Effects</b>: Returns a reference to the first
1183 //! element of the container.
1184 //!
1185 //! <b>Throws</b>: Nothing.
1186 //!
1187 //! <b>Complexity</b>: Constant.
1188 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1189 {
1190 BOOST_ASSERT(!this->empty());
1191 return *this->priv_addr();
1192 }
1193
1194 //! <b>Requires</b>: !empty()
1195 //!
1196 //! <b>Effects</b>: Returns a const reference to the first
1197 //! element of the container.
1198 //!
1199 //! <b>Throws</b>: Nothing.
1200 //!
1201 //! <b>Complexity</b>: Constant.
1202 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1203 {
1204 BOOST_ASSERT(!this->empty());
1205 return *this->priv_addr();
1206 }
1207
1208 //! <b>Requires</b>: !empty()
1209 //!
1210 //! <b>Effects</b>: Returns a reference to the last
1211 //! element of the container.
1212 //!
1213 //! <b>Throws</b>: Nothing.
1214 //!
1215 //! <b>Complexity</b>: Constant.
1216 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1217 {
1218 BOOST_ASSERT(!this->empty());
1219 return *(this->priv_addr() + (this->size() - 1u) );
1220 }
1221
1222 //! <b>Requires</b>: !empty()
1223 //!
1224 //! <b>Effects</b>: Returns a const reference to the last
1225 //! element of the container.
1226 //!
1227 //! <b>Throws</b>: Nothing.
1228 //!
1229 //! <b>Complexity</b>: Constant.
1230 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1231 {
1232 BOOST_ASSERT(!this->empty());
1233 return *(this->priv_addr() + (this->size() - 1u) );
1234 }
1235
1236 //! <b>Requires</b>: size() > n.
1237 //!
1238 //! <b>Effects</b>: Returns a reference to the nth element
1239 //! from the beginning of the container.
1240 //!
1241 //! <b>Throws</b>: Nothing.
1242 //!
1243 //! <b>Complexity</b>: Constant.
1244 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1245 {
1246 BOOST_ASSERT(this->size() > n);
1247 return *(this->priv_addr() + n);
1248 }
1249
1250 //! <b>Requires</b>: size() > n.
1251 //!
1252 //! <b>Effects</b>: Returns a const reference to the nth element
1253 //! from the beginning of the container.
1254 //!
1255 //! <b>Throws</b>: Nothing.
1256 //!
1257 //! <b>Complexity</b>: Constant.
1258 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1259 {
1260 BOOST_ASSERT(this->size() > n);
1261 return *(this->priv_addr() + n);
1262 }
1263
1264 //! <b>Requires</b>: size() > n.
1265 //!
1266 //! <b>Effects</b>: Returns a reference to the nth element
1267 //! from the beginning of the container.
1268 //!
1269 //! <b>Throws</b>: std::range_error if n >= size()
1270 //!
1271 //! <b>Complexity</b>: Constant.
1272 reference at(size_type n)
1273 {
1274 if (n >= this->size())
1275 throw_out_of_range("basic_string::at invalid subscript");
1276 return *(this->priv_addr() + n);
1277 }
1278
1279 //! <b>Requires</b>: size() > n.
1280 //!
1281 //! <b>Effects</b>: Returns a const reference to the nth element
1282 //! from the beginning of the container.
1283 //!
1284 //! <b>Throws</b>: std::range_error if n >= size()
1285 //!
1286 //! <b>Complexity</b>: Constant.
1287 const_reference at(size_type n) const {
1288 if (n >= this->size())
1289 throw_out_of_range("basic_string::at invalid subscript");
1290 return *(this->priv_addr() + n);
1291 }
1292
1293 //////////////////////////////////////////////
1294 //
1295 // modifiers
1296 //
1297 //////////////////////////////////////////////
1298
1299 //! <b>Effects</b>: Calls append(str.data, str.size()).
1300 //!
1301 //! <b>Returns</b>: *this
1302 basic_string& operator+=(const basic_string& s)
1303 { return this->append(s); }
1304
1305 //! <b>Effects</b>: Same as `return append(sv)`.
1306 //!
1307 template<template<class, class> class BasicStringView>
1308 basic_string& operator+=(BasicStringView<CharT, Traits> sv)
1309 {
1310 return this->append(sv);
1311 }
1312
1313 //! <b>Effects</b>: Calls append(s).
1314 //!
1315 //! <b>Returns</b>: *this
1316 basic_string& operator+=(const CharT* s)
1317 { return this->append(s); }
1318
1319 //! <b>Effects</b>: Calls append(1, c).
1320 //!
1321 //! <b>Returns</b>: *this
1322 basic_string& operator+=(CharT c)
1323 { this->push_back(c); return *this; }
1324
1325 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1326 //! <b>Effects</b>: Returns append(il)
1327 //!
1328 basic_string& operator+=(std::initializer_list<CharT> il)
1329 {
1330 return this->append(il);
1331 }
1332 #endif
1333
1334 //! <b>Effects</b>: Calls append(str.data(), str.size()).
1335 //!
1336 //! <b>Returns</b>: *this
1337 basic_string& append(const basic_string& s)
1338 { return this->append(s.begin(), s.end()); }
1339
1340 //! <b>Effects</b>: Same as return append(sv.data(), sv.size()).
1341 //!
1342 template<template<class, class> class BasicStringView>
1343 basic_string& append(BasicStringView<CharT, Traits> sv)
1344 { return this->append(sv.data(), sv.size()); }
1345
1346 //! <b>Requires</b>: pos <= str.size()
1347 //!
1348 //! <b>Effects</b>: Determines the effective length rlen of the string to append
1349 //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
1350 //!
1351 //! <b>Throws</b>: If memory allocation throws and out_of_range if pos > str.size()
1352 //!
1353 //! <b>Returns</b>: *this
1354 basic_string& append(const basic_string& s, size_type pos, size_type n = npos)
1355 {
1356 if (pos > s.size())
1357 throw_out_of_range("basic_string::append out of range position");
1358 return this->append(s.begin() + pos,
1359 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
1360 }
1361
1362 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1363 //!
1364 //! <b>Effects</b>: The function replaces the string controlled by *this with
1365 //! a string of length size() + n whose irst size() elements are a copy of the
1366 //! original string controlled by *this and whose remaining
1367 //! elements are a copy of the initial n elements of s.
1368 //!
1369 //! <b>Throws</b>: If memory allocation throws length_error if size() + n > max_size().
1370 //!
1371 //! <b>Returns</b>: *this
1372 basic_string& append(const CharT* s, size_type n)
1373 { return this->append(s, s + n); }
1374
1375 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1376 //!
1377 //! <b>Effects</b>: Calls append(s, traits::length(s)).
1378 //!
1379 //! <b>Returns</b>: *this
1380 basic_string& append(const CharT* s)
1381 { return this->append(s, s + Traits::length(s)); }
1382
1383 //! <b>Effects</b>: Equivalent to append(basic_string(n, c)).
1384 //!
1385 //! <b>Returns</b>: *this
1386 basic_string& append(size_type n, CharT c)
1387 { return this->append(cvalue_iterator(c, n), cvalue_iterator()); }
1388
1389 //! <b>Requires</b>: [first,last) is a valid range.
1390 //!
1391 //! <b>Effects</b>: Equivalent to append(basic_string(first, last)).
1392 //!
1393 //! <b>Returns</b>: *this
1394 template <class InputIter>
1395 basic_string& append(InputIter first, InputIter last)
1396 { this->insert(this->end(), first, last); return *this; }
1397
1398 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1399 //! <b>Effects</b>: Returns append(il.begin(), il.size()).
1400 //!
1401 basic_string& append(std::initializer_list<CharT> il)
1402 {
1403 return this->append(il.begin(), il.size());
1404 }
1405 #endif
1406
1407 //! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
1408 //!
1409 void push_back(CharT c)
1410 {
1411 const size_type old_size = this->priv_size();
1412 if (old_size < this->capacity()){
1413 const pointer addr = this->priv_addr();
1414 this->priv_construct_null(addr + old_size + 1);
1415 Traits::assign(addr[old_size], c);
1416 this->priv_size(old_size+1);
1417 }
1418 else{
1419 //No enough memory, insert a new object at the end
1420 this->append(size_type(1), c);
1421 }
1422 }
1423
1424 //! <b>Effects</b>: Equivalent to assign(str, 0, npos).
1425 //!
1426 //! <b>Returns</b>: *this
1427 basic_string& assign(const basic_string& s)
1428 { return this->operator=(s); }
1429
1430 //! <b>Effects</b>: Equivalent to return assign(sv.data(), sv.size()).
1431 //!
1432 //! <b>Returns</b>: *this
1433 template<template <class, class> class BasicStringView>
1434 basic_string& assign(BasicStringView<CharT, Traits> sv)
1435 { return this->operator=(sv); }
1436
1437 //! <b>Effects</b>: The function replaces the string controlled by *this
1438 //! with a string of length str.size() whose elements are a copy of the string
1439 //! controlled by str. Leaves str in a valid but unspecified state.
1440 //!
1441 //! <b>Throws</b>: Nothing
1442 //!
1443 //! <b>Returns</b>: *this
1444 basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_NOEXCEPT_OR_NOTHROW
1445 { return this->swap_data(ms), *this; }
1446
1447 //! <b>Requires</b>: pos <= str.size()
1448 //!
1449 //! <b>Effects</b>: Determines the effective length rlen of the string to assign as
1450 //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).
1451 //!
1452 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
1453 //!
1454 //! <b>Returns</b>: *this
1455 basic_string& assign(const basic_string& s, size_type pos, size_type n)
1456 {
1457 if (pos > s.size())
1458 throw_out_of_range("basic_string::assign out of range position");
1459 return this->assign(s.begin() + pos,
1460 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
1461 }
1462
1463 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
1464 //!
1465 //! <b>Effects</b>: Replaces the string controlled by *this with a string of
1466 //! length n whose elements are a copy of those pointed to by s.
1467 //!
1468 //! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
1469 //!
1470 //! <b>Returns</b>: *this
1471 basic_string& assign(const CharT* s, size_type n)
1472 { return this->assign(s, s + n); }
1473
1474 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
1475 //!
1476 //! <b>Effects</b>: Calls assign(s, traits::length(s)).
1477 //!
1478 //! <b>Returns</b>: *this
1479 basic_string& assign(const CharT* s)
1480 { return this->assign(s, s + Traits::length(s)); }
1481
1482 //! <b>Effects</b>: Equivalent to assign(basic_string(n, c)).
1483 //!
1484 //! <b>Returns</b>: *this
1485 basic_string& assign(size_type n, CharT c)
1486 { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
1487
1488 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1489 //!
1490 //! <b>Returns</b>: *this
1491 basic_string& assign(const CharT* first, const CharT* last)
1492 {
1493 size_type n = static_cast<size_type>(last - first);
1494 this->reserve(n);
1495 CharT* ptr = boost::movelib::to_raw_pointer(this->priv_addr());
1496 Traits::copy(ptr, first, n);
1497 this->priv_construct_null(ptr + n);
1498 this->priv_size(n);
1499 return *this;
1500 }
1501
1502 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
1503 //!
1504 //! <b>Returns</b>: *this
1505 template <class InputIter>
1506 basic_string& assign(InputIter first, InputIter last
1507 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1508 , typename container_detail::disable_if_convertible<InputIter, size_type>::type * = 0
1509 #endif
1510 )
1511 {
1512 size_type cur = 0;
1513 const pointer addr = this->priv_addr();
1514 CharT *ptr = boost::movelib::to_raw_pointer(addr);
1515 const size_type old_size = this->priv_size();
1516 while (first != last && cur != old_size) {
1517 Traits::assign(*ptr, *first);
1518 ++first;
1519 ++cur;
1520 ++ptr;
1521 }
1522 if (first == last)
1523 this->erase(addr + cur, addr + old_size);
1524 else
1525 this->append(first, last);
1526 return *this;
1527 }
1528
1529 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1530 //! <b>Effects</b>: Returns assign(il.begin(), il.size()).
1531 //!
1532 basic_string& assign(std::initializer_list<CharT> il)
1533 {
1534 return this->assign(il.begin(), il.size());
1535 }
1536 #endif
1537
1538 //! <b>Requires</b>: pos <= size().
1539 //!
1540 //! <b>Effects</b>: Calls insert(pos, str.data(), str.size()).
1541 //!
1542 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
1543 //!
1544 //! <b>Returns</b>: *this
1545 basic_string& insert(size_type pos, const basic_string& s)
1546 {
1547 const size_type sz = this->size();
1548 if (pos > sz)
1549 throw_out_of_range("basic_string::insert out of range position");
1550 if (sz > this->max_size() - s.size())
1551 throw_length_error("basic_string::insert max_size() exceeded");
1552 this->insert(this->priv_addr() + pos, s.begin(), s.end());
1553 return *this;
1554 }
1555
1556 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
1557 //!
1558 //! <b>Effects</b>: Determines the effective length rlen of the string to insert as
1559 //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
1560 //!
1561 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
1562 //!
1563 //! <b>Returns</b>: *this
1564 basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n = npos)
1565 {
1566 const size_type sz = this->size();
1567 const size_type str_size = s.size();
1568 if (pos1 > sz || pos2 > str_size)
1569 throw_out_of_range("basic_string::insert out of range position");
1570 size_type len = container_detail::min_value(n, str_size - pos2);
1571 if (sz > this->max_size() - len)
1572 throw_length_error("basic_string::insert max_size() exceeded");
1573 const CharT *beg_ptr = boost::movelib::to_raw_pointer(s.begin()) + pos2;
1574 const CharT *end_ptr = beg_ptr + len;
1575 this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
1576 return *this;
1577 }
1578
1579 //! <b>Requires</b>: s points to an array of at least n elements of CharT and pos <= size().
1580 //!
1581 //! <b>Effects</b>: Replaces the string controlled by *this with a string of length size() + n
1582 //! whose first pos elements are a copy of the initial elements of the original string
1583 //! controlled by *this and whose next n elements are a copy of the elements in s and whose
1584 //! remaining elements are a copy of the remaining elements of the original string controlled by *this.
1585 //!
1586 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size() or
1587 //! length_error if size() + n > max_size().
1588 //!
1589 //! <b>Returns</b>: *this
1590 basic_string& insert(size_type pos, const CharT* s, size_type n)
1591 {
1592 if (pos > this->size())
1593 throw_out_of_range("basic_string::insert out of range position");
1594 if (this->size() > this->max_size() - n)
1595 throw_length_error("basic_string::insert max_size() exceeded");
1596 this->insert(this->priv_addr() + pos, s, s + n);
1597 return *this;
1598 }
1599
1600 //! <b>Requires</b>: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT
1601 //!
1602 //! <b>Effects</b>: Calls insert(pos, s, traits::length(s)).
1603 //!
1604 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1605 //! length_error if size() > max_size() - Traits::length(s)
1606 //!
1607 //! <b>Returns</b>: *this
1608 basic_string& insert(size_type pos, const CharT* s)
1609 {
1610 if (pos > this->size())
1611 throw_out_of_range("basic_string::insert out of range position");
1612 size_type len = Traits::length(s);
1613 if (this->size() > this->max_size() - len)
1614 throw_length_error("basic_string::insert max_size() exceeded");
1615 this->insert(this->priv_addr() + pos, s, s + len);
1616 return *this;
1617 }
1618
1619 //! <b>Effects</b>: Equivalent to insert(pos, basic_string(n, c)).
1620 //!
1621 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
1622 //! length_error if size() > max_size() - n
1623 //!
1624 //! <b>Returns</b>: *this
1625 basic_string& insert(size_type pos, size_type n, CharT c)
1626 {
1627 if (pos > this->size())
1628 throw_out_of_range("basic_string::insert out of range position");
1629 if (this->size() > this->max_size() - n)
1630 throw_length_error("basic_string::insert max_size() exceeded");
1631 this->insert(const_iterator(this->priv_addr() + pos), n, c);
1632 return *this;
1633 }
1634
1635 //! <b>Effects</b>: Same as `return insert(pos, sv.data(), sv.size())`.
1636 //!
1637 template<template<class, class> class BasicStringView>
1638 basic_string& insert(size_type pos, BasicStringView<CharT, Traits> sv)
1639 { return this->insert(pos, sv.data(), sv.size()); }
1640
1641 //! <b>Requires</b>: p is a valid iterator on *this.
1642 //!
1643 //! <b>Effects</b>: inserts a copy of c before the character referred to by p.
1644 //!
1645 //! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
1646 iterator insert(const_iterator p, CharT c)
1647 {
1648 size_type new_offset = p - this->priv_addr();
1649 this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
1650 return this->priv_addr() + new_offset;
1651 }
1652
1653 //! <b>Requires</b>: p is a valid iterator on *this.
1654 //!
1655 //! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
1656 //!
1657 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1658 iterator insert(const_iterator p, size_type n, CharT c)
1659 { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
1660
1661 //! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
1662 //!
1663 //! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
1664 //!
1665 //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
1666 template <class InputIter>
1667 iterator insert(const_iterator p, InputIter first, InputIter last
1668 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1669 , typename container_detail::disable_if_or
1670 < void
1671 , container_detail::is_convertible<InputIter, size_type>
1672 , container_detail::is_not_input_iterator<InputIter>
1673 >::type * = 0
1674 #endif
1675 )
1676 {
1677 const size_type n_pos = p - this->cbegin();
1678 for ( ; first != last; ++first, ++p) {
1679 p = this->insert(p, *first);
1680 }
1681 return this->begin() + n_pos;
1682 }
1683
1684 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1685 template <class ForwardIter>
1686 iterator insert(const_iterator p, ForwardIter first, ForwardIter last
1687 , typename container_detail::disable_if_or
1688 < void
1689 , container_detail::is_convertible<ForwardIter, size_type>
1690 , container_detail::is_input_iterator<ForwardIter>
1691 >::type * = 0
1692 )
1693 {
1694 const size_type n_pos = p - this->cbegin();
1695 if (first != last) {
1696 const size_type n = boost::container::iterator_distance(first, last);
1697 const size_type old_size = this->priv_size();
1698 const size_type remaining = this->capacity() - old_size;
1699 const pointer old_start = this->priv_addr();
1700 bool enough_capacity = false;
1701 size_type new_cap = 0;
1702
1703 //Check if we have enough capacity
1704 pointer hint = pointer();
1705 pointer allocation_ret = pointer();
1706 if (remaining >= n){
1707 enough_capacity = true;
1708 }
1709 else {
1710 //Otherwise expand current buffer or allocate new storage
1711 new_cap = this->next_capacity(n);
1712 hint = old_start;
1713 allocation_ret = this->allocation_command
1714 (allocate_new | expand_fwd | expand_bwd, old_size + n + 1, new_cap, hint);
1715
1716 //Check forward expansion
1717 if(old_start == allocation_ret){
1718 enough_capacity = true;
1719 this->priv_storage(new_cap);
1720 }
1721 }
1722
1723 //Reuse same buffer
1724 if(enough_capacity){
1725 const size_type elems_after = old_size - (p - old_start);
1726 const size_type old_length = old_size;
1727 if (elems_after >= n) {
1728 const pointer pointer_past_last = old_start + old_size + 1;
1729 priv_uninitialized_copy(old_start + (old_size - n + 1),
1730 pointer_past_last, pointer_past_last);
1731
1732 this->priv_size(old_size+n);
1733 Traits::move(const_cast<CharT*>(boost::movelib::to_raw_pointer(p + n)),
1734 boost::movelib::to_raw_pointer(p),
1735 (elems_after - n) + 1);
1736 this->priv_copy(first, last, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1737 }
1738 else {
1739 ForwardIter mid = first;
1740 boost::container::iterator_advance(mid, elems_after + 1);
1741
1742 priv_uninitialized_copy(mid, last, old_start + old_size + 1);
1743 const size_type newer_size = old_size + (n - elems_after);
1744 this->priv_size(newer_size);
1745 priv_uninitialized_copy
1746 (p, const_iterator(old_start + old_length + 1),
1747 old_start + newer_size);
1748 this->priv_size(newer_size + elems_after);
1749 this->priv_copy(first, mid, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
1750 }
1751 }
1752 else{
1753 pointer new_start = allocation_ret;
1754 if(!hint){
1755 //Copy data to new buffer
1756 size_type new_length = 0;
1757 //This can't throw, since characters are POD
1758 new_length += priv_uninitialized_copy
1759 (const_iterator(old_start), p, new_start);
1760 new_length += priv_uninitialized_copy
1761 (first, last, new_start + new_length);
1762 new_length += priv_uninitialized_copy
1763 (p, const_iterator(old_start + old_size),
1764 new_start + new_length);
1765 this->priv_construct_null(new_start + new_length);
1766
1767 this->deallocate_block();
1768 this->is_short(false);
1769 this->priv_long_addr(new_start);
1770 this->priv_long_size(new_length);
1771 this->priv_long_storage(new_cap);
1772 }
1773 else{
1774 //value_type is POD, so backwards expansion is much easier
1775 //than with vector<T>
1776 value_type * const oldbuf = boost::movelib::to_raw_pointer(old_start);
1777 value_type * const newbuf = boost::movelib::to_raw_pointer(new_start);
1778 const value_type *const pos = boost::movelib::to_raw_pointer(p);
1779 const size_type before = pos - oldbuf;
1780
1781 //First move old data
1782 Traits::move(newbuf, oldbuf, before);
1783 Traits::move(newbuf + before + n, pos, old_size - before);
1784 //Now initialize the new data
1785 priv_uninitialized_copy(first, last, new_start + before);
1786 this->priv_construct_null(new_start + (old_size + n));
1787 this->is_short(false);
1788 this->priv_long_addr(new_start);
1789 this->priv_long_size(old_size + n);
1790 this->priv_long_storage(new_cap);
1791 }
1792 }
1793 }
1794 return this->begin() + n_pos;
1795 }
1796 #endif
1797
1798 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1799 //! <b>Effects</b>: As if by insert(p, il.begin(), il.end()).
1800 //!
1801 //! <b>Returns</b>: An iterator which refers to the copy of the first inserted
1802 //! character, or p if i1 is empty.
1803 iterator insert(const_iterator p, std::initializer_list<CharT> il)
1804 {
1805 return this->insert(p, il.begin(), il.end());
1806 }
1807 #endif
1808
1809 //! <b>Effects</b>: Removes the last element from the container.
1810 //!
1811 //! <b>Throws</b>: Nothing.
1812 //!
1813 //! <b>Complexity</b>: Constant time.
1814 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1815 {
1816 BOOST_ASSERT(!this->empty());
1817 iterator p = this->end();
1818 this->erase(--p);
1819 }
1820
1821 //! <b>Requires</b>: pos <= size()
1822 //!
1823 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
1824 //! The function then replaces the string controlled by *this with a string of length size() - xlen
1825 //! whose first pos elements are a copy of the initial elements of the original string controlled by *this,
1826 //! and whose remaining elements are a copy of the elements of the original string controlled by *this
1827 //! beginning at position pos + xlen.
1828 //!
1829 //! <b>Throws</b>: out_of_range if pos > size().
1830 //!
1831 //! <b>Returns</b>: *this
1832 basic_string& erase(size_type pos = 0, size_type n = npos)
1833 {
1834 if (pos > this->size())
1835 throw_out_of_range("basic_string::erase out of range position");
1836 const pointer addr = this->priv_addr();
1837 erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos));
1838 return *this;
1839 }
1840
1841 //! <b>Effects</b>: Removes the character referred to by p.
1842 //!
1843 //! <b>Throws</b>: Nothing
1844 //!
1845 //! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
1846 //! erased. If no such element exists, end() is returned.
1847 iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1848 {
1849 // The move includes the terminating null.
1850 CharT * const ptr = const_cast<CharT*>(boost::movelib::to_raw_pointer(p));
1851 const size_type old_size = this->priv_size();
1852 Traits::move(ptr,
1853 boost::movelib::to_raw_pointer(p + 1),
1854 old_size - (p - this->priv_addr()));
1855 this->priv_size(old_size-1);
1856 return iterator(ptr);
1857 }
1858
1859 //! <b>Requires</b>: first and last are valid iterators on *this, defining a range [first,last).
1860 //!
1861 //! <b>Effects</b>: Removes the characters in the range [first,last).
1862 //!
1863 //! <b>Throws</b>: Nothing
1864 //!
1865 //! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
1866 //! the other elements being erased. If no such element exists, end() is returned.
1867 iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
1868 {
1869 CharT * f = const_cast<CharT*>(boost::movelib::to_raw_pointer(first));
1870 if (first != last) { // The move includes the terminating null.
1871 const size_type num_erased = last - first;
1872 const size_type old_size = this->priv_size();
1873 Traits::move(f,
1874 boost::movelib::to_raw_pointer(last),
1875 (old_size + 1)-(last - this->priv_addr()));
1876 const size_type new_length = old_size - num_erased;
1877 this->priv_size(new_length);
1878 }
1879 return iterator(f);
1880 }
1881
1882 //! <b>Effects</b>: Erases all the elements of the vector.
1883 //!
1884 //! <b>Throws</b>: Nothing.
1885 //!
1886 //! <b>Complexity</b>: Linear to the number of elements in the vector.
1887 void clear() BOOST_NOEXCEPT_OR_NOTHROW
1888 {
1889 if (!this->empty()) {
1890 Traits::assign(*this->priv_addr(), CharT(0));
1891 this->priv_size(0);
1892 }
1893 }
1894
1895 //! <b>Requires</b>: pos1 <= size().
1896 //!
1897 //! <b>Effects</b>: Calls replace(pos1, n1, str.data(), str.size()).
1898 //!
1899 //! <b>Throws</b>: if memory allocation throws or out_of_range if pos1 > size().
1900 //!
1901 //! <b>Returns</b>: *this
1902 basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
1903 {
1904 if (pos1 > this->size())
1905 throw_out_of_range("basic_string::replace out of range position");
1906 const size_type len = container_detail::min_value(n1, this->size() - pos1);
1907 if (this->size() - len >= this->max_size() - str.size())
1908 throw_length_error("basic_string::replace max_size() exceeded");
1909 const pointer addr = this->priv_addr();
1910 return this->replace( const_iterator(addr + pos1)
1911 , const_iterator(addr + pos1 + len)
1912 , str.begin(), str.end());
1913 }
1914
1915 //! <b>Effects</b>: Calls `return replace(pos1, n1, sv.data(), sv.size());`.
1916 //!
1917 template<template<class, class> class BasicStringView>
1918 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv)
1919 {
1920 return this->replace(pos1, n1, sv.data(), sv.size());
1921 }
1922
1923 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
1924 //!
1925 //! <b>Effects</b>: Determines the effective length rlen of the string to be
1926 //! inserted as the smaller of n2 and str.size() - pos2 and calls
1927 //! replace(pos1, n1, str.data() + pos2, rlen).
1928 //!
1929 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size().
1930 //!
1931 //! <b>Returns</b>: *this
1932 basic_string& replace(size_type pos1, size_type n1,
1933 const basic_string& str, size_type pos2, size_type n2 = npos)
1934 {
1935 if (pos2 > str.size())
1936 throw_out_of_range("basic_string::replace out of range position");
1937 return this->replace(pos1, n1, str.data()+pos2, container_detail::min_value(n2, str.size() - pos2));
1938 }
1939
1940 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size().
1941 //!
1942 //! <b>Effects</b>: Determines the effective length rlen of the string to be inserted as the
1943 //! smaller of n2 and sv.size() - pos2 and calls `replace(pos1, n1, sv.data() + pos2, rlen)`.
1944 //!
1945 //! <b>Returns</b>: *this.
1946 template<template<class, class> class BasicStringView>
1947 basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv,
1948 size_type pos2, size_type n2 = npos)
1949 {
1950 if (pos2 > sv.size())
1951 throw_out_of_range("basic_string::replace out of range position");
1952 return this->replace(pos1, n1, sv.data()+pos2, container_detail::min_value(n2, sv.size() - pos2));
1953 }
1954
1955 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1956 //!
1957 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the
1958 //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error.
1959 //! Otherwise, the function replaces the string controlled by *this with a string of
1960 //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements
1961 //! of the original string controlled by *this, whose next n2 elements are a copy of the
1962 //! initial n2 elements of s, and whose remaining elements are a copy of the elements of
1963 //! the original string controlled by *this beginning at position pos + xlen.
1964 //!
1965 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1966 //! if the length of the resulting string would exceed max_size()
1967 //!
1968 //! <b>Returns</b>: *this
1969 basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
1970 {
1971 if (pos1 > this->size())
1972 throw_out_of_range("basic_string::replace out of range position");
1973 const size_type len = container_detail::min_value(n1, this->size() - pos1);
1974 const size_type max_size = this->max_size();
1975 if (n2 > max_size || (this->size() - len) >= (max_size - n2))
1976 throw_length_error("basic_string::replace max_size() exceeded");
1977 const pointer addr = this->priv_addr() + pos1;
1978 return this->replace(addr, addr + len, s, s + n2);
1979 }
1980
1981 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
1982 //!
1983 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller
1984 //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise,
1985 //! the function replaces the string controlled by *this with a string of length size() - xlen + n2
1986 //! whose first pos1 elements are a copy of the initial elements of the original string controlled
1987 //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose
1988 //! remaining elements are a copy of the elements of the original string controlled by *this
1989 //! beginning at position pos + xlen.
1990 //!
1991 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
1992 //! if the length of the resulting string would exceed max_size()
1993 //!
1994 //! <b>Returns</b>: *this
1995 basic_string& replace(size_type pos, size_type n1, const CharT* s)
1996 {
1997 return this->replace(pos, n1, s, Traits::length(s));
1998 }
1999
2000 //! <b>Requires</b>: pos1 <= size().
2001 //!
2002 //! <b>Effects</b>: Equivalent to replace(pos1, n1, basic_string(n2, c)).
2003 //!
2004 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
2005 //! if the length of the resulting string would exceed max_size()
2006 //!
2007 //! <b>Returns</b>: *this
2008 basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
2009 {
2010 if (pos1 > this->size())
2011 throw_out_of_range("basic_string::replace out of range position");
2012 const size_type len = container_detail::min_value(n1, this->size() - pos1);
2013 if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
2014 throw_length_error("basic_string::replace max_size() exceeded");
2015 const pointer addr = this->priv_addr();
2016 return this->replace(addr + pos1, addr + pos1 + len, n2, c);
2017 }
2018
2019 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2020 //!
2021 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, str).
2022 //!
2023 //! <b>Throws</b>: if memory allocation throws
2024 //!
2025 //! <b>Returns</b>: *this
2026 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
2027 { return this->replace(i1, i2, str.data(), str.data()+str.size()); }
2028
2029 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and
2030 //! s points to an array of at least n elements
2031 //!
2032 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, n).
2033 //!
2034 //! <b>Throws</b>: if memory allocation throws
2035 //!
2036 //! <b>Returns</b>: *this
2037 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n)
2038 { return this->replace(i1, i2, s, s + n); }
2039
2040 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and s points to an
2041 //! array of at least traits::length(s) + 1 elements of CharT.
2042 //!
2043 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)).
2044 //!
2045 //! <b>Throws</b>: if memory allocation throws
2046 //!
2047 //! <b>Returns</b>: *this
2048 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s)
2049 { return this->replace(i1, i2, s, s + Traits::length(s)); }
2050
2051 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
2052 //!
2053 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)).
2054 //!
2055 //! <b>Throws</b>: if memory allocation throws
2056 //!
2057 //! <b>Returns</b>: *this
2058 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c)
2059 {
2060 const size_type len = static_cast<size_type>(i2 - i1);
2061 if (len >= n) {
2062 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), n, c);
2063 erase(i1 + n, i2);
2064 }
2065 else {
2066 Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), len, c);
2067 insert(i2, n - len, c);
2068 }
2069 return *this;
2070 }
2071
2072 //! <b>Requires</b>: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges.
2073 //!
2074 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)).
2075 //!
2076 //! <b>Throws</b>: if memory allocation throws
2077 //!
2078 //! <b>Returns</b>: *this
2079 template <class InputIter>
2080 basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
2081 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2082 , typename container_detail::disable_if_or
2083 < void
2084 , container_detail::is_convertible<InputIter, size_type>
2085 , container_detail::is_input_iterator<InputIter>
2086 >::type * = 0
2087 #endif
2088 )
2089 {
2090 for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
2091 Traits::assign(*const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), *j1);
2092 }
2093
2094 if (j1 == j2)
2095 this->erase(i1, i2);
2096 else
2097 this->insert(i2, j1, j2);
2098 return *this;
2099 }
2100
2101 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
2102 template <class ForwardIter>
2103 basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
2104 , typename container_detail::disable_if_or
2105 < void
2106 , container_detail::is_convertible<ForwardIter, size_type>
2107 , container_detail::is_not_input_iterator<ForwardIter>
2108 >::type * = 0
2109 )
2110 {
2111 difference_type n = boost::container::iterator_distance(j1, j2);
2112 const difference_type len = i2 - i1;
2113 if (len >= n) {
2114 this->priv_copy(j1, j2, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2115 this->erase(i1 + n, i2);
2116 }
2117 else {
2118 ForwardIter m = j1;
2119 boost::container::iterator_advance(m, len);
2120 this->priv_copy(j1, m, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
2121 this->insert(i2, m, j2);
2122 }
2123 return *this;
2124 }
2125 #endif
2126
2127 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2128 //!
2129 //! <b>Effects</b>: Calls `replace(i1 - begin(), i2 - i1, sv).`.
2130 //!
2131 //! <b>Returns</b>: *this.
2132 template<template <class, class> class BasicStringView>
2133 basic_string& replace(const_iterator i1, const_iterator i2, BasicStringView<CharT, Traits> sv)
2134 {
2135 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2136 , static_cast<size_type>(i2 - i1), sv);
2137 }
2138
2139 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
2140 //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
2141 //!
2142 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, il.begin(), il.size()).
2143 //!
2144 //! <b>Returns</b>: *this.
2145 basic_string& replace(const_iterator i1, const_iterator i2, std::initializer_list<CharT> il)
2146 {
2147 return this->replace( static_cast<size_type>(i1 - this->cbegin())
2148 , static_cast<size_type>(i2 - i1)
2149 , il.begin(), il.size());
2150 }
2151 #endif
2152
2153 //! <b>Requires</b>: pos <= size()
2154 //!
2155 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as the
2156 //! smaller of n and size() - pos. s shall designate an array of at least rlen elements.
2157 //! The function then replaces the string designated by s with a string of length rlen
2158 //! whose elements are a copy of the string controlled by *this beginning at position pos.
2159 //! The function does not append a null object to the string designated by s.
2160 //!
2161 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos > size().
2162 //!
2163 //! <b>Returns</b>: rlen
2164 size_type copy(CharT* s, size_type n, size_type pos = 0) const
2165 {
2166 if (pos > this->size())
2167 throw_out_of_range("basic_string::copy out of range position");
2168 const size_type len = container_detail::min_value(n, this->size() - pos);
2169 Traits::copy(s, boost::movelib::to_raw_pointer(this->priv_addr() + pos), len);
2170 return len;
2171 }
2172
2173 //! <b>Effects</b>: *this contains the same sequence of characters that was in s,
2174 //! s contains the same sequence of characters that was in *this.
2175 //!
2176 //! <b>Throws</b>: Nothing
2177 void swap(basic_string& x)
2178 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
2179 || allocator_traits_type::is_always_equal::value)
2180 {
2181 this->base_t::swap_data(x);
2182 container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
2183 container_detail::swap_alloc(this->alloc(), x.alloc(), flag);
2184 }
2185
2186 //////////////////////////////////////////////
2187 //
2188 // data access
2189 //
2190 //////////////////////////////////////////////
2191
2192 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2193 //!
2194 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2195 //!
2196 //! <b>Complexity</b>: constant time.
2197 const CharT* c_str() const BOOST_NOEXCEPT_OR_NOTHROW
2198 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2199
2200 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
2201 //!
2202 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2203 //!
2204 //! <b>Complexity</b>: constant time.
2205 const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW
2206 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2207
2208 //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
2209 //!
2210 //! <b>Complexity</b>: constant time.
2211 CharT* data() BOOST_NOEXCEPT_OR_NOTHROW
2212 { return boost::movelib::to_raw_pointer(this->priv_addr()); }
2213
2214 #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
2215 //! <b>Returns</b>: a string_view to the characters in the string.
2216 //!
2217 //! <b>Complexity</b>: constant time.
2218 template<template <class, class> class BasicStringView>
2219 operator BasicStringView<CharT, Traits>() const BOOST_NOEXCEPT_OR_NOTHROW
2220 { return this->to_view< BasicStringView<CharT, Traits> >(); }
2221 #endif
2222
2223 //! <b>Returns</b>: a string_view to the characters in the string.
2224 //!
2225 //! <b>Complexity</b>: constant time.
2226 //!
2227 //! <b>Note</b>: This function is available to write portable code for compilers
2228 //! that don't support templated conversion operators.
2229 template<class BasicStringView>
2230 BasicStringView to_view() const BOOST_NOEXCEPT_OR_NOTHROW
2231 { return BasicStringView(this->data(), this->size()); }
2232
2233 //////////////////////////////////////////////
2234 //
2235 // string operations
2236 //
2237 //////////////////////////////////////////////
2238
2239 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2240 //! of the following conditions hold:
2241 //! 1) pos <= xpos and xpos + str.size() <= size();
2242 //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2243 //!
2244 //! <b>Throws</b>: Nothing
2245 //!
2246 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2247 size_type find(const basic_string& s, size_type pos = 0) const
2248 { return find(s.c_str(), pos, s.size()); }
2249
2250 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
2251 //! of the following conditions hold:
2252 //! 1) pos <= xpos and xpos + sv.size() <= size();
2253 //! 2) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2254 //!
2255 //! <b>Throws</b>: Nothing
2256 //!
2257 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2258 template<template <class, class> class BasicStringView>
2259 size_type find(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2260 { return find(sv.data(), pos, sv.size()); }
2261
2262 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2263 //!
2264 //! <b>Throws</b>: Nothing
2265 //!
2266 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
2267 size_type find(const CharT* s, size_type pos, size_type n) const
2268 {
2269 if (pos + n > this->size())
2270 return npos;
2271 else {
2272 const pointer addr = this->priv_addr();
2273 pointer finish = addr + this->priv_size();
2274 const const_iterator result =
2275 boost::container::search(boost::movelib::to_raw_pointer(addr + pos),
2276 boost::movelib::to_raw_pointer(finish),
2277 s, s + n, Eq_traits<Traits>());
2278 return result != finish ? result - begin() : npos;
2279 }
2280 }
2281
2282 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2283 //!
2284 //! <b>Throws</b>: Nothing
2285 //!
2286 //! <b>Returns</b>: find(basic_string(s), pos).
2287 size_type find(const CharT* s, size_type pos = 0) const
2288 { return this->find(s, pos, Traits::length(s)); }
2289
2290 //! <b>Throws</b>: Nothing
2291 //!
2292 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
2293 size_type find(CharT c, size_type pos = 0) const
2294 {
2295 const size_type sz = this->size();
2296 if (pos >= sz)
2297 return npos;
2298 else {
2299 const pointer addr = this->priv_addr();
2300 pointer finish = addr + sz;
2301 const const_iterator result =
2302 boost::container::find_if(addr + pos, finish,
2303 boost::container::bind2nd(Eq_traits<Traits>(), c));
2304 return result != finish ? result - begin() : npos;
2305 }
2306 }
2307
2308 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2309 //! that both of the following conditions obtain:
2310 //! a) xpos <= pos and xpos + str.size() <= size();
2311 //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
2312 //!
2313 //! <b>Throws</b>: Nothing
2314 //!
2315 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2316 size_type rfind(const basic_string& str, size_type pos = npos) const
2317 { return rfind(str.c_str(), pos, str.size()); }
2318
2319 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
2320 //! that both of the following conditions obtain:
2321 //! a) xpos <= pos and xpos + sv.size() <= size();
2322 //! b) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
2323 //!
2324 //! <b>Throws</b>: Nothing
2325 //!
2326 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2327 template<template <class, class> class BasicStringView>
2328 size_type rfind(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2329 { return rfind(sv.data(), pos, sv.size()); }
2330
2331 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2332 //!
2333 //! <b>Throws</b>: Nothing
2334 //!
2335 //! <b>Returns</b>: rfind(basic_string(s, n), pos).
2336 size_type rfind(const CharT* s, size_type pos, size_type n) const
2337 {
2338 const size_type len = this->size();
2339
2340 if (n > len)
2341 return npos;
2342 else if (n == 0)
2343 return container_detail::min_value(len, pos);
2344 else {
2345 const const_iterator last = begin() + container_detail::min_value(len - n, pos) + n;
2346 const const_iterator result = find_end(begin(), last,
2347 s, s + n,
2348 Eq_traits<Traits>());
2349 return result != last ? result - begin() : npos;
2350 }
2351 }
2352
2353 //! <b>Requires</b>: pos <= size() and s points to an array of at least
2354 //! traits::length(s) + 1 elements of CharT.
2355 //!
2356 //! <b>Throws</b>: Nothing
2357 //!
2358 //! <b>Returns</b>: rfind(basic_string(s), pos).
2359 size_type rfind(const CharT* s, size_type pos = npos) const
2360 { return rfind(s, pos, Traits::length(s)); }
2361
2362 //! <b>Throws</b>: Nothing
2363 //!
2364 //! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
2365 size_type rfind(CharT c, size_type pos = npos) const
2366 {
2367 const size_type len = this->size();
2368
2369 if (len < 1)
2370 return npos;
2371 else {
2372 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2373 const_reverse_iterator rresult =
2374 boost::container::find_if(const_reverse_iterator(last), rend(),
2375 boost::container::bind2nd(Eq_traits<Traits>(), c));
2376 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2377 }
2378 }
2379
2380 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2381 //! following conditions obtain: a) pos <= xpos and xpos < size();
2382 //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2383 //!
2384 //! <b>Throws</b>: Nothing
2385 //!
2386 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2387 size_type find_first_of(const basic_string& str, size_type pos = 0) const
2388 { return this->find_first_of(str.c_str(), pos, str.size()); }
2389
2390 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
2391 //! following conditions obtain: a) pos <= xpos and xpos < size();
2392 //! b) traits::eq(at(xpos), sv.at(I)) for some element I of the string controlled by sv.
2393 //!
2394 //! <b>Throws</b>: Nothing
2395 //!
2396 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2397 template<template <class, class> class BasicStringView>
2398 size_type find_first_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2399 { return this->find_first_of(sv.data(), pos, sv.size()); }
2400
2401 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2402 //!
2403 //! <b>Throws</b>: Nothing
2404 //!
2405 //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
2406 size_type find_first_of(const CharT* s, size_type pos, size_type n) const
2407 {
2408 const size_type sz = this->size();
2409 if (pos >= sz)
2410 return npos;
2411 else {
2412 const pointer addr = this->priv_addr();
2413 pointer finish = addr + sz;
2414 const_iterator result = boost::container::find_first_of
2415 (addr + pos, finish, s, s + n, Eq_traits<Traits>());
2416 return result != finish ? result - this->begin() : npos;
2417 }
2418 }
2419
2420 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2421 //!
2422 //! <b>Throws</b>: Nothing
2423 //!
2424 //! <b>Returns</b>: find_first_of(basic_string(s), pos).
2425 size_type find_first_of(const CharT* s, size_type pos = 0) const
2426 { return this->find_first_of(s, pos, Traits::length(s)); }
2427
2428 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2429 //!
2430 //! <b>Throws</b>: Nothing
2431 //!
2432 //! <b>Returns</b>: find_first_of(basic_string<CharT,traits,Allocator>(1,c), pos).
2433 size_type find_first_of(CharT c, size_type pos = 0) const
2434 { return this->find(c, pos); }
2435
2436 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2437 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2438 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2439 //!
2440 //! <b>Throws</b>: Nothing
2441 //!
2442 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2443 size_type find_last_of(const basic_string& str, size_type pos = npos) const
2444 { return this->find_last_of(str.c_str(), pos, str.size()); }
2445
2446 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
2447 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
2448 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
2449 //!
2450 //! <b>Throws</b>: Nothing
2451 //!
2452 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2453 template<template <class, class> class BasicStringView>
2454 size_type find_last_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2455 { return this->find_last_of(sv.data(), pos, sv.size()); }
2456
2457 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2458 //!
2459 //! <b>Throws</b>: Nothing
2460 //!
2461 //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
2462 size_type find_last_of(const CharT* s, size_type pos, size_type n) const
2463 {
2464 const size_type len = this->size();
2465
2466 if (len < 1)
2467 return npos;
2468 else {
2469 const pointer addr = this->priv_addr();
2470 const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
2471 const const_reverse_iterator rresult =
2472 boost::container::find_first_of(const_reverse_iterator(last), rend(),
2473 s, s + n, Eq_traits<Traits>());
2474 return rresult != rend() ? (rresult.base() - 1) - addr : npos;
2475 }
2476 }
2477
2478 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2479 //!
2480 //! <b>Throws</b>: Nothing
2481 //!
2482 //! <b>Returns</b>: find_last_of(basic_string<CharT,traits,Allocator>(1,c),pos).
2483 size_type find_last_of(const CharT* s, size_type pos = npos) const
2484 { return find_last_of(s, pos, Traits::length(s)); }
2485
2486 //! <b>Throws</b>: Nothing
2487 //!
2488 //! <b>Returns</b>: find_last_of(basic_string(s), pos).
2489 size_type find_last_of(CharT c, size_type pos = npos) const
2490 { return rfind(c, pos); }
2491
2492 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2493 //! both of the following conditions obtain:
2494 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no
2495 //! element I of the string controlled by str.
2496 //!
2497 //! <b>Throws</b>: Nothing
2498 //!
2499 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2500 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
2501 { return find_first_not_of(str.c_str(), pos, str.size()); }
2502
2503 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
2504 //! both of the following conditions obtain:
2505 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), sv.at(I)) for no
2506 //! element I of the string controlled by sv.
2507 //!
2508 //! <b>Throws</b>: Nothing
2509 //!
2510 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2511 template<template <class, class> class BasicStringView>
2512 size_type find_first_not_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
2513 { return find_first_not_of(sv.data(), pos, sv.size()); }
2514
2515 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2516 //!
2517 //! <b>Throws</b>: Nothing
2518 //!
2519 //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
2520 size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
2521 {
2522 if (pos > this->size())
2523 return npos;
2524 else {
2525 const pointer addr = this->priv_addr();
2526 const pointer finish = addr + this->priv_size();
2527 const const_iterator result = boost::container::find_if
2528 (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
2529 return result != finish ? result - addr : npos;
2530 }
2531 }
2532
2533 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2534 //!
2535 //! <b>Throws</b>: Nothing
2536 //!
2537 //! <b>Returns</b>: find_first_not_of(basic_string(s), pos).
2538 size_type find_first_not_of(const CharT* s, size_type pos = 0) const
2539 { return find_first_not_of(s, pos, Traits::length(s)); }
2540
2541 //! <b>Throws</b>: Nothing
2542 //!
2543 //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
2544 size_type find_first_not_of(CharT c, size_type pos = 0) const
2545 {
2546 if (pos > this->size())
2547 return npos;
2548 else {
2549 const pointer addr = this->priv_addr();
2550 const pointer finish = addr + this->priv_size();
2551 const const_iterator result
2552 = boost::container::find_if(addr + pos, finish,
2553 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2554 return result != finish ? result - begin() : npos;
2555 }
2556 }
2557
2558 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2559 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2560 //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.
2561 //!
2562 //! <b>Throws</b>: Nothing
2563 //!
2564 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2565 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
2566 { return find_last_not_of(str.c_str(), pos, str.size()); }
2567
2568 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
2569 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
2570 //! b) traits::eq(at(xpos), sv.at(I)) for no element I of the string controlled by sv.
2571 //!
2572 //! <b>Throws</b>: Nothing
2573 //!
2574 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
2575 template<template <class, class> class BasicStringView>
2576 size_type find_last_not_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
2577 { return find_last_not_of(sv.data(), pos, sv.size()); }
2578
2579 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
2580 //!
2581 //! <b>Throws</b>: Nothing
2582 //!
2583 //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
2584 size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
2585 {
2586 const size_type len = this->size();
2587
2588 if (len < 1)
2589 return npos;
2590 else {
2591 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2592 const const_reverse_iterator rresult =
2593 boost::container::find_if(const_reverse_iterator(last), rend(),
2594 Not_within_traits<Traits>(s, s + n));
2595 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2596 }
2597 }
2598
2599 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
2600 //!
2601 //! <b>Throws</b>: Nothing
2602 //!
2603 //! <b>Returns</b>: find_last_not_of(basic_string(s), pos).
2604 size_type find_last_not_of(const CharT* s, size_type pos = npos) const
2605 { return find_last_not_of(s, pos, Traits::length(s)); }
2606
2607 //! <b>Throws</b>: Nothing
2608 //!
2609 //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
2610 size_type find_last_not_of(CharT c, size_type pos = npos) const
2611 {
2612 const size_type len = this->size();
2613
2614 if (len < 1)
2615 return npos;
2616 else {
2617 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
2618 const const_reverse_iterator rresult =
2619 boost::container::find_if(const_reverse_iterator(last), rend(),
2620 boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
2621 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
2622 }
2623 }
2624
2625 //! <b>Requires</b>: Requires: pos <= size()
2626 //!
2627 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2628 //! the smaller of n and size() - pos.
2629 //!
2630 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
2631 //!
2632 //! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
2633 basic_string substr(size_type pos = 0, size_type n = npos) const
2634 {
2635 if (pos > this->size())
2636 throw_out_of_range("basic_string::substr out of range position");
2637 const pointer addr = this->priv_addr();
2638 return basic_string(addr + pos,
2639 addr + pos + container_detail::min_value(n, size() - pos), this->alloc());
2640 }
2641
2642 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2643 //! the smaller of size() and str.size(). The function then compares the two strings by
2644 //! calling traits::compare(data(), str.data(), rlen).
2645 //!
2646 //! <b>Throws</b>: Nothing
2647 //!
2648 //! <b>Returns</b>: The nonzero result if the result of the comparison is nonzero.
2649 //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
2650 //! and value > 0 if size() > str.size()
2651 int compare(const basic_string& str) const
2652 {
2653 const pointer addr = this->priv_addr();
2654 const pointer str_addr = str.priv_addr();
2655 return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
2656 }
2657
2658 //! <b>Throws</b>: Nothing
2659 //!
2660 //! <b>Returns</b>: compare(basic_string(sv)).
2661 template<template <class, class> class BasicStringView>
2662 int compare(BasicStringView<CharT,Traits> sv) const
2663 {
2664 const pointer addr = this->priv_addr();
2665 return s_compare(addr, addr + this->priv_size(), sv.data(), sv.data() + sv.size());
2666 }
2667
2668 //! <b>Requires</b>: pos1 <= size()
2669 //!
2670 //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
2671 //! the smaller of (this->size() - pos1), n1 and str.size(). The function then compares the two strings by
2672 //! calling traits::compare(data()+pos1, str.data(), rlen).
2673 //!
2674 //! <b>Throws</b>: out_of_range if pos1 > size()
2675 //!
2676 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
2677 int compare(size_type pos1, size_type n1, const basic_string& str) const
2678 {
2679 if (pos1 > this->size())
2680 throw_out_of_range("basic_string::compare out of range position");
2681 const pointer addr = this->priv_addr();
2682 const pointer str_addr = str.priv_addr();
2683 return s_compare(addr + pos1,
2684 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
2685 str_addr, str_addr + str.priv_size());
2686 }
2687
2688 //! <b>Requires</b>: pos1 <= size()
2689 //!
2690 //! <b>Throws</b>: out_of_range if pos1 > size()
2691 //!
2692 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(sv).
2693 template<template <class, class> class BasicStringView>
2694 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv) const
2695 {
2696 if (pos1 > this->size())
2697 throw_out_of_range("basic_string::compare out of range position");
2698 const pointer addr = this->priv_addr() + pos1;
2699 const CharT* str_addr = sv.data();
2700 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2701 str_addr, str_addr + sv.size());
2702 }
2703
2704 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2705 //!
2706 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2707 //! the smaller of
2708 //!
2709 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
2710 //!
2711 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
2712 int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const
2713 {
2714 if (pos1 > this->size() || pos2 > str.size())
2715 throw_out_of_range("basic_string::compare out of range position");
2716 const pointer addr = this->priv_addr() + pos1;
2717 const pointer str_addr = str.priv_addr() + pos2;
2718 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2719 str_addr, str_addr + container_detail::min_value(n2, str.size() - pos2));
2720 }
2721
2722 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
2723 //!
2724 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
2725 //! the smaller of
2726 //!
2727 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size()
2728 //!
2729 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(BasicStringView<CharT, Traits>(sv, pos2, n2)).
2730 template<template <class, class> class BasicStringView>
2731 int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv, size_type pos2, size_type n2) const
2732 {
2733 if (pos1 > this->size() || pos2 > sv.size())
2734 throw_out_of_range("basic_string::compare out of range position");
2735 const pointer addr = this->priv_addr() + pos1;
2736 const CharT * str_addr = sv.data() + pos2;
2737 return s_compare(addr, addr + container_detail::min_value(n1, this->size() - pos1),
2738 str_addr, str_addr + container_detail::min_value(n2, sv.size() - pos2));
2739 }
2740
2741 //! <b>Throws</b>: Nothing
2742 //!
2743 //! <b>Returns</b>: compare(basic_string(s)).
2744 int compare(const CharT* s) const
2745 {
2746 const pointer addr = this->priv_addr();
2747 return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
2748 }
2749
2750 //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
2751 //!
2752 //! <b>Throws</b>: out_of_range if pos1 > size()
2753 //!
2754 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2755 int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
2756 {
2757 if (pos1 > this->size())
2758 throw_out_of_range("basic_string::compare out of range position");
2759 const pointer addr = this->priv_addr();
2760 return s_compare( addr + pos1,
2761 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
2762 s, s + n2);
2763 }
2764
2765 //! <b>Requires</b>: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT.
2766 //!
2767 //! <b>Throws</b>: out_of_range if pos1 > size()
2768 //!
2769 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
2770 int compare(size_type pos1, size_type n1, const CharT* s) const
2771 { return this->compare(pos1, n1, s, Traits::length(s)); }
2772
2773 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2774 private:
2775 void priv_reserve(size_type res_arg, const bool null_terminate = true)
2776 {
2777 if (res_arg > this->max_size()){
2778 throw_length_error("basic_string::reserve max_size() exceeded");
2779 }
2780
2781 if (this->capacity() < res_arg){
2782 size_type n = container_detail::max_value(res_arg, this->size()) + 1;
2783 size_type new_cap = this->next_capacity(n);
2784 pointer reuse = 0;
2785 pointer new_start = this->allocation_command(allocate_new, n, new_cap, reuse);
2786 size_type new_length = 0;
2787
2788 const pointer addr = this->priv_addr();
2789 new_length += priv_uninitialized_copy
2790 (addr, addr + this->priv_size(), new_start);
2791 if(null_terminate){
2792 this->priv_construct_null(new_start + new_length);
2793 }
2794 this->deallocate_block();
2795 this->is_short(false);
2796 this->priv_long_addr(new_start);
2797 this->priv_long_size(new_length);
2798 this->priv_storage(new_cap);
2799 }
2800 }
2801
2802 template<class It1, class It2>
2803 static int s_compare(It1 f1, It1 l1, It2 f2, It2 l2)
2804 {
2805 const difference_type n1 = l1 - f1;
2806 const difference_type n2 = l2 - f2;
2807 const int cmp = Traits::compare(boost::movelib::to_raw_pointer(f1),
2808 boost::movelib::to_raw_pointer(f2),
2809 container_detail::min_value(n1, n2));
2810 return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0));
2811 }
2812
2813 template<class AllocVersion>
2814 void priv_shrink_to_fit_dynamic_buffer
2815 ( AllocVersion
2816 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, version_1> >::type* = 0)
2817 {
2818 //Allocate a new buffer.
2819 size_type real_cap = 0;
2820 const pointer long_addr = this->priv_long_addr();
2821 const size_type long_size = this->priv_long_size();
2822 const size_type long_storage = this->priv_long_storage();
2823 //We can make this nothrow as chars are always NoThrowCopyables
2824 BOOST_TRY{
2825 pointer reuse = 0;
2826 real_cap = long_size+1;
2827 const pointer ret = this->allocation_command(allocate_new, long_size+1, real_cap, reuse);
2828 //Copy and update
2829 Traits::copy( boost::movelib::to_raw_pointer(ret)
2830 , boost::movelib::to_raw_pointer(this->priv_long_addr())
2831 , long_size+1);
2832 this->priv_long_addr(ret);
2833 this->priv_storage(real_cap);
2834 //And release old buffer
2835 this->alloc().deallocate(long_addr, long_storage);
2836 }
2837 BOOST_CATCH(...){
2838 return;
2839 }
2840 BOOST_CATCH_END
2841 }
2842
2843 template<class AllocVersion>
2844 void priv_shrink_to_fit_dynamic_buffer
2845 ( AllocVersion
2846 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, version_2> >::type* = 0)
2847 {
2848 size_type received_size = this->priv_long_size()+1;
2849 pointer hint = this->priv_long_addr();
2850 if(this->alloc().allocation_command
2851 ( shrink_in_place | nothrow_allocation, this->priv_long_storage(), received_size, hint)){
2852 this->priv_storage(received_size);
2853 }
2854 }
2855
2856 void priv_construct_null(pointer p)
2857 { this->construct(p, CharT(0)); }
2858
2859 // Helper functions used by constructors. It is a severe error for
2860 // any of them to be called anywhere except from within constructors.
2861 void priv_terminate_string()
2862 { this->priv_construct_null(this->priv_end_addr()); }
2863
2864 template<class FwdIt, class Count> inline
2865 void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
2866 {
2867 //Save initial position
2868 FwdIt init = first;
2869
2870 BOOST_TRY{
2871 //Construct objects
2872 for (; count--; ++first){
2873 this->construct(first, val);
2874 }
2875 }
2876 BOOST_CATCH(...){
2877 //Call destructors
2878 for (; init != first; ++init){
2879 this->destroy(init);
2880 }
2881 BOOST_RETHROW
2882 }
2883 BOOST_CATCH_END
2884 }
2885
2886 template<class InpIt, class FwdIt> inline
2887 size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest)
2888 {
2889 //Save initial destination position
2890 FwdIt dest_init = dest;
2891 size_type constructed = 0;
2892
2893 BOOST_TRY{
2894 //Try to build objects
2895 for (; first != last; ++dest, ++first, ++constructed){
2896 this->construct(dest, *first);
2897 }
2898 }
2899 BOOST_CATCH(...){
2900 //Call destructors
2901 for (; constructed--; ++dest_init){
2902 this->destroy(dest_init);
2903 }
2904 BOOST_RETHROW
2905 }
2906 BOOST_CATCH_END
2907 return (constructed);
2908 }
2909
2910 template <class InputIterator, class OutIterator>
2911 void priv_copy(InputIterator first, InputIterator last, OutIterator result)
2912 {
2913 for ( ; first != last; ++first, ++result)
2914 Traits::assign(*result, *first);
2915 }
2916
2917 void priv_copy(const CharT* first, const CharT* last, CharT* result)
2918 { Traits::copy(result, first, last - first); }
2919
2920 template <class Integer>
2921 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2922 Integer n, Integer x,
2923 container_detail::true_)
2924 { return this->replace(first, last, (size_type) n, (CharT) x); }
2925
2926 template <class InputIter>
2927 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
2928 InputIter f, InputIter l,
2929 container_detail::false_)
2930 {
2931 typedef typename boost::container::iterator_traits<InputIter>::iterator_category Category;
2932 return this->priv_replace(first, last, f, l, Category());
2933 }
2934
2935 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2936 };
2937
2938 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
2939
2940 //!Typedef for a basic_string of
2941 //!narrow characters
2942 typedef basic_string
2943 <char
2944 ,std::char_traits<char>
2945 ,new_allocator<char> >
2946 string;
2947
2948 //!Typedef for a basic_string of
2949 //!narrow characters
2950 typedef basic_string
2951 <wchar_t
2952 ,std::char_traits<wchar_t>
2953 ,new_allocator<wchar_t> >
2954 wstring;
2955
2956 #else
2957
2958 template <class CharT, class Traits, class Allocator>
2959 const typename basic_string<CharT,Traits,Allocator>::size_type
2960 basic_string<CharT,Traits,Allocator>::npos;
2961
2962 template<class S>
2963 struct is_string
2964 {
2965 static const bool value = false;
2966 };
2967
2968 template<class C, class T, class A>
2969 struct is_string< basic_string<C, T, A> >
2970 {
2971 static const bool value = true;
2972 };
2973
2974 #endif
2975
2976 // ------------------------------------------------------------
2977 // Non-member functions.
2978
2979 // Operator+
2980
2981 template <class CharT, class Traits, class Allocator> inline
2982 basic_string<CharT,Traits,Allocator>
2983 operator+(const basic_string<CharT,Traits,Allocator>& x
2984 ,const basic_string<CharT,Traits,Allocator>& y)
2985 {
2986 typedef basic_string<CharT,Traits,Allocator> str_t;
2987 typedef typename str_t::reserve_t reserve_t;
2988 reserve_t reserve;
2989 str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
2990 result.append(x);
2991 result.append(y);
2992 return result;
2993 }
2994
2995 template <class CharT, class Traits, class Allocator> inline
2996 basic_string<CharT, Traits, Allocator> operator+
2997 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
2998 , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
2999 {
3000 x += y;
3001 return boost::move(x);
3002 }
3003
3004 template <class CharT, class Traits, class Allocator> inline
3005 basic_string<CharT, Traits, Allocator> operator+
3006 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
3007 , const basic_string<CharT,Traits,Allocator>& y)
3008 {
3009 x += y;
3010 return boost::move(x);
3011 }
3012
3013 template <class CharT, class Traits, class Allocator> inline
3014 basic_string<CharT, Traits, Allocator> operator+
3015 (const basic_string<CharT,Traits,Allocator>& x
3016 ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
3017 {
3018 y.insert(y.begin(), x.begin(), x.end());
3019 return boost::move(y);
3020 }
3021
3022 template <class CharT, class Traits, class Allocator> inline
3023 basic_string<CharT, Traits, Allocator> operator+
3024 (const CharT* s, basic_string<CharT, Traits, Allocator> y)
3025 {
3026 y.insert(y.begin(), s, s + Traits::length(s));
3027 return y;
3028 }
3029
3030 template <class CharT, class Traits, class Allocator> inline
3031 basic_string<CharT,Traits,Allocator> operator+
3032 (basic_string<CharT,Traits,Allocator> x, const CharT* s)
3033 {
3034 x += s;
3035 return x;
3036 }
3037
3038 template <class CharT, class Traits, class Allocator> inline
3039 basic_string<CharT,Traits,Allocator> operator+
3040 (CharT c, basic_string<CharT,Traits,Allocator> y)
3041 {
3042 y.insert(y.begin(), c);
3043 return y;
3044 }
3045
3046 template <class CharT, class Traits, class Allocator> inline
3047 basic_string<CharT,Traits,Allocator> operator+
3048 (basic_string<CharT,Traits,Allocator> x, const CharT c)
3049 {
3050 x += c;
3051 return x;
3052 }
3053
3054 // Operator== and operator!=
3055
3056 template <class CharT, class Traits, class Allocator>
3057 inline bool
3058 operator==(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3059 {
3060 return x.size() == y.size() &&
3061 Traits::compare(x.data(), y.data(), x.size()) == 0;
3062 }
3063
3064 template <class CharT, class Traits, class Allocator>
3065 inline bool
3066 operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3067 {
3068 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3069 return n == y.size() && Traits::compare(s, y.data(), n) == 0;
3070 }
3071
3072 template <class CharT, class Traits, class Allocator>
3073 inline bool
3074 operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3075 {
3076 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
3077 return x.size() == n && Traits::compare(x.data(), s, n) == 0;
3078 }
3079
3080 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3081 inline
3082 BOOST_CONTAINER_DOC1ST( bool,
3083 typename container_detail::disable_if
3084 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3085 operator==( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3086 {
3087 return x.size() == y.size() &&
3088 Traits::compare(x.data(), y.data(), x.size()) == 0;
3089 }
3090
3091 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3092 inline
3093 BOOST_CONTAINER_DOC1ST( bool,
3094 typename container_detail::disable_if
3095 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3096 operator==( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3097 {
3098 return x.size() == y.size() &&
3099 Traits::compare(x.data(), y.data(), x.size()) == 0;
3100 }
3101
3102 template <class CharT, class Traits, class Allocator>
3103 inline bool
3104 operator!=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3105 { return !(x == y); }
3106
3107 template <class CharT, class Traits, class Allocator>
3108 inline bool
3109 operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3110 { return !(s == y); }
3111
3112 template <class CharT, class Traits, class Allocator>
3113 inline bool
3114 operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3115 { return !(x == s); }
3116
3117
3118 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3119 inline
3120 BOOST_CONTAINER_DOC1ST( bool,
3121 typename container_detail::disable_if
3122 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3123 operator!=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3124 { return !(x == y); }
3125
3126 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3127 inline
3128 BOOST_CONTAINER_DOC1ST( bool,
3129 typename container_detail::disable_if
3130 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3131 operator!=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3132 { return !(x == y); }
3133
3134 // Operator< (and also >, <=, and >=).
3135 template <class CharT, class Traits, class Allocator>
3136 inline bool
3137 operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3138 {
3139 return x.compare(y) < 0;
3140 }
3141
3142 template <class CharT, class Traits, class Allocator>
3143 inline bool
3144 operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3145 {
3146 return y.compare(s) > 0;
3147 }
3148
3149 template <class CharT, class Traits, class Allocator>
3150 inline bool
3151 operator<(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3152 {
3153 return x.compare(s) < 0;
3154 }
3155
3156 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3157 inline
3158 BOOST_CONTAINER_DOC1ST( bool,
3159 typename container_detail::disable_if
3160 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3161 operator<( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3162 { return y.compare(x) > 0; }
3163
3164 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3165 inline
3166 BOOST_CONTAINER_DOC1ST( bool,
3167 typename container_detail::disable_if
3168 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3169 operator<( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3170 { return x.compare(y) < 0; }
3171
3172 template <class CharT, class Traits, class Allocator>
3173 inline bool
3174 operator>(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y) {
3175 return y < x;
3176 }
3177
3178 template <class CharT, class Traits, class Allocator>
3179 inline bool
3180 operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
3181 return y < s;
3182 }
3183
3184 template <class CharT, class Traits, class Allocator>
3185 inline bool
3186 operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3187 {
3188 return s < x;
3189 }
3190
3191 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3192 inline
3193 BOOST_CONTAINER_DOC1ST( bool,
3194 typename container_detail::disable_if
3195 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3196 operator>( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3197 { return y < x; }
3198
3199 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3200 inline
3201 BOOST_CONTAINER_DOC1ST( bool,
3202 typename container_detail::disable_if
3203 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3204 operator>( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3205 { return y < x; }
3206
3207 template <class CharT, class Traits, class Allocator>
3208 inline bool
3209 operator<=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
3210 {
3211 return !(y < x);
3212 }
3213
3214 template <class CharT, class Traits, class Allocator>
3215 inline bool
3216 operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3217 { return !(y < s); }
3218
3219 template <class CharT, class Traits, class Allocator>
3220 inline bool
3221 operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3222 { return !(s < x); }
3223
3224
3225 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3226 inline
3227 BOOST_CONTAINER_DOC1ST( bool,
3228 typename container_detail::disable_if
3229 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3230 operator<=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3231 { return !(y < x); }
3232
3233 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3234 inline
3235 BOOST_CONTAINER_DOC1ST( bool,
3236 typename container_detail::disable_if
3237 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3238 operator<=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3239 { return !(y < x); }
3240
3241 template <class CharT, class Traits, class Allocator>
3242 inline bool
3243 operator>=(const basic_string<CharT,Traits,Allocator>& x,
3244 const basic_string<CharT,Traits,Allocator>& y)
3245 { return !(x < y); }
3246
3247 template <class CharT, class Traits, class Allocator>
3248 inline bool
3249 operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
3250 { return !(s < y); }
3251
3252 template <class CharT, class Traits, class Allocator>
3253 inline bool
3254 operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
3255 { return !(x < s); }
3256
3257 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3258 inline
3259 BOOST_CONTAINER_DOC1ST( bool,
3260 typename container_detail::disable_if
3261 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3262 operator>=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
3263 { return !(x < y); }
3264
3265 template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
3266 inline
3267 BOOST_CONTAINER_DOC1ST( bool,
3268 typename container_detail::disable_if
3269 <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
3270 operator>=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
3271 { return !(x < y); }
3272
3273 // Swap.
3274 template <class CharT, class Traits, class Allocator>
3275 inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
3276 { x.swap(y); }
3277
3278 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3279 // I/O.
3280 namespace container_detail {
3281
3282 template <class CharT, class Traits>
3283 inline bool
3284 string_fill(std::basic_ostream<CharT, Traits>& os,
3285 std::basic_streambuf<CharT, Traits>* buf,
3286 std::size_t n)
3287 {
3288 CharT f = os.fill();
3289 std::size_t i;
3290 bool ok = true;
3291
3292 for (i = 0; i < n; i++)
3293 ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof());
3294 return ok;
3295 }
3296
3297 } //namespace container_detail {
3298 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3299
3300 template <class CharT, class Traits, class Allocator>
3301 std::basic_ostream<CharT, Traits>&
3302 operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
3303 {
3304 typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
3305 bool ok = false;
3306
3307 if (sentry) {
3308 ok = true;
3309 typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
3310 typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
3311 const bool left = (os.flags() & std::ios::left) != 0;
3312 const std::size_t w = os.width(0);
3313 std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
3314
3315 if (w != 0 && n < w)
3316 pad_len = w - n;
3317
3318 if (!left)
3319 ok = container_detail::string_fill(os, buf, pad_len);
3320
3321 ok = ok &&
3322 buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
3323
3324 if (left)
3325 ok = ok && container_detail::string_fill(os, buf, pad_len);
3326 }
3327
3328 if (!ok)
3329 os.setstate(std::ios_base::failbit);
3330
3331 return os;
3332 }
3333
3334
3335 template <class CharT, class Traits, class Allocator>
3336 std::basic_istream<CharT, Traits>&
3337 operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3338 {
3339 typename std::basic_istream<CharT, Traits>::sentry sentry(is);
3340
3341 if (sentry) {
3342 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3343 const std::ctype<CharT>& ctype = std::use_facet<std::ctype<CharT> >(is.getloc());
3344
3345 s.clear();
3346 std::size_t n = is.width(0);
3347 if (n == 0)
3348 n = static_cast<std::size_t>(-1);
3349 else
3350 s.reserve(n);
3351
3352 while (n-- > 0) {
3353 typename Traits::int_type c1 = buf->sbumpc();
3354
3355 if (Traits::eq_int_type(c1, Traits::eof())) {
3356 is.setstate(std::ios_base::eofbit);
3357 break;
3358 }
3359 else {
3360 CharT c = Traits::to_char_type(c1);
3361
3362 if (ctype.is(std::ctype<CharT>::space, c)) {
3363 if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof()))
3364 is.setstate(std::ios_base::failbit);
3365 break;
3366 }
3367 else
3368 s.push_back(c);
3369 }
3370 }
3371
3372 // If we have read no characters, then set failbit.
3373 if (s.size() == 0)
3374 is.setstate(std::ios_base::failbit);
3375 }
3376 else
3377 is.setstate(std::ios_base::failbit);
3378
3379 return is;
3380 }
3381
3382 template <class CharT, class Traits, class Allocator>
3383 std::basic_istream<CharT, Traits>&
3384 getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
3385 {
3386 typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
3387 typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
3388 if (sentry) {
3389 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
3390 s.clear();
3391
3392 while (nread < s.max_size()) {
3393 int c1 = buf->sbumpc();
3394 if (Traits::eq_int_type(c1, Traits::eof())) {
3395 is.setstate(std::ios_base::eofbit);
3396 break;
3397 }
3398 else {
3399 ++nread;
3400 CharT c = Traits::to_char_type(c1);
3401 if (!Traits::eq(c, delim))
3402 s.push_back(c);
3403 else
3404 break; // Character is extracted but not appended.
3405 }
3406 }
3407 }
3408 if (nread == 0 || nread >= s.max_size())
3409 is.setstate(std::ios_base::failbit);
3410
3411 return is;
3412 }
3413
3414 template <class CharT, class Traits, class Allocator>
3415 inline std::basic_istream<CharT, Traits>&
3416 getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
3417 {
3418 return getline(is, s, '\n');
3419 }
3420
3421 template <class Ch, class Allocator>
3422 inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, Allocator> const& v)
3423 {
3424 return hash_range(v.begin(), v.end());
3425 }
3426
3427 }}
3428
3429 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3430
3431 namespace boost {
3432
3433 //!has_trivial_destructor_after_move<> == true_type
3434 //!specialization for optimizations
3435 template <class C, class T, class Allocator>
3436 struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
3437 {
3438 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3439 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3440 ::boost::has_trivial_destructor_after_move<pointer>::value;
3441 };
3442
3443 }
3444
3445 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3446
3447 #include <boost/container/detail/config_end.hpp>
3448
3449 #endif // BOOST_CONTAINER_STRING_HPP