Skip to content

Commit

Permalink
refactor(codegen): reduce allocation for print_unquoted_str (#3525)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Jun 4, 2024
1 parent 9706c3b commit ddac2a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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"$");
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ddac2a0

Please sign in to comment.