Skip to content

Commit

Permalink
Don't use 'current' if 'from' is not relative.
Browse files Browse the repository at this point in the history
R=rnystrom@google.com
BUG=

Review URL: https://codereview.chromium.org//937683002
  • Loading branch information
Konstantin Shcheglov committed Feb 18, 2015
1 parent 441de92 commit 0cada32
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pkgs/path/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.3

* Performance improvement in `Context.relative` - don't call `current` if `from`
is not relative.

## 1.3.2

* Fix some analyzer hints.
Expand Down
8 changes: 7 additions & 1 deletion pkgs/path/lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ class Context {
/// "/", no path can be determined. In this case, a [PathException] will be
/// thrown.
String relative(String path, {String from}) {
from = from == null ? current : this.join(current, from);
// Avoid calling [current] since it is slow and calling join() when
// [from] is absolute does nothing.
if (from == null) {
from = current;
} else if (this.isRelative(from) || this.isRootRelative(from)) {
from = this.join(current, from);
}

// We can't determine the path from a relative path to an absolute path.
if (this.isRelative(from) && this.isAbsolute(path)) {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/path/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: path
version: 1.3.2
version: 1.3.3
author: Dart Team <misc@dartlang.org>
description: >
A string-based path manipulation library. All of the path operations you know
Expand Down
2 changes: 1 addition & 1 deletion pkgs/path/test/relative_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void relativeTest(path.Context context, String prefix) {
var isRelative = (context.current == '.');
// Cases where the arguments are absolute paths.
expectRelative(result, pathArg, fromArg) {
expect(context.normalize(result), context.relative(pathArg, from: fromArg));
expect(context.relative(pathArg, from: fromArg), context.normalize(result));
}

expectRelative('c/d', '${prefix}a/b/c/d', '${prefix}a/b');
Expand Down

0 comments on commit 0cada32

Please sign in to comment.