Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 18, 2012
1 parent 3efa303 commit 64bdc60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
3 changes: 1 addition & 2 deletions lib/Mojo/Path.pm
Expand Up @@ -44,10 +44,9 @@ sub canonicalize {
sub clone {
my $self = shift;
my $clone = Mojo::Path->new;
$clone->parts([@{$self->parts}]);
$clone->leading_slash($self->leading_slash);
$clone->trailing_slash($self->trailing_slash);
return $clone;
return $clone->parts([@{$self->parts}]);
}

sub contains {
Expand Down
25 changes: 11 additions & 14 deletions lib/Mojo/URL.pm
Expand Up @@ -124,8 +124,7 @@ sub path {
$path = $self->{path} || Mojo::Path->new;
pop @{$path->parts} unless $path->trailing_slash;
push @{$path->parts}, @{$new->parts};
$path->leading_slash(1);
$path->trailing_slash($new->trailing_slash);
$path->leading_slash(1)->trailing_slash($new->trailing_slash);
}
}
$self->{path} = $path;
Expand Down Expand Up @@ -186,14 +185,13 @@ sub to_abs {
$abs->path($base_path->clone)->path->trailing_slash(0)->canonicalize;

# Query
return $abs if length($abs->query->to_string);
return $abs if length $abs->query->to_string;
$abs->query($base->query->clone);
}

# Merge paths
else {
my $new = $base_path->clone;
$new->leading_slash(1);
my $new = $base_path->clone->leading_slash(1);

# Characters after the right-most '/' need to go
pop @{$new->parts} if @{$path->parts} && !$new->trailing_slash;
Expand All @@ -210,23 +208,22 @@ sub to_rel {
my $base = shift || $self->base->clone;

# Scheme and authority
my $rel = $self->clone->base($base);
$rel->scheme(undef);
my $rel = $self->clone->base($base)->scheme(undef);
$rel->userinfo(undef)->host(undef)->port(undef) if $base->authority;

# Path
my @rel_parts = @{$rel->path->parts};
my @parts = @{$rel->path->parts};
my $base_path = $base->path;
my @base_parts = @{$base_path->parts};
pop @base_parts unless $base_path->trailing_slash;
while (@rel_parts && @base_parts && $rel_parts[0] eq $base_parts[0]) {
shift @rel_parts;
while (@parts && @base_parts && $parts[0] eq $base_parts[0]) {
shift @parts;
shift @base_parts;
}
my $rel_path = $rel->path(Mojo::Path->new)->path;
$rel_path->leading_slash(1) if $rel->authority;
$rel_path->parts([('..') x @base_parts, @rel_parts]);
$rel_path->trailing_slash(1) if $self->path->trailing_slash;
my $path = $rel->path(Mojo::Path->new)->path;
$path->leading_slash(1) if $rel->authority;
$path->parts([('..') x @base_parts, @parts]);
$path->trailing_slash(1) if $self->path->trailing_slash;

return $rel;
}
Expand Down

0 comments on commit 64bdc60

Please sign in to comment.