From 23dc134f63da88594fef6dc8946cd0fe06eeb0f4 Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Sat, 28 Aug 2021 15:40:38 +0200 Subject: [PATCH] Implement JsStirng::concat_array() (#1509) --- boa/src/string.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/boa/src/string.rs b/boa/src/string.rs index a84f3581bc8..7a6bf6c2344 100644 --- a/boa/src/string.rs +++ b/boa/src/string.rs @@ -63,10 +63,13 @@ impl Inner { unsafe { NonNull::new_unchecked(inner) } } - /// Concatinate two string. + /// Concatenate array of strings. #[inline] - fn concat(x: &str, y: &str) -> NonNull { - let total_string_size = x.len() + y.len(); + fn concat_array(strings: &[&str]) -> NonNull { + 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 // of the string array. @@ -91,8 +94,11 @@ impl Inner { debug_assert!(std::ptr::eq(inner.cast::().add(offset), data)); // Copy the two string data into data offset. - copy_nonoverlapping(x.as_ptr(), data, x.len()); - copy_nonoverlapping(y.as_ptr(), data.add(x.len()), y.len()); + let mut offset = 0; + for string in strings { + copy_nonoverlapping(string.as_ptr(), data.add(offset), string.len()); + offset += string.len(); + } inner }; @@ -144,7 +150,7 @@ impl JsString { } } - /// Concatinate two string. + /// Concatenate two string. pub fn concat(x: T, y: U) -> JsString where T: AsRef, @@ -154,7 +160,15 @@ impl JsString { let y = y.as_ref(); 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, } }