@@ -1120,7 +1120,7 @@ module ts {
1120
1120
newEndN = Math . max ( newEnd2 , newEnd2 + ( newEnd1 - oldEnd2 ) ) ;
1121
1121
}
1122
1122
1123
- return createTextChangeRange ( createTextSpanFromBounds ( oldStartN , oldEndN ) , /*newLength: */ newEndN - oldStartN ) ;
1123
+ return createTextChangeRange ( createTextSpanFromBounds ( oldStartN , oldEndN ) , /*newLength: */ newEndN - oldStartN ) ;
1124
1124
}
1125
1125
1126
1126
// @internal
@@ -1202,4 +1202,53 @@ module ts {
1202
1202
}
1203
1203
}
1204
1204
}
1205
+
1206
+ var backslashOrDoubleQuote = / [ \" \\ ] / g;
1207
+ var escapedCharsRegExp = / [ \u0000 - \u001f \t \v \f \b \r \n \u2028 \u2029 \u0085 ] / g;
1208
+ var escapedCharsMap : Map < string > = {
1209
+ "\0" : "\\0" ,
1210
+ "\t" : "\\t" ,
1211
+ "\v" : "\\v" ,
1212
+ "\f" : "\\f" ,
1213
+ "\b" : "\\b" ,
1214
+ "\r" : "\\r" ,
1215
+ "\n" : "\\n" ,
1216
+ "\\" : "\\\\" ,
1217
+ "\"" : "\\\"" ,
1218
+ "\u2028" : "\\u2028" , // lineSeparator
1219
+ "\u2029" : "\\u2029" , // paragraphSeparator
1220
+ "\u0085" : "\\u0085" // nextLine
1221
+ } ;
1222
+
1223
+ /**
1224
+ * Based heavily on the abstract 'Quote'/ 'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
1225
+ * but augmented for a few select characters.
1226
+ * Note that this doesn't actually wrap the input in double quotes.
1227
+ */
1228
+ export function escapeString ( s : string ) : string {
1229
+ // Prioritize '"' and '\'
1230
+ s = backslashOrDoubleQuote . test ( s ) ? s . replace ( backslashOrDoubleQuote , getReplacement ) : s ;
1231
+ s = escapedCharsRegExp . test ( s ) ? s . replace ( escapedCharsRegExp , getReplacement ) : s ;
1232
+
1233
+ return s ;
1234
+
1235
+ function getReplacement ( c : string ) {
1236
+ return escapedCharsMap [ c ] || get16BitUnicodeEscapeSequence ( c . charCodeAt ( 0 ) ) ;
1237
+ }
1238
+ }
1239
+
1240
+ function get16BitUnicodeEscapeSequence ( charCode : number ) : string {
1241
+ var hexCharCode = charCode . toString ( 16 ) ;
1242
+ var paddedHexCode = ( "0000" + hexCharCode ) . slice ( - 4 ) ;
1243
+ return "\\u" + paddedHexCode ;
1244
+ }
1245
+
1246
+ var nonAsciiCharacters = / [ ^ \u0000 - \u007F ] / g;
1247
+ export function replaceNonAsciiCharacters ( s : string ) : string {
1248
+ // Replace non-ASCII characters with '\uNNNN' escapes if any exist.
1249
+ // Otherwise just return the original string.
1250
+ return nonAsciiCharacters . test ( s ) ?
1251
+ s . replace ( nonAsciiCharacters , c => get16BitUnicodeEscapeSequence ( c . charCodeAt ( 0 ) ) ) :
1252
+ s ;
1253
+ }
1205
1254
}
0 commit comments