]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::join_path_list
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 16 Aug 2025 19:07:29 +0000 (21:07 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 16 Aug 2025 19:10:29 +0000 (21:10 +0200)
src/ccache/util/string.cpp
src/ccache/util/string.hpp
unittest/test_util_string.cpp

index 77ba77a9abe7a19f96312b49d88d2bf72d54529c..b213cacda13edc82376619cee24b699a73dc40be 100644 (file)
@@ -31,6 +31,12 @@ namespace fs = util::filesystem;
 
 namespace {
 
+#ifdef _WIN32
+const char k_path_delimiter[] = ";";
+#else
+const char k_path_delimiter[] = ":";
+#endif
+
 template<typename T>
 std::vector<T>
 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<std::filesystem::path>& path_list)
+{
+  return join(path_list, k_path_delimiter);
+}
+
 tl::expected<double, std::string>
 parse_double(const std::string& value)
 {
@@ -515,12 +527,7 @@ split_option_with_concat_path(std::string_view string)
 std::vector<fs::path>
 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<fs::path> paths;
   std::copy(strings.cbegin(), strings.cend(), std::back_inserter(paths));
   return paths;
index e59e5287b148b1421564d0e026873bb4b8007993..e4a0de8a65cfe39f9529cb9e3b3f1d980f3e8a81 100644 (file)
@@ -99,6 +99,9 @@ template<typename T>
 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<std::filesystem::path>& path_list);
+
 // Parse a string into a double.
 //
 // Returns an error string if `value` cannot be parsed as a double.
index d78398f70f1a358d6cb11299a8d1fa6898c40bf3..efce696a4d0ecc4509aef3cce61ef363c7e4c338 100644 (file)
@@ -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));