Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some string cleanups #4090

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: simplify JsString::as_str
  • Loading branch information
CrazyboyQCD committed Dec 18, 2024
commit 4b5ce32613ea28004615b0d86ee9e5e9e8611d78
24 changes: 9 additions & 15 deletions core/string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,20 @@ impl JsString {
// which means it is safe to read the `refcount` as `read_only` here.
unsafe {
let h = h.as_ptr();
if (*h).refcount.read_only == 0 {
let tagged_len = (*h).tagged_len;
let len = tagged_len.len();
let is_latin1 = tagged_len.is_latin1();
let ptr = if (*h).refcount.read_only == 0 {
let h = h.cast::<StaticJsString>();
return if (*h).tagged_len.is_latin1() {
JsStr::latin1(std::slice::from_raw_parts(
(*h).ptr,
(*h).tagged_len.len(),
))
(*h).ptr
} else {
JsStr::utf16(std::slice::from_raw_parts(
(*h).ptr.cast(),
(*h).tagged_len.len(),
))
(&raw const (*h).data).cast::<u8>()
};
}

let len = (*h).len();
if (*h).is_latin1() {
JsStr::latin1(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
if is_latin1 {
JsStr::latin1(std::slice::from_raw_parts(ptr, len))
} else {
JsStr::utf16(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
JsStr::utf16(std::slice::from_raw_parts(ptr.cast::<u16>(), len))
}
}
}
Expand Down