Skip to content

Commit

Permalink
Cleaner normalizePath regex
Browse files Browse the repository at this point in the history
Signed-off-by: J0WI <J0WI@users.noreply.github.com>
  • Loading branch information
J0WI committed Mar 27, 2021
1 parent aec9c84 commit e3d5e3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/private/Files/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,10 @@ public static function normalizePath($path, $stripTrailingSlash = true, $isAbsol
$path = '/' . $path;

$patterns = [
'/\\\\/s', // no windows style slashes
'/\/\.(\/\.)?\//s', // remove '/./'
'/\/{2,}/s', // remove sequence of slashes
'/\/\.$/s', // remove trailing /.
'#\\\\#s', // no windows style '\\' slashes
'#/\.(/\.)*/#s', // remove '/./'
'#\//+#s', // remove sequence of slashes
'#/\.$#s', // remove trailing '/.'
];

do {
Expand Down
27 changes: 22 additions & 5 deletions tests/lib/Files/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,26 @@ public function normalizePathData() {
['/foo/.bar', '/foo/.bar/'],
['/foo/.bar/', '/foo/.bar/', false],
['/foo/.bar/tee', '/foo/.bar/tee'],

['/foo/bar', '/.///././//./foo/.///././//./bar/./././.'],
['/foo/bar/', '/.///././//./foo/.///././//./bar/./././.', false],
['/foo/bar', '/.///././//./foo/.///././//./bar/././././'],
['/foo/bar/', '/.///././//./foo/.///././//./bar/././././', false],
['/foo/bar.', '/foo/bar./'],
['/foo/bar./', '/foo/bar./', false],
['/foo/bar./tee', '/foo/bar./tee'],
['/foo/.bar.', '/foo/.bar./'],
['/foo/.bar./', '/foo/.bar./', false],
['/foo/.bar./tee', '/foo/.bar./tee'],

['/foo/bar', '/.////././//./foo/.///././//./bar/././/./.'],
['/foo/bar/', '/.////././//./foo/.///././//./bar/./././.', false],
['/foo/bar', '/.////././//./foo/.///././//./bar/././/././'],
['/foo/bar/', '/.////././//./foo/.///././//./bar/././/././', false],
['/foo/.bar', '/.////././//./foo/./././/./.bar/././/././'],
['/foo/.bar/', '/.////././//./foo/./././/./.bar/././/././', false],
['/foo/.bar/tee.', '/.////././//./foo/./././/./.bar/tee././/././', false],
['/foo/bar.', '/.////././//./foo/./././/./bar./././/././'],
['/foo/bar./', '/.////././//./foo/./././/./bar./././/././', false],
['/foo/bar./tee./', '/.////././//./foo/./././/./bar./tee././/././', false],
['/foo/.bar.', '/.////././//./foo/./././/./.bar./././/././'],
['/foo/.bar./', '/.////././//./foo/./././/./.bar./././././', false],
['/foo/.bar./tee./', '/.////././//./foo/./././/./.bar./tee././././', false],

// Windows paths
['/', ''],
Expand Down Expand Up @@ -186,7 +201,9 @@ public function normalizePathData() {

// normalize does not resolve '..' (by design)
['/foo/..', '/foo/../'],
['/foo/../bar', '/foo/../bar/.'],
['/foo/..', '\\foo\\..\\'],
['/foo/../bar', '\\foo\\..\\bar'],
];
}

Expand Down

0 comments on commit e3d5e3a

Please sign in to comment.