1- "use strict" ;
2-
31// This file is taken from Node.js project.
42// Full implementation can be found from https://github.com/nodejs/node/blob/main/lib/internal/querystring.js
53
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" ) ;
4+ const hexTable = Array . from (
5+ { length : 256 } ,
6+ ( _ , i ) => "%" + ( ( i < 16 ? "0" : "" ) + i . toString ( 16 ) ) . toUpperCase ( ) ,
7+ ) ;
268
279// These characters do not need escaping when generating query strings:
2810// ! - . _ ~
@@ -48,63 +30,63 @@ const noEscape = new Int8Array([
4830 */
4931function encodeString ( str ) {
5032 const len = str . length ;
51- if ( len === 0 ) return str ;
33+ if ( len === 0 ) return "" ;
5234
5335 let out = "" ;
5436 let lastPos = 0 ;
5537 let i = 0 ;
56- let c = 0 ;
5738
58- while ( i < len ) {
59- c = str . charCodeAt ( i ) ;
39+ outer: for ( ; i < len ; i ++ ) {
40+ let c = str . charCodeAt ( i ) ;
6041
6142 // ASCII
62- if ( c < 0x80 ) {
63- if ( noEscape [ c ] === 1 ) {
64- ++ i ;
65- } else {
43+ while ( c < 0x80 ) {
44+ if ( noEscape [ c ] !== 1 ) {
6645 if ( lastPos < i ) out += str . slice ( lastPos , i ) ;
67- lastPos = ++ i ;
46+ lastPos = i + 1 ;
6847 out += hexTable [ c ] ;
6948 }
70- // Multi-byte characters ...
71- } else {
72- if ( lastPos < i ) {
73- out += str . slice ( lastPos , i ) ;
74- }
7549
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
50+ if ( ++ i === len ) break outer;
8451
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- }
52+ c = str . charCodeAt ( i ) ;
53+ }
9154
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- }
55+ if ( lastPos < i ) out += str . slice ( lastPos , i ) ;
56+
57+ // Multi-byte characters ...
58+ if ( c < 0x800 ) {
59+ lastPos = i + 1 ;
60+ out += hexTable [ 0xc0 | ( c >> 6 ) ] + hexTable [ 0x80 | ( c & 0x3f ) ] ;
61+ continue ;
10762 }
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" ) ;
79+ }
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 ) ] ;
10890 }
10991 if ( lastPos === 0 ) return str ;
11092 if ( lastPos < len ) return out + str . slice ( lastPos ) ;
0 commit comments