diff --git a/SystemTools.cxx b/SystemTools.cxx
index 39846c0f3e10f2322f9768569b1bee092baa5684..dcf2b6601205c47618c2a960e49d947473da04b9 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -2087,38 +2087,21 @@ void SystemTools::ConvertToUnixSlashes(std::string& path)
   }
 
   char const* pathCString = path.c_str();
-  bool hasDoubleSlash = false;
 #ifdef __VMS
   ConvertVMSToUnix(path);
 #else
-  char const* pos0 = pathCString;
-  for (std::string::size_type pos = 0; *pos0; ++pos) {
-    if (*pos0 == '\\') {
-      path[pos] = '/';
-    }
-
-    // Also, reuse the loop to check for slash followed by another slash
-    if (!hasDoubleSlash && *(pos0 + 1) == '/' && *(pos0 + 2) == '/') {
-#  ifdef _WIN32
-      // However, on windows if the first characters are both slashes,
-      // then keep them that way, so that network paths can be handled.
-      if (pos > 0) {
-        hasDoubleSlash = true;
-      }
-#  else
-      hasDoubleSlash = true;
-#  endif
-    }
-
-    pos0++;
-  }
-
-  if (hasDoubleSlash) {
-    SystemTools::ReplaceString(path, "//", "/");
-  }
+  // replace backslashes
+  std::replace(path.begin(), path.end(), '\\', '/');
+
+  // collapse repeated slashes, except exactly two leading slashes are
+  // meaningful and must be preserved.
+  bool hasDoubleSlash = path[0] == '/' && path[1] == '/' && path[2] != '/';
+  auto uniqueEnd = std::unique(
+    path.begin() + hasDoubleSlash, path.end(),
+    [](char c1, char c2) -> bool { return c1 == '/' && c1 == c2; });
+  path.erase(uniqueEnd, path.end());
 #endif
 
-  // remove any trailing slash
   // if there is a tilda ~ then replace it with HOME
   pathCString = path.c_str();
   if (pathCString[0] == '~' &&
@@ -2140,13 +2123,11 @@ void SystemTools::ConvertToUnixSlashes(std::string& path)
     }
   }
 #endif
-  // remove trailing slash if the path is more than
-  // a single /
-  pathCString = path.c_str();
+  // remove trailing slash, but preserve the root slash and the slash
+  // after windows drive letter (c:/).
   size_t size = path.size();
   if (size > 1 && path.back() == '/') {
-    // if it is c:/ then do not remove the trailing slash
-    if (!((size == 3 && pathCString[1] == ':'))) {
+    if (!(size == 3 && path[1] == ':') && path[size - 2] != '/') {
       path.resize(size - 1);
     }
   }
diff --git a/testSystemTools.cxx b/testSystemTools.cxx
index 055ffd1869284c110a15d7fcdf7075058e8ae3e6..07044587c4699fcb9abc6dd85c054c41a67d0874 100644
--- a/testSystemTools.cxx
+++ b/testSystemTools.cxx
@@ -53,6 +53,20 @@ static char const* toUnixPaths[][2] = {
   { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
   { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
   { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo/ cal/bin/pa/ sswd" },
+  { "\\", "/" },
+  { "/", "/" },
+  { "\\\\", "//" },
+  { "//", "//" },
+  { "\\\\\\", "/" },
+  { "///", "/" },
+  { "C:\\", "C:/" },
+  { "C:\\\\", "C:/" },
+  { "C:\\\\\\", "C:/" },
+  { "\\\\UNC\\path", "//UNC/path" },
+  { "//UNC/path", "//UNC/path" },
+  { "\\\\\\triple\\\\back\\\\\\slash\\\\\\", "/triple/back/slash" },
+  { "///triple//back///slash///", "/triple/back/slash" },
+  { "///////ex treme/////////", "/ex treme" },
   { nullptr, nullptr }
 };