driveRoot method

String? driveRoot(
  1. String path
)

The root path hangs from: a drive (C:\), a UNC share (\\server\share\), or the plain root (\ or /). Returns null when path is relative.

Implementation

String? driveRoot(String path) {
  if (!_windows) {
    return path.startsWith('/') ? '/' : null;
  }
  final RegExpMatch? unc =
      RegExp(r'^(\\\\+[^\\]+\\+[^\\]+)\\?').firstMatch(path);
  if (unc != null) {
    return '${unc.group(1)!}\\';
  }
  final RegExpMatch? drive = RegExp(r'^[A-Za-z]:[\\/]').firstMatch(path);
  if (drive != null) {
    return drive.group(0)!;
  }
  if (path.startsWith('\\')) {
    return '\\';
  }
  if (path.startsWith('/')) {
    return '/';
  }
  return null;
}