This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klib.string.h
691 lines (531 loc) · 18.8 KB
/
klib.string.h
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
#ifndef KLIB_STRING_H
#define KLIB_STRING_H
#include <string> // for iostream
typedef class String {
friend String operator+ (const char *, const String &);
friend bool operator== (const char *, String &);
friend bool operator!= (const char *, String &);
friend std::ostream& operator<< (std::ostream &, const String &);
friend std::istream& operator>> (std::istream &, String &);
friend std::istream& getline(std::istream &, String &);
/* The method converts numbers to arithmetic string. max decimal places is 2. */
template <class number>
friend String toString(const number &);
template <class number>
friend String toCalStr(const number &);
/* The method converts Unicode values into characters. */
friend char fromCharCode(const unsigned &);
/* The method retruns length of c-string. */
friend unsigned strlen(const char *);
private:
/* The encrypted data of string class (stored in c-string form). */
char *_proto_;
public:
/* The length property returns the length of a string (number of characters). */
unsigned length;
String();
String(const char *);
String(const char &);
String(const String &);
~String();
operator char *();
operator const char *();
char& operator[] (const unsigned &);
String operator+ (const char *);
String operator+ (const String &);
String operator+= (const char *);
String operator+= (const char &);
String operator+= (const String &);
String operator= (const char *);
String operator= (const String &);
bool operator== (const char *);
bool operator== (const String &);
bool operator!= (const char *);
bool operator!= (const String &);
String operator* (const unsigned);
/* The method returns the character at the specified index in a string. */
char charAt(const unsigned &);
/* The method returns the Unicode of the character at the specified index in a string. */
unsigned charCodeAt(const unsigned &);
/* The method is used to join two or more strings. */
String concat(const String &);
/* The method retunrs the value of c-string. */
char* cstr();
/* The method determines whether a string ends with the characters of a specified string. */
bool endsWith(const String &, unsigned=-1);
/* The method determines whether a string contains the characters of a specified string. */
bool includes(const String &, const unsigned=0);
/* The method returns the position of the first occurrence of a specified value in a string. */
int indexOf(const String &, const unsigned=0);
/* The method returns the position of the last occurrence of a specified value in a string. */
int lastIndexOf(const String &, unsigned=-1);
/* The method compares two strings in the current locale. */
int localeCompare(const String &);
/* The method searches a string for a match against a regular expression, and returns the matches, as an Array object. */
String match(const String &);
/* method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. */
String matchAll(const String &);
/* The method returns the Unicode Normalization Form of the string. */
String normalize(const String & = "NFC");
/* The method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length */
String padEnd(const unsigned &, const String &);
/* The method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length */
String padStart(const unsigned &, const String &);
/* The method returns a new string with a specified number of copies of the string it was called on. */
String repeat(const unsigned &);
/* The method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. */
String replace(const String &, const String &);
/* The method searches a string for a specified value, and returns the position of the match. */
int search(const String &);
/* The method extracts parts of a string and returns the extracted parts in a new string. */
String slice(const unsigned, unsigned=-1);
/* The method is used to split a string into an array of substrings, and returns the new array. */
Array<String> split(const String &);
/* The method determines whether a string begins with the characters of a specified string. */
bool startsWith(const String &, const unsigned=0);
/* The method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. */
String substr(const unsigned, unsigned=-1);
/* The method extracts the characters from a string, between two specified indices, and returns the new sub string. */
String substring(unsigned, unsigned=-1);
/* The method converts a string to lowercase letters. */
String toLowerCase();
/* The method returns the value of a String object. */
String toString();
/* The method converts a string to uppercase letters. */
String toUpperCase();
/* The method removes whitespace from both sides of a string. */
String trim();
/* The method removes whitespace from the beginning of the string. */
String trimStart();
/* The method removes whitespace from the beginning of the string. */
String trimLeft();
/* The method removes whitespace from the end of the string. */
String trimEnd();
/* The method removes whitespace from the end of the string. */
String trimRight();
/* The method returns the primitive value of a String object. */
String valueOf();
} string;
/* constructor */
String::String() {
_proto_ = NULL;
length = 0;
}
String::String(const char *str) {
_proto_ = new char[(length = strlen(str))+1];
_proto_[length] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = str[i];
}
String::String(const char &c) {
length = 1;
_proto_ = new char[2];
_proto_[0] = c;
_proto_[1] = '\0';
}
String::String(const String &str) {
_proto_ = new char[(length = str.length)+1];
_proto_[length] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = str._proto_[i];
}
String::~String() {
delete[] _proto_;
}
/* call operators */
String::operator char*() { return _proto_; }
String::operator const char*() { return _proto_; }
char& String::operator[] (const unsigned &index) { return _proto_[index]; }
/* processing operators: FRIEND */
std::ostream& operator<< (std::ostream &out, const String &str) {
out << str._proto_;
return out;
}
std::istream& operator>> (std::istream &in, String &str) {
std::string t;
in >> t;
str._proto_ = new char[(str.length = t.size())+1];
str._proto_[str.length] = '\0';
for (unsigned i = 0; i < str.length; i++) str._proto_[i] = t[i];
return in;
}
std::istream& getline(std::istream &in, String &str) {
std::string t;
std::getline(std::cin, t);
str._proto_ = new char[(str.length = t.size())+1];
str._proto_[str.length] = '\0';
for (unsigned i = 0; i < str.length; i++) str._proto_[i] = t[i];
return in;
}
/* processing operators: OVERLOAD */
String operator+ (const char *lstr, const String &rstr) {
unsigned len = strlen(lstr);
char result[len+rstr.length+1];
result[len+rstr.length] = '\0';
for (unsigned i = 0; i < len; i++) result[i] = lstr[i];
for (unsigned i = 0; i < rstr.length; i++) result[len+i] = rstr._proto_[i];
return result;
}
String String::operator+ (const char *str) {
unsigned len = strlen(str);
char result[length+len+1];
result[length+len] = '\0';
for (unsigned i = 0; i < length; i++) result[i] = _proto_[i];
for (unsigned i = 0; i < len; i++) result[length+i] = str[i];
return result;
}
String String::operator+ (const String &str) {
char result[length+str.length+1];
result[length+str.length] = '\0';
for (unsigned i = 0; i < length; i++) result[i] = _proto_[i];
for (unsigned i = 0; i < str.length; i++) result[length+i] = str._proto_[i];
return result;
}
/* ### */
String String::operator+= (const char *str) {
unsigned len = strlen(str);
char *old = _proto_;
_proto_ = new char[length+len+1];
_proto_[length+len] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = old[i];
for (unsigned i = 0; i < len; i++) _proto_[length+i] = str[i];
delete[] old;
length += len;
return *this;
}
String String::operator+= (const char &chr) {
char *old = _proto_;
_proto_ = new char[length+2];
_proto_[length++] = chr;
_proto_[length] = '\0';
for (unsigned i = 0; i < length-1; i++) _proto_[i] = old[i];
delete[] old;
return *this;
}
String String::operator+= (const String &str) {
char *old = _proto_;
_proto_ = new char[length+str.length+1];
_proto_[length+str.length] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = old[i];
for (unsigned i = 0; i < str.length; i++) _proto_[length+i] = str._proto_[i];
delete[] old;
length += str.length;
return *this;
}
/* ### */
String String::operator= (const char *str) {
delete[] _proto_;
_proto_ = new char[(length = strlen(str))+1];
_proto_[length] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = str[i];
return *this;
}
String String::operator= (const String &str) {
delete[] _proto_;
_proto_ = new char[(length = str.length)+1];
_proto_[length] = '\0';
for (unsigned i = 0; i < length; i++) _proto_[i] = str._proto_[i];
return *this;
}
/* ### */
bool String::operator== (const String &str) {
if (length != str.length) return false;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] != str._proto_[i]) return false;
}
return true;
}
bool String::operator== (const char *str) {
if (length != strlen(str)) return false;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] != str[i]) return false;
}
return true;
}
bool String::operator!= (const String &str) {
if (length != str.length) return true;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] != str._proto_[i]) return true;
}
return false;
}
bool String::operator!= (const char *str) {
if (length != strlen(str)) return true;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] != str[i]) return true;
}
return false;
}
/* ### */
String String::operator* (const unsigned count) {
if (count < 1) return "";
unsigned len = length*count;
char result[len+1];
result[len] = '\0';
for (unsigned i = 0; i < len; i++) result[i] = _proto_[i%length];
return result;
}
/* class methods: FRIEND */
template <class number>
String toString(const number &n) {
string t = std::to_string(n).c_str();
short dotPos = t.search(".");
if (dotPos == -1) return t;
else {
string cut = t.slice(0, dotPos+3);
if (cut[cut.length - 1] == '0' && cut[cut.length - 2] == '0') return t.slice(0, dotPos);
else if (cut[cut.length - 1] == '0') return t.slice(0, dotPos+2); //.x
else return cut; // .xx
}
}
template <class number>
String toCalStr(const number &n) {
if (n == 1) return "";
else if (n == -1) return "-";
string t = std::to_string(n).c_str();
short dotPos = t.search(".");
if (dotPos == -1) return t;
else {
string cut = t.slice(0, dotPos+3);
if (cut[cut.length - 1] == '0' && cut[cut.length - 2] == '0') return t.slice(0, dotPos);
else if (cut[cut.length - 1] == '0') return t.slice(0, dotPos+2); //.x
else return cut; // .xx
}
}
unsigned strlen(const char *str) {
unsigned i = 0;
while (str[i]) i++;
return i;
}
/* class methods: BUILT-IN */
char String::charAt(const unsigned &index) {
return _proto_[index];
}
unsigned String::charCodeAt(const unsigned &index) {
return _proto_[index];
}
char* String::cstr() {
return _proto_;
}
String String::concat(const String &str) {
char result[length+str.length+1];
result[length+str.length] = '\0';
for (unsigned i = 0; i < length; i++) result[i] = _proto_[i];
for (unsigned i = 0; i < str.length; i++) result[length + i] = str._proto_[i];
return result;
}
bool String::endsWith(const String &searchvalue, unsigned atlength) {
if (atlength == -1 || atlength > length) atlength = length;
unsigned j = 0;
for (unsigned i = atlength-searchvalue.length; i < atlength; i++) {
if (searchvalue._proto_[j] == _proto_[i]) {
if (++j == searchvalue.length) return true;
}
else return false;
}
return false;
}
bool String::includes(const String &searchvalue, const unsigned start) {
unsigned j = 0;
for (unsigned i = start; i < length; i++) {
if (_proto_[i] == searchvalue._proto_[j]) {
if (++j == searchvalue.length) return true;
}
else j = 0;
}
return false;
}
int String::indexOf(const String &searchvalue, const unsigned start) {
unsigned j = 0, fisrtFindIndex = 0;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] == searchvalue._proto_[j]) {
if (!j) fisrtFindIndex = i;
if (++j == searchvalue.length) return fisrtFindIndex;
}
else {
j = 0;
}
}
return -1;
}
int String::lastIndexOf(const String &searchvalue, unsigned start) {
if (start == -1) start = length;
unsigned j = searchvalue.length-1;
for (unsigned i = length-1; i >= 0; i--) {
if (searchvalue._proto_[j--] == _proto_[i]) {
if (!j) return i-1;
}
else j = searchvalue.length-1;
}
return -1;
}
int String::localeCompare(const String &compareString) {
}
String String::match(const String ®ex) {
}
String String::matchAll(const String ®ex) {
}
String String::normalize(const String &form) {
if (form._proto_ == "NFC") {
}
else if (form._proto_ == "NFD") {
}
else if (form._proto_ == "NFKC") {
}
else if (form._proto_ == "NFKD") {
}
else {
throw "RangeError";
}
}
String String::padEnd(const unsigned &, const String &) {
}
String String::padStart(const unsigned &, const String &) {
}
String String::repeat(const unsigned &count) {
if (count < 1) return "";
unsigned len = length*count;
char result[len+1];
result[len] = '\0';
for (unsigned i = 0; i < len; i++) result[i] = _proto_[i%length];
return result;
}
String String::replace(const String &searchvalue, const String &newvalue) {
if (!searchvalue.length) return *this;
string result = "";
unsigned j = 0, firstFindIndex = 0;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] == searchvalue._proto_[j]) {
if (!j) firstFindIndex = i;
if (++j == searchvalue.length) {
firstFindIndex = j = 0;
if (!newvalue.length) continue;
result += newvalue;
}
}
else {
if (firstFindIndex) result += this->slice(firstFindIndex, i+1);
else result += _proto_[i];
firstFindIndex = j = 0;
}
}
return result;
}
int String::search(const String &searchvalue) {
unsigned j = 0, fisrtFindIndex = 0;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] == searchvalue._proto_[j]) {
if (!j) fisrtFindIndex = i;
if (++j == searchvalue.length) return fisrtFindIndex;
}
else {
j = 0;
}
}
return -1;
}
String String::slice(const unsigned start, unsigned end) {
if (end == -1) end = length;
if (end-start <= 0) return "";
char result[end-start+1];
result[end-start] = '\0';
unsigned j = 0;
for (unsigned i = start; i < end; i++) result[j++] = _proto_[i];
return result;
}
Array<String> String::split(const String &separator) {
Array<String> results;
unsigned splitat = 0, j = 0;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] == separator._proto_[j]) {
if (++j == separator.length) {
results.push(this->slice(splitat,i-separator.length+1));
splitat = i+1;
j = 0;
}
}
else {
j = 0;
}
}
results.push(this->slice(splitat));
return results;
}
bool String::startsWith(const String &searchvalue, const unsigned start) {
unsigned i = start;
while (i < length) {
if (_proto_[i] == searchvalue._proto_[i]) {
if (++i == searchvalue.length) return true;
}
else return false;
}
return false;
}
String String::substr(const unsigned start, unsigned atlength) {
if (atlength == -1) atlength = length;
if (atlength-start <= 0) return "";
char result[atlength-start+1];
result[atlength-start] = '\0';
unsigned j = 0;
for (unsigned i = start; i < atlength; i++) result[j++] = _proto_[i];
return result;
}
String String::substring(unsigned start, unsigned end) {
if (end == -1) end = length;
if (end-start <= 0) return "";
else if (start > end) {
unsigned temp = start;
start = end;
end = start;
}
char result[end-start+1];
result[end-start] = '\0';
unsigned j = 0;
for (unsigned i = start; i < end; i++) result[j++] = _proto_[i];
return result;
}
String String::toLowerCase() {
char str[length+1];
str[length] = '\0';
for (unsigned i = 0; i < length; i++) {
if ((str[i] = _proto_[i]) >= 65 && str[i] <= 90) str[i] += 32;
}
return str;
}
String String::toString() {
return *this;
}
String String::toUpperCase() {
char str[length+1];
str[length] = '\0';
for (unsigned i = 0; i < length; i++) {
if ((str[i] = _proto_[i]) >= 97 && str[i] <= 122) str[i] -= 32;
}
return str;
}
String String::trim() {
unsigned indexLeft = 0, indexRight = 0;
for (unsigned i = 0; i < length; i++) {
if (_proto_[i] != ' ') {
indexLeft = i;
break;
}
}
for (unsigned i = length-1; i >= 0; i--) {
if (_proto_[i] != ' ') {
indexRight = i+1;
break;
}
}
return this->slice(indexLeft, indexRight);
}
String String::trimStart() {
}
String String::trimLeft() {
}
String String::trimEnd() {
}
String String::trimRight() {
}
String String::valueOf() {
return *this;
}
#endif