refactor(minifier): reduce and clean match checks (#406)

This commit is contained in:
Wenzhe Wang 2023-06-06 12:46:13 +08:00 committed by GitHub
parent 2bebbb5a77
commit 1a31c76ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View file

@ -829,7 +829,7 @@ fn print_non_negative_float(value: f64, p: &mut Printer) -> String {
let dot = chars.iter().position(|&c| c == b'.');
let u8_to_string = |num: &[u8]| unsafe { String::from_utf8_unchecked(num.to_vec()) };
if matches!(dot, Some(1)) && matches!(chars[0], b'0') {
if dot == Some(1) && chars[0] == b'0' {
// Strip off the leading zero when minifying
// "0.5" => ".5"
let stripped_result = &chars[1..];
@ -838,9 +838,9 @@ fn print_non_negative_float(value: f64, p: &mut Printer) -> String {
// Try using an exponent
// "0.001" => "1e-3"
if matches!(stripped_result[after_dot], b'0') {
if stripped_result[after_dot] == b'0' {
let mut i = after_dot + 1;
while matches!(stripped_result[i], b'0') {
while stripped_result[i] == b'0' {
i += 1;
}
let remaining = &stripped_result[i..];
@ -855,11 +855,11 @@ fn print_non_negative_float(value: f64, p: &mut Printer) -> String {
} else {
result = u8_to_string(stripped_result);
}
} else if matches!(chars[len - 1], b'0') {
} else if chars[len - 1] == b'0' {
// Simplify numbers ending with "0" by trying to use an exponent
// "1000" => "1e3"
let mut i = len - 1;
while i > 0 && matches!(chars[i - 1], b'0') {
while i > 0 && chars[i - 1] == b'0' {
i -= 1;
}
let remaining = &chars[0..i];

View file

@ -837,13 +837,21 @@ impl<'a> Lexer<'a> {
}
fn read_decimal_exponent(&mut self, builder: &mut AutoCow<'a>) -> Kind {
let c = self.peek();
if matches!(c, '+' | '-') {
self.current.chars.next();
builder.push_matching(c);
}
let kind = match self.peek() {
'-' => {
self.current.chars.next();
builder.push_matching('-');
Kind::NegativeExponential
}
'+' => {
self.current.chars.next();
builder.push_matching('+');
Kind::PositiveExponential
}
_ => Kind::PositiveExponential,
};
self.read_decimal_digits(builder);
if matches!(c, '-') { Kind::NegativeExponential } else { Kind::PositiveExponential }
kind
}
fn read_decimal_digits(&mut self, builder: &mut AutoCow<'a>) {