path: simplify normalizeString

This improves the `path.normalize()` and `path.resolve()` performance a
tiny bit.
One statement could never be truthy, another check could be simplified
and `code` is now monomorphic.

PR-URL: https://github.com/nodejs/node/pull/27240
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-04-15 03:59:21 +02:00 committed by Daniel Bevenius
parent 4f8b497991
commit 9946c59707

View File

@ -50,11 +50,11 @@ function isWindowsDeviceRoot(code) {
// Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = '';
var lastSegmentLength = 0;
var lastSlash = -1;
var dots = 0;
var code;
let res = '';
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code = 0;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length)
code = path.charCodeAt(i);
@ -66,7 +66,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
} else if (dots === 2) {
if (res.length < 2 || lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
@ -82,7 +82,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
lastSlash = i;
dots = 0;
continue;
} else if (res.length === 2 || res.length === 1) {
} else if (res.length !== 0) {
res = '';
lastSegmentLength = 0;
lastSlash = i;