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