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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 for (int i = common + 1; i < baseSegments.length; i++) { | 63 for (int i = common + 1; i < baseSegments.length; i++) { |
64 sb.add('../'); | 64 sb.add('../'); |
65 } | 65 } |
66 if (base.hasTrailingSeparator) { | 66 if (base.hasTrailingSeparator) { |
67 sb.add('../'); | 67 sb.add('../'); |
68 } | 68 } |
69 for (int i = common; i < pathSegments.length - 1; i++) { | 69 for (int i = common; i < pathSegments.length - 1; i++) { |
70 sb.add('${pathSegments[i]}/'); | 70 sb.add('${pathSegments[i]}/'); |
71 } | 71 } |
72 sb.add('${pathSegments.last()}'); | 72 sb.add('${pathSegments.last}'); |
73 if (hasTrailingSeparator) { | 73 if (hasTrailingSeparator) { |
74 sb.add('/'); | 74 sb.add('/'); |
75 } | 75 } |
76 return new Path(sb.toString()); | 76 return new Path(sb.toString()); |
77 } | 77 } |
78 throw new NotImplementedException( | 78 throw new NotImplementedException( |
79 "Unimplemented case of Path.relativeTo(base):\n" | 79 "Unimplemented case of Path.relativeTo(base):\n" |
80 " Only absolute paths are handled at present.\n" | 80 " Only absolute paths are handled at present.\n" |
81 " Arguments: $_path.relativeTo($base)"); | 81 " Arguments: $_path.relativeTo($base)"); |
82 } | 82 } |
(...skipping 29 matching lines...) Expand all Loading... |
112 List segs = _path.split('/'); // Don't mask the getter 'segments'. | 112 List segs = _path.split('/'); // Don't mask the getter 'segments'. |
113 if (segs[0] == '') { // Absolute path | 113 if (segs[0] == '') { // Absolute path |
114 segs[0] = null; // Faster than removeRange(). | 114 segs[0] = null; // Faster than removeRange(). |
115 } else { // A canonical relative path may start with .. segments. | 115 } else { // A canonical relative path may start with .. segments. |
116 for (int pos = 0; | 116 for (int pos = 0; |
117 pos < segs.length && segs[pos] == '..'; | 117 pos < segs.length && segs[pos] == '..'; |
118 ++pos) { | 118 ++pos) { |
119 segs[pos] = null; | 119 segs[pos] = null; |
120 } | 120 } |
121 } | 121 } |
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: |
(...skipping 75 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 |