-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_string.h
More file actions
2271 lines (1994 loc) · 74.7 KB
/
basic_string.h
File metadata and controls
2271 lines (1994 loc) · 74.7 KB
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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* basic_string.h
*
* This file defines an STL-compatible string container
*
* Extensions/Changes:
* - reduce() reduces the underlying memory usage
* - shrink_to_fit() from C++11 is implemented
* - var-arg constructors to do printf-style construction
* - format, append_format, insert_format, replace_format variations exist for printf-style operations
* - a pre-allocated memory block can be specified as a template parameter
* - works as a holder for literal/external strings
*/
#ifndef THOR_BASIC_STRING_H
#define THOR_BASIC_STRING_H
#pragma once
#ifndef THOR_ITERATOR_H
#include "iterator.h"
#endif
#ifndef THOR_MEMORY_H
#include "memory.h"
#endif
#ifndef THOR_ATOMIC_INTEGER_H
#include "atomic_integer.h"
#endif
#ifndef THOR_STRING_UTIL_H
#include "string_util.h"
#endif
#ifndef THOR_SWAP_H
#include "swap.h"
#endif
#ifndef THOR_ALGORITHM_H
#include "algorithm.h"
#endif
#ifndef THOR_HASH_FUNCS_H
#include "hash_funcs.h"
#endif
namespace thor
{
template <typename T, thor_size_type T_SIZE = 0> class basic_string;
// Specialization for the base vector that does no preallocation.
template <typename T> class basic_string<T, 0>
{
public:
// STL-compatible typedefs
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
// npos
static const size_type npos = size_type(-1);
// embedded_size
static const size_type embedded_size = 0;
// Iterator base class
struct iterator_base : public iterator_type<random_access_iterator_tag, T>
{
typedef THOR_TYPENAME basic_string<T>::pointer pointer;
pointer element;
#ifdef THOR_DEBUG
const basic_string* owner;
iterator_base(pointer element_, const basic_string* owner_)
: element(element_), owner(owner_)
{}
#else
iterator_base(pointer element_, const basic_string*)
: element(element_)
{}
#endif
void verify_owner(const basic_string* owner_)
{
THOR_UNUSED(owner_);
THOR_DEBUG_ASSERT(owner == owner_);
}
void verify_range(bool allowEnd = false) const
{
THOR_UNUSED(allowEnd);
THOR_DEBUG_ASSERT(element >= owner->elements_ && element < (owner->elements_ + owner->size_ + allowEnd));
}
void decr() { --element; }
void incr() { verify_range(); ++element; }
bool operator == (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return element == i.element; }
bool operator != (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return element != i.element; }
bool operator < (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return element < i.element; }
bool operator > (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return i.element < element; }
bool operator <= (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return !(i.element < element); }
bool operator >= (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner == i.owner); return !(element < i.element); }
};
// Forward iterator template
template<typename Traits> class fwd_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef fwd_iterator<nonconst_traits<T> > nonconst_iterator;
typedef fwd_iterator<Traits> selftype;
fwd_iterator(pointer p = 0, const basic_string* o = 0) : iterator_base((typename iterator_base::pointer)p, o) {}
fwd_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_range(); return *element; }
selftype operator - (difference_type i) const { selftype n(*this); n.element -= i; return n; }
selftype& operator -= (difference_type i) { element -= i; return *this; }
selftype& operator -- () /* --iterator */ { decr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); decr(); return n; }
selftype operator + (difference_type i) const { selftype n(*this); n.element += i; return n; }
selftype& operator += (difference_type i) { element += i; return *this; }
selftype& operator ++ () /* ++iterator */ { incr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); incr(); return n; }
difference_type operator - (const selftype& t) const { THOR_DEBUG_ASSERT(owner == t.owner); return element - t.element; }
};
// Reverse iterator template
template<typename Traits> class rev_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef rev_iterator<nonconst_traits<T> > nonconst_iterator;
typedef rev_iterator<Traits> selftype;
rev_iterator(pointer p = 0, const basic_string* o = 0) : iterator_base((typename iterator_base::pointer)p, o) {}
rev_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_range(); return *element; }
selftype operator - (difference_type i) const { selftype n(*this); n.element += i; return n; }
selftype& operator -= (difference_type i) { element += i; return *this; }
selftype& operator -- () /* --iterator */ { incr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); incr(); return n; }
selftype operator + (difference_type i) const { selftype n(*this); n.element -= i; return n; }
selftype& operator += (difference_type i) { element -= i; return *this; }
selftype& operator ++ () /* ++iterator */ { decr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); decr(); return n; }
difference_type operator - (const selftype& t) const { THOR_DEBUG_ASSERT(owner == t.owner); return t.element - element; }
};
// Iterator definitions
typedef fwd_iterator<nonconst_traits<T> > iterator;
typedef fwd_iterator<const_traits<T> > const_iterator;
typedef rev_iterator<nonconst_traits<T> > reverse_iterator;
typedef rev_iterator<const_traits<T> > const_reverse_iterator;
enum Format { fmt };
enum Literal
{
lit_allow_share, // holds a literal string and will freely share with other thor::strings
lit_copy_on_share // holds a string owned externally; any attempts to share will result in a copy
};
// Constructors
explicit basic_string();
basic_string(const basic_string& rhs);
basic_string(const basic_string& rhs, size_type pos, size_type len = npos);
basic_string(const_pointer s);
basic_string(const_pointer s, size_type len);
basic_string(size_type fill, value_type c);
template<class InputIterator> basic_string(InputIterator first, InputIterator last);
// Formatting constructor extensions
basic_string(Format, const_pointer s, ...);
basic_string(const_pointer s, va_list va);
// Literal string construtor extensions
basic_string(Literal, const_pointer s);
basic_string(Literal, const_pointer s, size_type len);
// Destructor
virtual ~basic_string();
// Forward Iteration
iterator begin() { return iterator(elements_, this); }
iterator end() { return iterator(end_ptr(), this); }
const_iterator begin() const { return const_iterator(elements_, this); }
const_iterator end() const { return const_iterator(end_ptr(), this); }
const_iterator cbegin() const { return const_iterator(elements_, this); } // C++11
const_iterator cend() const { return const_iterator(end_ptr(), this); } // C++11
// Reverse iteration
reverse_iterator rbegin() { return reverse_iterator(end_ptr() - 1, this); }
reverse_iterator rend() { return reverse_iterator(elements_ - 1, this); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end_ptr() - 1, this); }
const_reverse_iterator rend() const { return const_reverse_iterator(elements_ - 1, this); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(end_ptr() - 1, this); } // C++11
const_reverse_iterator crend() const { return const_reverse_iterator(elements_ - 1, this); } // C++11
// Capacity
size_type size() const { return size_; }
size_type length() const { return size_; }
size_type max_capacity() const { return size_type(-1); }
size_type capacity() const { return capacity_; }
bool empty() const { return size_ == 0; }
// Memory management
void resize(size_type n);
void resize(size_type n, value_type c);
void reserve(size_type n = 0);
void clear();
void reduce(size_type n = 0);
void shrink_to_fit() { reduce(0); } // C++11
// Element access
reference operator [] (size_type index);
const_reference operator [] (size_type index) const;
reference at(size_type index);
const_reference at(size_type index) const;
reference front(); // C++11
const_reference front() const; // C++11
reference back(); // C++11
const_reference back() const; // C++11
// Operators
basic_string& operator = (const basic_string& str);
basic_string& operator = (const_pointer s);
basic_string& operator = (value_type c);
basic_string& operator += (const basic_string& str);
basic_string& operator += (const_pointer s);
basic_string& operator += (value_type c);
basic_string operator + (const basic_string& str) const;
basic_string operator + (const_pointer s) const;
basic_string operator + (value_type c) const;
// Appending
basic_string& append(const basic_string& str);
basic_string& append(const basic_string& str, size_type pos, size_type len = npos);
basic_string& append(const_pointer s);
basic_string& append(const_pointer s, size_type len);
basic_string& append(size_type len, value_type fill);
template<class InputIterator> basic_string& append(InputIterator first, InputIterator last);
value_type& push_back(value_type c);
value_type& push_back();
size_type append_format(const_pointer s, ...);
size_type append_format_v(const_pointer s, va_list va);
// Assigning
basic_string& assign(const basic_string& str);
basic_string& assign(const basic_string& str, size_type pos, size_type len = npos);
basic_string& assign(const_pointer s);
basic_string& assign(const_pointer s, size_type len);
basic_string& assign(size_type len, value_type fill);
template<class InputIterator> basic_string& assign(InputIterator first, InputIterator last);
basic_string& assign(Literal lit, const_pointer s);
basic_string& assign(Literal lit, const_pointer s, size_type len);
size_type format(const_pointer s, ...);
size_type format_v(const_pointer s, va_list va);
// Insertion
basic_string& insert(size_type pos, const basic_string& str);
basic_string& insert(size_type pos, const basic_string& str, size_type subpos, size_type len = npos);
basic_string& insert(size_type pos, const_pointer s);
basic_string& insert(size_type pos, const_pointer s, size_type len);
basic_string& insert(size_type pos, size_type len, value_type fill);
void insert(iterator pos, size_type len, value_type fill);
iterator insert(iterator pos, value_type c);
template<class InputIterator> void insert(iterator pos, InputIterator first, InputIterator last);
size_type insert_format(size_type pos, const_pointer s, ...);
size_type insert_format(iterator pos, const_pointer s, ...);
size_type insert_format_v(size_type pos, const_pointer s, va_list va);
size_type insert_format_v(iterator pos, const_pointer s, va_list va);
// Erasure
basic_string& erase(size_type pos = 0, size_type len = npos);
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
value_type pop_back();
// Replacement
basic_string& replace(size_type pos, size_type len, const basic_string& str);
basic_string& replace(iterator pos1, iterator pos2, const basic_string& str);
basic_string& replace(size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen = npos);
basic_string& replace(size_type pos, size_type len, const_pointer s);
basic_string& replace(iterator pos1, iterator pos2, const_pointer s);
basic_string& replace(size_type pos, size_type len, const_pointer s, size_type n);
basic_string& replace(iterator pos1, iterator pos2, const_pointer s, size_type n);
basic_string& replace(size_type pos, size_type len, size_type fill_len, value_type fill);
basic_string& replace(iterator pos1, iterator pos2, size_type fill_len, value_type fill);
template<class InputIterator> basic_string& replace(iterator pos1, iterator pos2, InputIterator first, InputIterator last);
size_type replace_format(size_type pos, size_type len, const_pointer s, ...);
size_type replace_format(iterator pos1, iterator pos2, const_pointer s, ...);
size_type replace_format_v(size_type pos, size_type len, const_pointer s, va_list va);
size_type replace_format_v(iterator pos1, iterator pos2, const_pointer s, va_list va);
// Swap
void swap(basic_string& rhs);
// String operations
const_pointer c_str() const { return elements_; }
const_pointer data() const { return elements_; }
size_type copy(pointer out, size_type n, size_type pos = 0) const;
basic_string substr(size_type pos = 0, size_type len = npos) const;
// Find
size_type find(const basic_string& str, size_type pos = 0) const;
size_type find(const_pointer s, size_type pos = 0) const;
size_type find(const_pointer s, size_type pos, size_type len) const;
size_type find(value_type c, size_type pos = 0) const;
size_type rfind(const basic_string& str, size_type pos = npos) const;
size_type rfind(const_pointer s, size_type pos = npos) const;
size_type rfind(const_pointer s, size_type pos, size_type len) const;
size_type rfind(value_type c, size_type pos = npos) const;
size_type find_i(const basic_string& str, size_type pos = 0) const;
size_type find_i(const_pointer s, size_type pos = 0) const;
size_type find_i(const_pointer s, size_type pos, size_type len) const;
size_type find_i(value_type c, size_type pos = 0) const;
size_type rfind_i(const basic_string& str, size_type pos = npos) const;
size_type rfind_i(const_pointer s, size_type pos = npos) const;
size_type rfind_i(const_pointer s, size_type pos, size_type len) const;
size_type rfind_i(value_type c, size_type pos = npos) const;
size_type find_first_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_of(const_pointer s, size_type pos = 0) const;
size_type find_first_of(const_pointer s, size_type pos, size_type len) const;
size_type find_first_of(value_type c, size_type pos = 0) const;
size_type find_last_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_of(const_pointer s, size_type pos = npos) const;
size_type find_last_of(const_pointer s, size_type pos, size_type len) const;
size_type find_last_of(value_type c, size_type pos = npos) const;
size_type find_first_not_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_not_of(const_pointer s, size_type pos = 0) const;
size_type find_first_not_of(const_pointer s, size_type pos, size_type len) const;
size_type find_first_not_of(value_type c, size_type pos = 0) const;
size_type find_last_not_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_not_of(const_pointer s, size_type pos = npos) const;
size_type find_last_not_of(const_pointer s, size_type pos, size_type len) const;
size_type find_last_not_of(value_type c, size_type pos = npos) const;
// Comparison
int compare(const basic_string& str) const;
int compare(size_type pos, size_type len, const basic_string& str) const;
int compare(size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen) const;
int compare(const_pointer s) const;
int compare(size_type pos, size_type len, const_pointer s) const;
int compare(size_type pos, size_type len, const_pointer s, size_type n) const;
int compare_i(const basic_string& str) const;
int compare_i(size_type pos, size_type len, const basic_string& str) const;
int compare_i(size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen) const;
int compare_i(const_pointer s) const;
int compare_i(size_type pos, size_type len, const_pointer s) const;
int compare_i(size_type pos, size_type len, const_pointer s, size_type n) const;
protected:
// Memory allocation must be aligned according to ref_counter's requirements
typedef atomic_integer<int> ref_counter;
enum { alignment = memory::align_selector<ref_counter>::alignment };
typedef memory::align_alloc<thor_byte, alignment> align_alloc;
static pointer empty_string()
{
static value_type NUL(0);
return &NUL;
}
virtual thor_byte* alloc(size_type raw_needed, size_type& raw_avail, bool& shareable)
{
shareable = true;
raw_avail = raw_needed;
return align_alloc::alloc(raw_needed);
}
virtual void free(thor_byte* data)
{
align_alloc::free(data);
}
pointer end_ptr() const
{
return elements_ + size_;
}
thor_byte* ref_get_mem() const
{
THOR_DEBUG_ASSERT((capacity_ + 1) > 1); // assert if capacity_ is zero or npos
return ((thor_byte*)elements_) - sizeof(ref_counter);
}
ref_counter& ref_get_counter() const
{
return *(ref_counter*)ref_get_mem();
}
int ref_get() const
{
if ((capacity_ + 1) <= 1) // zero ref count if capacity_ is zero or npos
{
return 0;
}
return ref_get_counter().get();
}
void ref_add() const
{
++ref_get_counter();
}
void ref_release()
{
// Can't release if capacity_ is zero or npos
if ((capacity_ + 1) > 1 && --ref_get_counter() == 0)
{
// Actually deleting the string
ref_get_counter().~atomic_integer();
THOR_DEBUG_INIT_MEM(ref_get_mem(), sizeof(ref_counter) + ((capacity_ + 1) * sizeof(value_type)), 0xdd);
free(ref_get_mem());
}
}
bool is_shareable() const
{
return ref_get() != 0;
}
enum make_writeable_options
{
exact = 1,
copy_existing = 2,
allow_shrink = 4,
exact_copy = exact|copy_existing,
shrink_copy = allow_shrink|copy_existing,
exact_shrink_copy = exact|copy_existing|allow_shrink
};
template<make_writeable_options options> void make_writeable(size_type needed)
{
// +1 to needed and capacity to ignore npos
if ((needed + 1) > (capacity_ + 1) || ref_get() > 1)
{
make_writeable_internal<options>(needed);
}
}
template<make_writeable_options options> void make_writeable_internal(size_type needed)
{
if (THOR_SUPPRESS_WARNING((options & shrink_copy) == copy_existing) && needed < size_)
{
needed = size_;
}
if (!THOR_SUPPRESS_WARNING(options & exact) && (capacity_ + 1) > 1)
{
// exponential growth
needed = thor::_max(needed, capacity_ + (capacity_ >> 1));
// round to multiple of 16 bytes
needed = (needed + 15) & ~0xF;
}
// Allocate new space
bool shareable;
size_type raw_avail;
size_type raw_needed = ((needed + 1) * sizeof(value_type)) + sizeof(ref_counter);
thor_byte* data = alloc(raw_needed, raw_avail, shareable);
THOR_DEBUG_INIT_MEM(data, raw_avail, 0xbb);
new (data) ref_counter(shareable ? 1 : 0);
pointer new_string = (pointer)(data + sizeof(ref_counter));
size_type new_capacity = ((raw_avail - sizeof(ref_counter)) / sizeof(value_type)) - 1;
size_type new_size = size_;
// Copy old string to new
if (THOR_SUPPRESS_WARNING(options & copy_existing))
{
if (THOR_SUPPRESS_WARNING(options & allow_shrink))
{
const size_type copy_len = thor::_min(needed, size_);
typetraits<T>::copy(new_string, elements_, copy_len);
new_string[copy_len] = T(0);
}
else
{
typetraits<T>::copy(new_string, elements_, size_ + 1);
}
}
ref_release();
elements_ = new_string;
size_ = new_size;
capacity_ = new_capacity;
}
private:
pointer elements_;
size_type size_;
size_type capacity_;
};
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
// UTF-8 to/from wchar_t support:
string wide_to_utf8(const wchar_t* s);
string wide_to_utf8(const wstring& str);
bool wide_to_utf8(const wchar_t* s, string& out);
bool wide_to_utf8(const wstring& str, string& out);
wstring utf8_to_wide(const char* s);
wstring utf8_to_wide(const string& str);
bool utf8_to_wide(const char* s, wstring& out);
bool utf8_to_wide(const string& str, wstring& out);
bool utf8_is_valid(const char* s);
bool utf8_is_valid(const string& str);
size_type wide_length(const char* s);
size_type wide_length(const string& str);
size_type utf8_length(const wchar_t* s);
size_type utf8_length(const wstring& str);
///////////////////////////////////////////////////////////////////////////////
// Inline implementations
///////////////////////////////////////////////////////////////////////////////
// Constructors
template<typename T> basic_string<T>::basic_string()
: elements_(empty_string())
, size_(0)
, capacity_(0)
{}
template<typename T> basic_string<T>::basic_string(const basic_string<T>& str)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(str);
}
template<typename T> basic_string<T>::basic_string(const basic_string& str, size_type pos, size_type len = npos)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(str, pos, len);
}
template<typename T> basic_string<T>::basic_string(const_pointer s)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(s);
}
template<typename T> basic_string<T>::basic_string(const_pointer s, size_type len)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(s, len);
}
template<typename T> basic_string<T>::basic_string(size_type len, value_type fill)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(len, fill);
}
template<typename T> template<class InputIterator> basic_string<T>::basic_string(InputIterator first, InputIterator last)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
assign(first, last);
}
template<typename T> basic_string<T>::basic_string(Format, const_pointer s, ...)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
va_list va;
va_start(va, s);
format_v(s, va);
va_end(va);
}
template<typename T> basic_string<T>::basic_string(const_pointer s, va_list va)
: elements_(empty_string())
, size_(0)
, capacity_(0)
{
format_v(s, va);
}
template<typename T> basic_string<T>::basic_string(Literal lit, const_pointer s)
: elements_(const_cast<pointer>(s))
, size_(string_length(s))
, capacity_(lit == lit_allow_share ? npos : 0)
{
}
template<typename T> basic_string<T>::basic_string(Literal lit, const_pointer s, size_type len)
: elements_(const_cast<pointer>(s))
, size_(len)
, capacity_(lit == lit_allow_share ? npos : 0)
{
// This is a literal string; we can't insert a NUL character at len, so the length
// must match.
THOR_DEBUG_ASSERT(len == string_length(s));
}
template<typename T> basic_string<T>::~basic_string()
{
ref_release();
}
template<typename T> void basic_string<T>::resize(size_type n)
{
if (n != size_)
{
make_writeable<shrink_copy>(n);
if (n < size_)
{
size_ = n;
*end_ptr() = T(0);
}
else
{
typetraits<T>::range_copy(elements_ + size_, elements_ + n + 1, T(0));
size_ = n;
}
}
}
template<typename T> void basic_string<T>::resize(size_type n, value_type c)
{
if (n != size_)
{
make_writeable<shrink_copy>(n);
if (n < size_)
{
size_ = n;
*end_ptr() = T(0);
}
else
{
typetraits<T>::range_copy(elements_ + size_, elements_ + n, c);
size_ = n;
}
}
}
template<typename T> void basic_string<T>::reserve(size_type n)
{
// +1 to n and capacity_ to ignore npos
if ((n + 1) > (capacity_ + 1))
{
make_writeable<exact_copy>(n);
}
}
template<typename T> void basic_string<T>::clear()
{
if (ref_get() == 1)
{
size_ = 0;
*end_ptr() = T(0);
}
else
{
ref_release();
elements_ = empty_string();
size_ = 0;
capacity_ = 0;
}
}
template<typename T> void basic_string<T>::reduce(size_type n)
{
// No point in doing this for a string that we don't own
if (ref_get() != 1) return;
if (n < size_) n = size_;
if (n == 0)
{
ref_release();
elements_ = empty_string();
size_ = 0;
capacity_ = 0;
}
else if (capacity_ > n)
{
make_writeable_internal<exact_shrink_copy>(n);
}
}
template<typename T> typename basic_string<T>::reference basic_string<T>::operator [] (size_type index)
{
THOR_DEBUG_ASSERT(index < size_); // Don't allow NUL
make_writeable<copy_existing>(size_);
return elements_[index];
}
template<typename T> typename basic_string<T>::const_reference basic_string<T>::operator [] (size_type index) const
{
THOR_DEBUG_ASSERT(index <= size_);
return elements_[index];
}
template<typename T> typename basic_string<T>::reference basic_string<T>::at(size_type index)
{
THOR_DEBUG_ASSERT(index < size_); // Don't allow NUL
make_writeable<copy_existing>(size_);
return elements_[index];
}
template<typename T> typename basic_string<T>::const_reference basic_string<T>::at(size_type index) const
{
THOR_DEBUG_ASSERT(index <= size_);
return elements_[index];
}
template<typename T> typename basic_string<T>::reference basic_string<T>::front()
{
THOR_DEBUG_ASSERT(!empty());
make_writeable<copy_existing>(size_);
return elements_[0];
}
template<typename T> typename basic_string<T>::const_reference basic_string<T>::front() const
{
THOR_DEBUG_ASSERT(!empty());
return elements_[0];
}
template<typename T> typename basic_string<T>::reference basic_string<T>::back()
{
THOR_DEBUG_ASSERT(!empty());
make_writeable<copy_existing>(size_);
return elements_[size_ - 1];
}
template<typename T> typename basic_string<T>::const_reference basic_string<T>::back() const
{
THOR_DEBUG_ASSERT(!empty());
return elements_[size_ - 1];
}
template<typename T> basic_string<T>& basic_string<T>::operator = (const basic_string& str)
{
return assign(str);
}
template<typename T> basic_string<T>& basic_string<T>::operator = (const_pointer s)
{
return assign(s);
}
template<typename T> basic_string<T>& basic_string<T>::operator = (value_type c)
{
return assign(1, c);
}
template<typename T> basic_string<T>& basic_string<T>::operator += (const basic_string& str)
{
return append(str);
}
template<typename T> basic_string<T>& basic_string<T>::operator += (const_pointer s)
{
return append(s);
}
template<typename T> basic_string<T>& basic_string<T>::operator += (value_type c)
{
return append(1, c);
}
template<typename T> basic_string<T> basic_string<T>::operator + (const basic_string& str) const
{
basic_string temp(*this);
temp += str;
return temp;
}
template<typename T> basic_string<T> basic_string<T>::operator + (const_pointer s) const
{
basic_string temp(*this);
temp += s;
return temp;
}
template<typename T> basic_string<T> basic_string<T>::operator + (value_type c) const
{
basic_string temp(*this);
temp += c;
return temp;
}
template<typename T> basic_string<T>& basic_string<T>::append(const basic_string& str)
{
if (empty())
{
assign(str);
}
else if (!str.empty())
{
make_writeable<copy_existing>(size_ + str.size_);
typetraits<T>::copy(end_ptr(), str.elements_, str.size_ + 1);
size_ += str.size_;
}
return *this;
}
template<typename T> basic_string<T>& basic_string<T>::append(const basic_string& str, size_type pos, size_type len = npos)
{
if (empty())
{
assign(str, pos, len);
}
else
{
THOR_DEBUG_ASSERT(str.size_ >= pos);
const size_type max_len = str.size_ - pos;
if (len > max_len) len = max_len;
if (len > 0)
{
make_writeable<copy_existing>(size_ + len);
typetraits<T>::copy(end_ptr(), str.elements_ + pos, len);
size_ += len;
*end_ptr() = T(0);
}
}
return *this;
}
template<typename T> basic_string<T>& basic_string<T>::append(const_pointer s)
{
return append(s, string_length(s));
}
template<typename T> basic_string<T>& basic_string<T>::append(const_pointer s, size_type len)
{
if (empty())
{
assign(s, len);
}
else if (len != 0)
{
make_writeable<copy_existing>(size_ + len);
typetraits<T>::copy(end_ptr(), s, len);
size_ += len;
*end_ptr() = T(0);
}
return *this;
}
template<typename T> basic_string<T>& basic_string<T>::append(size_type len, value_type fill)
{
if (empty())
{
assign(len, fill);
}
else if (len != 0)
{
make_writeable<copy_existing>(size_ + len);
typetraits<T>::range_copy(end_ptr(), end_ptr() + len, fill);
size_ += len;
*end_ptr() = T(0);
}
return *this;
}
template<typename T> template<class InputIterator> basic_string<T>& basic_string<T>::append(InputIterator first, InputIterator last)
{
if (empty())
{
assign(first, last);
}
else
{
difference_type len = distance(first, last);
THOR_DEBUG_ASSERT(len >= 0);
if (len > 0)
{
make_writeable<copy_existing>(size_ + len);
pointer p = end_ptr();
do
{
THOR_DEBUG_ASSERT(p < &elements_[size_ + len]);
*p++ = *first++;
} while (first != last);
size_ += len;
*end_ptr() = T(0);
}
}
return *this;
}
template<typename T> typename basic_string<T>::value_type& basic_string<T>::push_back(value_type c)
{
make_writeable<copy_existing>(size_ + 1);
elements_[size_++] = c;
*end_ptr() = T(0);
return back();
}
template<typename T> typename basic_string<T>::value_type& basic_string<T>::push_back()
{
make_writeable<copy_existing>(size_ + 1);
THOR_DEBUG_ASSERT(*end_ptr() == T(0));
elements_[++size_] = T(0);
return back();
}
template<typename T> typename basic_string<T>::size_type basic_string<T>::append_format(const_pointer s, ...)
{
va_list va;
va_start(va, s);
size_type len = append_format_v(s, va);
va_end(va);
return len;
}
template<typename T> typename basic_string<T>::size_type basic_string<T>::append_format_v(const_pointer s, va_list va)
{
const size_type len = string_format_count_v(s, va);
if (len != 0 && len != npos)
{
make_writeable<copy_existing>(size_ + len);
const size_type written = string_format_v(elements_ + size_, len + 1, s, va);
THOR_DEBUG_ASSERT(written == len); THOR_UNUSED(written);
size_ += len;
THOR_DEBUG_ASSERT(*end_ptr() == T(0));
}
return len;
}
///////////////////////////////////////////////////////////////////////////////
template<typename T> basic_string<T>& basic_string<T>::assign(const basic_string& str)
{
if (str.elements_ != elements_)
{
if (str.is_shareable())
{
// Shareable string
str.ref_add();
ref_release();
elements_ = str.elements_;
size_ = str.size_;
capacity_ = str.capacity_;
}
else if (str.capacity_ == str.max_capacity())
{
// Literal string
ref_release();
elements_ = str.elements_;
size_ = str.size_;
capacity_ = str.capacity_;
}
else if (str.empty())
{
clear();
}
else
{
// Not shareable; make a copy
make_writeable<exact>(str.size_);
size_ = str.size_;
typetraits<T>::copy(elements_, str.elements_, size_ + 1);
THOR_DEBUG_ASSERT(*end_ptr() == T(0));
}
}
return *this;
}
template<typename T> basic_string<T>& basic_string<T>::assign(const basic_string& str, size_type pos, size_type len)
{
if (pos == 0 && len >= str.size_)
{
assign(str);
}
else
{
THOR_DEBUG_ASSERT(pos <= str.size_);
const size_type max_len = str.size_ - pos;
if (len > max_len) len = max_len;