Skip to content

Commit ab084c1

Browse files
aduh95danielleadams
authored andcommitted
querystring: refactor to use more primordials
PR-URL: #36315 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent e7b2d91 commit ab084c1

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎lib/internal/querystring.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
const{
44
Array,
55
Int8Array,
6+
NumberPrototypeToString,
7+
StringPrototypeCharCodeAt,
8+
StringPrototypeSlice,
9+
StringPrototypeToUpperCase,
610
}=primordials;
711

812
const{ERR_INVALID_URI}=require('internal/errors').codes;
913

1014
consthexTable=newArray(256);
1115
for(leti=0;i<256;++i)
12-
hexTable[i]='%'+((i<16 ? '0' : '')+i.toString(16)).toUpperCase();
16+
hexTable[i]='%'+
17+
StringPrototypeToUpperCase((i<16 ? '0' : '')+
18+
NumberPrototypeToString(i,16));
1319

1420
constisHexTable=newInt8Array([
1521
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,// 0 - 15
@@ -41,25 +47,25 @@ function encodeStr(str, noEscapeTable, hexTable) {
4147

4248
outer:
4349
for(;i<len;i++){
44-
letc=str.charCodeAt(i);
50+
letc=StringPrototypeCharCodeAt(str,i);
4551

4652
// ASCII
4753
while(c<0x80){
4854
if(noEscapeTable[c]!==1){
4955
if(lastPos<i)
50-
out+=str.slice(lastPos,i);
56+
out+=StringPrototypeSlice(str,lastPos,i);
5157
lastPos=i+1;
5258
out+=hexTable[c];
5359
}
5460

5561
if(++i===len)
5662
break outer;
5763

58-
c=str.charCodeAt(i);
64+
c=StringPrototypeCharCodeAt(str,i);
5965
}
6066

6167
if(lastPos<i)
62-
out+=str.slice(lastPos,i);
68+
out+=StringPrototypeSlice(str,lastPos,i);
6369

6470
// Multi-byte characters ...
6571
if(c<0x800){
@@ -84,7 +90,7 @@ function encodeStr(str, noEscapeTable, hexTable) {
8490
if(i>=len)
8591
thrownewERR_INVALID_URI();
8692

87-
constc2=str.charCodeAt(i)&0x3FF;
93+
constc2=StringPrototypeCharCodeAt(str,i)&0x3FF;
8894

8995
lastPos=i+1;
9096
c=0x10000+(((c&0x3FF)<<10)|c2);
@@ -96,7 +102,7 @@ function encodeStr(str, noEscapeTable, hexTable) {
96102
if(lastPos===0)
97103
returnstr;
98104
if(lastPos<len)
99-
returnout+str.slice(lastPos);
105+
returnout+StringPrototypeSlice(str,lastPos);
100106
returnout;
101107
}
102108

‎lib/querystring.js‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const {
3232
ObjectCreate,
3333
ObjectKeys,
3434
String,
35+
StringPrototypeCharCodeAt,
36+
StringPrototypeSlice,
3537
}=primordials;
3638

3739
const{ Buffer }=require('buffer');
@@ -86,20 +88,20 @@ function unescapeBuffer(s, decodeSpaces) {
8688
// Flag to know if some hex chars have been decoded
8789
lethasHex=false;
8890
while(index<s.length){
89-
currentChar=s.charCodeAt(index);
91+
currentChar=StringPrototypeCharCodeAt(s,index);
9092
if(currentChar===43/* '+' */&&decodeSpaces){
9193
out[outIndex++]=32;// ' '
9294
index++;
9395
continue;
9496
}
9597
if(currentChar===37/* '%' */&&index<maxLength){
96-
currentChar=s.charCodeAt(++index);
98+
currentChar=StringPrototypeCharCodeAt(s,++index);
9799
hexHigh=unhexTable[currentChar];
98100
if(!(hexHigh>=0)){
99101
out[outIndex++]=37;// '%'
100102
continue;
101103
}else{
102-
nextChar=s.charCodeAt(++index);
104+
nextChar=StringPrototypeCharCodeAt(s,++index);
103105
hexLow=unhexTable[nextChar];
104106
if(!(hexLow>=0)){
105107
out[outIndex++]=37;// '%'
@@ -231,10 +233,10 @@ function stringify(obj, sep, eq, options) {
231233

232234
functioncharCodes(str){
233235
if(str.length===0)return[];
234-
if(str.length===1)return[str.charCodeAt(0)];
236+
if(str.length===1)return[StringPrototypeCharCodeAt(str,0)];
235237
constret=newArray(str.length);
236238
for(leti=0;i<str.length;++i)
237-
ret[i]=str.charCodeAt(i);
239+
ret[i]=StringPrototypeCharCodeAt(str,i);
238240
returnret;
239241
}
240242
constdefSepCodes=[38];// &
@@ -268,8 +270,8 @@ function parse(qs, sep, eq, options) {
268270
returnobj;
269271
}
270272

271-
constsepCodes=(!sep ? defSepCodes : charCodes(sep+''));
272-
consteqCodes=(!eq ? defEqCodes : charCodes(eq+''));
273+
constsepCodes=(!sep ? defSepCodes : charCodes(String(sep)));
274+
consteqCodes=(!eq ? defEqCodes : charCodes(String(eq)));
273275
constsepLen=sepCodes.length;
274276
consteqLen=eqCodes.length;
275277

@@ -300,7 +302,7 @@ function parse(qs, sep, eq, options) {
300302
constplusChar=(customDecode ? '%20' : ' ');
301303
letencodeCheck=0;
302304
for(leti=0;i<qs.length;++i){
303-
constcode=qs.charCodeAt(i);
305+
constcode=StringPrototypeCharCodeAt(qs,i);
304306

305307
// Try matching key/value pair separator (e.g. '&')
306308
if(code===sepCodes[sepIdx]){
@@ -311,7 +313,7 @@ function parse(qs, sep, eq, options) {
311313
// We didn't find the (entire) key/value separator
312314
if(lastPos<end){
313315
// Treat the substring as part of the key instead of the value
314-
key+=qs.slice(lastPos,end);
316+
key+=StringPrototypeSlice(qs,lastPos,end);
315317
}elseif(key.length===0){
316318
// We saw an empty substring between separators
317319
if(--pairs===0)
@@ -321,7 +323,7 @@ function parse(qs, sep, eq, options) {
321323
continue;
322324
}
323325
}elseif(lastPos<end){
324-
value+=qs.slice(lastPos,end);
326+
value+=StringPrototypeSlice(qs,lastPos,end);
325327
}
326328

327329
addKeyVal(obj,key,value,keyEncoded,valEncoded,decode);
@@ -343,7 +345,7 @@ function parse(qs, sep, eq, options) {
343345
// Key/value separator match!
344346
constend=i-eqIdx+1;
345347
if(lastPos<end)
346-
key+=qs.slice(lastPos,end);
348+
key+=StringPrototypeSlice(qs,lastPos,end);
347349
encodeCheck=0;
348350
lastPos=i+1;
349351
}
@@ -369,15 +371,15 @@ function parse(qs, sep, eq, options) {
369371
}
370372
if(code===43/* + */){
371373
if(lastPos<i)
372-
key+=qs.slice(lastPos,i);
374+
key+=StringPrototypeSlice(qs,lastPos,i);
373375
key+=plusChar;
374376
lastPos=i+1;
375377
continue;
376378
}
377379
}
378380
if(code===43/* + */){
379381
if(lastPos<i)
380-
value+=qs.slice(lastPos,i);
382+
value+=StringPrototypeSlice(qs,lastPos,i);
381383
value+=plusChar;
382384
lastPos=i+1;
383385
}elseif(!valEncoded){
@@ -400,9 +402,9 @@ function parse(qs, sep, eq, options) {
400402
// Deal with any leftover key or value data
401403
if(lastPos<qs.length){
402404
if(eqIdx<eqLen)
403-
key+=qs.slice(lastPos);
405+
key+=StringPrototypeSlice(qs,lastPos);
404406
elseif(sepIdx<sepLen)
405-
value+=qs.slice(lastPos);
407+
value+=StringPrototypeSlice(qs,lastPos);
406408
}elseif(eqIdx===0&&key.length===0){
407409
// We ended on an empty substring
408410
returnobj;

0 commit comments

Comments
 (0)