-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
215 lines (207 loc) · 4.76 KB
/
string.cpp
File metadata and controls
215 lines (207 loc) · 4.76 KB
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
#pragma once
TString::TString(const char *str) {
if (str == nullptr) {
return;
}
unsigned long long size = 0;
while (str[size] != 0) {
++size;
}
Data = new char[size + 1];
Size = size;
Capacity = size + 1;
for (unsigned long long i = 0; i < Size; ++i) {
Data[i] = str[i];
}
Data[size] = 0;
}
TString::TString(const TString& other) {
Data = new char[other.Size + 1];
Size = other.Size;
Capacity = Size + 1;
for (unsigned long long i = 0; i < other.Size; ++i) {
Data[i] = other.Data[i];
}
Data[Size] = 0;
}
bool TString::operator < (const TString& other) const {
unsigned long long i = 0;
while (i < other.Size && i < Size) {
if (Data[i] < other.Data[i]) {
return true;
}
if (Data[i] > other.Data[i]) {
return false;
}
++i;
}
return Size < other.Size;
}
TString& TString::operator = (const TString& other) {
if (Data != nullptr) {
delete[] Data;
}
Data = new char[other.Size + 1];
Size = other.Size;
Capacity = Size + 1;
for (unsigned long long i = 0; i < other.Size; ++i) {
Data[i] = other.Data[i];
}
Data[Size] = 0;
return *this;
}
TString& TString::operator = (TString&& other) {
if (this == & other) {
return *this;
}
if (Data != nullptr) {
delete[] Data;
}
Data = other.Data;
Size = other.Size;
Capacity = other.Capacity;
other.Data = nullptr;
other.Size = 0;
other.Capacity = 0;
return *this;
}
TString::TString(TString&& other) {
Data = other.Data;
Size = other.Size;
Capacity = other.Capacity;
other.Data = nullptr;
other.Size = 0;
other.Capacity = 0;
}
TString& TString::operator = (const char *str) {
if (str == nullptr) {
Clear();
return *this;
}
unsigned long long size = 0;
while (str[size] != 0) {
++size;
}
if (Capacity > size) {
for (unsigned long long i = 0; i < size; ++i) {
Data[i] = str[i];
}
Data[size] = 0;
Size = size;
return *this;
}
if (Data != nullptr) {
delete[] Data;
}
Data = new char[size + 1];
Capacity = size + 1;
Size = size;
for (unsigned long long i = 0; i < size; ++i) {
Data[i] = str[i];
}
Data[size] = 0;
return *this;
}
char* TString::Buffer() {
return Data;
}
bool TString::operator == (const char *str) const {
unsigned long long i = 0;
if (str == nullptr) {
return false;
}
while (i < Size && str[i] != 0) {
if (str[i] != Data[i]) {
return false;
}
++i;
}
return Size == i;
}
bool TString::operator == (const char c) const {
if (Size == 1 && Data[0] == c) {
return true;
}
return false;
}
TString::operator char* () const {
char * res = new char[Size + 1];
for (unsigned long long i = 0; i < Size; ++i) {
res[i] = Data[i];
}
res[Size] = 0;
return res;
}
bool TString::operator == (const TString& other) const {
if (other.Size != Size) {
return false;
}
for (unsigned long long i = 0; i < Size; ++i) {
if (Data[i] != other.Data[i]) {
return false;
}
}
return true;
}
void TString::UpdateSize() {
if (Data == nullptr) {
Size = 0;
return;
}
Size = 0;
while (Data[Size] != 0) {
++Size;
}
return;
}
std::ostream& operator << (std::ostream& os, const TString& str) {
if (str.Data != nullptr) {
os << str.Data;
}
return os;
}
/*
// slow, but dynamic realization of operator >>
std::istream& operator >> (std::istream& is, TString& str) {
char c;
str.Size = 0;
bool got_input = false;
while (is.read(&c,1)) { // i thought is.read will be faster than is.get, but seems like they both are pretty slow compared to is >> char*
if (c == ' ' || c == '\n' || c == 0) {
if (got_input) {
break;
} else {
continue;
}
}
str.PushBack(c);
got_input = true;
}
return is;
}
*/
// static operator>> : if input size increases str.Capacity, segfault occurs
std::istream& operator >> (std::istream& is, TString& str) {
static const int BUFF_SIZE = 256;
if (str.Data == nullptr) {
str.Reallocate(BUFF_SIZE+1);
}
is >> str.Buffer();
str.UpdateSize();
return is;
}
const char* TString::CString() const {
return Data;
}
void TString::ToLower() {
if(Data == nullptr) {
return;
}
int diff = 'a' - 'A';
for (unsigned long long i = 0; i< Size; ++i) {
if (Data[i] >= 'A' && Data[i] <= 'Z') {
Data[i] += diff;
}
}
return;
}