public: reimplement COM_TrimSpace, add it to crtlib, add tests for it

This commit is contained in:
Alibek Omarov
2026-02-28 00:05:35 +05:00
parent 62c547570a
commit 9a285986c4
7 changed files with 58 additions and 51 deletions

View File

@@ -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 )
{

View File

@@ -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

View File

@@ -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 );

View File

@@ -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" ))
{

View File

@@ -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;
}

View File

@@ -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 )

View File

@@ -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;