You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.4 KiB
JavaScript
58 lines
2.4 KiB
JavaScript
10 years ago
|
"use strict";
|
||
|
|
||
11 years ago
|
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
|
||
|
|
||
11 years ago
|
/* JavaScript code generation helpers. */
|
||
9 years ago
|
var js = {
|
||
11 years ago
|
stringEscape: function(s) {
|
||
11 years ago
|
/*
|
||
|
* 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.
|
||
|
* Note that "\0" and "\v" escape sequences are not used because JSHint does
|
||
|
* not like the first and IE the second.
|
||
|
*/
|
||
11 years ago
|
return s
|
||
|
.replace(/\\/g, '\\\\') // backslash
|
||
|
.replace(/"/g, '\\"') // closing double quote
|
||
|
.replace(/\x08/g, '\\b') // backspace
|
||
|
.replace(/\t/g, '\\t') // horizontal tab
|
||
|
.replace(/\n/g, '\\n') // line feed
|
||
|
.replace(/\f/g, '\\f') // form feed
|
||
|
.replace(/\r/g, '\\r') // carriage return
|
||
|
.replace(/[\x00-\x07\x0B\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
|
||
|
.replace(/[\x10-\x1F\x80-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
|
||
11 years ago
|
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
|
||
|
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
regexpClassEscape: function(s) {
|
||
11 years ago
|
/*
|
||
|
* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
||
|
*
|
||
|
* For portability, we also escape all control and non-ASCII characters.
|
||
|
*/
|
||
|
return s
|
||
|
.replace(/\\/g, '\\\\') // backslash
|
||
|
.replace(/\//g, '\\/') // closing slash
|
||
|
.replace(/\]/g, '\\]') // closing bracket
|
||
|
.replace(/\^/g, '\\^') // caret
|
||
|
.replace(/-/g, '\\-') // dash
|
||
|
.replace(/\0/g, '\\0') // null
|
||
|
.replace(/\t/g, '\\t') // horizontal tab
|
||
|
.replace(/\n/g, '\\n') // line feed
|
||
|
.replace(/\v/g, '\\x0B') // vertical tab
|
||
|
.replace(/\f/g, '\\f') // form feed
|
||
|
.replace(/\r/g, '\\r') // carriage return
|
||
11 years ago
|
.replace(/[\x00-\x08\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
|
||
|
.replace(/[\x10-\x1F\x80-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
|
||
|
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
|
||
|
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
|
||
11 years ago
|
}
|
||
|
};
|
||
|
|
||
9 years ago
|
module.exports = js;
|