]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add cross-platform Util::is_dir_separator helper function
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 18 Apr 2020 16:27:10 +0000 (18:27 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 18 Apr 2020 16:28:21 +0000 (18:28 +0200)
src/Util.hpp
unittest/test_Util.cpp

index f1dbd9418351d4af25ce43f3029a4f833000a44b..4379f859df55b23a34216d3ce7158ea855760520 100644 (file)
@@ -204,6 +204,18 @@ int_to_big_endian(int8_t value, uint8_t* buffer)
 // Return whether `path` is absolute.
 bool is_absolute_path(nonstd::string_view path);
 
+// Return whether `ch` is a directory separator, i.e. '/' on POSIX systems and
+// '/' or '\\' on Windows systems.
+inline bool
+is_dir_separator(char ch)
+{
+  return ch == '/'
+#ifdef _WIN32
+         || ch == '\\'
+#endif
+    ;
+}
+
 // Normalize absolute path `path`, not taking symlinks into account.
 //
 // Normalization here means syntactically removing redundant slashes and
index 3fc4d952c6a3eb9b7156e18424e85d8fc3a43c4a..b593d2b905c1586a8fcacdcf2568ff8ecdcebac3 100644 (file)
@@ -364,6 +364,17 @@ TEST_CASE("Util::is_absolute_path")
   CHECK(!Util::is_absolute_path("foo/fie"));
 }
 
+TEST_CASE("Util::is_dir_separator")
+{
+  CHECK(!Util::is_dir_separator('x'));
+  CHECK(Util::is_dir_separator('/'));
+#ifdef _WIN32
+  CHECK(Util::is_dir_separator('\\'));
+#else
+  CHECK(!Util::is_dir_separator('\\'));
+#endif
+}
+
 TEST_CASE("Util::normalize_absolute_path")
 {
   CHECK(Util::normalize_absolute_path("") == "");