lib: remove startsWith/endsWith primordials for char checks

PR-URL: https://github.com/nodejs/node/pull/55407
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
Gürgün Dayıoğlu 2024-10-19 12:18:10 +02:00 committed by Ruy Adorno
parent 6e3e99c81e
commit 753cbede2a
No known key found for this signature in database
11 changed files with 17 additions and 22 deletions

View file

@ -341,7 +341,7 @@ function calculateServerName(options, req) {
// abc:123 => abc // abc:123 => abc
// [::1] => ::1 // [::1] => ::1
// [::1]:123 => ::1 // [::1]:123 => ::1
if (hostHeader.startsWith('[')) { if (hostHeader[0] === '[') {
const index = hostHeader.indexOf(']'); const index = hostHeader.indexOf(']');
if (index === -1) { if (index === -1) {
// Leading '[', but no ']'. Need to do something... // Leading '[', but no ']'. Need to do something...

View file

@ -46,7 +46,7 @@ for (let i = 0; i < process.execArgv.length; i++) {
if (StringPrototypeStartsWith(arg, '--watch')) { if (StringPrototypeStartsWith(arg, '--watch')) {
i++; i++;
const nextArg = process.execArgv[i]; const nextArg = process.execArgv[i];
if (nextArg && StringPrototypeStartsWith(nextArg, '-')) { if (nextArg && nextArg[0] === '-') {
ArrayPrototypePush(argsWithoutWatchOptions, nextArg); ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
} }
continue; continue;

View file

@ -3,9 +3,7 @@ const {
ObjectPrototypeHasOwnProperty, ObjectPrototypeHasOwnProperty,
PromisePrototypeThen, PromisePrototypeThen,
SafeMap, SafeMap,
StringPrototypeEndsWith,
StringPrototypeSlice, StringPrototypeSlice,
StringPrototypeStartsWith,
} = primordials; } = primordials;
const { const {
Buffer: { concat: BufferConcat }, Buffer: { concat: BufferConcat },
@ -248,8 +246,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
async function isLocalAddress(hostname) { async function isLocalAddress(hostname) {
try { try {
if ( if (
StringPrototypeStartsWith(hostname, '[') && hostname.length &&
StringPrototypeEndsWith(hostname, ']') hostname[0] === '[' &&
hostname[hostname.length - 1] === ']'
) { ) {
hostname = StringPrototypeSlice(hostname, 1, -1); hostname = StringPrototypeSlice(hostname, 1, -1);
} }

View file

@ -391,8 +391,9 @@ function resolvePackageTargetString(
} }
if (!StringPrototypeStartsWith(target, './')) { if (!StringPrototypeStartsWith(target, './')) {
if (internal && !StringPrototypeStartsWith(target, '../') && if (internal &&
!StringPrototypeStartsWith(target, '/')) { target[0] !== '/' &&
!StringPrototypeStartsWith(target, '../')) {
// No need to convert target to string, since it's already presumed to be // No need to convert target to string, since it's already presumed to be
if (!URLCanParse(target)) { if (!URLCanParse(target)) {
const exportTarget = pattern ? const exportTarget = pattern ?

View file

@ -202,7 +202,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
ArrayPrototypeForEach(builtinModules, (name) => { ArrayPrototypeForEach(builtinModules, (name) => {
// Neither add underscored modules, nor ones that contain slashes (e.g., // Neither add underscored modules, nor ones that contain slashes (e.g.,
// 'fs/promises') or ones that are already defined. // 'fs/promises') or ones that are already defined.
if (StringPrototypeStartsWith(name, '_') || if (name[0] === '_' ||
StringPrototypeIncludes(name, '/') || StringPrototypeIncludes(name, '/') ||
ObjectPrototypeHasOwnProperty(object, name)) { ObjectPrototypeHasOwnProperty(object, name)) {
return; return;

View file

@ -25,7 +25,6 @@ const {
StringPrototypeEndsWith, StringPrototypeEndsWith,
StringPrototypeReplace, StringPrototypeReplace,
StringPrototypeSlice, StringPrototypeSlice,
StringPrototypeStartsWith,
Symbol, Symbol,
SymbolIterator, SymbolIterator,
} = primordials; } = primordials;
@ -300,7 +299,7 @@ function buildAllowedFlags() {
} }
function isAccepted(to) { function isAccepted(to) {
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true; if (!to.length || to[0] !== '-' || to === '--') return true;
const recursiveExpansion = aliases.get(to); const recursiveExpansion = aliases.get(to);
if (recursiveExpansion) { if (recursiveExpansion) {
if (recursiveExpansion[0] === to) if (recursiveExpansion[0] === to)

View file

@ -14,7 +14,6 @@ const {
ObjectFreeze, ObjectFreeze,
ObjectGetOwnPropertyDescriptor, ObjectGetOwnPropertyDescriptor,
String, String,
StringPrototypeStartsWith,
Symbol, Symbol,
globalThis, globalThis,
} = primordials; } = primordials;
@ -242,8 +241,7 @@ function patchProcessObject(expandArgv1) {
let mainEntry; let mainEntry;
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of // If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
// the entry point. // the entry point.
if (expandArgv1 && process.argv[1] && if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') {
!StringPrototypeStartsWith(process.argv[1], '-')) {
// Expand process.argv[1] into a full path. // Expand process.argv[1] into a full path.
const path = require('path'); const path = require('path');
try { try {

View file

@ -10,12 +10,10 @@ const {
RegExpPrototypeExec, RegExpPrototypeExec,
SafeSet, SafeSet,
SafeStringIterator, SafeStringIterator,
StringPrototypeEndsWith,
StringPrototypeIndexOf, StringPrototypeIndexOf,
StringPrototypeLastIndexOf, StringPrototypeLastIndexOf,
StringPrototypeReplaceAll, StringPrototypeReplaceAll,
StringPrototypeSlice, StringPrototypeSlice,
StringPrototypeStartsWith,
StringPrototypeToLowerCase, StringPrototypeToLowerCase,
StringPrototypeTrim, StringPrototypeTrim,
Symbol, Symbol,
@ -298,8 +296,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
function getInputPreview(input, callback) { function getInputPreview(input, callback) {
// For similar reasons as `defaultEval`, wrap expressions starting with a // For similar reasons as `defaultEval`, wrap expressions starting with a
// curly brace with parenthesis. // curly brace with parenthesis.
if (StringPrototypeStartsWith(input, '{') && if (!wrapped && input[0] === '{' && input[input.length - 1] !== ';') {
!StringPrototypeEndsWith(input, ';') && !wrapped) {
input = `(${input})`; input = `(${input})`;
wrapped = true; wrapped = true;
} }

View file

@ -1428,7 +1428,7 @@ function urlToHttpOptions(url) {
__proto__: null, __proto__: null,
...url, // In case the url object was extended by the user. ...url, // In case the url object was extended by the user.
protocol: url.protocol, protocol: url.protocol,
hostname: hostname && StringPrototypeStartsWith(hostname, '[') ? hostname: hostname && hostname[0] === '[' ?
StringPrototypeSlice(hostname, 1, -1) : StringPrototypeSlice(hostname, 1, -1) :
hostname, hostname,
hash: url.hash, hash: url.hash,

View file

@ -1191,7 +1191,7 @@ function getClassBase(value, constructor, tag) {
function getFunctionBase(value, constructor, tag) { function getFunctionBase(value, constructor, tag) {
const stringified = FunctionPrototypeToString(value); const stringified = FunctionPrototypeToString(value);
if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) { if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
const slice = StringPrototypeSlice(stringified, 5, -1); const slice = StringPrototypeSlice(stringified, 5, -1);
const bracketIndex = StringPrototypeIndexOf(slice, '{'); const bracketIndex = StringPrototypeIndexOf(slice, '{');
if (bracketIndex !== -1 && if (bracketIndex !== -1 &&
@ -1570,7 +1570,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
function addNumericSeparator(integerString) { function addNumericSeparator(integerString) {
let result = ''; let result = '';
let i = integerString.length; let i = integerString.length;
const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0; assert(i !== 0);
const start = integerString[0] === '-' ? 1 : 0;
for (; i >= start + 4; i -= 3) { for (; i >= start + 4; i -= 3) {
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`; result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
} }

View file

@ -130,7 +130,7 @@ const { shouldColorize } = require('internal/util/colors');
const CJSModule = require('internal/modules/cjs/loader').Module; const CJSModule = require('internal/modules/cjs/loader').Module;
let _builtinLibs = ArrayPrototypeFilter( let _builtinLibs = ArrayPrototypeFilter(
CJSModule.builtinModules, CJSModule.builtinModules,
(e) => !StringPrototypeStartsWith(e, '_'), (e) => e[0] !== '_',
); );
const nodeSchemeBuiltinLibs = ArrayPrototypeMap( const nodeSchemeBuiltinLibs = ArrayPrototypeMap(
_builtinLibs, (lib) => `node:${lib}`); _builtinLibs, (lib) => `node:${lib}`);