2
0
mirror of https://github.com/FWGS/xash3d-fwgs synced 2024-11-22 01:45:19 +01:00

public: optimize COM_FileExtension

This commit is contained in:
Alibek Omarov 2024-11-17 06:20:32 +03:00
parent 31a86d8af2
commit 721c8043f1
3 changed files with 33 additions and 13 deletions

View File

@ -528,22 +528,16 @@ COM_FileExtension
*/
const char *COM_FileExtension( const char *in )
{
const char *separator, *backslash, *colon, *dot;
separator = Q_strrchr( in, '/' );
backslash = Q_strrchr( in, '\\' );
if( !separator || separator < backslash )
separator = backslash;
colon = Q_strrchr( in, ':' );
if( !separator || separator < colon )
separator = colon;
const char *dot;
dot = Q_strrchr( in, '.' );
if( dot == NULL || ( separator && ( dot < separator )))
// quickly exit if there is no dot at all
if( dot == NULL )
return "";
// if there are any of these special symbols after the dot, the file has no extension
if( Q_strpbrk( dot + 1, "\\/:" ))
return "";
return dot + 1;

View File

@ -0,0 +1,25 @@
#include "crtlib.h"
int main( int argc, char **argv )
{
int i;
const char *strings[] =
{
"test.txt", "txt",
"path/to/file.wad", "wad",
"noext", "",
"dir/", "",
"dir.pk3dir/", "",
"https.proto://is_this_an_url?", "",
"inside.wad/AAATRIGGER", "",
"c:/games/gamedir/liblist.cum", "cum",
};
for( i = 0; i < sizeof( strings ) / sizeof( strings[0] ); i += 2 )
{
if( Q_strcmp( COM_FileExtension( strings[i] ), strings[i + 1] ))
return i;
}
return 0;
}

View File

@ -134,6 +134,7 @@ def build(bld):
'strings': 'tests/test_strings.c',
'build': 'tests/test_build.c',
'filebase': 'tests/test_filebase.c',
'fileext': 'tests/test_fileext.c',
'efp': 'tests/test_efp.c',
'atoi': 'tests/test_atoi.c',
'parsefile': 'tests/test_parsefile.c',