forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
strtools.cpp
435 lines (361 loc) · 9.81 KB
/
strtools.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
//========= Copyright Valve Corporation, All rights reserved. ============//
#include <utlvector.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include "miniutl.h"
static char* AllocString( const char *pStr, int nMaxChars )
{
int allocLen;
if ( nMaxChars == -1 )
allocLen = V_strlen( pStr ) + 1;
else
allocLen = Min( V_strlen(pStr), nMaxChars ) + 1;
char *pOut = new char[allocLen];
V_strncpy( pOut, pStr, allocLen );
return pOut;
}
int V_strncmp( const char *s1, const char *s2, int count )
{
Assert( count >= 0 );
Assert( count == 0 || s1 != NULL );
Assert( count == 0 || s2 != NULL );
while ( count-- > 0 )
{
if ( *s1 != *s2 )
return *s1 < *s2 ? -1 : 1; // string different
if ( *s1 == '\0' )
return 0; // null terminator hit - strings the same
s1++;
s2++;
}
return 0; // count characters compared the same
}
//-----------------------------------------------------------------------------
// Finds a string in another string with a case insensitive test w/ length validation
//-----------------------------------------------------------------------------
char const* V_strnistr( char const* pStr, char const* pSearch, int n )
{
Assert( pStr != NULL );
Assert( pSearch != NULL );
if ( pStr == NULL || pSearch == NULL )
return 0;
char const* pLetter = pStr;
// Check the entire string
while ( *pLetter != 0 )
{
if ( n <= 0 )
return 0;
// Skip over non-matches
if ( tolower( *pLetter ) == tolower( *pSearch ) )
{
int n1 = n - 1;
// Check for match
char const* pMatch = pLetter + 1;
char const* pTest = pSearch + 1;
while (*pTest != 0)
{
if ( n1 <= 0 )
return 0;
// We've run off the end; don't bother.
if (*pMatch == 0)
return 0;
if ( tolower( *pMatch ) != tolower( *pTest ) )
break;
++pMatch;
++pTest;
--n1;
}
// Found a match!
if (*pTest == 0)
return pLetter;
}
++pLetter;
--n;
}
return 0;
}
const char* V_strnchr( const char* pStr, char c, int n )
{
char const* pLetter = pStr;
char const* pLast = pStr + n;
// Check the entire string
while ( (pLetter < pLast) && (*pLetter != 0) )
{
if (*pLetter == c)
return pLetter;
++pLetter;
}
return NULL;
}
int V_strnicmp( const char *s1, const char *s2, int n )
{
Assert( n >= 0 );
Assert( n == 0 || s1 != NULL );
Assert( n == 0 || s2 != NULL );
while ( n-- > 0 )
{
int c1 = *s1++;
int c2 = *s2++;
if ( c1 != c2 )
{
if ( c1 >= 'a' && c1 <= 'z' )
c1 -= ('a' - 'A');
if ( c2 >= 'a' && c2 <= 'z' )
c2 -= ( 'a' - 'A' );
if ( c1 != c2 )
return c1 < c2 ? -1 : 1;
}
if ( c1 == '\0' )
return 0; // null terminator hit - strings the same
}
return 0; // n characters compared the same
}
//-----------------------------------------------------------------------------
// Finds a string in another string with a case insensitive test
//-----------------------------------------------------------------------------
char const* V_stristr( char const* pStr, char const* pSearch )
{
Assert( pStr != NULL );
Assert( pSearch != NULL );
if ( pStr == NULL || pSearch == NULL )
return NULL;
char const* pLetter = pStr;
// Check the entire string
while ( *pLetter != 0 )
{
// Skip over non-matches
if ( tolower( (unsigned char) *pLetter ) == tolower( (unsigned char) *pSearch ) )
{
// Check for match
char const* pMatch = pLetter + 1;
char const* pTest = pSearch + 1;
while (*pTest != 0)
{
// We've run off the end; don't bother.
if (*pMatch == 0)
return 0;
if ( tolower( (unsigned char) *pMatch ) != tolower( (unsigned char) *pTest ) )
break;
++pMatch;
++pTest;
}
// Found a match!
if ( *pTest == 0 )
return pLetter;
}
++pLetter;
}
return 0;
}
//-----------------------------------------------------------------------------
// Purpose: copy a string while observing the maximum length of the target buffer
//
// Inputs: pDest - pointer to the destination string
// pSrc - pointer to the source string
// maxLen - number of bytes available at pDest
//
// This function will always leave pDest nul-terminated if maxLen > 0
//
// maxLen is a count of bytes, not a count of characters.
// maxLen includes budget for the nul terminator at pDest,
// so using maxLen = 3 would copy two characters from pSrc and add a trailing nul.
//-----------------------------------------------------------------------------
void V_strncpy( char *pDest, char const *pSrc, size_t maxLen )
{
Assert( maxLen == 0 || pDest != NULL );
Assert( pSrc != NULL );
size_t nCount = maxLen;
char *pstrDest = pDest;
const char *pstrSource = pSrc;
while ( 0 < nCount && 0 != ( *pstrDest++ = *pstrSource++ ) )
nCount--;
if ( maxLen > 0 )
pstrDest[-1] = 0;
}
int V_snprintf( char *pDest, size_t bufferLen, char const *pFormat, ... )
{
Assert( bufferLen > 0 );
Assert( pDest != NULL );
Assert( pFormat != NULL );
va_list marker;
va_start( marker, pFormat );
// _vsnprintf will not write a terminator if the output string uses the entire buffer you provide
int len = _vsnprintf( pDest, bufferLen, pFormat, marker );
va_end( marker );
// Len < 0 represents an overflow on windows; len > buffer length on posix
if (( len < 0 ) || ((size_t)len >= bufferLen ) )
{
len = (int)(bufferLen-1);
}
pDest[len] = 0;
return len;
}
int V_vsnprintf( char *pDest, int bufferLen, char const *pFormat, va_list params )
{
Assert( bufferLen > 0 );
Assert( pDest != NULL );
Assert( pFormat != NULL );
int len = _vsnprintf( pDest, bufferLen, pFormat, params );
// Len < 0 represents an overflow on windows; len > buffer length on posix
if (( len < 0 ) || (len >= bufferLen ) )
{
len = bufferLen-1;
}
pDest[len] = 0;
return len;
}
int V_vsnprintfRet( char *pDest, int bufferLen, const char *pFormat, va_list params, bool *pbTruncated )
{
Assert( bufferLen > 0 );
Assert( bufferLen == 0 || pDest != NULL );
Assert( pFormat != NULL );
bool bTruncatedUnderstudy;
if ( !pbTruncated )
pbTruncated = &bTruncatedUnderstudy;
int len = _vsnprintf( pDest, bufferLen, pFormat, params );
// Len < 0 represents an overflow on windows; len > buffer length on posix
if (( len < 0 ) || (len >= bufferLen ) )
{
*pbTruncated = true;
len = bufferLen-1;
}
else
{
*pbTruncated = false;
}
pDest[len] = 0;
return len;
}
//-----------------------------------------------------------------------------
// Purpose: If COPY_ALL_CHARACTERS == max_chars_to_copy then we try to add the whole pSrc to the end of pDest, otherwise
// we copy only as many characters as are specified in max_chars_to_copy (or the # of characters in pSrc if thats's less).
// Input : *pDest - destination buffer
// *pSrc - string to append
// destBufferSize - sizeof the buffer pointed to by pDest
// max_chars_to_copy - COPY_ALL_CHARACTERS in pSrc or max # to copy
// Output : char * the copied buffer
//-----------------------------------------------------------------------------
char *V_strncat(char *pDest, const char *pSrc, size_t destBufferSize, int max_chars_to_copy )
{
size_t charstocopy = (size_t)0;
Assert( pDest != NULL );
Assert( pSrc != NULL );
size_t len = strlen(pDest);
size_t srclen = strlen( pSrc );
if ( max_chars_to_copy <= COPY_ALL_CHARACTERS )
{
charstocopy = srclen;
}
else
{
charstocopy = (size_t)Min( max_chars_to_copy, (int)srclen );
}
if ( len + charstocopy >= destBufferSize )
{
charstocopy = destBufferSize - len - 1;
}
if ( (ptrdiff_t)charstocopy <= 0 )
{
return pDest;
}
char *pOut = strncat( pDest, pSrc, charstocopy );
return pOut;
}
void V_SplitString2( const char *pString, const char * const *pSeparators, int nSeparators, CUtlVector<char*> &outStrings, bool bIncludeEmptyStrings )
{
outStrings.Purge();
const char *pCurPos = pString;
for (;;)
{
int iFirstSeparator = -1;
const char *pFirstSeparator = 0;
for ( int i=0; i < nSeparators; i++ )
{
const char *pTest = V_stristr( pCurPos, pSeparators[i] );
if ( pTest && (!pFirstSeparator || pTest < pFirstSeparator) )
{
iFirstSeparator = i;
pFirstSeparator = pTest;
}
}
if ( pFirstSeparator )
{
// Split on this separator and continue on.
int separatorLen = (int)strlen( pSeparators[iFirstSeparator] );
if ( pFirstSeparator > pCurPos || ( pFirstSeparator == pCurPos && bIncludeEmptyStrings ) )
{
outStrings.AddToTail( AllocString( pCurPos, pFirstSeparator-pCurPos ) );
}
pCurPos = pFirstSeparator + separatorLen;
}
else
{
// Copy the rest of the string, if there's anything there
if ( pCurPos[0] != 0 )
{
outStrings.AddToTail( AllocString( pCurPos, -1 ) );
}
return;
}
}
}
void V_SplitString( const char *pString, const char *pSeparator, CUtlVector<char*> &outStrings, bool bIncludeEmptyStrings )
{
V_SplitString2( pString, &pSeparator, 1, outStrings, bIncludeEmptyStrings );
}
//-----------------------------------------------------------------------------
void V_StripTrailingWhitespaceASCII( char *pch )
{
if ( !pch )
return;
// Remember where we would terminate the string
char *t = pch;
// Scan until we hit the end of the string
while ( *pch != '\0' )
{
// Non-whitespace? Then assume termination immediately
// after, if we don't find any more whitespace
if ( !V_isspace( *pch ) )
t = pch+1;
++pch;
}
// Terminate
*t = '\0';
}
int V_StrTrim( char *pStr )
{
char *pSource = pStr;
char *pDest = pStr;
// skip white space at the beginning
while ( *pSource != 0 && isspace( *pSource ) )
{
pSource++;
}
// copy everything else
char *pLastWhiteBlock = NULL;
char *pStart = pDest;
while ( *pSource != 0 )
{
*pDest = *pSource++;
if ( isspace( *pDest ) )
{
if ( pLastWhiteBlock == NULL )
pLastWhiteBlock = pDest;
}
else
{
pLastWhiteBlock = NULL;
}
pDest++;
}
*pDest = 0;
// did we end in a whitespace block?
if ( pLastWhiteBlock != NULL )
{
// yep; shorten the string
pDest = pLastWhiteBlock;
*pLastWhiteBlock = 0;
}
return pDest - pStart;
}