path: remove redundant function
PR-URL: https://github.com/nodejs/node/pull/19237 Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
fa8594779a
commit
4ae320f2b3
86
lib/path.js
86
lib/path.js
@ -50,7 +50,7 @@ function isWindowsDeviceRoot(code) {
|
||||
}
|
||||
|
||||
// Resolves . and .. elements in a path with directory names
|
||||
function normalizeStringWin32(path, allowAboveRoot) {
|
||||
function normalizeString(path, allowAboveRoot, separator) {
|
||||
var res = '';
|
||||
var lastSegmentLength = 0;
|
||||
var lastSlash = -1;
|
||||
@ -72,14 +72,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
|
||||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
|
||||
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
|
||||
if (res.length > 2) {
|
||||
const lastSlashIndex = res.lastIndexOf('\\');
|
||||
const lastSlashIndex = res.lastIndexOf(separator);
|
||||
if (lastSlashIndex !== res.length - 1) {
|
||||
if (lastSlashIndex === -1) {
|
||||
res = '';
|
||||
lastSegmentLength = 0;
|
||||
} else {
|
||||
res = res.slice(0, lastSlashIndex);
|
||||
lastSegmentLength = res.length - 1 - res.lastIndexOf('\\');
|
||||
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
|
||||
}
|
||||
lastSlash = i;
|
||||
dots = 0;
|
||||
@ -95,82 +95,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
|
||||
}
|
||||
if (allowAboveRoot) {
|
||||
if (res.length > 0)
|
||||
res += '\\..';
|
||||
res += `${separator}..`;
|
||||
else
|
||||
res = '..';
|
||||
lastSegmentLength = 2;
|
||||
}
|
||||
} else {
|
||||
if (res.length > 0)
|
||||
res += '\\' + path.slice(lastSlash + 1, i);
|
||||
else
|
||||
res = path.slice(lastSlash + 1, i);
|
||||
lastSegmentLength = i - lastSlash - 1;
|
||||
}
|
||||
lastSlash = i;
|
||||
dots = 0;
|
||||
} else if (code === CHAR_DOT && dots !== -1) {
|
||||
++dots;
|
||||
} else {
|
||||
dots = -1;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// Resolves . and .. elements in a path with directory names
|
||||
function normalizeStringPosix(path, allowAboveRoot) {
|
||||
var res = '';
|
||||
var lastSegmentLength = 0;
|
||||
var lastSlash = -1;
|
||||
var dots = 0;
|
||||
var code;
|
||||
for (var i = 0; i <= path.length; ++i) {
|
||||
if (i < path.length)
|
||||
code = path.charCodeAt(i);
|
||||
else if (code === CHAR_FORWARD_SLASH)
|
||||
break;
|
||||
else
|
||||
code = CHAR_FORWARD_SLASH;
|
||||
if (code === CHAR_FORWARD_SLASH) {
|
||||
if (lastSlash === i - 1 || dots === 1) {
|
||||
// NOOP
|
||||
} else if (lastSlash !== i - 1 && dots === 2) {
|
||||
if (res.length < 2 || lastSegmentLength !== 2 ||
|
||||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
|
||||
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
|
||||
if (res.length > 2) {
|
||||
const lastSlashIndex = res.lastIndexOf('/');
|
||||
if (lastSlashIndex !== res.length - 1) {
|
||||
if (lastSlashIndex === -1) {
|
||||
res = '';
|
||||
lastSegmentLength = 0;
|
||||
} else {
|
||||
res = res.slice(0, lastSlashIndex);
|
||||
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
|
||||
}
|
||||
lastSlash = i;
|
||||
dots = 0;
|
||||
continue;
|
||||
}
|
||||
} else if (res.length === 2 || res.length === 1) {
|
||||
res = '';
|
||||
lastSegmentLength = 0;
|
||||
lastSlash = i;
|
||||
dots = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (allowAboveRoot) {
|
||||
if (res.length > 0)
|
||||
res += '/..';
|
||||
else
|
||||
res = '..';
|
||||
lastSegmentLength = 2;
|
||||
}
|
||||
} else {
|
||||
if (res.length > 0)
|
||||
res += '/' + path.slice(lastSlash + 1, i);
|
||||
res += separator + path.slice(lastSlash + 1, i);
|
||||
else
|
||||
res = path.slice(lastSlash + 1, i);
|
||||
lastSegmentLength = i - lastSlash - 1;
|
||||
@ -340,7 +272,7 @@ const win32 = {
|
||||
// fails)
|
||||
|
||||
// Normalize the tail path
|
||||
resolvedTail = normalizeStringWin32(resolvedTail, !resolvedAbsolute);
|
||||
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');
|
||||
|
||||
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
|
||||
'.';
|
||||
@ -432,7 +364,7 @@ const win32 = {
|
||||
|
||||
var tail;
|
||||
if (rootEnd < len)
|
||||
tail = normalizeStringWin32(path.slice(rootEnd), !isAbsolute);
|
||||
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
|
||||
else
|
||||
tail = '';
|
||||
if (tail.length === 0 && !isAbsolute)
|
||||
@ -1163,7 +1095,7 @@ const posix = {
|
||||
// handle relative paths to be safe (might happen when process.cwd() fails)
|
||||
|
||||
// Normalize the path
|
||||
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
|
||||
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/');
|
||||
|
||||
if (resolvedAbsolute) {
|
||||
if (resolvedPath.length > 0)
|
||||
@ -1189,7 +1121,7 @@ const posix = {
|
||||
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
|
||||
|
||||
// Normalize the path
|
||||
path = normalizeStringPosix(path, !isAbsolute);
|
||||
path = normalizeString(path, !isAbsolute, '/');
|
||||
|
||||
if (path.length === 0 && !isAbsolute)
|
||||
path = '.';
|
||||
|
Loading…
x
Reference in New Issue
Block a user