OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class _Path implements Path { | 5 class _Path implements Path { |
6 final String _path; | 6 final String _path; |
7 | 7 |
8 _Path(String source) : _path = source; | 8 _Path(String source) : _path = source; |
9 _Path.fromNative(String source) : _path = _clean(source); | 9 _Path.fromNative(String source) : _path = _clean(source); |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 static String _cleanWindows(source) { | 22 static String _cleanWindows(source) { |
23 // Change \ to /. | 23 // Change \ to /. |
24 var clean = source.replaceAll('\\', '/'); | 24 var clean = source.replaceAll('\\', '/'); |
25 // Add / before intial [Drive letter]: | 25 // Add / before intial [Drive letter]: |
26 if (clean.length >= 2 && clean[1] == ':') { | 26 if (clean.length >= 2 && clean[1] == ':') { |
27 clean = '/$clean'; | 27 clean = '/$clean'; |
28 } | 28 } |
29 return clean; | 29 return clean; |
30 } | 30 } |
31 | 31 |
32 bool get isEmpty => _path.isEmpty(); | 32 bool get isEmpty => _path.isEmpty; |
33 bool get isAbsolute => _path.startsWith('/'); | 33 bool get isAbsolute => _path.startsWith('/'); |
34 bool get hasTrailingSeparator => _path.endsWith('/'); | 34 bool get hasTrailingSeparator => _path.endsWith('/'); |
35 | 35 |
36 String toString() => _path; | 36 String toString() => _path; |
37 | 37 |
38 Path relativeTo(Path base) { | 38 Path relativeTo(Path base) { |
39 // Throws exception if an unimplemented or impossible case is reached. | 39 // Throws exception if an unimplemented or impossible case is reached. |
40 // Returns a path "relative" such that | 40 // Returns a path "relative" such that |
41 // base.join(relative) == this.canonicalize. | 41 // base.join(relative) == this.canonicalize. |
42 // Throws an exception if no such path exists, or the case is not | 42 // Throws an exception if no such path exists, or the case is not |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 if (segs.last() == '') segs.removeLast(); // Path ends with /. | 122 if (segs.last() == '') segs.removeLast(); // Path ends with /. |
123 // No remaining segments can be ., .., or empty. | 123 // No remaining segments can be ., .., or empty. |
124 return !segs.some((s) => s == '' || s == '.' || s == '..'); | 124 return !segs.some((s) => s == '' || s == '.' || s == '..'); |
125 } | 125 } |
126 | 126 |
127 Path makeCanonical() { | 127 Path makeCanonical() { |
128 bool isAbs = isAbsolute; | 128 bool isAbs = isAbsolute; |
129 List segs = segments(); | 129 List segs = segments(); |
130 String drive; | 130 String drive; |
131 if (isAbs && | 131 if (isAbs && |
132 !segs.isEmpty() && | 132 !segs.isEmpty && |
133 segs[0].length == 2 && | 133 segs[0].length == 2 && |
134 segs[0][1] == ':') { | 134 segs[0][1] == ':') { |
135 drive = segs[0]; | 135 drive = segs[0]; |
136 segs.removeRange(0, 1); | 136 segs.removeRange(0, 1); |
137 } | 137 } |
138 List newSegs = []; | 138 List newSegs = []; |
139 for (String segment in segs) { | 139 for (String segment in segs) { |
140 switch (segment) { | 140 switch (segment) { |
141 case '..': | 141 case '..': |
142 // Absolute paths drop leading .. markers, including after a drive. | 142 // Absolute paths drop leading .. markers, including after a drive. |
143 if (newSegs.isEmpty()) { | 143 if (newSegs.isEmpty) { |
144 if (isAbs) { | 144 if (isAbs) { |
145 // Do nothing: drop the segment. | 145 // Do nothing: drop the segment. |
146 } else { | 146 } else { |
147 newSegs.add('..'); | 147 newSegs.add('..'); |
148 } | 148 } |
149 } else if (newSegs.last() == '..') { | 149 } else if (newSegs.last() == '..') { |
150 newSegs.add('..'); | 150 newSegs.add('..'); |
151 } else { | 151 } else { |
152 newSegs.removeLast(); | 152 newSegs.removeLast(); |
153 } | 153 } |
154 break; | 154 break; |
155 case '.': | 155 case '.': |
156 case '': | 156 case '': |
157 // Do nothing - drop the segment. | 157 // Do nothing - drop the segment. |
158 break; | 158 break; |
159 default: | 159 default: |
160 newSegs.add(segment); | 160 newSegs.add(segment); |
161 break; | 161 break; |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 List segmentsToJoin = []; | 165 List segmentsToJoin = []; |
166 if (isAbs) { | 166 if (isAbs) { |
167 segmentsToJoin.add(''); | 167 segmentsToJoin.add(''); |
168 if (drive != null) { | 168 if (drive != null) { |
169 segmentsToJoin.add(drive); | 169 segmentsToJoin.add(drive); |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 if (newSegs.isEmpty()) { | 173 if (newSegs.isEmpty) { |
174 if (isAbs) { | 174 if (isAbs) { |
175 segmentsToJoin.add(''); | 175 segmentsToJoin.add(''); |
176 } else { | 176 } else { |
177 segmentsToJoin.add('.'); | 177 segmentsToJoin.add('.'); |
178 } | 178 } |
179 } else { | 179 } else { |
180 segmentsToJoin.addAll(newSegs); | 180 segmentsToJoin.addAll(newSegs); |
181 if (hasTrailingSeparator) { | 181 if (hasTrailingSeparator) { |
182 segmentsToJoin.add(''); | 182 segmentsToJoin.add(''); |
183 } | 183 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if (pos < 0) return new Path(''); | 235 if (pos < 0) return new Path(''); |
236 while (pos > 0 && _path[pos - 1] == '/') --pos; | 236 while (pos > 0 && _path[pos - 1] == '/') --pos; |
237 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); | 237 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); |
238 } | 238 } |
239 | 239 |
240 String get filename { | 240 String get filename { |
241 int pos = _path.lastIndexOf('/'); | 241 int pos = _path.lastIndexOf('/'); |
242 return _path.substring(pos + 1); | 242 return _path.substring(pos + 1); |
243 } | 243 } |
244 } | 244 } |
OLD | NEW |