summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNathan Woltman <nwoltman@outlook.com>2014-11-21 03:22:07 -0500
committerTrevor Norris <trev.norris@gmail.com>2014-11-26 12:16:56 -0800
commite0a0e913c7b340e756a6728226eb5c638f76be05 (patch)
tree99cf7c8990aa818d848a8179257e48fcf595b1b6 /lib
parent9d9ed61c5a97562b93a2326f33922783ed509d47 (diff)
downloadnode-e0a0e913c7b340e756a6728226eb5c638f76be05.tar.gz
path: refactor normalizeArray()
The normalizeArray() function now avoids using the slow Array#splice() method to improve performance and now also filters out empty path parts. Code that pre-filtered empty parts has been removed. PR-URL: https://github.com/joyent/node/pull/8724 Reviewed-by: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/path.js64
1 files changed, 23 insertions, 41 deletions
diff --git a/lib/path.js b/lib/path.js
index 70a901644..1765ef075 100644
--- a/lib/path.js
+++ b/lib/path.js
@@ -25,33 +25,30 @@ var util = require('util');
// resolves . and .. elements in a path array with directory names there
-// must be no slashes, empty elements, or device names (c:\) in the array
+// must be no slashes or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
- // if the path tries to go above the root, `up` ends up > 0
- var up = 0;
- for (var i = parts.length - 1; i >= 0; i--) {
- var last = parts[i];
- if (last === '.') {
- parts.splice(i, 1);
- } else if (last === '..') {
- parts.splice(i, 1);
- up++;
- } else if (up) {
- parts.splice(i, 1);
- up--;
- }
- }
+ var res = [];
+ for (var i = 0; i < parts.length; i++) {
+ var p = parts[i];
+
+ // ignore empty parts
+ if (!p || p === '.')
+ continue;
- // if the path is allowed to go above the root, restore leading ..s
- if (allowAboveRoot) {
- for (; up--; up) {
- parts.unshift('..');
+ if (p === '..') {
+ if (res.length && res[res.length - 1] !== '..') {
+ res.pop();
+ } else if (allowAboveRoot) {
+ res.push('..');
+ }
+ } else {
+ res.push(p);
}
}
- return parts;
+ return res;
}
// Regex to split a windows path into three parts: [*, device, slash,
@@ -153,12 +150,7 @@ win32.resolve = function() {
// fails)
// Normalize the tail path
-
- function f(p) {
- return !!p;
- }
-
- resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
+ resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/),
!resolvedAbsolute).join('\\');
// If device is a drive letter, we'll normalize to lower case.
@@ -186,9 +178,7 @@ win32.normalize = function(path) {
}
// Normalize the tail path
- tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
- return !!p;
- }), !isAbsolute).join('\\');
+ tail = normalizeArray(tail.split(/[\\\/]+/), !isAbsolute).join('\\');
if (!tail && !isAbsolute) {
tail = '.';
@@ -453,9 +443,8 @@ posix.resolve = function() {
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
- resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
- return !!p;
- }), !resolvedAbsolute).join('/');
+ resolvedPath = normalizeArray(resolvedPath.split('/'),
+ !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
};
@@ -464,17 +453,10 @@ posix.resolve = function() {
// posix version
posix.normalize = function(path) {
var isAbsolute = posix.isAbsolute(path),
- trailingSlash = path.substr(-1) === '/',
- segments = path.split('/'),
- nonEmptySegments = [];
+ trailingSlash = path.substr(-1) === '/';
// Normalize the path
- for (var i = 0; i < segments.length; i++) {
- if (segments[i]) {
- nonEmptySegments.push(segments[i]);
- }
- }
- path = normalizeArray(nonEmptySegments, !isAbsolute).join('/');
+ path = normalizeArray(path.split('/'), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';