forked from karottc/sgi-stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string
2413 lines (2026 loc) · 74.4 KB
/
string
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
/*
* Copyright (c) 1997-1999
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
#ifndef __SGI_STL_STRING
#define __SGI_STL_STRING
#include <stl_config.h>
#include <stl_string_fwd.h>
#include <ctype.h>
#include <functional>
#include <stl_ctraits_fns.h>
#include <stdexcept>
#include <stl_iterator_base.h>
#include <memory>
#include <algorithm>
#ifdef __STL_USE_NEW_IOSTREAMS
#include <iosfwd>
#else /* __STL_USE_NEW_IOSTREAMS */
#include <char_traits.h>
#endif /* __STL_USE_NEW_IOSTREAMS */
// Standard C++ string class. 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.
// There are three reasons why basic_string is not identical to
// vector. First, basic_string always stores a null character at the
// end; this makes it possible for c_str to be a fast operation.
// Second, the C++ standard requires basic_string to copy elements
// using char_traits<>::assign, char_traits<>::copy, and
// char_traits<>::move. This means that all of vector<>'s low-level
// operations must be rewritten. Third, basic_string<> has a lot of
// extra functions in its interface that are convenient but, strictly
// speaking, redundant.
// Additionally, the C++ standard imposes a major restriction: according
// to the standard, the character type _CharT must be a POD type. This
// implementation weakens that restriction, and allows _CharT to be a
// a user-defined non-POD type. However, _CharT must still have a
// default constructor.
__STL_BEGIN_NAMESPACE
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#pragma set woff 1375
#endif
// A helper class to use a char_traits as a function object.
template <class _Traits>
struct _Not_within_traits
: public unary_function<typename _Traits::char_type, bool>
{
typedef const typename _Traits::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 _Traits::char_type& __x) const {
return find_if(_M_first, _M_last,
bind1st(_Eq_traits<_Traits>(), __x)) == _M_last;
}
};
// ------------------------------------------------------------
// Class _String_base.
// _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 _M_start either is null, or else
// points to a block of memory that was allocated using _String_base's
// allocator and whose size is _M_end_of_storage - _M_start.
// Additionally, _String_base encapsulates the difference between
// old SGI-style allocators and standard-conforming allocators.
#ifdef __STL_USE_STD_ALLOCATORS
// General base class.
template <class _Tp, class _Alloc, bool _S_instanceless>
class _String_alloc_base {
public:
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
allocator_type get_allocator() const { return _M_data_allocator; }
_String_alloc_base(const allocator_type& __a)
: _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
{}
protected:
_Tp* _M_allocate(size_t __n)
{ return _M_data_allocator.allocate(__n); }
void _M_deallocate(_Tp* __p, size_t __n) {
if (__p)
_M_data_allocator.deallocate(__p, __n);
}
protected:
allocator_type _M_data_allocator;
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;
};
// Specialization for instanceless allocators.
template <class _Tp, class _Alloc>
class _String_alloc_base<_Tp,_Alloc,true> {
public:
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
allocator_type get_allocator() const { return allocator_type(); }
_String_alloc_base(const allocator_type&)
: _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
protected:
typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Alloc_type;
_Tp* _M_allocate(size_t __n)
{ return _Alloc_type::allocate(__n); }
void _M_deallocate(_Tp* __p, size_t __n)
{ _Alloc_type::deallocate(__p, __n); }
protected:
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;
};
template <class _Tp, class _Alloc>
class _String_base
: public _String_alloc_base<_Tp, _Alloc,
_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
{
protected:
typedef _String_alloc_base<_Tp, _Alloc,
_Alloc_traits<_Tp, _Alloc>::_S_instanceless>
_Base;
typedef typename _Base::allocator_type allocator_type;
void _M_allocate_block(size_t __n) {
if (__n <= max_size()) {
_M_start = _M_allocate(__n);
_M_finish = _M_start;
_M_end_of_storage = _M_start + __n;
}
else
_M_throw_length_error();
}
void _M_deallocate_block()
{ _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
_String_base(const allocator_type& __a) : _Base(__a) { }
_String_base(const allocator_type& __a, size_t __n) : _Base(__a)
{ _M_allocate_block(__n); }
~_String_base() { _M_deallocate_block(); }
void _M_throw_length_error() const;
void _M_throw_out_of_range() const;
};
#else /* __STL_USE_STD_ALLOCATORS */
template <class _Tp, class _Alloc> class _String_base {
public:
typedef _Alloc allocator_type;
allocator_type get_allocator() const { return allocator_type(); }
protected:
typedef simple_alloc<_Tp, _Alloc> _Alloc_type;
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;
// Precondition: 0 < __n <= max_size().
_Tp* _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); }
void _M_deallocate(_Tp* __p, size_t __n) {
if (__p)
_Alloc_type::deallocate(__p, __n);
}
void _M_allocate_block(size_t __n) {
if (__n <= max_size()) {
_M_start = _M_allocate(__n);
_M_finish = _M_start;
_M_end_of_storage = _M_start + __n;
}
else
_M_throw_length_error();
}
void _M_deallocate_block()
{ _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
_String_base(const allocator_type&)
: _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
_String_base(const allocator_type&, size_t __n)
: _M_start(0), _M_finish(0), _M_end_of_storage(0)
{ _M_allocate_block(__n); }
~_String_base() { _M_deallocate_block(); }
void _M_throw_length_error() const;
void _M_throw_out_of_range() const;
};
#endif /* __STL_USE_STD_ALLOCATORS */
// Helper functions for exception handling.
template <class _Tp, class _Alloc>
void _String_base<_Tp,_Alloc>::_M_throw_length_error() const {
__STL_THROW(length_error("basic_string"));
}
template <class _Tp, class _Alloc>
void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const {
__STL_THROW(out_of_range("basic_string"));
}
// ------------------------------------------------------------
// Class basic_string.
// Class invariants:
// (1) [start, finish) is a valid range.
// (2) Each iterator in [start, finish) points to a valid object
// of type value_type.
// (3) *finish is a valid object of type value_type; in particular,
// it is value_type().
// (4) [finish + 1, end_of_storage) is a valid range.
// (5) Each iterator in [finish + 1, end_of_storage) points to
// unininitialized memory.
// Note one important consequence: a string of length n must manage
// a block of memory whose size is at least n + 1.
template <class _CharT, class _Traits, class _Alloc>
class basic_string : private _String_base<_CharT,_Alloc> {
public:
typedef _CharT value_type;
typedef _Traits traits_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef const value_type* const_iterator;
typedef value_type* iterator;
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef reverse_iterator<const_iterator> const_reverse_iterator;
typedef reverse_iterator<iterator> reverse_iterator;
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
typedef reverse_iterator<const_iterator, value_type, const_reference,
difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type, reference, difference_type>
reverse_iterator;
#endif /* __STL_PARTIAL_SPECIALIZATION */
static const size_type npos;
typedef _String_base<_CharT,_Alloc> _Base;
public: // Constructor, destructor, assignment.
typedef typename _Base::allocator_type allocator_type;
allocator_type get_allocator() const { return _Base::get_allocator(); }
explicit basic_string(const allocator_type& __a = allocator_type())
: _Base(__a, 8) { _M_terminate_string(); }
struct _Reserve_t {};
basic_string(_Reserve_t, size_t __n,
const allocator_type& __a = allocator_type())
: _Base(__a, __n + 1) { _M_terminate_string(); }
basic_string(const basic_string& __s) : _Base(__s.get_allocator())
{ _M_range_initialize(__s.begin(), __s.end()); }
basic_string(const basic_string& __s, size_type __pos, size_type __n = npos,
const allocator_type& __a = allocator_type())
: _Base(__a) {
if (__pos > __s.size())
_M_throw_out_of_range();
else
_M_range_initialize(__s.begin() + __pos,
__s.begin() + __pos + min(__n, __s.size() - __pos));
}
basic_string(const _CharT* __s, size_type __n,
const allocator_type& __a = allocator_type())
: _Base(__a)
{ _M_range_initialize(__s, __s + __n); }
basic_string(const _CharT* __s,
const allocator_type& __a = allocator_type())
: _Base(__a)
{ _M_range_initialize(__s, __s + _Traits::length(__s)); }
basic_string(size_type __n, _CharT __c,
const allocator_type& __a = allocator_type())
: _Base(__a, __n + 1)
{
_M_finish = uninitialized_fill_n(_M_start, __n, __c);
_M_terminate_string();
}
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIterator>
basic_string(_InputIterator __f, _InputIterator __l,
const allocator_type& __a = allocator_type())
: _Base(__a)
{
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_initialize_dispatch(__f, __l, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
basic_string(const _CharT* __f, const _CharT* __l,
const allocator_type& __a = allocator_type())
: _Base(__a)
{
_M_range_initialize(__f, __l);
}
#endif
~basic_string() { destroy(_M_start, _M_finish + 1); }
basic_string& operator=(const basic_string& __s) {
if (&__s != this)
assign(__s.begin(), __s.end());
return *this;
}
basic_string& operator=(const _CharT* __s)
{ return assign(__s, __s + _Traits::length(__s)); }
basic_string& operator=(_CharT __c)
{ return assign(static_cast<size_type>(1), __c); }
protected: // Protected members inherited from base.
#ifdef __STL_HAS_NAMESPACES
using _Base::_M_allocate;
using _Base::_M_deallocate;
using _Base::_M_allocate_block;
using _Base::_M_deallocate_block;
using _Base::_M_throw_length_error;
using _Base::_M_throw_out_of_range;
using _Base::_M_start;
using _Base::_M_finish;
using _Base::_M_end_of_storage;
#endif /* __STL_HAS_NAMESPACES */
private: // Helper functions used by constructors
// and elsewhere.
void _M_construct_null(_CharT* __p) {
construct(__p);
# ifdef __STL_DEFAULT_CONSTRUCTOR_BUG
__STL_TRY {
*__p = (_CharT) 0;
}
__STL_UNWIND(destroy(__p));
# endif
}
static _CharT _M_null() {
# ifndef __STL_DEFAULT_CONSTRUCTOR_BUG
return _CharT();
# else
return (_CharT) 0;
# endif
}
private:
// Helper functions used by constructors. It is a severe error for
// any of them to be called anywhere except from within constructors.
void _M_terminate_string() {
__STL_TRY {
_M_construct_null(_M_finish);
}
__STL_UNWIND(destroy(_M_start, _M_finish));
}
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
void _M_range_initialize(_InputIter __f, _InputIter __l,
input_iterator_tag) {
_M_allocate_block(8);
_M_construct_null(_M_finish);
__STL_TRY {
append(__f, __l);
}
__STL_UNWIND(destroy(_M_start, _M_finish + 1));
}
template <class _ForwardIter>
void _M_range_initialize(_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag) {
difference_type __n = 0;
distance(__f, __l, __n);
_M_allocate_block(__n + 1);
_M_finish = uninitialized_copy(__f, __l, _M_start);
_M_terminate_string();
}
template <class _InputIter>
void _M_range_initialize(_InputIter __f, _InputIter __l) {
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
_M_range_initialize(__f, __l, _Category());
}
template <class _Integer>
void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
_M_allocate_block(__n + 1);
_M_finish = uninitialized_fill_n(_M_start, __n, __x);
_M_terminate_string();
}
template <class _InputIter>
void _M_initialize_dispatch(_InputIter __f, _InputIter __l, __false_type) {
_M_range_initialize(__f, __l);
}
#else /* __STL_MEMBER_TEMPLATES */
void _M_range_initialize(const _CharT* __f, const _CharT* __l) {
ptrdiff_t __n = __l - __f;
_M_allocate_block(__n + 1);
_M_finish = uninitialized_copy(__f, __l, _M_start);
_M_terminate_string();
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Iterators.
iterator begin() { return _M_start; }
iterator end() { return _M_finish; }
const_iterator begin() const { return _M_start; }
const_iterator end() const { return _M_finish; }
reverse_iterator rbegin()
{ return reverse_iterator(_M_finish); }
reverse_iterator rend()
{ return reverse_iterator(_M_start); }
const_reverse_iterator rbegin() const
{ return const_reverse_iterator(_M_finish); }
const_reverse_iterator rend() const
{ return const_reverse_iterator(_M_start); }
public: // Size, capacity, etc.
size_type size() const { return _M_finish - _M_start; }
size_type length() const { return size(); }
size_t max_size() const { return _Base::max_size(); }
void resize(size_type __n, _CharT __c) {
if (__n <= size())
erase(begin() + __n, end());
else
append(__n - size(), __c);
}
void resize(size_type __n) { resize(__n, _M_null()); }
void reserve(size_type = 0);
size_type capacity() const { return (_M_end_of_storage - _M_start) - 1; }
void clear() {
if (!empty()) {
_Traits::assign(*_M_start, _M_null());
destroy(_M_start+1, _M_finish+1);
_M_finish = _M_start;
}
}
bool empty() const { return _M_start == _M_finish; }
public: // Element access.
const_reference operator[](size_type __n) const
{ return *(_M_start + __n); }
reference operator[](size_type __n)
{ return *(_M_start + __n); }
const_reference at(size_type __n) const {
if (__n >= size())
_M_throw_out_of_range();
return *(_M_start + __n);
}
reference at(size_type __n) {
if (__n >= size())
_M_throw_out_of_range();
return *(_M_start + __n);
}
public: // Append, operator+=, push_back.
basic_string& operator+=(const basic_string& __s) { return append(__s); }
basic_string& operator+=(const _CharT* __s) { return append(__s); }
basic_string& operator+=(_CharT __c) { push_back(__c); return *this; }
basic_string& append(const basic_string& __s)
{ return append(__s.begin(), __s.end()); }
basic_string& append(const basic_string& __s,
size_type __pos, size_type __n)
{
if (__pos > __s.size())
_M_throw_out_of_range();
return append(__s.begin() + __pos,
__s.begin() + __pos + min(__n, __s.size() - __pos));
}
basic_string& append(const _CharT* __s, size_type __n)
{ return append(__s, __s+__n); }
basic_string& append(const _CharT* __s)
{ return append(__s, __s + _Traits::length(__s)); }
basic_string& append(size_type __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
basic_string& append(_InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_append_dispatch(__first, __last, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
basic_string& append(const _CharT* __first, const _CharT* __last);
#endif /* __STL_MEMBER_TEMPLATES */
void push_back(_CharT __c) {
if (_M_finish + 1 == _M_end_of_storage)
reserve(size() + max(size(), static_cast<size_type>(1)));
_M_construct_null(_M_finish + 1);
_Traits::assign(*_M_finish, __c);
++_M_finish;
}
void pop_back() {
_Traits::assign(*(_M_finish - 1), _M_null());
destroy(_M_finish);
--_M_finish;
}
private: // Helper functions for append.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
basic_string& append(_InputIter __f, _InputIter __l, input_iterator_tag);
template <class _ForwardIter>
basic_string& append(_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag);
template <class _Integer>
basic_string& _M_append_dispatch(_Integer __n, _Integer __x, __true_type) {
return append((size_type) __n, (_CharT) __x);
}
template <class _InputIter>
basic_string& _M_append_dispatch(_InputIter __f, _InputIter __l,
__false_type) {
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
return append(__f, __l, _Category());
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Assign
basic_string& assign(const basic_string& __s)
{ return assign(__s.begin(), __s.end()); }
basic_string& assign(const basic_string& __s,
size_type __pos, size_type __n) {
if (__pos > __s.size())
_M_throw_out_of_range();
return assign(__s.begin() + __pos,
__s.begin() + __pos + min(__n, __s.size() - __pos));
}
basic_string& assign(const _CharT* __s, size_type __n)
{ return assign(__s, __s + __n); }
basic_string& assign(const _CharT* __s)
{ return assign(__s, __s + _Traits::length(__s)); }
basic_string& assign(size_type __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
basic_string& assign(_InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_assign_dispatch(__first, __last, _Integral());
}
#endif /* __STL_MEMBER_TEMPLATES */
basic_string& assign(const _CharT* __f, const _CharT* __l);
private: // Helper functions for assign.
#ifdef __STL_MEMBER_TEMPLATES
template <class _Integer>
basic_string& _M_assign_dispatch(_Integer __n, _Integer __x, __true_type) {
return assign((size_type) __n, (_CharT) __x);
}
template <class _InputIter>
basic_string& _M_assign_dispatch(_InputIter __f, _InputIter __l,
__false_type);
#endif /* __STL_MEMBER_TEMPLATES */
public: // Insert
basic_string& insert(size_type __pos, const basic_string& __s) {
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __s.size())
_M_throw_length_error();
insert(_M_start + __pos, __s.begin(), __s.end());
return *this;
}
basic_string& insert(size_type __pos, const basic_string& __s,
size_type __beg, size_type __n) {
if (__pos > size() || __beg > __s.size())
_M_throw_out_of_range();
size_type __len = min(__n, __s.size() - __beg);
if (size() > max_size() - __len)
_M_throw_length_error();
insert(_M_start + __pos,
__s.begin() + __beg, __s.begin() + __beg + __len);
return *this;
}
basic_string& insert(size_type __pos, const _CharT* __s, size_type __n) {
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __n)
_M_throw_length_error();
insert(_M_start + __pos, __s, __s + __n);
return *this;
}
basic_string& insert(size_type __pos, const _CharT* __s) {
if (__pos > size())
_M_throw_out_of_range();
size_type __len = _Traits::length(__s);
if (size() > max_size() - __len)
_M_throw_length_error();
insert(_M_start + __pos, __s, __s + __len);
return *this;
}
basic_string& insert(size_type __pos, size_type __n, _CharT __c) {
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __n)
_M_throw_length_error();
insert(_M_start + __pos, __n, __c);
return *this;
}
iterator insert(iterator __p, _CharT __c) {
if (__p == _M_finish) {
push_back(__c);
return _M_finish - 1;
}
else
return _M_insert_aux(__p, __c);
}
void insert(iterator __p, size_t __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
void insert(iterator __p, _InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
_M_insert_dispatch(__p, __first, __last, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
void insert(iterator __p, const _CharT* __first, const _CharT* __last);
#endif /* __STL_MEMBER_TEMPLATES */
private: // Helper functions for insert.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
void insert(iterator __p, _InputIter, _InputIter, input_iterator_tag);
template <class _ForwardIter>
void insert(iterator __p, _ForwardIter, _ForwardIter, forward_iterator_tag);
template <class _Integer>
void _M_insert_dispatch(iterator __p, _Integer __n, _Integer __x,
__true_type) {
insert(__p, (size_type) __n, (_CharT) __x);
}
template <class _InputIter>
void _M_insert_dispatch(iterator __p, _InputIter __first, _InputIter __last,
__false_type) {
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
insert(__p, __first, __last, _Category());
}
template <class _InputIterator>
void
_M_copy(_InputIterator __first, _InputIterator __last, iterator __result) {
for ( ; __first != __last; ++__first, ++__result)
_Traits::assign(*__result, *__first);
}
#endif /* __STL_MEMBER_TEMPLATES */
iterator _M_insert_aux(iterator, _CharT);
void
_M_copy(const _CharT* __first, const _CharT* __last, _CharT* __result) {
_Traits::copy(__result, __first, __last - __first);
}
public: // Erase.
basic_string& erase(size_type __pos = 0, size_type __n = npos) {
if (__pos > size())
_M_throw_out_of_range();
erase(_M_start + __pos, _M_start + __pos + min(__n, size() - __pos));
return *this;
}
iterator erase(iterator __position) {
// The move includes the terminating null.
_Traits::move(__position, __position + 1, _M_finish - __position);
destroy(_M_finish);
--_M_finish;
return __position;
}
iterator erase(iterator __first, iterator __last) {
if (__first != __last) {
// The move includes the terminating null.
_Traits::move(__first, __last, (_M_finish - __last) + 1);
const iterator __new_finish = _M_finish - (__last - __first);
destroy(__new_finish + 1, _M_finish + 1);
_M_finish = __new_finish;
}
return __first;
}
public: // Replace. (Conceptually equivalent
// to erase followed by insert.)
basic_string& replace(size_type __pos, size_type __n,
const basic_string& __s) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n, size() - __pos);
if (size() - __len >= max_size() - __s.size())
_M_throw_length_error();
return replace(_M_start + __pos, _M_start + __pos + __len,
__s.begin(), __s.end());
}
basic_string& replace(size_type __pos1, size_type __n1,
const basic_string& __s,
size_type __pos2, size_type __n2) {
if (__pos1 > size() || __pos2 > __s.size())
_M_throw_out_of_range();
const size_type __len1 = min(__n1, size() - __pos1);
const size_type __len2 = min(__n2, __s.size() - __pos2);
if (size() - __len1 >= max_size() - __len2)
_M_throw_length_error();
return replace(_M_start + __pos1, _M_start + __pos1 + __len1,
__s._M_start + __pos2, __s._M_start + __pos2 + __len2);
}
basic_string& replace(size_type __pos, size_type __n1,
const _CharT* __s, size_type __n2) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(_M_start + __pos, _M_start + __pos + __len,
__s, __s + __n2);
}
basic_string& replace(size_type __pos, size_type __n1,
const _CharT* __s) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
const size_type __n2 = _Traits::length(__s);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(_M_start + __pos, _M_start + __pos + __len,
__s, __s + _Traits::length(__s));
}
basic_string& replace(size_type __pos, size_type __n1,
size_type __n2, _CharT __c) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(_M_start + __pos, _M_start + __pos + __len, __n2, __c);
}
basic_string& replace(iterator __first, iterator __last,
const basic_string& __s)
{ return replace(__first, __last, __s.begin(), __s.end()); }
basic_string& replace(iterator __first, iterator __last,
const _CharT* __s, size_type __n)
{ return replace(__first, __last, __s, __s + __n); }
basic_string& replace(iterator __first, iterator __last,
const _CharT* __s) {
return replace(__first, __last, __s, __s + _Traits::length(__s));
}
basic_string& replace(iterator __first, iterator __last,
size_type __n, _CharT __c);
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
basic_string& replace(iterator __first, iterator __last,
_InputIter __f, _InputIter __l) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_replace_dispatch(__first, __last, __f, __l, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
basic_string& replace(iterator __first, iterator __last,
const _CharT* __f, const _CharT* __l);
#endif /* __STL_MEMBER_TEMPLATES */
private: // Helper functions for replace.
#ifdef __STL_MEMBER_TEMPLATES
template <class _Integer>
basic_string& _M_replace_dispatch(iterator __first, iterator __last,
_Integer __n, _Integer __x,
__true_type) {
return replace(__first, __last, (size_type) __n, (_CharT) __x);
}
template <class _InputIter>
basic_string& _M_replace_dispatch(iterator __first, iterator __last,
_InputIter __f, _InputIter __l,
__false_type) {
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
return replace(__first, __last, __f, __l, _Category());
}
template <class _InputIter>
basic_string& replace(iterator __first, iterator __last,
_InputIter __f, _InputIter __l, input_iterator_tag);
template <class _ForwardIter>
basic_string& replace(iterator __first, iterator __last,
_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag);
#endif /* __STL_MEMBER_TEMPLATES */
public: // Other modifier member functions.
size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n, size() - __pos);
_Traits::copy(__s, _M_start + __pos, __len);
return __len;
}
void swap(basic_string& __s) {
__STD::swap(_M_start, __s._M_start);
__STD::swap(_M_finish, __s._M_finish);
__STD::swap(_M_end_of_storage, __s._M_end_of_storage);
}
public: // Conversion to C string.
const _CharT* c_str() const { return _M_start; }
const _CharT* data() const { return _M_start; }
public: // find.
size_type find(const basic_string& __s, size_type __pos = 0) const
{ return find(__s.begin(), __pos, __s.size()); }
size_type find(const _CharT* __s, size_type __pos = 0) const
{ return find(__s, __pos, _Traits::length(__s)); }
size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
size_type find(_CharT __c, size_type __pos = 0) const;
public: // rfind.
size_type rfind(const basic_string& __s, size_type __pos = npos) const
{ return rfind(__s.begin(), __pos, __s.size()); }
size_type rfind(const _CharT* __s, size_type __pos = npos) const
{ return rfind(__s, __pos, _Traits::length(__s)); }
size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
size_type rfind(_CharT __c, size_type __pos = npos) const;
public: // find_first_of
size_type find_first_of(const basic_string& __s, size_type __pos = 0) const
{ return find_first_of(__s.begin(), __pos, __s.size()); }
size_type find_first_of(const _CharT* __s, size_type __pos = 0) const
{ return find_first_of(__s, __pos, _Traits::length(__s)); }
size_type find_first_of(const _CharT* __s, size_type __pos,
size_type __n) const;
size_type find_first_of(_CharT __c, size_type __pos = 0) const
{ return find(__c, __pos); }
public: // find_last_of
size_type find_last_of(const basic_string& __s,
size_type __pos = npos) const
{ return find_last_of(__s.begin(), __pos, __s.size()); }
size_type find_last_of(const _CharT* __s, size_type __pos = npos) const
{ return find_last_of(__s, __pos, _Traits::length(__s)); }
size_type find_last_of(const _CharT* __s, size_type __pos,
size_type __n) const;
size_type find_last_of(_CharT __c, size_type __pos = npos) const {
return rfind(__c, __pos);
}