Skip to content

Commit 0f6c3f7

Browse files
aduh95targos
authored andcommitted
querystring: refactor to use more primordials
PR-URL: #36315 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent a1b4681 commit 0f6c3f7

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/internal/querystring.js

+13-7
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
const hexTable = new Array(256);
1115
for (let i = 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
const isHexTable = new Int8Array([
1521
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
@@ -47,25 +53,25 @@ function encodeStr(str, noEscapeTable, hexTable) {
4753

4854
outer:
4955
for (; i < len; i++) {
50-
let c = str.charCodeAt(i);
56+
let c = StringPrototypeCharCodeAt(str, i);
5157

5258
// ASCII
5359
while (c < 0x80) {
5460
if (noEscapeTable[c] !== 1) {
5561
if (lastPos < i)
56-
out += str.slice(lastPos, i);
62+
out += StringPrototypeSlice(str, lastPos, i);
5763
lastPos = i + 1;
5864
out += hexTable[c];
5965
}
6066

6167
if (++i === len)
6268
break outer;
6369

64-
c = str.charCodeAt(i);
70+
c = StringPrototypeCharCodeAt(str, i);
6571
}
6672

6773
if (lastPos < i)
68-
out += str.slice(lastPos, i);
74+
out += StringPrototypeSlice(str, lastPos, i);
6975

7076
// Multi-byte characters ...
7177
if (c < 0x800) {
@@ -90,7 +96,7 @@ function encodeStr(str, noEscapeTable, hexTable) {
9096
if (i >= len)
9197
throw new ERR_INVALID_URI();
9298

93-
const c2 = str.charCodeAt(i) & 0x3FF;
99+
const c2 = StringPrototypeCharCodeAt(str, i) & 0x3FF;
94100

95101
lastPos = i + 1;
96102
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
@@ -102,7 +108,7 @@ function encodeStr(str, noEscapeTable, hexTable) {
102108
if (lastPos === 0)
103109
return str;
104110
if (lastPos < len)
105-
return out + str.slice(lastPos);
111+
return out + StringPrototypeSlice(str, lastPos);
106112
return out;
107113
}
108114

lib/querystring.js

+17-15
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');
@@ -91,20 +93,20 @@ function unescapeBuffer(s, decodeSpaces) {
9193
// Flag to know if some hex chars have been decoded
9294
let hasHex = false;
9395
while (index < s.length) {
94-
currentChar = s.charCodeAt(index);
96+
currentChar = StringPrototypeCharCodeAt(s, index);
9597
if (currentChar === 43 /* '+' */ && decodeSpaces) {
9698
out[outIndex++] = 32; // ' '
9799
index++;
98100
continue;
99101
}
100102
if (currentChar === 37 /* '%' */ && index < maxLength) {
101-
currentChar = s.charCodeAt(++index);
103+
currentChar = StringPrototypeCharCodeAt(s, ++index);
102104
hexHigh = unhexTable[currentChar];
103105
if (!(hexHigh >= 0)) {
104106
out[outIndex++] = 37; // '%'
105107
continue;
106108
} else {
107-
nextChar = s.charCodeAt(++index);
109+
nextChar = StringPrototypeCharCodeAt(s, ++index);
108110
hexLow = unhexTable[nextChar];
109111
if (!(hexLow >= 0)) {
110112
out[outIndex++] = 37; // '%'
@@ -272,10 +274,10 @@ function stringify(obj, sep, eq, options) {
272274
*/
273275
function charCodes(str) {
274276
if (str.length === 0) return [];
275-
if (str.length === 1) return [str.charCodeAt(0)];
277+
if (str.length === 1) return [StringPrototypeCharCodeAt(str, 0)];
276278
const ret = new Array(str.length);
277279
for (let i = 0; i < str.length; ++i)
278-
ret[i] = str.charCodeAt(i);
280+
ret[i] = StringPrototypeCharCodeAt(str, i);
279281
return ret;
280282
}
281283
const defSepCodes = [38]; // &
@@ -319,8 +321,8 @@ function parse(qs, sep, eq, options) {
319321
return obj;
320322
}
321323

322-
const sepCodes = (!sep ? defSepCodes : charCodes(sep + ''));
323-
const eqCodes = (!eq ? defEqCodes : charCodes(eq + ''));
324+
const sepCodes = (!sep ? defSepCodes : charCodes(String(sep)));
325+
const eqCodes = (!eq ? defEqCodes : charCodes(String(eq)));
324326
const sepLen = sepCodes.length;
325327
const eqLen = eqCodes.length;
326328

@@ -351,7 +353,7 @@ function parse(qs, sep, eq, options) {
351353
const plusChar = (customDecode ? '%20' : ' ');
352354
let encodeCheck = 0;
353355
for (let i = 0; i < qs.length; ++i) {
354-
const code = qs.charCodeAt(i);
356+
const code = StringPrototypeCharCodeAt(qs, i);
355357

356358
// Try matching key/value pair separator (e.g. '&')
357359
if (code === sepCodes[sepIdx]) {
@@ -362,7 +364,7 @@ function parse(qs, sep, eq, options) {
362364
// We didn't find the (entire) key/value separator
363365
if (lastPos < end) {
364366
// Treat the substring as part of the key instead of the value
365-
key += qs.slice(lastPos, end);
367+
key += StringPrototypeSlice(qs, lastPos, end);
366368
} else if (key.length === 0) {
367369
// We saw an empty substring between separators
368370
if (--pairs === 0)
@@ -372,7 +374,7 @@ function parse(qs, sep, eq, options) {
372374
continue;
373375
}
374376
} else if (lastPos < end) {
375-
value += qs.slice(lastPos, end);
377+
value += StringPrototypeSlice(qs, lastPos, end);
376378
}
377379

378380
addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);
@@ -394,7 +396,7 @@ function parse(qs, sep, eq, options) {
394396
// Key/value separator match!
395397
const end = i - eqIdx + 1;
396398
if (lastPos < end)
397-
key += qs.slice(lastPos, end);
399+
key += StringPrototypeSlice(qs, lastPos, end);
398400
encodeCheck = 0;
399401
lastPos = i + 1;
400402
}
@@ -420,15 +422,15 @@ function parse(qs, sep, eq, options) {
420422
}
421423
if (code === 43/* + */) {
422424
if (lastPos < i)
423-
key += qs.slice(lastPos, i);
425+
key += StringPrototypeSlice(qs, lastPos, i);
424426
key += plusChar;
425427
lastPos = i + 1;
426428
continue;
427429
}
428430
}
429431
if (code === 43/* + */) {
430432
if (lastPos < i)
431-
value += qs.slice(lastPos, i);
433+
value += StringPrototypeSlice(qs, lastPos, i);
432434
value += plusChar;
433435
lastPos = i + 1;
434436
} else if (!valEncoded) {
@@ -451,9 +453,9 @@ function parse(qs, sep, eq, options) {
451453
// Deal with any leftover key or value data
452454
if (lastPos < qs.length) {
453455
if (eqIdx < eqLen)
454-
key += qs.slice(lastPos);
456+
key += StringPrototypeSlice(qs, lastPos);
455457
else if (sepIdx < sepLen)
456-
value += qs.slice(lastPos);
458+
value += StringPrototypeSlice(qs, lastPos);
457459
} else if (eqIdx === 0 && key.length === 0) {
458460
// We ended on an empty substring
459461
return obj;

0 commit comments

Comments
 (0)