Skip to content

Truly independent web browser

AK: Expose the UTF-8 encoding of well-formed UTF-16 to Rust

Aliaksandr Kalenik825b950bd1d3
diff --git a/AK/FFIHelpers.cpp b/AK/FFIHelpers.cpp
index 858572c7e5b..075033f304b 100644
--- a/AK/FFIHelpers.cpp
+++ b/AK/FFIHelpers.cpp
@@ -88,6 +88,11 @@ extern "C" bool ladybird_utf16_validate_prefix(u16 const* data, size_t length, s
return AK::Utf16View { reinterpret_cast<char16_t const*>(data), length }.validate(*valid_code_units);
}
+extern "C" size_t ladybird_utf8_length_from_valid_utf16(u16 const* data, size_t length)
+{
+ return simdutf::utf8_length_from_utf16(reinterpret_cast<char16_t const*>(data), length);
+}
+
extern "C" size_t ladybird_convert_valid_utf16_to_utf8(u16 const* data, size_t length, u8* output)
{
return simdutf::convert_valid_utf16_to_utf8(reinterpret_cast<char16_t const*>(data), length, reinterpret_cast<char*>(output));
diff --git a/AK/FFIHelpers.h b/AK/FFIHelpers.h
index 964648b5952..c0a71d409f5 100644
--- a/AK/FFIHelpers.h
+++ b/AK/FFIHelpers.h
@@ -38,6 +38,7 @@ FlatPtr ladybird_utf16_fly_string_from_utf16_string(FlatPtr);
void ladybird_utf16_string_unref(FlatPtr);
bool ladybird_utf16_validate(u16 const*, size_t);
bool ladybird_utf16_validate_prefix(u16 const*, size_t, size_t* valid_code_units);
+size_t ladybird_utf8_length_from_valid_utf16(u16 const*, size_t);
size_t ladybird_convert_valid_utf16_to_utf8(u16 const*, size_t, u8* output);
FlatPtr ladybird_utf16_string_from_utf8(u8 const*, size_t);
FlatPtr ladybird_utf16_string_to_well_formed(u16 const*, size_t);
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index 15e8ae3b44c..a8c756a9819 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -10,6 +10,7 @@ mod scope_guard;
pub use scope_guard::ScopeGuard;
+use core::mem::MaybeUninit;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU32, Ordering};
@@ -53,6 +54,7 @@ unsafe extern "C" {
fn ladybird_utf16_string_unref(raw: usize);
fn ladybird_utf16_validate(data: *const u16, length: usize) -> bool;
fn ladybird_utf16_validate_prefix(data: *const u16, length: usize, valid_code_units: *mut usize) -> bool;
+ fn ladybird_utf8_length_from_valid_utf16(data: *const u16, length: usize) -> usize;
fn ladybird_convert_valid_utf16_to_utf8(data: *const u16, length: usize, output: *mut u8) -> usize;
fn ladybird_utf16_string_from_utf8(data: *const u8, length: usize) -> usize;
fn ladybird_utf16_string_to_well_formed(data: *const u16, length: usize) -> usize;
@@ -207,9 +209,15 @@ macro_rules! impl_utf16_string_owner {
/// Returns whether the string has no code units.
#[inline]
pub fn is_empty(&self) -> bool {
+ self.length_in_code_units() == 0
+ }
+
+ /// Returns the number of UTF-16 code units, which is the number of bytes of ASCII storage.
+ #[inline]
+ pub fn length_in_code_units(&self) -> usize {
match self.as_units() {
- Utf16StringUnits::Ascii(units) => units.is_empty(),
- Utf16StringUnits::Utf16(units) => units.is_empty(),
+ Utf16StringUnits::Ascii(units) => units.len(),
+ Utf16StringUnits::Utf16(units) => units.len(),
}
}
}
@@ -405,6 +413,26 @@ pub fn validate_utf16(string: &[u16]) -> bool {
unsafe { ladybird_utf16_validate(string.as_ptr(), string.len()) }
}
+/// The number of bytes of the UTF-8 encoding of a well-formed UTF-16 string.
+pub fn utf8_length_from_valid_utf16(string: &[u16]) -> usize {
+ // SAFETY: The slice remains alive for the duration of the call.
+ unsafe { ladybird_utf8_length_from_valid_utf16(string.as_ptr(), string.len()) }
+}
+
+/// Writes the UTF-8 encoding of a well-formed UTF-16 string to the start of `output`, which need not be initialized,
+/// and returns the number of bytes it wrote, which are initialized afterwards.
+///
+/// # Safety
+///
+/// `string` must be well-formed UTF-16, and `output` must have room for its UTF-8 encoding, which is
+/// utf8_length_from_valid_utf16(string) bytes.
+pub unsafe fn convert_valid_utf16_to_utf8(string: &[u16], output: &mut [MaybeUninit<u8>]) -> usize {
+ debug_assert!(validate_utf16(string));
+ debug_assert!(output.len() >= utf8_length_from_valid_utf16(string));
+ // SAFETY: The caller guarantees that the string is well-formed and that the output has room for its encoding.
+ unsafe { ladybird_convert_valid_utf16_to_utf8(string.as_ptr(), string.len(), output.as_mut_ptr().cast::<u8>()) }
+}
+
pub fn append_utf16_as_utf8_up_to_unpaired_surrogate(output: &mut Vec<u8>, string: &[u16]) -> usize {
let mut valid_code_units = 0;
// SAFETY: The slice remains alive for the duration of the call.