This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
string.d
1415 lines (1235 loc) · 54 KB
/
string.d
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
/**
* D header file for interaction with C++ std::string.
*
* Copyright: Copyright (c) 2019 D Language Foundation
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Guillaume Chatelet
* Manu Evans
* Source: $(DRUNTIMESRC core/stdcpp/string.d)
*/
module core.stdcpp.string;
import core.stdcpp.allocator;
import core.stdcpp.xutility : StdNamespace;
import core.stdc.stddef : wchar_t;
version (OSX)
{
// Apple decided to rock a different ABI... good for them!
version = _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT;
}
version (CppRuntime_Gcc)
{
version (_GLIBCXX_USE_CXX98_ABI)
{
private enum StringNamespace = "std";
// version = __GTHREADS; // TODO: we need to make ref-count interactions atomic
}
else
{
import core.internal.traits : AliasSeq;
private enum StringNamespace = AliasSeq!("std", "__cxx11");
}
}
else
alias StringNamespace = StdNamespace;
enum DefaultConstruct { value }
/// Constructor argument for default construction
enum Default = DefaultConstruct();
@nogc:
/**
* Character traits classes specify character properties and provide specific
* semantics for certain operations on characters and sequences of characters.
*/
extern(C++, (StdNamespace)) struct char_traits(CharT) {}
// I don't think we can have these here, otherwise symbols are emit to druntime, and we don't want that...
//alias std_string = basic_string!char;
//alias std_u16string = basic_string!wchar; // TODO: can't mangle these yet either...
//alias std_u32string = basic_string!dchar;
//alias std_wstring = basic_string!wchar_t; // TODO: we can't mangle wchar_t properly (yet?)
/**
* D language counterpart to C++ std::basic_string.
*
* C++ reference: $(LINK2 https://en.cppreference.com/w/cpp/string/basic_string)
*/
extern(C++, class)
extern(C++, (StringNamespace))
struct basic_string(T, Traits = char_traits!T, Alloc = allocator!T)
{
extern(D):
@nogc:
///
enum size_type npos = size_type.max;
///
alias size_type = size_t;
///
alias difference_type = ptrdiff_t;
///
alias value_type = T;
///
alias traits_type = Traits;
///
alias allocator_type = Alloc;
///
alias pointer = value_type*;
///
alias const_pointer = const(value_type)*;
///
alias toString = as_array;
/// MSVC allocates on default initialisation in debug, which can't be modelled by D `struct`
@disable this();
///
alias length = size;
///
alias opDollar = length;
///
bool empty() const nothrow @safe { return size() == 0; }
///
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const pure nothrow @safe @nogc { return [start, end]; }
///
ref inout(T) opIndex(size_t index) inout pure nothrow @safe @nogc { return as_array[index]; }
///
inout(T)[] opIndex(size_t[2] slice) inout pure nothrow @safe @nogc { return as_array[slice[0] .. slice[1]]; }
///
inout(T)[] opIndex() inout pure nothrow @safe @nogc { return as_array(); }
///
void clear() { eos(0); } // TODO: bounds-check
///
void resize(size_type n, T c = T(0)) @trusted
{
if (n <= size())
eos(n);
else
assert(false); // append(n - size(), c); // write this
}
// void reserve(size_type n = 0) @trusted;
// void shrink_to_fit();
///
ref inout(T) front() inout nothrow @safe { return this[0]; }
///
ref inout(T) back() inout nothrow @safe { return this[$-1]; }
///
const(T)* c_str() const nothrow @safe { return data(); }
// Modifiers
///
ref basic_string opAssign()(auto ref basic_string str) { return assign(str.as_array); }
// ref basic_string assign(size_type n, T c);
///
ref basic_string opAssign(const(T)[] str) { return assign(str); }
///
ref basic_string opAssign(T c) { return assign((&c)[0 .. 1]); }
///
ref basic_string opIndexAssign(T c, size_t index) { as_array[index] = c; return this; }
///
ref basic_string opIndexAssign(T c, size_t[2] slice) { as_array[slice[0] .. slice[1]] = c; return this; }
///
ref basic_string opIndexAssign(const(T)[] str, size_t[2] slice) { as_array[slice[0] .. slice[1]] = str[]; return this; }
///
ref basic_string opIndexAssign(T c) { as_array[] = c; return this; }
///
ref basic_string opIndexAssign(const(T)[] str) { as_array[] = str[]; return this; }
///
ref basic_string opIndexOpAssign(string op)(T c, size_t index) { mixin("as_array[index] " ~ op ~ "= c;"); return this; }
///
ref basic_string opIndexOpAssign(string op)(T c, size_t[2] slice) { mixin("as_array[slice[0] .. slice[1]] " ~ op ~ "= c;"); return this; }
///
ref basic_string opIndexOpAssign(string op)(const(T)[] str, size_t[2] slice) { mixin("as_array[slice[0] .. slice[1]] " ~ op ~ "= str[];"); return this; }
///
ref basic_string opIndexOpAssign(string op)(T c) { mixin("as_array[] " ~ op ~ "= c;"); return this; }
///
ref basic_string opIndexOpAssign(string op)(const(T)[] str) { mixin("as_array[] " ~ op ~ "= str[];"); return this; }
// ref basic_string append(size_type n, T c);
///
ref basic_string append(T c) { return append((&c)[0 .. 1]); }
///
ref basic_string opOpAssign(string op : "~")(const(T)[] str) { return append(str); }
///
ref basic_string opOpAssign(string op : "~")(T c) { return append((&c)[0 .. 1]); }
// ref basic_string insert(size_type pos, ref const(basic_string) str);
// ref basic_string insert(size_type pos, ref const(basic_string) str, size_type subpos, size_type sublen);
// ref basic_string insert(size_type pos, const(T)* s) nothrow { assert(s); return insert(pos, s, strlen(s)); }
// ref basic_string insert(size_type pos, const(T)* s, size_type n) nothrow @trusted;
// ref basic_string insert(size_type pos, size_type n, T c);
// ref basic_string insert(size_type pos, const(T)[] s) nothrow @safe { insert(pos, &s[0], s.length); return this; }
///
ref basic_string erase(size_type pos = 0) // TODO: bounds-check
{
// _My_data._Check_offset(pos);
eos(pos);
return this;
}
///
ref basic_string erase(size_type pos, size_type len) // TODO: bounds-check
{
// _My_data._Check_offset(pos);
T[] str = as_array();
size_type new_len = str.length - len;
this[pos .. new_len] = this[pos + len .. str.length]; // TODO: should be memmove!
eos(new_len);
return this;
}
// replace
// swap
///
void push_back(T c) @trusted { append((&c)[0 .. 1]); }
///
void pop_back() { erase(size() - 1); }
version (CppRuntime_Microsoft)
{
//----------------------------------------------------------------------------------
// Microsoft runtime
//----------------------------------------------------------------------------------
///
this(DefaultConstruct) { _Alloc_proxy(); _Tidy_init(); }
///
this(const(T)[] str) { _Alloc_proxy(); _Tidy_init(); assign(str); }
///
this(const(T)[] str, ref const(allocator_type) al) { _Alloc_proxy(); _AssignAllocator(al); _Tidy_init(); assign(str); }
///
this(this)
{
_Alloc_proxy();
if (_Get_data()._IsAllocated())
{
T[] _Str = _Get_data()._Mystr;
_Tidy_init();
assign(_Str);
}
}
///
~this() { _Tidy_deallocate(); }
///
ref inout(Alloc) get_allocator() inout { return _Getal(); }
///
size_type max_size() const nothrow @safe { return ((size_t.max / T.sizeof) - 1) / 2; } // HACK: clone the windows version precisely?
///
size_type size() const nothrow @safe { return _Get_data()._Mysize; }
///
size_type capacity() const nothrow @safe { return _Get_data()._Myres; }
///
inout(T)* data() inout @safe { return _Get_data()._Myptr; }
///
inout(T)[] as_array() inout nothrow @trusted { return _Get_data()._Myptr[0 .. _Get_data()._Mysize]; }
///
ref inout(T) at(size_type i) inout nothrow @trusted { return _Get_data()._Myptr[0 .. _Get_data()._Mysize][i]; }
///
ref basic_string assign(const(T)[] str)
{
size_type _Count = str.length;
auto _My_data = &_Get_data();
if (_Count <= _My_data._Myres)
{
T* _Old_ptr = _My_data._Myptr;
_My_data._Mysize = _Count;
_Old_ptr[0 .. _Count] = str[]; // TODO: this needs to be a memmove(), does that work here?
_Old_ptr[_Count] = T(0);
return this;
}
return _Reallocate_for(_Count, (T* _New_ptr, size_type _Count, const(T)* _Ptr) nothrow {
_New_ptr[0 .. _Count] = _Ptr[0 .. _Count];
_New_ptr[_Count] = T(0);
}, str.ptr);
}
///
ref basic_string append(const(T)[] str)
{
size_type _Count = str.length;
auto _My_data = &_Get_data();
size_type _Old_size = _My_data._Mysize;
if (_Count <= _My_data._Myres - _Old_size)
{
pointer _Old_ptr = _My_data._Myptr;
_My_data._Mysize = _Old_size + _Count;
_Old_ptr[_Old_size .. _Old_size + _Count] = str[]; // TODO: this needs to be a memmove(), does that work here?
_Old_ptr[_Old_size + _Count] = T(0);
return this;
}
return _Reallocate_grow_by(_Count, (T* _New_ptr, const(T)[] _Old_str, const(T)[] _Str) {
_New_ptr[0 .. _Old_str.length] = _Old_str[];
_New_ptr[_Old_str.length .. _Old_str.length + _Str.length] = _Str[];
_New_ptr[_Old_str.length + _Str.length] = T(0);
}, str);
}
private:
import core.stdcpp.xutility : MSVCLinkDirectives;
// Make sure the object files wont link against mismatching objects
mixin MSVCLinkDirectives!true;
pragma (inline, true)
{
void eos(size_type offset) nothrow { _Get_data()._Myptr[_Get_data()._Mysize = offset] = T(0); }
ref inout(_Base.Alloc) _Getal() inout nothrow @safe { return _Base._Mypair._Myval1; }
ref inout(_Base.ValTy) _Get_data() inout nothrow @safe { return _Base._Mypair._Myval2; }
}
void _Alloc_proxy() nothrow
{
static if (_ITERATOR_DEBUG_LEVEL > 0)
_Base._Alloc_proxy();
}
void _AssignAllocator(ref const(allocator_type) al) nothrow
{
static if (_Base._Mypair._HasFirst)
_Getal() = al;
}
void _Tidy_init() nothrow
{
auto _My_data = &_Get_data();
_My_data._Mysize = 0;
_My_data._Myres = _My_data._BUF_SIZE - 1;
_My_data._Bx._Buf[0] = T(0);
}
size_type _Calculate_growth(size_type _Requested) const nothrow
{
auto _My_data = &_Get_data();
size_type _Masked = _Requested | _My_data._ALLOC_MASK;
size_type _Old = _My_data._Myres;
size_type _Expanded = _Old + _Old / 2;
return _Masked > _Expanded ? _Masked : _Expanded;
}
ref basic_string _Reallocate_for(_ArgTys...)(size_type _New_size, void function(pointer, size_type, _ArgTys) nothrow @nogc _Fn, _ArgTys _Args)
{
auto _My_data = &_Get_data();
size_type _Old_capacity = _My_data._Myres;
size_type _New_capacity = _Calculate_growth(_New_size);
auto _Al = &_Getal();
pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
_Base._Orphan_all();
_My_data._Mysize = _New_size;
_My_data._Myres = _New_capacity;
_Fn(_New_ptr, _New_size, _Args);
if (_My_data._BUF_SIZE <= _Old_capacity)
_Al.deallocate(_My_data._Bx._Ptr, _Old_capacity + 1);
_My_data._Bx._Ptr = _New_ptr;
return this;
}
ref basic_string _Reallocate_grow_by(_ArgTys...)(size_type _Size_increase, void function(pointer, const(T)[], _ArgTys) nothrow @nogc _Fn, _ArgTys _Args)
{
auto _My_data = &_Get_data();
size_type _Old_size = _My_data._Mysize;
size_type _New_size = _Old_size + _Size_increase;
size_type _Old_capacity = _My_data._Myres;
size_type _New_capacity = _Calculate_growth(_New_size);
auto _Al = &_Getal();
pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
_Base._Orphan_all();
_My_data._Mysize = _New_size;
_My_data._Myres = _New_capacity;
if (_My_data._BUF_SIZE <= _Old_capacity)
{
pointer _Old_ptr = _My_data._Bx._Ptr;
_Fn(_New_ptr, _Old_ptr[0 .. _Old_size], _Args);
_Al.deallocate(_Old_ptr, _Old_capacity + 1);
}
else
_Fn(_New_ptr, _My_data._Bx._Buf[0 .. _Old_size], _Args);
_My_data._Bx._Ptr = _New_ptr;
return this;
}
void _Tidy_deallocate()
{
_Base._Orphan_all();
auto _My_data = &_Get_data();
if (_My_data._BUF_SIZE <= _My_data._Myres)
{
pointer _Ptr = _My_data._Bx._Ptr;
auto _Al = &_Getal();
_Al.deallocate(_Ptr, _My_data._Myres + 1);
}
_My_data._Mysize = 0;
_My_data._Myres = _My_data._BUF_SIZE - 1;
_My_data._Bx._Buf[0] = T(0);
}
_String_alloc!(_String_base_types!(T, Alloc)) _Base;
}
else version (CppRuntime_Gcc)
{
version (_GLIBCXX_USE_CXX98_ABI)
{
//----------------------------------------------------------------------------------
// Old GCC/libstdc++ ref-counted implementation
//----------------------------------------------------------------------------------
///
this(DefaultConstruct)
{
version (_GLIBCXX_FULLY_DYNAMIC_STRING)
static_assert(false, "DO WE NEED THIS?");
else
_M_data = _S_empty_rep()._M_refdata();
}
///
this(const(T)[] str, ref const(allocator_type) al) { _M_assign_allocator(al); this(str); }
///
this(const(T)[] str)
{
_M_data = _S_construct(str.ptr, str.ptr + str.length, _M_get_allocator);
}
///
this(this)
{
if (_M_rep() != &_S_empty_rep())
{
// TODO: atomic inc
++_M_rep()._M_refcount;
}
}
///
~this() { _M_rep()._M_dispose(get_allocator()); }
///
ref inout(Alloc) get_allocator() inout { return _M_get_allocator(); }
///
size_type max_size() const nothrow @safe { return _Rep._S_max_size; }
///
size_type size() const nothrow @safe { return _M_rep()._M_length; }
///
size_type capacity() const nothrow { return _M_rep()._M_capacity; }
///
inout(T)* data() inout @safe { return _M_data; }
///
inout(T)[] as_array() inout nothrow @trusted { return _M_data[0 .. _M_rep()._M_length]; }
///
ref inout(T) at(size_type i) inout nothrow { return _M_data[0 .. _M_rep()._M_length][i]; }
///
ref basic_string assign(const(T)[] str)
{
const(T)* __s = str.ptr;
size_t __n = str.length;
// __glibcxx_requires_string_len(__s, __n);
_M_check_length(size(), __n, "basic_string::assign");
if (_M_disjunct(__s) || _M_rep()._M_is_shared())
return _M_replace_safe(size_type(0), this.size(), __s, __n);
else
{
const size_type __pos = __s - _M_data;
if (__pos >= __n)
_S_copy(_M_data, __s, __n);
else if (__pos)
_S_move(_M_data, __s, __n);
_M_rep()._M_set_length_and_sharable(__n);
return this;
}
}
///
ref basic_string append(const(T)[] str)
{
const(T)* __s = str.ptr;
size_t __n = str.length;
// __glibcxx_requires_string_len(__s, __n);
if (__n)
{
_M_check_length(size_type(0), __n, "basic_string::append");
const size_type __len = __n + size();
if (__len > capacity() || _M_rep()._M_is_shared())
{
if (_M_disjunct(__s))
reserve(__len);
else
{
const size_type __off = __s - _M_data;
reserve(__len);
__s = _M_data + __off;
}
}
_S_copy(_M_data + size(), __s, __n);
_M_rep()._M_set_length_and_sharable(__len);
}
return this;
}
void reserve(size_type __res)
{
if (__res != capacity() || _M_rep()._M_is_shared())
{
// Make sure we don't shrink below the current size
if (__res < size())
__res = size();
allocator_type __a = get_allocator();
T* __tmp = _M_rep()._M_clone(__a, __res - size());
_M_rep()._M_dispose(__a);
_M_data = __tmp;
}
}
private:
import core.stdcpp.type_traits : is_empty;
version (__GTHREADS)
{
import core.atomic;
alias _Atomic_word = int; // should we use atomic!int?
}
else
alias _Atomic_word = int;
struct _Rep_base
{
size_type _M_length;
size_type _M_capacity;
_Atomic_word _M_refcount;
}
struct _Rep
{
_Rep_base base;
alias base this;
alias _Raw_bytes_alloc = Alloc.rebind!char;
enum size_type _S_max_size = (((npos - _Rep_base.sizeof) / T.sizeof) - 1) / 4;
enum T _S_terminal = T(0);
__gshared size_type[(_Rep_base.sizeof + T.sizeof + size_type.sizeof - 1) / size_type.sizeof] _S_empty_rep_storage;
static ref _Rep _S_empty_rep() nothrow @trusted { return *cast(_Rep*)_S_empty_rep_storage.ptr; }
void _M_set_sharable() nothrow
{
_M_refcount = 0;
}
void _M_set_length_and_sharable(size_type __n) nothrow
{
if (&this != &_S_empty_rep())
{
_M_set_sharable();
_M_length = __n;
_M_refdata()[__n] = _S_terminal;
}
}
// bool _M_is_leaked() const nothrow
// {
//// version (__GTHREADS)
//// return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
//// else
// return _M_refcount < 0;
// }
//
bool _M_is_shared() const nothrow
{
// version (__GTHREADS)
// return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
// else
return _M_refcount > 0;
}
T* _M_refdata() nothrow @trusted { return cast(T*)(&this + 1); }
static _Rep* _S_create(size_type __capacity, size_type __old_capacity, ref Alloc __alloc)
{
assert(__capacity <= _S_max_size);
// if (__capacity > _S_max_size)
// __throw_length_error(__N("basic_string::_S_create"));
enum __pagesize = 4096;
enum __malloc_header_size = 4 * pointer.sizeof;
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
__capacity = 2 * __old_capacity;
size_type __size = (__capacity + 1) * T.sizeof + _Rep.sizeof;
const size_type __adj_size = __size + __malloc_header_size;
if (__adj_size > __pagesize && __capacity > __old_capacity)
{
const size_type __extra = __pagesize - __adj_size % __pagesize;
__capacity += __extra / T.sizeof;
if (__capacity > _S_max_size)
__capacity = _S_max_size;
__size = (__capacity + 1) * T.sizeof + _Rep.sizeof;
}
_Rep* __p = cast(_Rep*)_Raw_bytes_alloc(__alloc).allocate(__size);
*__p = _Rep.init;
__p._M_capacity = __capacity;
__p._M_set_sharable();
return __p;
}
void _M_dispose(ref Alloc __a)
{
if (&this != &_S_empty_rep())
{
// // Be race-detector-friendly. For more info see bits/c++config.
// _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
// // Decrement of _M_refcount is acq_rel, because:
// // - all but last decrements need to release to synchronize with
// // the last decrement that will delete the object.
// // - the last decrement needs to acquire to synchronize with
// // all the previous decrements.
// // - last but one decrement needs to release to synchronize with
// // the acquire load in _M_is_shared that will conclude that
// // the object is not shared anymore.
// if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
// -1) <= 0)
// {
// _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
// _M_destroy(__a);
// }
// TODO: atomic version!!
if (--_M_refcount <= 0)
_M_destroy(__a);
}
}
void _M_destroy(ref Alloc __a)
{
const size_type __size = _Rep_base.sizeof + (_M_capacity + 1) * T.sizeof;
_Raw_bytes_alloc(__a).deallocate(cast(char*)&this, __size);
}
T* _M_clone(ref Alloc __alloc, size_type __res)
{
const size_type __requested_cap = _M_length + __res;
_Rep* __r = _S_create(__requested_cap, _M_capacity, __alloc);
if (_M_length)
_S_copy(__r._M_refdata(), _M_refdata(), _M_length);
__r._M_set_length_and_sharable(_M_length);
return __r._M_refdata();
}
}
static if (!is_empty!allocator_type.value)
allocator_type _M_Alloc;
T* _M_p; // The actual data.
alias _M_data = _M_p;
pragma (inline, true)
{
void eos(size_type offset)
{
_M_mutate(offset, size() - offset, size_type(0));
}
ref inout(allocator_type) _M_get_allocator() inout
{
static if (!is_empty!allocator_type.value)
return _M_Alloc;
else
return *cast(inout(allocator_type)*)&this;
}
_Rep* _M_rep() const nothrow @trusted { return &(cast(_Rep*)_M_data)[-1]; }
}
size_type _M_check(size_type __pos, const char* __s) const
{
assert(__pos <= size());
// if (__pos > size())
// __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
// "this->size() (which is %zu)"),
// __s, __pos, this->size());
return __pos;
}
static ref _Rep _S_empty_rep() nothrow
{
return _Rep._S_empty_rep();
}
static T* _S_construct(const(T)* __beg, const(T)* __end, ref Alloc __a)
{
version (_GLIBCXX_FULLY_DYNAMIC_STRING) {} else
{
if (__beg == __end && __a == Alloc())
return _S_empty_rep()._M_refdata();
}
const size_type __dnew = __end - __beg;
_Rep* __r = _Rep._S_create(__dnew, size_type(0), __a);
_S_copy(__r._M_refdata(), __beg, __end - __beg);
__r._M_set_length_and_sharable(__dnew);
return __r._M_refdata();
}
ref basic_string _M_replace_safe(size_type __pos1, size_type __n1, const(T)* __s, size_type __n2)
{
_M_mutate(__pos1, __n1, __n2);
if (__n2)
_S_copy(_M_data + __pos1, __s, __n2);
return this;
}
void _M_mutate(size_type __pos, size_type __len1, size_type __len2)
{
const size_type __old_size = size();
const size_type __new_size = __old_size + __len2 - __len1;
const size_type __how_much = __old_size - __pos - __len1;
if (__new_size > capacity() || _M_rep()._M_is_shared())
{
allocator_type __a = get_allocator();
_Rep* __r = _Rep._S_create(__new_size, capacity(), __a);
if (__pos)
_S_copy(__r._M_refdata(), _M_data, __pos);
if (__how_much)
_S_copy(__r._M_refdata() + __pos + __len2, _M_data + __pos + __len1, __how_much);
_M_rep()._M_dispose(__a);
_M_data = __r._M_refdata();
}
else if (__how_much && __len1 != __len2)
_S_move(_M_data + __pos + __len2, _M_data + __pos + __len1, __how_much);
_M_rep()._M_set_length_and_sharable(__new_size);
}
}
else
{
pragma(msg, "libstdc++ std::__cxx11::basic_string is not yet supported; the struct contains an interior pointer which breaks D move semantics!");
//----------------------------------------------------------------------------------
// GCC/libstdc++ modern implementation
//----------------------------------------------------------------------------------
///
this(DefaultConstruct) { _M_p = _M_local_data(); _M_set_length(0); }
///
this(const(T)[] str, ref const(allocator_type) al) { _M_assign_allocator(al); this(str); }
///
this(const(T)[] str)
{
_M_p = _M_local_data();
_M_construct(str.ptr, str.length);
}
///
this(this)
{
assert(false);
// TODO: how do I know if it was local before?!
}
///
~this() { _M_dispose(); }
///
ref inout(Alloc) get_allocator() inout { return _M_get_allocator(); }
///
size_type max_size() const nothrow @safe { return ((size_t.max / T.sizeof) - 1) / 2; }
///
size_type size() const nothrow @safe { return _M_string_length; }
///
size_type capacity() const nothrow { return _M_is_local ? _S_local_capacity : _M_allocated_capacity; }
///
inout(T)* data() inout @safe { return _M_data; }
///
inout(T)[] as_array() inout nothrow @trusted { return _M_data[0 .. _M_string_length]; }
///
ref inout(T) at(size_type i) inout nothrow { return _M_data[0 .. _M_string_length][i]; }
///
ref basic_string assign(const(T)[] str)
{
// __glibcxx_requires_string_len(str.ptr, str.length);
return _M_replace(size_type(0), size(), str.ptr, str.length);
}
///
ref basic_string append(const(T)[] str)
{
// __glibcxx_requires_string_len(str.ptr, str.length);
_M_check_length(size_type(0), str.length, "basic_string::append");
return _M_append(str.ptr, str.length);
}
private:
// import core.exception : RangeError;
import core.stdcpp.type_traits : is_empty;
static if (!is_empty!allocator_type.value)
allocator_type _M_Alloc;
pointer _M_p; // The actual data.
size_type _M_string_length;
enum size_type _S_local_capacity = 15 / T.sizeof;
union
{
T[_S_local_capacity + 1] _M_local_buf;
size_type _M_allocated_capacity;
}
alias _M_length = _M_string_length;
alias _M_capacity = _M_allocated_capacity;
alias _M_data = _M_p;
pragma (inline, true)
{
void eos(size_type offset) nothrow { _M_set_length(offset); }
inout(pointer) _M_local_data() inout { return _M_local_buf.ptr; }
bool _M_is_local() const { return _M_data == _M_local_data; }
ref inout(allocator_type) _M_get_allocator() inout
{
static if (!is_empty!allocator_type.value)
return _M_Alloc;
else
return *cast(inout(allocator_type)*)&this;
}
void _M_set_length(size_type __n)
{
_M_length = __n;
_M_data[__n] = T(0);
}
}
void _M_construct(const(T)* __beg, size_type __dnew)
{
if (__dnew > _S_local_capacity)
{
_M_data = _M_create(__dnew, size_type(0));
_M_capacity = __dnew;
}
_M_data[0 .. __dnew] = __beg[0 .. __dnew];
_M_set_length(__dnew);
}
pointer _M_create(ref size_type __capacity, size_type __old_capacity)
{
assert(__capacity <= max_size());
// if (__capacity > max_size())
// throw new RangeError("Length exceeds `max_size()`"); // std::__throw_length_error(__N("basic_string::_M_create"));
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
{
__capacity = 2 * __old_capacity;
if (__capacity > max_size())
__capacity = max_size();
}
return _M_get_allocator().allocate(__capacity + 1);
}
ref basic_string _M_replace(size_type __pos, size_type __len1, const T* __s, const size_type __len2)
{
_M_check_length(__len1, __len2, "basic_string::_M_replace");
const size_type __old_size = size();
const size_type __new_size = __old_size + __len2 - __len1;
if (__new_size <= capacity())
{
pointer __p = _M_data + __pos;
const size_type __how_much = __old_size - __pos - __len1;
if (_M_disjunct(__s))
{
if (__how_much && __len1 != __len2)
_S_move(__p + __len2, __p + __len1, __how_much);
if (__len2)
_S_copy(__p, __s, __len2);
}
else
{
// Work in-place.
if (__len2 && __len2 <= __len1)
_S_move(__p, __s, __len2);
if (__how_much && __len1 != __len2)
_S_move(__p + __len2, __p + __len1, __how_much);
if (__len2 > __len1)
{
if (__s + __len2 <= __p + __len1)
_S_move(__p, __s, __len2);
else if (__s >= __p + __len1)
_S_copy(__p, __s + __len2 - __len1, __len2);
else
{
const size_type __nleft = (__p + __len1) - __s;
_S_move(__p, __s, __nleft);
_S_copy(__p + __nleft, __p + __len2,
__len2 - __nleft);
}
}
}
}
else
_M_mutate(__pos, __len1, __s, __len2);
_M_set_length(__new_size);
return this;
}
ref basic_string _M_append(const(T)* __s, size_type __n)
{
const size_type __len = __n + size();
if (__len <= capacity())
{
if (__n)
_S_copy(_M_data + size(), __s, __n);
}
else
_M_mutate(size(), size_type(0), __s, __n);
_M_set_length(__len);
return this;
}
void _M_mutate(size_type __pos, size_type __len1, const(T)* __s, size_type __len2)
{
const size_type __how_much = length() - __pos - __len1;
size_type __new_capacity = length() + __len2 - __len1;
pointer __r = _M_create(__new_capacity, capacity());
if (__pos)
_S_copy(__r, _M_data, __pos);
if (__s && __len2)
_S_copy(__r + __pos, __s, __len2);
if (__how_much)
_S_copy(__r + __pos + __len2,
_M_data + __pos + __len1, __how_much);
_M_dispose();
_M_data = __r;
_M_capacity = __new_capacity;
}
void _M_dispose()
{
if (!_M_is_local)
_M_destroy(_M_allocated_capacity);
}
void _M_destroy(size_type __size)
{
_M_get_allocator().deallocate(_M_data, __size + 1);
}
}
// common GCC/stdlibc++ code
void _M_check_length(size_type __n1, size_type __n2, const char* __s) const
{
assert (!(max_size() - (size() - __n1) < __n2));
// if (max_size() - (size() - __n1) < __n2)
// __throw_length_error(__N(__s));
}
void _M_assign_allocator(ref const(allocator_type) al) nothrow
{
static if (!is_empty!allocator_type.value)
_M_Alloc = al;
}
bool _M_disjunct(const(T)* __s) const nothrow
{
return __s < _M_data || _M_data + size() < __s;
}
static void _S_move(T* __d, const(T)* __s, size_type __n)
{
if (__d == __s)
return;
if (__d < __s)
{
for (size_t i = 0; i < __n; ++i)
__d[i] = __s[i];
}
else
{
for (ptrdiff_t i = __n - 1; i >= 0; --i)
__d[i] = __s[i];
}
}
static void _S_copy(T* __d, const(T)* __s, size_type __n)
{
__d[0 .. __n] = __s[0 .. __n];
}
}
else version (CppRuntime_Clang)
{
//----------------------------------------------------------------------------------
// Clang/libc++ implementation
//----------------------------------------------------------------------------------