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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions core/string/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
marker::PhantomData,
mem::ManuallyDrop,
ops::{Add, AddAssign},
ptr::{self, addr_of_mut, NonNull},
ptr::{self, NonNull},
str::{self},
};

Expand Down Expand Up @@ -60,7 +60,7 @@ impl<D: Copy> JsStringBuilder<D> {
/// - The elements at `old_len..new_len` must be initialized.
///
#[inline]
pub unsafe fn set_len(&mut self, new_len: usize) {
pub const unsafe fn set_len(&mut self, new_len: usize) {
debug_assert!(new_len <= self.capacity());

self.len = new_len;
Expand Down Expand Up @@ -140,10 +140,10 @@ impl<D: Copy> JsStringBuilder<D> {
///
/// Caller should ensure that the inner is allocated.
#[must_use]
unsafe fn data(&self) -> *mut D {
const unsafe fn data(&self) -> *mut D {
// SAFETY:
// Caller should ensure that the inner is allocated.
unsafe { addr_of_mut!((*self.inner.as_ptr()).data).cast() }
unsafe { (&raw mut (*self.inner.as_ptr()).data).cast() }
}

/// Allocates when there is not sufficient capacity.
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<D: Copy> JsStringBuilder<D> {
///
/// Caller should ensure the capacity is large enough to hold elements.
#[inline]
pub unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) {
pub const unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) {
// SAFETY: Caller should ensure the capacity is large enough to hold elements.
unsafe {
ptr::copy_nonoverlapping(v.as_ptr(), self.data().add(self.len()), v.len());
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<D: Copy> JsStringBuilder<D> {
///
/// Caller should ensure the capacity is large enough to hold elements.
#[inline]
pub unsafe fn push_unchecked(&mut self, v: D) {
pub const unsafe fn push_unchecked(&mut self, v: D) {
// SAFETY: Caller should ensure the capacity is large enough to hold elements.
unsafe {
self.data().add(self.len()).write(v);
Expand Down
35 changes: 15 additions & 20 deletions core/string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::{
iter::Peekable,
mem::ManuallyDrop,
process::abort,
ptr::{self, addr_of, addr_of_mut, NonNull},
ptr::{self, NonNull},
str::FromStr,
};

Expand Down Expand Up @@ -321,6 +321,7 @@ impl JsString {
/// Obtains the underlying [`&[u16]`][slice] slice of a [`JsString`]
#[inline]
#[must_use]
#[allow(clippy::cast_ptr_alignment)]
pub fn as_str(&self) -> JsStr<'_> {
match self.ptr.unwrap() {
UnwrappedTagged::Ptr(h) => {
Expand All @@ -341,26 +342,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(),
))
} else {
JsStr::utf16(std::slice::from_raw_parts(
(*h).ptr.cast(),
(*h).tagged_len.len(),
))
};
}
(*h).ptr
} else {
(&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 Expand Up @@ -400,7 +395,7 @@ impl JsString {

let string = {
// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
let mut data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
let mut data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::<u8>() };
for &string in strings {
// SAFETY:
// The sum of all `count` for each `string` equals `full_count`, and since we're
Expand Down Expand Up @@ -807,7 +802,7 @@ impl JsString {
let ptr = Self::allocate_inner(count, string.is_latin1());

// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
let data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
let data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::<u8>() };

// SAFETY:
// - We read `count = data.len()` elements from `data`, which is within the bounds of the slice.
Expand Down
Loading