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; | 5 library path.parsed_path; |
6 | 6 |
7 import 'internal_style.dart'; | 7 import 'internal_style.dart'; |
8 import 'style.dart'; | 8 import 'style.dart'; |
9 | 9 |
10 class ParsedPath { | 10 class ParsedPath { |
(...skipping 29 matching lines...) Expand all Loading... |
40 /// `true` if this is an absolute path. | 40 /// `true` if this is an absolute path. |
41 bool get isAbsolute => root != null; | 41 bool get isAbsolute => root != null; |
42 | 42 |
43 factory ParsedPath.parse(String path, InternalStyle style) { | 43 factory ParsedPath.parse(String path, InternalStyle style) { |
44 // Remove the root prefix, if any. | 44 // Remove the root prefix, if any. |
45 var root = style.getRoot(path); | 45 var root = style.getRoot(path); |
46 var isRootRelative = style.isRootRelative(path); | 46 var isRootRelative = style.isRootRelative(path); |
47 if (root != null) path = path.substring(root.length); | 47 if (root != null) path = path.substring(root.length); |
48 | 48 |
49 // Split the parts on path separators. | 49 // Split the parts on path separators. |
50 var parts = []; | 50 var parts = <String>[]; |
51 var separators = []; | 51 var separators = <String>[]; |
52 | 52 |
53 var start = 0; | 53 var start = 0; |
54 | 54 |
55 if (path.isNotEmpty && style.isSeparator(path.codeUnitAt(0))) { | 55 if (path.isNotEmpty && style.isSeparator(path.codeUnitAt(0))) { |
56 separators.add(path[0]); | 56 separators.add(path[0]); |
57 start = 1; | 57 start = 1; |
58 } else { | 58 } else { |
59 separators.add(''); | 59 separators.add(''); |
60 } | 60 } |
61 | 61 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 while (!parts.isEmpty && parts.last == '') { | 95 while (!parts.isEmpty && parts.last == '') { |
96 parts.removeLast(); | 96 parts.removeLast(); |
97 separators.removeLast(); | 97 separators.removeLast(); |
98 } | 98 } |
99 if (separators.length > 0) separators[separators.length - 1] = ''; | 99 if (separators.length > 0) separators[separators.length - 1] = ''; |
100 } | 100 } |
101 | 101 |
102 void normalize() { | 102 void normalize() { |
103 // Handle '.', '..', and empty parts. | 103 // Handle '.', '..', and empty parts. |
104 var leadingDoubles = 0; | 104 var leadingDoubles = 0; |
105 var newParts = []; | 105 var newParts = <String>[]; |
106 for (var part in parts) { | 106 for (var part in parts) { |
107 if (part == '.' || part == '') { | 107 if (part == '.' || part == '') { |
108 // Do nothing. Ignore it. | 108 // Do nothing. Ignore it. |
109 } else if (part == '..') { | 109 } else if (part == '..') { |
110 // Pop the last part off. | 110 // Pop the last part off. |
111 if (newParts.length > 0) { | 111 if (newParts.length > 0) { |
112 newParts.removeLast(); | 112 newParts.removeLast(); |
113 } else { | 113 } else { |
114 // Backed out past the beginning, so preserve the "..". | 114 // Backed out past the beginning, so preserve the "..". |
115 leadingDoubles++; | 115 leadingDoubles++; |
116 } | 116 } |
117 } else { | 117 } else { |
118 newParts.add(part); | 118 newParts.add(part); |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
122 // A relative path can back out from the start directory. | 122 // A relative path can back out from the start directory. |
123 if (!isAbsolute) { | 123 if (!isAbsolute) { |
124 newParts.insertAll(0, new List.filled(leadingDoubles, '..')); | 124 newParts.insertAll(0, new List.filled(leadingDoubles, '..')); |
125 } | 125 } |
126 | 126 |
127 // If we collapsed down to nothing, do ".". | 127 // If we collapsed down to nothing, do ".". |
128 if (newParts.length == 0 && !isAbsolute) { | 128 if (newParts.length == 0 && !isAbsolute) { |
129 newParts.add('.'); | 129 newParts.add('.'); |
130 } | 130 } |
131 | 131 |
132 // Canonicalize separators. | 132 // Canonicalize separators. |
133 var newSeparators = new List.generate( | 133 var newSeparators = new List<String>.generate( |
134 newParts.length, (_) => style.separator, growable: true); | 134 newParts.length, (_) => style.separator, growable: true); |
135 newSeparators.insert(0, isAbsolute && | 135 newSeparators.insert(0, isAbsolute && |
136 newParts.length > 0 && | 136 newParts.length > 0 && |
137 style.needsSeparator(root) ? style.separator : ''); | 137 style.needsSeparator(root) ? style.separator : ''); |
138 | 138 |
139 parts = newParts; | 139 parts = newParts; |
140 separators = newSeparators; | 140 separators = newSeparators; |
141 | 141 |
142 // Normalize the Windows root if needed. | 142 // Normalize the Windows root if needed. |
143 if (root != null && style == Style.windows) { | 143 if (root != null && style == Style.windows) { |
(...skipping 30 matching lines...) Expand all Loading... |
174 // If there is no dot, or it's the first character, like '.bashrc', it | 174 // If there is no dot, or it's the first character, like '.bashrc', it |
175 // doesn't count. | 175 // doesn't count. |
176 if (lastDot <= 0) return [file, '']; | 176 if (lastDot <= 0) return [file, '']; |
177 | 177 |
178 return [file.substring(0, lastDot), file.substring(lastDot)]; | 178 return [file.substring(0, lastDot), file.substring(lastDot)]; |
179 } | 179 } |
180 | 180 |
181 ParsedPath clone() => new ParsedPath._(style, root, isRootRelative, | 181 ParsedPath clone() => new ParsedPath._(style, root, isRootRelative, |
182 new List.from(parts), new List.from(separators)); | 182 new List.from(parts), new List.from(separators)); |
183 } | 183 } |
OLD | NEW |