-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstring.h
4066 lines (3269 loc) · 146 KB
/
string.h
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) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Implements a basicString class, much like the C++ std::basicString.
// The primary distinctions between basicString and std::basicString are:
// - basicString has a few extension functions that allow for increased performance.
// - basicString has a few extension functions that make use easier,
// such as a member sprintf function and member tolower/toupper functions.
// - basicString supports debug memory naming natively.
// - basicString is easier to read, debug, and visualize.
// - basicString internally manually expands basic functions such as begin(),
// size(), etc. in order to improve debug performance and optimizer success.
// - basicString is savvy to an environment that doesn't have exception handling,
// as is sometimes the case with console or embedded environments.
// - basicString has less deeply nested function calls and allows the user to
// enable forced inlining in debug builds in order to reduce bloat.
// - basicString doesn't use char traits. As a result, EASTL assumes that
// strings will hold characters and not exotic things like widgets. At the
// very least, basicString assumes that the value_type is a POD.
// - basicString::size_type is defined as eastl_size_t instead of size_t in
// order to save memory and run faster on 64 bit systems.
// - basicString data is guaranteed to be contiguous.
// - basicString data is guaranteed to be 0-terminated, and the c_str() function
// is guaranteed to return the same pointer as the data() which is guaranteed
// to be the same value as &string[0].
// - basicString has a setCapacity() function which frees excess capacity.
// The only way to do this with std::basicString is via the cryptic non-obvious
// trick of using: basicString<char>(x).swap(x);
// - basicString has a forceSize() function, which unilaterally moves the string
// end position (mpEnd) to the given location. Useful for when the user writes
// into the string via some external means such as C strcpy or sprintf.
// - basicString substr() deviates from the standard and returns a string with
// a copy of this->getAllocator()
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Copy on Write (cow)
//
// This string implementation does not do copy on write (cow). This is by design,
// as cow penalizes 95% of string uses for the benefit of only 5% of the uses
// (these percentages are qualitative, not quantitative). The primary benefit of
// cow is that it allows for the sharing of string data between two string objects.
// Thus if you say this:
// string a("hello");
// string b(a);
// the "hello" will be shared between a and b. If you then say this:
// a = "world";
// then a will release its reference to "hello" and leave b with the only reference
// to it. Normally this functionality is accomplished via reference counting and
// with atomic operations or mutexes.
//
// The C++ standard does not say anything about basicString and cow. However,
// for a basicString implementation to be standards-conforming, a number of
// issues arise which dictate some things about how one would have to implement
// a cow string. The discussion of these issues will not be rehashed here, as you
// can read the references below for better detail than can be provided in the
// space we have here. However, we can say that the C++ standard is sensible and
// that anything we try to do here to allow for an efficient cow implementation
// would result in a generally unacceptable string interface.
//
// The disadvantages of cow strings are:
// - A reference count needs to exist with the string, which increases string memory usage.
// - With thread safety, atomic operations and mutex locks are expensive, especially
// on weaker memory systems such as console gaming platforms.
// - All non-const string accessor functions need to do a sharing check then the
// first such check needs to detach the string. Similarly, all string assignments
// need to do a sharing check as well. If you access the string before doing an
// assignment, the assignment doesn't result in a shared string, because the string
// has already been detached.
// - String sharing doesn't happen the large majority of the time. In some cases,
// the total sum of the reference count memory can exceed any memory savings
// gained by the strings that share representations.
//
// The addition of a string_cow class is under consideration for this library.
// There are conceivably some systems which have string usage patterns which would
// benefit from cow sharing. Such functionality is best saved for a separate string
// implementation so that the other string uses aren't penalized.
//
// References:
// This is a good starting HTML reference on the topic:
// http://www.gotw.ca/publications/optimizations.htm
// Here is a Usenet discussion on the topic:
// http://groups-beta.google.com/group/comp.lang.c++.moderated/browse_thread/thread/3dc6af5198d0bf7/886c8642cb06e03d
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_STRING_H
#define EASTL_STRING_H
#include <eastl/internal/config.h>
#include <eastl/allocator.h>
#include <eastl/iterator.h>
#include <eastl/algorithm.h>
#include <eastl/initializer_list.h>
#include <eastl/bonus/compressed_pair.h>
EA_DISABLE_ALL_VC_WARNINGS()
#include <stddef.h> // size_t, ptrdiff_t, etc.
#include <stdarg.h> // vararg functionality.
#include <stdlib.h> // malloc, free.
#include <stdio.h> // snprintf, etc.
#include <ctype.h> // toupper, etc.
EA_DISABLE_GCC_WARNING(-Wtype-limits)
#include <wchar.h>
EA_RESTORE_GCC_WARNING()
#include <string.h> // strlen, etc.
#if EASTL_EXCEPTIONS_ENABLED
#include <stdexcept> // std::out_of_range, std::length_error.
#endif
EA_RESTORE_ALL_VC_WARNINGS()
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4480 - nonstandard extension used: specifying underlying type for enum
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
// 4267 - 'argument' : conversion from 'size_t' to 'const uint32_t', possible loss of data. This is a bogus warning resulting from a bug in VC++.
// 4702 - unreachable code
EA_DISABLE_VC_WARNING(4530 4480 4571 4267 4702);
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
#include <eastl/internal/char_traits.h>
#include <eastl/string_view.h>
///////////////////////////////////////////////////////////////////////////////
// EASTL_STRING_EXPLICIT
//
// See EASTL_STRING_OPT_EXPLICIT_CTORS for documentation.
//
#if EASTL_STRING_OPT_EXPLICIT_CTORS
#define EASTL_STRING_EXPLICIT explicit
#else
#define EASTL_STRING_EXPLICIT
#endif
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Vsnprintf
//
// The user is expected to supply these functions one way or another. Note that
// these functions are expected to accept parameters as per the C99 standard.
// These functions can deal with C99 standard return values or Microsoft non-standard
// return values but act more efficiently if implemented via the C99 style.
//
// In the case of EASTL_EASTDC_VSNPRINTF == 1, the user is expected to either
// link EAStdC or provide the functions below that act the same. In the case of
// EASTL_EASTDC_VSNPRINTF == 0, the user is expected to provide the function
// implementations, and may simply use C vsnprintf if desired, though it's not
// completely portable between compilers.
//
#if EASTL_EASTDC_VSNPRINTF
namespace EA
{
namespace StdC
{
// Provided by the EAStdC package or by the user.
EASTL_EASTDC_API int Vsnprintf(char* EASTL_RESTRICT pDestination, size_t n, const char* EASTL_RESTRICT pFormat, va_list arguments);
EASTL_EASTDC_API int Vsnprintf(char16_t* EASTL_RESTRICT pDestination, size_t n, const char16_t* EASTL_RESTRICT pFormat, va_list arguments);
EASTL_EASTDC_API int Vsnprintf(char32_t* EASTL_RESTRICT pDestination, size_t n, const char32_t* EASTL_RESTRICT pFormat, va_list arguments);
#if EA_CHAR8_UNIQUE
EASTL_EASTDC_API int Vsnprintf(char8_t* EASTL_RESTRICT pDestination, size_t n, const char8_t* EASTL_RESTRICT pFormat, va_list arguments);
#endif
#if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
EASTL_EASTDC_API int Vsnprintf(wchar_t* EASTL_RESTRICT pDestination, size_t n, const wchar_t* EASTL_RESTRICT pFormat, va_list arguments);
#endif
}
}
namespace eastl
{
inline int Vsnprintf(char* EASTL_RESTRICT pDestination, size_t n, const char* EASTL_RESTRICT pFormat, va_list arguments)
{ return EA::StdC::Vsnprintf(pDestination, n, pFormat, arguments); }
inline int Vsnprintf(char16_t* EASTL_RESTRICT pDestination, size_t n, const char16_t* EASTL_RESTRICT pFormat, va_list arguments)
{ return EA::StdC::Vsnprintf(pDestination, n, pFormat, arguments); }
inline int Vsnprintf(char32_t* EASTL_RESTRICT pDestination, size_t n, const char32_t* EASTL_RESTRICT pFormat, va_list arguments)
{ return EA::StdC::Vsnprintf(pDestination, n, pFormat, arguments); }
#if EA_CHAR8_UNIQUE
inline int Vsnprintf(char8_t* EASTL_RESTRICT pDestination, size_t n, const char8_t* EASTL_RESTRICT pFormat, va_list arguments)
{ return EA::StdC::Vsnprintf((char*)pDestination, n, (const char*)pFormat, arguments); }
#endif
#if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
inline int Vsnprintf(wchar_t* EASTL_RESTRICT pDestination, size_t n, const wchar_t* EASTL_RESTRICT pFormat, va_list arguments)
{ return EA::StdC::Vsnprintf(pDestination, n, pFormat, arguments); }
#endif
}
#else
// User-provided functions.
extern int Vsnprintf8 (char* pDestination, size_t n, const char* pFormat, va_list arguments);
extern int Vsnprintf16(char16_t* pDestination, size_t n, const char16_t* pFormat, va_list arguments);
extern int Vsnprintf32(char32_t* pDestination, size_t n, const char32_t* pFormat, va_list arguments);
#if EA_CHAR8_UNIQUE
extern int Vsnprintf8 (char8_t* pDestination, size_t n, const char8_t* pFormat, va_list arguments);
#endif
#if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
extern int VsnprintfW(wchar_t* pDestination, size_t n, const wchar_t* pFormat, va_list arguments);
#endif
namespace eastl
{
inline int Vsnprintf(char* pDestination, size_t n, const char* pFormat, va_list arguments)
{ return Vsnprintf8(pDestination, n, pFormat, arguments); }
inline int Vsnprintf(char16_t* pDestination, size_t n, const char16_t* pFormat, va_list arguments)
{ return Vsnprintf16(pDestination, n, pFormat, arguments); }
inline int Vsnprintf(char32_t* pDestination, size_t n, const char32_t* pFormat, va_list arguments)
{ return Vsnprintf32(pDestination, n, pFormat, arguments); }
#if EA_CHAR8_UNIQUE
inline int Vsnprintf(char8_t* pDestination, size_t n, const char8_t* pFormat, va_list arguments)
{ return Vsnprintf8(pDestination, n, pFormat, arguments); }
#endif
#if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
inline int Vsnprintf(wchar_t* pDestination, size_t n, const wchar_t* pFormat, va_list arguments)
{ return VsnprintfW(pDestination, n, pFormat, arguments); }
#endif
}
#endif
///////////////////////////////////////////////////////////////////////////////
namespace eastl
{
/// EASTL_BASIC_STRING_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_BASIC_STRING_DEFAULT_NAME
#define EASTL_BASIC_STRING_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " basicString" // Unless the user overrides something, this is "EASTL basicString".
#endif
/// EASTL_BASIC_STRING_DEFAULT_ALLOCATOR
///
#ifndef EASTL_BASIC_STRING_DEFAULT_ALLOCATOR
#define EASTL_BASIC_STRING_DEFAULT_ALLOCATOR allocator_type(EASTL_BASIC_STRING_DEFAULT_NAME)
#endif
///////////////////////////////////////////////////////////////////////////////
/// basicString
///
/// Implements a templated string class, somewhat like C++ std::basicString.
///
/// Notes:
/// As of this writing, an insert of a string into itself necessarily
/// triggers a reallocation, even if there is enough capacity in self
/// to handle the increase in size. This is due to the slightly tricky
/// nature of the operation of modifying one's self with one's self,
/// and thus the source and destination are being modified during the
/// operation. It might be useful to rectify this to the extent possible.
///
/// Our usage of noexcept specifiers is a little different from the
/// requirements specified by std::basicString in C++11. This is because
/// our allocators are instances and not types and thus can be non-equal
/// and result in exceptions during assignments that theoretically can't
/// occur with std containers.
///
template <typename T, typename Allocator = EASTLAllocatorType>
class basicString
{
public:
typedef basicString<T, Allocator> this_type;
typedef basicString_view<T> view_type;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T* iterator; // Maintainer note: We want to leave iterator defined as T* -- at least in release builds -- as this gives some algorithms an advantage that optimizers cannot get around.
typedef const T* const_iterator;
typedef eastl::reverse_iterator<iterator> reverse_iterator;
typedef eastl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef eastl_size_t size_type; // See config.h for the definition of eastl_size_t, which defaults to size_t.
typedef ptrdiff_t difference_type;
typedef Allocator allocator_type;
static const EA_CONSTEXPR size_type npos = (size_type)-1; /// 'npos' means non-valid position or simply non-position.
public:
// CtorDoNotInitialize exists so that we can create a constructor that allocates but doesn't
// initialize and also doesn't collide with any other constructor declaration.
struct CtorDoNotInitialize{};
// CtorSprintf exists so that we can create a constructor that accepts printf-style
// arguments but also doesn't collide with any other constructor declaration.
#ifdef EA_PLATFORM_MINGW
// Workaround for MinGW compiler bug: variadic arguments are corrupted if empty object is passed before it
struct CtorSprintf{ int dummy; };
#else
struct CtorSprintf{};
#endif
// CtorConvert exists so that we can have a constructor that implements string encoding
// conversion, such as between UCS2 char16_t and UTF8 char8_t.
struct CtorConvert{};
protected:
// Masks used to determine if we are in SSO or Heap
#ifdef EA_SYSTEM_BIG_ENDIAN
// Big Endian use LSB, unless we want to reorder struct layouts on endianness, Bit is set when we are in Heap
static EA_CONSTEXPR_OR_CONST size_type kHeapMask = 0x1;
static EA_CONSTEXPR_OR_CONST size_type kSSOMask = 0x1;
#else
// Little Endian use MSB
static EA_CONSTEXPR_OR_CONST size_type kHeapMask = ~(size_type(~size_type(0)) >> 1);
static EA_CONSTEXPR_OR_CONST size_type kSSOMask = 0x80;
#endif
public:
#ifdef EA_SYSTEM_BIG_ENDIAN
static EA_CONSTEXPR_OR_CONST size_type kMaxSize = (~kHeapMask) >> 1;
#else
static EA_CONSTEXPR_OR_CONST size_type kMaxSize = ~kHeapMask;
#endif
protected:
// The view of memory when the string data is obtained from the allocator.
struct HeapLayout
{
value_type* mpBegin; // Begin of string.
size_type mnSize; // Size of the string. Number of characters currently in the string, not including the trailing '0'
size_type mnCapacity; // Capacity of the string. Number of characters string can hold, not including the trailing '0'
};
template <typename CharT, size_t = sizeof(CharT)>
struct SSOPadding
{
char padding[sizeof(CharT) - sizeof(char)];
};
template <typename CharT>
struct SSOPadding<CharT, 1>
{
// template specialization to remove the padding structure to avoid warnings on zero length arrays
// also, this allows us to take advantage of the empty-base-class optimization.
};
// The view of memory when the string data is able to store the string data locally (without a heap allocation).
struct SSOLayout
{
static EA_CONSTEXPR_OR_CONST size_type SSO_CAPACITY = (sizeof(HeapLayout) - sizeof(char)) / sizeof(value_type);
// mnSize must correspond to the last byte of HeapLayout.mnCapacity, so we don't want the compiler to insert
// padding after mnSize if sizeof(value_type) != 1; Also ensures both layouts are the same size.
struct SSOSize : SSOPadding<value_type>
{
char mnRemainingSize;
};
value_type mData[SSO_CAPACITY]; // Local buffer for string data.
SSOSize mRemainingSizeField;
};
// This view of memory is a utility structure for easy copying of the string data.
struct RawLayout
{
char mBuffer[sizeof(HeapLayout)];
};
static_assert(sizeof(SSOLayout) == sizeof(HeapLayout), "heap and sso layout structures must be the same size");
static_assert(sizeof(HeapLayout) == sizeof(RawLayout), "heap and raw layout structures must be the same size");
// This implements the 'short string optimization' or SSO. SSO reuses the existing storage of string class to
// hold string data short enough to fit therefore avoiding a heap allocation. The number of characters stored in
// the string SSO buffer is variable and depends on the string character width. This implementation favors a
// consistent string size than increasing the size of the string local data to accommodate a consistent number
// of characters despite character width.
struct Layout
{
union
{
HeapLayout heap;
SSOLayout sso;
RawLayout raw;
};
Layout() { ResetToSSO(); } // start as SSO by default
Layout(const Layout& other) { Copy(*this, other); }
Layout(Layout&& other) { Move(*this, other); }
Layout& operator=(const Layout& other) { Copy(*this, other); return *this; }
Layout& operator=(Layout&& other) { Move(*this, other); return *this; }
// We are using Heap when the bit is set, easier to conceptualize checking IsHeap instead of IsSSO
inline bool IsHeap() const EASTL_NOEXCEPT { return !!(sso.mRemainingSizeField.mnRemainingSize & kSSOMask); }
inline bool IsSSO() const EASTL_NOEXCEPT { return !IsHeap(); }
inline value_type* SSOBufferPtr() EASTL_NOEXCEPT { return sso.mData; }
inline const value_type* SSOBufferPtr() const EASTL_NOEXCEPT { return sso.mData; }
// Largest value for SSO.mnSize == 23, which has two LSB bits set, but on big-endian (BE)
// use least significant bit (LSB) to denote heap so shift.
inline size_type GetSSOSize() const EASTL_NOEXCEPT
{
#ifdef EA_SYSTEM_BIG_ENDIAN
return SSOLayout::SSO_CAPACITY - (sso.mRemainingSizeField.mnRemainingSize >> 2);
#else
return (SSOLayout::SSO_CAPACITY - sso.mRemainingSizeField.mnRemainingSize);
#endif
}
inline size_type GetHeapSize() const EASTL_NOEXCEPT { return heap.mnSize; }
inline size_type GetSize() const EASTL_NOEXCEPT { return IsHeap() ? GetHeapSize() : GetSSOSize(); }
inline void SetSSOSize(size_type size) EASTL_NOEXCEPT
{
#ifdef EA_SYSTEM_BIG_ENDIAN
sso.mRemainingSizeField.mnRemainingSize = (char)((SSOLayout::SSO_CAPACITY - size) << 2);
#else
sso.mRemainingSizeField.mnRemainingSize = (char)(SSOLayout::SSO_CAPACITY - size);
#endif
}
inline void SetHeapSize(size_type size) EASTL_NOEXCEPT { heap.mnSize = size; }
inline void SetSize(size_type size) EASTL_NOEXCEPT { IsHeap() ? SetHeapSize(size) : SetSSOSize(size); }
inline size_type GetRemainingCapacity() const EASTL_NOEXCEPT { return size_type(CapacityPtr() - EndPtr()); }
inline value_type* HeapBeginPtr() EASTL_NOEXCEPT { return heap.mpBegin; };
inline const value_type* HeapBeginPtr() const EASTL_NOEXCEPT { return heap.mpBegin; };
inline value_type* SSOBeginPtr() EASTL_NOEXCEPT { return sso.mData; }
inline const value_type* SSOBeginPtr() const EASTL_NOEXCEPT { return sso.mData; }
inline value_type* BeginPtr() EASTL_NOEXCEPT { return IsHeap() ? HeapBeginPtr() : SSOBeginPtr(); }
inline const value_type* BeginPtr() const EASTL_NOEXCEPT { return IsHeap() ? HeapBeginPtr() : SSOBeginPtr(); }
inline value_type* HeapEndPtr() EASTL_NOEXCEPT { return heap.mpBegin + heap.mnSize; }
inline const value_type* HeapEndPtr() const EASTL_NOEXCEPT { return heap.mpBegin + heap.mnSize; }
inline value_type* SSOEndPtr() EASTL_NOEXCEPT { return sso.mData + GetSSOSize(); }
inline const value_type* SSOEndPtr() const EASTL_NOEXCEPT { return sso.mData + GetSSOSize(); }
// Points to end of character stream, *ptr == '0'
inline value_type* EndPtr() EASTL_NOEXCEPT { return IsHeap() ? HeapEndPtr() : SSOEndPtr(); }
inline const value_type* EndPtr() const EASTL_NOEXCEPT { return IsHeap() ? HeapEndPtr() : SSOEndPtr(); }
inline value_type* HeapCapacityPtr() EASTL_NOEXCEPT { return heap.mpBegin + GetHeapCapacity(); }
inline const value_type* HeapCapacityPtr() const EASTL_NOEXCEPT { return heap.mpBegin + GetHeapCapacity(); }
inline value_type* SSOCapacityPtr() EASTL_NOEXCEPT { return sso.mData + SSOLayout::SSO_CAPACITY; }
inline const value_type* SSOCapacityPtr() const EASTL_NOEXCEPT { return sso.mData + SSOLayout::SSO_CAPACITY; }
// Points to end of the buffer at the terminating '0', *ptr == '0' <- only true when size() == capacity()
inline value_type* CapacityPtr() EASTL_NOEXCEPT { return IsHeap() ? HeapCapacityPtr() : SSOCapacityPtr(); }
inline const value_type* CapacityPtr() const EASTL_NOEXCEPT { return IsHeap() ? HeapCapacityPtr() : SSOCapacityPtr(); }
inline void SetHeapBeginPtr(value_type* pBegin) EASTL_NOEXCEPT { heap.mpBegin = pBegin; }
inline void SetHeapCapacity(size_type cap) EASTL_NOEXCEPT
{
#ifdef EA_SYSTEM_BIG_ENDIAN
heap.mnCapacity = (cap << 1) | kHeapMask;
#else
heap.mnCapacity = (cap | kHeapMask);
#endif
}
inline size_type GetHeapCapacity() const EASTL_NOEXCEPT
{
#ifdef EA_SYSTEM_BIG_ENDIAN
return (heap.mnCapacity >> 1);
#else
return (heap.mnCapacity & ~kHeapMask);
#endif
}
inline void Copy(Layout& dst, const Layout& src) EASTL_NOEXCEPT { dst.raw = src.raw; }
inline void Move(Layout& dst, Layout& src) EASTL_NOEXCEPT { eastl::swap(dst.raw, src.raw); }
inline void Swap(Layout& a, Layout& b) EASTL_NOEXCEPT { eastl::swap(a.raw, b.raw); }
inline void ResetToSSO() EASTL_NOEXCEPT { *SSOBeginPtr() = 0; SetSSOSize(0); }
};
eastl::compressed_pair<Layout, allocator_type> mPair;
inline Layout& internalLayout() EASTL_NOEXCEPT { return mPair.first(); }
inline const Layout& internalLayout() const EASTL_NOEXCEPT { return mPair.first(); }
inline allocator_type& internalAllocator() EASTL_NOEXCEPT { return mPair.second(); }
inline const allocator_type& internalAllocator() const EASTL_NOEXCEPT { return mPair.second(); }
public:
// Constructor, destructor
basicString() EASTL_NOEXCEPT_IF(EASTL_NOEXCEPT_EXPR(EASTL_BASIC_STRING_DEFAULT_ALLOCATOR));
explicit basicString(const allocator_type& allocator) EASTL_NOEXCEPT;
basicString(const this_type& x, size_type position, size_type n = npos);
basicString(const value_type* p, size_type n, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
EASTL_STRING_EXPLICIT basicString(const value_type* p, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(size_type n, value_type c, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(const this_type& x);
basicString(const this_type& x, const allocator_type& allocator);
basicString(const value_type* pBegin, const value_type* pEnd, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(CtorDoNotInitialize, size_type n, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(CtorSprintf, const value_type* pFormat, ...);
basicString(std::initializer_list<value_type> init, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(this_type&& x) EASTL_NOEXCEPT;
basicString(this_type&& x, const allocator_type& allocator);
explicit basicString(const view_type& sv, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
basicString(const view_type& sv, size_type position, size_type n, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
template <typename OtherCharType>
basicString(CtorConvert, const OtherCharType* p, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
template <typename OtherCharType>
basicString(CtorConvert, const OtherCharType* p, size_type n, const allocator_type& allocator = EASTL_BASIC_STRING_DEFAULT_ALLOCATOR);
template <typename OtherStringType> // Unfortunately we need the CtorConvert here because otherwise this function would collide with the value_type* constructor.
basicString(CtorConvert, const OtherStringType& x);
~basicString();
// Allocator
const allocator_type& getAllocator() const EASTL_NOEXCEPT;
allocator_type& getAllocator() EASTL_NOEXCEPT;
void setAllocator(const allocator_type& allocator);
// Implicit conversion operator
operator basicString_view<T>() const EASTL_NOEXCEPT;
// Operator=
this_type& operator=(const this_type& x);
this_type& operator=(const value_type* p);
this_type& operator=(value_type c);
this_type& operator=(std::initializer_list<value_type> ilist);
this_type& operator=(view_type v);
this_type& operator=(this_type&& x); // TODO(c++17): noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value);
#if EASTL_OPERATOR_EQUALS_OTHER_ENABLED
this_type& operator=(value_type* p) { return operator=((const value_type*)p); } // We need this because otherwise the const value_type* version can collide with the const OtherStringType& version below.
template <typename OtherCharType>
this_type& operator=(const OtherCharType* p);
template <typename OtherStringType>
this_type& operator=(const OtherStringType& x);
#endif
void swap(this_type& x); // TODO(c++17): noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || allocator_traits<Allocator>::is_always_equal::value);
// Assignment operations
this_type& assign(const this_type& x);
this_type& assign(const this_type& x, size_type position, size_type n = npos);
this_type& assign(const value_type* p, size_type n);
this_type& assign(const value_type* p);
this_type& assign(size_type n, value_type c);
this_type& assign(const value_type* pBegin, const value_type* pEnd);
this_type& assign(this_type&& x); // TODO(c++17): noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value);
this_type& assign(std::initializer_list<value_type>);
template <typename OtherCharType>
this_type& assign_convert(const OtherCharType* p);
template <typename OtherCharType>
this_type& assign_convert(const OtherCharType* p, size_type n);
template <typename OtherStringType>
this_type& assign_convert(const OtherStringType& x);
// Iterators.
iterator begin() EASTL_NOEXCEPT; // Expanded in source code as: mpBegin
const_iterator begin() const EASTL_NOEXCEPT; // Expanded in source code as: mpBegin
const_iterator cbegin() const EASTL_NOEXCEPT;
iterator end() EASTL_NOEXCEPT; // Expanded in source code as: mpEnd
const_iterator end() const EASTL_NOEXCEPT; // Expanded in source code as: mpEnd
const_iterator cend() const EASTL_NOEXCEPT;
reverse_iterator rbegin() EASTL_NOEXCEPT;
const_reverse_iterator rbegin() const EASTL_NOEXCEPT;
const_reverse_iterator crbegin() const EASTL_NOEXCEPT;
reverse_iterator rend() EASTL_NOEXCEPT;
const_reverse_iterator rend() const EASTL_NOEXCEPT;
const_reverse_iterator crend() const EASTL_NOEXCEPT;
// Size-related functionality
bool empty() const EASTL_NOEXCEPT;
size_type size() const EASTL_NOEXCEPT;
size_type length() const EASTL_NOEXCEPT;
size_type maxSize() const EASTL_NOEXCEPT;
size_type capacity() const EASTL_NOEXCEPT;
void resize(size_type n, value_type c);
void resize(size_type n);
void reserve(size_type = 0);
void setCapacity(size_type n = npos); // Revises the capacity to the user-specified value. Resizes the container to match the capacity if the requested capacity n is less than the current size. If n == npos then the capacity is reallocated (if necessary) such that capacity == size.
void forceSize(size_type n); // Unilaterally moves the string end position (mpEnd) to the given location. Useful for when the user writes into the string via some extenal means such as C strcpy or sprintf. This allows for more efficient use than using resize to achieve this.
void shrink_to_fit();
// Raw access
const value_type* data() const EASTL_NOEXCEPT;
value_type* data() EASTL_NOEXCEPT;
const value_type* c_str() const EASTL_NOEXCEPT;
// Element access
reference operator[](size_type n);
const_reference operator[](size_type n) const;
reference at(size_type n);
const_reference at(size_type n) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
// Append operations
this_type& operator+=(const this_type& x);
this_type& operator+=(const value_type* p);
this_type& operator+=(value_type c);
this_type& append(const this_type& x);
this_type& append(const this_type& x, size_type position, size_type n = npos);
this_type& append(const value_type* p, size_type n);
this_type& append(const value_type* p);
this_type& append(size_type n, value_type c);
this_type& append(const value_type* pBegin, const value_type* pEnd);
this_type& appendSprintfVaList(const value_type* pFormat, va_list arguments);
this_type& appendSprintf(const value_type* pFormat, ...);
template <typename OtherCharType>
this_type& append_convert(const OtherCharType* p);
template <typename OtherCharType>
this_type& append_convert(const OtherCharType* p, size_type n);
template <typename OtherStringType>
this_type& append_convert(const OtherStringType& x);
void pushBack(value_type c);
void popBack();
// Insertion operations
this_type& insert(size_type position, const this_type& x);
this_type& insert(size_type position, const this_type& x, size_type beg, size_type n);
this_type& insert(size_type position, const value_type* p, size_type n);
this_type& insert(size_type position, const value_type* p);
this_type& insert(size_type position, size_type n, value_type c);
iterator insert(const_iterator p, value_type c);
iterator insert(const_iterator p, size_type n, value_type c);
iterator insert(const_iterator p, const value_type* pBegin, const value_type* pEnd);
iterator insert(const_iterator p, std::initializer_list<value_type>);
// Erase operations
this_type& erase(size_type position = 0, size_type n = npos);
iterator erase(const_iterator p);
iterator erase(const_iterator pBegin, const_iterator pEnd);
reverse_iterator erase(reverse_iterator position);
reverse_iterator erase(reverse_iterator first, reverse_iterator last);
void clear() EASTL_NOEXCEPT;
// Detach memory
pointer detach() EASTL_NOEXCEPT;
// Replacement operations
this_type& replace(size_type position, size_type n, const this_type& x);
this_type& replace(size_type pos1, size_type n1, const this_type& x, size_type pos2, size_type n2 = npos);
this_type& replace(size_type position, size_type n1, const value_type* p, size_type n2);
this_type& replace(size_type position, size_type n1, const value_type* p);
this_type& replace(size_type position, size_type n1, size_type n2, value_type c);
this_type& replace(const_iterator first, const_iterator last, const this_type& x);
this_type& replace(const_iterator first, const_iterator last, const value_type* p, size_type n);
this_type& replace(const_iterator first, const_iterator last, const value_type* p);
this_type& replace(const_iterator first, const_iterator last, size_type n, value_type c);
this_type& replace(const_iterator first, const_iterator last, const value_type* pBegin, const value_type* pEnd);
size_type copy(value_type* p, size_type n, size_type position = 0) const;
// Find operations
size_type find(const this_type& x, size_type position = 0) const EASTL_NOEXCEPT;
size_type find(const value_type* p, size_type position = 0) const;
size_type find(const value_type* p, size_type position, size_type n) const;
size_type find(value_type c, size_type position = 0) const EASTL_NOEXCEPT;
// Reverse find operations
size_type rfind(const this_type& x, size_type position = npos) const EASTL_NOEXCEPT;
size_type rfind(const value_type* p, size_type position = npos) const;
size_type rfind(const value_type* p, size_type position, size_type n) const;
size_type rfind(value_type c, size_type position = npos) const EASTL_NOEXCEPT;
// Find first-of operations
size_type findFirstOf(const this_type& x, size_type position = 0) const EASTL_NOEXCEPT;
size_type findFirstOf(const value_type* p, size_type position = 0) const;
size_type findFirstOf(const value_type* p, size_type position, size_type n) const;
size_type findFirstOf(value_type c, size_type position = 0) const EASTL_NOEXCEPT;
// Find last-of operations
size_type findLastOf(const this_type& x, size_type position = npos) const EASTL_NOEXCEPT;
size_type findLastOf(const value_type* p, size_type position = npos) const;
size_type findLastOf(const value_type* p, size_type position, size_type n) const;
size_type findLastOf(value_type c, size_type position = npos) const EASTL_NOEXCEPT;
// Find first not-of operations
size_type findFirstNotOf(const this_type& x, size_type position = 0) const EASTL_NOEXCEPT;
size_type findFirstNotOf(const value_type* p, size_type position = 0) const;
size_type findFirstNotOf(const value_type* p, size_type position, size_type n) const;
size_type findFirstNotOf(value_type c, size_type position = 0) const EASTL_NOEXCEPT;
// Find last not-of operations
size_type findLastNotOf(const this_type& x, size_type position = npos) const EASTL_NOEXCEPT;
size_type findLastNotOf(const value_type* p, size_type position = npos) const;
size_type findLastNotOf(const value_type* p, size_type position, size_type n) const;
size_type findLastNotOf(value_type c, size_type position = npos) const EASTL_NOEXCEPT;
// Substring functionality
this_type substr(size_type position = 0, size_type n = npos) const;
// Comparison operations
int compare(const this_type& x) const EASTL_NOEXCEPT;
int compare(size_type pos1, size_type n1, const this_type& x) const;
int compare(size_type pos1, size_type n1, const this_type& x, size_type pos2, size_type n2) const;
int compare(const value_type* p) const;
int compare(size_type pos1, size_type n1, const value_type* p) const;
int compare(size_type pos1, size_type n1, const value_type* p, size_type n2) const;
static int compare(const value_type* pBegin1, const value_type* pEnd1, const value_type* pBegin2, const value_type* pEnd2);
// Case-insensitive comparison functions. Not part of C++ this_type. Only ASCII-level locale functionality is supported. Thus this is not suitable for localization purposes.
int comparei(const this_type& x) const EASTL_NOEXCEPT;
int comparei(const value_type* p) const;
static int comparei(const value_type* pBegin1, const value_type* pEnd1, const value_type* pBegin2, const value_type* pEnd2);
// Misc functionality, not part of C++ this_type.
void makeLower();
void makeUpper();
void ltrim();
void rtrim();
void trim();
void ltrim(const value_type* p);
void rtrim(const value_type* p);
void trim(const value_type* p);
this_type left(size_type n) const;
this_type right(size_type n) const;
this_type& sprintfVaList(const value_type* pFormat, va_list arguments);
this_type& sprintf(const value_type* pFormat, ...);
bool validate() const EASTL_NOEXCEPT;
int validateIterator(const_iterator i) const EASTL_NOEXCEPT;
protected:
// Helper functions for initialization/insertion operations.
value_type* DoAllocate(size_type n);
void DoFree(value_type* p, size_type n);
size_type GetNewCapacity(size_type currentCapacity);
size_type GetNewCapacity(size_type currentCapacity, size_type minimumGrowSize);
void AllocateSelf();
void AllocateSelf(size_type n);
void DeallocateSelf();
iterator InsertInternal(const_iterator p, value_type c);
void RangeInitialize(const value_type* pBegin, const value_type* pEnd);
void RangeInitialize(const value_type* pBegin);
void SizeInitialize(size_type n, value_type c);
bool IsSSO() const EASTL_NOEXCEPT;
void ThrowLengthException() const;
void ThrowRangeException() const;
void ThrowInvalidArgumentException() const;
#if EASTL_OPERATOR_EQUALS_OTHER_ENABLED
template <typename CharType>
void DoAssignConvert(CharType c, true_type);
template <typename StringType>
void DoAssignConvert(const StringType& x, false_type);
#endif
}; // basicString
///////////////////////////////////////////////////////////////////////////////
// basicString
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString() EASTL_NOEXCEPT_IF(EASTL_NOEXCEPT_EXPR(EASTL_BASIC_STRING_DEFAULT_ALLOCATOR))
: mPair(allocator_type(EASTL_BASIC_STRING_DEFAULT_NAME))
{
AllocateSelf();
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const allocator_type& allocator) EASTL_NOEXCEPT
: mPair(allocator)
{
AllocateSelf();
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const this_type& x)
: mPair(x.getAllocator())
{
RangeInitialize(x.internalLayout().BeginPtr(), x.internalLayout().EndPtr());
}
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(const this_type& x, const allocator_type& allocator)
: mPair(allocator)
{
RangeInitialize(x.internalLayout().BeginPtr(), x.internalLayout().EndPtr());
}
template <typename T, typename Allocator>
template <typename OtherStringType>
inline basicString<T, Allocator>::basicString(CtorConvert, const OtherStringType& x)
: mPair(x.getAllocator())
{
AllocateSelf();
append_convert(x.c_str(), x.length());
}
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(const this_type& x, size_type position, size_type n)
: mPair(x.getAllocator())
{
#if EASTL_STRING_OPT_RANGE_ERRORS
if (EASTL_UNLIKELY(position > x.internalLayout().GetSize())) // 21.4.2 p4
{
ThrowRangeException();
AllocateSelf();
}
else
RangeInitialize(
x.internalLayout().BeginPtr() + position,
x.internalLayout().BeginPtr() + position + eastl::minAlt(n, x.internalLayout().GetSize() - position));
#else
RangeInitialize(
x.internalLayout().BeginPtr() + position,
x.internalLayout().BeginPtr() + position + eastl::minAlt(n, x.internalLayout().GetSize() - position));
#endif
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const value_type* p, size_type n, const allocator_type& allocator)
: mPair(allocator)
{
RangeInitialize(p, p + n);
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const view_type& sv, const allocator_type& allocator)
: basicString(sv.data(), static_cast<size_type>(sv.size()), allocator)
{
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const view_type& sv, size_type position, size_type n, const allocator_type& allocator)
: basicString(sv.substr(position, n), allocator)
{
}
template <typename T, typename Allocator>
template <typename OtherCharType>
inline basicString<T, Allocator>::basicString(CtorConvert, const OtherCharType* p, const allocator_type& allocator)
: mPair(allocator)
{
AllocateSelf(); // In this case we are converting from one string encoding to another, and we
append_convert(p); // implement this in the simplest way, by simply default-constructing and calling assign.
}
template <typename T, typename Allocator>
template <typename OtherCharType>
inline basicString<T, Allocator>::basicString(CtorConvert, const OtherCharType* p, size_type n, const allocator_type& allocator)
: mPair(allocator)
{
AllocateSelf(); // In this case we are converting from one string encoding to another, and we
append_convert(p, n); // implement this in the simplest way, by simply default-constructing and calling assign.
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const value_type* p, const allocator_type& allocator)
: mPair(allocator)
{
RangeInitialize(p);
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(size_type n, value_type c, const allocator_type& allocator)
: mPair(allocator)
{
SizeInitialize(n, c);
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::basicString(const value_type* pBegin, const value_type* pEnd, const allocator_type& allocator)
: mPair(allocator)
{
RangeInitialize(pBegin, pEnd);
}
// CtorDoNotInitialize exists so that we can create a version that allocates but doesn't
// initialize but also doesn't collide with any other constructor declaration.
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(CtorDoNotInitialize /*unused*/, size_type n, const allocator_type& allocator)
: mPair(allocator)
{
// Note that we do not call SizeInitialize here.
AllocateSelf(n);
internalLayout().SetSize(0);
*internalLayout().EndPtr() = 0;
}
// CtorSprintf exists so that we can create a version that does a variable argument
// sprintf but also doesn't collide with any other constructor declaration.
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(CtorSprintf /*unused*/, const value_type* pFormat, ...)
: mPair()
{
const size_type n = (size_type)CharStrlen(pFormat);
AllocateSelf(n);
internalLayout().SetSize(0);
va_list arguments;
va_start(arguments, pFormat);
appendSprintfVaList(pFormat, arguments);
va_end(arguments);
}
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(std::initializer_list<value_type> init, const allocator_type& allocator)
: mPair(allocator)
{
RangeInitialize(init.begin(), init.end());
}
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(this_type&& x) EASTL_NOEXCEPT
: mPair(x.getAllocator())
{
internalLayout() = eastl::move(x.internalLayout());
x.AllocateSelf();
}
template <typename T, typename Allocator>
basicString<T, Allocator>::basicString(this_type&& x, const allocator_type& allocator)
: mPair(allocator)
{
if(getAllocator() == x.getAllocator()) // If we can borrow from x...
{
internalLayout() = eastl::move(x.internalLayout());
x.AllocateSelf();
}
else if(x.internalLayout().BeginPtr())
{
RangeInitialize(x.internalLayout().BeginPtr(), x.internalLayout().EndPtr());
// Let x destruct its own items.
}
}
template <typename T, typename Allocator>
inline basicString<T, Allocator>::~basicString()
{
DeallocateSelf();
}
template <typename T, typename Allocator>
inline const typename basicString<T, Allocator>::allocator_type&
basicString<T, Allocator>::getAllocator() const EASTL_NOEXCEPT
{
return internalAllocator();
}