-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr
705 lines (626 loc) · 15.9 KB
/
str
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
// -*- C++ -*-
// Created by priyanshu on 10-01-2022.
//
#ifndef STRING_STR_
#define STRING_STR_
#ifndef _GLIBCXX_IOSTREAM
#include "iostream"
#endif
#ifndef _GLIBCXX_CSTDARG
#include "cstdarg"
#endif
#ifndef _TYPEINFO
#include "typeinfo"
#endif
class str {
private:
char* s;
template<class T> void tostr(T val);
public:
//Constructors - Below
str(char* string_value);
explicit str(bool bool_value);
explicit str(char char_value);
explicit str(unsigned char unsigned_char_value);
explicit str(short short_value);
explicit str(unsigned short unsigned_short_value);
explicit str(int int_value);
explicit str(unsigned int unsigned_int_value);
explicit str(long long_value);
explicit str(unsigned long unsigned_long_value);
explicit str(long long long_long_value);
explicit str(unsigned long long unsigned_long_long_val);
explicit str(float float_value);
explicit str(double double_value);
//Constructors - Above
//Type Casting - Below
operator char* ();
explicit operator bool ();
explicit operator char ();
explicit operator unsigned char ();
explicit operator short ();
explicit operator unsigned short ();
explicit operator int ();
explicit operator unsigned int ();
explicit operator long ();
explicit operator unsigned long ();
explicit operator long long ();
explicit operator unsigned long long ();
explicit operator float ();
explicit operator double ();
//Type Casting - Above
// Str Operations - Below
int len(); // returns string length
str rev(bool internal); // reverse string
str operator +(str x); // concatenate 2 string and gives result in another string
void operator +=(str x); // concatenate string on right side to left side
void operator <<=(str x); // copys the content of right side to left side
bool operator ==(str x);
bool operator ==(char* x);
str operator ()(int start,int end, int jump); // string slicing
char operator [](int x);
int countChar(char c); // count occurrence of c in string
int firstIndexOf(char c); // returns 1st index of char c in string, if not present returns -1
int lastIndexOf(char c); // returns last index of char c in string, if not present returns -1
// Str Operations - Above
//friend functions - Below
friend int len(str s);
friend int len(char* s);
friend str rev(str s);
friend void rev(str* s);
friend int countChar(str s,char c);
friend int firstIndexOf(str s,char c);
friend int lastIndexOf(str s,char c);
friend str read(const str message);
friend str read(const str message);
friend str readline(const str message);
template<class... Str> friend void write(Str... messages);
template<class... Str> friend void writeln(Str... messages);
friend std::istream & operator >>(std::istream &in, str &s);
friend std::ostream & operator <<(std::ostream &ou, str const&s);
//friend functions - Above
};
// Definition Begins Here
int len(char* s) {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
// Constructor Begins
template<class T> void str::tostr(T val) {
char c = (typeid(val).name())[0];
if(c=='b'){
(this->s) = new char[(val?5:6)];
(this->s) = (char*)((bool)val ? "true" : "false");
} else if(c=='c'){
this->s = new char[2];
*(this->s) = val;
*(this->s+1)='\0';
}else if(c=='h'){
this->s = new char[2];
*(this->s) = (char)val;
*(this->s+1)='\0';
}
else {
T k = val;
int l = 0;
for (int i = 0; k != 0; i++, l++, k /= 10);
l--;
k = val;
if (val < 0) {
k = k * -1;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l + 1)) = '\0';
for (; k != 0; l--) {
char o = (char) (((T)k % 10) + '0');
*((this->s) + l) = o;
k /= 10;
}
}
}
str::str(char *string_value):s{string_value} {
this->s = new char [::len(string_value)];
int i=0;
for(;*(string_value+i)!='\0';i++){
*(this->s+i)=*(string_value+i);
}
*(this->s+i)='\0';
}
str::str(bool bool_value) {
tostr(bool_value);
}
str::str(char char_value) {
tostr(char_value);
}
str::str(unsigned char unsigned_char_value) {
tostr(unsigned_char_value);
}
str::str(short short_value) {
tostr(short_value);
}
str::str(unsigned short unsigned_short_value) {
tostr(unsigned_short_value);
}
str::str(int int_value) {
tostr(int_value);
}
str::str(unsigned int unsigned_int_value) {
tostr(unsigned_int_value);
}
str::str(long long_value) {
tostr(long_value);
}
str::str(unsigned long unsigned_long_value){
tostr(unsigned_long_value);
}
str::str(long long long_long_value) {
tostr(long_long_value);
}
str::str(unsigned long long unsigned_long_long_value) {
tostr(unsigned_long_long_value);
}
str::str(float float_value){
float k = float_value;
int l = 0, ptAt;
for (int i = 0; k >=1; i++, l++, k /= 10);
k=float_value;
ptAt=l;
for (int i = 0; ((long )k)%10!=0; i++, l++, k *= 10);
if (float_value< 0) {
k = k * -1;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l + 1)) = '\0';
for (; k != 0; l--) {
char c = (char) (((long)k % 10) + '0');
if(ptAt==l) {
*((this->s) + l) ='.';
l--;
}
*((this->s) + l) = c;
k /= 10;
}
}
str::str(double double_value) {
double k = double_value;
int l = 0, ptAt;
for (int i = 0; k >=1; i++, l++, k /= 10);
k=double_value;
ptAt=l;
for (int i = 0; ((long long )k)%10!=0; i++, l++, k *= 10);
if (double_value< 0) {
k = k * -1;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l + 1)) = '\0';
for (; k != 0; l--) {
char c = (char) (((long long )k % 10) + '0');
if(ptAt==l) {
*((this->s) + l) ='.';
l--;
}
*((this->s) + l) = c;
k /= 10;
}
}
// Constructor Ends
// Friend Functions Below
int len(str s) {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
str rev(str s) {
str op = new char[::len(s)];
int j = 0;
for (int i = ::len(s) - 1; i >= 0; i--, j++) {
*(op + j) = *(s + i);
}
*(op + j) = '\0';
return op;
}
void rev(str *s) {
str op = new char[::len(*s)];
int j = 0;
for (int i = ::len(*s) - 1; i >= 0; i--, j++) {
*(op + j) = *(*s + i);
}
*(op + j) = '\0';
*s = op;
}
int countChar(str s, char c) {
int count=0;
for(int i=0;i<::len(s);i++)
count = *(s+i)==c ? count+1 : count+0;
return count;
}
int firstIndexOf(str s, char c) {
int index=-1;
for(int i=0;i<::len(s);i++){
if(*(s+i)==c){
index = i;
break;
}
}
return index;
}
int lastIndexOf(str s, char c) {
int index=-1;
for(int i=0;i<::len(s);i++)
index = *(s+i)==c ? i : index;
return index;
}
str read(const str message=""){
std::cout<<message;
int i=0;
int size=1;
char *pntName=NULL;
char *temp=NULL;
pntName = (char*)malloc( size + 1);
while(1){
size++;
temp = (char*)realloc(pntName, size + 1);
pntName = temp;
if ( ( scanf("%c",&pntName[i])) == 1) {
i++;
pntName[i] = '\0';
if ( pntName[i-1] == ' ' || pntName[i-1]=='\n') {
break;
}
}
else {
break;
}
}
return (str)pntName;
}
str readline(const str message=""){
std::cout<<message;
int i=0;
int size=1;
char *pntName=NULL;
char *temp=NULL;
pntName = (char*)malloc( size + 1);
while(1){
size++;
temp = (char*)realloc(pntName, size + 1);
pntName = temp;
if ( ( scanf("%c",&pntName[i])) == 1) {
i++;
pntName[i] = '\0';
if ( pntName[i-1] == '\n') {
break;
}
}
else {
break;
}
}
return (str)pntName;
}
template<class... Str> void write(Str... messages){
(std::cout<<...<<messages)<<" ";
}
template<class... Str> void writeln(Str... messages){
(std::cout<<...<<messages)<<"\n";
}
std::ostream &operator<<(std::ostream &ou, str const&s) {
ou << s.s;
return ou;
}
std::istream &operator>>(std::istream &in, str &x) {
x = new char[::len(x)];
in >>(x.s);
return in;
}
// Friend Functions Above
//Type Casting - Below
str::operator bool() {
if(s=="YES" || s=="yes" || s=="Yes" ||s=="true" || s=="TRUE" || s=="True" || s=="1")
return true;
else if(s=="NO" || s=="no" || s=="No" || s=="false" || s=="FALSE" || s=="False" || s=="0")
return false;
else throw std::invalid_argument("Cannot cast from str to bool");
}
str::operator char(){
if(::len(s)!=1){
throw std::invalid_argument("Cannot Cast String Literal to Char Type");
}
else{
return *s;
}
}
str::operator unsigned char(){
if(::len(s)!=1){
throw std::invalid_argument("Cannot Cast String Literal to Char Type");
}
else{
return (unsigned char)*s;
}
}
str::operator short() {
short n=0,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(short)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to short type");
}
}
return sign_factor*n;
}
str::operator unsigned short() {
unsigned short n=0;
int i = *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned short)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned short type");
}
}
return n;
}
str::operator int() {
int n=0,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to int type");
}
}
return sign_factor*n;
}
str::operator unsigned int() {
unsigned int n=0;
int i = *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned int )n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned int type");
}
}
return n;
}
str::operator long() {
long n=0L,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(long)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to long type");
}
}
return sign_factor*n;
}
str::operator unsigned long() {
unsigned long n=0L;
int i= *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned long)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned long type");
}
}
return n;
}
str::operator long long() {
long long n=0L,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(long long)n*10LL+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to long long type");
}
}
return sign_factor*n;
}
str::operator unsigned long long() {
unsigned long long n=0LL;
int i= *s=='+' ? 1 : 0;
for(;i<::len(s);i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned long long)n*10LL+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned long long type");
}
}
return n;
}
str::operator float() {
float rez = 0, fact = 1;
int point_seen=0,i;
if (*s == '-'){
fact = -1;
};
i = *s == '-' || *s == '+' ? 1 : 0;
for (; i<::len(s); i++){
if (*(s+i) == '.'){
point_seen = 1;
continue;
}
int d = *(s+i) - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
} else{
throw std::invalid_argument("Cannot Cast Non numeric str type to float type");
}
}
return rez * fact;
}
str::operator double() {
double rez = 0, fact = 1;
int point_seen=0,i;
if (*s == '-'){
fact = -1;
};
i = *s == '-' || *s == '+' ? 1 : 0;
for (; i<::len(s); i++){
if (*(s+i) == '.'){
point_seen = 1;
continue;
}
int d = *(s+i) - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0;
rez = rez * 10.0 + (double )d;
} else{
throw std::invalid_argument("Cannot Cast Non numeric str type to double type");
}
}
return rez * fact;
}
//Type Casting - Above
// String Operations - Below
int str::len() {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
str str::operator+(str x) {
str op = new char[::len(s)+x.len()+1];
int i=0,j=0;
for(;j<::len(s);j++){
*(op+i)=*(s+j);
i++;
}
for(j=0;j<x.len();j++){
*(op+i)=*(x+j);
i++;
}
*(op+i)='\0';
return op;
}
void str::operator+=(str x) {
str op = new char[::len(s)+x.len()+1];
int i=0,j=0;
for(;j<::len(s);j++){
*(op+i)=*(s+j);
i++;
}
for(j=0;j<x.len();j++){
*(op+i)=*(x+j);
i++;
}
*(op+i)='\0';
s=op;
}
void str::operator<<=(str x) {
str op = new char [x.len()];
int i=0;
for(i=0;i<x.len();i++){
*(op+i)=*(x+i);
}
*(op+i)='\0';
s=op;
}
str str::rev(bool internal= false) {
str op = new char[::len(s)];
int j = 0;
for (int i = ::len(s) - 1; i >= 0; i--, j++) {
*(op + j) = *(s + i);
}
*(op + j) = '\0';
if(internal) {
s=op;
}
return op;
}
str str::operator()(int start, int end, int jump=1) {
str op= new char [((end-start+1)/jump)+1];
int i,j=0;
if(start<=end) {
for (i = start; i <= end; i += jump) {
*(op + j) = *(s + i);
j++;
}
} else{
for (i = start; i >= end; i += jump) {
*(op + j) = *(s + i);
j++;
}
}
*(op+j)='\0';
return op;
}
int str::countChar(char c) {
int count=0;
for(int i=0;i<::len(s);i++)
count = *(s+i)==c ? count+1 : count+0;
return count;
}
int str::firstIndexOf(char c) {
int index=-1;
for(int i=0;i<::len(s);i++){
if(*(s+i)==c){
index = i;
break;
}
}
return index;
}
int str::lastIndexOf(char c) {
int index=-1;
for(int i=0;i<::len(s);i++)
index = *(s+i)==c ? i : index;
return index;
}
char str::operator[](int x) {
if(x<-1*::len(s) || x>::len(s)-1){
throw std::out_of_range("Index our of Range\n");
}
if(x>=0){
return *(s+x);
}
else{
return *(s+::len(s)+x);
}
}
bool str::operator==(str x) {
for(int i=0;i<::len(s);i++){
if(*(s+i)!=*(x+i))
return false;
}
return true;
}
bool str::operator==(char *x) {
for(int i=0;i<::len(s);i++){
if(*(s+i)!=*(x+i))
return false;
}
return true;
}
// String Operations - Above
str::operator char *() {
return s;
}
#endif //STRING_STR_