Browse Source

public: rewrite Q_strncpy with standard C functions, make it inlined to allow compiler remove unneeded checks

So far, passes all tests.
pull/2/head
Alibek Omarov 2 years ago
parent
commit
aee5e46516
  1. 29
      public/crtlib.c
  2. 20
      public/crtlib.h

29
public/crtlib.c

@ -134,35 +134,6 @@ size_t Q_strncat( char *dst, const char *src, size_t size ) @@ -134,35 +134,6 @@ size_t Q_strncat( char *dst, const char *src, size_t size )
return( dlen + ( s - src )); // count does not include NULL
}
size_t Q_strncpy( char *dst, const char *src, size_t size )
{
register char *d = dst;
register const char *s = src;
register size_t n = size;
if( !dst || !src || !size )
return 0;
// copy as many bytes as will fit
if( n != 0 && --n != 0 )
{
do
{
if(( *d++ = *s++ ) == 0 )
break;
} while( --n != 0 );
}
// not enough room in dst, add NULL and traverse rest of src
if( n == 0 )
{
if( size != 0 )
*d = '\0'; // NULL-terminate dst
while( *s++ );
}
return ( s - src - 1 ); // count does not include NULL
}
int Q_atoi( const char *str )
{
int val = 0;

20
public/crtlib.h

@ -65,7 +65,6 @@ size_t Q_colorstr( const char *string ); @@ -65,7 +65,6 @@ size_t Q_colorstr( const char *string );
char Q_toupper( const char in );
char Q_tolower( const char in );
size_t Q_strncat( char *dst, const char *src, size_t siz );
size_t Q_strncpy( char *dst, const char *src, size_t siz );
qboolean Q_isdigit( const char *str );
qboolean Q_isspace( const char *str );
int Q_atoi( const char *str );
@ -123,6 +122,25 @@ static inline char *Q_strstr( const char *s1, const char *s2 ) @@ -123,6 +122,25 @@ static inline char *Q_strstr( const char *s1, const char *s2 )
return unlikely( !s1 || !s2 ) ? NULL : (char*)strstr( s1, s2 );
}
// Q_strncpy is the same as strlcpy
static inline size_t Q_strncpy( char *dst, const char *src, size_t siz )
{
size_t len;
if( unlikely( !dst || !src || !siz ))
return 0;
len = strlen( src );
if( len + 1 > siz ) // check if truncate
{
memcpy( dst, src, siz - 1 );
dst[siz - 1] = 0;
}
else memcpy( dst, src, len + 1 );
return len; // count does not include NULL
}
// libc extensions, be careful
#if XASH_WIN32

Loading…
Cancel
Save