@@ -17,7 +17,7 @@ export function isHex(c: string) {
1717 return ( c >= 'a' && c <= 'f' ) || ( c >= 'A' && c <= 'F' ) || ( c >= '0' && c <= '9' ) ;
1818}
1919
20- export const identSpecialChars : Record < string , boolean > = {
20+ export const identEscapeChars : Record < string , boolean > = {
2121 '!' : true ,
2222 '"' : true ,
2323 '#' : true ,
@@ -49,20 +49,12 @@ export const identSpecialChars: Record<string, boolean> = {
4949 '~' : true
5050} ;
5151
52- export const strReplacementsRev : Record < string , string > = {
53- '\n' : '\\n' ,
54- '\r' : '\\r' ,
55- '\t' : '\\t' ,
56- '\f' : '\\f' ,
57- '\v' : '\\v'
58- } ;
59-
60- export const stringEscapeChars : Record < string , string > = {
61- n : '\n' ,
62- r : '\r' ,
63- t : '\t' ,
64- f : '\f' ,
65- '\\' : '\\'
52+ export const stringRenderEscapeChars : Record < string , boolean > = {
53+ '\n' : true ,
54+ '\r' : true ,
55+ '\t' : true ,
56+ '\f' : true ,
57+ '\v' : true
6658} ;
6759
6860export const whitespaceChars : Record < string , boolean > = {
@@ -91,24 +83,26 @@ export const digitsChars: Record<string, boolean> = {
9183 9 : true
9284} ;
9385
86+ export const maxHexLength = 6 ;
87+
9488export function escapeIdentifier ( s : string ) {
9589 const len = s . length ;
9690 let result = '' ;
9791 let i = 0 ;
9892 while ( i < len ) {
9993 const chr = s . charAt ( i ) ;
100- if ( identSpecialChars [ chr ] ) {
94+ if ( identEscapeChars [ chr ] || ( chr === '-' && i === 1 && s . charAt ( 0 ) === '-' ) ) {
10195 result += '\\' + chr ;
10296 } else {
10397 if (
104- ! (
105- chr === '_' ||
106- chr === '-' ||
107- ( chr >= 'A' && chr <= 'Z' ) ||
108- ( chr >= 'a' && chr <= 'z' ) ||
109- ( i !== 0 && chr >= '0' && chr <= '9' )
110- )
98+ chr === '-' ||
99+ chr === '_' ||
100+ ( chr >= 'A' && chr <= 'Z' ) ||
101+ ( chr >= 'a' && chr <= 'z' ) ||
102+ ( chr >= '0' && chr <= '9' && i !== 0 && ! ( i === 1 && s . charAt ( 0 ) === '-' ) )
111103 ) {
104+ result += chr ;
105+ } else {
112106 let charCode = chr . charCodeAt ( 0 ) ;
113107 if ( ( charCode & 0xf800 ) === 0xd800 ) {
114108 const extraCharCode = s . charCodeAt ( i ++ ) ;
@@ -118,28 +112,25 @@ export function escapeIdentifier(s: string) {
118112 charCode = ( ( charCode & 0x3ff ) << 10 ) + ( extraCharCode & 0x3ff ) + 0x10000 ;
119113 }
120114 result += '\\' + charCode . toString ( 16 ) + ' ' ;
121- } else {
122- result += chr ;
123115 }
124116 }
125117 i ++ ;
126118 }
127119 return result . trim ( ) ;
128120}
129121
130- export function escapeStr ( s : string ) {
122+ export function escapeString ( s : string ) {
131123 const len = s . length ;
132124 let result = '' ;
133125 let i = 0 ;
134- let replacement : string ;
135126 while ( i < len ) {
136127 let chr = s . charAt ( i ) ;
137128 if ( chr === '"' ) {
138129 chr = '\\"' ;
139130 } else if ( chr === '\\' ) {
140131 chr = '\\\\' ;
141- } else if ( ( replacement = strReplacementsRev [ chr ] ) !== undefined ) {
142- chr = replacement ;
132+ } else if ( stringRenderEscapeChars [ chr ] ) {
133+ chr = '\\' + chr . charCodeAt ( 0 ) . toString ( 16 ) + ( i === len - 1 ? '' : ' ' ) ;
143134 }
144135 result += chr ;
145136 i ++ ;
0 commit comments