Truly independent web browser
LibJS: Share one UnicodeTextMappingOutput between the unicode modules
Andreas Klingc24d5747d41d
diff --git a/Libraries/LibJS/Rust/src/unicode.rs b/Libraries/LibJS/Rust/src/unicode.rsindex 1fa2347f2a0..b31d08af4f9 100644--- a/Libraries/LibJS/Rust/src/unicode.rs+++ b/Libraries/LibJS/Rust/src/unicode.rs@@ -22,7 +22,7 @@ pub mod relative_time_format;pub mod time_zone;#[repr(C)]-struct UnicodeTextMappingOutput {+pub(crate) struct UnicodeTextMappingOutput {context: *mut c_void,allocate_text: unsafe extern "C" fn(context: *mut c_void, length: usize) -> *mut u16,append_edit: unsafe extern "C" fn(@@ -77,7 +77,7 @@ unsafe extern "C" fn ignore_edit(_: *mut c_void, _: usize, _: usize, _: usize, _/// Calls a LibUnicode export that writes text through a UnicodeTextMappingOutput, and collects the text it writes,/// along with what the export returns.-fn collect_text<R>(write: impl FnOnce(UnicodeTextMappingOutput) -> R) -> (R, Vec<u16>) {+pub(crate) fn collect_text<R>(write: impl FnOnce(UnicodeTextMappingOutput) -> R) -> (R, Vec<u16>) {let mut output: Vec<u16> = Vec::new();let result = write(UnicodeTextMappingOutput {context: (&raw mut output).cast(),@@ -90,15 +90,7 @@ fn collect_text<R>(write: impl FnOnce(UnicodeTextMappingOutput) -> R) -> (R, Vec/// Calls a LibUnicode text mapping with `text` and collects the text it writes.fn map_text(text: Utf16View<'_>, map: impl FnOnce(&[u16], UnicodeTextMappingOutput)) -> Utf16String {let code_units: Vec<u16> = text.code_units().collect();- let mut output: Vec<u16> = Vec::new();- map(- &code_units,- UnicodeTextMappingOutput {- context: (&raw mut output).cast(),- allocate_text,- append_edit: ignore_edit,- },- );+ let ((), output) = collect_text(|output| map(&code_units, output));Utf16String::from_utf16(&output)}diff --git a/Libraries/LibJS/Rust/src/unicode/date_time_format.rs b/Libraries/LibJS/Rust/src/unicode/date_time_format.rsindex c2336e2a18e..b8a6db95c02 100644--- a/Libraries/LibJS/Rust/src/unicode/date_time_format.rs+++ b/Libraries/LibJS/Rust/src/unicode/date_time_format.rs@@ -12,7 +12,8 @@ use core::ptr::NonNull;use ak::Utf16String;-use super::intl::{UnicodeIntlText, UnicodeTextMappingOutput, text_output};+use super::UnicodeTextMappingOutput;+use super::intl::{UnicodeIntlText, text_output};use crate::utf16::Utf16View;/// UNICODE_CALENDAR_PATTERN_FIELD_ABSENT.diff --git a/Libraries/LibJS/Rust/src/unicode/intl.rs b/Libraries/LibJS/Rust/src/unicode/intl.rsindex ebd0421cc6b..dc41b6eb094 100644--- a/Libraries/LibJS/Rust/src/unicode/intl.rs+++ b/Libraries/LibJS/Rust/src/unicode/intl.rs@@ -12,6 +12,7 @@ use core::ptr::NonNull;use ak::Utf16String;+use super::{UnicodeTextMappingOutput, collect_text};use crate::utf16::{Utf16StringBuilder, Utf16View};/// UnicodeIntlText: a borrowed string in either of AK::Utf16View's storages.@@ -40,19 +41,6 @@ impl UnicodeIntlText {}}-#[repr(C)]-pub(crate) struct UnicodeTextMappingOutput {- context: *mut c_void,- allocate_text: unsafe extern "C" fn(context: *mut c_void, length: usize) -> *mut u16,- append_edit: unsafe extern "C" fn(- context: *mut c_void,- source_start: usize,- source_length: usize,- destination_start: usize,- destination_length: usize,- ),-}-type UnicodeAppendString = unsafe extern "C" fn(context: *mut c_void, text: *const u16, length: usize);type UnicodeAppendLocaleIDPart = unsafe extern "C" fn(context: *mut c_void, part: u8, text: *const u16, length: usize);type UnicodeAppendListFormatPart = unsafe extern "C" fn(@@ -217,24 +205,10 @@ unsafe extern "C" {fn unicode_segmenter_destroy(segmenter: *mut c_void);}-unsafe extern "C" fn allocate_text(context: *mut c_void, length: usize) -> *mut u16 {- // SAFETY: The context is the output buffer the caller passed, which outlives the call.- let output = unsafe { &mut *context.cast::<Vec<u16>>() };- output.resize(length, 0);- output.as_mut_ptr()-}--unsafe extern "C" fn ignore_edit(_: *mut c_void, _: usize, _: usize, _: usize, _: usize) {}-/// Calls an export that writes one string through a UnicodeTextMappingOutput, and returns what it returns with the/// string it wrote.fn with_text_output<R>(write: impl FnOnce(UnicodeTextMappingOutput) -> R) -> (R, Utf16String) {- let mut output: Vec<u16> = Vec::new();- let result = write(UnicodeTextMappingOutput {- context: (&raw mut output).cast(),- allocate_text,- append_edit: ignore_edit,- });+ let (result, output) = collect_text(write);(result, Utf16String::from_utf16(&output))}diff --git a/Libraries/LibJS/Rust/src/unicode/relative_time_format.rs b/Libraries/LibJS/Rust/src/unicode/relative_time_format.rsindex 4990af2c843..4caf49bebfc 100644--- a/Libraries/LibJS/Rust/src/unicode/relative_time_format.rs+++ b/Libraries/LibJS/Rust/src/unicode/relative_time_format.rs@@ -12,7 +12,8 @@ use core::ptr::NonNull;use ak::Utf16String;-use super::intl::{Style, UnicodeIntlText, UnicodeTextMappingOutput, text_output};+use super::UnicodeTextMappingOutput;+use super::intl::{Style, UnicodeIntlText, text_output};use crate::utf16::Utf16View;type UnicodeAppendRelativeTimeFormatPart = unsafe extern "C" fn(