-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathstrtools.cpp
2982 lines (2542 loc) · 77.3 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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: String Tools
//
//===========================================================================//
// These are redefined in the project settings to prevent anyone from using them.
// We in this module are of a higher caste and thus are privileged in their use.
#ifdef strncpy
#undef strncpy
#endif
#ifdef _snprintf
#undef _snprintf
#endif
#if defined( sprintf )
#undef sprintf
#endif
#if defined( vsprintf )
#undef vsprintf
#endif
#ifdef _vsnprintf
#ifdef _WIN32
#undef _vsnprintf
#endif
#endif
#ifdef vsnprintf
#ifndef _WIN32
#undef vsnprintf
#endif
#endif
#if defined( strcat )
#undef strcat
#endif
#ifdef strncat
#undef strncat
#endif
// NOTE: I have to include stdio + stdarg first so vsnprintf gets compiled in
#include <stdio.h>
#include <stdarg.h>
#ifdef POSIX
#include <iconv.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#define _getcwd getcwd
#elif _WIN32
#include <direct.h>
#if !defined( _X360 )
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#endif
#ifdef _WIN32
#ifndef CP_UTF8
#define CP_UTF8 65001
#endif
#endif
#include "tier0/dbg.h"
#include "tier1/strtools.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "tier0/basetypes.h"
#include "tier1/utldict.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
#include "tier0/memdbgon.h"
static int FastToLower( char c )
{
int i = (unsigned char) c;
if ( i < 0x80 )
{
// Brutally fast branchless ASCII tolower():
i += (((('A'-1) - i) & (i - ('Z'+1))) >> 26) & 0x20;
}
else
{
i += isupper( i ) ? 0x20 : 0;
}
return i;
}
void _V_memset (const char* file, int line, void *dest, int fill, int count)
{
Assert( count >= 0 );
AssertValidWritePtr( dest, count );
memset(dest,fill,count);
}
void _V_memcpy (const char* file, int line, void *dest, const void *src, int count)
{
Assert( count >= 0 );
AssertValidReadPtr( src, count );
AssertValidWritePtr( dest, count );
memcpy( dest, src, count );
}
void _V_memmove(const char* file, int line, void *dest, const void *src, int count)
{
Assert( count >= 0 );
AssertValidReadPtr( src, count );
AssertValidWritePtr( dest, count );
memmove( dest, src, count );
}
int _V_memcmp (const char* file, int line, const void *m1, const void *m2, int count)
{
Assert( count >= 0 );
AssertValidReadPtr( m1, count );
AssertValidReadPtr( m2, count );
return memcmp( m1, m2, count );
}
int _V_strlen(const char* file, int line, const char *str)
{
AssertValidStringPtr(str);
return strlen( str );
}
void _V_strcpy (const char* file, int line, char *dest, const char *src)
{
AssertValidWritePtr(dest);
AssertValidStringPtr(src);
strcpy( dest, src );
}
int _V_wcslen(const char* file, int line, const wchar_t *pwch)
{
return wcslen( pwch );
}
char *_V_strrchr(const char* file, int line, const char *s, char c)
{
AssertValidStringPtr( s );
int len = V_strlen(s);
s += len;
while (len--)
if (*--s == c) return (char *)s;
return 0;
}
int _V_strcmp (const char* file, int line, const char *s1, const char *s2)
{
AssertValidStringPtr( s1 );
AssertValidStringPtr( s2 );
return strcmp( s1, s2 );
}
int _V_wcscmp (const char* file, int line, const wchar_t *s1, const wchar_t *s2)
{
AssertValidReadPtr( s1 );
AssertValidReadPtr( s2 );
while ( *s1 == *s2 )
{
if ( !*s1 )
return 0; // strings are equal
s1++;
s2++;
}
return *s1 > *s2 ? 1 : -1; // strings not equal
}
char *_V_strstr(const char* file, int line, const char *s1, const char *search )
{
AssertValidStringPtr( s1 );
AssertValidStringPtr( search );
#if defined( _X360 )
return (char *)strstr( (char *)s1, search );
#else
return (char *)strstr( s1, search );
#endif
}
wchar_t *_V_wcsupr (const char* file, int line, wchar_t *start)
{
return _wcsupr( start );
}
wchar_t *_V_wcslower (const char* file, int line, wchar_t *start)
{
return _wcslwr(start);
}
char *V_strupr( char *start )
{
unsigned char *str = (unsigned char*)start;
while( *str )
{
if ( (unsigned char)(*str - 'a') <= ('z' - 'a') )
*str -= 'a' - 'A';
else if ( (unsigned char)*str >= 0x80 ) // non-ascii, fall back to CRT
*str = toupper( *str );
str++;
}
return start;
}
char *V_strlower( char *start )
{
unsigned char *str = (unsigned char*)start;
while( *str )
{
if ( (unsigned char)(*str - 'A') <= ('Z' - 'A') )
*str += 'a' - 'A';
else if ( (unsigned char)*str >= 0x80 ) // non-ascii, fall back to CRT
*str = tolower( *str );
str++;
}
return start;
}
char *V_strnlwr(char *s, size_t count)
{
// Assert( count >= 0 ); tautology since size_t is unsigned
AssertValidStringPtr( s, count );
char* pRet = s;
if ( !s || !count )
return s;
while ( -- count > 0 )
{
if ( !*s )
return pRet; // reached end of string
*s = tolower( *s );
++s;
}
*s = 0; // null-terminate original string at "count-1"
return pRet;
}
int V_stricmp( const char *str1, const char *str2 )
{
// It is not uncommon to compare a string to itself. See
// VPanelWrapper::GetPanel which does this a lot. Since stricmp
// is expensive and pointer comparison is cheap, this simple test
// can save a lot of cycles, and cache pollution.
if ( str1 == str2 )
{
return 0;
}
const unsigned char *s1 = (const unsigned char*)str1;
const unsigned char *s2 = (const unsigned char*)str2;
for ( ; *s1; ++s1, ++s2 )
{
if ( *s1 != *s2 )
{
// in ascii char set, lowercase = uppercase | 0x20
unsigned char c1 = *s1 | 0x20;
unsigned char c2 = *s2 | 0x20;
if ( c1 != c2 || (unsigned char)(c1 - 'a') > ('z' - 'a') )
{
// if non-ascii mismatch, fall back to CRT for locale
if ( (c1 | c2) >= 0x80 ) return stricmp( (const char*)s1, (const char*)s2 );
// ascii mismatch. only use the | 0x20 value if alphabetic.
if ((unsigned char)(c1 - 'a') > ('z' - 'a')) c1 = *s1;
if ((unsigned char)(c2 - 'a') > ('z' - 'a')) c2 = *s2;
return c1 > c2 ? 1 : -1;
}
}
}
return *s2 ? -1 : 0;
}
int V_strnicmp( const char *str1, const char *str2, int n )
{
const unsigned char *s1 = (const unsigned char*)str1;
const unsigned char *s2 = (const unsigned char*)str2;
for ( ; n > 0 && *s1; --n, ++s1, ++s2 )
{
if ( *s1 != *s2 )
{
// in ascii char set, lowercase = uppercase | 0x20
unsigned char c1 = *s1 | 0x20;
unsigned char c2 = *s2 | 0x20;
if ( c1 != c2 || (unsigned char)(c1 - 'a') > ('z' - 'a') )
{
// if non-ascii mismatch, fall back to CRT for locale
if ( (c1 | c2) >= 0x80 ) return strnicmp( (const char*)s1, (const char*)s2, n );
// ascii mismatch. only use the | 0x20 value if alphabetic.
if ((unsigned char)(c1 - 'a') > ('z' - 'a')) c1 = *s1;
if ((unsigned char)(c2 - 'a') > ('z' - 'a')) c2 = *s2;
return c1 > c2 ? 1 : -1;
}
}
}
return (n > 0 && *s2) ? -1 : 0;
}
int V_strncmp( const char *s1, const char *s2, int count )
{
Assert( count >= 0 );
AssertValidStringPtr( s1, count );
AssertValidStringPtr( s2, count );
while ( count > 0 )
{
if ( *s1 != *s2 )
return (unsigned char)*s1 < (unsigned char)*s2 ? -1 : 1; // string different
if ( *s1 == '\0' )
return 0; // null terminator hit - strings the same
s1++;
s2++;
count--;
}
return 0; // count characters compared the same
}
const char *StringAfterPrefix( const char *str, const char *prefix )
{
AssertValidStringPtr( str );
AssertValidStringPtr( prefix );
do
{
if ( !*prefix )
return str;
}
while ( FastToLower( *str++ ) == FastToLower( *prefix++ ) );
return NULL;
}
const char *StringAfterPrefixCaseSensitive( const char *str, const char *prefix )
{
AssertValidStringPtr( str );
AssertValidStringPtr( prefix );
do
{
if ( !*prefix )
return str;
}
while ( *str++ == *prefix++ );
return NULL;
}
int64 V_atoi64( const char *str )
{
AssertValidStringPtr( str );
int64 val;
int64 sign;
int64 c;
Assert( str );
if (*str == '-')
{
sign = -1;
str++;
}
else
sign = 1;
val = 0;
//
// check for hex
//
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
{
str += 2;
while (1)
{
c = *str++;
if (c >= '0' && c <= '9')
val = (val<<4) + c - '0';
else if (c >= 'a' && c <= 'f')
val = (val<<4) + c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
val = (val<<4) + c - 'A' + 10;
else
return val*sign;
}
}
//
// check for character
//
if (str[0] == '\'')
{
return sign * str[1];
}
//
// assume decimal
//
while (1)
{
c = *str++;
if (c <'0' || c > '9')
return val*sign;
val = val*10 + c - '0';
}
return 0;
}
uint64 V_atoui64( const char *str )
{
AssertValidStringPtr( str );
uint64 val;
uint64 c;
Assert( str );
val = 0;
//
// check for hex
//
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
{
str += 2;
while (1)
{
c = *str++;
if (c >= '0' && c <= '9')
val = (val<<4) + c - '0';
else if (c >= 'a' && c <= 'f')
val = (val<<4) + c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
val = (val<<4) + c - 'A' + 10;
else
return val;
}
}
//
// check for character
//
if (str[0] == '\'')
{
return str[1];
}
//
// assume decimal
//
while (1)
{
c = *str++;
if (c <'0' || c > '9')
return val;
val = val*10 + c - '0';
}
return 0;
}
int V_atoi( const char *str )
{
return (int)V_atoi64( str );
}
float V_atof (const char *str)
{
AssertValidStringPtr( str );
double val;
int sign;
int c;
int decimal, total;
if (*str == '-')
{
sign = -1;
str++;
}
else if (*str == '+')
{
sign = 1;
str++;
}
else
{
sign = 1;
}
val = 0;
//
// check for hex
//
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
{
str += 2;
while (1)
{
c = *str++;
if (c >= '0' && c <= '9')
val = (val*16) + c - '0';
else if (c >= 'a' && c <= 'f')
val = (val*16) + c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
val = (val*16) + c - 'A' + 10;
else
return val*sign;
}
}
//
// check for character
//
if (str[0] == '\'')
{
return sign * str[1];
}
//
// assume decimal
//
decimal = -1;
total = 0;
int exponent = 0;
while (1)
{
c = *str++;
if (c == '.')
{
if ( decimal != -1 )
{
break;
}
decimal = total;
continue;
}
if (c <'0' || c > '9')
{
if ( c == 'e' || c == 'E' )
{
exponent = V_atoi(str);
}
break;
}
val = val*10 + c - '0';
total++;
}
if ( exponent != 0 )
{
val *= pow( 10.0, exponent );
}
if (decimal == -1)
return val*sign;
while (total > decimal)
{
val /= 10;
total--;
}
return val*sign;
}
//-----------------------------------------------------------------------------
// Normalizes a float string in place.
//
// (removes leading zeros, trailing zeros after the decimal point, and the decimal point itself where possible)
//-----------------------------------------------------------------------------
void V_normalizeFloatString( char* pFloat )
{
// If we have a decimal point, remove trailing zeroes:
if( strchr( pFloat,'.' ) )
{
int len = V_strlen(pFloat);
while( len > 1 && pFloat[len - 1] == '0' )
{
pFloat[len - 1] = '\0';
len--;
}
if( len > 1 && pFloat[ len - 1 ] == '.' )
{
pFloat[len - 1] = '\0';
len--;
}
}
// TODO: Strip leading zeros
}
//-----------------------------------------------------------------------------
// Finds a string in another string with a case insensitive test
//-----------------------------------------------------------------------------
char const* V_stristr( char const* pStr, char const* pSearch )
{
AssertValidStringPtr(pStr);
AssertValidStringPtr(pSearch);
if (!pStr || !pSearch)
return 0;
char const* pLetter = pStr;
// Check the entire string
while (*pLetter != 0)
{
// Skip over non-matches
if (FastToLower((unsigned char)*pLetter) == FastToLower((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 (FastToLower((unsigned char)*pMatch) != FastToLower((unsigned char)*pTest))
break;
++pMatch;
++pTest;
}
// Found a match!
if (*pTest == 0)
return pLetter;
}
++pLetter;
}
return 0;
}
char* V_stristr( char* pStr, char const* pSearch )
{
AssertValidStringPtr( pStr );
AssertValidStringPtr( pSearch );
return (char*)V_stristr( (char const*)pStr, pSearch );
}
//-----------------------------------------------------------------------------
// 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 )
{
AssertValidStringPtr(pStr);
AssertValidStringPtr(pSearch);
if (!pStr || !pSearch)
return 0;
char const* pLetter = pStr;
// Check the entire string
while (*pLetter != 0)
{
if ( n <= 0 )
return 0;
// Skip over non-matches
if (FastToLower(*pLetter) == FastToLower(*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 (FastToLower(*pMatch) != FastToLower(*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;
}
void V_strncpy( char *pDest, char const *pSrc, int maxLen )
{
Assert( maxLen >= sizeof( *pDest ) );
AssertValidWritePtr( pDest, maxLen );
AssertValidStringPtr( pSrc );
strncpy( pDest, pSrc, maxLen );
if ( maxLen > 0 )
{
pDest[maxLen-1] = 0;
}
}
// warning C6053: Call to 'wcsncpy' might not zero-terminate string 'pDest'
// warning C6059: Incorrect length parameter in call to 'strncat'. Pass the number of remaining characters, not the buffer size of 'argument 1'
// warning C6386: Buffer overrun: accessing 'argument 1', the writable size is 'destBufferSize' bytes, but '1000' bytes might be written
// These warnings were investigated through code inspection and writing of tests and they are
// believed to all be spurious.
#ifdef _PREFAST_
#pragma warning( push )
#pragma warning( disable : 6053 6059 6386 )
#endif
void V_wcsncpy( wchar_t *pDest, wchar_t const *pSrc, int maxLenInBytes )
{
Assert( maxLenInBytes >= sizeof( *pDest ) );
AssertValidWritePtr( pDest, maxLenInBytes );
AssertValidReadPtr( pSrc );
int maxLen = maxLenInBytes / sizeof(wchar_t);
wcsncpy( pDest, pSrc, maxLen );
if( maxLen )
{
pDest[maxLen-1] = 0;
}
}
int V_snwprintf( wchar_t *pDest, int maxLen, const wchar_t *pFormat, ... )
{
Assert( maxLen > 0 );
AssertValidWritePtr( pDest, maxLen );
AssertValidReadPtr( pFormat );
va_list marker;
va_start( marker, pFormat );
#ifdef _WIN32
int len = _vsnwprintf( pDest, maxLen, pFormat, marker );
#elif POSIX
int len = vswprintf( pDest, maxLen, pFormat, marker );
#else
#error "define vsnwprintf type."
#endif
va_end( marker );
// Len > maxLen represents an overflow on POSIX, < 0 is an overflow on windows
if( len < 0 || len >= maxLen )
{
len = maxLen;
pDest[maxLen-1] = 0;
}
return len;
}
int V_vsnwprintf( wchar_t *pDest, int maxLen, const wchar_t *pFormat, va_list params )
{
Assert( maxLen > 0 );
#ifdef _WIN32
int len = _vsnwprintf( pDest, maxLen, pFormat, params );
#elif POSIX
int len = vswprintf( pDest, maxLen, pFormat, params );
#else
#error "define vsnwprintf type."
#endif
// Len < 0 represents an overflow
// Len == maxLen represents exactly fitting with no NULL termination
// Len >= maxLen represents overflow on POSIX
if ( len < 0 || len >= maxLen )
{
len = maxLen;
pDest[maxLen-1] = 0;
}
return len;
}
int V_snprintf( char *pDest, int maxLen, char const *pFormat, ... )
{
Assert( maxLen > 0 );
AssertValidWritePtr( pDest, maxLen );
AssertValidStringPtr( pFormat );
va_list marker;
va_start( marker, pFormat );
#ifdef _WIN32
int len = _vsnprintf( pDest, maxLen, pFormat, marker );
#elif POSIX
int len = vsnprintf( pDest, maxLen, pFormat, marker );
#else
#error "define vsnprintf type."
#endif
va_end( marker );
// Len > maxLen represents an overflow on POSIX, < 0 is an overflow on windows
if( len < 0 || len >= maxLen )
{
len = maxLen;
pDest[maxLen-1] = 0;
}
return len;
}
int V_vsnprintf( char *pDest, int maxLen, char const *pFormat, va_list params )
{
Assert( maxLen > 0 );
AssertValidWritePtr( pDest, maxLen );
AssertValidStringPtr( pFormat );
int len = _vsnprintf( pDest, maxLen, pFormat, params );
// Len > maxLen represents an overflow on POSIX, < 0 is an overflow on windows
if( len < 0 || len >= maxLen )
{
len = maxLen;
pDest[maxLen-1] = 0;
}
return len;
}
int V_vsnprintfRet( char *pDest, int maxLen, const char *pFormat, va_list params, bool *pbTruncated )
{
Assert( maxLen > 0 );
AssertValidWritePtr( pDest, maxLen );
AssertValidStringPtr( pFormat );
int len = _vsnprintf( pDest, maxLen, pFormat, params );
if ( pbTruncated )
{
*pbTruncated = ( len < 0 || len >= maxLen );
}
if ( len < 0 || len >= maxLen )
{
len = maxLen;
pDest[maxLen-1] = 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( (ptrdiff_t)destBufferSize >= 0 );
AssertValidStringPtr( pDest);
AssertValidStringPtr( pSrc );
size_t len = strlen(pDest);
size_t srclen = strlen( pSrc );
if ( max_chars_to_copy <= COPY_ALL_CHARACTERS )
{
charstocopy = srclen;
}
else
{
charstocopy = (size_t)V_min( max_chars_to_copy, (int)srclen );
}
if ( len + charstocopy >= destBufferSize )
{
charstocopy = destBufferSize - len - 1;
}
if ( (int)charstocopy <= 0 )
{
return pDest;
}
ANALYZE_SUPPRESS( 6059 ); // warning C6059: : Incorrect length parameter in call to 'strncat'. Pass the number of remaining characters, not the buffer size of 'argument 1'
char *pOut = strncat( pDest, pSrc, charstocopy );
return pOut;
}
wchar_t *V_wcsncat( INOUT_Z_CAP(cchDest) wchar_t *pDest, const wchar_t *pSrc, size_t cchDest, int max_chars_to_copy )
{
size_t charstocopy = (size_t)0;
Assert( (ptrdiff_t)cchDest >= 0 );
size_t len = wcslen(pDest);
size_t srclen = wcslen( pSrc );
if ( max_chars_to_copy <= COPY_ALL_CHARACTERS )
{
charstocopy = srclen;
}
else
{
charstocopy = (size_t)V_min( max_chars_to_copy, (int)srclen );
}
if ( len + charstocopy >= cchDest )
{
charstocopy = cchDest - len - 1;
}
if ( (int)charstocopy <= 0 )
{
return pDest;
}
ANALYZE_SUPPRESS( 6059 ); // warning C6059: : Incorrect length parameter in call to 'strncat'. Pass the number of remaining characters, not the buffer size of 'argument 1'
wchar_t *pOut = wcsncat( pDest, pSrc, charstocopy );
return pOut;
}
//-----------------------------------------------------------------------------
// Purpose: Converts value into x.xx MB/ x.xx KB, x.xx bytes format, including commas
// Input : value -
// 2 -
// false -
// Output : char
//-----------------------------------------------------------------------------
#define NUM_PRETIFYMEM_BUFFERS 8
char *V_pretifymem( float value, int digitsafterdecimal /*= 2*/, bool usebinaryonek /*= false*/ )
{
static char output[ NUM_PRETIFYMEM_BUFFERS ][ 32 ];
static int current;
float onekb = usebinaryonek ? 1024.0f : 1000.0f;
float onemb = onekb * onekb;
char *out = output[ current ];
current = ( current + 1 ) & ( NUM_PRETIFYMEM_BUFFERS -1 );