sanitizePath static method

String sanitizePath(
  1. String path
)

Removes duplicated as well as trailing and leading path separators.

Implementation

static String sanitizePath(String path) {
  var str = path;
  // Remove duplicated slashes with a max depth of 255
  for (var depth = 0; depth < 255; depth++) {
    var length = str.length;
    str = str.replaceAll("//", "/");
    if (length == str.length) break;
  }
  // Remove leading and trailing slashes
  if (str.startsWith("/")) str = str.substring(1);
  if (str.endsWith("/")) str = str.substring(0, str.length - 1);
  return str;
}