OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library path.style.windows; | 5 library path.style.windows; |
6 | 6 |
| 7 import '../characters.dart' as chars; |
| 8 import '../internal_style.dart'; |
7 import '../parsed_path.dart'; | 9 import '../parsed_path.dart'; |
8 import '../internal_style.dart'; | 10 import '../utils.dart'; |
9 | 11 |
10 /// The style for Windows paths. | 12 /// The style for Windows paths. |
11 class WindowsStyle extends InternalStyle { | 13 class WindowsStyle extends InternalStyle { |
12 WindowsStyle(); | 14 WindowsStyle(); |
13 | 15 |
14 final name = 'windows'; | 16 final name = 'windows'; |
15 final separator = '\\'; | 17 final separator = '\\'; |
| 18 final separators = const ['/', '\\']; |
| 19 |
| 20 // Deprecated properties. |
| 21 |
16 final separatorPattern = new RegExp(r'[/\\]'); | 22 final separatorPattern = new RegExp(r'[/\\]'); |
17 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); | 23 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); |
18 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); | 24 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); |
19 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])"); | 25 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])"); |
20 | 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 |
21 String pathFromUri(Uri uri) { | 49 String pathFromUri(Uri uri) { |
22 if (uri.scheme != '' && uri.scheme != 'file') { | 50 if (uri.scheme != '' && uri.scheme != 'file') { |
23 throw new ArgumentError("Uri $uri must have scheme 'file:'."); | 51 throw new ArgumentError("Uri $uri must have scheme 'file:'."); |
24 } | 52 } |
25 | 53 |
26 var path = uri.path; | 54 var path = uri.path; |
27 if (uri.host == '') { | 55 if (uri.host == '') { |
28 // Drive-letter paths look like "file:///C:/path/to/file". The | 56 // Drive-letter paths look like "file:///C:/path/to/file". The |
29 // replaceFirst removes the extra initial slash. | 57 // replaceFirst removes the extra initial slash. |
30 if (path.startsWith('/')) path = path.replaceFirst("/", ""); | 58 if (path.startsWith('/')) path = path.replaceFirst("/", ""); |
(...skipping 28 matching lines...) Expand all Loading... |
59 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently | 87 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently |
60 // be empty. We add an empty component so the URL constructor produces | 88 // be empty. We add an empty component so the URL constructor produces |
61 // "file:///C:/", with a trailing slash. We also add an empty component if | 89 // "file:///C:/", with a trailing slash. We also add an empty component if |
62 // the URL otherwise has a trailing slash. | 90 // the URL otherwise has a trailing slash. |
63 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { | 91 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { |
64 parsed.parts.add(""); | 92 parsed.parts.add(""); |
65 } | 93 } |
66 | 94 |
67 // Get rid of the trailing "\" in "C:\" because the URI constructor will | 95 // Get rid of the trailing "\" in "C:\" because the URI constructor will |
68 // add a separator on its own. | 96 // add a separator on its own. |
69 parsed.parts.insert(0, parsed.root.replaceAll(separatorPattern, "")); | 97 parsed.parts.insert(0, |
| 98 parsed.root.replaceAll("/", "").replaceAll("\\", "")); |
70 | 99 |
71 return new Uri(scheme: 'file', pathSegments: parsed.parts); | 100 return new Uri(scheme: 'file', pathSegments: parsed.parts); |
72 } | 101 } |
73 } | 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 } |
74 } | 138 } |
OLD | NEW |