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
|
#include "String.h"
#include <cstring>
void String::free() {
delete[] str;
}
void String::copyFrom(const String& other) {
length = other.length;
// Не забравяме за терминиращата нула
str = new char[length + 1];
strcpy(str, other.str);
}
String::String() {
str = nullptr;
length = 0;
}
String::~String() {
free();
}
String::String(const String& other) {
copyFrom(other);
}
String& String::operator=(const String& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
String::String(String&& other) {
str = other.str;
length = other.length;
other.str = nullptr;
other.length = 0;
}
String& String::operator=(String&& other) {
if (this != &other) {
free();
str = other.str;
length = other.length;
str = nullptr;
length = 0;
}
return *this;
}
String::String(const char* str) {
this->length = strlen(str);
this->str = new char[this->length + 1];
strcpy(this->str, str);
}
std::ostream& operator<<(std::ostream& ostr, const String& string) {
if (string.str == nullptr)
return ostr << "";
return ostr << string.str;
}
String& String::operator+=(const String& other) {
char* largerStr = new char[length + other.length + 1];
strcpy(largerStr, str);
strcat(largerStr, other.str);
delete[] str;
str = largerStr;
length += other.length;
return *this;
}
char String::operator[](unsigned index) const {
return str[index];
}
String::IndexRange::IndexRange(unsigned index) {
start = end = index;
}
String::IndexRange& String::IndexRange::operator,(const IndexRange& rhs) {
if (rhs.start < this->start)
this->start = rhs.start;
if (this->end < rhs.end)
this->end = rhs.end;
return *this;
}
String String::operator[](IndexRange range) const {
if (range.end >= length)
range.end = length - 1;
if (range.start > range.end)
return String();
if (range.start == 0 && range.end == length - 1)
return *this;
String substr;
substr.length = range.end - range.start + 1;
substr.str = new char[substr.length + 1];
strncpy(substr.str, str + range.start, substr.length);
substr.str[substr.length] = '\0';
return substr;
}
bool operator==(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str) == 0;
}
bool operator!=(const String& lhs, const String& rhs) {
return !(lhs == rhs);
}
bool operator<(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str) < 0;
}
bool operator<=(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str) <= 0;
}
bool operator>(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str) > 0;
}
bool operator>=(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str) >= 0;
}
bool operator<<(const String& lhs, const String& rhs) {
if (rhs.length > lhs.length)
return false;
return strcmp(lhs.str + (lhs.length - rhs.length), rhs.str) == 0;
}
bool operator>>(const String& lhs, const String& rhs) {
if (lhs.length > rhs.length)
return false;
return strncmp(lhs.str, rhs.str, lhs.length) == 0;
}
String operator+(const String& lhs, const String& rhs) {
return String(lhs) += rhs;
}
|