OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library path.style.windows; |
| 6 |
| 7 import '../characters.dart' as chars; |
| 8 import '../internal_style.dart'; |
| 9 import '../parsed_path.dart'; |
| 10 import '../utils.dart'; |
| 11 |
| 12 /// The style for Windows paths. |
| 13 class WindowsStyle extends InternalStyle { |
| 14 WindowsStyle(); |
| 15 |
| 16 final name = 'windows'; |
| 17 final separator = '\\'; |
| 18 final separators = const ['/', '\\']; |
| 19 |
| 20 // Deprecated properties. |
| 21 |
| 22 final separatorPattern = new RegExp(r'[/\\]'); |
| 23 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); |
| 24 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); |
| 25 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])"); |
| 26 |
| 27 bool containsSeparator(String path) => path.contains('/'); |
| 28 |
| 29 bool isSeparator(int codeUnit) => |
| 30 codeUnit == chars.SLASH || codeUnit == chars.BACKSLASH; |
| 31 |
| 32 bool needsSeparator(String path) { |
| 33 if (path.isEmpty) return false; |
| 34 return !isSeparator(path.codeUnitAt(path.length - 1)); |
| 35 } |
| 36 |
| 37 String getRoot(String path) { |
| 38 var root = _getRoot(path); |
| 39 return root == null ? getRelativeRoot(path) : root; |
| 40 } |
| 41 |
| 42 String getRelativeRoot(String path) { |
| 43 if (path.isEmpty) return null; |
| 44 if (!isSeparator(path.codeUnitAt(0))) return null; |
| 45 if (path.length > 1 && isSeparator(path.codeUnitAt(1))) return null; |
| 46 return path[0]; |
| 47 } |
| 48 |
| 49 String pathFromUri(Uri uri) { |
| 50 if (uri.scheme != '' && uri.scheme != 'file') { |
| 51 throw new ArgumentError("Uri $uri must have scheme 'file:'."); |
| 52 } |
| 53 |
| 54 var path = uri.path; |
| 55 if (uri.host == '') { |
| 56 // Drive-letter paths look like "file:///C:/path/to/file". The |
| 57 // replaceFirst removes the extra initial slash. |
| 58 if (path.startsWith('/')) path = path.replaceFirst("/", ""); |
| 59 } else { |
| 60 // Network paths look like "file://hostname/path/to/file". |
| 61 path = '\\\\${uri.host}$path'; |
| 62 } |
| 63 return Uri.decodeComponent(path.replaceAll("/", "\\")); |
| 64 } |
| 65 |
| 66 Uri absolutePathToUri(String path) { |
| 67 var parsed = new ParsedPath.parse(path, this); |
| 68 if (parsed.root.startsWith(r'\\')) { |
| 69 // Network paths become "file://server/share/path/to/file". |
| 70 |
| 71 // The root is of the form "\\server\share". We want "server" to be the |
| 72 // URI host, and "share" to be the first element of the path. |
| 73 var rootParts = parsed.root.split('\\').where((part) => part != ''); |
| 74 parsed.parts.insert(0, rootParts.last); |
| 75 |
| 76 if (parsed.hasTrailingSeparator) { |
| 77 // If the path has a trailing slash, add a single empty component so the |
| 78 // URI has a trailing slash as well. |
| 79 parsed.parts.add(""); |
| 80 } |
| 81 |
| 82 return new Uri(scheme: 'file', host: rootParts.first, |
| 83 pathSegments: parsed.parts); |
| 84 } else { |
| 85 // Drive-letter paths become "file:///C:/path/to/file". |
| 86 |
| 87 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently |
| 88 // be empty. We add an empty component so the URL constructor produces |
| 89 // "file:///C:/", with a trailing slash. We also add an empty component if |
| 90 // the URL otherwise has a trailing slash. |
| 91 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { |
| 92 parsed.parts.add(""); |
| 93 } |
| 94 |
| 95 // Get rid of the trailing "\" in "C:\" because the URI constructor will |
| 96 // add a separator on its own. |
| 97 parsed.parts.insert(0, |
| 98 parsed.root.replaceAll("/", "").replaceAll("\\", "")); |
| 99 |
| 100 return new Uri(scheme: 'file', pathSegments: parsed.parts); |
| 101 } |
| 102 } |
| 103 |
| 104 // A helper method for [getRoot] that doesn't handle relative roots. |
| 105 String _getRoot(String path) { |
| 106 if (path.length < 3) return null; |
| 107 |
| 108 // We aren't using a RegExp for this because they're slow (issue 19090). If |
| 109 // we could, we'd match against r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'. |
| 110 |
| 111 // Try roots like "C:\". |
| 112 if (isAlphabetic(path.codeUnitAt(0))) { |
| 113 if (path.codeUnitAt(1) != chars.COLON) return null; |
| 114 if (!isSeparator(path.codeUnitAt(2))) return null; |
| 115 return path.substring(0, 3); |
| 116 } |
| 117 |
| 118 // Try roots like "\\server\share". |
| 119 if (!path.startsWith('\\\\')) return null; |
| 120 |
| 121 var start = 2; |
| 122 // The server is one or more non-"\" characters. |
| 123 while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) { |
| 124 start++; |
| 125 } |
| 126 if (start == 2 || start == path.length) return null; |
| 127 |
| 128 // The share is one or more non-"\" characters. |
| 129 start += 1; |
| 130 if (path.codeUnitAt(start) == chars.BACKSLASH) return null; |
| 131 start += 1; |
| 132 while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) { |
| 133 start++; |
| 134 } |
| 135 |
| 136 return path.substring(0, start); |
| 137 } |
| 138 } |
OLD | NEW |