Skip to content

Instantly share code, notes, and snippets.

@kazuho
Created October 20, 2009 03:41
Show Gist options
  • Save kazuho/213969 to your computer and use it in GitHub Desktop.
Save kazuho/213969 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
use strict;
use warnings;
use Test::More tests => 12;
sub vrealpath {
my $orig_path = shift;
# main loop
my @true_path = ();
for my $p (split '/', "/$orig_path") {
if ($p eq '..') {
pop @true_path
if @true_path;
} elsif ($p eq '.' || $p eq '') {
# skip
} else {
push @true_path, $p;
}
}
# join parts
my $true_path = '/' . join '/', @true_path;
# add / if $orig_path ends with /
$true_path .= '/'
if @true_path && $orig_path =~ m|/$|;
$true_path;
}
is(vrealpath('/'), '/');
is(vrealpath('/abc/..'), '/');
is(vrealpath('/abc/../de'), '/de');
is(vrealpath('/./abc'), '/abc');
is(vrealpath('/abc/de/../f'), '/abc/f');
is(vrealpath('/abc/de/./f'), '/abc/de/f');
is(vrealpath('//abc//de'), '/abc/de');
is(vrealpath('/abc/'), '/abc/');
is(vrealpath(''), '/');
is(vrealpath('..'), '/');
is(vrealpath('/..'), '/');
is(vrealpath('/abc/../..'), '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment