| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// A comprehensive, cross-platform path manipulation library. | 5 /// A comprehensive, cross-platform path manipulation library. |
| 6 /// | 6 /// |
| 7 /// ## Installing ## | 7 /// ## Installing ## |
| 8 /// | 8 /// |
| 9 /// Use [pub][] to install this package. Add the following to your | 9 /// Use [pub][] to install this package. Add the following to your |
| 10 /// `pubspec.yaml` file. | 10 /// `pubspec.yaml` file. |
| (...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 } | 962 } |
| 963 | 963 |
| 964 /// The style for Windows paths. | 964 /// The style for Windows paths. |
| 965 class _WindowsStyle extends Style { | 965 class _WindowsStyle extends Style { |
| 966 _WindowsStyle(); | 966 _WindowsStyle(); |
| 967 | 967 |
| 968 final name = 'windows'; | 968 final name = 'windows'; |
| 969 final separator = '\\'; | 969 final separator = '\\'; |
| 970 final separatorPattern = new RegExp(r'[/\\]'); | 970 final separatorPattern = new RegExp(r'[/\\]'); |
| 971 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); | 971 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); |
| 972 final rootPattern = new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])'); | 972 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); |
| 973 | 973 |
| 974 String pathFromUri(Uri uri) { | 974 String pathFromUri(Uri uri) { |
| 975 if (uri.scheme != '' && uri.scheme != 'file') { | 975 if (uri.scheme != '' && uri.scheme != 'file') { |
| 976 throw new ArgumentError("Uri $uri must have scheme 'file:'."); | 976 throw new ArgumentError("Uri $uri must have scheme 'file:'."); |
| 977 } | 977 } |
| 978 | 978 |
| 979 var path = uri.path; | 979 var path = uri.path; |
| 980 if (uri.host == '') { | 980 if (uri.host == '') { |
| 981 // Drive-letter paths look like "file:///C:/path/to/file". The | 981 // Drive-letter paths look like "file:///C:/path/to/file". The |
| 982 // replaceFirst removes the extra initial slash. | 982 // replaceFirst removes the extra initial slash. |
| 983 if (path.startsWith('/')) path = path.replaceFirst("/", ""); | 983 if (path.startsWith('/')) path = path.replaceFirst("/", ""); |
| 984 } else { | 984 } else { |
| 985 // Network paths look like "file://hostname/path/to/file". | 985 // Network paths look like "file://hostname/path/to/file". |
| 986 path = '\\\\${uri.host}$path'; | 986 path = '\\\\${uri.host}$path'; |
| 987 } | 987 } |
| 988 return Uri.decodeComponent(path.replaceAll("/", "\\")); | 988 return Uri.decodeComponent(path.replaceAll("/", "\\")); |
| 989 } | 989 } |
| 990 | 990 |
| 991 Uri absolutePathToUri(String path) { | 991 Uri absolutePathToUri(String path) { |
| 992 var parsed = builder._parse(path); | 992 var parsed = builder._parse(path); |
| 993 if (parsed.root == r'\\') { | 993 if (parsed.root.startsWith(r'\\')) { |
| 994 // Network paths become "file://hostname/path/to/file". | 994 // Network paths become "file://server/share/path/to/file". |
| 995 | 995 |
| 996 var host = parsed.parts.removeAt(0); | 996 // The root is of the form "\\server\share". We want "server" to be the |
| 997 // URI host, and "share" to be the first element of the path. |
| 998 var rootParts = parsed.root.split('\\').where((part) => part != ''); |
| 999 parsed.parts.insert(0, rootParts.last); |
| 997 | 1000 |
| 998 if (parsed.parts.isEmpty) { | 1001 if (parsed.hasTrailingSeparator) { |
| 999 // If the path is a bare root (e.g. "\\hostname"), [parsed.parts] will | |
| 1000 // currently be empty. We add two empty components so the URL | |
| 1001 // constructor produces "file://hostname/", with a trailing slash. | |
| 1002 parsed.parts.addAll(["", ""]); | |
| 1003 } else if (parsed.hasTrailingSeparator) { | |
| 1004 // If the path has a trailing slash, add a single empty component so the | 1002 // If the path has a trailing slash, add a single empty component so the |
| 1005 // URI has a trailing slash as well. | 1003 // URI has a trailing slash as well. |
| 1006 parsed.parts.add(""); | 1004 parsed.parts.add(""); |
| 1007 } | 1005 } |
| 1008 | 1006 |
| 1009 return new Uri(scheme: 'file', host: host, pathSegments: parsed.parts); | 1007 return new Uri(scheme: 'file', host: rootParts.first, |
| 1008 pathSegments: parsed.parts); |
| 1010 } else { | 1009 } else { |
| 1011 // Drive-letter paths become "file:///C:/path/to/file". | 1010 // Drive-letter paths become "file:///C:/path/to/file". |
| 1012 | 1011 |
| 1013 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently | 1012 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently |
| 1014 // be empty. We add an empty component so the URL constructor produces | 1013 // be empty. We add an empty component so the URL constructor produces |
| 1015 // "file:///C:/", with a trailing slash. We also add an empty component if | 1014 // "file:///C:/", with a trailing slash. We also add an empty component if |
| 1016 // the URL otherwise has a trailing slash. | 1015 // the URL otherwise has a trailing slash. |
| 1017 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { | 1016 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { |
| 1018 parsed.parts.add(""); | 1017 parsed.parts.add(""); |
| 1019 } | 1018 } |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1179 // doesn't count. | 1178 // doesn't count. |
| 1180 if (lastDot <= 0) return [file, '']; | 1179 if (lastDot <= 0) return [file, '']; |
| 1181 | 1180 |
| 1182 return [file.substring(0, lastDot), file.substring(lastDot)]; | 1181 return [file.substring(0, lastDot), file.substring(lastDot)]; |
| 1183 } | 1182 } |
| 1184 | 1183 |
| 1185 _ParsedPath clone() => new _ParsedPath( | 1184 _ParsedPath clone() => new _ParsedPath( |
| 1186 style, root, isRootRelative, | 1185 style, root, isRootRelative, |
| 1187 new List.from(parts), new List.from(separators)); | 1186 new List.from(parts), new List.from(separators)); |
| 1188 } | 1187 } |
| OLD | NEW |