-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBString.h
501 lines (417 loc) · 14.2 KB
/
BBString.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
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
#pragma once
#include "Utils/Logger.h"
#include "BBMemory.h"
#include "Utils/Utils.h"
namespace BB
{
namespace String_Specs
{
constexpr const size_t multipleValue = 8;
constexpr const size_t standardSize = 8;
}
template<typename CharT>
class Basic_String
{
public:
Basic_String(Allocator a_Allocator);
Basic_String(Allocator a_Allocator, size_t a_Size);
Basic_String(Allocator a_Allocator, const CharT* a_String);
Basic_String(Allocator a_Allocator, const CharT* a_String, size_t a_Size);
Basic_String(const Basic_String<CharT>& a_String);
Basic_String(Basic_String<CharT>&& a_String) noexcept;
~Basic_String();
Basic_String& operator=(const Basic_String<CharT>& a_Rhs);
Basic_String& operator=(Basic_String<CharT>&& a_Rhs) noexcept;
bool operator==(const Basic_String<CharT>& a_Rhs) const;
void append(const Basic_String<CharT>& a_String);
void append(const Basic_String<CharT>& a_String, size_t a_SubPos, size_t a_SubLength);
void append(const CharT* a_String);
void append(const CharT* a_String, size_t a_Size);
void insert(size_t a_Pos, const Basic_String<CharT>& a_String);
void insert(size_t a_Pos, const Basic_String<CharT>& a_String, size_t a_SubPos, size_t a_SubLength);
void insert(size_t a_Pos, const CharT* a_String);
void insert(size_t a_Pos, const CharT* a_String, size_t a_Size);
void push_back(const CharT a_Char);
void pop_back();
bool compare(const Basic_String<CharT>& a_String) const;
bool compare(const Basic_String<CharT>& a_String, size_t a_Size) const;
bool compare(size_t a_Pos, const Basic_String<CharT>& a_String, size_t a_Subpos, size_t a_Size) const;
bool compare(const CharT* a_String) const;
bool compare(const CharT* a_String, size_t a_Size) const;
bool compare(size_t a_Pos, const CharT* a_String) const;
bool compare(size_t a_Pos, const CharT* a_String, size_t a_Size) const;
void clear();
void reserve(const size_t a_Size);
void shrink_to_fit();
size_t size() const { return m_Size; }
size_t capacity() const { return m_Capacity; }
CharT* data() const { return m_String; }
const CharT* c_str() const { return m_String; }
private:
void grow(size_t a_MinCapacity = 1);
void reallocate(size_t a_NewCapacity);
Allocator m_Allocator;
CharT* m_String;
size_t m_Size = 0;
size_t m_Capacity = 64;
};
using String = Basic_String<char>;
using WString = Basic_String<wchar_t>;
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(Allocator a_Allocator)
: Basic_String(a_Allocator, String_Specs::standardSize)
{}
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(Allocator a_Allocator, size_t a_Size)
{
constexpr bool is_char = std::is_same_v<CharT, char> || std::is_same_v<CharT, wchar_t>;
BB_STATIC_ASSERT(is_char, "String is not a char or wchar");
m_Allocator = a_Allocator;
m_Capacity = Math::RoundUp(a_Size, String_Specs::multipleValue);
m_String = reinterpret_cast<CharT*>(BBalloc(m_Allocator, m_Capacity * sizeof(CharT)));
Memory::Set(m_String, NULL, m_Capacity);
}
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(Allocator a_Allocator, const CharT* a_String)
: Basic_String(a_Allocator, a_String, Memory::StrLength(a_String))
{}
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(Allocator a_Allocator, const CharT* a_String, size_t a_Size)
{
constexpr bool is_char = std::is_same_v<CharT, char> || std::is_same_v<CharT, wchar_t>;
BB_STATIC_ASSERT(is_char, "String is not a char or wchar");
m_Allocator = a_Allocator;
m_Capacity = Math::RoundUp(a_Size + 1, String_Specs::multipleValue);
m_Size = a_Size;
m_String = reinterpret_cast<CharT*>(BBalloc(m_Allocator, m_Capacity * sizeof(CharT)));
Memory::Copy(m_String, a_String, a_Size);
Memory::Set(m_String + a_Size, NULL, m_Capacity - a_Size);
}
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(const Basic_String<CharT>& a_String)
{
m_Allocator = a_String.m_Allocator;
m_Capacity = a_String.m_Capacity;
m_Size = a_String.m_Size;
m_String = reinterpret_cast<CharT*>(BBalloc(m_Allocator, m_Capacity * sizeof(CharT)));
Memory::Copy(m_String, a_String.m_String, m_Capacity);
}
template<typename CharT>
inline BB::Basic_String<CharT>::Basic_String(Basic_String<CharT>&& a_String) noexcept
{
m_Allocator = a_String.m_Allocator;
m_Capacity = a_String.m_Capacity;
m_Size = a_String.m_Size;
m_String = a_String.m_String;
a_String.m_Allocator.allocator = nullptr;
a_String.m_Allocator.func = nullptr;
a_String.m_Capacity = 0;
a_String.m_Size = 0;
a_String.m_String = nullptr;
}
template<typename CharT>
inline BB::Basic_String<CharT>::~Basic_String()
{
if (m_String != nullptr)
{
BBfree(m_Allocator, m_String);
m_String = nullptr;
}
}
template<typename CharT>
inline Basic_String<CharT>& BB::Basic_String<CharT>::operator=(const Basic_String<CharT>& a_Rhs)
{
this->~Basic_String();
m_Allocator = a_Rhs.m_Allocator;
m_Capacity = a_Rhs.m_Capacity;
m_Size = a_Rhs.m_Size;
m_String = reinterpret_cast<CharT*>(BBalloc(m_Allocator, m_Capacity * sizeof(CharT)));
Memory::Copy(m_String, a_Rhs.m_String, m_Capacity);
return *this;
}
template<typename CharT>
inline Basic_String<CharT>& BB::Basic_String<CharT>::operator=(Basic_String<CharT>&& a_Rhs) noexcept
{
this->~Basic_String();
m_Allocator = a_Rhs.m_Allocator;
m_Capacity = a_Rhs.m_Capacity;
m_Size = a_Rhs.m_Size;
m_String = a_Rhs.m_String;
a_Rhs.m_Allocator.allocator = nullptr;
a_Rhs.m_Allocator.func = nullptr;
a_Rhs.m_Capacity = 0;
a_Rhs.m_Size = 0;
a_Rhs.m_String = nullptr;
return *this;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::operator==(const Basic_String<CharT>& a_Rhs) const
{
if (Memory::Compare(m_String, a_Rhs.data(), m_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::append(const Basic_String<CharT>& a_String)
{
append(a_String.c_str(), a_String.size());
}
template<typename CharT>
inline void BB::Basic_String<CharT>::append(const Basic_String<CharT>& a_String, size_t a_SubPos, size_t a_SubLength)
{
append(a_String.c_str() + a_SubPos, a_SubLength);
}
template<typename CharT>
inline void BB::Basic_String<CharT>::append(const CharT* a_String)
{
append(a_String, Memory::StrLength(a_String));
}
template<typename CharT>
inline void BB::Basic_String<CharT>::append(const CharT* a_String, size_t a_Size)
{
if (m_Size + 1 + a_Size >= m_Capacity)
grow(a_Size + 1);
BB::Memory::Copy(m_String + m_Size, a_String, a_Size);
m_Size += a_Size;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::insert(size_t a_Pos, const Basic_String<CharT>& a_String)
{
insert(a_Pos, a_String.c_str(), a_String.size());
}
template<typename CharT>
inline void BB::Basic_String<CharT>::insert(size_t a_Pos, const Basic_String<CharT>& a_String, size_t a_SubPos, size_t a_SubLength)
{
insert(a_Pos, a_String.c_str() + a_SubPos, a_SubLength);
}
template<typename CharT>
inline void BB::Basic_String<CharT>::insert(size_t a_Pos, const CharT* a_String)
{
insert(a_Pos, a_String, Memory::StrLength(a_String));
}
template<typename CharT>
inline void BB::Basic_String<CharT>::insert(size_t a_Pos, const CharT* a_String, size_t a_Size)
{
BB_ASSERT(m_Size >= a_Pos, "String::Insert, trying to insert a string in a invalid position.");
if (m_Size + 1 + a_Size >= m_Capacity)
grow(a_Size + 1);
Memory::sMove(m_String + (a_Pos + a_Size), m_String + a_Pos, m_Size - a_Pos);
Memory::Copy(m_String + a_Pos, a_String, a_Size);
m_Size += a_Size;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::push_back(const CharT a_Char)
{
if (m_Size + 1 >= m_Capacity)
grow();
m_String[m_Size++] = a_Char;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::pop_back()
{
m_String[m_Size--] = NULL;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(const Basic_String<CharT>& a_String) const
{
if (Memory::Compare(m_String, a_String.data(), m_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(const Basic_String<CharT>& a_String, size_t a_Size) const
{
if (Memory::Compare(m_String, a_String.c_str(), a_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(size_t a_Pos, const Basic_String<CharT>& a_String, size_t a_Subpos, size_t a_Size) const
{
if (Memory::Compare(m_String + a_Pos, a_String.c_str() + a_Subpos, a_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(const CharT* a_String) const
{
return compare(a_String, Memory::StrLength(a_String));
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(const CharT* a_String, size_t a_Size) const
{
if (Memory::Compare(m_String, a_String, a_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(size_t a_Pos, const CharT* a_String) const
{
return compare(a_Pos, a_String, Memory::StrLength(a_String));
}
template<typename CharT>
inline bool BB::Basic_String<CharT>::compare(size_t a_Pos, const CharT* a_String, size_t a_Size) const
{
if (Memory::Compare(m_String + a_Pos, a_String, a_Size) == 0)
return true;
return false;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::clear()
{
Memory::Set(m_String, NULL, m_Size);
m_Size = 0;
}
template<typename CharT>
inline void BB::Basic_String<CharT>::reserve(const size_t a_Size)
{
if (a_Size > m_Capacity)
{
size_t t_ModifiedCapacity = Math::RoundUp(a_Size + 1, String_Specs::multipleValue);
reallocate(t_ModifiedCapacity);
}
}
template<typename CharT>
inline void BB::Basic_String<CharT>::shrink_to_fit()
{
size_t t_ModifiedCapacity = Math::RoundUp(m_Size + 1, String_Specs::multipleValue);
if (t_ModifiedCapacity < m_Capacity)
{
reallocate(t_ModifiedCapacity);
}
}
template<typename CharT>
inline void BB::Basic_String<CharT>::grow(size_t a_MinCapacity)
{
size_t t_ModifiedCapacity = m_Capacity * 2;
if (a_MinCapacity > t_ModifiedCapacity)
t_ModifiedCapacity = Math::RoundUp(a_MinCapacity, String_Specs::multipleValue);
reallocate(t_ModifiedCapacity);
}
template<typename CharT>
inline void BB::Basic_String<CharT>::reallocate(size_t a_NewCapacity)
{
CharT* t_NewString = reinterpret_cast<CharT*>(BBalloc(m_Allocator, a_NewCapacity * sizeof(CharT)));
Memory::Copy(t_NewString, m_String, m_Size);
BBfree(m_Allocator, m_String);
m_String = t_NewString;
m_Capacity = a_NewCapacity;
}
template<typename CharT, size_t stringSize>
class Stack_String
{
public:
Stack_String()
{
Memory::Set(m_String, 0, stringSize);
};
Stack_String(const CharT* a_String) : Stack_String(a_String, Memory::StrLength(a_String)) {};
Stack_String(const CharT* a_String, size_t a_Size)
{
BB_ASSERT(a_Size < sizeof(m_String), "Stack string overflow");
Memory::Set(m_String, 0, sizeof(m_String));
Memory::Copy(m_String, a_String, a_Size);
m_Size = a_Size;
};
Stack_String(const Stack_String<CharT, stringSize>& a_String)
{
Memory::Copy(m_String, a_String, sizeof(m_String));
m_Size = a_String.size;
};
Stack_String(Stack_String<CharT, stringSize>&& a_String) noexcept
{
Memory::Copy(m_String, a_String, sizeof(m_String));
m_Size = a_String.size();
Memory::Set(a_String.m_String, 0, stringSize);
a_String.m_Size = 0
};
~Stack_String()
{
clear();
};
Stack_String& operator=(const Stack_String<CharT, stringSize>& a_Rhs)
{
this->~Stack_String();
Memory::Copy(m_String, a_String, sizeof(m_String));
m_Size = a_String.size();
};
Stack_String& operator=(Stack_String<CharT, stringSize>&& a_Rhs) noexcept
{
this->~Stack_String();
Memory::Copy(m_String, a_String, sizeof(m_String));
m_Size = a_String.size();
Memory::Set(a_Rhs.m_String, 0, stringSize);
a_Rhs.m_Size = 0
};
bool operator==(const Stack_String<CharT, stringSize>& a_Rhs) const
{
if (Memory::Compare(m_String, a_Rhs.data(), sizeof(m_String)) == 0)
return true;
return false;
};
void append(const Stack_String<CharT, stringSize>& a_String)
{
append(a_String.c_str(), a_String.size());
};
void append(const Stack_String<CharT, stringSize>& a_String, size_t a_SubPos, size_t a_SubLength)
{
append(a_String.c_str() + a_SubPos, a_SubLength);
};
void append(const CharT* a_String)
{
append(a_String, Memory::StrLength(a_String));
};
void append(const CharT* a_String, size_t a_Size)
{
BB_ASSERT(m_Size + a_Size < sizeof(m_String), "Stack string overflow");
BB::Memory::Copy(m_String + m_Size, a_String, a_Size);
m_Size += a_Size;
};
void insert(size_t a_Pos, const Stack_String<CharT, stringSize>& a_String)
{
insert(a_Pos, a_String.c_str(), a_String.size());
};
void insert(size_t a_Pos, const Stack_String<CharT, stringSize>& a_String, size_t a_SubPos, size_t a_SubLength)
{
insert(a_Pos, a_String.c_str() + a_SubPos, a_SubLength);
}
void insert(size_t a_Pos, const CharT* a_String)
{
insert(a_Pos, a_String, Memory::StrLength(a_String));
};
void insert(size_t a_Pos, const CharT* a_String, size_t a_Size)
{
BB_ASSERT(m_Size >= a_Pos, "Trying to insert a string in a invalid position.");
BB_ASSERT(m_Size + a_Size < sizeof(m_String), "Stack string overflow");
Memory::sMove(m_String + (a_Pos + a_Size), m_String + a_Pos, m_Size - a_Pos);
Memory::Copy(m_String + a_Pos, a_String, a_Size);
m_Size += a_Size;
};
void push_back(const CharT a_Char)
{
m_String[m_Size++] = a_Char;
BB_ASSERT(m_Size < sizeof(m_String), "Stack string overflow");
};
void pop_back(uint32_t a_Count)
{
m_Size -= a_Count;
memset(Pointer::Add(m_String, m_Size), NULL, a_Count);
};
void clear()
{
Memory::Set(m_String, 0, stringSize);
m_Size = 0;
}
size_t size() const { return m_Size; }
size_t capacity() const { return stringSize; }
CharT* data() { return m_String; }
const CharT* c_str() const { return m_String; }
private:
CharT m_String[stringSize + 1];
size_t m_Size = 0;
};
template<size_t stringSize>
using StackString = Stack_String<char, stringSize>;
template<size_t stringSize>
using StackWString = Stack_String<wchar_t, stringSize>;
}