-
Notifications
You must be signed in to change notification settings - Fork 117
/
string.hpp
3585 lines (3192 loc) · 132 KB
/
string.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_CONTAINER_STRING_HPP
#define BOOST_CONTAINER_STRING_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/container_fwd.hpp>
// container
#include <boost/container/allocator_traits.hpp>
#include <boost/container/new_allocator.hpp> //new_allocator
#include <boost/container/throw_exception.hpp>
// container/detail
#include <boost/container/detail/alloc_helpers.hpp>
#include <boost/container/detail/allocator_version_traits.hpp>
#include <boost/container/detail/allocation_type.hpp>
#include <boost/container/detail/iterator.hpp>
#include <boost/container/detail/iterators.hpp>
#include <boost/container/detail/min_max.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/next_capacity.hpp>
#include <boost/move/detail/to_raw_pointer.hpp>
#include <boost/container/detail/version_type.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/detail/algorithm.hpp>
#include <boost/container/detail/minimal_char_traits_header.hpp> // for char_traits
//intrusive
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/intrusive/detail/hash_combine.hpp>
#include <boost/move/detail/force_ptr.hpp>
//move
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/traits.hpp>
#include <iosfwd>
#include <istream> //
#include <ostream>
#include <ios>
#include <locale>
#include <cstddef>
#include <climits>
//std
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
#include <initializer_list> //for std::initializer_list
#endif
//GCC 12 has a regression for array-bounds warnings
#if defined(BOOST_GCC) && (BOOST_GCC >= 120000) && (BOOST_GCC < 130000)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#endif
namespace boost {
namespace container {
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
namespace dtl {
// ------------------------------------------------------------
// Class basic_string_base.
// basic_string_base is a helper class that makes it it easier to write
// an exception-safe version of basic_string. The constructor allocates,
// but does not initialize, a block of memory. The destructor
// deallocates, but does not destroy elements within, a block of
// memory. The destructor assumes that the memory either is the internal buffer,
// or else points to a block of memory that was allocated using string_base's
// allocator and whose size is this->m_storage.
template <class Allocator>
class basic_string_base
{
basic_string_base & operator=(const basic_string_base &);
basic_string_base(const basic_string_base &);
typedef Allocator allocator_type;
public:
typedef allocator_traits<allocator_type> allocator_traits_type;
typedef allocator_type stored_allocator_type;
typedef typename allocator_traits_type::pointer pointer;
typedef typename allocator_traits_type::value_type value_type;
typedef typename allocator_traits_type::size_type size_type;
typedef typename allocator_traits_type::difference_type difference_type;
typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
inline basic_string_base()
: members_()
{}
inline explicit basic_string_base(const allocator_type& a)
: members_(a)
{}
inline explicit basic_string_base(BOOST_RV_REF(allocator_type) a)
: members_(boost::move(a))
{}
inline basic_string_base(const allocator_type& a, size_type n)
: members_(a)
{
this->allocate_initial_block(n);
}
inline explicit basic_string_base(size_type n)
: members_()
{
this->allocate_initial_block(n);
}
inline ~basic_string_base()
{
if(!this->is_short()){
this->deallocate(this->priv_long_addr(), this->priv_long_storage());
}
}
private:
#if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
//This is the structure controlling a long string
struct long_t
{
size_type is_short : 1;
size_type length : (sizeof(size_type)*CHAR_BIT - 1);
size_type storage;
pointer start;
inline long_t()
: is_short(0)
{}
inline long_t(size_type len, size_type stor, pointer ptr)
: is_short(0), length(len), storage(stor), start(ptr)
{}
inline long_t(const long_t &other)
{
this->is_short = false;
length = other.length;
storage = other.storage;
start = other.start;
}
inline long_t &operator= (const long_t &other)
{
length = other.length;
storage = other.storage;
start = other.start;
return *this;
}
};
#if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
#pragma GCC diagnostic pop
#endif
//This type is the first part of the structure controlling a short string
//The "data" member stores
struct short_header
{
unsigned char is_short : 1;
unsigned char length : (CHAR_BIT - 1);
};
//This type has the same alignment and size as long_t but it's POD
//so, unlike long_t, it can be placed in a union
typedef typename dtl::aligned_storage
<sizeof(long_t), dtl::alignment_of<long_t>::value>::type long_raw_t;
protected:
static const size_type MinInternalBufferChars = 8;
static const size_type AlignmentOfValueType =
alignment_of<value_type>::value;
static const size_type ShortDataOffset = ((sizeof(short_header)-1)/AlignmentOfValueType+1)*AlignmentOfValueType;
static const size_type ZeroCostInternalBufferChars =
(sizeof(long_t) - ShortDataOffset)/sizeof(value_type);
static const size_type UnalignedFinalInternalBufferChars =
(ZeroCostInternalBufferChars > MinInternalBufferChars) ?
ZeroCostInternalBufferChars : MinInternalBufferChars;
struct short_t
{
short_header h;
value_type data[UnalignedFinalInternalBufferChars];
};
union repr_t_size_t
{
long_raw_t r;
short_t s;
};
union repr_t
{
long_raw_t r_aligner;
short_t s_aligner;
unsigned char data[sizeof(repr_t_size_t)];
};
struct members_holder
: public allocator_type
{
inline void init()
{
short_t &s = *::new(this->m_repr.data) short_t;
s.h.is_short = 1;
s.h.length = 0;
}
inline members_holder()
: allocator_type()
{ this->init(); }
template<class AllocatorConvertible>
inline explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
: allocator_type(boost::forward<AllocatorConvertible>(a))
{ this->init(); }
inline const short_t *pshort_repr() const
{ return move_detail::force_ptr<const short_t*>(m_repr.data); }
inline const long_t *plong_repr() const
{ return move_detail::force_ptr<const long_t*>(m_repr.data); }
inline short_t *pshort_repr()
{ return move_detail::force_ptr<short_t*>(m_repr.data); }
inline long_t *plong_repr()
{ return move_detail::force_ptr<long_t*>(m_repr.data); }
repr_t m_repr;
} members_;
inline const allocator_type &alloc() const
{ return members_; }
inline allocator_type &alloc()
{ return members_; }
static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
private:
static const size_type MinAllocation = InternalBufferChars*2;
protected:
inline bool is_short() const
{
//Access and copy (to avoid UB) the first byte of the union to know if the
//active representation is short or long
short_header hdr;
BOOST_CONTAINER_STATIC_ASSERT((sizeof(short_header) == 1));
*(unsigned char*)&hdr = *(unsigned char*)&this->members_.m_repr;
return hdr.is_short != 0;
}
inline short_t *construct_short()
{
short_t *ps = ::new(this->members_.m_repr.data) short_t;
ps->h.is_short = 1;
return ps;
}
inline void destroy_short()
{
BOOST_ASSERT(this->is_short());
this->members_.pshort_repr()->~short_t();
}
short_t *assure_short()
{
if (!this->is_short()){
this->destroy_long();
return construct_short();
}
return this->members_.pshort_repr();
}
inline long_t *construct_long()
{
long_t *pl = ::new(this->members_.m_repr.data) long_t;
//is_short flag is written in the constructor
return pl;
}
inline void destroy_long()
{
BOOST_ASSERT(!this->is_short());
this->members_.plong_repr()->~long_t();
}
long_t *assure_long()
{
if (this->is_short()){
this->destroy_short();
return this->construct_long();
}
return this->members_.plong_repr();
}
protected:
typedef dtl::integral_constant<unsigned,
boost::container::dtl::version<allocator_type>::value> alloc_version;
pointer allocation_command(allocation_type command,
size_type limit_size,
size_type &prefer_in_recvd_out_size,
pointer &reuse)
{
if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
reuse = 0;
command &= ~(expand_fwd | expand_bwd);
}
return dtl::allocator_version_traits<allocator_type>::allocation_command
(this->alloc(), command, limit_size, prefer_in_recvd_out_size, reuse);
}
size_type next_capacity(size_type additional_objects) const
{
return growth_factor_100()
( this->priv_storage(), additional_objects, allocator_traits_type::max_size(this->alloc()));
}
void deallocate(pointer p, size_type n)
{
if (p && (n > InternalBufferChars))
this->alloc().deallocate(p, n);
}
void destroy(pointer p, size_type n)
{
value_type *raw_p = boost::movelib::to_raw_pointer(p);
for(; n--; ++raw_p){
allocator_traits_type::destroy( this->alloc(), raw_p);
}
}
inline void destroy(pointer p)
{
allocator_traits_type::destroy
( this->alloc()
, boost::movelib::to_raw_pointer(p)
);
}
void allocate_initial_block(size_type n)
{
if (n <= this->max_size()) {
if(n > InternalBufferChars){
size_type new_cap = this->next_capacity(n);
pointer reuse = 0;
pointer p = this->allocation_command(allocate_new, n, new_cap, reuse);
BOOST_ASSERT(this->is_short());
this->construct_long();
this->priv_long_addr(p);
this->priv_long_size(0);
this->priv_storage(new_cap);
}
}
else{
throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
}
}
inline void deallocate_block()
{ this->deallocate(this->priv_addr(), this->priv_storage()); }
inline size_type max_size() const
{ return allocator_traits_type::max_size(this->alloc()) - 1; }
protected:
inline size_type priv_capacity() const
{ return this->priv_storage() - 1; }
inline pointer priv_short_addr() const
{ return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.pshort_repr()->data[0])); }
//GCC seems a bit confused about uninitialized accesses
#if defined(BOOST_GCC) && (BOOST_GCC >= 40700)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
inline pointer priv_long_addr() const
{ return this->members_.plong_repr()->start; }
inline pointer priv_addr() const
{
return this->is_short()
? priv_short_addr()
: priv_long_addr()
;
}
inline pointer priv_end_addr() const
{
return this->is_short()
? this->priv_short_addr() + difference_type(this->priv_short_size())
: this->priv_long_addr() + difference_type(this->priv_long_size())
;
}
inline void priv_long_addr(pointer addr)
{ this->members_.plong_repr()->start = addr; }
inline size_type priv_storage() const
{ return this->is_short() ? priv_short_storage() : priv_long_storage(); }
inline size_type priv_short_storage() const
{ return InternalBufferChars; }
inline size_type priv_long_storage() const
{ return this->members_.plong_repr()->storage; }
inline void priv_storage(size_type storage)
{
if(!this->is_short())
this->priv_long_storage(storage);
}
inline void priv_long_storage(size_type storage)
{
this->members_.plong_repr()->storage = storage;
}
inline size_type priv_size() const
{ return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
inline size_type priv_short_size() const
{ return this->members_.pshort_repr()->h.length; }
inline size_type priv_long_size() const
{ return this->members_.plong_repr()->length; }
inline void priv_size(size_type sz)
{
if(this->is_short())
this->priv_short_size(sz);
else
this->priv_long_size(sz);
}
inline void priv_short_size(size_type sz)
{
typedef unsigned char uchar_type;
static const uchar_type mask = uchar_type(uchar_type(-1) >> 1U);
BOOST_ASSERT( sz <= mask );
//Make -Wconversion happy
this->members_.pshort_repr()->h.length = uchar_type(uchar_type(sz) & mask);
}
inline void priv_long_size(size_type sz)
{
static const size_type mask = size_type(-1) >> 1U;
BOOST_ASSERT( sz <= mask );
//Make -Wconversion happy
this->members_.plong_repr()->length = sz & mask;
}
#if defined(BOOST_GCC) && (BOOST_GCC >= 40700)
#pragma GCC diagnostic pop
#endif
void swap_data(basic_string_base& other)
{
if(this->is_short()){
if(other.is_short()){
repr_t tmp(this->members_.m_repr);
this->members_.m_repr = other.members_.m_repr;
other.members_.m_repr = tmp;
}
else{
short_t short_backup(*this->members_.pshort_repr());
this->members_.pshort_repr()->~short_t();
::new(this->members_.plong_repr()) long_t(*other.members_.plong_repr());
other.members_.plong_repr()->~long_t();
::new(other.members_.pshort_repr()) short_t(short_backup);
}
}
else{
if(other.is_short()){
short_t short_backup(*other.members_.pshort_repr());
other.members_.pshort_repr()->~short_t();
::new(other.members_.plong_repr()) long_t(*this->members_.plong_repr());
this->members_.plong_repr()->~long_t();
::new(this->members_.pshort_repr()) short_t(short_backup);
}
else{
boost::adl_move_swap(*this->members_.plong_repr(), *other.members_.plong_repr());
}
}
}
};
} //namespace dtl {
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
//! The basic_string class represents a Sequence of characters. It contains all the
//! usual operations of a Sequence, and, additionally, it contains standard string
//! operations such as search and concatenation.
//!
//! The basic_string class is parameterized by character type, and by that type's
//! Character Traits.
//!
//! This class has performance characteristics very much like vector<>, meaning,
//! for example, that it does not perform reference-count or copy-on-write, and that
//! concatenation of two strings is an O(N) operation.
//!
//! Some of basic_string's member functions use an unusual method of specifying positions
//! and ranges. In addition to the conventional method using iterators, many of
//! basic_string's member functions use a single value pos of type size_type to represent a
//! position (in which case the position is begin() + pos, and many of basic_string's
//! member functions use two values, pos and n, to represent a range. In that case pos is
//! the beginning of the range and n is its size. That is, the range is
//! [begin() + pos, begin() + pos + n).
//!
//! Note that the C++ standard does not specify the complexity of basic_string operations.
//! In this implementation, basic_string has performance characteristics very similar to
//! those of vector: access to a single character is O(1), while copy and concatenation
//! are O(N).
//!
//! In this implementation, begin(),
//! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
//! In this implementation, iterators are only invalidated by member functions that
//! explicitly change the string's contents.
//!
//! \tparam CharT The type of character it contains.
//! \tparam Traits The Character Traits type, which encapsulates basic character operations
//! \tparam Allocator The allocator, used for internal memory management.
#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = void >
#else
template <class CharT, class Traits, class Allocator>
#endif
class basic_string
: private dtl::basic_string_base<typename real_allocator<CharT, Allocator>::type>
{
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
BOOST_COPYABLE_AND_MOVABLE(basic_string)
typedef dtl::basic_string_base<typename real_allocator<CharT, Allocator>::type> base_t;
typedef typename base_t::allocator_traits_type allocator_traits_type;
static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
protected:
// Allocator helper class to use a char_traits as a function object.
template <class Tr>
struct Eq_traits
{
//Compatibility with std::binary_function
typedef typename Tr::char_type first_argument_type;
typedef typename Tr::char_type second_argument_type;
typedef bool result_type;
bool operator()(const first_argument_type& x, const second_argument_type& y) const
{ return Tr::eq(x, y); }
};
template <class Tr>
struct Not_within_traits
{
typedef typename Tr::char_type argument_type;
typedef bool result_type;
typedef const typename Tr::char_type* Pointer;
const Pointer m_first;
const Pointer m_last;
Not_within_traits(Pointer f, Pointer l)
: m_first(f), m_last(l) {}
bool operator()(const typename Tr::char_type& x) const
{
return boost::container::find_if(m_first, m_last,
boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
}
};
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public:
//////////////////////////////////////////////
//
// types
//
//////////////////////////////////////////////
typedef Traits traits_type;
typedef CharT value_type;
typedef typename real_allocator<CharT, Allocator>::type allocator_type;
typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
typedef typename ::boost::container::allocator_traits<allocator_type>::const_pointer const_pointer;
typedef typename ::boost::container::allocator_traits<allocator_type>::reference reference;
typedef typename ::boost::container::allocator_traits<allocator_type>::const_reference const_reference;
typedef typename ::boost::container::allocator_traits<allocator_type>::size_type size_type;
typedef typename ::boost::container::allocator_traits<allocator_type>::difference_type difference_type;
typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
static const size_type npos = size_type(-1);
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
typedef constant_iterator<CharT> cvalue_iterator;
typedef typename base_t::alloc_version alloc_version;
typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public: // Constructor, destructor, assignment.
//////////////////////////////////////////////
//
// construct/copy/destroy
//
//////////////////////////////////////////////
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
struct reserve_t {};
basic_string(reserve_t, size_type n,
const allocator_type& a = allocator_type())
//Select allocator as in copy constructor as reserve_t-based constructors
//are two step copies optimized for capacity
: base_t( allocator_traits_type::select_on_container_copy_construction(a)
, n + 1)
{ this->priv_terminate_string(); }
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
//! <b>Effects</b>: Default constructs a basic_string.
//!
//! <b>Throws</b>: If allocator_type's default constructor throws.
basic_string() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
: base_t()
{ this->priv_terminate_string(); }
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
//!
//! <b>Throws</b>: Nothing
explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
: base_t(a)
{ this->priv_terminate_string(); }
//! <b>Effects</b>: Copy constructs a basic_string.
//!
//! <b>Postcondition</b>: x == *this.
//!
//! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
basic_string(const basic_string& s)
: base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
{
this->priv_terminate_string();
this->assign(s.begin(), s.end());
}
//! <b>Effects</b>: Same as basic_string(sv.data(), sv.size(), a).
//!
//! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
template<template <class, class> class BasicStringView>
explicit basic_string(BasicStringView<CharT, Traits> sv, const allocator_type& a = allocator_type())
: base_t(allocator_traits_type::select_on_container_copy_construction(a))
{
this->priv_terminate_string();
this->assign(sv);
}
//! <b>Effects</b>: Move constructor. Moves s's resources to *this.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW
: base_t(boost::move(s.alloc()))
{
if(s.alloc() == this->alloc()){
this->swap_data(s);
}
else{
this->assign(s.begin(), s.end());
}
}
//! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
//!
//! <b>Postcondition</b>: x == *this.
//!
//! <b>Throws</b>: If allocation throws.
basic_string(const basic_string& s, const allocator_type &a)
: base_t(a)
{
this->priv_terminate_string();
this->assign(s.begin(), s.end());
}
//! <b>Effects</b>: Move constructor using the specified allocator.
//! Moves s's resources to *this.
//!
//! <b>Throws</b>: If allocation throws.
//!
//! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
: base_t(a)
{
this->priv_terminate_string();
if(s.alloc() == this->alloc()){
this->swap_data(s);
}
else{
this->assign(s.begin(), s.end());
}
}
//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
//! and is initialized by a specific number of characters of the s string.
basic_string(const basic_string& s, size_type pos, size_type n = npos)
: base_t()
{
this->priv_terminate_string();
if (pos > s.size())
throw_out_of_range("basic_string::basic_string out of range position");
else
this->assign
(s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by a specific number of characters of the s string.
basic_string(const basic_string& s, size_type pos, size_type n, const allocator_type& a)
: base_t(a)
{
this->priv_terminate_string();
if (pos > s.size())
throw_out_of_range("basic_string::basic_string out of range position");
else
this->assign
(s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
}
//! <b>Effects</b>: Constructs a basic_string taking a default-constructed allocator,
//! and is initialized by a specific number of characters of the s c-string.
basic_string(const CharT* s, size_type n)
: base_t()
{
this->priv_terminate_string();
this->assign(s, s + difference_type(n));
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by a specific number of characters of the s c-string.
basic_string(const CharT* s, size_type n, const allocator_type& a)
: base_t(a)
{
this->priv_terminate_string();
this->assign(s, s + difference_type(n));
}
//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
//! and is initialized by the null-terminated s c-string.
basic_string(const CharT* s)
: base_t()
{
this->priv_terminate_string();
this->assign(s, s + Traits::length(s));
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by the null-terminated s c-string.
basic_string(const CharT* s, const allocator_type& a)
: base_t(a)
{
this->priv_terminate_string();
this->assign(s, s + Traits::length(s));
}
//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
//! and is initialized by n copies of c.
basic_string(size_type n, CharT c)
: base_t()
{
this->priv_terminate_string();
this->assign(n, c);
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by n copies of c.
basic_string(size_type n, CharT c, const allocator_type& a)
: base_t(a)
{
this->priv_terminate_string();
this->assign(n, c);
}
//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
//! and is initialized by n default-initialized characters.
basic_string(size_type n, default_init_t)
: base_t(n + 1)
{
this->priv_size(n);
this->priv_terminate_string();
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and is initialized by n default-initialized characters.
basic_string(size_type n, default_init_t, const allocator_type& a)
: base_t(a, n + 1)
{
this->priv_size(n);
this->priv_terminate_string();
}
//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
//! and a range of iterators.
template <class InputIterator>
basic_string(InputIterator f, InputIterator l)
: base_t()
{
this->priv_terminate_string();
this->assign(f, l);
}
//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
//! and a range of iterators.
template <class InputIterator>
basic_string(InputIterator f, InputIterator l, const allocator_type& a)
: base_t(a)
{
this->priv_terminate_string();
this->assign(f, l);
}
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: Same as basic_string(il.begin(), il.end(), a).
//!
basic_string(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
: base_t(a)
{
this->priv_terminate_string();
this->assign(il.begin(), il.end());
}
#endif
//! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
~basic_string() BOOST_NOEXCEPT_OR_NOTHROW
{}
//! <b>Effects</b>: Copy constructs a string.
//!
//! <b>Postcondition</b>: x == *this.
//!
//! <b>Complexity</b>: Linear to the elements x contains.
basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
{
if (BOOST_LIKELY(this != &x)) {
allocator_type &this_alloc = this->alloc();
const allocator_type &x_alloc = x.alloc();
dtl::bool_<allocator_traits_type::
propagate_on_container_copy_assignment::value> flag;
if(flag && this_alloc != x_alloc){
if(!this->is_short()){
this->deallocate_block();
this->assure_short();
Traits::assign(*this->priv_addr(), CharT(0));
this->priv_short_size(0);
}
}
dtl::assign_alloc(this->alloc(), x.alloc(), flag);
this->assign(x.begin(), x.end());
}
return *this;
}
//! <b>Effects</b>: Move constructor. Moves x's resources to *this.
//!
//! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
//! is false and allocation throws
//!
//! <b>Complexity</b>: Constant if allocator_traits_type::
//! propagate_on_container_move_assignment is true or
//! this->get>allocator() == x.get_allocator(). Linear otherwise.
basic_string& operator=(BOOST_RV_REF(basic_string) x)
BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
|| allocator_traits_type::is_always_equal::value)
{
if (BOOST_LIKELY(this != &x)) {
allocator_type &this_alloc = this->alloc();
allocator_type &x_alloc = x.alloc();
const bool propagate_alloc = allocator_traits_type::
propagate_on_container_move_assignment::value;
dtl::bool_<propagate_alloc> flag;
const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
//Resources can be transferred if both allocators are
//going to be equal after this function (either propagated or already equal)
if(propagate_alloc || allocators_equal){
//Destroy objects but retain memory in case x reuses it in the future
this->clear();
//Move allocator if needed
dtl::move_alloc(this_alloc, x_alloc, flag);
//Nothrow swap
this->swap_data(x);
}
//Else do a one by one move
else{
this->assign( x.begin(), x.end());
}
}
return *this;
}
//! <b>Effects</b>: Assignment from a null-terminated c-string.
//!
basic_string& operator=(const CharT* s)
{ return this->assign(s, s + Traits::length(s)); }
//! <b>Effects</b>: Returns *this = basic_string(1, c).
//!
basic_string& operator=(CharT c)
{ return this->assign(static_cast<size_type>(1), c); }
//! <b>Effects</b>: Equivalent to return assign(sv).
//!
template<template <class, class> class BasicStringView>
basic_string& operator=(BasicStringView<CharT, Traits> sv)
{ return this->assign(sv.data(), sv.size()); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: Returns *this = basic_string(il);
//!
basic_string& operator=(std::initializer_list<CharT> il)
{
return this->assign(il.begin(), il.end());
}
#endif
//! <b>Effects</b>: Returns a copy of the internal allocator.
//!
//! <b>Throws</b>: If allocator's copy constructor throws.
//!
//! <b>Complexity</b>: Constant.
BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
{ return this->alloc(); }
//! <b>Effects</b>: Returns a reference to the internal allocator.
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
//!
//! <b>Note</b>: Non-standard extension.
BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
{ return this->alloc(); }
//! <b>Effects</b>: Returns a reference to the internal allocator.
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
//!
//! <b>Note</b>: Non-standard extension.
BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
{ return this->alloc(); }
//////////////////////////////////////////////
//
// iterators
//
//////////////////////////////////////////////