From bb9e898eabb259f33f38f3eebb1eb3d7fd64b3a6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 10:45:27 +0100 Subject: [PATCH] path: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36302 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/path.js | 307 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 176 insertions(+), 131 deletions(-) diff --git a/lib/path.js b/lib/path.js index 7532b795bf63f7..246bace17873b6 100644 --- a/lib/path.js +++ b/lib/path.js @@ -21,6 +21,13 @@ 'use strict'; +const { + FunctionPrototypeBind, + StringPrototypeCharCodeAt, + StringPrototypeLastIndexOf, + StringPrototypeSlice, + StringPrototypeToLowerCase, +} = primordials; const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { CHAR_UPPERCASE_A, @@ -57,7 +64,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { let code = 0; for (let i = 0; i <= path.length; ++i) { if (i < path.length) - code = path.charCodeAt(i); + code = StringPrototypeCharCodeAt(path, i); else if (isPathSeparator(code)) break; else @@ -68,16 +75,17 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { // NOOP } else if (dots === 2) { if (res.length < 2 || lastSegmentLength !== 2 || - res.charCodeAt(res.length - 1) !== CHAR_DOT || - res.charCodeAt(res.length - 2) !== CHAR_DOT) { + StringPrototypeCharCodeAt(res, res.length - 1) !== CHAR_DOT || + StringPrototypeCharCodeAt(res, res.length - 2) !== CHAR_DOT) { if (res.length > 2) { - const lastSlashIndex = res.lastIndexOf(separator); + const lastSlashIndex = StringPrototypeLastIndexOf(res, separator); if (lastSlashIndex === -1) { res = ''; lastSegmentLength = 0; } else { - res = res.slice(0, lastSlashIndex); - lastSegmentLength = res.length - 1 - res.lastIndexOf(separator); + res = StringPrototypeSlice(res, 0, lastSlashIndex); + lastSegmentLength = + res.length - 1 - StringPrototypeLastIndexOf(res, separator); } lastSlash = i; dots = 0; @@ -96,9 +104,9 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { } } else { if (res.length > 0) - res += `${separator}${path.slice(lastSlash + 1, i)}`; + res += `${separator}${StringPrototypeSlice(path, lastSlash + 1, i)}`; else - res = path.slice(lastSlash + 1, i); + res = StringPrototypeSlice(path, lastSlash + 1, i); lastSegmentLength = i - lastSlash - 1; } lastSlash = i; @@ -155,8 +163,9 @@ const win32 = { // Verify that a cwd was found and that it actually points // to our drive. If not, default to the drive's root. if (path === undefined || - (path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() && - path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) { + (StringPrototypeSlice(path, 0, 2).toLowerCase() !== + StringPrototypeToLowerCase(resolvedDevice) && + StringPrototypeCharCodeAt(path, 2) === CHAR_BACKWARD_SLASH)) { path = `${resolvedDevice}\\`; } } @@ -165,7 +174,7 @@ const win32 = { let rootEnd = 0; let device = ''; let isAbsolute = false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); // Try to match a root if (len === 1) { @@ -181,32 +190,36 @@ const win32 = { // absolute path of some kind (UNC or otherwise) isAbsolute = true; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // 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))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { - const firstPart = path.slice(last, j); + const firstPart = StringPrototypeSlice(path, last, j); // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len || j !== last) { // We matched a UNC root - device = `\\\\${firstPart}\\${path.slice(last, j)}`; + device = + `\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`; rootEnd = j; } } @@ -215,11 +228,11 @@ const win32 = { rootEnd = 1; } } else if (isWindowsDeviceRoot(code) && - path.charCodeAt(1) === CHAR_COLON) { + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root - device = path.slice(0, 2); + device = StringPrototypeSlice(path, 0, 2); rootEnd = 2; - if (len > 2 && isPathSeparator(path.charCodeAt(2))) { + if (len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { // Treat separator following drive name as an absolute path // indicator isAbsolute = true; @@ -229,7 +242,8 @@ const win32 = { if (device.length > 0) { if (resolvedDevice.length > 0) { - if (device.toLowerCase() !== resolvedDevice.toLowerCase()) + if (StringPrototypeToLowerCase(device) !== + StringPrototypeToLowerCase(resolvedDevice)) // This path points to another device so it is not applicable continue; } else { @@ -241,7 +255,8 @@ const win32 = { if (resolvedDevice.length > 0) break; } else { - resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`; + resolvedTail = + `${StringPrototypeSlice(path, rootEnd)}\\${resolvedTail}`; resolvedAbsolute = isAbsolute; if (isAbsolute && resolvedDevice.length > 0) { break; @@ -270,7 +285,7 @@ const win32 = { let rootEnd = 0; let device; let isAbsolute = false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); // Try to match a root if (len === 1) { @@ -285,38 +300,42 @@ const win32 = { // path of some kind (UNC or otherwise) isAbsolute = true; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // 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))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { - const firstPart = path.slice(last, j); + const firstPart = StringPrototypeSlice(path, last, j); // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { // We matched a UNC root only // Return the normalized version of the UNC root since there // is nothing left to process - return `\\\\${firstPart}\\${path.slice(last)}\\`; + return `\\\\${firstPart}\\${StringPrototypeSlice(path, last)}\\`; } if (j !== last) { // We matched a UNC root with leftovers - device = `\\\\${firstPart}\\${path.slice(last, j)}`; + device = + `\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`; rootEnd = j; } } @@ -324,11 +343,12 @@ const win32 = { } else { rootEnd = 1; } - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root - device = path.slice(0, 2); + device = StringPrototypeSlice(path, 0, 2); rootEnd = 2; - if (len > 2 && isPathSeparator(path.charCodeAt(2))) { + if (len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { // Treat separator following drive name as an absolute path // indicator isAbsolute = true; @@ -337,11 +357,13 @@ const win32 = { } let tail = rootEnd < len ? - normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) : + normalizeString(StringPrototypeSlice(path, rootEnd), + !isAbsolute, '\\', isPathSeparator) : ''; if (tail.length === 0 && !isAbsolute) tail = '.'; - if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) + if (tail.length > 0 && + isPathSeparator(StringPrototypeCharCodeAt(path, len - 1))) tail += '\\'; if (device === undefined) { return isAbsolute ? `\\${tail}` : tail; @@ -355,13 +377,13 @@ const win32 = { if (len === 0) return false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); return isPathSeparator(code) || // Possible device root (len > 2 && isWindowsDeviceRoot(code) && - path.charCodeAt(1) === CHAR_COLON && - isPathSeparator(path.charCodeAt(2))); + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON && + isPathSeparator(StringPrototypeCharCodeAt(path, 2))); }, join(...args) { @@ -399,13 +421,14 @@ const win32 = { // path.join('//server', 'share') -> '\\\\server\\share\\') let needsReplace = true; let slashCount = 0; - if (isPathSeparator(firstPart.charCodeAt(0))) { + if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 0))) { ++slashCount; const firstLen = firstPart.length; - if (firstLen > 1 && isPathSeparator(firstPart.charCodeAt(1))) { + if (firstLen > 1 && + isPathSeparator(StringPrototypeCharCodeAt(firstPart, 1))) { ++slashCount; if (firstLen > 2) { - if (isPathSeparator(firstPart.charCodeAt(2))) + if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 2))) ++slashCount; else { // We matched a UNC path in the first part @@ -417,13 +440,13 @@ const win32 = { if (needsReplace) { // Find any more consecutive slashes we need to replace while (slashCount < joined.length && - isPathSeparator(joined.charCodeAt(slashCount))) { + isPathSeparator(StringPrototypeCharCodeAt(joined, slashCount))) { slashCount++; } // Replace the slashes if needed if (slashCount >= 2) - joined = `\\${joined.slice(slashCount)}`; + joined = `\\${StringPrototypeSlice(joined, slashCount)}`; } return win32.normalize(joined); @@ -446,8 +469,8 @@ const win32 = { if (fromOrig === toOrig) return ''; - from = fromOrig.toLowerCase(); - to = toOrig.toLowerCase(); + from = StringPrototypeToLowerCase(fromOrig); + to = StringPrototypeToLowerCase(toOrig); if (from === to) return ''; @@ -455,13 +478,15 @@ const win32 = { // Trim any leading backslashes let fromStart = 0; while (fromStart < from.length && - from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(from, fromStart) === CHAR_BACKWARD_SLASH) { fromStart++; } // Trim trailing backslashes (applicable to UNC paths only) let fromEnd = from.length; - while (fromEnd - 1 > fromStart && - from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) { + while ( + fromEnd - 1 > fromStart && + StringPrototypeCharCodeAt(from, fromEnd - 1) === CHAR_BACKWARD_SLASH + ) { fromEnd--; } const fromLen = fromEnd - fromStart; @@ -469,13 +494,13 @@ const win32 = { // Trim any leading backslashes let toStart = 0; while (toStart < to.length && - to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(to, toStart) === CHAR_BACKWARD_SLASH) { toStart++; } // Trim trailing backslashes (applicable to UNC paths only) let toEnd = to.length; while (toEnd - 1 > toStart && - to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(to, toEnd - 1) === CHAR_BACKWARD_SLASH) { toEnd--; } const toLen = toEnd - toStart; @@ -485,8 +510,8 @@ const win32 = { let lastCommonSep = -1; let i = 0; for (; i < length; i++) { - const fromCode = from.charCodeAt(fromStart + i); - if (fromCode !== to.charCodeAt(toStart + i)) + const fromCode = StringPrototypeCharCodeAt(from, fromStart + i); + if (fromCode !== StringPrototypeCharCodeAt(to, toStart + i)) break; else if (fromCode === CHAR_BACKWARD_SLASH) lastCommonSep = i; @@ -499,19 +524,21 @@ const win32 = { return toOrig; } else { if (toLen > length) { - if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(to, toStart + i) === + CHAR_BACKWARD_SLASH) { // We get here if `from` is the exact base path for `to`. // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz' - return toOrig.slice(toStart + i + 1); + return StringPrototypeSlice(toOrig, toStart + i + 1); } if (i === 2) { // We get here if `from` is the device root. // For example: from='C:\\'; to='C:\\foo' - return toOrig.slice(toStart + i); + return StringPrototypeSlice(toOrig, toStart + i); } } if (fromLen > length) { - if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(from, fromStart + i) === + CHAR_BACKWARD_SLASH) { // We get here if `to` is the exact base path for `from`. // For example: from='C:\\foo\\bar'; to='C:\\foo' lastCommonSep = i; @@ -529,7 +556,8 @@ const win32 = { // Generate the relative path based on the path difference between `to` and // `from` for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { - if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) { + if (i === fromEnd || + StringPrototypeCharCodeAt(from, i) === CHAR_BACKWARD_SLASH) { out += out.length === 0 ? '..' : '\\..'; } } @@ -539,11 +567,11 @@ const win32 = { // Lastly, append the rest of the destination (`to`) path that comes after // the common path parts if (out.length > 0) - return `${out}${toOrig.slice(toStart, toEnd)}`; + return `${out}${StringPrototypeSlice(toOrig, toStart, toEnd)}`; - if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) + if (StringPrototypeCharCodeAt(toOrig, toStart) === CHAR_BACKWARD_SLASH) ++toStart; - return toOrig.slice(toStart, toEnd); + return StringPrototypeSlice(toOrig, toStart, toEnd); }, toNamespacedPath(path) { @@ -560,18 +588,20 @@ const win32 = { if (resolvedPath.length <= 2) return path; - if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(resolvedPath, 0) === CHAR_BACKWARD_SLASH) { // Possible UNC root - if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) { - const code = resolvedPath.charCodeAt(2); + if (StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_BACKWARD_SLASH) { + const code = StringPrototypeCharCodeAt(resolvedPath, 2); if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) { // Matched non-long UNC root, convert the path to a long UNC path - return `\\\\?\\UNC\\${resolvedPath.slice(2)}`; + return `\\\\?\\UNC\\${StringPrototypeSlice(resolvedPath, 2)}`; } } - } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0)) && - resolvedPath.charCodeAt(1) === CHAR_COLON && - resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) { + } else if ( + isWindowsDeviceRoot(StringPrototypeCharCodeAt(resolvedPath, 0)) && + StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_COLON && + StringPrototypeCharCodeAt(resolvedPath, 2) === CHAR_BACKWARD_SLASH + ) { // Matched device root, convert the path to a long UNC path return `\\\\?\\${resolvedPath}`; } @@ -586,7 +616,7 @@ const win32 = { return '.'; let rootEnd = -1; let offset = 0; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); if (len === 1) { // `path` contains just a path separator, exit early to avoid @@ -600,26 +630,29 @@ const win32 = { rootEnd = offset = 1; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // 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))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { @@ -637,15 +670,17 @@ const win32 = { } } // Possible device root - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { - rootEnd = len > 2 && isPathSeparator(path.charCodeAt(2)) ? 3 : 2; + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { + rootEnd = + len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2)) ? 3 : 2; offset = rootEnd; } let end = -1; let matchedSlash = true; for (let i = len - 1; i >= offset; --i) { - if (isPathSeparator(path.charCodeAt(i))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) { if (!matchedSlash) { end = i; break; @@ -662,7 +697,7 @@ const win32 = { end = rootEnd; } - return path.slice(0, end); + return StringPrototypeSlice(path, 0, end); }, basename(path, ext) { @@ -677,8 +712,8 @@ const win32 = { // path separator as an extra separator at the end of the path that can be // disregarded if (path.length >= 2 && - isWindowsDeviceRoot(path.charCodeAt(0)) && - path.charCodeAt(1) === CHAR_COLON) { + isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0)) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { start = 2; } @@ -688,7 +723,7 @@ const win32 = { let extIdx = ext.length - 1; let firstNonSlashEnd = -1; for (let i = path.length - 1; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -705,7 +740,7 @@ const win32 = { } if (extIdx >= 0) { // Try to match the explicit extension - if (code === ext.charCodeAt(extIdx)) { + if (code === StringPrototypeCharCodeAt(ext, extIdx)) { if (--extIdx === -1) { // We matched the extension, so mark this as the end of our path // component @@ -725,10 +760,10 @@ const win32 = { end = firstNonSlashEnd; else if (end === -1) end = path.length; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); } for (let i = path.length - 1; i >= start; --i) { - if (isPathSeparator(path.charCodeAt(i))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now if (!matchedSlash) { @@ -745,7 +780,7 @@ const win32 = { if (end === -1) return ''; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); }, extname(path) { @@ -764,13 +799,13 @@ const win32 = { // disregarded if (path.length >= 2 && - path.charCodeAt(1) === CHAR_COLON && - isWindowsDeviceRoot(path.charCodeAt(0))) { + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON && + isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0))) { start = startPart = 2; } for (let i = path.length - 1; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -809,10 +844,10 @@ const win32 = { startDot === startPart + 1)) { return ''; } - return path.slice(startDot, end); + return StringPrototypeSlice(path, startDot, end); }, - format: _format.bind(null, '\\'), + format: FunctionPrototypeBind(_format, null, '\\'), parse(path) { validateString(path, 'path'); @@ -823,7 +858,7 @@ const win32 = { const len = path.length; let rootEnd = 0; - let code = path.charCodeAt(0); + let code = StringPrototypeCharCodeAt(path, 0); if (len === 1) { if (isPathSeparator(code)) { @@ -840,26 +875,29 @@ const win32 = { // Possible UNC root rootEnd = 1; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // 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))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { @@ -872,7 +910,8 @@ const win32 = { } } } - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root if (len <= 2) { // `path` contains just a drive root, exit early to avoid @@ -881,7 +920,7 @@ const win32 = { return ret; } rootEnd = 2; - if (isPathSeparator(path.charCodeAt(2))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { if (len === 3) { // `path` contains just a drive root, exit early to avoid // unnecessary work @@ -892,7 +931,7 @@ const win32 = { } } if (rootEnd > 0) - ret.root = path.slice(0, rootEnd); + ret.root = StringPrototypeSlice(path, 0, rootEnd); let startDot = -1; let startPart = rootEnd; @@ -906,7 +945,7 @@ const win32 = { // Get non-dir info for (; i >= rootEnd; --i) { - code = path.charCodeAt(i); + code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -943,11 +982,11 @@ const win32 = { (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) { - ret.base = ret.name = path.slice(startPart, end); + ret.base = ret.name = StringPrototypeSlice(path, startPart, end); } else { - ret.name = path.slice(startPart, startDot); - ret.base = path.slice(startPart, end); - ret.ext = path.slice(startDot, end); + ret.name = StringPrototypeSlice(path, startPart, startDot); + ret.base = StringPrototypeSlice(path, startPart, end); + ret.ext = StringPrototypeSlice(path, startDot, end); } } @@ -955,7 +994,7 @@ const win32 = { // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the // trailing slash (`C:\abc\def` -> `C:\abc`). if (startPart > 0 && startPart !== rootEnd) - ret.dir = path.slice(0, startPart - 1); + ret.dir = StringPrototypeSlice(path, 0, startPart - 1); else ret.dir = ret.root; @@ -985,7 +1024,8 @@ const posix = { } resolvedPath = `${path}/${resolvedPath}`; - resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + resolvedAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; } // At this point the path should be resolved to a full absolute path, but @@ -1007,9 +1047,10 @@ const posix = { if (path.length === 0) return '.'; - const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const isAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; const trailingSeparator = - path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH; + StringPrototypeCharCodeAt(path, path.length - 1) === CHAR_FORWARD_SLASH; // Normalize the path path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator); @@ -1027,7 +1068,8 @@ const posix = { isAbsolute(path) { validateString(path, 'path'); - return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH; + return path.length > 0 && + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; }, join(...args) { @@ -1074,26 +1116,27 @@ const posix = { let lastCommonSep = -1; let i = 0; for (; i < length; i++) { - const fromCode = from.charCodeAt(fromStart + i); - if (fromCode !== to.charCodeAt(toStart + i)) + const fromCode = StringPrototypeCharCodeAt(from, fromStart + i); + if (fromCode !== StringPrototypeCharCodeAt(to, toStart + i)) break; else if (fromCode === CHAR_FORWARD_SLASH) lastCommonSep = i; } if (i === length) { if (toLen > length) { - if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(to, toStart + i) === CHAR_FORWARD_SLASH) { // We get here if `from` is the exact base path for `to`. // For example: from='/foo/bar'; to='/foo/bar/baz' - return to.slice(toStart + i + 1); + return StringPrototypeSlice(to, toStart + i + 1); } if (i === 0) { // We get here if `from` is the root // For example: from='/'; to='/foo' - return to.slice(toStart + i); + return StringPrototypeSlice(to, toStart + i); } } else if (fromLen > length) { - if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(from, fromStart + i) === + CHAR_FORWARD_SLASH) { // We get here if `to` is the exact base path for `from`. // For example: from='/foo/bar/baz'; to='/foo/bar' lastCommonSep = i; @@ -1109,14 +1152,15 @@ const posix = { // Generate the relative path based on the path difference between `to` // and `from`. for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { - if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (i === fromEnd || + StringPrototypeCharCodeAt(from, i) === CHAR_FORWARD_SLASH) { out += out.length === 0 ? '..' : '/..'; } } // Lastly, append the rest of the destination (`to`) path that comes after // the common path parts. - return `${out}${to.slice(toStart + lastCommonSep)}`; + return `${out}${StringPrototypeSlice(to, toStart + lastCommonSep)}`; }, toNamespacedPath(path) { @@ -1128,11 +1172,11 @@ const posix = { validateString(path, 'path'); if (path.length === 0) return '.'; - const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const hasRoot = StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; let end = -1; let matchedSlash = true; for (let i = path.length - 1; i >= 1; --i) { - if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(path, i) === CHAR_FORWARD_SLASH) { if (!matchedSlash) { end = i; break; @@ -1147,7 +1191,7 @@ const posix = { return hasRoot ? '/' : '.'; if (hasRoot && end === 1) return '//'; - return path.slice(0, end); + return StringPrototypeSlice(path, 0, end); }, basename(path, ext) { @@ -1165,7 +1209,7 @@ const posix = { let extIdx = ext.length - 1; let firstNonSlashEnd = -1; for (let i = path.length - 1; i >= 0; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1182,7 +1226,7 @@ const posix = { } if (extIdx >= 0) { // Try to match the explicit extension - if (code === ext.charCodeAt(extIdx)) { + if (code === StringPrototypeCharCodeAt(ext, extIdx)) { if (--extIdx === -1) { // We matched the extension, so mark this as the end of our path // component @@ -1202,10 +1246,10 @@ const posix = { end = firstNonSlashEnd; else if (end === -1) end = path.length; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); } for (let i = path.length - 1; i >= 0; --i) { - if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(path, i) === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now if (!matchedSlash) { @@ -1222,7 +1266,7 @@ const posix = { if (end === -1) return ''; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); }, extname(path) { @@ -1235,7 +1279,7 @@ const posix = { // after any path separator we find let preDotState = 0; for (let i = path.length - 1; i >= 0; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1274,7 +1318,7 @@ const posix = { startDot === startPart + 1)) { return ''; } - return path.slice(startDot, end); + return StringPrototypeSlice(path, startDot, end); }, format: _format.bind(null, '/'), @@ -1285,7 +1329,8 @@ const posix = { const ret = { root: '', dir: '', base: '', ext: '', name: '' }; if (path.length === 0) return ret; - const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const isAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; let start; if (isAbsolute) { ret.root = '/'; @@ -1305,7 +1350,7 @@ const posix = { // Get non-dir info for (; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1343,16 +1388,16 @@ const posix = { (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) { - ret.base = ret.name = path.slice(start, end); + ret.base = ret.name = StringPrototypeSlice(path, start, end); } else { - ret.name = path.slice(start, startDot); - ret.base = path.slice(start, end); - ret.ext = path.slice(startDot, end); + ret.name = StringPrototypeSlice(path, start, startDot); + ret.base = StringPrototypeSlice(path, start, end); + ret.ext = StringPrototypeSlice(path, startDot, end); } } if (startPart > 0) - ret.dir = path.slice(0, startPart - 1); + ret.dir = StringPrototypeSlice(path, 0, startPart - 1); else if (isAbsolute) ret.dir = '/';