From d73a5208a0d5ab8dc263d229fc917def7f85c8b2 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 11 Jun 2016 13:29:50 +0200 Subject: [PATCH] Simplify various escaping functions Simplify regexps that specify ranges of characters to escape with "\xXX" and "\uXXXX" in various escaping functions. Until now, these regexps were (mostly) mutually exclusive with more selective regexps applied before them, but this became a maintenance headache. I decided to abandon the exclusivity, which allowed to simplify these regexps (at the cost of introducing an ordering dependency). --- lib/compiler/js.js | 10 +++++----- lib/compiler/passes/generate-bytecode.js | 8 ++++---- lib/compiler/passes/generate-js.js | 4 ++-- lib/parser.js | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/compiler/js.js b/lib/compiler/js.js index 1cdfb50..3da25a4 100644 --- a/lib/compiler/js.js +++ b/lib/compiler/js.js @@ -24,10 +24,10 @@ var js = { .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\x7F-\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); }); + .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\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); }); }, regexpClassEscape: function(s) { @@ -48,7 +48,7 @@ var js = { .replace(/\v/g, '\\x0B') // vertical tab .replace(/\f/g, '\\f') // form feed .replace(/\r/g, '\\r') // carriage return - .replace(/[\x00-\x08\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) + .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) .replace(/[\x10-\x1F\x7F-\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); }); diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 42bfbf6..8aab0ae 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -292,8 +292,8 @@ function generateBytecode(ast) { .replace(/\t/g, '\\t') // horizontal tab .replace(/\n/g, '\\n') // line feed .replace(/\r/g, '\\r') // carriage return - .replace(/[\x01-\x08\x0B\x0C\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); + .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } function classDescriptionEscape(s) { @@ -306,8 +306,8 @@ function generateBytecode(ast) { .replace(/\t/g, '\\t') // horizontal tab .replace(/\n/g, '\\n') // line feed .replace(/\r/g, '\\r') // carriage return - .replace(/[\x01-\x08\x0B\x0C\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); + .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } var generate = visitor.build({ diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index aed516b..7c3ba23 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1085,8 +1085,8 @@ function generateJS(ast, options) { ' .replace(/\\t/g, \'\\\\t\')', // horizontal tab ' .replace(/\\n/g, \'\\\\n\')', // line feed ' .replace(/\\r/g, \'\\\\r\')', // carriage return - ' .replace(/[\\x01-\\x08\\x0B\\x0C\\x0E\\x0F]/g, function(ch) { return \'\\\\x0\' + hex(ch); })', - ' .replace(/[\\x10-\\x1F\\x7F-\\x9F]/g, function(ch) { return \'\\\\x\' + hex(ch); });', + ' .replace(/[\\x00-\\x0F]/g, function(ch) { return \'\\\\x0\' + hex(ch); })', + ' .replace(/[\\x10-\\x1F\\x7F-\\x9F]/g, function(ch) { return \'\\\\x\' + hex(ch); });', ' }', '', ' var expectedDescs = new Array(expected.length),', diff --git a/lib/parser.js b/lib/parser.js index 43599b0..e5947cc 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -542,8 +542,8 @@ .replace(/\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') - .replace(/[\x01-\x08\x0B\x0C\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); + .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } var expectedDescs = new Array(expected.length),