Skip to content

Commit a5b0d89

Browse files
gurgundaymarco-ippolito
authored andcommitted
lib: remove startsWith/endsWith primordials for char checks
PR-URL: #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>
1 parent 50b6729 commit a5b0d89

11 files changed

Lines changed: 17 additions & 22 deletions

File tree

‎lib/_http_agent.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ function calculateServerName(options, req) {
347347
// abc:123 => abc
348348
// [::1] => ::1
349349
// [::1]:123 => ::1
350-
if(hostHeader.startsWith('[')){
350+
if(hostHeader[0]==='['){
351351
constindex=hostHeader.indexOf(']');
352352
if(index===-1){
353353
// Leading '[', but no ']'. Need to do something...

‎lib/internal/main/watch_mode.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ for (let i = 0; i < process.execArgv.length; i++) {
4545
if(StringPrototypeStartsWith(arg,'--watch')){
4646
i++;
4747
constnextArg=process.execArgv[i];
48-
if(nextArg&&StringPrototypeStartsWith(nextArg,'-')){
48+
if(nextArg&&nextArg[0]==='-'){
4949
ArrayPrototypePush(argsWithoutWatchOptions,nextArg);
5050
}
5151
continue;

‎lib/internal/modules/esm/fetch_module.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ const {
33
ObjectPrototypeHasOwnProperty,
44
PromisePrototypeThen,
55
SafeMap,
6-
StringPrototypeEndsWith,
76
StringPrototypeSlice,
8-
StringPrototypeStartsWith,
97
}=primordials;
108
const{
119
Buffer: {concat: BufferConcat},
@@ -248,8 +246,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
248246
asyncfunctionisLocalAddress(hostname){
249247
try{
250248
if(
251-
StringPrototypeStartsWith(hostname,'[')&&
252-
StringPrototypeEndsWith(hostname,']')
249+
hostname.length&&
250+
hostname[0]==='['&&
251+
hostname[hostname.length-1]===']'
253252
){
254253
hostname=StringPrototypeSlice(hostname,1,-1);
255254
}

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,9 @@ function resolvePackageTargetString(
399399
}
400400

401401
if(!StringPrototypeStartsWith(target,'./')){
402-
if(internal&&!StringPrototypeStartsWith(target,'../')&&
403-
!StringPrototypeStartsWith(target,'/')){
402+
if(internal&&
403+
target[0]!=='/'&&
404+
!StringPrototypeStartsWith(target,'../')){
404405
// No need to convert target to string, since it's already presumed to be
405406
if(!URLCanParse(target)){
406407
constexportTarget=pattern ?

‎lib/internal/modules/helpers.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
244244
ArrayPrototypeForEach(builtinModules,(name)=>{
245245
// Neither add underscored modules, nor ones that contain slashes (e.g.,
246246
// 'fs/promises') or ones that are already defined.
247-
if(StringPrototypeStartsWith(name,'_')||
247+
if(name[0]==='_'||
248248
StringPrototypeIncludes(name,'/')||
249249
ObjectPrototypeHasOwnProperty(object,name)){
250250
return;

‎lib/internal/process/per_thread.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const {
2525
StringPrototypeEndsWith,
2626
StringPrototypeReplace,
2727
StringPrototypeSlice,
28-
StringPrototypeStartsWith,
2928
Symbol,
3029
SymbolIterator,
3130
}=primordials;
@@ -302,7 +301,7 @@ function buildAllowedFlags() {
302301
}
303302

304303
functionisAccepted(to){
305-
if(!StringPrototypeStartsWith(to,'-')||to==='--')returntrue;
304+
if(!to.length||to[0]!=='-'||to==='--')returntrue;
306305
constrecursiveExpansion=aliases.get(to);
307306
if(recursiveExpansion){
308307
if(recursiveExpansion[0]===to)

‎lib/internal/process/pre_execution.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
ObjectGetOwnPropertyDescriptor,
1616
SafeMap,
1717
String,
18-
StringPrototypeStartsWith,
1918
Symbol,
2019
globalThis,
2120
}=primordials;
@@ -244,8 +243,7 @@ function patchProcessObject(expandArgv1) {
244243
letmainEntry;
245244
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
246245
// the entry point.
247-
if(expandArgv1&&process.argv[1]&&
248-
!StringPrototypeStartsWith(process.argv[1],'-')){
246+
if(expandArgv1&&process.argv[1]&&process.argv[1][0]!=='-'){
249247
// Expand process.argv[1] into a full path.
250248
constpath=require('path');
251249
try{

‎lib/internal/repl/utils.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ const {
1010
RegExpPrototypeExec,
1111
SafeSet,
1212
SafeStringIterator,
13-
StringPrototypeEndsWith,
1413
StringPrototypeIndexOf,
1514
StringPrototypeLastIndexOf,
1615
StringPrototypeReplaceAll,
1716
StringPrototypeSlice,
18-
StringPrototypeStartsWith,
1917
StringPrototypeToLowerCase,
2018
StringPrototypeTrim,
2119
Symbol,
@@ -298,8 +296,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
298296
functiongetInputPreview(input,callback){
299297
// For similar reasons as `defaultEval`, wrap expressions starting with a
300298
// curly brace with parenthesis.
301-
if(StringPrototypeStartsWith(input,'{')&&
302-
!StringPrototypeEndsWith(input,';')&&!wrapped){
299+
if(!wrapped&&input[0]==='{'&&input[input.length-1]!==';'){
303300
input=`(${input})`;
304301
wrapped=true;
305302
}

‎lib/internal/url.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ function urlToHttpOptions(url) {
14081408
__proto__: null,
14091409
...url,// In case the url object was extended by the user.
14101410
protocol: url.protocol,
1411-
hostname: hostname&&StringPrototypeStartsWith(hostname,'[') ?
1411+
hostname: hostname&&hostname[0]==='[' ?
14121412
StringPrototypeSlice(hostname,1,-1) :
14131413
hostname,
14141414
hash: url.hash,

‎lib/internal/util/inspect.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ function getClassBase(value, constructor, tag) {
11811181

11821182
functiongetFunctionBase(value,constructor,tag){
11831183
conststringified=FunctionPrototypeToString(value);
1184-
if(StringPrototypeStartsWith(stringified,'class')&&StringPrototypeEndsWith(stringified,'}')){
1184+
if(StringPrototypeStartsWith(stringified,'class')&&stringified[stringified.length-1]==='}'){
11851185
constslice=StringPrototypeSlice(stringified,5,-1);
11861186
constbracketIndex=StringPrototypeIndexOf(slice,'{');
11871187
if(bracketIndex!==-1&&
@@ -1560,7 +1560,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
15601560
functionaddNumericSeparator(integerString){
15611561
letresult='';
15621562
leti=integerString.length;
1563-
conststart=StringPrototypeStartsWith(integerString,'-') ? 1 : 0;
1563+
assert(i!==0);
1564+
conststart=integerString[0]==='-' ? 1 : 0;
15641565
for(;i>=start+4;i-=3){
15651566
result=`_${StringPrototypeSlice(integerString,i-3,i)}${result}`;
15661567
}

0 commit comments

Comments
 (0)