@@ -22,15 +22,41 @@ const Buffer = require('buffer').Buffer;
22
22
function ParsedQueryString ( ) { }
23
23
ParsedQueryString . prototype = Object . create ( null ) ;
24
24
25
-
25
+ const unhexTable = [
26
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 0 - 15
27
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 16 - 31
28
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 32 - 47
29
+ + 0 , + 1 , + 2 , + 3 , + 4 , + 5 , + 6 , + 7 , + 8 , + 9 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 48 - 63
30
+ - 1 , 10 , 11 , 12 , 13 , 14 , 15 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 64 - 79
31
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 80 - 95
32
+ - 1 , 10 , 11 , 12 , 13 , 14 , 15 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 96 - 111
33
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 112 - 127
34
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , // 128 ...
35
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
36
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
37
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
38
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
39
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
40
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ,
41
+ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 // ... 255
42
+ ] ;
26
43
// a safe fast alternative to decodeURIComponent
27
44
function unescapeBuffer ( s , decodeSpaces ) {
28
45
var out = Buffer . allocUnsafe ( s . length ) ;
29
46
var state = 0 ;
30
- var n , m , hexchar ;
47
+ var n , m , hexchar , c ;
31
48
32
- for ( var inIndex = 0 , outIndex = 0 ; inIndex <= s . length ; inIndex ++ ) {
33
- var c = inIndex < s . length ? s . charCodeAt ( inIndex ) : NaN ;
49
+ for ( var inIndex = 0 , outIndex = 0 ; ; inIndex ++ ) {
50
+ if ( inIndex < s . length ) {
51
+ c = s . charCodeAt ( inIndex ) ;
52
+ } else {
53
+ if ( state > 0 ) {
54
+ out [ outIndex ++ ] = 37 /*%*/ ;
55
+ if ( state === 2 )
56
+ out [ outIndex ++ ] = hexchar ;
57
+ }
58
+ break ;
59
+ }
34
60
switch ( state ) {
35
61
case 0 : // Any character
36
62
switch ( c ) {
@@ -51,13 +77,8 @@ function unescapeBuffer(s, decodeSpaces) {
51
77
52
78
case 1 : // First hex digit
53
79
hexchar = c ;
54
- if ( c >= 48 /*0*/ && c <= 57 /*9*/ ) {
55
- n = c - 48 /*0*/ ;
56
- } else if ( c >= 65 /*A*/ && c <= 70 /*F*/ ) {
57
- n = c - 65 /*A*/ + 10 ;
58
- } else if ( c >= 97 /*a*/ && c <= 102 /*f*/ ) {
59
- n = c - 97 /*a*/ + 10 ;
60
- } else {
80
+ n = unhexTable [ c ] ;
81
+ if ( ! ( n >= 0 ) ) {
61
82
out [ outIndex ++ ] = 37 /*%*/ ;
62
83
out [ outIndex ++ ] = c ;
63
84
state = 0 ;
@@ -68,13 +89,8 @@ function unescapeBuffer(s, decodeSpaces) {
68
89
69
90
case 2 : // Second hex digit
70
91
state = 0 ;
71
- if ( c >= 48 /*0*/ && c <= 57 /*9*/ ) {
72
- m = c - 48 /*0*/ ;
73
- } else if ( c >= 65 /*A*/ && c <= 70 /*F*/ ) {
74
- m = c - 65 /*A*/ + 10 ;
75
- } else if ( c >= 97 /*a*/ && c <= 102 /*f*/ ) {
76
- m = c - 97 /*a*/ + 10 ;
77
- } else {
92
+ m = unhexTable [ c ] ;
93
+ if ( ! ( m >= 0 ) ) {
78
94
out [ outIndex ++ ] = 37 /*%*/ ;
79
95
out [ outIndex ++ ] = hexchar ;
80
96
out [ outIndex ++ ] = c ;
@@ -87,7 +103,7 @@ function unescapeBuffer(s, decodeSpaces) {
87
103
88
104
// TODO support returning arbitrary buffers.
89
105
90
- return out . slice ( 0 , outIndex - 1 ) ;
106
+ return out . slice ( 0 , outIndex ) ;
91
107
}
92
108
93
109
0 commit comments