mirror of
https://github.com/FWGS/xash3d-fwgs
synced 2024-11-22 09:56:22 +01:00
public: optimize COM_FileExtension
This commit is contained in:
parent
31a86d8af2
commit
721c8043f1
@ -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;
|
||||
|
25
public/tests/test_fileext.c
Normal file
25
public/tests/test_fileext.c
Normal 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;
|
||||
}
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user