-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGStr.cpp
1508 lines (1324 loc) · 38.3 KB
/
GStr.cpp
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
//---------------------------------------------------------------------------
#include "GStr.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "GBase.h"
#include <stdarg.h>
#include <errno.h>
//---------------------------------------------------------------------------
GStr::Data GStr::null_data;
//=========================================
GStr::Data * GStr::new_data(uint len, uint addcap) {
//static method to return a new Data object (allocate length)
//content is undefined, but it's null terminated
if (len > 0) {
Data* data;
GMALLOC(data, sizeof(Data)+len+addcap);
data->ref_count = 0;
data->length = len;
data->cap=len+addcap;
data->chars[len] = '\0';
return data;
}
else
return &null_data;
}
GStr::Data* GStr::new_data(const char* str, uint addcap) {
//static method to return a new Data object (allocate: length+addcap)
//as a copy of a given string
//if (str==NULL) return &null_data;
int len= (str==NULL)? 0 : strlen(str);
if (len+addcap > 0) {
Data* data;
GMALLOC(data, sizeof(Data)+len+addcap);
if (len) strcpy(data->chars, str);
data->ref_count = 0;
data->cap=len+addcap;
data->length = len;
data->chars[len] = '\0';
return data;
}
else
return &null_data;
}
void GStr::prep_data(uint len, uint addcap) {
uint newcap=len+addcap;
if (newcap > 0 && my_data->ref_count <= 1 &&
my_data->cap>=newcap && my_data->cap-newcap<(newcap>>1)+2) {
//no need to shrink/reallocate the already allocated space
my_data->length = len;
my_data->chars[len]=0;
return;
}
if (my_data != &null_data && --my_data->ref_count == 0)
GFREE(my_data);
if (len + addcap> 0) {
GMALLOC(my_data, sizeof(Data)+len+addcap);
my_data->ref_count = 1;
my_data->length = len;
my_data->cap=len+addcap;
my_data->chars[len] = 0;
}
else
my_data = &null_data;
}
GStr& GStr::clear(int init_cap) {
make_unique(); //edit operation ahead
prep_data(0, init_cap);
return *this;
}
void GStr::replace_data(Data *data) {
if (my_data != &null_data && --my_data->ref_count == 0)
GFREE(my_data);
if (data != &null_data)
data->ref_count++;
my_data = data;
}
void GStr::make_unique() {//make sure it's not a reference to other string
if (my_data->ref_count > 1) {
Data *data = new_data(my_data->length, 0);
::memcpy(data->chars, my_data->chars, my_data->length);
my_data->ref_count--;
my_data = data;
my_data->ref_count++;
}
}
bool operator==(const char *s1, const GStr& s2){
if (s1==NULL) return s2.is_empty();
return (strcmp(s1, s2.chars()) == 0);
}
bool operator<(const char *s1, const GStr& s2) {
if (s1==NULL) return !s2.is_empty();
return (strcmp(s1, s2.chars()) < 0);
}
bool operator<=(const char *s1, const GStr& s2){
if (s1==NULL) return true;
return (strcmp(s1, s2.chars()) <= 0);
}
bool operator>(const char *s1, const GStr& s2) {
if (s1==NULL) return false;
return (strcmp(s1, s2.chars()) > 0);
}
GStr::GStr():my_data(&null_data) {
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
}
//detach from the string data, returning a pointer to it
char* GStr::detach() {
make_unique();
char *r=my_data->chars;
my_data=&null_data;
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
return r;
}
GStr::GStr(const GStr& s): my_data(&null_data){
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
replace_data(s.my_data);
}
GStr::GStr(const char *s, uint addcap): my_data(&null_data) {
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
my_data=new_data(s, addcap);
my_data->ref_count = 1;
}
GStr::GStr(const int i, uint addcap): my_data(&null_data) {
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
char buf[20];
sprintf(buf,"%d",i);
my_data=new_data(buf, addcap);
}
GStr::GStr(const double f): my_data(&null_data) {
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
char buf[20];
sprintf(buf,"%f",f);
const int len = ::strlen(buf);
prep_data(len);
::memcpy(chrs(), buf, len);
}
GStr::GStr(const char c, int n): my_data(&null_data) {
fTokenDelimiter=NULL;
fTokenizeMode=tkCharSet;
fLastTokenStart=0;
readbuf=NULL;
readbufsize=0;
prep_data(n);
::memset(chrs(), c, n);
}
GStr::~GStr() {
if (my_data != &null_data && --my_data->ref_count == 0)
GFREE(my_data);
GFREE(fTokenDelimiter);
GFREE(readbuf);
}
char& GStr::operator[](int idx){
//returns reference to char (can be l-value)
if (idx < 0) idx += length();
if (idx < 0 || idx >= length()) invalid_index_error("operator[]");
make_unique(); //because the user will probably modify this char!
return chrs()[idx];
}
char GStr::operator[](int idx) const {
//returns char copy (cannot be l-value!)
if (idx < 0) idx += length();
if (idx < 0 || idx >= length()) invalid_index_error("operator[]");
return my_data->chars[idx];
}
GStr& GStr::operator=(const GStr& s) {
if (s.my_data!=my_data) {
make_unique(); //edit operation ahead
replace_data(s.my_data);
}
return *this;
}
GStr& GStr::operator=(const char *s) {
make_unique(); //edit operation ahead
if (s==NULL) {
prep_data(0);
return *this;
}
const int len = ::strlen(s); prep_data(len);
::memcpy(my_data->chars, s, len);
return *this;
}
GStr& GStr::assign(const char* s) {
make_unique(); //edit operation ahead
if (s==NULL) {
prep_data(0, my_data->cap);
return *this;
}
const int len = ::strlen(s);
prep_data(len, my_data->cap-len-2);
::memcpy(my_data->chars, s, len);
return *this;
}
GStr& GStr::assign(const int v) {
make_unique(); //edit operation ahead
char buf[20];
sprintf(buf,"%d",v);
const int len = ::strlen(buf);
prep_data(len, my_data->cap-len-2);
::memcpy(my_data->chars, buf, len);
return *this;
}
GStr& GStr::operator=(const double f) {
make_unique(); //edit operation ahead
char buf[20];
sprintf(buf,"%f",f);
const int len = ::strlen(buf);
prep_data(len);
::memcpy(my_data->chars, buf, len);
return *this;
}
GStr& GStr::operator=(const int i) {
make_unique(); //edit operation ahead
char buf[20];
sprintf(buf,"%d",i);
const int len = ::strlen(buf);
prep_data(len);
::memcpy(my_data->chars, buf, len);
return *this;
}
bool GStr::operator==(const GStr& s) const {
if (s.is_empty()) return is_empty();
return (length() == s.length()) &&
(memcmp(my_data->chars, s.chars(), length()) == 0);
}
bool GStr::operator==(const char *s) const {
if (s==NULL) return is_empty();
return (strcmp(my_data->chars, s) == 0);
}
bool GStr::operator<(const GStr& s) const {
if (s.is_empty()) return false;
return (strcmp(my_data->chars, s.chars()) < 0);
}
bool GStr::operator<(const char *s) const {
if (s==NULL) return false;
return (strcmp(my_data->chars, s) < 0);
}
bool GStr::operator<=(const GStr& s) const {
if (s.is_empty()) return is_empty();
return (strcmp(my_data->chars, s.chars()) <= 0);
}
bool GStr::operator<=(const char *s) const {
if (s==NULL) return is_empty();
return (strcmp(my_data->chars, s) <= 0);
}
bool GStr::operator>(const GStr& s) const {
if (s.is_empty()) return !is_empty();
return (strcmp(my_data->chars, s.chars()) > 0);
}
bool GStr::operator>(const char *s) const {
if (s==NULL) return !is_empty();
return (strcmp(my_data->chars, s) > 0);
}
bool GStr::operator>=(const GStr& s) const {
if (s.is_empty()) return true;
return (strcmp(my_data->chars, s.chars()) >= 0);
}
bool GStr::operator>=(const char *s) const {
if (s==NULL) return true;
return (strcmp(my_data->chars, s) >= 0);
}
bool GStr::operator!=(const GStr& s) const {
if (s.is_empty()) return !is_empty();
return (length() != s.length()) ||
(memcmp(my_data->chars, s.chars(), length()) != 0);
}
bool GStr::operator!=(const char *s) const {
if (s==NULL) return !is_empty();
return (strcmp(my_data->chars, s) != 0);
}
GStr& GStr::append(int i) {
char buf[20];
sprintf(buf,"%d",i);
return append(buf);
}
GStr& GStr::append(uint i) {
char buf[20];
sprintf(buf,"%u",i);
return append(buf);
}
GStr& GStr::append(long l) {
char buf[20];
sprintf(buf,"%ld",l);
return append(buf);
}
GStr& GStr::append(unsigned long l) {
char buf[20];
sprintf(buf,"%lu", l);
return append(buf);
}
GStr& GStr::append(double f) {
char buf[30];
sprintf(buf,"%f",f);
return append(buf);
}
bool GStr::is_empty() const {
//return my_data == &null_data;
return (length()==0);
}
GStr GStr::copy() const {
GStr newstring(*this);
return newstring;
}
int GStr::index(const GStr& s, int start_index) const {
return index(s.chars(), start_index);
}
bool GStr::contains(const GStr& s) const {
return (index(s, 0) >= 0);
}
bool GStr::contains(const char *s) const {
return (index(s, 0) >= 0);
}
bool GStr::startsWith(const char *s) const {
//return (index(s, 0) == 0);
return ::startsWith(my_data->chars, s);
}
bool GStr::startsWith(const GStr& s) const {
//return (index(s, 0) == 0);
return ::startsWith(my_data->chars, s.chars());
}
bool GStr::endsWith(const char *s) const {
//return (index(s, 0) == 0);
return ::endsWith(my_data->chars, s);
}
bool GStr::endsWith(const GStr& s) const {
//return (index(s, 0) == 0);
return ::endsWith(my_data->chars, s.chars());
}
bool GStr::contains(char c) const {
return (index(c, 0) >= 0);
}
GStr& GStr::format(const char *fmt,...) {
// Format as in sprintf
make_unique(); //edit operation ahead
char* buf;
GMALLOC(buf, strlen(fmt)+1024);
va_list arguments;
va_start(arguments,fmt);
//+1K buffer, should be enough for common expressions
int len=vsprintf(buf,fmt,arguments);
va_end(arguments);
prep_data(len); //this also adds the '\0' at the end!
//and sets the right len
::memcpy(chrs(), buf, len);
GFREE(buf);
return *this;
}
GStr& GStr::appendfmt(const char *fmt,...) {
// Format as in sprintf
make_unique(); //edit operation ahead
char* buf;
GMALLOC(buf, strlen(fmt)+1024);
va_list arguments;
va_start(arguments,fmt);
//+1K buffer, should be enough for common expressions
vsprintf(buf,fmt,arguments);
va_end(arguments);
append(buf);
GFREE(buf);
return *this;
}
GStr& GStr::trim(char c) {
int istart;
int iend;
for (istart=0; istart<length() && my_data->chars[istart]==c;istart++) ;
if (istart==length()) {
make_unique(); //edit operation ahead
prep_data(0); //string was entirely trimmed
return *this;
}
for (iend=length()-1; iend>istart && my_data->chars[iend]==c;iend--) ;
int newlen=iend-istart+1;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
Data *data = new_data(newlen);
::memcpy(data->chars, &my_data->chars[istart], newlen);
replace_data(data);
return *this;
}
GStr& GStr::trim(const char* c) {
int istart;
int iend;
for (istart=0; istart<length() && strchr(c, my_data->chars[istart])!=NULL ;istart++) ;
if (istart==length()) {
prep_data(0); //string was entirely trimmed
return *this;
}
for (iend=length()-1; iend>istart && strchr(c, my_data->chars[iend])!=NULL;iend--) ;
int newlen=iend-istart+1;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
Data *data = new_data(newlen);
::memcpy(data->chars, & (my_data->chars[istart]), newlen);
replace_data(data);
return *this;
}
GStr& GStr::trimR(char c) {
//only trim the right end
int iend;
for (iend=length()-1; iend>=0 && my_data->chars[iend]==c;iend--) ;
if (iend==-1) {
make_unique();
prep_data(0); //string was entirely trimmed
return *this;
}
int newlen=iend+1;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
my_data->length=newlen;
my_data->chars[newlen]='\0';
/*
Data *data = new_data(newlen);
::memcpy(data->chars, my_data->chars, newlen);
replace_data(data);
*/
return *this;
}
GStr& GStr::trimR(const char* c) {
int iend;
for (iend=length()-1; iend>=0 && strchr(c,my_data->chars[iend])!=NULL;iend--) ;
if (iend==-1) {
make_unique();
prep_data(0); //string was entirely trimmed
return *this;
}
int newlen=iend+1;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
my_data->length=newlen;
my_data->chars[newlen]='\0';
/*
Data *data = new_data(newlen);
::memcpy(data->chars, my_data->chars, newlen);
replace_data(data);
*/
return *this;
}
GStr& GStr::chomp(const char* cstr) {
int iend;
if (cstr==NULL || *cstr==0) return *this;
//check if this ends with cstr
int cend=strlen(cstr)-1;
iend=my_data->length-1;
while (iend>=0 && cend>=0) {
if (my_data->chars[iend]!=cstr[cend]) return *this;
iend--;
cend--;
}
if (iend==-1) {
make_unique();
prep_data(0); //string will be entirely trimmed
return *this;
}
int newlen=iend+1;
make_unique(); //edit operation ahead
my_data->length=newlen;
my_data->chars[newlen]='\0';
//Data *data = new_data(newlen);
//::memcpy(data->chars, my_data->chars, newlen);
//replace_data(data);
return *this;
}
GStr& GStr::trimL(char c) {
int istart;
for (istart=0; istart<length() && my_data->chars[istart]==c;istart++) ;
if (istart==length()) {
prep_data(0); //string was entirely trimmed
return *this;
}
int newlen=length()-istart;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
Data *data = new_data(newlen);
::memcpy(data->chars, &my_data->chars[istart], newlen);
replace_data(data);
return *this;
}
GStr& GStr::trimL(const char* c) {
int istart;
for (istart=0; istart<length() && strchr(c,my_data->chars[istart])!=NULL;istart++) ;
if (istart==length()) {
prep_data(0); //string was entirely trimmed
return *this;
}
int newlen=length()-istart;
if (newlen==length()) //nothing to trim
return *this;
make_unique(); //edit operation ahead
Data *data = new_data(newlen);
::memcpy(data->chars, &my_data->chars[istart], newlen);
replace_data(data);
return *this;
}
GStr& GStr::padR(uint len, char c) {
//pad with c until total string length is len
if (my_data->length>=len) return *this; //no room for padding
make_unique(); //edit operation ahead
if (my_data->cap>=len) {
::memset(my_data->chars, c, len-my_data->length);
my_data->length=len;
return *this;
}
Data *data = new_data(len);
::memset(data->chars,c,len-my_data->length);
::memcpy(&data->chars[len-length()], my_data->chars, my_data->length);
replace_data(data);
return *this;
}
GStr& GStr::padL(uint len, char c) { //align left the string
if (my_data->length>=len) return *this; //no room for padding
make_unique(); //edit operation ahead
Data *data = new_data(len);
::memcpy(data->chars, my_data->chars, length());
::memset(&data->chars[length()],c,len-length());
replace_data(data);
return *this;
}
GStr& GStr::padC(uint len, char c) {
if (my_data->length>=len) return *this; //no room for padding
make_unique(); //edit operation ahead
uint istart=(len-length())/2;
Data *data = new_data(len);
if (istart>0)
::memset(data->chars, c, istart);
::memcpy(&data->chars[istart], my_data->chars, length());
uint iend=istart+length();
if (iend<len)
::memset(&data->chars[iend],c,len-iend);
replace_data(data);
return *this;
}
GStr operator+(const char *s1, const GStr& s2) {
const int s1_length = ::strlen(s1);
if (s1_length == 0)
return s2;
else {
GStr newstring;
newstring.prep_data(s1_length + s2.length());
::memcpy(newstring.chrs(), s1, s1_length);
::memcpy(&(newstring.chrs())[s1_length], s2.chars(), s2.length());
return newstring;
}
}
//=========================================
GStr GStr::operator+(const GStr& s) const {
if (length() == 0)
return s;
else if (s.length() == 0)
return *this;
else {
GStr newstring;
newstring.prep_data(length() + s.length());
::memcpy(newstring.chrs(), my_data->chars, length());
::memcpy(&(newstring.chrs())[length()], s.chars(), s.length());
return newstring;
}
}
//=========================================
GStr GStr::operator+(const char *s) const {
const int s_length = ::strlen(s);
if (s_length == 0)
return *this;
else {
GStr newstring;
newstring.prep_data(length() + s_length);
::memcpy(newstring.chrs(), my_data->chars, length());
::memcpy(&(newstring.chrs())[length()], s, s_length);
return newstring;
}
}
GStr GStr::operator+(const int i) const {
char buf[20];
sprintf(buf, "%d", i);
const int s_length = ::strlen(buf);
GStr newstring;
newstring.prep_data(length() + s_length);
::memcpy(newstring.chrs(), my_data->chars, length());
::memcpy(&(newstring.chrs())[length()], buf, s_length);
return newstring;
}
GStr GStr::operator+(const char c) const {
char buf[4];
sprintf(buf, "%c", c);
const int s_length = ::strlen(buf);
GStr newstring;
newstring.prep_data(length() + s_length);
::memcpy(newstring.chrs(), my_data->chars, length());
::memcpy(&(newstring.chrs())[length()], buf, s_length);
return newstring;
}
GStr GStr::operator+(const double f) const {
char buf[30];
sprintf(buf, "%f", f);
const int s_length = ::strlen(buf);
GStr newstring;
newstring.prep_data(length() + s_length);
::memcpy(newstring.chrs(), my_data->chars, length());
::memcpy(&(newstring.chrs())[length()], buf, s_length);
return newstring;
}
//=========================================
bool GStr::is_space() const {
if (my_data == &null_data)
return false;
for (const char *p = my_data->chars; *p; p++)
if (!isspace(*p))
return false;
return true;
}
//=========================================
GStr GStr::substr(int idx, int len) const {
// A negative idx specifies an idx from the right of the string.
if (idx < 0)
idx += length();
else if (idx>=length()) {
len=0;
idx=length();
}
if (len) {
// A length of -1 specifies the rest of the string.
if (len < 0 || len>length()-idx)
len = length() - idx;
if (idx<0 || idx>=length() || len<0 )
invalid_args_error("substr()");
}
GStr newstring;
if (len) {
newstring.prep_data(len);
::memcpy(newstring.chrs(), &my_data->chars[idx], len);
}
return newstring;
}
GStr& GStr::reverse() {
make_unique();
int l=0;
int r=my_data->length-1;
char c;
while (l<r) {
c=my_data->chars[l];
my_data->chars[l]=my_data->chars[r];
my_data->chars[r]=c;
l++;r--;
}
return *this;
}
//transform: any character from 'from' is replaced with a coresponding
//char from 'to'
GStr& GStr::tr(const char *rfrom, const char* rto) {
if (length() == 0 || rfrom==NULL || strlen(rfrom)==0)
return *this;
unsigned int l=strlen(rfrom);
if (rto!=NULL && rto[0]==0) rto=NULL;
if (rto!=NULL && strlen(rto)!=l)
invalid_args_error("tr()");
make_unique(); //edit operation ahead
if (rto==NULL) { //delete all characters
Data *data = new_data(length());
char* s = my_data->chars;
char* p=NULL;
char* dest = data->chars;
do {
if ((p=strpbrk(s,rfrom))!=NULL) {
memcpy(dest,s,p-s);
dest+=p-s;
s=p+1;
}
else {
strcpy(dest, s);
dest+=strlen(s);
}
} while (p!=NULL);
(*dest)='\0';
data->length=strlen(data->chars);
replace_data(data);
}
else { //char substitution case - easier!
const char* p=NULL;
for (int i=0; i<length(); i++) {
if ((p=strchr(rfrom, my_data->chars[i]))!=NULL)
my_data->chars[i]=rto[p-rfrom];
}
}
return *this;
}
// search and replace all the occurences of a string with another string
// or just remove the given string (if replacement is NULL)
GStr& GStr::replace(const char *rfrom, const char* rto) {
if (length() == 0 || rfrom==NULL || strlen(rfrom)==0)
return *this;
unsigned int l=strlen(rfrom);
unsigned int tl= (rto==NULL)?0:strlen(rto);
make_unique(); //edit operation ahead
char* p;
char* dest;
char* newdest=NULL;
char* s = my_data->chars;
if (tl!=l) { //reallocation
if (tl>l) { //possible enlargement
GMALLOC(newdest, length()*(tl-l+1)+1);
}
else {//delete or replace with a shorter string
GMALLOC(newdest, length() + 1);
}
dest=newdest;
if (tl==0) {//deletion
while ((p=strstr(s,rfrom))!=NULL) {
//rfrom found at position p
memcpy(dest,s,p-s);
dest+=p-s;
s+=p-s+l; //s positioned in string after rfrom
}
//no more occurences, copy the remaining string
strcpy(dest, s);
}
else { //replace with another string
while ((p=strstr(s,rfrom))!=NULL) {
memcpy(dest,s,p-s); //copy up rto the match
dest+=p-s;
memcpy(dest,rto,tl); //put the replacement string
dest+=tl;
s+=p-s+l;
}
//not found any more, copy rto end of string
strcpy(dest, s);
}
Data* data=new_data(newdest);
replace_data(data);
GFREE(newdest);
}
else { //inplace editing: no need rto reallocate
while ((p=strstr(s,rfrom))!=NULL) {
memcpy(p,rto,l);
s+=p-s+l;
}
}
return *this;
}
GStr& GStr::cut(int idx, int len) {
if (len == 0)
return *this;
make_unique(); //edit operation ahead
// A negative idx specifies an idx from the right of the string,
// so the left part will be cut out
if (idx < 0)
idx += length();
// A length of -1 specifies the rest of the string.
if (len == -1)
len = length() - idx;
if (idx<0 || idx>=length() || len<0 || len>length()-idx)
invalid_args_error("cut()");
Data *data = new_data(length() - len);
if (idx > 0)
::memcpy(data->chars, my_data->chars, idx);
::strcpy(&data->chars[idx], &my_data->chars[idx+len]);
replace_data(data);
return *this;
}
//=========================================
GStr& GStr::paste(const GStr& s, int idx, int len) {
// A negative idx specifies an idx from the right of the string.
if (idx < 0)
idx += length();
make_unique(); //edit operation ahead
// A length of -1 specifies the rest of the string.
if (len == -1)
len = length() - idx;
if (idx<0 || idx>=length() || len<0 || len>length()-idx)
invalid_args_error("replace()");
if (len == s.length() && my_data->ref_count == 1)
::memcpy(&chrs()[idx], s.chars(), len);
else {
Data *data = new_data(length() - len + s.length());
if (idx > 0)
::memcpy(data->chars, my_data->chars, idx);
if (s.length() > 0)
::memcpy(&data->chars[idx], s.chars(), s.length());
::strcpy(&data->chars[idx+s.length()], &my_data->chars[idx+len]);
replace_data(data);
}
return *this;
}
//=========================================
GStr& GStr::paste(const char *s, int idx, int len) {
// A negative idx specifies an idx from the right of the string.
make_unique(); //edit operation ahead
if (idx < 0)
idx += length();
// A length of -1 specifies the rest of the string.
if (len == -1)
len = length() - idx;
if (idx<0 || idx>=length() || len<0 || len>length()-idx)
invalid_args_error("replace()");
const int s_length = ::strlen(s);
if (len == s_length && my_data->ref_count == 1)
::memcpy(&chrs()[idx], s, len);
else {
Data *data = new_data(length() - len + s_length);
if (idx > 0)
::memcpy(data->chars, my_data->chars, idx);
if (s_length > 0)
::memcpy(&data->chars[idx], s, s_length);
::strcpy(&data->chars[idx+s_length], &my_data->chars[idx+len]);
replace_data(data);
}
return *this;
}
//=========================================
GStr& GStr::insert(const GStr& s, int idx) {
make_unique(); //edit operation ahead
// A negative idx specifies an idx from the right of the string.
if (idx < 0)
idx += length();
if (idx < 0 || idx >= length())
invalid_index_error("insert()");
if (s.length() > 0) {
Data *data = new_data(length() + s.length());
if (idx > 0)
::memcpy(data->chars, my_data->chars, idx);
::memcpy(&data->chars[idx], s.chars(), s.length());
::strcpy(&data->chars[idx+s.length()], &my_data->chars[idx]);
replace_data(data);
}
return *this;
}
//=========================================
GStr& GStr::insert(const char *s, int idx) {
// A negative idx specifies an idx from the right of the string.
make_unique(); //edit operation ahead
if (idx < 0)
idx += length();
if (idx < 0 || idx >= length())
invalid_index_error("insert()");
const int s_length = ::strlen(s);
if (s_length > 0) {
Data *data = new_data(length() + s_length);