| 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 int rootLength(String path) { | |
| 38 if (path.isEmpty) return 0; | |
| 39 if (path.codeUnitAt(0) == chars.SLASH) return 1; | |
| 40 if (path.codeUnitAt(0) == chars.BACKSLASH) { | |
| 41 if (path.length < 2 || path.codeUnitAt(1) != chars.BACKSLASH) return 1; | |
| 42 // The path is a network share. Search for up to two '\'s, as they are | |
| 43 // the server and share - and part of the root part. | |
| 44 var index = path.indexOf('\\', 2); | |
| 45 if (index > 0) { | |
| 46 index = path.indexOf('\\', index + 1); | |
| 47 if (index > 0) return index; | |
| 48 } | |
| 49 return path.length; | |
| 50 } | |
| 51 // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's | |
| 52 // a root part. | |
| 53 if (path.length < 3) return 0; | |
| 54 // Check for the letter. | |
| 55 if (!isAlphabetic(path.codeUnitAt(0))) return 0; | |
| 56 // Check for the ':'. | |
| 57 if (path.codeUnitAt(1) != chars.COLON) return 0; | |
| 58 // Check for either '/' or '\'. | |
| 59 if (!isSeparator(path.codeUnitAt(2))) return 0; | |
| 60 return 3; | |
| 61 } | |
| 62 | |
| 63 bool isRootRelative(String path) => rootLength(path) == 1; | |
| 64 | |
| 65 String getRelativeRoot(String path) { | |
| 66 var length = rootLength(path); | |
| 67 if (length == 1) return path[0]; | |
| 68 return null; | |
| 69 } | |
| 70 | |
| 71 String pathFromUri(Uri uri) { | |
| 72 if (uri.scheme != '' && uri.scheme != 'file') { | |
| 73 throw new ArgumentError("Uri $uri must have scheme 'file:'."); | |
| 74 } | |
| 75 | |
| 76 var path = uri.path; | |
| 77 if (uri.host == '') { | |
| 78 // Drive-letter paths look like "file:///C:/path/to/file". The | |
| 79 // replaceFirst removes the extra initial slash. | |
| 80 if (path.startsWith('/')) path = path.replaceFirst("/", ""); | |
| 81 } else { | |
| 82 // Network paths look like "file://hostname/path/to/file". | |
| 83 path = '\\\\${uri.host}$path'; | |
| 84 } | |
| 85 return Uri.decodeComponent(path.replaceAll("/", "\\")); | |
| 86 } | |
| 87 | |
| 88 Uri absolutePathToUri(String path) { | |
| 89 var parsed = new ParsedPath.parse(path, this); | |
| 90 if (parsed.root.startsWith(r'\\')) { | |
| 91 // Network paths become "file://server/share/path/to/file". | |
| 92 | |
| 93 // The root is of the form "\\server\share". We want "server" to be the | |
| 94 // URI host, and "share" to be the first element of the path. | |
| 95 var rootParts = parsed.root.split('\\').where((part) => part != ''); | |
| 96 parsed.parts.insert(0, rootParts.last); | |
| 97 | |
| 98 if (parsed.hasTrailingSeparator) { | |
| 99 // If the path has a trailing slash, add a single empty component so the | |
| 100 // URI has a trailing slash as well. | |
| 101 parsed.parts.add(""); | |
| 102 } | |
| 103 | |
| 104 return new Uri( | |
| 105 scheme: 'file', host: rootParts.first, pathSegments: parsed.parts); | |
| 106 } else { | |
| 107 // Drive-letter paths become "file:///C:/path/to/file". | |
| 108 | |
| 109 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently | |
| 110 // be empty. We add an empty component so the URL constructor produces | |
| 111 // "file:///C:/", with a trailing slash. We also add an empty component if | |
| 112 // the URL otherwise has a trailing slash. | |
| 113 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { | |
| 114 parsed.parts.add(""); | |
| 115 } | |
| 116 | |
| 117 // Get rid of the trailing "\" in "C:\" because the URI constructor will | |
| 118 // add a separator on its own. | |
| 119 parsed.parts.insert( | |
| 120 0, parsed.root.replaceAll("/", "").replaceAll("\\", "")); | |
| 121 | |
| 122 return new Uri(scheme: 'file', pathSegments: parsed.parts); | |
| 123 } | |
| 124 } | |
| 125 } | |
| OLD | NEW |