| 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.parsed_path; | |
| 6 | |
| 7 import 'internal_style.dart'; | 5 import 'internal_style.dart'; |
| 8 import 'style.dart'; | 6 import 'style.dart'; |
| 9 | 7 |
| 10 class ParsedPath { | 8 class ParsedPath { |
| 11 /// The [InternalStyle] that was used to parse this path. | 9 /// The [InternalStyle] that was used to parse this path. |
| 12 InternalStyle style; | 10 InternalStyle style; |
| 13 | 11 |
| 14 /// The absolute root portion of the path, or `null` if the path is relative. | 12 /// The absolute root portion of the path, or `null` if the path is relative. |
| 15 /// On POSIX systems, this will be `null` or "/". On Windows, it can be | 13 /// On POSIX systems, this will be `null` or "/". On Windows, it can be |
| 16 /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive | 14 /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 !parts.isEmpty && (parts.last == '' || separators.last != ''); | 90 !parts.isEmpty && (parts.last == '' || separators.last != ''); |
| 93 | 91 |
| 94 void removeTrailingSeparators() { | 92 void removeTrailingSeparators() { |
| 95 while (!parts.isEmpty && parts.last == '') { | 93 while (!parts.isEmpty && parts.last == '') { |
| 96 parts.removeLast(); | 94 parts.removeLast(); |
| 97 separators.removeLast(); | 95 separators.removeLast(); |
| 98 } | 96 } |
| 99 if (separators.length > 0) separators[separators.length - 1] = ''; | 97 if (separators.length > 0) separators[separators.length - 1] = ''; |
| 100 } | 98 } |
| 101 | 99 |
| 102 void normalize() { | 100 void normalize({bool canonicalize: false}) { |
| 103 // Handle '.', '..', and empty parts. | 101 // Handle '.', '..', and empty parts. |
| 104 var leadingDoubles = 0; | 102 var leadingDoubles = 0; |
| 105 var newParts = <String>[]; | 103 var newParts = <String>[]; |
| 106 for (var part in parts) { | 104 for (var part in parts) { |
| 107 if (part == '.' || part == '') { | 105 if (part == '.' || part == '') { |
| 108 // Do nothing. Ignore it. | 106 // Do nothing. Ignore it. |
| 109 } else if (part == '..') { | 107 } else if (part == '..') { |
| 110 // Pop the last part off. | 108 // Pop the last part off. |
| 111 if (newParts.length > 0) { | 109 if (newParts.length > 0) { |
| 112 newParts.removeLast(); | 110 newParts.removeLast(); |
| 113 } else { | 111 } else { |
| 114 // Backed out past the beginning, so preserve the "..". | 112 // Backed out past the beginning, so preserve the "..". |
| 115 leadingDoubles++; | 113 leadingDoubles++; |
| 116 } | 114 } |
| 117 } else { | 115 } else { |
| 118 newParts.add(part); | 116 newParts.add(canonicalize ? style.canonicalizePart(part) : part); |
| 119 } | 117 } |
| 120 } | 118 } |
| 121 | 119 |
| 122 // A relative path can back out from the start directory. | 120 // A relative path can back out from the start directory. |
| 123 if (!isAbsolute) { | 121 if (!isAbsolute) { |
| 124 newParts.insertAll(0, new List.filled(leadingDoubles, '..')); | 122 newParts.insertAll(0, new List.filled(leadingDoubles, '..')); |
| 125 } | 123 } |
| 126 | 124 |
| 127 // If we collapsed down to nothing, do ".". | 125 // If we collapsed down to nothing, do ".". |
| 128 if (newParts.length == 0 && !isAbsolute) { | 126 if (newParts.length == 0 && !isAbsolute) { |
| 129 newParts.add('.'); | 127 newParts.add('.'); |
| 130 } | 128 } |
| 131 | 129 |
| 132 // Canonicalize separators. | 130 // Canonicalize separators. |
| 133 var newSeparators = new List<String>.generate( | 131 var newSeparators = new List<String>.generate( |
| 134 newParts.length, (_) => style.separator, growable: true); | 132 newParts.length, (_) => style.separator, growable: true); |
| 135 newSeparators.insert(0, isAbsolute && | 133 newSeparators.insert(0, isAbsolute && |
| 136 newParts.length > 0 && | 134 newParts.length > 0 && |
| 137 style.needsSeparator(root) ? style.separator : ''); | 135 style.needsSeparator(root) ? style.separator : ''); |
| 138 | 136 |
| 139 parts = newParts; | 137 parts = newParts; |
| 140 separators = newSeparators; | 138 separators = newSeparators; |
| 141 | 139 |
| 142 // Normalize the Windows root if needed. | 140 // Normalize the Windows root if needed. |
| 143 if (root != null && style == Style.windows) { | 141 if (root != null && style == Style.windows) { |
| 142 if (canonicalize) root = root.toLowerCase(); |
| 144 root = root.replaceAll('/', '\\'); | 143 root = root.replaceAll('/', '\\'); |
| 145 } | 144 } |
| 146 removeTrailingSeparators(); | 145 removeTrailingSeparators(); |
| 147 } | 146 } |
| 148 | 147 |
| 149 String toString() { | 148 String toString() { |
| 150 var builder = new StringBuffer(); | 149 var builder = new StringBuffer(); |
| 151 if (root != null) builder.write(root); | 150 if (root != null) builder.write(root); |
| 152 for (var i = 0; i < parts.length; i++) { | 151 for (var i = 0; i < parts.length; i++) { |
| 153 builder.write(separators[i]); | 152 builder.write(separators[i]); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 174 // If there is no dot, or it's the first character, like '.bashrc', it | 173 // If there is no dot, or it's the first character, like '.bashrc', it |
| 175 // doesn't count. | 174 // doesn't count. |
| 176 if (lastDot <= 0) return [file, '']; | 175 if (lastDot <= 0) return [file, '']; |
| 177 | 176 |
| 178 return [file.substring(0, lastDot), file.substring(lastDot)]; | 177 return [file.substring(0, lastDot), file.substring(lastDot)]; |
| 179 } | 178 } |
| 180 | 179 |
| 181 ParsedPath clone() => new ParsedPath._(style, root, isRootRelative, | 180 ParsedPath clone() => new ParsedPath._(style, root, isRootRelative, |
| 182 new List.from(parts), new List.from(separators)); | 181 new List.from(parts), new List.from(separators)); |
| 183 } | 182 } |
| OLD | NEW |