Browse Source

Implement JsStirng::concat_array() (#1509)

pull/1525/head
Halid Odat 3 years ago committed by GitHub
parent
commit
23dc134f63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      boa/src/string.rs

28
boa/src/string.rs

@ -63,10 +63,13 @@ impl Inner {
unsafe { NonNull::new_unchecked(inner) } unsafe { NonNull::new_unchecked(inner) }
} }
/// Concatinate two string. /// Concatenate array of strings.
#[inline] #[inline]
fn concat(x: &str, y: &str) -> NonNull<Inner> { fn concat_array(strings: &[&str]) -> NonNull<Inner> {
let total_string_size = x.len() + y.len(); let mut total_string_size = 0;
for string in strings {
total_string_size += string.len();
}
// We get the layout of the `Inner` type and we extend by the size // We get the layout of the `Inner` type and we extend by the size
// of the string array. // of the string array.
@ -91,8 +94,11 @@ impl Inner {
debug_assert!(std::ptr::eq(inner.cast::<u8>().add(offset), data)); debug_assert!(std::ptr::eq(inner.cast::<u8>().add(offset), data));
// Copy the two string data into data offset. // Copy the two string data into data offset.
copy_nonoverlapping(x.as_ptr(), data, x.len()); let mut offset = 0;
copy_nonoverlapping(y.as_ptr(), data.add(x.len()), y.len()); for string in strings {
copy_nonoverlapping(string.as_ptr(), data.add(offset), string.len());
offset += string.len();
}
inner inner
}; };
@ -144,7 +150,7 @@ impl JsString {
} }
} }
/// Concatinate two string. /// Concatenate two string.
pub fn concat<T, U>(x: T, y: U) -> JsString pub fn concat<T, U>(x: T, y: U) -> JsString
where where
T: AsRef<str>, T: AsRef<str>,
@ -154,7 +160,15 @@ impl JsString {
let y = y.as_ref(); let y = y.as_ref();
Self { Self {
inner: Inner::concat(x, y), inner: Inner::concat_array(&[x, y]),
_marker: PhantomData,
}
}
/// Concatenate array of string.
pub fn concat_array(strings: &[&str]) -> JsString {
Self {
inner: Inner::concat_array(strings),
_marker: PhantomData, _marker: PhantomData,
} }
} }

Loading…
Cancel
Save