-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathString.h
More file actions
349 lines (291 loc) · 5.97 KB
/
String.h
File metadata and controls
349 lines (291 loc) · 5.97 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
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
#ifndef STRING_H
#define STRING_H
#ifdef __cplusplus
class String
{
private:
unsigned int m_Length;
char* m_Buffer;
public:
String()
{
m_Length = 0;
m_Buffer = new char[0];
}
String(const char* cString)
{
if (cString)
{
unsigned int n = 0;
unsigned int i = 0;
while (cString[n] != '\0') n++;
m_Buffer = new char[n + 1];
for (i; i < n; i++) m_Buffer[i] = cString[i];
m_Buffer[n] = '\0';
m_Length = n;
}
else
{
m_Length = 0;
m_Buffer = new char[0];
}
}
String(const String& string)
{
int i = 0;
int len = string.Size();
m_Buffer = new char[len + 1];
for (i; i < len; i++) m_Buffer[i] = string[i];
m_Length = len;
m_Buffer[len] = '\0';
}
~String()
{
m_Length = 0;
delete[] m_Buffer;
}
int Size() const
{
return m_Length;
}
const char* c_str() const
{
return m_Buffer;
}
bool Equals(String& other) const
{
if (m_Length != other.Size()) return false;
unsigned int n = 0;
while ((n < m_Length) && (m_Buffer[n] == other[n])) n++;
return (n == m_Length);
}
bool Equals(const char* other) const
{
String string(other);
return this->Equals(string);
}
int Index(char c) const
{
unsigned int i = 0;
for (i; i < m_Length; i++)
if (m_Buffer[i] == c) return (int)i;
return -1;
}
String& Concat(const String& string)
{
int stringSize = string.Size();
unsigned int newLength = m_Length + stringSize;
int len = newLength + 1;
char* tempStr = new char[len];
for (unsigned int i = 0; i < newLength; i++)
{
tempStr[i] = m_Buffer[i];
}
for (int i = 0; i < stringSize; i++)
{
tempStr[newLength + i] = string[i];
}
delete[] m_Buffer;
m_Buffer = new char[len];
for (unsigned int i = 0; i < newLength; i++)
{
m_Buffer[i] = tempStr[i];
}
m_Length = newLength;
m_Buffer[newLength] = '\0';
delete[] tempStr;
tempStr = nullptr;
return *this;
}
String& Concat(const char* string)
{
String other(string);
return this->Concat(other);
}
String& ToUpperCase()
{
char diff = ('a' - 'A');
unsigned int i = 0;
for (i; i < m_Length; i++)
{
if ('a' <= m_Buffer[i] && m_Buffer[i] <= 'z')
{
m_Buffer[i] -= diff;
}
}
return *this;
}
String& ToLowerCase()
{
for (unsigned int i = 0; i < m_Length; i++)
{
if ('A' <= m_Buffer[i] && m_Buffer[i] <= 'Z')
{
m_Buffer[i] += ('a' - 'A');
}
}
return *this;
}
int Find(String& string)
{
int strSize = string.Size();
if (strSize == 0) return -1;
int len = strSize - 1;
int posSearch = 0;
unsigned int i = 0;
for (i; i < m_Length; ++i)
{
if (m_Buffer[i] == string.c_str()[posSearch])
{
++posSearch;
if (posSearch == strSize) return i - len;
}
else
{
i -= posSearch;
posSearch = 0;
}
}
return -1;
}
int Find(const char* cString)
{
String string(cString);
return this->Find(string);
}
String& Replace(String& target, String& replacement)
{
int startAt = this->Find(target);
if (startAt == -1)
{
return *this;
}
int replacementSize = replacement.Size();
int targetSize = target.Size();
int newLength = m_Length + replacementSize - targetSize;
int len = newLength + 1;
char* tempStr = new char[len];
int intervalIndex = 0;
if (replacementSize > 0)
{
for (int i = 0; i < newLength; i++)
{
if (i >= startAt && intervalIndex < replacementSize)
{
tempStr[i] = replacement[intervalIndex];
intervalIndex++;
}
else
{
tempStr[i] = m_Buffer[intervalIndex > 0 ? (i + targetSize - intervalIndex) : i];
}
}
}
else
{
for (int i = 0; i < newLength; i++)
{
if (i >= startAt && intervalIndex < targetSize)
{
tempStr[i] = m_Buffer[i + targetSize];
intervalIndex++;
}
else
{
tempStr[i] = m_Buffer[intervalIndex > 0 ? (i + targetSize) : i];
}
}
}
delete[] m_Buffer;
m_Length = newLength;
m_Buffer = new char[len];
for (unsigned int i = 0; i < m_Length; i++)
{
m_Buffer[i] = tempStr[i];
}
m_Buffer[newLength] = '\0';
delete[] tempStr;
tempStr = nullptr;
return *this;
}
String& Replace(String& target, const char* replacement)
{
String strReplacement(replacement);
return this->Replace(target, strReplacement);
}
String& Replace(const char* target, String& replacement)
{
String strTarget(target);
return this->Replace(strTarget, replacement);
}
String& Replace(const char* target, const char* replacement)
{
String strTarget(target);
String strReplacement(replacement);
return this->Replace(strTarget, strReplacement);
}
String& operator=(const String& string)
{
if (this == &string) return *this;
int len = string.Size();
delete[] m_Buffer;
m_Buffer = new char[len + 1];
int i = 0;
for (i; i < len; i++) m_Buffer[i] = string[i];
m_Length = len;
m_Buffer[len] = '\0';
return *this;
}
char operator[](unsigned int index) const
{
if (index >= m_Length) throw 1;
return m_Buffer[index];
}
char& operator[](unsigned int index)
{
if (index >= m_Length) throw 1;
return m_Buffer[index];
}
friend bool operator==(String& left, String& right)
{
return left.Equals(right);
}
friend bool operator==(String& left, const char* right)
{
return left.Equals(right);
}
friend bool operator==(const char* left, String& right)
{
return right.Equals(left);
}
friend bool operator!=(String& left, String& right)
{
return !left.Equals(right);
}
friend bool operator!=(String& left, const char* right)
{
return !left.Equals(right);
}
friend bool operator!=(const char* left, String& right)
{
return !right.Equals(left);
}
friend String operator+(const String& left, const String& right)
{
return String(left).Concat(right);
}
friend String operator+(const String& left, const char* right)
{
return String(left).Concat(right);
}
friend String operator+(const char* left, const String& right)
{
return String(left).Concat(right);
}
String& operator+=(const String& string)
{
return this->Concat(string);
}
};
#endif /* ifdef __cplusplus */
#endif /* STRING_H */