Created
October 20, 2009 03:41
-
-
Save kazuho/213969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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