]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert usage of Util::remove_extension to std::filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 2 Jan 2024 15:25:55 +0000 (16:25 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 09:27:06 +0000 (10:27 +0100)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/core/mainoptions.cpp
unittest/test_Util.cpp

index cf27a16015fab63c6f3f0766fb49e95a39e0e823..ecd347a5f012c7882ad6adec75540937272326fb 100644 (file)
@@ -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
index 891cd75326d83c99236472063d7e3f3044925dae..b0a7d559046c6b3015fee65b6e96403dbdfd930e 100644 (file)
@@ -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
index 362d761c63858c289738d5a2993e7469cf50cd01..68ea3c685fe3a8e7f05df6d616953881fda48b91 100644 (file)
@@ -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(), '/', '#');
 
index cdb5c9ab2a7e5bbd5ee2995c2ea3a986e3259cce..8374823fed0c0acb2c909fc06edb6e96cfc3f60d 100644 (file)
@@ -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;
     }
 
index 5290a37f980da61ed9c7e0e7aad62152bf0dd4ad..432b18251405bb38dfc2315819232012b9d0e9b1 100644 (file)
@@ -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();