diff --git a/engine/client/titles.c b/engine/client/titles.c index fd6f5ff3..1a0746dd 100644 --- a/engine/client/titles.c +++ b/engine/client/titles.c @@ -228,7 +228,7 @@ void CL_TextMessageParse( byte *pMemFile, int fileSize ) while( COM_MemFgets( pMemFile, fileSize, &filePos, buf, 512 ) != NULL ) { - COM_TrimSpace( buf, trim ); + COM_TrimSpace( trim, buf, sizeof( trim )); switch( mode ) { diff --git a/engine/common/common.c b/engine/common/common.c index d2cd07bd..01ae5364 100644 --- a/engine/common/common.c +++ b/engine/common/common.c @@ -496,21 +496,6 @@ uint LZSS_Decompress( const byte *pInput, byte *pOutput, size_t input_len, size_ return totalBytes; } -/* -============== -COM_IsWhiteSpace - -interpret symbol as whitespace -============== -*/ - -static int COM_IsWhiteSpace( char space ) -{ - if( space == ' ' || space == '\t' || space == '\r' || space == '\n' ) - return 1; - return 0; -} - /* ================ COM_ParseVector @@ -572,39 +557,6 @@ int GAME_EXPORT COM_FileSize( const char *filename ) return FS_FileSize( filename, false ); } -/* -============= -COM_TrimSpace - -trims all whitespace from the front -and end of a string -============= -*/ -void COM_TrimSpace( const char *source, char *dest ) -{ - int start, end, length; - - start = 0; - end = Q_strlen( source ); - - while( source[start] && COM_IsWhiteSpace( source[start] )) - start++; - end--; - - while( end > 0 && COM_IsWhiteSpace( source[end] )) - end--; - end++; - - length = end - start; - - if( length > 0 ) - memcpy( dest, source + start, length ); - else length = 0; - - // terminate the dest string - dest[length] = 0; -} - /* ================== COM_Nibble diff --git a/engine/common/common.h b/engine/common/common.h index 844f2a8e..6bb9d91e 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -638,7 +638,6 @@ cvar_t *pfnCVarGetPointer( const char *szVarName ); int pfnDrawConsoleString( int x, int y, char *string ); void pfnDrawSetTextColor( float r, float g, float b ); void pfnDrawConsoleStringLen( const char *pText, int *length, int *height ); -void COM_TrimSpace( const char *source, char *dest ); void pfnGetModelBounds( model_t *mod, float *mins, float *maxs ); int COM_CheckParm( char *parm, char **ppnext ); int pfnGetModelType( model_t *mod ); diff --git a/engine/server/sv_client.c b/engine/server/sv_client.c index b10ac10f..454e160d 100644 --- a/engine/server/sv_client.c +++ b/engine/server/sv_client.c @@ -1833,7 +1833,7 @@ static void SV_UserinfoChanged( sv_client_t *cl ) val = Info_ValueForKey( cl->userinfo, "name" ); Q_strncpy( name2, val, sizeof( name2 )); - COM_TrimSpace( name2, name1 ); + COM_TrimSpace( name1, name2, sizeof( name1 )); if( !Q_stricmp( name1, "console" )) { diff --git a/public/crtlib.c b/public/crtlib.c index 751d3924..0e4a25bc 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -887,3 +887,24 @@ int matchpattern_with_separator( const char *in, const char *pattern, qboolean c return 1; // success } +void COM_TrimSpace( char *dst, const char *src, size_t size ) +{ + if( !dst || !src || !size ) + return; + + // remove spaces from the start + for( ; *src && isspace( *src ); src++ ); + + int len = Q_strlen( src ); + + // remove spaces from the end + for( ; len > 0 && isspace( src[len - 1] ); len-- ); + + if( len > 0 ) + { + // + 1 to fit null terminator in strlcpy + Q_strncpy( dst, src, Q_min( size, len + 1 )); + } + else + dst[0] = 0; +} diff --git a/public/crtlib.h b/public/crtlib.h index 3bc176a1..94bc9b4d 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -103,6 +103,7 @@ const char *COM_FileWithoutPath( const char *in ); void COM_StripExtension( char *path ); void COM_RemoveLineFeed( char *str, size_t bufsize ); void COM_PathSlashFix( char *path ); +void COM_TrimSpace( char *dst, const char *src, size_t size ); // returns true on empty or NULL string, false otherwise #define COM_StringEmpty( string ) (( string )[0] ? false : true ) diff --git a/public/tests/test_strings.c b/public/tests/test_strings.c index 1d99b1e5..f3a36472 100644 --- a/public/tests/test_strings.c +++ b/public/tests/test_strings.c @@ -70,6 +70,35 @@ static int Test_FixSlashes( void ) return 0; } +static int Test_TrimSpace( void ) +{ + string s; + + // test it removes white space from both sides + COM_TrimSpace( s, " \txash is cool \n\t ", sizeof( s )); + if( Q_strcmp( s, "xash is cool" )) + return 1; + + // check it truncates + COM_TrimSpace( s, "\t\t\txashxashxash", 5 ); + if( Q_strcmp( s, "xash" )) + return 2; + + COM_TrimSpace( s, " ", sizeof( s )); + if( Q_strcmp( s, "" )) + return 3; + + COM_TrimSpace( s, "s ", sizeof( s )); + if( Q_strcmp( s, "s" )) + return 4; + + COM_TrimSpace( s, " a", sizeof( s )); + if( Q_strcmp( s, "a" )) + return 5; + + return 0; +} + int main( void ) { int ret = Test_Strcpycatcmp(); @@ -84,6 +113,11 @@ int main( void ) ret = Test_FixSlashes(); + if( ret > 0 ) + return ret + 32; + + ret = Test_TrimSpace(); + if( ret > 0 ) return ret + 48;