-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.hpp
More file actions
550 lines (427 loc) · 12.3 KB
/
string.hpp
File metadata and controls
550 lines (427 loc) · 12.3 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
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
//
// 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:
// compile time constants
enum {
in_situ_capacity = 16,
in_situ_size = in_situ_capacity - 1
};
char* str;
size_t len;
union {
size_t capacity;
// last byte has flag bit, 1 = large string
// 0 = small string and null terminator
char data[in_situ_capacity];
} in_situ;
inline void init(size_t length) {
len = length;
if (length > in_situ_size) {
size_t capacity = length + 1;
in_situ.capacity = capacity;
in_situ.data[in_situ_size] = '\1';
str = (char*)malloc(capacity);
} else {
in_situ.data[in_situ_size] = '\0';
str = in_situ.data;
}
}
// change the string's capacity if the new capacity is greater
// does not change len
void resize(size_t new_capacity) {
// check capacity using flag bit
if (in_situ.data[in_situ_size] == '\1') {
if (new_capacity > in_situ.capacity) {
in_situ.capacity = new_capacity;
str = (char*)realloc(str, new_capacity);
}
} else {
// capacity <= in_situ_size
if (new_capacity > in_situ_size) {
// move data to the heap
char* new_str = (char*)malloc(new_capacity);
// copy str including null teminator
memcpy(new_str, str, len + 1);
str = new_str;
// change flag bit to large string
in_situ.data[in_situ_size] = '\1';
in_situ.capacity = new_capacity;
}
}
}
// private constructor to avoid copying
string(size_t length, void* value) : str((char*)value), len(length) {}
// creates a string with enough capacity for length
string(size_t length, size_t capacity) {
len = length;
if (capacity > in_situ_capacity) {
str = (char*)malloc(capacity);
in_situ.capacity = capacity;
// set flag bit
in_situ.data[in_situ_size] = '\1';
} else {
str = in_situ.data;
in_situ.data[in_situ_size] = '\0';
}
}
public:
// initialize str with 1 null byte ""
string() {
str = in_situ.data;
len = 0;
in_situ.data[0] = '\0';
in_situ.data[in_situ_size] = '\0';
}
string(decltype(nullptr)) {
str = in_situ.data;
len = 0;
in_situ.data[0] = '\0';
in_situ.data[in_situ_size] = '\0';
}
string(const char* value) {
if (value == nullptr) {
len = 0;
in_situ.data[0] = '\0';
in_situ.data[15] = '\0';
return;
}
init(strlen(value));
memcpy(str, value, len);
return;
}
string(const char* value, size_t length) {
init(length);
memcpy(str, value, len);
}
// implement your own constructors for other types
// must be explicit to avoid accidental conversions
template <typename T>
explicit string(T value) {
std::string str_value = std::to_string(value);
init(str_value.length());
memcpy(str, str_value.c_str(), len);
}
// an example of a custom constructor for a specific type
// we don't need to allocate because it will fit on the stack
explicit string(bool value) {
if (value) {
init(4);
memcpy(str, "true", len);
} else {
init(5);
memcpy(str, "false", len);
}
}
~string() {
// this can be manually set to 1 for move semantics
if (in_situ.data[in_situ_size] == '\1') {
free(str);
}
}
inline size_t size() {
return len;
}
inline size_t length() {
return len;
}
// copy constructor
string(string const& other) {
// check flag bit
if (other.in_situ.data[in_situ_size] == '\1') {
len = other.len;
in_situ.capacity = len + 1;
in_situ.data[in_situ_size] = '\1';
str = (char*)malloc(in_situ.capacity);
memcpy(str, other.str, in_situ.capacity);
} else {
// just copy the stack data
str = in_situ.data;
len = other.len;
memcpy(&in_situ.data, &other.in_situ.data, len + 1);
in_situ.data[in_situ_size] = '\0';
}
}
// copy assignment
string& operator = (string const& other) {
// check flag bit
if (other.in_situ.data[in_situ_size] == '\1') {
len = other.len;
in_situ.capacity = len + 1;
in_situ.data[in_situ_size] = '\1';
str = (char*)malloc(in_situ.capacity);
memcpy(str, other.str, in_situ.capacity);
} else {
// just copy the stack data
str = in_situ.data;
len = other.len;
memcpy(&in_situ.data, &other.in_situ.data, len + 1);
in_situ.data[in_situ_size] = '\0';
}
return *this;
}
// move constructor
string(string&& other) noexcept {
// check flag bit
if (other.in_situ.data[in_situ_size] == '\1') {
len = other.len;
in_situ.capacity = other.in_situ.capacity;
in_situ.data[in_situ_size] = '\1';
// take ownership
str = other.str;
// set other flag bit to 0 so destructor won't free
other.in_situ.data[in_situ_size] = '\0';
} else {
// just copy the stack data
str = in_situ.data;
len = other.len;
memcpy(&in_situ.data, &other.in_situ.data, len + 1);
in_situ.data[in_situ_size] = '\0';
}
}
// move assignment
string& operator = (string&& other) noexcept {
// unfortunate but necessary repitition of code
if (this != &other) {
// check flag bit
if (other.in_situ.data[in_situ_size] == '\1') {
len = other.len;
in_situ.capacity = other.in_situ.capacity;
in_situ.data[in_situ_size] = '\1';
// take ownership
str = other.str;
// set other flag bit to 0 so destructor won't free
other.in_situ.data[in_situ_size] = '\0';
} else {
// just copy the stack data
str = in_situ.data;
len = other.len;
memcpy(&in_situ.data, &other.in_situ.data, len + 1);
in_situ.data[in_situ_size] = '\0';
}
}
return *this;
}
const char* c_str() const {
return str;
}
bool empty() const {
return len == 0;
}
operator const char* () {
return str;
}
char& operator [] (int pos) {
return str[pos];
}
bool operator ! () {
return len == 0;
}
explicit operator bool() {
return len != 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;
resize(new_len + 1);
// move the right side of pos
memmove(&str[pos + value.len], &str[pos], len - pos);
memcpy(&str[pos], value.str, value.len);
// add null terminator
str[new_len] = '\0';
len = new_len;
}
void insert(size_t pos, const char* value) {
// allocate sum of both string lengths + 1 for null terminator
size_t value_len = strlen(value);
size_t new_len = len + value_len;
resize(new_len + 1);
// move the right side of pos
memmove(&str[pos + value_len], &str[pos], len - pos);
memcpy(&str[pos], value, value_len);
// add null terminator
str[new_len] = '\0';
len = new_len;
}
template <typename T>
void insert(size_t pos, T const& 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;
resize(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;
resize(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;
}
template <typename T>
void append(T const& value) {
append(string(value));
}
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 const& 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 to allocate an empty string with the given length
string s(new_len, 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) {
/* left is an rvalue, so we can just modify then move it
to increase performance */
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right.len;
// now resize left (won't change left.len)
left.resize(new_len + 1);
// copy the contents of the other string
memcpy(&left.str[left.len], right.str, right.len);
// update left.len
left.len = new_len;
// add null terminator
left.str[new_len] = '\0';
// transfer from left before it gets destroyed
return std::move(left);
}
template <typename T>
inline string operator + (string const& left, T const& right) {
return left + string(right);
}
template <typename T>
inline string operator + (T const& left, string const& right) {
return string(left) + right;
}
template <typename T>
inline string operator + (string&& left, T const& right) {
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 to avoid copying
string s(new_len, 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 to avoid copying
string s(new_len, 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) {
/* left is an rvalue, so we can just modify then move it
to increase performance */
size_t right_len = strlen(right);
// allocate sum of both string lengths + 1 for null terminator
size_t new_len = left.len + right_len;
// now resize left (won't change left.len)
left.resize(new_len + 1);
// copy the contents of the other string
memcpy(&left.str[left.len], right, right_len);
// update left.len
left.len = new_len;
// add null terminator
left.str[new_len] = '\0';
// transfer from left before it gets destroyed
return std::move(left);
}
// 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 */