diff --git a/Cargo.lock b/Cargo.lock index d4b64f902f4c5..a4ead9743ecdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1829,6 +1829,7 @@ dependencies = [ name = "oxc_mangler" version = "0.45.0" dependencies = [ + "compact_str", "itertools", "oxc_allocator", "oxc_ast", diff --git a/crates/oxc_mangler/Cargo.toml b/crates/oxc_mangler/Cargo.toml index 6532b5a20e635..4fde45906e2c6 100644 --- a/crates/oxc_mangler/Cargo.toml +++ b/crates/oxc_mangler/Cargo.toml @@ -27,5 +27,6 @@ oxc_index = { workspace = true } oxc_semantic = { workspace = true } oxc_span = { workspace = true } +compact_str = { workspace = true } itertools = { workspace = true } rustc-hash = { workspace = true } diff --git a/crates/oxc_mangler/src/lib.rs b/crates/oxc_mangler/src/lib.rs index 64744ddf22907..9b4b63dd739e9 100644 --- a/crates/oxc_mangler/src/lib.rs +++ b/crates/oxc_mangler/src/lib.rs @@ -1,3 +1,4 @@ +use compact_str::CompactString; use itertools::Itertools; use rustc_hash::FxHashSet; @@ -334,18 +335,18 @@ fn base54(n: usize) -> CompactStr { // Base 54 at first because these are the usable first characters in JavaScript identifiers // let base = 54usize; - let mut ret = String::new(); - ret.push(BASE54_CHARS[num % base] as char); + // SAFETY: `BASE54_CHARS` is utf8. + let mut s = unsafe { CompactString::from_utf8_unchecked([BASE54_CHARS[num % base]]) }; num /= base; // Base 64 for the rest because after the first character we can also use 0-9 too // let base = 64usize; while num > 0 { num -= 1; - ret.push(BASE54_CHARS[num % base] as char); + s.push(BASE54_CHARS[num % base] as char); num /= base; } - CompactStr::new(&ret) + CompactStr::from(s) } fn debug_name(n: usize) -> CompactStr {