]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert usage of Util::get_extension to std::filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 1 Jan 2024 12:37:53 +0000 (13:37 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 08:57:37 +0000 (09:57 +0100)
src/Util.cpp
src/Util.hpp
src/argprocessing.cpp
src/execute.cpp
src/language.cpp
src/util/path.cpp
unittest/test_Util.cpp

index 12e16a8b86550563f6d0c64786102d22b14dcbd0..438c6068c99ead7d3e5be557cedc822bc49581d4 100644 (file)
@@ -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
index 6ebe6d85dcb3e8b6af08220daf3736a591832188..772899b9f332557bcc3482f1215cad0ac1cc4d1b 100644 (file)
@@ -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
index f6b80e0dc0fefc620f88e3436e863e9aff64ad03..1e8ca3622dee75dd682f1d2f66d452463bf486a4 100644 (file)
@@ -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
index 776bfbdced819b41c06e14b392856992e4ef8df4..efa89558c3d0695f1b4bf6e2e7100d53b50c7c37 100644 (file)
@@ -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")) {
index 4a4635ae06f20adccfb4502af396678a53de4087..f58de52dfd349461086797155f0ad335273c1c3d 100644 (file)
@@ -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 <util/filesystem.hpp>
+
+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";
index bf2845c9db041584948c3ba3b04a07c98081d02a..11773cf9cf4551b73ea5a8ed5a8461f56deb3297 100644 (file)
@@ -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 {
index d01f960e6b9ffe8017bde45916107206af185be6..cb0f15272ee263cb74c85f2e0b308f7acf7cbe67 100644 (file)
@@ -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("/") == "/");