Skip to content

Commit daeb348

Browse files
ZYSzysrvagg
authored andcommitted
lib: move encodeStr function to internal for reusable
PR-URL: #24242 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 91494bf commit daeb348

3 files changed

Lines changed: 70 additions & 117 deletions

File tree

‎lib/internal/querystring.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const{ERR_INVALID_URI}=require('internal/errors').codes;
4+
35
consthexTable=newArray(256);
46
for(vari=0;i<256;++i)
57
hexTable[i]='%'+((i<16 ? '0' : '')+i.toString(16)).toUpperCase();
@@ -23,7 +25,72 @@ const isHexTable = [
2325
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0// ... 256
2426
];
2527

28+
functionencodeStr(str,noEscapeTable,hexTable){
29+
constlen=str.length;
30+
if(len===0)
31+
return'';
32+
33+
varout='';
34+
varlastPos=0;
35+
36+
for(vari=0;i<len;i++){
37+
varc=str.charCodeAt(i);
38+
39+
// ASCII
40+
if(c<0x80){
41+
if(noEscapeTable[c]===1)
42+
continue;
43+
if(lastPos<i)
44+
out+=str.slice(lastPos,i);
45+
lastPos=i+1;
46+
out+=hexTable[c];
47+
continue;
48+
}
49+
50+
if(lastPos<i)
51+
out+=str.slice(lastPos,i);
52+
53+
// Multi-byte characters ...
54+
if(c<0x800){
55+
lastPos=i+1;
56+
out+=hexTable[0xC0|(c>>6)]+
57+
hexTable[0x80|(c&0x3F)];
58+
continue;
59+
}
60+
if(c<0xD800||c>=0xE000){
61+
lastPos=i+1;
62+
out+=hexTable[0xE0|(c>>12)]+
63+
hexTable[0x80|((c>>6)&0x3F)]+
64+
hexTable[0x80|(c&0x3F)];
65+
continue;
66+
}
67+
// Surrogate pair
68+
++i;
69+
70+
// This branch should never happen because all URLSearchParams entries
71+
// should already be converted to USVString. But, included for
72+
// completion's sake anyway.
73+
if(i>=len)
74+
thrownewERR_INVALID_URI();
75+
76+
varc2=str.charCodeAt(i)&0x3FF;
77+
78+
lastPos=i+1;
79+
c=0x10000+(((c&0x3FF)<<10)|c2);
80+
out+=hexTable[0xF0|(c>>18)]+
81+
hexTable[0x80|((c>>12)&0x3F)]+
82+
hexTable[0x80|((c>>6)&0x3F)]+
83+
hexTable[0x80|(c&0x3F)];
84+
}
85+
if(lastPos===0)
86+
returnstr;
87+
if(lastPos<len)
88+
returnout+str.slice(lastPos);
89+
returnout;
90+
}
91+
2692
module.exports={
93+
encodeStr,
2794
hexTable,
2895
isHexTable
2996
};

‎lib/internal/url.js‎

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
constutil=require('util');
44
const{
5+
encodeStr,
56
hexTable,
67
isHexTable
78
}=require('internal/querystring');
@@ -833,70 +834,6 @@ const noEscape = [
833834
constparamHexTable=hexTable.slice();
834835
paramHexTable[0x20]='+';
835836

836-
functionencodeStr(str,noEscapeTable,hexTable){
837-
constlen=str.length;
838-
if(len===0)
839-
return'';
840-
841-
varout='';
842-
varlastPos=0;
843-
844-
for(vari=0;i<len;i++){
845-
varc=str.charCodeAt(i);
846-
847-
// ASCII
848-
if(c<0x80){
849-
if(noEscapeTable[c]===1)
850-
continue;
851-
if(lastPos<i)
852-
out+=str.slice(lastPos,i);
853-
lastPos=i+1;
854-
out+=hexTable[c];
855-
continue;
856-
}
857-
858-
if(lastPos<i)
859-
out+=str.slice(lastPos,i);
860-
861-
// Multi-byte characters ...
862-
if(c<0x800){
863-
lastPos=i+1;
864-
out+=hexTable[0xC0|(c>>6)]+
865-
hexTable[0x80|(c&0x3F)];
866-
continue;
867-
}
868-
if(c<0xD800||c>=0xE000){
869-
lastPos=i+1;
870-
out+=hexTable[0xE0|(c>>12)]+
871-
hexTable[0x80|((c>>6)&0x3F)]+
872-
hexTable[0x80|(c&0x3F)];
873-
continue;
874-
}
875-
// Surrogate pair
876-
++i;
877-
varc2;
878-
if(i<len)
879-
c2=str.charCodeAt(i)&0x3FF;
880-
else{
881-
// This branch should never happen because all URLSearchParams entries
882-
// should already be converted to USVString. But, included for
883-
// completion's sake anyway.
884-
c2=0;
885-
}
886-
lastPos=i+1;
887-
c=0x10000+(((c&0x3FF)<<10)|c2);
888-
out+=hexTable[0xF0|(c>>18)]+
889-
hexTable[0x80|((c>>12)&0x3F)]+
890-
hexTable[0x80|((c>>6)&0x3F)]+
891-
hexTable[0x80|(c&0x3F)];
892-
}
893-
if(lastPos===0)
894-
returnstr;
895-
if(lastPos<len)
896-
returnout+str.slice(lastPos);
897-
returnout;
898-
}
899-
900837
// application/x-www-form-urlencoded serializer
901838
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-serializer
902839
functionserializeParams(array){

‎lib/querystring.js‎

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
'use strict';
2525

2626
const{ Buffer }=require('buffer');
27-
const{ERR_INVALID_URI}=require('internal/errors').codes;
2827
const{
28+
encodeStr,
2929
hexTable,
3030
isHexTable
3131
}=require('internal/querystring');
@@ -140,59 +140,8 @@ function qsEscape(str) {
140140
else
141141
str+='';
142142
}
143-
varout='';
144-
varlastPos=0;
145-
146-
for(vari=0;i<str.length;++i){
147-
varc=str.charCodeAt(i);
148-
149-
// ASCII
150-
if(c<0x80){
151-
if(noEscape[c]===1)
152-
continue;
153-
if(lastPos<i)
154-
out+=str.slice(lastPos,i);
155-
lastPos=i+1;
156-
out+=hexTable[c];
157-
continue;
158-
}
159143

160-
if(lastPos<i)
161-
out+=str.slice(lastPos,i);
162-
163-
// Multi-byte characters ...
164-
if(c<0x800){
165-
lastPos=i+1;
166-
out+=hexTable[0xC0|(c>>6)]+hexTable[0x80|(c&0x3F)];
167-
continue;
168-
}
169-
if(c<0xD800||c>=0xE000){
170-
lastPos=i+1;
171-
out+=hexTable[0xE0|(c>>12)]+
172-
hexTable[0x80|((c>>6)&0x3F)]+
173-
hexTable[0x80|(c&0x3F)];
174-
continue;
175-
}
176-
// Surrogate pair
177-
++i;
178-
179-
if(i>=str.length)
180-
thrownewERR_INVALID_URI();
181-
182-
varc2=str.charCodeAt(i)&0x3FF;
183-
184-
lastPos=i+1;
185-
c=0x10000+(((c&0x3FF)<<10)|c2);
186-
out+=hexTable[0xF0|(c>>18)]+
187-
hexTable[0x80|((c>>12)&0x3F)]+
188-
hexTable[0x80|((c>>6)&0x3F)]+
189-
hexTable[0x80|(c&0x3F)];
190-
}
191-
if(lastPos===0)
192-
returnstr;
193-
if(lastPos<str.length)
194-
returnout+str.slice(lastPos);
195-
returnout;
144+
returnencodeStr(str,noEscape,hexTable);
196145
}
197146

198147
functionstringifyPrimitive(v){

0 commit comments

Comments
 (0)