diff --git a/public/crtlib.c b/public/crtlib.c index e53cccd4..5fdaebfc 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -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; diff --git a/public/tests/test_fileext.c b/public/tests/test_fileext.c new file mode 100644 index 00000000..cac0021b --- /dev/null +++ b/public/tests/test_fileext.c @@ -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; +} diff --git a/public/wscript b/public/wscript index ea49da04..953f007c 100644 --- a/public/wscript +++ b/public/wscript @@ -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',