From 894bcd7fc10d6f82c5582807ab3b6115ebf00d1a Mon Sep 17 00:00:00 2001 From: slipher Date: Mon, 8 Jul 2024 15:28:45 -0500 Subject: [PATCH] Fix buggy get/remove file extension - Wrong for Windows paths with both / and \ - Wrong if there is no path separator and no dot --- crnlib/crn_file_utils.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crnlib/crn_file_utils.cpp b/crnlib/crn_file_utils.cpp index f763901f..5ab2ae95 100644 --- a/crnlib/crn_file_utils.cpp +++ b/crnlib/crn_file_utils.cpp @@ -373,15 +373,16 @@ bool file_utils::full_path(dynamic_string& path) { } bool file_utils::get_extension(dynamic_string& filename) { - int sep = -1; -#ifdef WIN32 - sep = filename.find_right('\\'); + int sep = filename.find_right('/'); + +#ifdef _WIN32 + int sep2 = filename.find_right('\\'); + if (sep2 > sep) + sep = sep2; #endif - if (sep < 0) - sep = filename.find_right('/'); int dot = filename.find_right('.'); - if (dot < sep) { + if (dot <= sep) { filename.clear(); return false; } @@ -392,12 +393,13 @@ bool file_utils::get_extension(dynamic_string& filename) { } bool file_utils::remove_extension(dynamic_string& filename) { - int sep = -1; -#ifdef WIN32 - sep = filename.find_right('\\'); + int sep = filename.find_right('/'); + +#ifdef _WIN32 + int sep2 = filename.find_right('\\'); + if (sep2 > sep) + sep = sep2; #endif - if (sep < 0) - sep = filename.find_right('/'); int dot = filename.find_right('.'); if (dot < sep)