refactor(codegen): reduce allocation for print_unquoted_str (#3525)

This commit is contained in:
Dunqing 2024-06-04 16:46:56 +08:00 committed by GitHub
parent 9706c3b533
commit ddac2a0e12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 7 deletions

View file

@ -1265,12 +1265,12 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for RegExpLiteral<'a> {
}
fn print_unquoted_str<const MINIFY: bool>(s: &str, quote: char, p: &mut Codegen<{ MINIFY }>) {
let mut chars = s.chars();
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
match c {
'\x00' => {
if chars.clone().next().is_some_and(|next| next.is_ascii_digit()) {
if chars.peek().is_some_and(|&next| next.is_ascii_digit()) {
p.print_str(b"\\x00");
} else {
p.print_str(b"\\0");
@ -1325,7 +1325,7 @@ fn print_unquoted_str<const MINIFY: bool>(s: &str, quote: char, p: &mut Codegen<
}
}
'$' => {
if chars.clone().next().is_some_and(|next| next == '{') {
if chars.peek().is_some_and(|&next| next == '{') {
p.print_str(b"\\$");
} else {
p.print_str(b"$");

View file

@ -61,12 +61,12 @@ fn string() {
test("let x = '\x071'", "let x = '\\x071';\n", None);
test("let x = '\\7'", "let x = '\\x07';\n", None);
test("let x = '\\7!'", "let x = '\\x07!';\n", None);
// test( "let x = '\\01'", "let x = '\x01';\n", None);
// test( "let x = '\x10'", "let x = '\x10';\n", None);
// test( "let x = '\\x10'", "let x = '\x10';\n", None);
test("let x = '\\01'", "let x = '\x01';\n", None);
test("let x = '\x10'", "let x = '\x10';\n", None);
test("let x = '\\x10'", "let x = '\x10';\n", None);
test("let x = '\x1B'", "let x = '\\x1B';\n", None);
test("let x = '\\x1B'", "let x = '\\x1B';\n", None);
// test( r#"let x = '\uABCD'"#, r#"let x = "\uABCD";"#, None);
test("let x = '\\uABCD'", "let x = 'ꯍ';\n", None);
// test( "let x = '\\uABCD'", r#"let x = '\uABCD';\n"#, None);
// test( r#"let x = '\U000123AB'"#, r#"let x = '\U000123AB';\n"#, None);
// test( "let x = '\\u{123AB}'", r#"let x = '\U000123AB';\n"#, None);