Skip to content

Commit 4b5ce32

Browse files
committed
chore: simplify JsString::as_str
1 parent 517ad50 commit 4b5ce32

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

core/string/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,26 +341,20 @@ impl JsString {
341341
// which means it is safe to read the `refcount` as `read_only` here.
342342
unsafe {
343343
let h = h.as_ptr();
344-
if (*h).refcount.read_only == 0 {
344+
let tagged_len = (*h).tagged_len;
345+
let len = tagged_len.len();
346+
let is_latin1 = tagged_len.is_latin1();
347+
let ptr = if (*h).refcount.read_only == 0 {
345348
let h = h.cast::<StaticJsString>();
346-
return if (*h).tagged_len.is_latin1() {
347-
JsStr::latin1(std::slice::from_raw_parts(
348-
(*h).ptr,
349-
(*h).tagged_len.len(),
350-
))
349+
(*h).ptr
351350
} else {
352-
JsStr::utf16(std::slice::from_raw_parts(
353-
(*h).ptr.cast(),
354-
(*h).tagged_len.len(),
355-
))
351+
(&raw const (*h).data).cast::<u8>()
356352
};
357-
}
358353

359-
let len = (*h).len();
360-
if (*h).is_latin1() {
361-
JsStr::latin1(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
354+
if is_latin1 {
355+
JsStr::latin1(std::slice::from_raw_parts(ptr, len))
362356
} else {
363-
JsStr::utf16(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
357+
JsStr::utf16(std::slice::from_raw_parts(ptr.cast::<u16>(), len))
364358
}
365359
}
366360
}

0 commit comments

Comments
 (0)