From: Joel Rosdahl Date: Sat, 16 Aug 2025 19:07:29 +0000 (+0200) Subject: enhance: Add util::join_path_list X-Git-Tag: v4.12~38 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=08e8e56b94490ad30c4884dadd9a070bc30a1882;p=thirdparty%2Fccache.git enhance: Add util::join_path_list --- 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));