From: Joel Rosdahl Date: Mon, 17 Jul 2023 13:27:07 +0000 (+0200) Subject: refactor: Move Util::to_lowercase to util X-Git-Tag: v4.9~110 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b8c704b740b7f4195ebd9d11fe4742089ca78858;p=thirdparty%2Fccache.git refactor: Move Util::to_lowercase to util --- diff --git a/src/Config.cpp b/src/Config.cpp index 38fa4432d..4d947f492 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -225,7 +225,7 @@ parse_bool(const std::string& value, // Previously any value meant true, but this was surprising to users, who // might do something like CCACHE_DISABLE=0 and expect ccache to be // enabled. - std::string lower_value = Util::to_lowercase(value); + std::string lower_value = util::to_lowercase(value); if (value == "0" || lower_value == "false" || lower_value == "disable" || lower_value == "no") { throw core::Error( diff --git a/src/Util.cpp b/src/Util.cpp index b5c811666..54c9fed30 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -358,7 +358,7 @@ is_ccache_executable(const std::string_view path) { std::string name(Util::base_name(path)); #ifdef _WIN32 - name = Util::to_lowercase(name); + name = util::to_lowercase(name); #endif return util::starts_with(name, "ccache"); } @@ -610,15 +610,6 @@ strip_ansi_csi_seqs(std::string_view string) return result; } -std::string -to_lowercase(std::string_view string) -{ - std::string result; - result.resize(string.length()); - std::transform(string.begin(), string.end(), result.begin(), tolower); - return result; -} - #ifdef HAVE_DIRENT_H void diff --git a/src/Util.hpp b/src/Util.hpp index 9a92aa055..74d29c444 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -155,9 +155,6 @@ void send_to_fd(const Context& ctx, std::string_view text, int fd); // Returns a copy of string with the specified ANSI CSI sequences removed. [[nodiscard]] std::string strip_ansi_csi_seqs(std::string_view string); -// Convert a string to lowercase. -[[nodiscard]] std::string to_lowercase(std::string_view string); - // Traverse `path` recursively (postorder, i.e. files are visited before their // parent directory). // diff --git a/src/Win32Util.cpp b/src/Win32Util.cpp index d90cde1ab..84e7c5b62 100644 --- a/src/Win32Util.cpp +++ b/src/Win32Util.cpp @@ -20,6 +20,8 @@ #include "Util.hpp" +#include + #include #include @@ -48,7 +50,7 @@ namespace Win32Util { std::string add_exe_suffix(const std::string& path) { - auto ext = Util::to_lowercase(Util::get_extension(path)); + auto ext = util::to_lowercase(Util::get_extension(path)); if (ext == ".exe" || ext == ".bat" || ext == ".sh") { return path; } else { diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 99209fd69..cecd904f1 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -119,7 +119,7 @@ color_output_possible() { const char* term_env = getenv("TERM"); return isatty(STDERR_FILENO) && term_env - && Util::to_lowercase(term_env) != "dumb"; + && util::to_lowercase(term_env) != "dumb"; } bool diff --git a/src/ccache.cpp b/src/ccache.cpp index 2292d3347..6e0ace62d 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -260,7 +260,7 @@ guess_compiler(std::string_view path) #endif const auto name = - Util::to_lowercase(Util::remove_extension(Util::base_name(compiler_path))); + util::to_lowercase(Util::remove_extension(Util::base_name(compiler_path))); if (name.find("clang-cl") != std::string_view::npos) { return CompilerType::clang_cl; } else if (name.find("clang") != std::string_view::npos) { diff --git a/src/execute.cpp b/src/execute.cpp index e2af383f4..27cb24ae1 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -82,7 +82,7 @@ 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(Util::get_extension(path)) == ".sh" && path_list) { sh = find_executable_in_path("sh.exe", path_list); } if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) { diff --git a/src/util/string.cpp b/src/util/string.cpp index a9d61d1c6..38e2f2603 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -441,4 +441,13 @@ strip_whitespace(const std::string_view string) return start < end ? std::string(start, end) : std::string(); } +std::string +to_lowercase(std::string_view string) +{ + std::string result; + result.resize(string.length()); + std::transform(string.begin(), string.end(), result.begin(), tolower); + return result; +} + } // namespace util diff --git a/src/util/string.hpp b/src/util/string.hpp index 684f059e7..dfa54c33e 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -182,6 +182,9 @@ bool starts_with(std::string_view string, std::string_view prefix); // Strip whitespace from left and right side of a string. [[nodiscard]] std::string strip_whitespace(std::string_view string); +// Convert a string to lowercase. +[[nodiscard]] std::string to_lowercase(std::string_view string); + // --- Inline implementations --- inline bool diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 31aa068f4..2fe814c4d 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -398,14 +398,6 @@ TEST_CASE("Util::remove_extension") CHECK(Util::remove_extension("/foo/bar/f.abc.txt") == "/foo/bar/f.abc"); } -TEST_CASE("Util::to_lowercase") -{ - CHECK(Util::to_lowercase("") == ""); - CHECK(Util::to_lowercase("x") == "x"); - CHECK(Util::to_lowercase("X") == "x"); - CHECK(Util::to_lowercase(" x_X@") == " x_x@"); -} - TEST_CASE("Util::traverse") { TestContext test_context; diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 4b1a2cc4e..a36a77302 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -583,4 +583,12 @@ TEST_CASE("util::strip_whitespace") CHECK(util::strip_whitespace(" x y ") == "x y"); } +TEST_CASE("util::to_lowercase") +{ + CHECK(util::to_lowercase("") == ""); + CHECK(util::to_lowercase("x") == "x"); + CHECK(util::to_lowercase("X") == "x"); + CHECK(util::to_lowercase(" x_X@") == " x_x@"); +} + TEST_SUITE_END();