Skip to content

Commit

Permalink
Fix IO::Path::parent
Browse files Browse the repository at this point in the history
The previous logic was too naive with absolute paths (just dropped the
basename) and messed up relative paths that had only updirs in the path,
but a basename.

The new implementation is now simpler by treating relative and absolute
paths the same and only looking at the basename to decide whether to append
an updir or drop an element.
  • Loading branch information
patrickbkr committed Feb 17, 2022
1 parent 5caf7aa commit 4ef9426
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/core.c/IO/Path.pm6
Expand Up @@ -362,18 +362,16 @@ my class IO::Path is Cool does IO {
!! self
}
}
multi method parent(IO::Path:D:) { # XXX needs work
my $curdir := $!SPEC.curdir;
my $updir := $!SPEC.updir;

nqp::clone(self).cloned-with-path: self.is-absolute
?? $!SPEC.join($.volume, $.dirname, '')
!! $.dirname eq $curdir && $.basename eq $curdir
?? $!SPEC.join($.volume,$curdir,$updir)
!! ($.dirname eq $curdir && $.basename eq $updir)
|| !$!SPEC.splitdir($.dirname).first(* ne $updir)
?? $!SPEC.join($.volume,$!SPEC.catdir($.dirname,$updir),$.basename)
!! $!SPEC.join($.volume, $.dirname, '')
multi method parent(IO::Path:D:) {
my $curdir := $!SPEC.curdir;
my $updir := $!SPEC.updir;
my $basename := $.basename;

nqp::clone(self).cloned-with-path: $basename eq $curdir
?? $!SPEC.join($.volume,$.dirname,$updir)
!! $basename eq $updir
?? $!SPEC.join($.volume,$!SPEC.catdir($.dirname,$updir),$updir)
!! $!SPEC.join($.volume, $.dirname, '')
}

method child (IO::Path:D: \child) {
Expand Down

0 comments on commit 4ef9426

Please sign in to comment.