Skip to content

Commit 8a5bf85

Browse files
committed
Switch to encoding_rs::mem
Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
1 parent f9993c2 commit 8a5bf85

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

mozjs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ profilemozjs = ['mozjs_sys/profilemozjs']
1717
crown = ['mozjs_sys/crown']
1818

1919
[dependencies]
20+
encoding_rs = "0.8.35"
2021
libc.workspace = true
2122
log = "0.4"
2223
mozjs_sys = { path = "../mozjs-sys" }

mozjs/src/conversions.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use log::debug;
4848
use mozjs_sys::jsgc::Rooted;
4949
use std::borrow::Cow;
5050
use std::mem;
51+
use std::mem::MaybeUninit;
5152
use std::rc::Rc;
5253
use std::{ptr, slice};
5354

@@ -525,12 +526,6 @@ impl FromJSValConvertible for f64 {
525526
}
526527
}
527528

528-
/// A simple wrapper for ascii compatible chars. This and the mem::transmute call
529-
/// in latin1_to_string should be replaced when the `ascii_char` feature is stable.
530-
/// See: <https://github.com/rust-lang/rust/issues/110998>.
531-
#[repr(transparent)]
532-
struct AsciiChar(u8);
533-
534529
/// Converts a `JSString`, encoded in "Latin1" (i.e. U+0000-U+00FF encoded as 0x00-0xFF) into a
535530
/// `String`.
536531
pub unsafe fn latin1_to_string(cx: *mut JSContext, s: *mut JSString) -> String {
@@ -541,16 +536,12 @@ pub unsafe fn latin1_to_string(cx: *mut JSContext, s: *mut JSString) -> String {
541536
assert!(!chars.is_null());
542537

543538
let chars = slice::from_raw_parts(chars, length as usize);
544-
let mut s = String::with_capacity(length as usize);
545-
546-
let v = s.as_mut_vec();
547-
for c in chars {
548-
// This is safe as we are ensured that we only have "Latin1" strings which have the upper
549-
// code word 00.
550-
v.push(*c);
551-
}
539+
let mut v = Vec::with_capacity(length * 2);
540+
v.set_len(length * 2);
552541

553-
s
542+
let real_size = encoding_rs::mem::convert_latin1_to_utf8(chars, v.as_mut_slice());
543+
v.truncate(real_size);
544+
String::from_utf8_unchecked(v)
554545
}
555546

556547
/// Converts a `JSString` into a `String`, regardless of used encoding.

0 commit comments

Comments
 (0)