toPosixPath function

String toPosixPath(
  1. String anyPath
)

To posix path, both / and \ are separators.

\a\b    => /a/b
/a\b    => /a/b
C:\a\b  => /C:/a/b
C:\     => /C:

A drive path written with / is returned as is (C:/x => C:/x).

Implementation

String toPosixPath(String anyPath) {
  var context = posix;
  var parts = contextPathSplit(context, anyPath);
  // Handle C:\
  if (parts.isNotEmpty) {
    var rootPart = parts[0];

    if (rootPart.endsWith(':')) {
      parts = ['/', ...parts];
    } else if (rootPart.endsWith(':\\')) {
      parts = [
        '/',
        rootPart.substring(0, rootPart.length - 1),
        ...parts.sublist(1),
      ];
    }
  }
  return context.joinAll(parts);
}