-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.h
832 lines (722 loc) · 28 KB
/
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
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
#pragma once
#include <memory>
#include <initializer_list>
#include <iostream>
/*********************************************** CLASSES ***************************************************/
/*/////////////////////////////////////////// String class ////////////////////////////////////////////////*/
class String
{
private:
//Helping functions
void reallocate();
void free();
template<bool constness = false> class Iterator;
template<bool constness = false> class Reverse_Iterator;
public:
//Types
typedef char value_type;
typedef std::char_traits<char> char_traits;
typedef std::allocator<char> allocator_type;
typedef char& reference;
typedef const char& const_reference;
typedef char* pointer;
typedef const char* const_pointer;
typedef std::ptrdiff_t difference_type;
typedef size_t size_type;
typedef Iterator<> iterator;
typedef Iterator<true> const_iterator;
typedef Reverse_Iterator<> reverse_iterator;
typedef Reverse_Iterator<true> const_reverse_iterator;
//Public const member
static const size_t npos = -1;
public:
//Constructors, Destructor
String() = default;
String(const char* cptr);
String(const char* cptr, size_t n);
String(size_t n, char ch);
String(std::initializer_list<char> lst);
template<typename InputIterator> String(InputIterator first, InputIterator last);
String(const String& str);
String(const String& str, size_t subpos, size_t sublen = npos);
String(String&& str) noexcept;
~String() { free(); }
//Assignment overloads
String& operator=(const String& str);
String& operator=(const char* cptr);
String& operator=(char ch);
String& operator=(std::initializer_list<char> lst);
String& operator=(String&& str) noexcept;
//Iterators
iterator begin() noexcept;
const_iterator begin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
//Capacity
size_t size() const noexcept { return sz; }
size_t length() const noexcept { return sz; }
size_t max_size() const noexcept { return -1; }
size_t capacity() const noexcept { return cap; }
bool empty() const noexcept { return !cp; }
void clear() { free(); }
void resize(size_t n);
void resize(size_t n, char ch);
void reserve(size_t n = 0);
void shrink_to_fit();
//Element access
char& operator[](size_t n);
const char& operator[](size_t n) const;
char& at(size_t n);
const char& at(size_t n) const;
char& back();
const char& back() const;
char& front();
const char& front() const;
//Modifiers
String& operator+=(const String& str);
String& operator+=(const char* cptr);
String& operator+=(char ch);
String& operator+=(std::initializer_list<char> lst);
String& append(const String& str);
String& append(const String&, size_t str, size_t n = npos);
String& append(const char* cptr);
String& append(const char* cptr, size_t n);
String& append(size_t n, char ch);
template<typename InputIterator> String& append(InputIterator first, InputIterator last);
String& append(std::initializer_list<char> lst);
void push_back(char ch);
String& assign(const String& str);
String& assign(const String& str, size_t subpos, size_t sublen = npos);
String& assign(const char* cptr);
String& assign(const char* cptr, size_t n);
String& assign(size_t n, char ch);
template<typename InputIterator> String& assign(InputIterator first, InputIterator last);
String& assign(std::initializer_list<char> lst);
String& assign(String&& str) noexcept;
String& insert(size_t pos, const String& str);
String& insert(size_t pos, const String& str, size_t subpos, size_t sublen = npos);
String& insert(size_t pos, const char* cptr);
String& insert(size_t pos, const char* cptr, size_t n);
String& insert(size_t pos, size_t n, char ch);
iterator insert(const_iterator p, size_t n, char ch);
iterator insert(const_iterator p, char ch);
template<typename InputIterator> iterator insert(iterator p, InputIterator first, InputIterator last);
String& insert(const_iterator p, std::initializer_list<char> lst);
String& erase(size_t pos, size_t len = npos);
iterator erase(const_iterator p);
iterator erase(const_iterator first, const_iterator last);
String& replace(size_t pos, size_t len, const String& str);
String& replace(const_iterator first, const_iterator last, const String& str);
String& replace(size_t pos, size_t len, const String& str, size_t subpos, size_t sublen = npos);
String& replace(size_t pos, size_t len, const char* cptr);
String& replace(const_iterator first, const_iterator last, const char* cptr);
String& replace(size_t pos, size_t len, const char* cptr, size_t n);
String& replace(const_iterator first, const_iterator last, const char* cptr, size_t n);
String& replace(size_t pos, size_t len, size_t n, char ch);
String& replace(const_iterator first, const_iterator last, size_t n, char ch);
template<typename InputIterator>
String& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
String& replace(const_iterator first, const_iterator last, std::initializer_list<char> lst);
void swap(String& str);
void pop_back();
//String operations
const char* c_str();
const char* data();
allocator_type get_allocator() const noexcept;
size_t copy(char* cptr, size_t len, size_t pos = 0) const;
size_t find(const String& str, size_t pos = 0) const noexcept;
size_t find(const char* cptr, size_t pos = 0) const;
size_t find(const char* cptr, size_t pos, size_t n) const;
size_t find(char c, size_t pos = 0) const noexcept;
size_t rfind(const String& str, size_t pos = 0) const noexcept;
size_t rfind(const char* cptr, size_t pos = 0) const;
size_t rfind(const char* cptr, size_t pos, size_t n) const;
size_t rfind(char c, size_t pos = 0) const noexcept;
size_t find_first_of(const String& str, size_t pos = 0) const noexcept;
size_t find_first_of(const char* cptr, size_t pos = 0) const;
size_t find_first_of(const char* cptr, size_t pos, size_t n) const;
size_t find_first_of(char ch, size_t pos = 0) const noexcept;
size_t find_last_of(const String& str, size_t pos = 0) const noexcept;
size_t find_last_of(const char* cptr, size_t pos = 0) const;
size_t find_last_of(const char* cptr, size_t pos, size_t n) const;
size_t find_last_of(char ch, size_t pos = 0) const noexcept;
size_t find_first_not_of(const String& str, size_t pos = 0) const noexcept;
size_t find_first_not_of(const char* cptr, size_t pos = 0) const;
size_t find_first_not_of(const char* cptr, size_t pos, size_t n) const;
size_t find_first_not_of(char ch, size_t pos = 0) const noexcept;
size_t find_last_not_of(const String& str, size_t pos = 0) const noexcept;
size_t find_last_not_of(const char* cptr, size_t pos = 0) const;
size_t find_last_not_of(const char* cptr, size_t pos, size_t n) const;
size_t find_last_not_of(char ch, size_t pos = 0) const noexcept;
String substr(size_t pos = 0, size_t len = npos) const;
int compare(const String& str) const noexcept;
int compare(size_t pos, size_t len, const String& str) const;
int compare(size_t pos, size_t len, const String& str, size_t subpos, size_t sublen = npos) const;
int compare(const char* cptr) const;
int compare(size_t pos, size_t len, const char* cptr) const;
int compare(size_t pos, size_t len, const char* cptr, size_t n) const;
//Non-member function overloads
friend String operator+(const String&, const String&);
friend String operator+(const String&, String&&);
friend String operator+(String&&, const String&);
friend String operator+(String&&, String&&);
friend String operator+(const String&, const char*);
friend String operator+(String&&, const char*);
friend String operator+(const char*, const String&);
friend String operator+(const char*, String&&);
friend String operator+(const String&, char);
friend String operator+(String&&, char);
friend String operator+(char, const String&);
friend String operator+(char, String&&);
friend bool operator==(const String&, const String&) noexcept;
friend bool operator==(const char*, const String&);
friend bool operator==(const String&, const char*);
friend bool operator!=(const String&, const String&) noexcept;
friend bool operator!=(const char*, const String&);
friend bool operator!=(const String&, const char*);
friend bool operator<(const String&, const String&) noexcept;
friend bool operator<(const char*, const String&);
friend bool operator<(const String&, const char*);
friend bool operator<=(const String&, const String&) noexcept;
friend bool operator<=(const char*, const String&);
friend bool operator<=(const String&, const char*);
friend bool operator>(const String&, const String&) noexcept;
friend bool operator>(const char*, const String&);
friend bool operator>(const String&, const char*);
friend bool operator>=(const String&, const String&) noexcept;
friend bool operator>=(const char*, const String&);
friend bool operator>=(const String&, const char*);
friend void swap(String&, String&);
friend std::istream& operator>>(std::istream&, const String&);
friend std::ostream& operator<<(std::ostream&, const String&);
friend std::istream& getline(std::istream&, String&, char);
friend std::istream& getline(std::istream&&, String&, char);
friend std::istream& getline(std::istream&, String&);
friend std::istream& getline(std::istream&&, String&);
private:
static std::allocator<char> alloc;
size_t sz = 0;
char* cp = nullptr;
size_t cap = 0;
};
/*//////////////////////////////////////////// Iterator class ////////////////////////////////////////////////*/
/* String's iterator. Based of a random-access iterator */
template<bool constness> class String::Iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename std::conditional_t<constness, const char, char>;
using difference_type = std::ptrdiff_t;
using reference = typename std::conditional_t<constness, const char&, char&>;
using pointer = typename std::conditional_t<constness, const char*, char*>;
public:
explicit Iterator(char* cp) : m_cp(cp) {}
/* Conversion */
operator const_iterator() const { return const_iterator(m_cp); }
friend iterator;
/* Iterate operations */
Iterator& operator++();
Iterator operator++(int);
Iterator& operator--();
Iterator operator--(int);
Iterator operator+(size_t n) const;
Iterator& operator+=(size_t n);
Iterator operator-(size_t n) const;
Iterator& operator-=(size_t n);
/* Access operations */
template<bool _constness = constness> std::enable_if_t<_constness, reference>
operator*() const;
template<bool _constness = constness> std::enable_if_t<!_constness, reference>
operator*();
template<bool _constness = constness> std::enable_if_t<_constness, reference>
operator[](size_t index) const;
template<bool _constness = constness> std::enable_if_t<!_constness, reference>
operator[](size_t index);
/* Rational operations */
bool operator<(const const_iterator& rhs) const;
bool operator<=(const const_iterator& rhs) const;
bool operator>(const const_iterator& rhs) const;
bool operator>=(const const_iterator& rhs) const;
bool operator==(const const_iterator& rhs) const;
bool operator!=(const const_iterator& rhs) const;
private:
pointer m_cp;
};
/* String's reverse iterator, based of normal String's Iterator */
template <bool constness> class String::Reverse_Iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename std::conditional_t<constness, const char, char>;
using difference_type = std::ptrdiff_t;
using reference = typename std::conditional_t<constness, const char&, char&>;
using pointer = typename std::conditional_t<constness, const char*, char*>;
public:
Reverse_Iterator(char* cp) : m_cp(cp) {}
/* Conversion */
operator const_reverse_iterator() { return const_reverse_iterator(m_cp); }
friend reverse_iterator;
/* Iterate operations */
Reverse_Iterator& operator++();
Reverse_Iterator operator++(int);
Reverse_Iterator& operator--();
Reverse_Iterator operator--(int);
Reverse_Iterator operator+(size_t n) const;
Reverse_Iterator& operator+=(size_t n);
Reverse_Iterator operator-(size_t n) const;
Reverse_Iterator& operator-=(size_t n);
/* Access operations */
template<bool _constness = constness> std::enable_if_t<_constness, reference>
operator*() const;
template<bool _constness = constness> std::enable_if_t<!_constness, reference>
operator*();
template<bool _constness = constness> std::enable_if_t<_constness, reference>
operator[](size_t index) const;
template<bool _constness = constness> std::enable_if_t<!_constness, reference>
operator[](size_t index);
/* Rational operations */
bool operator<(const const_reverse_iterator& rhs) const;
bool operator<=(const const_reverse_iterator& rhs) const;
bool operator>(const const_reverse_iterator& rhs) const;
bool operator>=(const const_reverse_iterator& rhs) const;
bool operator==(const const_reverse_iterator& rhs) const;
bool operator!=(const const_reverse_iterator& rhs) const;
private:
pointer m_cp;
};
/****************************************** FUNCTIONS DECLARATIONS *********************************************/
String operator+(const String& lhs, const String& rhs);
String operator+(const String& lhs, String&& rhs);
String operator+(String&& lhs, const String& rhs);
String operator+(String&& lhs, String&& rhs);
String operator+(const String& lhs, const char* rhs);
String operator+(String&& lhs, const char* rhs);
String operator+(const char* lhs, const String& rhs);
String operator+(const char* lhs, String&& rhs);
String operator+(const String& lhs, char rhs);
String operator+(String&& lhs, char rhs);
String operator+(char lhs, const String& rhs);
String operator+(char lhs, String&& rhs);
bool operator==(const String& lhs, const String& rhs) noexcept;
bool operator==(const char* lhs, const String& rhs);
bool operator==(const String& lhs, const char* rhs);
bool operator!=(const String& lhs, const String& rhs) noexcept;
bool operator!=(const char* lhs, const String& rhs);
bool operator!=(const String& lhs, const char* rhs);
bool operator<(const String& lhs, const String& rhs) noexcept;
bool operator<(const char* lhs, const String& rhs);
bool operator<(const String& lhs, const char* rhs);
bool operator<=(const String& lhs, const String& rhs) noexcept;
bool operator<=(const char* lhs, const String& rhs);
bool operator<=(const String& lhs, const char* rhs);
bool operator>(const String& lhs, const String& rhs) noexcept;
bool operator>(const char* lhs, const String& rhs);
bool operator>(const String& lhs, const char* rhs);
bool operator>=(const String& lhs, const String& rhs) noexcept;
bool operator>=(const char* lhs, const String& rhs);
bool operator>=(const String& lhs, const char* rhs);
void swap(String& lhs, String& rhs);
std::istream& operator>>(std::istream& is, String& str);
std::ostream& operator<<(std::ostream& os, const String& str);
std::istream& getline(std::istream& is, String& str, char delim);
std::istream& getline(std::istream&& is, String& str, char delim);
std::istream& getline(std::istream& is, String& str);
std::istream& getline(std::istream&& is, String& str);
/******************************************** ITERATOR FUNCTIONS ********************************************/
/* Dereference the pointer */
template<bool constness>
template<bool _constness> std::enable_if_t<_constness, typename String::Iterator<constness>::reference>
String::Iterator<constness>::operator*() const
{
return *m_cp;
}
/* Dereference the pointer */
template<bool constness>
template<bool _constness> std::enable_if_t<!_constness, typename String::Iterator<constness>::reference>
String::Iterator<constness>::operator*()
{
return *m_cp;
}
/* Iterate one place forward */
template<typename bool constness>
String::Iterator<constness>& String::Iterator<constness>::operator++()
{
++m_cp;
return *this;
}
/* Iterate one place forward, return the old pointer */
template<typename bool constness>
String::Iterator<constness> String::Iterator<constness>::operator++(int)
{
Iterator result(m_cp);
++m_cp;
return result;
}
/* Iterate one place backward */
template<typename bool constness>
String::Iterator<constness>& String::Iterator<constness>::operator--()
{
--m_cp;
return *this;
}
/* Iterate one place backward, return the old pointer */
template<typename bool constness>
String::Iterator<constness> String::Iterator<constness>::operator--(int)
{
Iterator result(m_cp);
--m_cp;
return result;
}
/* Return the dereferenced character at the given index */
template<bool constness> template<bool _constness>
std::enable_if_t<_constness, typename String::Iterator<constness>::reference>
String::Iterator<constness>::operator[](size_t index) const
{
return *(m_cp + index);
}
/* Return the dereferenced character at the given index */
template<bool constness> template<bool _constness>
std::enable_if_t<!_constness, typename String::Iterator<constness>::reference>
String::Iterator<constness>::operator[](size_t index)
{
return *(m_cp + index);
}
/* Return the iterator, that's incremented (n) times */
template<bool constness>
String::Iterator<constness> String::Iterator<constness>::operator+(size_t n) const
{
return Iterator(m_cp + n);
}
/* Return the iterator, that's decremented (n) times */
template<bool constness>
String::Iterator<constness> String::Iterator<constness>::operator-(size_t n) const
{
return Iterator(m_cp + n);
}
/* Move this iterator forward (n) times */
template<bool constness>
String::Iterator<constness>& String::Iterator<constness>::operator+=(size_t n)
{
m_cp += n;
return *this;
}
/* Move this iterator backward (n) times */
template<bool constness>
String::Iterator<constness>& String::Iterator<constness>::operator-=(size_t n)
{
m_cp -= n;
return *this;
}
/* Check if the iterators are equal to each other */
template<bool constness>
bool String::Iterator<constness>::operator==(const const_iterator& rhs) const
{
return m_cp == rhs.m_cp;
}
/* Check if the iterators aren't equal to each other */
template<bool constness>
bool String::Iterator<constness>::operator!=(const const_iterator& rhs) const
{
return !(*this == rhs);
}
/* Check if the first iterator is lesser than the second */
template<bool constness>
bool String::Iterator<constness>::operator<(const const_iterator& rhs) const
{
return m_cp < rhs.m_cp;
}
/* Check if the first iterator is lesser than or equal to the second */
template<bool constness>
bool String::Iterator<constness>::operator<=(const const_iterator& rhs) const
{
return (*this < rhs) || (*this == rhs);
}
/* Check if the first iterator is higher than the second */
template<bool constness>
bool String::Iterator<constness>::operator>(const const_iterator& rhs) const
{
return !(*this < rhs) && (*this != rhs);
}
/* Check if the first iterator is higher than or equal to the second */
template<bool constness>
bool String::Iterator<constness>::operator>=(const const_iterator& rhs) const
{
return (*this > rhs) || (*this == rhs);
}
/* Iterate this reverse iterator "forwards"*/
template <bool constness>
String::Reverse_Iterator<constness>& String::Reverse_Iterator<constness>::operator++()
{
--m_cp;
return *this;
}
/* Iterate this reverse iterator "forwards", return the old reverse iterator */
template <bool constness>
String::Reverse_Iterator<constness> String::Reverse_Iterator<constness>::operator++(int)
{
Reverse_Iterator result(m_cp);
--m_cp;
return result;
}
/* Iterate this reverse iterator "backwards" */
template <bool constness>
String::Reverse_Iterator<constness>& String::Reverse_Iterator<constness>::operator--()
{
++m_cp;
return *this;
}
/* Iterate this reverse iterator "backwards", return the old reverse iterator */
template <bool constness>
String::Reverse_Iterator<constness> String::Reverse_Iterator<constness>::operator--(int)
{
Reverse_Iterator result(m_cp);
++m_cp;
return result;
}
/* Move the reverse iterator "fowards" (n) times, return it */
template<bool constness>
String::Reverse_Iterator<constness> String::Reverse_Iterator<constness>::operator+(size_t n) const
{
return Reverse_Iterator(m_cp - n);
}
/* Move the reverse iterator "backwards" (n) times, return it */
template<bool constness>
String::Reverse_Iterator<constness> String::Reverse_Iterator<constness>::operator-(size_t n) const
{
return Reverse_Iterator(m_cp + n);
}
/* Move this reverse iterator "fowards" (n) times, return it*/
template<bool constness>
String::Reverse_Iterator<constness>& String::Reverse_Iterator<constness>::operator+=(size_t n)
{
m_cp -= n;
return *this;
}
/* Move this reverse iterator "backwards" (n) times, return it */
template <bool constness>
String::Reverse_Iterator<constness>& String::Reverse_Iterator<constness>::operator-=(size_t n)
{
m_cp += n;
return *this;
}
/* Dereference this reverse iterator */
template<bool constness> template<bool _constness>
std::enable_if_t<_constness, typename String::Reverse_Iterator<constness>::reference>
String::Reverse_Iterator<constness>::operator*() const
{
return *m_cp;
}
/* Dereference this reverse iterator */
template<bool constness> template<bool _constness>
std::enable_if_t<!_constness, typename String::Reverse_Iterator<constness>::reference>
String::Reverse_Iterator<constness>::operator*()
{
return *m_cp;
}
/* Return the dereferenced character at the given index */
template<bool constness> template<bool _constness>
std::enable_if_t<_constness, typename String::Reverse_Iterator<constness>::reference>
String::Reverse_Iterator<constness>::operator[](size_t index) const
{
return *(m_cp + index);
}
/* Return the dereferenced character at the given index */
template<bool constness> template<bool _constness>
std::enable_if_t<!_constness, typename String::Reverse_Iterator<constness>::reference>
String::Reverse_Iterator<constness>::operator[](size_t index)
{
return *(m_cp + index);
}
/* Check if both reverse iterators are equal to each other */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator==(const const_reverse_iterator& rhs) const
{
return (m_cp == rhs.m_cp);
}
/* Check if both reverse iterators aren't equal to each other */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator!=(const const_reverse_iterator& rhs) const
{
return !(*this == rhs);
}
/* Check if this reverse iterator is lesser than the second */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator<(const const_reverse_iterator& rhs) const
{
return m_cp < rhs.m_cp;
}
/* Check if this reverse iterator is lesser than or equal to the second */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator<=(const const_reverse_iterator& rhs) const
{
return (*this < rhs) || (*this == rhs);
}
/* Check if this reverse iterator is higher than the second */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator>(const const_reverse_iterator& rhs) const
{
return (*this != rhs) && !(*this < rhs);
}
/* Check if this reverse iterator is higher than or equal to the second */
template <bool constness>
bool String::Reverse_Iterator<constness>::operator>=(const const_reverse_iterator& rhs) const
{
return (*this > rhs) || (*this == rhs);
}
/************************************* STRING ITERATOR FUNCTIONS ****************************************/
/* Create String from two the range in between two input iterators */
template<typename InputIterator> String::String(InputIterator first, InputIterator last)
{
size_t size = 0;
for (InputIterator beg = first; beg != last; beg++, size++)
;
cap = sz = size;
cp = alloc.allocate(cap);
size_t i = 0;
for (InputIterator beg = first; beg != last; beg++, i++)
alloc.construct(cp + i, *beg);
}
/* Append the String created from the given range, to this String */
template<typename InputIterator> String& String::append(InputIterator first, InputIterator last)
{
size_t itSize = 0;
for (InputIterator beg = first; beg != last; beg++, itSize++)
;
size_t newSize = sz + itSize;
InputIterator beg = first;
if (cap >= newSize) {
for (size_t i = 0; i < itSize; i++)
alloc.construct(cp + sz++, *(beg + i));
}
else {
auto newCp = alloc.allocate(newSize);
size_t i = 0;
for (; i < sz; i++)
alloc.construct(newCp + i, std::move(*(cp + i)));
free();
for (size_t j = 0; j < itSize; j++)
alloc.construct(newCp + i++, *(beg + j));
cp = newCp;
sz = cap = newSize;
}
return *this;
}
/* Assign this String to the String created from the given range */
template<typename InputIterator> String& String::assign(InputIterator first, InputIterator last)
{
size_t itSize = 0;
for (InputIterator beg = first; beg != last; beg++, itSize++)
;
InputIterator beg = first;
if (cap >= itSize) {
for (int i = sz; i >= 0; i--)
alloc.destroy(cp + i);
for (size_t j = 0; j < itSize; j++)
alloc.construct(cp + j, *beg++);
sz = itSize;
}
else {
free();
sz = cap = itSize;
cp = alloc.allocate(cap);
for (size_t i = 0; i < sz; i++)
alloc.construct(cp + i, *beg++);
}
return *this;
}
/* Insert the characters from the given range into the place where iterator points to */
template<typename InputIterator>
String::iterator String::insert(iterator p, InputIterator first, InputIterator last)
{
bool match = false;
size_t index = 0;
for (iterator beg = begin(); beg != end(); beg++, index++) {
if (beg == p) {
match = true;
break;
}
}
if (match == false) {
match = (end() == p);
if (match == false)
return last;
}
size_t rangeSize = 0;
for (InputIterator beg = first; beg != last; beg++)
rangeSize++;
const size_t newSize = sz + rangeSize;
String old(*this);
if (cap >= newSize) {
if (cp) {
for (int i = sz - 1; i >= 0; i--)
alloc.destroy(cp + i);
}
sz = newSize;
}
else {
free();
cp = alloc.allocate(newSize);
sz = cap = newSize;
}
InputIterator beg = first;
size_t i = 0;
for (; i < index; i++)
alloc.construct(cp + i, *(old.cp + i));
size_t stopPlace = i;
for (size_t j = 0; j < rangeSize; j++)
alloc.construct(cp + i++, *beg++);
for (; stopPlace < old.sz; stopPlace++)
alloc.construct(cp + i++, *(old.cp + stopPlace));
return (begin() + index + rangeSize);
}
/* Replace the given range with characters from the second range */
template<typename InputIterator>
String& String::replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last)
{
bool match1 = false;
bool match2 = false;
size_t index_last = 0, index = 0;
size_t index_first = 0;
for (iterator beg = begin(); beg != end(); beg++, index++) {
if (i1 == beg) {
match1 = true;
index_first = index;
}
if (i2 == beg) {
match2 = true;
index_last = index;
}
}
if (match2 == false) {
match2 = (i2 == end());
index_last = index;
}
if (match1 == false || match2 == false)
return *this;
size_t rangeSize = 0;
for (InputIterator beg = first; beg != last; beg++)
rangeSize++;
size_t newSize = sz - (index_last - index_first) + rangeSize;
auto newCp = alloc.allocate((newSize > cap) ? newSize : cap);
size_t i = 0;
for (; i < index_first; i++)
alloc.construct(newCp + i, std::move(*(cp + i)));
size_t left = i + (index_last - index_first);
InputIterator iter = first;
for (size_t j = 0; j < rangeSize; j++)
alloc.construct(newCp + i++, *iter++);
for (size_t j = left; j < sz; j++)
alloc.construct(newCp + i++, std::move(*(cp + j)));
free();
sz = cap = newSize;
cp = newCp;
return *this;
}