path: replace duplicate conditions by functions

It will also remove useless "code" variables by inlining
path.charCodeAt.

PR-URL: https://github.com/nodejs/node/pull/18693
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Sergey Golovin 2018-02-10 14:02:13 +03:00 committed by Ruben Bridgewater
parent 590eacecaa
commit b404aa56c0
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -40,6 +40,15 @@ function assertPath(path) {
} }
} }
function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}
function isWindowsDeviceRoot(code) {
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
}
// Resolves . and .. elements in a path with directory names // Resolves . and .. elements in a path with directory names
function normalizeStringWin32(path, allowAboveRoot) { function normalizeStringWin32(path, allowAboveRoot) {
var res = ''; var res = '';
@ -50,11 +59,12 @@ function normalizeStringWin32(path, allowAboveRoot) {
for (var i = 0; i <= path.length; ++i) { for (var i = 0; i <= path.length; ++i) {
if (i < path.length) if (i < path.length)
code = path.charCodeAt(i); code = path.charCodeAt(i);
else if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) else if (isPathSeparator(code))
break; break;
else else
code = CHAR_FORWARD_SLASH; code = CHAR_FORWARD_SLASH;
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) { if (lastSlash === i - 1 || dots === 1) {
// NOOP // NOOP
} else if (lastSlash !== i - 1 && dots === 2) { } else if (lastSlash !== i - 1 && dots === 2) {
@ -228,28 +238,26 @@ const win32 = {
var len = path.length; var len = path.length;
var rootEnd = 0; var rootEnd = 0;
var code = path.charCodeAt(0);
var device = ''; var device = '';
var isAbsolute = false; var isAbsolute = false;
const code = path.charCodeAt(0);
// Try to match a root // Try to match a root
if (len > 1) { if (len > 1) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// Possible UNC root // Possible UNC root
// If we started with a separator, we know we at least have an // If we started with a separator, we know we at least have an
// absolute path of some kind (UNC or otherwise) // absolute path of some kind (UNC or otherwise)
isAbsolute = true; isAbsolute = true;
code = path.charCodeAt(1); if (isPathSeparator(path.charCodeAt(1))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// Matched double path separator at beginning // Matched double path separator at beginning
var j = 2; var j = 2;
var last = j; var last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -258,8 +266,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more path separators // Match 1 or more path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (!isPathSeparator(path.charCodeAt(j)))
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -267,11 +274,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
const isPathSeparator =
code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
if (isPathSeparator)
break; break;
} }
if (j === len) { if (j === len) {
@ -290,16 +293,14 @@ const win32 = {
} else { } else {
rootEnd = 1; rootEnd = 1;
} }
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(code)) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (path.charCodeAt(1) === CHAR_COLON) { if (path.charCodeAt(1) === CHAR_COLON) {
device = path.slice(0, 2); device = path.slice(0, 2);
rootEnd = 2; rootEnd = 2;
if (len > 2) { if (len > 2) {
code = path.charCodeAt(2); if (isPathSeparator(path.charCodeAt(2))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// Treat separator following drive name as an absolute path // Treat separator following drive name as an absolute path
// indicator // indicator
isAbsolute = true; isAbsolute = true;
@ -308,7 +309,7 @@ const win32 = {
} }
} }
} }
} else if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { } else if (isPathSeparator(code)) {
// `path` contains just a path separator // `path` contains just a path separator
rootEnd = 1; rootEnd = 1;
isAbsolute = true; isAbsolute = true;
@ -351,28 +352,26 @@ const win32 = {
if (len === 0) if (len === 0)
return '.'; return '.';
var rootEnd = 0; var rootEnd = 0;
var code = path.charCodeAt(0);
var device; var device;
var isAbsolute = false; var isAbsolute = false;
const code = path.charCodeAt(0);
// Try to match a root // Try to match a root
if (len > 1) { if (len > 1) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// Possible UNC root // Possible UNC root
// If we started with a separator, we know we at least have an absolute // If we started with a separator, we know we at least have an absolute
// path of some kind (UNC or otherwise) // path of some kind (UNC or otherwise)
isAbsolute = true; isAbsolute = true;
code = path.charCodeAt(1); if (isPathSeparator(path.charCodeAt(1))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// Matched double path separator at beginning // Matched double path separator at beginning
var j = 2; var j = 2;
var last = j; var last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -381,8 +380,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more path separators // Match 1 or more path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (!isPathSeparator(path.charCodeAt(j)))
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -390,8 +388,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j === len) { if (j === len) {
@ -411,16 +408,14 @@ const win32 = {
} else { } else {
rootEnd = 1; rootEnd = 1;
} }
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(code)) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (path.charCodeAt(1) === CHAR_COLON) { if (path.charCodeAt(1) === CHAR_COLON) {
device = path.slice(0, 2); device = path.slice(0, 2);
rootEnd = 2; rootEnd = 2;
if (len > 2) { if (len > 2) {
code = path.charCodeAt(2); if (isPathSeparator(path.charCodeAt(2))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// Treat separator following drive name as an absolute path // Treat separator following drive name as an absolute path
// indicator // indicator
isAbsolute = true; isAbsolute = true;
@ -429,15 +424,12 @@ const win32 = {
} }
} }
} }
} else if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { } else if (isPathSeparator(code)) {
// `path` contains just a path separator, exit early to avoid unnecessary // `path` contains just a path separator, exit early to avoid unnecessary
// work // work
return '\\'; return '\\';
} }
code = path.charCodeAt(len - 1);
var trailingSeparator =
(code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH);
var tail; var tail;
if (rootEnd < len) if (rootEnd < len)
tail = normalizeStringWin32(path.slice(rootEnd), !isAbsolute); tail = normalizeStringWin32(path.slice(rootEnd), !isAbsolute);
@ -445,7 +437,7 @@ const win32 = {
tail = ''; tail = '';
if (tail.length === 0 && !isAbsolute) if (tail.length === 0 && !isAbsolute)
tail = '.'; tail = '.';
if (tail.length > 0 && trailingSeparator) if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
tail += '\\'; tail += '\\';
if (device === undefined) { if (device === undefined) {
if (isAbsolute) { if (isAbsolute) {
@ -476,16 +468,15 @@ const win32 = {
const len = path.length; const len = path.length;
if (len === 0) if (len === 0)
return false; return false;
var code = path.charCodeAt(0);
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { const code = path.charCodeAt(0);
if (isPathSeparator(code)) {
return true; return true;
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(code)) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (len > 2 && path.charCodeAt(1) === CHAR_COLON) { if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
code = path.charCodeAt(2); if (isPathSeparator(path.charCodeAt(2)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
return true; return true;
} }
} }
@ -528,17 +519,14 @@ const win32 = {
// path.join('//server', 'share') -> '\\\\server\\share\\') // path.join('//server', 'share') -> '\\\\server\\share\\')
var needsReplace = true; var needsReplace = true;
var slashCount = 0; var slashCount = 0;
var code = firstPart.charCodeAt(0); if (isPathSeparator(firstPart.charCodeAt(0))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
++slashCount; ++slashCount;
const firstLen = firstPart.length; const firstLen = firstPart.length;
if (firstLen > 1) { if (firstLen > 1) {
code = firstPart.charCodeAt(1); if (isPathSeparator(firstPart.charCodeAt(1))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
++slashCount; ++slashCount;
if (firstLen > 2) { if (firstLen > 2) {
code = firstPart.charCodeAt(2); if (isPathSeparator(firstPart.charCodeAt(2)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
++slashCount; ++slashCount;
else { else {
// We matched a UNC path in the first part // We matched a UNC path in the first part
@ -551,8 +539,7 @@ const win32 = {
if (needsReplace) { if (needsReplace) {
// Find any more consecutive slashes we need to replace // Find any more consecutive slashes we need to replace
for (; slashCount < joined.length; ++slashCount) { for (; slashCount < joined.length; ++slashCount) {
code = joined.charCodeAt(slashCount); if (!isPathSeparator(joined.charCodeAt(slashCount)))
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH)
break; break;
} }
@ -699,19 +686,17 @@ const win32 = {
const resolvedPath = win32.resolve(path); const resolvedPath = win32.resolve(path);
if (resolvedPath.length >= 3) { if (resolvedPath.length >= 3) {
var code = resolvedPath.charCodeAt(0); if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
if (code === CHAR_BACKWARD_SLASH) {
// Possible UNC root // Possible UNC root
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) { if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
code = resolvedPath.charCodeAt(2); const code = resolvedPath.charCodeAt(2);
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) { if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
// Matched non-long UNC root, convert the path to a long UNC path // Matched non-long UNC root, convert the path to a long UNC path
return '\\\\?\\UNC\\' + resolvedPath.slice(2); return '\\\\?\\UNC\\' + resolvedPath.slice(2);
} }
} }
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (resolvedPath.charCodeAt(1) === CHAR_COLON && if (resolvedPath.charCodeAt(1) === CHAR_COLON &&
@ -734,24 +719,22 @@ const win32 = {
var end = -1; var end = -1;
var matchedSlash = true; var matchedSlash = true;
var offset = 0; var offset = 0;
var 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 (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// Possible UNC root // Possible UNC root
rootEnd = offset = 1; rootEnd = offset = 1;
code = path.charCodeAt(1); if (isPathSeparator(path.charCodeAt(1))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// Matched double path separator at beginning // Matched double path separator at beginning
var j = 2; var j = 2;
var last = j; var last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -759,8 +742,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more path separators // Match 1 or more path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (!isPathSeparator(path.charCodeAt(j)))
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -768,8 +750,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j === len) { if (j === len) {
@ -786,28 +767,25 @@ const win32 = {
} }
} }
} }
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(code)) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (path.charCodeAt(1) === CHAR_COLON) { if (path.charCodeAt(1) === CHAR_COLON) {
rootEnd = offset = 2; rootEnd = offset = 2;
if (len > 2) { if (len > 2) {
code = path.charCodeAt(2); if (isPathSeparator(path.charCodeAt(2)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
rootEnd = offset = 3; rootEnd = offset = 3;
} }
} }
} }
} else if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { } else if (isPathSeparator(code)) {
// `path` contains just a path separator, exit early to avoid // `path` contains just a path separator, exit early to avoid
// unnecessary work // unnecessary work
return path; return path;
} }
for (var i = len - 1; i >= offset; --i) { for (var i = len - 1; i >= offset; --i) {
code = path.charCodeAt(i); if (isPathSeparator(path.charCodeAt(i))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
if (!matchedSlash) { if (!matchedSlash) {
end = i; end = i;
break; break;
@ -842,8 +820,7 @@ const win32 = {
// disregarded // disregarded
if (path.length >= 2) { if (path.length >= 2) {
const drive = path.charCodeAt(0); const drive = path.charCodeAt(0);
if ((drive >= CHAR_UPPERCASE_A && drive <= CHAR_UPPERCASE_Z) || if (isWindowsDeviceRoot(drive)) {
(drive >= CHAR_LOWERCASE_A && drive <= CHAR_LOWERCASE_Z)) {
if (path.charCodeAt(1) === CHAR_COLON) if (path.charCodeAt(1) === CHAR_COLON)
start = 2; start = 2;
} }
@ -856,7 +833,7 @@ const win32 = {
var firstNonSlashEnd = -1; var firstNonSlashEnd = -1;
for (i = path.length - 1; i >= start; --i) { for (i = path.length - 1; i >= start; --i) {
const code = path.charCodeAt(i); const code = path.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// If we reached a path separator that was not part of a set of path // If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now // separators at the end of the string, stop now
if (!matchedSlash) { if (!matchedSlash) {
@ -895,8 +872,7 @@ const win32 = {
return path.slice(start, end); return path.slice(start, end);
} else { } else {
for (i = path.length - 1; i >= start; --i) { for (i = path.length - 1; i >= start; --i) {
const code = path.charCodeAt(i); if (isPathSeparator(path.charCodeAt(i))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
// If we reached a path separator that was not part of a set of path // If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now // separators at the end of the string, stop now
if (!matchedSlash) { if (!matchedSlash) {
@ -932,18 +908,16 @@ const win32 = {
// Check for a drive letter prefix so as not to mistake the following // Check for a drive letter prefix so as not to mistake the following
// path separator as an extra separator at the end of the path that can be // path separator as an extra separator at the end of the path that can be
// disregarded // disregarded
if (path.length >= 2) {
const code = path.charCodeAt(0); if (path.length >= 2 &&
if (path.charCodeAt(1) === CHAR_COLON && path.charCodeAt(1) === CHAR_COLON &&
((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || isWindowsDeviceRoot(path.charCodeAt(0))) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z))) {
start = startPart = 2; start = startPart = 2;
} }
}
for (var i = path.length - 1; i >= start; --i) { for (var i = path.length - 1; i >= start; --i) {
const code = path.charCodeAt(i); const code = path.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// If we reached a path separator that was not part of a set of path // If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now // separators at the end of the string, stop now
if (!matchedSlash) { if (!matchedSlash) {
@ -1003,23 +977,21 @@ const win32 = {
var len = path.length; var len = path.length;
var rootEnd = 0; var rootEnd = 0;
var code = path.charCodeAt(0); let code = path.charCodeAt(0);
// Try to match a root // Try to match a root
if (len > 1) { if (len > 1) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// Possible UNC root // Possible UNC root
code = path.charCodeAt(1);
rootEnd = 1; rootEnd = 1;
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(path.charCodeAt(1))) {
// Matched double path separator at beginning // Matched double path separator at beginning
var j = 2; var j = 2;
var last = j; var last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -1027,8 +999,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more path separators // Match 1 or more path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (!isPathSeparator(path.charCodeAt(j)))
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH)
break; break;
} }
if (j < len && j !== last) { if (j < len && j !== last) {
@ -1036,8 +1007,7 @@ const win32 = {
last = j; last = j;
// Match 1 or more non-path separators // Match 1 or more non-path separators
for (; j < len; ++j) { for (; j < len; ++j) {
code = path.charCodeAt(j); if (isPathSeparator(path.charCodeAt(j)))
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH)
break; break;
} }
if (j === len) { if (j === len) {
@ -1052,15 +1022,13 @@ const win32 = {
} }
} }
} }
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || } else if (isWindowsDeviceRoot(code)) {
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) {
// Possible device root // Possible device root
if (path.charCodeAt(1) === CHAR_COLON) { if (path.charCodeAt(1) === CHAR_COLON) {
rootEnd = 2; rootEnd = 2;
if (len > 2) { if (len > 2) {
code = path.charCodeAt(2); if (isPathSeparator(path.charCodeAt(2))) {
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) {
if (len === 3) { if (len === 3) {
// `path` contains just a drive root, exit early to avoid // `path` contains just a drive root, exit early to avoid
// unnecessary work // unnecessary work
@ -1077,7 +1045,7 @@ const win32 = {
} }
} }
} }
} else if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { } else if (isPathSeparator(code)) {
// `path` contains just a path separator, exit early to avoid // `path` contains just a path separator, exit early to avoid
// unnecessary work // unnecessary work
ret.root = ret.dir = path; ret.root = ret.dir = path;
@ -1100,7 +1068,7 @@ const win32 = {
// Get non-dir info // Get non-dir info
for (; i >= rootEnd; --i) { for (; i >= rootEnd; --i) {
code = path.charCodeAt(i); code = path.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { if (isPathSeparator(code)) {
// If we reached a path separator that was not part of a set of path // If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now // separators at the end of the string, stop now
if (!matchedSlash) { if (!matchedSlash) {
@ -1363,13 +1331,11 @@ const posix = {
assertPath(path); assertPath(path);
if (path.length === 0) if (path.length === 0)
return '.'; return '.';
var code = path.charCodeAt(0); const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
var hasRoot = (code === CHAR_FORWARD_SLASH);
var end = -1; var end = -1;
var matchedSlash = true; var matchedSlash = true;
for (var i = path.length - 1; i >= 1; --i) { for (var i = path.length - 1; i >= 1; --i) {
code = path.charCodeAt(i); if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
if (code === CHAR_FORWARD_SLASH) {
if (!matchedSlash) { if (!matchedSlash) {
end = i; end = i;
break; break;
@ -1534,8 +1500,7 @@ const posix = {
var ret = { root: '', dir: '', base: '', ext: '', name: '' }; var ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0) if (path.length === 0)
return ret; return ret;
var code = path.charCodeAt(0); var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
var isAbsolute = (code === CHAR_FORWARD_SLASH);
var start; var start;
if (isAbsolute) { if (isAbsolute) {
ret.root = '/'; ret.root = '/';
@ -1555,7 +1520,7 @@ const posix = {
// Get non-dir info // Get non-dir info
for (; i >= start; --i) { for (; i >= start; --i) {
code = path.charCodeAt(i); const code = path.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH) { if (code === CHAR_FORWARD_SLASH) {
// If we reached a path separator that was not part of a set of path // If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now // separators at the end of the string, stop now