Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.3 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. #include "pch_tier0.h"
  3. // This function is marked in the VPC file as always being optimized, even in debug
  4. // builds, because this gives a ~7% speedup on debug builds because this function is
  5. // used by the debug allocator.
  6. #define TOLOWERC( x ) (( ( x >= 'A' ) && ( x <= 'Z' ) )?( x + 32 ) : x )
  7. #if !defined( STATIC_LINK )
  8. #define FDECL extern "C"
  9. #else
  10. #define FDECL
  11. #endif
  12. FDECL int V_tier0_stricmp(const char *s1, const char *s2 )
  13. {
  14. // A string is always equal to itself. This optimization is
  15. // surprisingly valuable.
  16. if ( s1 == s2 )
  17. return 0;
  18. uint8 const *pS1 = ( uint8 const * ) s1;
  19. uint8 const *pS2 = ( uint8 const * ) s2;
  20. for(;;)
  21. {
  22. int c1 = *( pS1++ );
  23. int c2 = *( pS2++ );
  24. if ( c1 == c2 )
  25. {
  26. if ( !c1 ) return 0;
  27. }
  28. else
  29. {
  30. if ( ! c2 )
  31. {
  32. return c1 - c2;
  33. }
  34. c1 = TOLOWERC( c1 );
  35. c2 = TOLOWERC( c2 );
  36. if ( c1 != c2 )
  37. {
  38. return c1 - c2;
  39. }
  40. }
  41. c1 = *( pS1++ );
  42. c2 = *( pS2++ );
  43. if ( c1 == c2 )
  44. {
  45. if ( !c1 ) return 0;
  46. }
  47. else
  48. {
  49. if ( ! c2 )
  50. {
  51. return c1 - c2;
  52. }
  53. c1 = TOLOWERC( c1 );
  54. c2 = TOLOWERC( c2 );
  55. if ( c1 != c2 )
  56. {
  57. return c1 - c2;
  58. }
  59. }
  60. }
  61. }
  62. FDECL void V_tier0_strncpy( char *a, const char *b, int n )
  63. {
  64. Assert( n >= sizeof( *a ) );
  65. // NOTE: Never never use strncpy! Here's what it actually does, which is not what we want!
  66. // (from MSDN)
  67. // The strncpy function copies the initial count characters of strSource to strDest
  68. // and returns strDest. If count is less than or equal to the length of strSource,
  69. // a null character is not appended automatically to the copied string. If count
  70. // is greater than the length of strSource, the destination string is padded with
  71. // null characters up to length count. The behavior of strncpy is undefined
  72. // if the source and destination strings overlap.
  73. // strncpy( pDest, pSrc, maxLen );
  74. char *pLast = a + n - 1;
  75. while ( (a < pLast) && (*b != 0) )
  76. {
  77. *a = *b;
  78. ++a; ++b;
  79. }
  80. *a = 0;
  81. }
  82. FDECL char *V_tier0_strncat( char *pDest, const char *pSrc, int destBufferSize, int max_chars_to_copy )
  83. {
  84. int charstocopy = 0;
  85. Assert( destBufferSize >= 0 );
  86. int len = (int)strlen(pDest);
  87. int srclen = (int)strlen( pSrc );
  88. if ( max_chars_to_copy <= -1 )
  89. {
  90. charstocopy = srclen;
  91. }
  92. else
  93. {
  94. charstocopy = Min( max_chars_to_copy, (int)srclen );
  95. }
  96. if ( len + charstocopy >= destBufferSize )
  97. {
  98. charstocopy = destBufferSize - len - 1;
  99. }
  100. // charstocopy can end up negative if you fill a buffer and then pass in a smaller
  101. // buffer size. Yes, this actually happens.
  102. // Cast to ptrdiff_t is necessary in order to check for negative (size_t is unsigned)
  103. if ( charstocopy <= 0 )
  104. {
  105. return pDest;
  106. }
  107. 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'
  108. char *pOut = strncat( pDest, pSrc, charstocopy );
  109. return pOut;
  110. }
  111. FDECL int V_tier0_vsnprintf( char *a, int n, const char *f, va_list l )
  112. {
  113. int len = _vsnprintf( a, n, f, l );
  114. if ( ( len < 0 ) ||
  115. ( n > 0 && len >= n ) )
  116. {
  117. len = n - 1;
  118. a[n - 1] = 0;
  119. }
  120. return len;
  121. }
  122. FDECL int V_tier0_snprintf( char *a, int n, const char *f, ... )
  123. {
  124. va_list l;
  125. va_start( l, f );
  126. int len = V_tier0_vsnprintf( a, n, f, l );
  127. va_end( l );
  128. return len;
  129. }