From: Joel Rosdahl Date: Tue, 2 Jan 2024 15:25:55 +0000 (+0100) Subject: refactor: Convert usage of Util::remove_extension to std::filesystem X-Git-Tag: v4.10~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=445f0cf51930a9e14aa2f5a67948c6c7c4a6b086;p=thirdparty%2Fccache.git refactor: Convert usage of Util::remove_extension to std::filesystem --- diff --git a/src/Util.cpp b/src/Util.cpp index cf27a1601..ecd347a5f 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -298,11 +298,4 @@ normalize_concrete_absolute_path(const std::string& path) : path; } -std::string_view -remove_extension(std::string_view path) -{ - return path.substr( - 0, path.length() - fs::path(path).extension().native().length()); -} - } // namespace Util diff --git a/src/Util.hpp b/src/Util.hpp index 891cd7532..b0a7d5590 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -85,8 +85,4 @@ std::string normalize_abstract_absolute_path(std::string_view path); // normalized result doesn't resolve to the same file system entry as `path`. std::string normalize_concrete_absolute_path(const std::string& path); -// Return a view into `path` containing the given path without the filename -// extension as determined by `get_extension()`. -std::string_view remove_extension(std::string_view path); - } // namespace Util diff --git a/src/ccache.cpp b/src/ccache.cpp index 362d761c6..68ea3c685 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1548,9 +1548,8 @@ hash_common_info(const Context& ctx, const auto output_dir = fs::path(ctx.args_info.output_obj).parent_path(); dir = fs::canonical(output_dir).value_or(output_dir).string(); } - std::string_view stem = Util::remove_extension( - fs::path(ctx.args_info.output_obj).filename().string()); - std::string gcda_path = FMT("{}/{}.gcda", dir, stem); + std::string gcda_path = + FMT("{}/{}.gcda", dir, fs::path(ctx.args_info.output_obj).stem()); LOG("Hashing coverage path {}", gcda_path); hash.hash_delimiter("gcda"); hash.hash(gcda_path); @@ -1888,7 +1887,8 @@ static bool hash_profile_data_file(const Context& ctx, Hash& hash) { const std::string& profile_path = ctx.args_info.profile_path; - std::string_view base_name = Util::remove_extension(ctx.args_info.output_obj); + const std::string base_name = + fs::path(ctx.args_info.output_obj).replace_extension("").string(); std::string hashified_cwd = ctx.apparent_cwd; std::replace(hashified_cwd.begin(), hashified_cwd.end(), '/', '#'); diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index cdb5c9ab2..8374823fe 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -757,11 +757,7 @@ process_main_options(int argc, const char* const* argv) case 'V': // --version { - std::string name = fs::path(argv[0]).filename().string(); -#ifdef _WIN32 - name = Util::remove_extension(name); -#endif - PRINT_RAW(stdout, get_version_text(name)); + PRINT_RAW(stdout, get_version_text(fs::path(argv[0]).stem().string())); break; } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 5290a37f9..432b18251 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -230,19 +230,4 @@ TEST_CASE("Util::normalize_concrete_absolute_path") #endif } -TEST_CASE("Util::remove_extension") -{ - CHECK(Util::remove_extension("") == ""); - CHECK(Util::remove_extension(".") == "."); - CHECK(Util::remove_extension("...") == ".."); - CHECK(Util::remove_extension("foo") == "foo"); - CHECK(Util::remove_extension("/") == "/"); - CHECK(Util::remove_extension("/foo") == "/foo"); - CHECK(Util::remove_extension("/foo/bar/f") == "/foo/bar/f"); - CHECK(Util::remove_extension("f.txt") == "f"); - CHECK(Util::remove_extension("f.abc.txt") == "f.abc"); - CHECK(Util::remove_extension("/foo/bar/f.txt") == "/foo/bar/f"); - CHECK(Util::remove_extension("/foo/bar/f.abc.txt") == "/foo/bar/f.abc"); -} - TEST_SUITE_END();