-
Notifications
You must be signed in to change notification settings - Fork 77
/
TFastString.h
516 lines (434 loc) · 14.5 KB
/
TFastString.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
/*****************************************************************************/
/* TFastString.h Copyright (c) Ladislav Zezula 2017 */
/*---------------------------------------------------------------------------*/
/* Helper class for growable string buffers */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 27.11.17 1.00 Lad The first version of TFastString.h */
/*****************************************************************************/
#ifndef __TFASTSTRING_H__
#define __TFASTSTRING_H__
//-----------------------------------------------------------------------------
// All allocations are done against global process heap handle
extern HANDLE g_hHeap;
//-----------------------------------------------------------------------------
// Useful functions
template <typename XCHAR>
bool IsSpace(XCHAR ch)
{
return (0 < ch && ch <= 0x20);
}
template <typename XCHAR>
FORCEINLINE XCHAR * SkipSpaces(const XCHAR * sz)
{
while(0 < *sz && *sz <= 0x20)
sz++;
return (XCHAR *)sz;
}
template <typename XCHAR>
FORCEINLINE XCHAR * SkipNonSpaces(const XCHAR * sz)
{
while(sz[0] != 0 && sz[0] > 0x20)
sz++;
return (XCHAR *)sz;
}
template <typename XCHAR>
FORCEINLINE XCHAR * SkipSpacesAndCommas(const XCHAR * sz)
{
while((0 < *sz && *sz <= 0x20) || *sz == ',')
sz++;
return (XCHAR *)sz;
}
FORCEINLINE size_t StringLength(const char * szStr)
{
return (szStr != NULL) ? strlen(szStr) : 0;
}
FORCEINLINE size_t StringLength(const wchar_t * szStr)
{
return (szStr != NULL) ? wcslen(szStr) : 0;
}
template <typename XCHAR>
static DWORD HashString(const XCHAR * szString)
{
DWORD dwHash = 0x7EEEEEE7;
// Sanity check
assert(szString != NULL);
// Add the string
while(szString[0] != 0)
{
dwHash = (dwHash >> 24) + (dwHash << 5) + dwHash + szString[0];
szString++;
}
return dwHash;
}
//-----------------------------------------------------------------------------
// Common implementation of the fast string
template <typename XCHAR>
class TFastString
{
public:
typedef const XCHAR * LPCXSTR;
typedef XCHAR * LPXSTR;
TFastString()
{
Init();
}
virtual ~TFastString()
{
if(m_pBuffer != m_StaticBuff)
HeapFree(g_hHeap, 0, m_pBuffer);
m_pBuffer = m_StaticBuff;
}
void InitFromLPCWSTR(LPCWSTR szStringW)
{
// Check for NULL and empty strings
if(szStringW && szStringW[0])
{
// Calculate length of the string
size_t nWideLength = wcslen(szStringW);
// Converting LPWSTR -> LPSTR?
if(m_nCharSize == sizeof(CHAR))
{
size_t nAnsiLength = nWideLength;
size_t nTryCount = 0;
__TryAgain:
// Make sure that the string is reset
Reset();
// Make sure there is enough space for the string
if(EnsureSpace(nAnsiLength))
{
// Convert the WIDE string to ANSI string
nAnsiLength = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, szStringW, (int)nWideLength, (LPSTR)m_pBuffer, (int)nAnsiLength, NULL, NULL);
if(nAnsiLength != 0)
{
m_pBufferPtr = m_pBuffer + nAnsiLength;
}
// If failed, we check whether this is a buffer overflow
else if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
nAnsiLength = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, szStringW, (int)nWideLength, NULL, 0, NULL, NULL);
if(nTryCount++ == 0)
{
goto __TryAgain;
}
}
}
}
else
{
if(EnsureSpace(nWideLength))
{
memcpy(m_pBuffer, szStringW, nWideLength * sizeof(XCHAR));
m_pBufferPtr = m_pBuffer + nWideLength;
}
}
}
else
{
m_pBufferPtr = m_pBuffer;
}
// Make sure we're zero terminated
m_pBufferPtr[0] = 0;
}
void InitFromLPCSTR(LPCSTR szStringA)
{
// Check for NULL and empty strings
if(szStringA && szStringA[0])
{
// Calculate length of the string
size_t nAnsiLength = strlen(szStringA);
// Converting LPSTR -> LPWSTR?
if(m_nCharSize == sizeof(WCHAR))
{
size_t nWideLength = nAnsiLength;
size_t nTryCount = 0;
__TryAgain:
// Make sure that the string is reset
Reset();
// Make sure there is enough space for the string
if(EnsureSpace(nWideLength))
{
// Convert the WIDE string to ANSI string
nWideLength = MultiByteToWideChar(CP_ACP, 0, szStringA, (int)nAnsiLength, (LPWSTR)m_pBuffer, (int)nWideLength);
if(nWideLength != 0)
{
m_pBufferPtr = m_pBuffer + nWideLength;
}
// If failed, we check whether this is a buffer overflow
else if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
nAnsiLength = MultiByteToWideChar(CP_ACP, 0, szStringA, (int)nAnsiLength, NULL, 0);
if(nTryCount++ == 0)
{
goto __TryAgain;
}
}
}
}
else
{
if(EnsureSpace(nAnsiLength))
{
memcpy(m_pBuffer, szStringA, nAnsiLength * sizeof(XCHAR));
m_pBufferPtr = m_pBuffer + nAnsiLength;
}
}
}
else
{
m_pBufferPtr = m_pBuffer;
}
// Make sure we're zero terminated
m_pBufferPtr[0] = 0;
}
bool AppendChar(XCHAR chOneChar)
{
// Ensure that there is space for one character
if(!EnsureSpace(1))
return false;
// Insert the character
*m_pBufferPtr++ = chOneChar;
return true;
}
bool AppendChars(XCHAR chOneChar, size_t nCount)
{
// Ensure that there is space for one character
if(!EnsureSpace(nCount))
return false;
// Insert the character
for(size_t i = 0; i < nCount; i++)
m_pBufferPtr[i] = chOneChar;
m_pBufferPtr += nCount;
return true;
}
bool AppendString(LPCXSTR szString, size_t nLength)
{
// Ensure that there is enough space in the buffers
if(!EnsureSpace(nLength))
return false;
memcpy(m_pBufferPtr, szString, nLength * sizeof(XCHAR));
m_pBufferPtr += nLength;
return true;
}
bool AppendString(LPCXSTR szString)
{
return AppendString(szString, StringLength(szString));
}
LPCXSTR PutEosAt(LPCXSTR szPosition)
{
LPXSTR szBufferPos = (LPXSTR)(szPosition);
if(m_pBuffer <= szBufferPos && szBufferPos <= m_pBufferPtr)
szBufferPos[0] = 0;
return m_pBuffer;
}
void TrimLeftAndRight(bool(*PfnIsTrimChar)(XCHAR ch))
{
LPXSTR pTrimRight = m_pBufferPtr;
LPXSTR pTrimLeft = m_pBuffer;
// Trim left
while(pTrimLeft < pTrimRight && PfnIsTrimChar(pTrimLeft[0]))
pTrimLeft++;
// Trim right
while(pTrimRight > pTrimLeft && PfnIsTrimChar(pTrimRight[-1]))
pTrimRight--;
// Perform memmove, if we trimmed something from the left
if(pTrimLeft > m_pBuffer)
memmove(m_pBuffer, pTrimLeft, (pTrimRight - pTrimLeft) * sizeof(TCHAR));
m_pBuffer[pTrimRight - pTrimLeft] = 0;
m_pBufferPtr = m_pBuffer + (pTrimRight - pTrimLeft);
}
void TrimSpaces()
{
TrimLeftAndRight(IsSpace);
}
bool CutLastChar()
{
if(m_pBufferPtr > m_pBuffer)
{
m_pBufferPtr--;
return true;
}
return false;
}
// Initializes the string a-new
void Init()
{
// Set the string to the static array
m_pBuffer = m_StaticBuff;
m_pBufferPtr = m_pBuffer;
m_pBufferEnd = m_pBuffer + _countof(m_StaticBuff) - 1;
m_nCharSize = sizeof(XCHAR);
// Terminate the string with zero
m_StaticBuff[0] = 0;
}
// Sets the string to have zero length. Also makes it zero terminated.
void Reset()
{
m_pBufferPtr = m_pBuffer;
m_pBuffer[0] = 0;
}
// Returns the buffer as string. Ensures that it's zero terminated.
LPCXSTR GetStr()
{
m_pBufferPtr[0] = 0;
return m_pBuffer;
}
// Very fast conversion from LPCWSTR. If XCHAR == WCHAR
// the function just returns the original pointer
LPCXSTR GetLPCXSTR(LPCWSTR szStringW)
{
// Must be non-NULL and non-empty
if(szStringW != NULL)
{
// Conversion from LPCWSTR to LPCSTR?
if(m_nCharSize == sizeof(CHAR))
{
InitFromLPCWSTR(szStringW);
return GetStr();
}
else
{
return (LPCXSTR)(szStringW);
}
}
return NULL;
}
// Very fast conversion from LPCSTR. If XCHAR == CHAR,
// the function just returns the original pointer
LPCXSTR GetLPCXSTR(LPCSTR szStringA)
{
// Must be non-NULL and non-empty
if(szStringA != NULL)
{
// Conversion from LPCSTR to LPCWSTR?
if(m_nCharSize == sizeof(WCHAR))
{
InitFromLPCSTR(szStringA);
return GetStr();
}
else
{
return (LPCXSTR)(szStringA);
}
}
return NULL;
}
LPCXSTR GetBuffer() const
{
return m_pBuffer;
}
bool SetLength(size_t nLength)
{
// Make sure there is enough space in the bufer
if(nLength > (size_t)(m_pBufferEnd - m_pBuffer))
{
if(!EnsureSpace(nLength - Length()))
{
return false;
}
}
// Adjust pointers
assert((m_pBuffer + nLength) < m_pBufferEnd);
m_pBufferPtr = m_pBuffer + nLength;
return true;
}
size_t Length() const
{
return m_pBufferPtr - m_pBuffer;
}
bool IsEmpty() const
{
return (m_pBufferPtr == m_pBuffer);
}
operator LPCXSTR()
{
return GetStr();
}
XCHAR operator[](size_t nIndex) const
{
if((m_pBuffer + nIndex) > m_pBufferPtr)
return 0;
return m_pBuffer[nIndex];
}
protected:
bool EnsureSpace(size_t nReserve)
{
// Would we go beyond the buffer if we added that number of chars?
if((m_pBufferPtr + nReserve) > m_pBufferEnd)
{
size_t nMaxLength = (m_pBufferEnd - m_pBuffer);
size_t nLength = (m_pBufferPtr - m_pBuffer);
// Calculate new needed size
while ((nLength + nReserve) > nMaxLength)
nMaxLength <<= 1;
// Allocate new buffer
if(m_pBuffer == m_StaticBuff)
{
// First reallocation: Allocate brand new.
m_pBuffer = (LPXSTR)HeapAlloc(g_hHeap, 0, ((nMaxLength + 1) * sizeof(XCHAR)));
if(m_pBuffer == NULL)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return false;
}
memcpy(m_pBuffer, m_StaticBuff, nLength * sizeof(XCHAR));
}
// Reallocate old buffer
else
{
// Second reallocation: Use HeapReAlloc
LPXSTR pNewBuffer = (LPXSTR)HeapReAlloc(g_hHeap, 0, m_pBuffer, ((nMaxLength + 1) * sizeof(XCHAR)));
// Failed - the old buffer is still allocated
if(pNewBuffer == NULL)
{
HeapFree(g_hHeap, 0, m_pBuffer);
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return false;
}
// Remember the new buffer
m_pBuffer = pNewBuffer;
}
// Adjust buffers
m_pBufferEnd = m_pBuffer + nMaxLength;
m_pBufferPtr = m_pBuffer + nLength;
}
return true;
}
LPXSTR m_pBuffer; // Pointer to the begin of the buffer
LPXSTR m_pBufferPtr; // Pointer to the current position in the buffer
LPXSTR m_pBufferEnd; // Pointer to the end of the buffer. There is always 1 extra character!
size_t m_nCharSize; // sizeof(XCHAR)
XCHAR m_StaticBuff[0x80]; // For shorter strings, we use static buffer
};
//-----------------------------------------------------------------------------
// Convertor from UNICODE string to ANSI string
struct TAnsiString : public TFastString<CHAR>
{
TAnsiString()
{}
TAnsiString(LPCSTR szString)
{
InitFromLPCSTR(szString);
}
TAnsiString(LPCWSTR szString)
{
InitFromLPCWSTR(szString);
}
};
//-----------------------------------------------------------------------------
// Convertor from ANSI string to UNICODE string
struct TWideString : public TFastString<WCHAR>
{
TWideString()
{}
TWideString(LPCSTR szString)
{
InitFromLPCSTR(szString);
}
TWideString(LPCWSTR szString)
{
InitFromLPCWSTR(szString);
}
};
#endif // __TFASTSTRING_H__