2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2014-05-08 15:22:43 +02:00
|
|
|
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
|
|
|
|
|
2016-09-17 16:28:28 +02:00
|
|
|
// JavaScript code generation helpers.
|
2016-09-08 16:04:36 +02:00
|
|
|
let js = {
|
2016-10-07 17:03:10 +02:00
|
|
|
stringEscape(s) {
|
2016-09-17 16:28:28 +02:00
|
|
|
// ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
|
|
|
// literal except for the closing quote character, backslash, carriage
|
|
|
|
// return, line separator, paragraph separator, and line feed. Any character
|
|
|
|
// may appear in the form of an escape sequence.
|
|
|
|
//
|
|
|
|
// For portability, we also escape all control and non-ASCII characters.
|
2014-05-08 15:22:43 +02:00
|
|
|
return s
|
2016-09-21 15:06:56 +02:00
|
|
|
.replace(/\\/g, "\\\\") // backslash
|
|
|
|
.replace(/"/g, "\\\"") // closing double quote
|
|
|
|
.replace(/\0/g, "\\0") // null
|
|
|
|
.replace(/\x08/g, "\\b") // backspace
|
|
|
|
.replace(/\t/g, "\\t") // horizontal tab
|
|
|
|
.replace(/\n/g, "\\n") // line feed
|
|
|
|
.replace(/\v/g, "\\v") // vertical tab
|
|
|
|
.replace(/\f/g, "\\f") // form feed
|
|
|
|
.replace(/\r/g, "\\r") // carriage return
|
|
|
|
.replace(/[\x00-\x0F]/g, ch => "\\x0" + hex(ch))
|
|
|
|
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex(ch))
|
|
|
|
.replace(/[\u0100-\u0FFF]/g, ch => "\\u0" + hex(ch))
|
|
|
|
.replace(/[\u1000-\uFFFF]/g, ch => "\\u" + hex(ch));
|
2014-05-08 11:48:56 +02:00
|
|
|
},
|
|
|
|
|
2016-10-07 17:03:10 +02:00
|
|
|
regexpClassEscape(s) {
|
2016-09-17 16:28:28 +02:00
|
|
|
// Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
|
|
|
//
|
|
|
|
// For portability, we also escape all control and non-ASCII characters.
|
2014-05-08 11:48:56 +02:00
|
|
|
return s
|
2016-09-21 15:06:56 +02:00
|
|
|
.replace(/\\/g, "\\\\") // backslash
|
|
|
|
.replace(/\//g, "\\/") // closing slash
|
2016-12-01 16:30:10 +01:00
|
|
|
.replace(/]/g, "\\]") // closing bracket
|
2016-09-21 15:06:56 +02:00
|
|
|
.replace(/\^/g, "\\^") // caret
|
|
|
|
.replace(/-/g, "\\-") // dash
|
|
|
|
.replace(/\0/g, "\\0") // null
|
|
|
|
.replace(/\x08/g, "\\b") // backspace
|
|
|
|
.replace(/\t/g, "\\t") // horizontal tab
|
|
|
|
.replace(/\n/g, "\\n") // line feed
|
|
|
|
.replace(/\v/g, "\\v") // vertical tab
|
|
|
|
.replace(/\f/g, "\\f") // form feed
|
|
|
|
.replace(/\r/g, "\\r") // carriage return
|
|
|
|
.replace(/[\x00-\x0F]/g, ch => "\\x0" + hex(ch))
|
|
|
|
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex(ch))
|
|
|
|
.replace(/[\u0100-\u0FFF]/g, ch => "\\u0" + hex(ch))
|
|
|
|
.replace(/[\u1000-\uFFFF]/g, ch => "\\u" + hex(ch));
|
2014-05-08 11:48:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-04 15:38:17 +02:00
|
|
|
module.exports = js;
|