}
}
-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)
{
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
-// 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.
//
// 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
}
} 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()) {
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
// 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.
//
{
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")) {
-// 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.
//
#include "language.hpp"
-#include "Util.hpp"
+#include <util/filesystem.hpp>
+
+namespace fs = util::filesystem;
namespace {
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";
-// 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.
//
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 {
-// 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.
//
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");
#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
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("/") == "/");