1+ "use strict" ;
2+
13// This file is taken from Node.js project.
24// Full implementation can be found from https://github.com/nodejs/node/blob/main/lib/internal/querystring.js
35
4- const hexTable = Array . from (
5- { length : 256 } ,
6- ( _ , i ) => "%" + ( ( i < 16 ? "0" : "" ) + i . toString ( 16 ) ) . toUpperCase ( ) ,
7- ) ;
6+ // rome-ignore format: the array should not be formatted
7+ const hexTable = [
8+ "%00" , "%01" , "%02" , "%03" , "%04" , "%05" , "%06" , "%07" , "%08" , "%09" , "%0A" , "%0B" , "%0C" , "%0D" , "%0E" , "%0F" ,
9+ "%10" , "%11" , "%12" , "%13" , "%14" , "%15" , "%16" , "%17" , "%18" , "%19" , "%1A" , "%1B" , "%1C" , "%1D" , "%1E" , "%1F" ,
10+ "%20" , "%21" , "%22" , "%23" , "%24" , "%25" , "%26" , "%27" , "%28" , "%29" , "%2A" , "%2B" , "%2C" , "%2D" , "%2E" , "%2F" ,
11+ "%30" , "%31" , "%32" , "%33" , "%34" , "%35" , "%36" , "%37" , "%38" , "%39" , "%3A" , "%3B" , "%3C" , "%3D" , "%3E" , "%3F" ,
12+ "%40" , "%41" , "%42" , "%43" , "%44" , "%45" , "%46" , "%47" , "%48" , "%49" , "%4A" , "%4B" , "%4C" , "%4D" , "%4E" , "%4F" ,
13+ "%50" , "%51" , "%52" , "%53" , "%54" , "%55" , "%56" , "%57" , "%58" , "%59" , "%5A" , "%5B" , "%5C" , "%5D" , "%5E" , "%5F" ,
14+ "%60" , "%61" , "%62" , "%63" , "%64" , "%65" , "%66" , "%67" , "%68" , "%69" , "%6A" , "%6B" , "%6C" , "%6D" , "%6E" , "%6F" ,
15+ "%70" , "%71" , "%72" , "%73" , "%74" , "%75" , "%76" , "%77" , "%78" , "%79" , "%7A" , "%7B" , "%7C" , "%7D" , "%7E" , "%7F" ,
16+ "%80" , "%81" , "%82" , "%83" , "%84" , "%85" , "%86" , "%87" , "%88" , "%89" , "%8A" , "%8B" , "%8C" , "%8D" , "%8E" , "%8F" ,
17+ "%90" , "%91" , "%92" , "%93" , "%94" , "%95" , "%96" , "%97" , "%98" , "%99" , "%9A" , "%9B" , "%9C" , "%9D" , "%9E" , "%9F" ,
18+ "%A0" , "%A1" , "%A2" , "%A3" , "%A4" , "%A5" , "%A6" , "%A7" , "%A8" , "%A9" , "%AA" , "%AB" , "%AC" , "%AD" , "%AE" , "%AF" ,
19+ "%B0" , "%B1" , "%B2" , "%B3" , "%B4" , "%B5" , "%B6" , "%B7" , "%B8" , "%B9" , "%BA" , "%BB" , "%BC" , "%BD" , "%BE" , "%BF" ,
20+ "%C0" , "%C1" , "%C2" , "%C3" , "%C4" , "%C5" , "%C6" , "%C7" , "%C8" , "%C9" , "%CA" , "%CB" , "%CC" , "%CD" , "%CE" , "%CF" ,
21+ "%D0" , "%D1" , "%D2" , "%D3" , "%D4" , "%D5" , "%D6" , "%D7" , "%D8" , "%D9" , "%DA" , "%DB" , "%DC" , "%DD" , "%DE" , "%DF" ,
22+ "%E0" , "%E1" , "%E2" , "%E3" , "%E4" , "%E5" , "%E6" , "%E7" , "%E8" , "%E9" , "%EA" , "%EB" , "%EC" , "%ED" , "%EE" , "%EF" ,
23+ "%F0" , "%F1" , "%F2" , "%F3" , "%F4" , "%F5" , "%F6" , "%F7" , "%F8" , "%F9" , "%FA" , "%FB" , "%FC" , "%FD" , "%FE" , "%FF"
24+ ] ;
25+ const utf16 = require ( "./utf16" ) ;
826
927// These characters do not need escaping when generating query strings:
1028// ! - . _ ~
@@ -21,7 +39,7 @@ const noEscape = new Int8Array([
2139 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // 64 - 79
2240 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , // 80 - 95
2341 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // 96 - 111
24- 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , // 112 - 127
42+ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , // 112 - 127
2543] ) ;
2644
2745/**
@@ -30,63 +48,63 @@ const noEscape = new Int8Array([
3048 */
3149function encodeString ( str ) {
3250 const len = str . length ;
33- if ( len === 0 ) return "" ;
51+ if ( len === 0 ) return str ;
3452
3553 let out = "" ;
3654 let lastPos = 0 ;
3755 let i = 0 ;
56+ let c = 0 ;
3857
39- outer: for ( ; i < len ; i ++ ) {
40- let c = str . charCodeAt ( i ) ;
58+ while ( i < len ) {
59+ c = str . charCodeAt ( i ) ;
4160
4261 // ASCII
43- while ( c < 0x80 ) {
44- if ( noEscape [ c ] !== 1 ) {
62+ if ( c < 0x80 ) {
63+ if ( noEscape [ c ] === 1 ) {
64+ ++ i ;
65+ } else {
4566 if ( lastPos < i ) out += str . slice ( lastPos , i ) ;
46- lastPos = i + 1 ;
67+ lastPos = ++ i ;
4768 out += hexTable [ c ] ;
4869 }
70+ // Multi-byte characters ...
71+ } else {
72+ if ( lastPos < i ) {
73+ out += str . slice ( lastPos , i ) ;
74+ }
4975
50- if ( ++ i === len ) break outer;
51-
52- c = str . charCodeAt ( i ) ;
53- }
76+ if ( c < 0x800 ) {
77+ lastPos = ++ i ;
78+ out += utf16 [ c ] ;
79+ } else if ( c < 0xD800 ) {
80+ lastPos = ++ i ;
81+ out += utf16 [ c ] ;
82+ } else if ( c < 0xE000 ) {
83+ // Surrogate pair
5484
55- if ( lastPos < i ) out += str . slice ( lastPos , i ) ;
85+ // This branch should never happen because all URLSearchParams entries
86+ // should already be converted to USVString. But, included for
87+ // completion's sake anyway.
88+ if ( ++ i === len ) {
89+ throw new Error ( "URI malformed" ) ;
90+ }
5691
57- // Multi-byte characters ...
58- if ( c < 0x800 ) {
59- lastPos = i + 1 ;
60- out += hexTable [ 0xC0 | ( c >> 6 ) ] + hexTable [ 0x80 | ( c & 0x3F ) ] ;
61- continue ;
62- }
63- if ( c < 0xD800 || c >= 0xE000 ) {
64- lastPos = i + 1 ;
65- out +=
66- hexTable [ 0xE0 | ( c >> 12 ) ] +
67- hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] +
68- hexTable [ 0x80 | ( c & 0x3F ) ] ;
69- continue ;
70- }
71- // Surrogate pair
72- ++ i ;
73-
74- // This branch should never happen because all URLSearchParams entries
75- // should already be converted to USVString. But, included for
76- // completion's sake anyway.
77- if ( i >= len ) {
78- throw new Error ( "URI malformed" ) ;
92+ c = 0x10000 + ( ( ( c & 0x3FF ) << 10 ) | ( str . charCodeAt ( i ) & 0x3FF ) ) ;
93+ lastPos = ++ i ;
94+ out +=
95+ hexTable [ 0xF0 | ( c >> 18 ) ] +
96+ hexTable [ 0x80 | ( ( c >> 12 ) & 0x3F ) ] +
97+ hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] +
98+ hexTable [ 0x80 | ( c & 0x3F ) ] ;
99+ } else {
100+ if ( lastPos < i ) {
101+ out += str . slice ( lastPos , i ) ;
102+ }
103+ // Multi-byte characters ...
104+ lastPos = ++ i ;
105+ out += utf16 [ c ] ;
106+ }
79107 }
80-
81- const c2 = str . charCodeAt ( i ) & 0x3FF ;
82-
83- lastPos = i + 1 ;
84- c = 0x10000 + ( ( ( c & 0x3FF ) << 10 ) | c2 ) ;
85- out +=
86- hexTable [ 0xF0 | ( c >> 18 ) ] +
87- hexTable [ 0x80 | ( ( c >> 12 ) & 0x3F ) ] +
88- hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] +
89- hexTable [ 0x80 | ( c & 0x3F ) ] ;
90108 }
91109 if ( lastPos === 0 ) return str ;
92110 if ( lastPos < len ) return out + str . slice ( lastPos ) ;
0 commit comments