forked from maniacbug/StandardCplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string
1042 lines (875 loc) · 29.3 KB
/
string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2004 Garrett A. Kajmowicz
This file is part of the uClibc++ Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <basic_definitions>
#include <char_traits>
#include <string.h>
#include <func_exception>
#include <memory>
#include <vector>
#ifdef __UCLIBCXX_HAS_WCHAR__
#include <cwchar>
#include <cwctype>
#endif
#ifndef __HEADER_STD_STRING
#define __HEADER_STD_STRING 1
#pragma GCC visibility push(default)
namespace std{
//Basic basic_string
template<class Ch, class Tr = char_traits<Ch>, class A = allocator<Ch> > class basic_string;
typedef basic_string<char> string;
#ifdef __UCLIBCXX_HAS_WCHAR__
typedef basic_string<wchar_t> wstring;
#endif
//template<class Ch, class Tr = char_traits<Ch>, class A = allocator<Ch> > class _UCXXEXPORT basic_string
template<class Ch, class Tr, class A> class basic_string
: public std::vector<Ch, A>
{
public:
typedef Tr traits_type;
typedef typename Tr::char_type value_type;
typedef A allocator_type;
typedef typename A::size_type size_type;
typedef typename A::difference_type difference_type;
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef typename A::pointer pointer;
typedef typename A::const_pointer const_pointer;
typedef typename vector<Ch, A>::iterator iterator;
typedef typename vector<Ch, A>::const_iterator const_iterator;
typedef typename vector<Ch, A>::reverse_iterator reverse_iterator;
typedef typename vector<Ch, A>::const_reverse_iterator const_reverse_iterator;
static const size_type npos = (size_type)-1;
explicit _UCXXEXPORT basic_string(const A& al = A()) : vector<Ch, A>(al){ return; }
_UCXXEXPORT basic_string(const basic_string& str, size_type pos = 0, size_type n = npos, const A& al = A()); //Below
_UCXXEXPORT basic_string(const Ch* s, size_type n, const A& al = A())
: vector<Ch, A>(al)
{
if(n == npos){
__throw_out_of_range();
}
if(s){
resize(n);
Tr::copy(vector<Ch, A>::data, s, vector<Ch, A>::elements);
}
}
_UCXXEXPORT basic_string(const Ch* s, const A& al = A()); //Below
_UCXXEXPORT basic_string(size_type n, Ch c, const A& al = A())
: vector<Ch, A>(n, c, al)
{
}
template<class InputIterator> _UCXXEXPORT basic_string(InputIterator begin, InputIterator end, const A& a = A())
:vector<Ch, A>(begin, end)
{
}
_UCXXEXPORT ~basic_string() {
return;
}
_UCXXEXPORT basic_string& operator=(const basic_string& str); //Below
_UCXXEXPORT basic_string& operator=(const Ch* s){
vector<Ch, A>::clear();
if(s!=0){
size_type len = Tr::length(s);
resize(len);
Tr::copy( vector<Ch, A>::data, s, len);
}
return *this;
}
_UCXXEXPORT basic_string& operator=(Ch c){
vector<Ch, A>::clear();
vector<Ch, A>::push_back(c);
return *this;
}
inline _UCXXEXPORT size_type length() const { return vector<Ch, A>::size(); }
void _UCXXEXPORT resize(size_type n, Ch c = Ch()){
vector<Ch, A>::resize(n, c);
}
_UCXXEXPORT basic_string& operator+=(const basic_string& str){
return append(str);
}
_UCXXEXPORT basic_string& operator+=(const Ch * s){
return append(s);
}
_UCXXEXPORT basic_string& operator+=(Ch c){
vector<Ch, A>::push_back(c);
return *this;
}
_UCXXEXPORT basic_string& append(const basic_string& str){
size_t temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + str.elements);
Tr::copy( vector<Ch, A>::data + temp, str.vector<Ch, A>::data, str.elements);
return *this;
}
_UCXXEXPORT basic_string& append(const basic_string& str, size_type pos, size_type n){
if(pos > str.size()){
__throw_out_of_range();
}
size_type rlen = str.elements - pos;
if(rlen > n){
rlen = n;
}
if(vector<Ch, A>::elements > npos - rlen){
__throw_length_error();
}
size_t temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + rlen);
Tr::copy( vector<Ch, A>::data + temp, str.vector<Ch, A>::data + pos, rlen);
return *this;
}
_UCXXEXPORT basic_string& append(const Ch* s, size_type n){
size_t temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + n);
Tr::copy( vector<Ch, A>::data + temp, s, n);
return *this;
}
_UCXXEXPORT basic_string& append(const Ch* s){
size_type strLen = Tr::length(s);
size_t temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + strLen);
Tr::copy( vector<Ch, A>::data + temp, s, strLen);
return *this;
}
_UCXXEXPORT basic_string& append(size_type n, Ch c){
vector<Ch, A>::resize(vector<Ch, A>::elements + n, c);
return *this;
}
_UCXXEXPORT basic_string& assign(const basic_string& str){
operator=(str);
return *this;
}
_UCXXEXPORT basic_string& assign(const basic_string& str, size_type pos, size_type n){
if(pos > str.elements){
__throw_out_of_range();
}
size_type r = str.elements - pos;
if(r > n){
r = n;
}
resize(r);
Tr::copy(vector<Ch, A>::data, str.vector<Ch, A>::data + pos, r);
return *this;
}
_UCXXEXPORT basic_string& assign(const Ch* s, size_type n){
resize(n);
Tr::copy(vector<Ch, A>::data, s, n);
return *this;
}
_UCXXEXPORT basic_string& assign(const Ch* s){
size_type len = Tr::length(s);
return assign(s, len);
}
_UCXXEXPORT basic_string& assign(size_type n, Ch c){
vector<Ch, A>::clear();
vector<Ch, A>::resize(n, Ch() );
return *this;
}
template<class InputIterator> _UCXXEXPORT basic_string& assign(InputIterator first, InputIterator last){
vector<Ch, A>::resize(0, Ch());
while (first != last){
append(*first);
++first;
}
return *this;
}
_UCXXEXPORT basic_string& insert(size_type pos1, const basic_string& str, size_type pos2=0, size_type n=npos){
if(pos1 > vector<Ch, A>::elements || pos2 > str.elements){
__throw_out_of_range();
}
size_type r = str.elements - pos2;
if( r > n){
r = n;
}
if(vector<Ch, A>::elements > npos - r){
__throw_length_error();
}
size_type temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + r);
Tr::move(vector<Ch, A>::data + pos1 + r, vector<Ch, A>::data + pos1, temp - pos1);
Tr::copy(vector<Ch, A>::data + pos1, str.vector<Ch, A>::data + pos2, r);
return *this;
}
_UCXXEXPORT basic_string& insert(size_type pos, const Ch* s, size_type n){
if(pos > vector<Ch, A>::elements){
__throw_out_of_range();
}
if(vector<Ch, A>::elements > npos - n){
__throw_length_error();
}
size_type temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + n);
Tr::move(vector<Ch, A>::data + pos + n, vector<Ch, A>::data + pos, temp - pos);
Tr::copy(vector<Ch, A>::data + pos, s, n);
return *this;
}
inline _UCXXEXPORT basic_string& insert(size_type pos, const Ch* s){
size_type len = Tr::length(s);
return insert(pos, s, len);
}
_UCXXEXPORT basic_string& insert(size_type pos, size_type n, Ch c){
if(pos > vector<Ch, A>::elements){
__throw_out_of_range();
}
if(vector<Ch, A>::elements > npos - n){
__throw_length_error();
}
size_type temp = vector<Ch, A>::elements;
resize(vector<Ch, A>::elements + n);
Tr::move(vector<Ch, A>::data + pos + n, vector<Ch, A>::data + pos, temp - pos);
Tr::assign(vector<Ch, A>::data + pos, n, c);
return *this;
}
using vector<Ch, A>::insert;
// void insert(iterator p, size_type n, charT c);
// template<class InputIterator> void insert(iterator p, InputIterator first, InputIterator last);
_UCXXEXPORT basic_string& erase(size_type pos = 0, size_type n = npos){
size_type xlen = vector<Ch, A>::elements - pos;
if(xlen > n){
xlen = n;
}
size_type temp = vector<Ch, A>::elements;
Tr::move(vector<Ch, A>::data + pos, vector<Ch, A>::data + pos + xlen, temp - pos - xlen);
resize(temp - xlen);
return *this;
}
_UCXXEXPORT iterator erase(iterator position){
if(position == vector<Ch, A>::end()){
return position;
}
++position;
iterator temp = position;
while(position != vector<Ch, A>::end()){
*(position-1) = *position;
++position;
}
vector<Ch, A>::pop_back();
return temp;
}
_UCXXEXPORT iterator erase(iterator first, iterator last){
size_t count = last - first;
iterator temp = last;
while(last != vector<Ch, A>::end()){
*(last - count) = *last;
++last;
}
resize( vector<Ch, A>::elements-count);
return temp;
}
_UCXXEXPORT basic_string&
replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2=0, size_type n2=npos)
{
if(pos1 > vector<Ch, A>::elements){
__throw_out_of_range();
}
size_type xlen = vector<Ch, A>::elements - pos1;
if(xlen > n1){
xlen = n1;
}
size_type rlen = str.elements - pos2;
if(rlen > n2){
rlen = n2;
}
if((vector<Ch, A>::elements - xlen) >= (npos - rlen)){
__throw_length_error();
}
size_t temp = vector<Ch, A>::elements;
if(rlen > xlen){ //Only if making larger
resize(temp - xlen + rlen);
}
//Final length = vector<Ch, A>::elements - xlen + rlen
//Initial block is of size pos1
//Block 2 is of size len
Tr::move(vector<Ch, A>::data + pos1 + rlen, vector<Ch, A>::data + pos1 + xlen, temp - pos1 - xlen);
Tr::copy(vector<Ch, A>::data + pos1, str.vector<Ch, A>::data + pos2, rlen);
resize(temp - xlen + rlen);
return *this;
}
_UCXXEXPORT basic_string& replace(size_type pos, size_type n1, const Ch* s, size_type n2){
return replace(pos,n1,basic_string<Ch,Tr,A>(s,n2));
}
inline _UCXXEXPORT basic_string& replace(size_type pos, size_type n1, const Ch* s){
return replace(pos,n1,basic_string<Ch,Tr,A>(s));
}
_UCXXEXPORT basic_string& replace(size_type pos, size_type n1, size_type n2, Ch c){
return replace(pos,n1,basic_string<Ch, Tr, A>(n2,c));
}
// _UCXXEXPORT basic_string& replace(iterator i1, iterator i2, const basic_string& str);
// _UCXXEXPORT basic_string& replace(iterator i1, iterator i2, const Ch* s, size_type n);
// _UCXXEXPORT basic_string& replace(iterator i1, iterator i2, const Ch* s);
// _UCXXEXPORT basic_string& replace(iterator i1, iterator i2, size_type n, Ch c);
/* template<class InputIterator> _UCXXEXPORT basic_string& replace(iterator i1, iterator i2,
InputIterator j1, InputIterator j2);*/
size_type _UCXXEXPORT copy(Ch* s, size_type n, size_type pos = 0) const{
if(pos > vector<Ch, A>::elements){
__throw_out_of_range();
}
size_type r = vector<Ch, A>::elements - pos;
if(r > n){
r = n;
}
Tr::copy(s, vector<Ch, A>::data + pos, r);
return r;
}
_UCXXEXPORT void swap(basic_string<Ch,Tr,A>& s){
//Data pointers
vector<Ch, A>::swap(s);
}
_UCXXEXPORT const Ch* c_str() const{
const_cast<basic_string<Ch,Tr,A> *>(this)->reserve(vector<Ch, A>::elements+1);
vector<Ch, A>::data[vector<Ch, A>::elements] = 0; //Add 0 at the end
return vector<Ch, A>::data;
}
_UCXXEXPORT const Ch* data() const{
return vector<Ch, A>::data;
}
_UCXXEXPORT allocator_type get_allocator() const{
return vector<Ch, A>::a;
}
_UCXXEXPORT size_type find (const basic_string& str, size_type pos = 0) const; //Below
_UCXXEXPORT size_type find (const Ch* s, size_type pos, size_type n) const{
return find(basic_string<Ch, Tr, A>(s,n), pos);
}
_UCXXEXPORT size_type find (const Ch* s, size_type pos = 0) const{
return find(basic_string<Ch, Tr, A>(s), pos);
}
_UCXXEXPORT size_type find (Ch c, size_type pos = 0) const{
for(size_type i = pos; i < length(); ++i){
if(this->operator[](i) == c){
return i;
}
}
return npos;
}
_UCXXEXPORT size_type rfind(const basic_string& str, size_type pos = npos) const{
if(pos >= length()){
pos = length();
}
for(size_type i = pos; i > 0; --i){
if(str == substr(i-1, str.length())){
return i-1;
}
}
return npos;
}
_UCXXEXPORT size_type rfind(const Ch* s, size_type pos, size_type n) const{
return rfind(basic_string<Ch, Tr, A>(s,n),pos);
}
_UCXXEXPORT size_type rfind(const Ch* s, size_type pos = npos) const{
return rfind(basic_string<Ch, Tr, A>(s),pos);
}
_UCXXEXPORT size_type rfind(Ch c, size_type pos = npos) const{
return rfind(basic_string<Ch, Tr, A>(1,c),pos);
}
_UCXXEXPORT size_type find_first_of(const basic_string& str, size_type pos = 0) const{
for(size_type i = pos; i < length(); ++i){
for(size_type j = 0; j < str.length() ; ++j){
if( Tr::eq(str[j], operator[](i)) ){
return i;
}
}
}
return npos;
}
_UCXXEXPORT size_type find_first_of(const Ch* s, size_type pos, size_type n) const{
return find_first_of(basic_string<Ch, Tr, A>(s,n),pos);
}
_UCXXEXPORT size_type find_first_of(const Ch* s, size_type pos = 0) const{
return find_first_of(basic_string<Ch, Tr, A>(s),pos);
}
_UCXXEXPORT size_type find_first_of(Ch c, size_type pos = 0) const{
for(size_type i = pos; i< length(); ++i){
if( Tr::eq(operator[](i), c) ){
return i;
}
}
return npos;
}
_UCXXEXPORT size_type find_last_of (const basic_string& str, size_type pos = npos) const{
if(pos > length()){
pos = length();
}
for(size_type i = pos; i >0 ; --i){
for(size_type j = 0 ; j < str.length(); ++j){
if( Tr::eq(operator[](i-1), str[j]) ){
return i-1;
}
}
}
return npos;
}
_UCXXEXPORT size_type find_last_of (const Ch* s, size_type pos, size_type n) const{
return find_last_of(basic_string<Ch, Tr, A>(s,n),pos);
}
_UCXXEXPORT size_type find_last_of (const Ch* s, size_type pos = npos) const{
return find_last_of(basic_string<Ch, Tr, A>(s),pos);
}
_UCXXEXPORT size_type find_last_of (Ch c, size_type pos = npos) const{
if(pos > length()){
pos = length();
}
for(size_type i = pos; i >0 ; --i){
if( Tr::eq(operator[](i-1), c) ){
return i-1;
}
}
return npos;
}
_UCXXEXPORT size_type find_first_not_of(const basic_string& str, size_type pos = 0) const{
bool foundCharacter;
for(size_type i = pos; i < length(); ++i){
foundCharacter = false;
for(size_type j = 0; j < str.length() ; ++j){
if( Tr::eq(str[j], operator[](i)) ){
foundCharacter = true;
}
}
if(foundCharacter == false){
return i;
}
}
return npos;
}
_UCXXEXPORT size_type find_first_not_of(const Ch* s, size_type pos, size_type n) const{
return find_first_not_of(basic_string<Ch, Tr, A>(s,n),pos);
}
_UCXXEXPORT size_type find_first_not_of(const Ch* s, size_type pos = 0) const{
return find_first_not_of(basic_string<Ch, Tr, A>(s),pos);
}
_UCXXEXPORT size_type find_first_not_of(Ch c, size_type pos = 0) const{
for(size_type i = pos; i < length() ; ++i){
if(operator[](i) != c){
return i;
}
}
return npos;
}
_UCXXEXPORT size_type find_last_not_of (const basic_string& str, size_type pos = npos) const{
size_type xpos(length() - 1);
if(xpos > pos){
xpos = pos;
}
while(xpos != npos && npos != str.find_first_of(at(xpos))){
--xpos;
}
return xpos;
}
_UCXXEXPORT size_type find_last_not_of (const Ch* s, size_type pos, size_type n) const{
return find_last_not_of(basic_string<Ch, Tr, A>(s,n),pos);
}
_UCXXEXPORT size_type find_last_not_of (const Ch* s, size_type pos = npos) const{
return find_last_not_of(basic_string<Ch, Tr, A>(s),pos);
}
_UCXXEXPORT size_type find_last_not_of (Ch c, size_type pos = npos) const{
size_type xpos(length() - 1);
if(xpos > pos){
xpos = pos;
}
while(xpos != npos && Tr::eq(at(xpos), c)){
--xpos;
}
return xpos;
}
_UCXXEXPORT basic_string substr(size_type pos = 0, size_type n = npos) const;
_UCXXEXPORT int compare(const basic_string& str) const{
size_type rlen = vector<Ch, A>::elements;
if(rlen > str.elements){
rlen = str.elements;
}
int retval = Tr::compare(vector<Ch, A>::data, str.vector<Ch, A>::data, rlen);
if(retval == 0){
if(vector<Ch, A>::elements < str.elements){
retval = -1;
}
if(vector<Ch, A>::elements > str.elements){
retval = 1;
}
}
return retval;
}
_UCXXEXPORT int compare(size_type pos1, size_type n1, const basic_string& str,
size_type pos2=0, size_type n2=npos) const{
size_type len1 = vector<Ch, A>::elements - pos1;
if(len1 > n1){
len1 = n1;
}
size_type len2 = str.vector<Ch, A>::elements - pos2;
if(len2 > n2){
len2 = n2;
}
size_type rlen = len1;
if(rlen > len2){
rlen = len2;
}
int retval = Tr::compare(vector<Ch, A>::data + pos1, str.vector<Ch, A>::data + pos2, rlen);
if(retval == 0){
if(len1 < len2){
retval = -1;
}
if(len1 > len2){
retval = 1;
}
}
return retval;
}
_UCXXEXPORT int compare(const Ch* s) const{
size_type slen = Tr::length(s);
size_type rlen = slen;
if(rlen > vector<Ch, A>::elements){
rlen=vector<Ch, A>::elements;
}
int retval = Tr::compare(vector<Ch, A>::data, s, rlen);
if(retval==0){
if(vector<Ch, A>::elements < slen){
retval = -1;
}
if(vector<Ch, A>::elements > slen){
retval = 1;
}
}
return retval;
}
_UCXXEXPORT int compare(size_type pos1, size_type n1, const Ch* s, size_type n2 = npos) const{
size_type len1 = vector<Ch, A>::elements - pos1;
if(len1 > n1){
len1 = n1;
}
size_type slen = Tr::length(s);
size_type len2 = slen;
if(len2 > n2){
len2 = n2;
}
size_type rlen = len1;
if(rlen > len2){
rlen = len2;
}
int retval = Tr::compare(vector<Ch, A>::data + pos1, s, rlen);
if(retval == 0){
if(len1 < len2){
retval = -1;
}
if(len1 > len2){
retval = 1;
}
}
return retval;
}
};
//Functions
template<class Ch,class Tr,class A> _UCXXEXPORT basic_string<Ch,Tr,A>::basic_string(const Ch* s, const A& al)
: vector<Ch, A>(al)
{
if(s!=0){
size_type temp = Tr::length(s);
append(s, temp);
}
}
template<class Ch,class Tr,class A> _UCXXEXPORT basic_string<Ch,Tr,A>::
basic_string(const basic_string& str, size_type pos, size_type n, const A& al)
: vector<Ch, A>(al)
{
if(pos>str.size()){
__throw_out_of_range();
}
size_type rlen = str.size() - pos;
if( rlen > n){
rlen = n;
}
resize(rlen);
Tr::copy(vector<Ch, A>::data, str.vector<Ch, A>::data + pos, vector<Ch, A>::elements);
}
template<class Ch,class Tr,class A> _UCXXEXPORT basic_string<Ch,Tr,A>&
basic_string<Ch,Tr,A>::operator=(const basic_string<Ch,Tr,A> & str)
{
if(&str == this){ //Check if we are doing a=a
return *this;
}
vector<Ch, A>::clear();
resize(str.elements);
Tr::copy( vector<Ch, A>::data, str.vector<Ch, A>::data, str.elements);
return *this;
}
template<class Ch,class Tr,class A> _UCXXEXPORT typename basic_string<Ch,Tr,A>::size_type
basic_string<Ch,Tr,A>::find (const basic_string<Ch,Tr,A>& str, size_type pos) const
{
if(str.length() > length()){
return npos;
}
size_type max_string_start = 1 + length() - str.length();
for(size_type i = pos; i < max_string_start; ++i){
if(str == substr(i, str.length())){
return i;
}
}
return npos;
}
template<class Ch,class Tr,class A>
_UCXXEXPORT basic_string<Ch, Tr, A> basic_string<Ch,Tr,A>::substr(size_type pos, size_type n) const
{
if(pos > vector<Ch, A>::elements){
__throw_out_of_range();
}
size_type rlen = vector<Ch, A>::elements - pos;
if(rlen > n){
rlen = n;
}
return basic_string<Ch,Tr,A>(vector<Ch, A>::data + pos,rlen);
}
#ifdef __UCLIBCXX_EXPAND_STRING_CHAR__
#ifndef __UCLIBCXX_COMPILE_STRING__
#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
template <> _UCXXEXPORT string::basic_string(const allocator<char> &);
template <> _UCXXEXPORT string::basic_string(size_type n, char c, const allocator<char> & );
template <> _UCXXEXPORT string::basic_string(const char* s, const allocator<char>& al);
template <> _UCXXEXPORT string::basic_string(const basic_string& str, size_type pos, size_type n, const allocator<char>& al);
template <> _UCXXEXPORT string::~basic_string();
#endif
template <> _UCXXEXPORT string & string::append(const char * s, size_type n);
template <> _UCXXEXPORT string::size_type string::find(const string & str, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find(const char* s, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find (char c, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::rfind(const string & str, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::rfind(char c, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::rfind(const char* s, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_first_of(const string &, size_type) const;
template <> _UCXXEXPORT string::size_type string::find_first_of(const char *, size_type pos, size_type n) const;
template <> _UCXXEXPORT string::size_type string::find_first_of(const char*, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_first_of(char c, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_last_of (const string & , size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_last_of (const char* s, size_type pos, size_type n) const;
template <> _UCXXEXPORT string::size_type string::find_last_of (const char* s, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_last_of (char c, size_type pos) const;
template <> _UCXXEXPORT string::size_type string::find_first_not_of(const string &, size_type) const;
template <> _UCXXEXPORT string::size_type string::find_first_not_of(const char*, size_type, size_type) const;
template <> _UCXXEXPORT string::size_type string::find_first_not_of(const char*, size_type) const;
template <> _UCXXEXPORT string::size_type string::find_first_not_of(char c, size_type) const;
template <> _UCXXEXPORT int string::compare(const string & str) const;
template <> _UCXXEXPORT int string::compare(
size_type pos1, size_type n1, const string & str, size_type pos2, size_type n2) const;
template <> _UCXXEXPORT string string::substr(size_type pos, size_type n) const;
template <> _UCXXEXPORT string & string::operator=(const string & str);
template <> _UCXXEXPORT string & string::operator=(const char * s);
#endif
#endif
//typedef basic_string<char> string;
template<class charT, class traits, class Allocator> _UCXXEXPORT basic_string<charT,traits,Allocator>
operator+(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
temp.append(rhs);
return temp;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT basic_string<charT,traits,Allocator>
operator+(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
temp.append(rhs);
return temp;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT basic_string<charT,traits,Allocator>
operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(1, lhs);
temp.append(rhs);
return temp;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT basic_string<charT,traits,Allocator>
operator+(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
temp.append(rhs);
return temp;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT basic_string<charT,traits,Allocator>
operator+(const basic_string<charT,traits,Allocator>& lhs, charT rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
temp+=rhs;
return temp;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator==(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) == 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator==(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(rhs.compare(lhs) == 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
if(lhs.compare(rhs)==0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator!=(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) !=0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator!=(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
return (temp != rhs);
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator!=(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(rhs);
return (lhs != temp);
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator< (const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) < 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator< (const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(rhs);
if(lhs.compare(rhs) < 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator< (const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
if(temp.compare(rhs) < 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator> (const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) > 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator> (const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(rhs);
if(lhs.compare(rhs) > 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator> (const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
if(temp.compare(rhs) > 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator<=(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) <=0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator<=(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(rhs);
if(lhs.compare(temp) <=0 ){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator<=(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
if(temp.compare(rhs) <= 0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator>=(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs)
{
if(lhs.compare(rhs) >=0){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator>=(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs)
{
basic_string<charT,traits,Allocator> temp(rhs);
if(lhs.compare(temp) >=0 ){
return true;
}
return false;
}
template<class charT, class traits, class Allocator> _UCXXEXPORT bool
operator>=(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs)
{
basic_string<charT,traits,Allocator> temp(lhs);
if(temp.compare(rhs) >=0 ){
return true;
}
return false;
}