-
Notifications
You must be signed in to change notification settings - Fork 106
/
string.h
450 lines (365 loc) · 11.6 KB
/
string.h
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
#pragma once
#include <cinttypes>
#include <cstring>
#include <ostream>
#include <string>
#include <string_view>
#include "cista/containers/ptr.h"
#include "cista/decay.h"
namespace cista {
template <typename Ptr = char const*>
struct generic_string {
using msize_t = uint32_t;
static msize_t mstrlen(char const* s) {
return static_cast<msize_t>(std::strlen(s));
}
static constexpr struct owning_t {
} owning{};
static constexpr struct non_owning_t {
} non_owning{};
generic_string() {
std::memset(static_cast<void*>(this), 0, sizeof(*this));
h_.ptr_ = nullptr;
}
~generic_string() { reset(); }
generic_string(std::string_view s, owning_t) : generic_string() {
set_owning(s);
}
generic_string(std::string_view s, non_owning_t) : generic_string() {
set_non_owning(s);
}
generic_string(std::string const& s, owning_t) : generic_string() {
set_owning(s);
}
generic_string(std::string const& s, non_owning_t) : generic_string() {
set_non_owning(s);
}
generic_string(char const* s, owning_t) : generic_string() {
set_owning(s, mstrlen(s));
}
generic_string(char const* s, non_owning_t) : generic_string() {
set_non_owning(s);
}
char* begin() { return data(); }
char* end() { return data() + size(); }
char const* begin() const { return data(); }
char const* end() const { return data() + size(); }
bool is_short() const { return s_.is_short_; }
void reset() {
if (!h_.is_short_ && h_.ptr_ != nullptr && h_.self_allocated_) {
std::free(const_cast<char*>(data()));
}
std::memset(static_cast<void*>(this), 0, sizeof(*this));
h_.ptr_ = nullptr;
}
void set_owning(std::string const& s) {
set_owning(s.data(), static_cast<msize_t>(s.size()));
}
void set_owning(std::string_view s) {
set_owning(s.data(), static_cast<msize_t>(s.size()));
}
void set_owning(char const* str) { set_owning(str, mstrlen(str)); }
void set_owning(char const* str, msize_t const len) {
reset();
if (str == nullptr || len == 0) {
return;
}
s_.is_short_ = (len <= 15);
if (s_.is_short_) {
std::memcpy(s_.s_, str, len);
for (auto i = len; i < 15; ++i) {
s_.s_[i] = '\0';
}
} else {
h_.ptr_ = static_cast<char*>(std::malloc(len));
if (h_.ptr_ == nullptr) {
throw std::bad_alloc{};
}
h_.size_ = len;
h_.self_allocated_ = true;
std::memcpy(const_cast<char*>(data()), str, len);
}
}
void set_non_owning(std::string const& v) {
set_non_owning(v.data(), static_cast<msize_t>(v.size()));
}
void set_non_owning(std::string_view v) {
set_non_owning(v.data(), static_cast<msize_t>(v.size()));
}
void set_non_owning(char const* str) { set_non_owning(str, mstrlen(str)); }
void set_non_owning(char const* str, msize_t const len) {
reset();
if (str == nullptr || len == 0) {
return;
}
if (len <= 15) {
return set_owning(str, len);
}
h_.is_short_ = false;
h_.self_allocated_ = false;
h_.ptr_ = str;
h_.size_ = len;
}
void move_from(generic_string&& s) {
std::memcpy(static_cast<void*>(this), &s, sizeof(*this));
if constexpr (std::is_pointer_v<Ptr>) {
std::memset(static_cast<void*>(&s), 0, sizeof(*this));
} else {
if (!s.is_short()) {
h_.ptr_ = s.h_.ptr_;
s.h_.ptr_ = nullptr;
}
}
}
void copy_from(generic_string const& s) {
reset();
if (s.is_short()) {
std::memcpy(static_cast<void*>(this), &s, sizeof(s));
} else if (s.h_.self_allocated_) {
set_owning(s.data(), s.size());
} else {
set_non_owning(s.data(), s.size());
}
}
inline bool empty() const { return size() == 0U; }
std::string_view view() const { return {data(), size()}; }
std::string str() const { return {data(), size()}; }
operator std::string_view() const { return view(); }
char& operator[](size_t i) { return data()[i]; }
char const& operator[](size_t i) const { return data()[i]; }
friend std::ostream& operator<<(std::ostream& out, generic_string const& s) {
return out << s.view();
}
friend bool operator==(generic_string const& a, generic_string const& b) {
return a.view() == b.view();
}
friend bool operator!=(generic_string const& a, generic_string const& b) {
return a.view() != b.view();
}
friend bool operator<(generic_string const& a, generic_string const& b) {
return a.view() < b.view();
}
friend bool operator>(generic_string const& a, generic_string const& b) {
return a.view() > b.view();
}
friend bool operator<=(generic_string const& a, generic_string const& b) {
return a.view() <= b.view();
}
friend bool operator>=(generic_string const& a, generic_string const& b) {
return a.view() >= b.view();
}
friend bool operator==(generic_string const& a, std::string_view b) {
return a.view() == b;
}
friend bool operator!=(generic_string const& a, std::string_view b) {
return a.view() != b;
}
friend bool operator<(generic_string const& a, std::string_view b) {
return a.view() < b;
}
friend bool operator>(generic_string const& a, std::string_view b) {
return a.view() > b;
}
friend bool operator<=(generic_string const& a, std::string_view b) {
return a.view() <= b;
}
friend bool operator>=(generic_string const& a, std::string_view b) {
return a.view() >= b;
}
friend bool operator==(std::string_view a, generic_string const& b) {
return a == b.view();
}
friend bool operator!=(std::string_view a, generic_string const& b) {
return a != b.view();
}
friend bool operator<(std::string_view a, generic_string const& b) {
return a < b.view();
}
friend bool operator>(std::string_view a, generic_string const& b) {
return a > b.view();
}
friend bool operator<=(std::string_view a, generic_string const& b) {
return a <= b.view();
}
friend bool operator>=(std::string_view a, generic_string const& b) {
return a >= b.view();
}
friend bool operator==(generic_string const& a, char const* b) {
return a.view() == std::string_view{b};
}
friend bool operator!=(generic_string const& a, char const* b) {
return a.view() != std::string_view{b};
}
friend bool operator<(generic_string const& a, char const* b) {
return a.view() < std::string_view{b};
}
friend bool operator>(generic_string const& a, char const* b) {
return a.view() > std::string_view{b};
}
friend bool operator<=(generic_string const& a, char const* b) {
return a.view() <= std::string_view{b};
}
friend bool operator>=(generic_string const& a, char const* b) {
return a.view() >= std::string_view{b};
}
friend bool operator==(char const* a, generic_string const& b) {
return std::string_view{a} == b.view();
}
friend bool operator!=(char const* a, generic_string const& b) {
return std::string_view{a} != b.view();
}
friend bool operator<(char const* a, generic_string const& b) {
return std::string_view{a} < b.view();
}
friend bool operator>(char const* a, generic_string const& b) {
return std::string_view{a} > b.view();
}
friend bool operator<=(char const* a, generic_string const& b) {
return std::string_view{a} <= b.view();
}
friend bool operator>=(char const* a, generic_string const& b) {
return std::string_view{a} >= b.view();
}
char* data() {
if constexpr (std::is_pointer_v<Ptr>) {
return is_short() ? const_cast<char*>(s_.s_) : const_cast<char*>(h_.ptr_);
} else {
return is_short() ? const_cast<char*>(s_.s_)
: const_cast<char*>(h_.ptr_.get());
}
}
char const* data() const {
if constexpr (std::is_pointer_v<Ptr>) {
return is_short() ? s_.s_ : h_.ptr_;
} else {
return is_short() ? s_.s_ : h_.ptr_.get();
}
}
msize_t size() const {
if (is_short()) {
auto const pos = static_cast<char const*>(std::memchr(s_.s_, '\0', 15));
if (pos == nullptr) {
return 15;
} else {
return static_cast<msize_t>(pos - s_.s_);
}
} else {
return h_.size_;
}
}
struct heap {
bool is_short_{false};
bool self_allocated_{false};
uint8_t __fill_2__{0};
uint8_t __fill_3__{0};
uint32_t size_{0};
Ptr ptr_{nullptr};
};
struct stack {
bool is_short_{true};
char s_[15]{0};
};
union {
heap h_;
stack s_;
};
};
template <typename Ptr>
struct basic_string : public generic_string<Ptr> {
using base = generic_string<Ptr>;
using base::base;
using base::operator std::string_view;
friend std::ostream& operator<<(std::ostream& out, basic_string const& s) {
return out << s.view();
}
explicit operator std::string() const { return {base::data(), base::size()}; }
basic_string(std::string_view s) : base{s, base::owning} {}
basic_string(std::string const& s) : base{s, base::owning} {}
basic_string(char const* s) : base{s, base::owning} {}
basic_string(basic_string const& o) : base{} {
base::set_owning(o.data(), o.size());
}
basic_string(basic_string&& o) : base{} {
base::set_owning(o.data(), o.size());
}
basic_string& operator=(basic_string const& o) {
base::set_owning(o.data(), o.size());
return *this;
}
basic_string& operator=(basic_string&& o) {
base::move_from(std::move(o));
return *this;
}
basic_string& operator=(char const* s) {
base::set_owning(s);
return *this;
}
basic_string& operator=(std::string const& s) {
base::set_owning(s);
return *this;
}
basic_string& operator=(std::string_view s) {
base::set_owning(s);
return *this;
}
};
template <typename Ptr>
struct basic_string_view : public generic_string<Ptr> {
using base = generic_string<Ptr>;
using base::base;
using base::operator std::string_view;
friend std::ostream& operator<<(std::ostream& out,
basic_string_view const& s) {
return out << s.view();
}
basic_string_view(std::string_view s) : base{s, base::owning} {}
basic_string_view(std::string const& s) : base{s, base::owning} {}
basic_string_view(char const* s) : base{s, base::owning} {}
basic_string_view(basic_string_view const& o) : base{} {
base::set_non_owning(o.data(), o.size());
}
basic_string_view(basic_string_view&& o) : base{} {
base::set_non_owning(o.data(), o.size());
}
basic_string_view& operator=(basic_string_view const& o) {
base::set_non_owning(o.data(), o.size());
return *this;
}
basic_string_view& operator=(basic_string_view&& o) {
base::set_non_owning(o.data(), o.size());
return *this;
}
basic_string_view& operator=(char const* s) {
base::set_non_owning(s);
return *this;
}
basic_string_view& operator=(std::string_view s) {
base::set_non_owning(s);
return *this;
}
basic_string_view& operator=(std::string const& s) {
base::set_non_owning(s);
return *this;
}
};
template <typename Ptr>
struct is_string_helper : std::false_type {};
template <typename Ptr>
struct is_string_helper<generic_string<Ptr>> : std::true_type {};
template <typename Ptr>
struct is_string_helper<basic_string<Ptr>> : std::true_type {};
template <typename Ptr>
struct is_string_helper<basic_string_view<Ptr>> : std::true_type {};
template <class T>
constexpr bool is_string_v = is_string_helper<std::remove_cv_t<T>>::value;
namespace raw {
using generic_string = generic_string<ptr<char const>>;
using string = basic_string<ptr<char const>>;
using string_view = basic_string_view<ptr<char const>>;
} // namespace raw
namespace offset {
using generic_string = generic_string<ptr<char const>>;
using string = basic_string<ptr<char const>>;
using string_view = basic_string_view<ptr<char const>>;
} // namespace offset
} // namespace cista