forked from Mashpoe/cpp-string
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.hpp
396 lines (310 loc) · 9 KB
/
string.hpp
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
//
// string.hpp
// cpp-string
//
// Created by Mashpoe on 7/28/19.
//
#ifndef string_hpp
#define string_hpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <memory>
namespace mp {
class string {
private:
char* str;
size_t len;
// char* empty_str() {
// // return 1 null byte for empty "" string
// // this is so realloc will work
// return (char*)calloc(1, sizeof(char));
// }
// enum {
// INT_STRING_SIZE = ((sizeof(int)*CHAR_BIT - 1)*28/93 + 3)
// };
// private constructor to avoid copying
string(size_t length, void* value) : str((char*)value), len(length) {}
public:
// initialize str with 1 null byte ""
string() : str((char*)calloc(1, sizeof(char))), len(0) {}
string(decltype(nullptr)) : str((char*)calloc(1, sizeof(char))), len(0) {}
string(const char* value) {
if (value == nullptr) {
len = 0;
str = (char*)calloc(1, sizeof(char));
return;
}
len = strlen(value);
str = (char*)malloc(len + 1);
strcpy(str, value);
}
string(const char* value, size_t length) {
len = length;
str = (char*)malloc(len + 1);
strcpy(str, value);
}
// implement your own constructors for other types
template <typename T>
string(T value) {
std::string str_value = std::to_string(value);
len = str_value.length();
str = (char*)malloc(len + 1);
str = strcpy(str, str_value.c_str());
}
// an example of a custom constructor for a specific type
string(bool value) {
if (value) {
len = 4;
str = (char*)malloc(len + 1);
str = strcpy(str, "true");
} else {
len = 5;
str = (char*)malloc(len + 1);
str = strcpy(str, "false");
}
}
~string() {
if (str != nullptr) {
free(str);
}
}
inline size_t size() {
return len;
}
inline size_t length() {
return len;
}
// copy constructor
string(string const& other) {
str = (char*)malloc(other.len + 1);
strcpy(str, other.str);
len = other.len;
}
// copy assignment
string& operator = (string const& other) {
str = (char*)malloc(other.len + 1);
strcpy(str, other.str);
len = other.len;
return *this;
}
// move constructor
string(string&& other) noexcept {
// take ownership
str = other.str;
len = other.len;
// reset other
other.str = nullptr;
}
// move assignment
string& operator = (string&& other) noexcept {
if (this != &other) {
// take ownership
str = other.str;
len = other.len;
// reset other
other.str = nullptr;
}
return *this;
}
operator const char* () {
return str;
}
const char* c_str() const {
return str;
}
char& operator [] (int pos) {
return str[pos];
}
bool operator ! () {
/* empty strings have 1 null byte allocated because
a) you can use realloc on it
b) it prevents internal null checks
c) it can be cast to an empty string "" */
return str[0] == '\0';
}
explicit operator bool() {
return str[0] != '\0';
}
bool operator == (const char* value) {
return strcmp(str, value) == 0;
}
bool operator != (const char* value) {
return strcmp(str, value) != 0;
}
bool operator == (string value) {
return len == value.len && strcmp(str, value.str) == 0;
}
void insert(size_t pos, const string& value) {
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = len + value.len;
// don't use realloc becase we only want to copy the first section of the string
char* new_str = (char*)malloc(new_len + 1);
// copy the contents of both strings
memcpy(new_str, str, pos);
memcpy(&new_str[pos], value.str, value.len);
memcpy(&new_str[pos + value.len], &str[pos], len - pos);
// add null terminator
new_str[new_len] = '\0';
// free old string
free(str);
str = new_str;
len = new_len;
}
template <typename T>
void insert(size_t pos, T& value) {
insert(pos, string(value));
}
void append(string const& value) {
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = len + value.len;
str = (char*)realloc(str, new_len + 1);
// copy the other string
memcpy(&str[len], value.str, value.len);
// add null terminator
str[new_len] = '\0';
// update length
len = new_len;
}
void append(const char* value) {
size_t value_len = strlen(value);
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = len + value_len;
str = (char*)realloc(str, new_len + 1);
// copy the other string
memcpy(&str[len], value, value_len);
// add null terminator
str[new_len] = '\0';
// update length
len = new_len;
}
inline string& operator += (string const& value) {
append(value);
return *this;
}
inline string& operator += (const char* value) {
append(value);
return *this;
}
template <typename T>
string& operator += (T& value) {
append(string(value));
return *this;
}
void erase(size_t pos, size_t count) {
size_t new_len = len - count;
// move the right side of the erased portion
memmove(&str[pos], &str[pos + count], len - pos - count);
// add null terminator
str[new_len] = '\0';
len = new_len;
}
void remove(size_t pos) {
erase(pos, 1);
}
// concatenation overloads
string friend operator + (string const& left, string const& right);
// rvalue optimization (we can only optimize the left side)
string friend operator + (string&& left, string const& right);
// generic types
template <typename T>
string friend operator + (string const& left, T const& right);
template <typename T>
string friend operator + (T const& left, const string& right);
// rvalue optimization (we can only optimize the left side)
template <typename T>
string friend operator + (string&& left, T const& right);
// regular c string
string friend operator + (string const& left, const char* right);
string friend operator + (const char* left, string const& right);
// rvalue optimization (we can only optimize the left side)
string friend operator + (string&& left, const char* right);
};
string operator + (string const& left, string const& right) {
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right.len;
// call private constructor with void* to avoid copying
string s(new_len, malloc(new_len + 1));
// copy the contents of both strings
memcpy(s.str, left.str, left.len);
memcpy(&s.str[left.len], right.str, right.len);
// add null terminator
s.str[new_len] = '\0';
return s;
}
string operator + (string&& left, string const& right) {
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right.len ;
// call private constructor with void* to avoid copying
string s(new_len, realloc(left.str, new_len + 1));
// take ownership of left.str
left.str = nullptr;
// copy the contents of both strings
//memcpy(s.str, left.str, left.len);
memcpy(&s.str[left.len], right.str, right.len);
// add null terminator
s.str[new_len] = '\0';
// explicitly call destructor for left rvalue
return s;
}
template <typename T>
inline string operator + (string const& left, T const& right) {
std::cout << "reeeee1" << std::endl;
return left + string(right);
}
template <typename T>
inline string operator + (T const& left, string const& right) {
//std::cout << "reeeee2" << std::endl;
return string(left) + right;
}
template <typename T>
inline string operator + (string&& left, T const& right) {
//std::cout << "reeeee3" << std::endl;
return std::forward<string>(left) + string(right);
}
string operator + (string const& left, const char* right) {
size_t right_len = strlen(right);
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right_len;
// call private constructor with void* to avoid copying
string s(new_len, malloc(new_len + 1));
// copy the contents of both strings
memcpy(s.str, left.str, left.len);
memcpy(&s.str[left.len], right, right_len);
// add null terminator
s.str[new_len] = '\0';
return s;
}
string operator + (const char* left, string const& right) {
size_t left_len = strlen(left);
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left_len + right.len;
// call private constructor with void* to avoid copying
string s(new_len, malloc(new_len + 1));
// copy the contents of both strings
memcpy(s.str, left, left_len);
memcpy(&s.str[left_len], right.str, right.len);
// add null terminator
s.str[new_len] = '\0';
return s;
}
string operator + (string&& left, const char* right) {
size_t right_len = strlen(right);
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right_len;
// call private constructor with void* to avoid copying
string s(new_len, realloc(left.str, new_len + 1));
// take ownership of left.str
left.str = nullptr;
// copy the contents of the other string
memcpy(&s.str[left.len], right, right_len);
// add null terminator
s.str[new_len] = '\0';
return s;
}
// only works if using namespace mp, "string"_mp will be equal to string("string", 6)
inline string operator""_mp(const char* value, unsigned long size) {
return string(value, size);
}
};
#endif /* string_hpp */