From: Joel Rosdahl Date: Mon, 1 Jan 2024 12:37:53 +0000 (+0100) Subject: refactor: Convert usage of Util::get_extension to std::filesystem X-Git-Tag: v4.10~139 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=18449498106ca2604b52843b8a3e98b65456d889;p=thirdparty%2Fccache.git refactor: Convert usage of Util::get_extension to std::filesystem --- diff --git a/src/Util.cpp b/src/Util.cpp index 12e16a8b8..438c6068c 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -108,26 +108,6 @@ dir_name(std::string_view path) } } -std::string_view -get_extension(std::string_view path) -{ -#ifndef _WIN32 - const char stop_at_chars[] = "./"; -#else - const char stop_at_chars[] = "./\\"; -#endif - size_t pos = path.find_last_of(stop_at_chars); - if (pos == std::string_view::npos || path.at(pos) == '/') { - return {}; -#ifdef _WIN32 - } else if (path.at(pos) == '\\') { - return {}; -#endif - } else { - return path.substr(pos); - } -} - std::string get_relative_path(std::string_view dir, std::string_view path) { @@ -353,7 +333,8 @@ normalize_concrete_absolute_path(const std::string& path) std::string_view remove_extension(std::string_view path) { - return path.substr(0, path.length() - get_extension(path).length()); + 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 6ebe6d85d..772899b9f 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2023 Joel Rosdahl and other contributors +// Copyright (C) 2019-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -46,10 +46,6 @@ size_t common_dir_prefix_length(std::string_view dir, std::string_view path); // Get directory name of path. std::string_view dir_name(std::string_view path); -// Return the file extension (including the dot) as a view into `path`. If -// `path` has no file extension, an empty string_view is returned. -std::string_view get_extension(std::string_view path); - // Compute a relative path from `dir` (an absolute path to a directory) to // `path` (an absolute path). Assumes that both `dir` and `path` are normalized. // The algorithm does *not* follow symlinks, so the result may not actually diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index f6b80e0dc..1e8ca3622 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -158,7 +158,7 @@ detect_pch(const std::string& option, } } else if (option == "-Fp") { std::string file = arg; - if (Util::get_extension(file).empty()) { + if (!fs::path(file).has_extension()) { file += ".pch"; } if (DirEntry(file).is_regular_file()) { @@ -1633,9 +1633,9 @@ process_args(Context& ctx) bool is_precompiled_header(std::string_view path) { - std::string_view ext = Util::get_extension(path); + fs::path ext = fs::path(path).extension(); return ext == ".gch" || ext == ".pch" || ext == ".pth" - || Util::get_extension(Util::dir_name(path)) == ".gch"; + || fs::path(path).parent_path().extension() == ".gch"; } bool diff --git a/src/execute.cpp b/src/execute.cpp index 776bfbdce..efa89558c 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2002 Andrew Tridgell -// Copyright (C) 2011-2023 Joel Rosdahl and other contributors +// Copyright (C) 2011-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -87,7 +87,8 @@ win32getshell(const std::string& path) { const char* path_list = getenv("PATH"); std::string sh; - if (util::to_lowercase(Util::get_extension(path)) == ".sh" && path_list) { + if (util::to_lowercase(fs::path(path).extension().string()) == ".sh" + && path_list) { sh = find_executable_in_path("sh.exe", path_list).string(); } if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) { diff --git a/src/language.cpp b/src/language.cpp index 4a4635ae0..f58de52df 100644 --- a/src/language.cpp +++ b/src/language.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2023 Joel Rosdahl and other contributors +// Copyright (C) 2010-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,7 +18,9 @@ #include "language.hpp" -#include "Util.hpp" +#include + +namespace fs = util::filesystem; namespace { @@ -105,7 +107,7 @@ const struct std::string language_for_file(const std::string& fname, CompilerType compiler_type) { - auto ext = Util::get_extension(fname); + const auto ext = fs::path(fname).extension(); if (ext == ".cu" && compiler_type == CompilerType::clang) { // Special case: Clang maps .cu to cuda. return "cuda"; diff --git a/src/util/path.cpp b/src/util/path.cpp index bf2845c9d..11773cf9c 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2023 Joel Rosdahl and other contributors +// Copyright (C) 2021-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -51,7 +51,7 @@ actual_cwd() std::string add_exe_suffix(const std::string& program) { - auto ext = util::to_lowercase(Util::get_extension(program)); + std::string ext = util::to_lowercase(fs::path(program).extension().string()); if (ext == ".exe" || ext == ".bat" || ext == ".sh") { return program; } else { diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index d01f960e6..cb0f15272 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2023 Joel Rosdahl and other contributors +// Copyright (C) 2019-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -54,7 +54,7 @@ TEST_CASE("Util::change_extension") CHECK(Util::change_extension("x", "") == "x"); CHECK(Util::change_extension("", "x") == "x"); CHECK(Util::change_extension("", ".") == "."); - CHECK(Util::change_extension(".", "") == ""); + CHECK(Util::change_extension(".", "") == "."); CHECK(Util::change_extension("...", "x") == "..x"); CHECK(Util::change_extension("abc", "def") == "abcdef"); CHECK(Util::change_extension("dot.", ".dot") == "dot.dot"); @@ -101,21 +101,6 @@ TEST_CASE("Util::dir_name") #endif } -TEST_CASE("Util::get_extension") -{ - CHECK(Util::get_extension("") == ""); - CHECK(Util::get_extension(".") == "."); - CHECK(Util::get_extension("...") == "."); - CHECK(Util::get_extension("foo") == ""); - CHECK(Util::get_extension("/") == ""); - CHECK(Util::get_extension("/foo") == ""); - CHECK(Util::get_extension("/foo/bar/f") == ""); - CHECK(Util::get_extension("f.txt") == ".txt"); - CHECK(Util::get_extension("f.abc.txt") == ".txt"); - CHECK(Util::get_extension("/foo/bar/f.txt") == ".txt"); - CHECK(Util::get_extension("/foo/bar/f.abc.txt") == ".txt"); -} - TEST_CASE("Util::get_relative_path") { #ifdef _WIN32 @@ -283,7 +268,7 @@ TEST_CASE("Util::normalize_concrete_absolute_path") TEST_CASE("Util::remove_extension") { CHECK(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("/") == "/");