improve numeral compression (#3108)
This commit is contained in:
@@ -1459,23 +1459,27 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function make_num(num) {
|
function make_num(num) {
|
||||||
var str = num.toString(10), a = [ str.replace(/^0\./, ".").replace('e+', 'e') ], m;
|
var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
|
||||||
|
var candidates = [ str ];
|
||||||
if (Math.floor(num) === num) {
|
if (Math.floor(num) === num) {
|
||||||
if (num >= 0) {
|
if (num < 0) {
|
||||||
a.push("0x" + num.toString(16).toLowerCase(), // probably pointless
|
candidates.push("-0x" + (-num).toString(16).toLowerCase());
|
||||||
"0" + num.toString(8)); // same.
|
|
||||||
} else {
|
} else {
|
||||||
a.push("-0x" + (-num).toString(16).toLowerCase(), // probably pointless
|
candidates.push("0x" + num.toString(16).toLowerCase());
|
||||||
"-0" + (-num).toString(8)); // same.
|
|
||||||
}
|
}
|
||||||
if ((m = /^(.*?)(0+)$/.exec(num))) {
|
|
||||||
a.push(m[1] + "e" + m[2].length);
|
|
||||||
}
|
}
|
||||||
} else if ((m = /^0?\.(0+)(.*)$/.exec(num))) {
|
var match, len, digits;
|
||||||
a.push(m[2] + "e-" + (m[1].length + m[2].length),
|
if (match = /^\.0+/.exec(str)) {
|
||||||
str.substr(str.indexOf(".")));
|
len = match[0].length;
|
||||||
|
digits = str.slice(len);
|
||||||
|
candidates.push(digits + "e-" + (digits.length + len - 1));
|
||||||
|
} else if (match = /0+$/.exec(str)) {
|
||||||
|
len = match[0].length;
|
||||||
|
candidates.push(str.slice(0, -len) + "e" + len);
|
||||||
|
} else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
|
||||||
|
candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
|
||||||
}
|
}
|
||||||
return best_of(a);
|
return best_of(candidates);
|
||||||
}
|
}
|
||||||
|
|
||||||
function make_block(stmt, output) {
|
function make_block(stmt, output) {
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
hex_numbers_in_parentheses_for_prototype_functions: {
|
hex_numbers_in_parentheses_for_prototype_functions: {
|
||||||
|
beautify = {
|
||||||
|
beautify: true,
|
||||||
|
}
|
||||||
input: {
|
input: {
|
||||||
|
function f() {
|
||||||
(-2);
|
(-2);
|
||||||
(-2).toFixed(0);
|
(-2).toFixed(0);
|
||||||
|
|
||||||
@@ -9,13 +13,37 @@ hex_numbers_in_parentheses_for_prototype_functions: {
|
|||||||
(0.2);
|
(0.2);
|
||||||
(0.2).toFixed(0);
|
(0.2).toFixed(0);
|
||||||
|
|
||||||
|
(2.34e20);
|
||||||
|
(2.34e20).toFixed(0);
|
||||||
|
|
||||||
(0.00000002);
|
(0.00000002);
|
||||||
(0.00000002).toFixed(0);
|
(0.00000002).toFixed(0);
|
||||||
|
|
||||||
(1000000000000000128);
|
(1000000000000000128);
|
||||||
(1000000000000000128).toFixed(0);
|
(1000000000000000128).toFixed(0);
|
||||||
|
|
||||||
|
(-1000000000000000128);
|
||||||
|
(-1000000000000000128).toFixed(0);
|
||||||
}
|
}
|
||||||
expect_exact: "-2;(-2).toFixed(0);2;2..toFixed(0);.2;.2.toFixed(0);2e-8;2e-8.toFixed(0);0xde0b6b3a7640080;(0xde0b6b3a7640080).toFixed(0);"
|
}
|
||||||
|
expect_exact: [
|
||||||
|
"function f() {",
|
||||||
|
" -2;",
|
||||||
|
" (-2).toFixed(0);",
|
||||||
|
" 2;",
|
||||||
|
" 2..toFixed(0);",
|
||||||
|
" .2;",
|
||||||
|
" .2.toFixed(0);",
|
||||||
|
" 234e18;",
|
||||||
|
" 234e18.toFixed(0);",
|
||||||
|
" 2e-8;",
|
||||||
|
" 2e-8.toFixed(0);",
|
||||||
|
" 0xde0b6b3a7640080;",
|
||||||
|
" (0xde0b6b3a7640080).toFixed(0);",
|
||||||
|
" -0xde0b6b3a7640080;",
|
||||||
|
" (-0xde0b6b3a7640080).toFixed(0);",
|
||||||
|
"}",
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
comparisons: {
|
comparisons: {
|
||||||
|
|||||||
Reference in New Issue
Block a user