dirname method

String dirname(
  1. String path
)

Implementation

String dirname(String path) {
  if (path.length == 0) return '.';
  var hasRoot = path[0] == '/';
  var end = -1;
  var matchedSlash = true;
  for (var i = path.length - 1; i >= 1; --i) {
    if (path[i] == '/') {
      if (!matchedSlash) {
        end = i;
        break;
      }
    } else {
      // We saw the first non-path separator
      matchedSlash = false;
    }
  }

  if (end == -1) return hasRoot ? '/' : '.';
  if (hasRoot && end == 1) return '//';
  return path.substring(0, end);
}