From 08e8e56b94490ad30c4884dadd9a070bc30a1882 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 16 Aug 2025 21:07:29 +0200 Subject: [PATCH] enhance: Add util::join_path_list --- src/ccache/util/string.cpp | 19 +++++++++++++------ src/ccache/util/string.hpp | 3 +++ unittest/test_util_string.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ccache/util/string.cpp b/src/ccache/util/string.cpp index 77ba77a9..b213cacd 100644 --- a/src/ccache/util/string.cpp +++ b/src/ccache/util/string.cpp @@ -31,6 +31,12 @@ namespace fs = util::filesystem; namespace { +#ifdef _WIN32 +const char k_path_delimiter[] = ";"; +#else +const char k_path_delimiter[] = ":"; +#endif + template std::vector split_into(std::string_view string, @@ -207,6 +213,12 @@ format_iso8601_timestamp(const TimePoint& time, TimeZone time_zone) return timestamp; } +std::string +join_path_list(const std::vector& path_list) +{ + return join(path_list, k_path_delimiter); +} + tl::expected parse_double(const std::string& value) { @@ -515,12 +527,7 @@ split_option_with_concat_path(std::string_view string) std::vector split_path_list(std::string_view path_list) { -#ifdef _WIN32 - const char path_delimiter[] = ";"; -#else - const char path_delimiter[] = ":"; -#endif - auto strings = split_into_views(path_list, path_delimiter); + auto strings = split_into_views(path_list, k_path_delimiter); std::vector paths; std::copy(strings.cbegin(), strings.cend(), std::back_inserter(paths)); return paths; diff --git a/src/ccache/util/string.hpp b/src/ccache/util/string.hpp index e59e5287..e4a0de8a 100644 --- a/src/ccache/util/string.hpp +++ b/src/ccache/util/string.hpp @@ -99,6 +99,9 @@ template std::string join(const T& begin, const T& end, const std::string_view delimiter); +// Join paths into a string with system-dependent delimiter. +std::string join_path_list(const std::vector& path_list); + // Parse a string into a double. // // Returns an error string if `value` cannot be parsed as a double. diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index d78398f7..efce696a 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -293,6 +293,16 @@ TEST_CASE("util::join") } } +TEST_CASE("util::join_path_list") +{ + CHECK(util::join_path_list({}).empty()); +#ifdef _WIN32 + CHECK(util::join_path_list({"a", "b"}) == "a;b"); +#else + CHECK(util::join_path_list({"a", "b"}) == "a:b"); +#endif +} + TEST_CASE("util::parse_double") { CHECK(*util::parse_double("0") == doctest::Approx(0.0)); -- 2.47.3