path: refactor for less indentation

This just switches the statements in a way that it reduces the
overall indentation. The function has a very deep indentation in
general and this should improve the readability.

PR-URL: https://github.com/nodejs/node/pull/26917
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-26 07:18:17 +01:00
parent f7c96856f9
commit dd0d6df9ae
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -165,63 +165,63 @@ const win32 = {
const code = path.charCodeAt(0); const code = path.charCodeAt(0);
// Try to match a root // Try to match a root
if (len > 1) { if (len === 1) {
if (isPathSeparator(code)) { if (isPathSeparator(code)) {
// Possible UNC root // `path` contains just a path separator
rootEnd = 1;
// If we started with a separator, we know we at least have an
// absolute path of some kind (UNC or otherwise)
isAbsolute = true; isAbsolute = true;
}
} else if (isPathSeparator(code)) {
// Possible UNC root
if (isPathSeparator(path.charCodeAt(1))) { // If we started with a separator, we know we at least have an
// Matched double path separator at beginning // absolute path of some kind (UNC or otherwise)
let j = 2; isAbsolute = true;
let last = j;
// Match 1 or more non-path separators if (isPathSeparator(path.charCodeAt(1))) {
while (j < len && !isPathSeparator(path.charCodeAt(j))) { // Matched double path separator at beginning
let j = 2;
let last = j;
// Match 1 or more non-path separators
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
j++;
}
if (j < len && j !== last) {
const firstPart = path.slice(last, j);
// Matched!
last = j;
// Match 1 or more path separators
while (j < len && isPathSeparator(path.charCodeAt(j))) {
j++; j++;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
const firstPart = path.slice(last, j);
// Matched! // Matched!
last = j; last = j;
// Match 1 or more path separators // Match 1 or more non-path separators
while (j < len && isPathSeparator(path.charCodeAt(j))) { while (j < len && !isPathSeparator(path.charCodeAt(j))) {
j++; j++;
} }
if (j < len && j !== last) { if (j === len || j !== last) {
// Matched! // We matched a UNC root
last = j; device = `\\\\${firstPart}\\${path.slice(last, j)}`;
// Match 1 or more non-path separators rootEnd = j;
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
j++;
}
if (j === len || j !== last) {
// We matched a UNC root
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
rootEnd = j;
}
} }
} }
} else {
rootEnd = 1;
}
} else if (isWindowsDeviceRoot(code) &&
path.charCodeAt(1) === CHAR_COLON) {
// Possible device root
device = path.slice(0, 2);
rootEnd = 2;
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
// Treat separator following drive name as an absolute path
// indicator
isAbsolute = true;
rootEnd = 3;
} }
} else {
rootEnd = 1;
}
} else if (isWindowsDeviceRoot(code) &&
path.charCodeAt(1) === CHAR_COLON) {
// Possible device root
device = path.slice(0, 2);
rootEnd = 2;
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
// Treat separator following drive name as an absolute path
// indicator
isAbsolute = true;
rootEnd = 3;
} }
} else if (isPathSeparator(code)) {
// `path` contains just a path separator
rootEnd = 1;
isAbsolute = true;
} }
if (device.length > 0) { if (device.length > 0) {