forked from Ares-Developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathWstring.h
607 lines (488 loc) · 11.5 KB
/
Wstring.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#pragma once
#include <cstring>
#include <Memory.h>
struct CharTrait
{
size_t Length(const char* pString) const
{
return strlen(pString);
}
size_t Find(const char* _Str, const char* _Control)
{
return strcspn(_Str, _Control);
}
const char* Find(const char* _String, char _Ch)
{
return strchr(_String, _Ch);
}
int ToInteger(const char* _String)
{
return atoi(_String);
}
char* CopyN(char* _Destination, const char* _Source, size_t _Count)
{
return strncpy(_Destination, _Source, _Count);
}
char* Copy(char* _Dest, const char* _Source)
{
return strcpy(_Dest, _Source);
}
char* ConcatN(char* _Dest, const char* _Source, size_t _Count)
{
return strncat(_Dest, _Source, _Count);
}
char* Concat(char* _Destination, const char* _Source, size_t _Count)
{
return strcat(_Destination, _Source);
}
int Compare(const char* _Str1, const char* _Str2)
{
return strcmp(_Str1, _Str2);
}
static int(__cdecl* Format)(char* Buffer, const char* Format, ...);
static const char* IntegerFormatString;
static const char* LeadingZeroIntegerFormatString;
static char Null;
private:
char Dummy;
};
__declspec(selectany) int(__cdecl* CharTrait::Format)(char*, const char*, ...) = sprintf;
__declspec(selectany) const char* CharTrait::IntegerFormatString = "%d";
__declspec(selectany) const char* CharTrait::LeadingZeroIntegerFormatString = "%%0%dd";
__declspec(selectany) char CharTrait::Null = '\0';
struct WCharTrait
{
size_t Length(const wchar_t* pString) const
{
return wcslen(pString);
}
size_t Find(const wchar_t* _Str, const wchar_t* _Control)
{
return wcscspn(_Str, _Control);
}
const wchar_t* Find(const wchar_t* _String, wchar_t _Ch)
{
return wcschr(_String, _Ch);
}
int ToInteger(const wchar_t* _String)
{
return _wtoi(_String);
}
wchar_t* CopyN(wchar_t* _Destination, const wchar_t* _Source, size_t _Count)
{
return wcsncpy(_Destination, _Source, _Count);
}
wchar_t* Copy(wchar_t* _Dest, const wchar_t* _Source)
{
return wcscpy(_Dest, _Source);
}
wchar_t* ConcatN(wchar_t* _Dest, const wchar_t* _Source, size_t _Count)
{
return wcsncat(_Dest, _Source, _Count);
}
wchar_t* Concat(wchar_t* _Destination, const wchar_t* _Source, size_t _Count)
{
return wcscat(_Destination, _Source);
}
int Compare(const wchar_t* _Str1, const wchar_t* _Str2)
{
return wcscmp(_Str1, _Str2);
}
static int(__cdecl* Format)(wchar_t* Buffer, const wchar_t* Format, ...);
static const wchar_t* IntegerFormatString;
static const wchar_t* LeadingZeroIntegerFormatString;
static wchar_t Null;
private:
char Dummy;
};
__declspec(selectany) int(__cdecl* WCharTrait::Format)(wchar_t*, const wchar_t*, ...) = swprintf;
__declspec(selectany) const wchar_t* WCharTrait::IntegerFormatString = L"%d";
__declspec(selectany) const wchar_t* WCharTrait::LeadingZeroIntegerFormatString = L"%%0%dd";
__declspec(selectany) wchar_t WCharTrait::Null = L'\0';
template<typename TChar, typename TCharTraits>
class Wstring_base
{
private:
using My_Type = Wstring_base<TChar, TCharTraits>;
public:
static const My_Type EmptyString;
explicit Wstring_base<TChar, TCharTraits>() noexcept { };
explicit Wstring_base<TChar, TCharTraits>(const TChar* pString) noexcept : Wstring_base<TChar, TCharTraits> { }
{
if (pString)
{
Buffer = AllocateBuffer(TCharTraits().Length(pString) + 1);
Buffer[0] = TCharTraits::Null;
TCharTraits().Copy(Buffer, pString);
}
else
{
Buffer = AllocateBuffer(1);
Buffer[0] = TCharTraits::Null;
}
}
explicit Wstring_base<TChar, TCharTraits>(const My_Type& another) noexcept : Wstring_base<TChar, TCharTraits> {}
{
if (another.Buffer)
{
Buffer = AllocateBuffer(another.Length() + 1);
TCharTraits().Copy(Buffer, another.Buffer);
}
}
My_Type& operator=(const TChar* pString)
{
ReleaseBuffer();
if (pString)
{
Buffer = AllocateBuffer(TCharTraits().Length(pString) + 1);
Buffer[0] = TCharTraits::Null;
TCharTraits().Copy(Buffer, pString);
}
else
{
Buffer = AllocateBuffer(1);
Buffer[0] = TCharTraits::Null;
}
return *this;
}
My_Type& operator=(const My_Type& another)
{
if (Buffer || another.Buffer)
{
if (!Buffer || !another.Buffer || *this != another)
{
ReleaseBuffer();
if (!another.Buffer)
const_cast<My_Type&>(another) = EmptyString;
Buffer = AllocateBuffer(another.Length() + 1);
TCharTraits().Copy(Buffer, another.Buffer);
}
}
return *this;
}
~Wstring_base() noexcept
{
ReleaseBuffer();
}
bool operator==(const TChar* pString) const
{
if (!Buffer && !pString)
return true;
if (Buffer && pString)
return TCharTraits().Compare(Buffer, pString) == 0;
return 0;
}
bool operator==(const My_Type& another) const
{
return *this == another.Buffer;
}
bool operator!=(const TChar* pString) const
{
return *this != pString;
}
bool operator!=(const My_Type& another) const
{
return *this == another;
}
My_Type& operator+=(const TChar* pString)
{
Concat(pString);
return *this;
}
My_Type& operator+=(const My_Type& another)
{
Concat(another);
return *this;
}
const My_Type operator+(const TChar* pString)
{
My_Type ret = *this;
ret += pString;
return ret;
}
const My_Type operator+(const My_Type& another)
{
My_Type ret = *this;
ret += another;
return ret;
}
TChar operator[](size_t nIdx) const
{
return GetChar(nIdx);
}
bool Concat(const TChar* pString)
{
if (!pString)
return true;
if (Buffer)
{
auto pOldBuffer = Buffer;
Buffer = AllocateBuffer(Length() + TCharTraits().Length(pString) + 1);
TCharTraits().Copy(Buffer, pOldBuffer);
YRMemory::Deallocate(pOldBuffer);
TCharTraits().Concat(Buffer, pString);
}
else
{
Buffer = AllocateBuffer(TCharTraits().Length(pString) + 1);
Buffer[0] = TCharTraits::Null;
TCharTraits().Concat(Buffer, pString);
}
return true;
}
bool Concat(size_t nSize, const TChar* pString)
{
if (!pString || nSize == 0)
return 1;
if (Buffer)
{
auto pOldBuffer = Buffer;
Buffer = AllocateBuffer(Length() + nSize + 1);
TCharTraits().Copy(Buffer, pOldBuffer);
YRMemory::Deallocate(pOldBuffer);
TCharTraits().ConcatN(Buffer, pString, nSize);
}
else
{
Buffer = AllocateBuffer(nSize + 1);
Buffer[0] = TCharTraits::Null;
TCharTraits().ConcatN(Buffer, pString, nSize);
}
return true;
}
bool Concat(const My_Type& another)
{
if (!another.Buffer || another.Length() == 0)
return true;
return Concat(another.Length(), another.Buffer);
}
bool TrimRange(size_t nStart, size_t nLength)
{
if (nStart + nLength > Length())
nStart = Length() - nLength;
if (nStart < 0)
{
nLength += nStart;
nStart = 0;
}
if (nLength <= 0)
return false;
auto pBuffer = AllocateBuffer(Length() - nLength + 1);
Buffer[nStart] = Buffer[nStart - 1 + nLength] = TCharTraits::Null;
TCharTraits().Copy(pBuffer, Buffer);
TCharTraits().Concat(pBuffer, Buffer + nStart + nLength);
YRMemory::Deallocate(Buffer);
Buffer = pBuffer;
return true;
}
bool RemoveCharacter(TChar ch)
{
if (!Buffer)
return false;
bool bRemoved = false;
if (auto p = TCharTraits().Find(Buffer, ch))
{
for (size_t len = Length(); p; p = TCharTraits().Find(Buffer, ch))
{
memcpy(p, p + 1, Buffer - p + len-- - 1);
Buffer[len] = TCharTraits::Null;
}
auto pBuffer = AllocateBuffer(Length() + 1);
TCharTraits().Copy(pBuffer, Buffer);
YRMemory::Deallocate(Buffer);
bRemoved = true;
}
return bRemoved;
}
void RemoveSpaces()
{
RemoveCharacter(TCharTraits::Space);
RemoveCharacter(TCharTraits::Tab);
}
void TrimToFirstDifference(const My_Type& another)
{
size_t i;
for (i = 0; ; ++i)
{
size_t len = Buffer ? Length() : 0;
if (i >= len)
break;
auto p = Buffer;
if (!Buffer)
p = EmptyString.Buffer;
if (!TCharTraits().Find(p, Buffer[i]))
break;
}
if (i > 0)
TrimRange(0, i);
}
void ReleaseBuffer()
{
if (Buffer)
{
YRMemory::Deallocate(Buffer);
Buffer = nullptr;
}
}
void Reverse(size_t nLength)
{
ReleaseBuffer();
if (nLength >= 0)
{
Buffer = AllocateBuffer(nLength);
memset(Buffer, 0, nLength * sizeof(TChar));
}
}
void MergeStrings(const TChar* pString, size_t nCount)
{
TCharTraits().CopyN(pString, Buffer, nCount);
if (Length() < nCount)
memset(pString + Length(), TCharTraits::Space, nCount - Length());
pString[nCount] = TCharTraits::Null;
}
const TChar* const PeekBuffer() const
{
return Buffer ? Buffer : EmptyString.Buffer;
}
const TChar GetChar(size_t nIdx) const
{
return nIdx >= Length() ? 0 : Buffer[nIdx];
}
const size_t GetLength() const
{
return Buffer ? Length() : 0;
}
bool Set(const TChar* pString)
{
*this = pString;
}
bool SetAt(TChar ch, size_t nIdx)
{
if (nIdx >= Length())
return false;
Buffer[nIdx] = ch;
return true;
}
bool Replace(size_t nSize, const TChar* pString)
{
ReleaseBuffer();
Buffer = AllocateBuffer(nSize + 1);
Buffer[0] = TCharTraits::Null;
if (pString)
TCharTraits().CopyN(Buffer, pString, nSize);
Buffer[nSize] = TCharTraits::Null;
return true;
}
void ToLower()
{
if (Buffer)
{
for (size_t i = 0; i < Length(); ++i)
TCharTraits().ToLower(Buffer[i]);
}
}
My_Type& ToLower(My_Type& ret)
{
ret = *this;
ret.ToLower();
return ret;
}
void ToUpper()
{
if (Buffer)
{
for (size_t i = 0; i < Length(); ++i)
TCharTraits().ToUpper(Buffer[i]);
}
}
My_Type& ToUpper(My_Type& ret)
{
ret = *this;
ret.ToUpper();
return ret;
}
bool Resize(size_t nLength)
{
auto pBuffer = AllocateBuffer(nLength + 1);
pBuffer[0] = TCharTraits::Null;
TCharTraits().CopyN(pBuffer, PeekBuffer(), nLength);
pBuffer[nLength] = TCharTraits::Null;
ReleaseBuffer();
// Stupid WestWood wrote:
// *this = pBuffer;
// YRMemory::Deallocate(pBuffer);
// I guess what we need to do is just assign this buffer
Buffer = pBuffer;
return true;
}
bool ResizeToChar(TChar ch)
{
if (!Buffer)
return false;
if (auto p = TCharTraits().Find(ch))
return Resize(p - Buffer);
return false;
}
// To be implemented
int Token(size_t nIdx, TChar* cDelim, My_Type& ret);
// To be implemented
int NextLine(unsigned nIdx, My_Type& ret);
int AsInteger() const
{
return TCharTraits().ToInteger(Buffer);
}
operator int() const
{
return AsInteger();
}
void FromInteger(int nValue)
{
TChar buffer[0x10];
TCharTraits::Format(buffer, TCharTraits::IntegerFormatString, nValue);
*this = buffer;
}
static My_Type StringFromInteger(int nValue)
{
My_Type ret;
ret.FromInteger(nValue);
return ret;
}
void FromInteger(int nValue, size_t nLeadingZero)
{
TChar buffer[0x10];
TCharTraits::Format(buffer, TCharTraits::LeadingZeroIntegerFormatString, nLeadingZero);
TCharTraits::Format(buffer, buffer, nValue);
*this = buffer;
}
static My_Type StringFromInteger(int nValue, size_t nLeadingZero)
{
My_Type ret;
ret.FromInteger(nValue, nLeadingZero);
return ret;
}
bool Contains(const TChar* pString) const
{
if (pString && Buffer)
return TCharTraits().Find(Buffer, pString) < Length();
return false;
}
bool Contains(const My_Type& another) const
{
return Contains(another.PeekBuffer());
}
private:
TChar* AllocateBuffer(size_t nLen)
{
return (TChar*)YRMemory::Allocate(sizeof(TChar) * nLen);
}
size_t Length() const
{
return TCharTraits().Length(Buffer);
}
public:
TChar* Buffer { nullptr };
};
template<typename TChar, typename TCharTrait>
__declspec(selectany) const Wstring_base<TChar, TCharTrait> Wstring_base<TChar, TCharTrait>::EmptyString;
using Wstring = Wstring_base<char, CharTrait>;
using WideWstring = Wstring_base<wchar_t, WCharTrait>;