feat(codegen): minify numbers with large exponents (#8074)

This commit is contained in:
Boshen 2024-12-23 13:35:52 +00:00
parent 373279b7f4
commit d84d60a3a9
2 changed files with 12 additions and 5 deletions

View file

@ -600,22 +600,27 @@ impl<'a> Codegen<'a> {
candidates.push(format!("0x{:x}", num as u128));
}
// create `1e-2`
if s.starts_with(".0") {
// create `1e-2`
if let Some((i, _)) = s[1..].bytes().enumerate().find(|(_, c)| *c != b'0') {
let len = i + 1; // `+1` to include the dot.
let digits = &s[len..];
candidates.push(format!("{digits}e-{}", digits.len() + len - 1));
}
} else if s.ends_with('0') {
// create 1e2
}
// create 1e2
if s.ends_with('0') {
if let Some((len, _)) = s.bytes().rev().enumerate().find(|(_, c)| *c != b'0') {
candidates.push(format!("{}e{len}", &s[0..s.len() - len]));
}
} else if let Some((integer, point, exponent)) =
}
// `1.2e101` -> ("1", "2", "101")
// `1.3415205933077406e300` -> `13415205933077406e284;`
if let Some((integer, point, exponent)) =
s.split_once('.').and_then(|(a, b)| b.split_once('e').map(|e| (a, e.0, e.1)))
{
// `1.2e101` -> ("1", "2", "101")
candidates.push(format!(
"{integer}{point}e{}",
exponent.parse::<isize>().unwrap() - point.len() as isize

View file

@ -298,6 +298,8 @@ fn test_call() {
fn test_member() {
test("x.y[z]", "x.y[z];\n");
test("((x+1).y+1)[z]", "((x + 1).y + 1)[z];\n");
test_minify("1.3415205933077406e300", "13415205933077406e284;");
}
#[test]