Chromium Code Reviews| 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 /// A comprehensive, cross-platform path manipulation library. | 5 /// A comprehensive, cross-platform path manipulation library. |
| 6 /// | 6 /// |
| 7 /// ## Installing ## | 7 /// ## Installing ## |
| 8 /// | 8 /// |
| 9 /// Use [pub][] to install this package. Add the following to your | 9 /// Use [pub][] to install this package. Add the following to your |
| 10 /// `pubspec.yaml` file. | 10 /// `pubspec.yaml` file. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 /// Returns the [Style] of the current context. | 72 /// Returns the [Style] of the current context. |
| 73 /// | 73 /// |
| 74 /// This is the style that all top-level path functions will use. | 74 /// This is the style that all top-level path functions will use. |
| 75 Style get style => context.style; | 75 Style get style => context.style; |
| 76 | 76 |
| 77 /// Gets the path to the current working directory. | 77 /// Gets the path to the current working directory. |
| 78 /// | 78 /// |
| 79 /// In the browser, this means the current URL, without the last file segment. | 79 /// In the browser, this means the current URL, without the last file segment. |
| 80 String get current { | 80 String get current { |
| 81 var uri = Uri.base; | 81 var uri = Uri.base; |
| 82 | |
| 83 // Converting the base URI to a file path is pretty slow, and the base URI | |
| 84 // rarely changes in practice, so we cache the result here. | |
| 85 if (uri == _currentUriBase) return _current; | |
| 86 _currentUriBase = uri; | |
| 87 | |
| 82 if (Style.platform == Style.url) { | 88 if (Style.platform == Style.url) { |
| 83 return uri.resolve('.').toString(); | 89 _current = uri.resolve('.').toString(); |
|
Lasse Reichstein Nielsen
2015/12/01 06:52:58
That is likely to be slower than necessary.
Instea
Lasse Reichstein Nielsen
2015/12/01 06:53:54
(That does assume that Uri.base has no query or fr
nweiz
2015/12/01 20:50:46
Given that this is cached now, I think it's probab
| |
| 90 return _current; | |
| 84 } else { | 91 } else { |
| 85 var path = uri.toFilePath(); | 92 var path = uri.toFilePath(); |
| 86 // Remove trailing '/' or '\'. | 93 // Remove trailing '/' or '\'. |
| 87 int lastIndex = path.length - 1; | 94 var lastIndex = path.length - 1; |
| 88 assert(path[lastIndex] == '/' || path[lastIndex] == '\\'); | 95 assert(path[lastIndex] == '/' || path[lastIndex] == '\\'); |
| 89 return path.substring(0, lastIndex); | 96 _current = path.substring(0, lastIndex); |
| 97 return _current; | |
| 90 } | 98 } |
| 91 } | 99 } |
| 92 | 100 |
| 101 /// The last value returned by [Uri.base]. | |
| 102 /// | |
| 103 /// This is used to cache the current working directory. | |
| 104 Uri _currentUriBase; | |
| 105 | |
| 106 /// The last known value of the current working directory. | |
| 107 /// | |
| 108 /// This is cached because [current] is called frequently but rarely actually | |
| 109 /// changes. | |
| 110 String _current; | |
| 111 | |
| 93 /// Gets the path separator for the current platform. This is `\` on Windows | 112 /// Gets the path separator for the current platform. This is `\` on Windows |
| 94 /// and `/` on other platforms (including the browser). | 113 /// and `/` on other platforms (including the browser). |
| 95 String get separator => context.separator; | 114 String get separator => context.separator; |
| 96 | 115 |
| 97 /// Creates a new path by appending the given path parts to [current]. | 116 /// Creates a new path by appending the given path parts to [current]. |
| 98 /// Equivalent to [join()] with [current] as the first argument. Example: | 117 /// Equivalent to [join()] with [current] as the first argument. Example: |
| 99 /// | 118 /// |
| 100 /// path.absolute('path', 'to/foo'); // -> '/your/current/dir/path/to/foo' | 119 /// path.absolute('path', 'to/foo'); // -> '/your/current/dir/path/to/foo' |
| 101 String absolute(String part1, [String part2, String part3, String part4, | 120 String absolute(String part1, [String part2, String part3, String part4, |
| 102 String part5, String part6, String part7]) => | 121 String part5, String part6, String part7]) => |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 /// | 391 /// |
| 373 /// // Windows at "C:\root\path" | 392 /// // Windows at "C:\root\path" |
| 374 /// path.prettyUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart' | 393 /// path.prettyUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart' |
| 375 /// path.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org' | 394 /// path.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org' |
| 376 /// | 395 /// |
| 377 /// // URL at "http://dartlang.org/root/path" | 396 /// // URL at "http://dartlang.org/root/path" |
| 378 /// path.prettyUri('http://dartlang.org/root/path/a/b.dart'); | 397 /// path.prettyUri('http://dartlang.org/root/path/a/b.dart'); |
| 379 /// // -> r'a/b.dart' | 398 /// // -> r'a/b.dart' |
| 380 /// path.prettyUri('file:///root/path'); // -> 'file:///root/path' | 399 /// path.prettyUri('file:///root/path'); // -> 'file:///root/path' |
| 381 String prettyUri(uri) => context.prettyUri(uri); | 400 String prettyUri(uri) => context.prettyUri(uri); |
| OLD | NEW |