forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utlstring.cpp
548 lines (452 loc) · 16.2 KB
/
utlstring.cpp
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
//========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Larger string functions go here.
//
// $Header: $
// $NoKeywords: $
//=============================================================================//
#define _GNU_SOURCE 1 // need for vasprintf
#include "utlstring.h"
#include "utlvector.h"
#include "winlite.h"
//-----------------------------------------------------------------------------
// Purpose: Helper: Find s substring
//-----------------------------------------------------------------------------
static ptrdiff_t IndexOf( const char *pstrToSearch, const char *pstrTarget )
{
const char *pstrHit = V_strstr( pstrToSearch, pstrTarget );
if ( pstrHit == NULL )
{
return -1; // Not found.
}
return ( pstrHit - pstrToSearch );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string ends with the string passed in
//-----------------------------------------------------------------------------
static bool BEndsWith( const char *pstrToSearch, const char *pstrToFind, bool bCaseless )
{
if ( !pstrToSearch )
return false;
if ( !pstrToFind )
return true;
size_t nThisLength = V_strlen( pstrToSearch );
size_t nThatLength = V_strlen( pstrToFind );
if ( nThatLength == 0 )
return true;
if ( nThatLength > nThisLength )
return false;
size_t nIndex = nThisLength - nThatLength;
if ( bCaseless )
return V_stricmp( pstrToSearch + nIndex, pstrToFind ) == 0;
else
return V_strcmp( pstrToSearch + nIndex, pstrToFind ) == 0;
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string starts with the string passed in
//-----------------------------------------------------------------------------
static bool BStartsWith( const char *pstrToSearch, const char *pstrToFind, bool bCaseless )
{
if ( !pstrToSearch )
return false;
if ( !pstrToFind )
return true;
if ( bCaseless )
{
int nThatLength = V_strlen( pstrToFind );
if ( nThatLength == 0 )
return true;
return V_strnicmp( pstrToSearch, pstrToFind, nThatLength ) == 0;
}
else
return V_strstr( pstrToSearch, pstrToFind ) == pstrToSearch;
}
//-----------------------------------------------------------------------------
// Purpose: Helper: kill all whitespace.
//-----------------------------------------------------------------------------
static size_t RemoveWhitespace( char *pszString )
{
if ( pszString == NULL )
return 0;
char *pstrDest = pszString;
size_t cRemoved = 0;
for ( char *pstrWalker = pszString; *pstrWalker != 0; pstrWalker++ )
{
if ( !isspace( (unsigned char)*pstrWalker ) )
{
*pstrDest = *pstrWalker;
pstrDest++;
}
else
cRemoved += 1;
}
*pstrDest = 0;
return cRemoved;
}
//-----------------------------------------------------------------------------
// Purpose: Helper for Format() method
//-----------------------------------------------------------------------------
size_t CUtlString::FormatV( const char *pFormat, va_list args )
{
size_t len = 0;
#if defined _WIN32 || defined __WATCOMC__
char buf[4096];
len = _vsnprintf( buf, sizeof( buf ), pFormat, args );
Assert( len >= 0 );
Assert( len < sizeof( buf ));
// get it
FreePv( m_pchString );
m_pchString = (char *)PvAlloc( len + 1 );
strcpy( m_pchString, buf );
#elif defined ( _PS3 )
// ignore the PS3 documentation about vsnprintf returning -1 when the string is too small. vsprintf seems to do the right thing (least at time of
// implementation) and returns the number of characters needed when you pass in a buffer that is too small
FreePv( m_pchString );
m_pchString = NULL;
len = vsnprintf( NULL, 0, pFormat, args );
if ( len > 0 )
{
m_pchString = (char*) PvAlloc( len + 1 );
len = vsnprintf( m_pchString, len + 1, pFormat, args );
}
#else
char *buf = NULL;
len = vasprintf( &buf, pFormat, args );
// Len < 0 represents an overflow
if( buf )
{
// We need to get the string into PvFree-compatible memory, which
// we can't assume is directly interoperable with the malloc memory
// that vasprintf returned (definitely not compatible with a debug
// allocator, for example).
Set( buf );
free( buf );
}
#endif
return len;
}
//-----------------------------------------------------------------------------
// Purpose: implementation helper for AppendFormat()
//-----------------------------------------------------------------------------
size_t CUtlString::VAppendFormat( const char *pFormat, va_list args )
{
size_t len = 0;
#if defined _WIN32 || defined __WATCOMC__
char pstrFormatted[4096];
// format into that space, which is certainly enough
len = _vsnprintf( pstrFormatted, sizeof(pstrFormatted), pFormat, args );
Assert( len >= 0 );
Assert( len < sizeof( pstrFormatted ));
#elif defined ( _PS3 )
char *pstrFormatted = NULL;
// ignore the PS3 documentation about vsnprintf returning -1 when the string is too small. vsprintf seems to do the right thing (least at time of
// implementation) and returns the number of characters needed when you pass in a buffer that is too small
len = vsnprintf( NULL, 0, pFormat, args );
if ( len > 0 )
{
pstrFormatted = (char*) PvAlloc( len + 1 );
len = vsnprintf( pstrFormatted, len + 1, pFormat, args );
}
#else
char *pstrFormatted = NULL;
len = vasprintf( &pstrFormatted, pFormat, args );
#endif
// if we ended with a formatted string, append and free it
if ( pstrFormatted != NULL )
{
Append( pstrFormatted, len );
#if defined( _WIN32 ) || defined __WATCOMC__
// no need to free a buffer on stack
#elif defined( _PS3 )
FreePv( pstrFormatted );
#else
free( pstrFormatted );
#endif
}
return len;
}
//-----------------------------------------------------------------------------
// Purpose: replace all occurrences of one string with another
// replacement string may be NULL or "" to remove target string
//-----------------------------------------------------------------------------
size_t CUtlString::Replace( const char *pstrTarget, const char *pstrReplacement )
{
return ReplaceInternal( pstrTarget, pstrReplacement,
(const char *(*)(const char *,const char *))V_strstr );
}
//-----------------------------------------------------------------------------
// Purpose: replace all occurrences of one string with another
// replacement string may be NULL or "" to remove target string
//-----------------------------------------------------------------------------
size_t CUtlString::ReplaceCaseless( const char *pstrTarget, const char *pstrReplacement )
{
return ReplaceInternal( pstrTarget, pstrReplacement, V_stristr );
}
//-----------------------------------------------------------------------------
// Purpose: replace all occurrences of one string with another
// replacement string may be NULL or "" to remove target string
//-----------------------------------------------------------------------------
size_t CUtlString::ReplaceInternal( const char *pstrTarget, const char *pstrReplacement, const char *pfnCompare(const char*, const char*) )
{
size_t cReplacements = 0;
if ( pstrReplacement == NULL )
pstrReplacement = "";
size_t nTargetLength = V_strlen( pstrTarget );
size_t nReplacementLength = V_strlen( pstrReplacement );
if ( m_pchString != NULL && pstrTarget != NULL )
{
// walk the string counting hits
const char *pstrHit = m_pchString;
for ( pstrHit = pfnCompare( pstrHit, pstrTarget ); pstrHit != NULL && *pstrHit != 0; /* inside */ )
{
cReplacements++;
// look for the next target and keep looping
pstrHit = pfnCompare( pstrHit + nTargetLength, pstrTarget );
}
// if we didn't miss, get to work
if ( cReplacements > 0 )
{
// reallocate only once; how big will we need?
size_t nNewLength = 1 + V_strlen( m_pchString ) + cReplacements * ( nReplacementLength - nTargetLength );
char *pstrNew = (char*) PvAlloc( nNewLength );
if ( nNewLength == 1 )
{
// shortcut simple case, even if rare
*pstrNew = 0;
}
else
{
const char *pstrPreviousHit = NULL;
char *pstrDestination = pstrNew;
pstrHit = m_pchString;
size_t cActualReplacements = 0;
for ( pstrHit = pfnCompare( m_pchString, pstrTarget ); pstrHit != NULL && *pstrHit != 0; /* inside */ )
{
cActualReplacements++;
// copy from the previous hit to the match
if ( pstrPreviousHit == NULL )
pstrPreviousHit = m_pchString;
memcpy( pstrDestination, pstrPreviousHit, pstrHit - pstrPreviousHit );
pstrDestination += ( pstrHit - pstrPreviousHit );
// push the replacement string in
memcpy( pstrDestination, pstrReplacement, nReplacementLength );
pstrDestination += nReplacementLength;
pstrPreviousHit = pstrHit + nTargetLength;
pstrHit = pfnCompare( pstrPreviousHit, pstrTarget );
}
while ( pstrPreviousHit != NULL && *pstrPreviousHit != 0 )
{
*pstrDestination = *pstrPreviousHit;
pstrDestination++;
pstrPreviousHit++;
}
*pstrDestination = 0;
Assert( pstrNew + nNewLength == pstrDestination + 1);
Assert( cActualReplacements == cReplacements );
}
// release the old string, set the new one
FreePv( m_pchString );
m_pchString = pstrNew;
}
}
return cReplacements;
}
//-----------------------------------------------------------------------------
// Purpose: Indicates if the target string exists in this instance.
// The index is negative if the target string is not found, otherwise it is the index in the string.
//-----------------------------------------------------------------------------
ptrdiff_t CUtlString::IndexOf( const char *pstrTarget ) const
{
return ::IndexOf( String(), pstrTarget );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string ends with the string passed in
//-----------------------------------------------------------------------------
bool CUtlString::BEndsWith( const char *pchString ) const
{
return ::BEndsWith( String(), pchString, false );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string ends with the string passed in (caseless)
//-----------------------------------------------------------------------------
bool CUtlString::BEndsWithCaseless( const char *pchString ) const
{
return ::BEndsWith( String(), pchString, true );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string starts with the string passed in
//-----------------------------------------------------------------------------
bool CUtlString::BStartsWith( const char *pchString ) const
{
return ::BStartsWith( String(), pchString, false );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the string ends with the string passed in (caseless)
//-----------------------------------------------------------------------------
bool CUtlString::BStartsWithCaseless( const char *pchString ) const
{
return ::BStartsWith( String(), pchString, true );
}
//-----------------------------------------------------------------------------
// Purpose:
// remove whitespace -- anything that is isspace() -- from the string
//-----------------------------------------------------------------------------
size_t CUtlString::RemoveWhitespace()
{
return ::RemoveWhitespace( m_pchString );
}
//-----------------------------------------------------------------------------
// Purpose:
// trim whitespace from front and back of string
//-----------------------------------------------------------------------------
size_t CUtlString::TrimWhitespace()
{
if ( m_pchString == NULL )
return 0;
int cChars = V_StrTrim( m_pchString );
return cChars;
}
//-----------------------------------------------------------------------------
// Purpose:
// trim whitespace from back of string
//-----------------------------------------------------------------------------
size_t CUtlString::TrimTrailingWhitespace()
{
if ( m_pchString == NULL )
return 0;
uint32_t cChars = Length();
if ( cChars == 0 )
return 0;
char *pCur = &m_pchString[cChars - 1];
while ( pCur >= m_pchString && isspace( *pCur ) )
{
*pCur = '\0';
pCur--;
}
return pCur - m_pchString + 1;
}
//-----------------------------------------------------------------------------
// Purpose: out-of-line assertion to keep code generation size down
//-----------------------------------------------------------------------------
void CUtlString::AssertStringTooLong()
{
AssertMsg( false, "Assertion failed: length > k_cchMaxString" );
}
//-----------------------------------------------------------------------------
// Purpose: format binary data as hex characters, appending to existing data
//-----------------------------------------------------------------------------
void CUtlString::AppendHex( const uint8_t *pbInput, size_t cubInput, bool bLowercase /*= true*/ )
{
if ( !cubInput )
return;
size_t existingLen = Length();
if ( existingLen >= k_cchMaxString || cubInput*2 >= k_cchMaxString - existingLen )
{
Assert( existingLen < k_cchMaxString && cubInput * 2 < k_cchMaxString - existingLen );
return;
}
const char *pchHexLookup = bLowercase ? "0123456789abcdef" : "0123456789ABCDEF";
CUtlString newValue( existingLen + cubInput * 2 + 1 );
V_memcpy( newValue.Access(), Access(), existingLen );
char *pOut = newValue.Access() + existingLen;
for ( ; cubInput; --cubInput, ++pbInput )
{
uint8_t val = *pbInput;
*pOut++ = pchHexLookup[val >> 4];
*pOut++ = pchHexLookup[val & 15];
}
*pOut = '\0';
Swap( newValue );
}
// Catch invalid UTF-8 sequences and return false if found, or true if the sequence is correct
static bool BVerifyValidUTF8Continuation( size_t unStart, size_t unContinuationLength, const uint8_t *pbCharacters )
{
for ( size_t i = 0; i < unContinuationLength; ++ i )
{
// Make sure byte is of the form 10xxxxxx
// Note: this also catches an unexpected NULL terminator and prevents us from overrunning the string
if ( ( pbCharacters[i + unStart] & 0xC0 ) != 0x80 )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Caps the string to the specified number of bytes/chars,
// while respecting UTF-8 character blocks. Resulting string will be
// strictly less than unMaxChars and unMaxBytes.
//-----------------------------------------------------------------------------
bool CUtlString::TruncateUTF8Internal( size_t unMaxChars, size_t unMaxBytes )
{
if ( !m_pchString )
return false;
const uint8_t *pbCharacters = ( const uint8_t * )m_pchString;
size_t unBytes = 0;
size_t unChars = 0;
bool bSuccess = true;
while ( unBytes < unMaxBytes && unChars < unMaxChars && pbCharacters[unBytes] != '\0' )
{
if ( ( pbCharacters[unBytes] & 0x80 ) == 0 )
{
// standard ASCII
unBytes ++;
}
else if ( ( pbCharacters[unBytes] & 0xE0 ) == 0xC0 ) // check for 110xxxxx bit pattern, indicates 2 byte character
{
if ( !BVerifyValidUTF8Continuation( unBytes, 1, pbCharacters + 1 ) )
{
bSuccess = false;
break;
}
unBytes += 2;
}
else if ( ( pbCharacters[unBytes] & 0xF0 ) == 0xE0 ) // check for 1110xxxx bit pattern, indicates 3 byte character
{
if ( !BVerifyValidUTF8Continuation( unBytes, 2, pbCharacters + 1 ) )
{
bSuccess = false;
break;
}
unBytes += 3;
}
else if ( ( pbCharacters[unBytes] & 0xF8 ) == 0xF0 ) // check for 11110xxx bit pattern, indicates 4 byte character
{
if ( !BVerifyValidUTF8Continuation( unBytes, 3, pbCharacters + 1 ) )
{
bSuccess = false;
break;
}
unBytes += 4;
}
else if ( ( pbCharacters[unBytes] & 0xFC ) == 0xF8 ) // check for 111110xx bit pattern, indicates 5 byte character
{
if ( !BVerifyValidUTF8Continuation( unBytes, 4, pbCharacters + 1 ) )
{
bSuccess = false;
break;
}
unBytes += 5;
}
else if ( ( pbCharacters[unBytes] & 0xFE ) == 0xFC ) // check for 1111110x bit pattern, indicates 6 byte character
{
if ( !BVerifyValidUTF8Continuation( unBytes, 5, pbCharacters + 1 ) )
{
bSuccess = false;
break;
}
unBytes += 6;
}
else
{
// Unexpected character
bSuccess = false;
break;
}
unChars ++;
}
m_pchString[unBytes] = '\0';
return bSuccess;
}
void CUtlString::SecureZero()
{
PlatformSecureZeroMemory( m_pchString, V_strlen( m_pchString ));
}