From ddac2a0e122c7629c34f1c92afee1042f66f1f21 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 4 Jun 2024 16:46:56 +0800 Subject: [PATCH] refactor(codegen): reduce allocation for print_unquoted_str (#3525) --- crates/oxc_codegen/src/gen.rs | 6 +++--- crates/oxc_codegen/tests/mod.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 46c0b5b9f9349..9feacb6e0a2d6 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1265,12 +1265,12 @@ impl<'a, const MINIFY: bool> Gen for RegExpLiteral<'a> { } fn print_unquoted_str(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(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"$"); diff --git a/crates/oxc_codegen/tests/mod.rs b/crates/oxc_codegen/tests/mod.rs index a193e8ba6cb16..4f42e22f5e540 100644 --- a/crates/oxc_codegen/tests/mod.rs +++ b/crates/oxc_codegen/tests/mod.rs @@ -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);