use std::cmp::Ordering;
// Bun and Windows both supply ICU; NLS orders punctuation differently.
pub fn compare_english(a: &str, b: &str, base_sensitivity: bool) -> Ordering {
#[cfg(windows)]
if let Some(order) = windows_icu::compare(a, b, base_sensitivity) {
return order;
}
if base_sensitivity {
a.to_lowercase().cmp(&b.to_lowercase())
} else {
a.cmp(b)
}
}
#[cfg(windows)]
mod windows_icu {
use std::{
cell::RefCell,
cmp::Ordering,
ffi::{c_char, c_void},
};
#[link(name = "icu")]
unsafe extern "C" {
fn ucol_open(locale: *const c_char, status: *mut i32) -> *mut c_void;
fn ucol_setStrength(collator: *mut c_void, strength: i32);
fn ucol_strcollUTF8(
collator: *const c_void,
a: *const c_char,
a_len: i32,
b: *const c_char,
b_len: i32,
status: *mut i32,
) -> i32;
fn ucol_close(collator: *mut c_void);
}
#[link(name = "ole32")]
unsafe extern "system" {
fn CoInitializeEx(reserved: *mut c_void, flags: u32) -> i32;
fn CoUninitialize();
#[cfg(test)]
fn OleInitialize(reserved: *mut c_void) -> i32;
#[cfg(test)]
fn OleUninitialize();
}
// Apartment-threaded: winit later calls OleInitialize on the UI thread and
// panics with RPC_E_CHANGED_MODE if this thread already joined the MTA.
const COINIT_APARTMENTTHREADED: u32 = 0x2;
struct Collators {
com: i32,
handles: [*mut c_void; 2],
}
impl Collators {
fn new() -> Self {
// SAFETY: null reserved pointer; balance successful COM init at thread teardown.
Self {
com: unsafe { CoInitializeEx(std::ptr::null_mut(), COINIT_APARTMENTTHREADED) },
handles: [std::ptr::null_mut(); 2],
}
}
fn compare(&mut self, a: &str, b: &str, base: bool) -> Option<Ordering> {
let (a_len, b_len) = (i32::try_from(a.len()).ok()?, i32::try_from(b.len()).ok()?);
if self.com < 0 && self.com != -2147417850 {
return None;
}
let slot = usize::from(!base);
let mut status = 0;
// SAFETY: each collator belongs to this thread and strength; it never leaves TLS.
// ICU receives valid UTF-8 buffers with checked lengths and a terminated locale.
unsafe {
if self.handles[slot].is_null() {
let handle = ucol_open(c"en".as_ptr(), &mut status);
if status > 0 || handle.is_null() {
if !handle.is_null() {
ucol_close(handle);
}
return None;
}
ucol_setStrength(handle, if base { 0 } else { 2 });
self.handles[slot] = handle;
}
let result = ucol_strcollUTF8(
self.handles[slot],
a.as_ptr().cast(),
a_len,
b.as_ptr().cast(),
b_len,
&mut status,
);
(status <= 0).then(|| result.cmp(&0))
}
}
}
impl Drop for Collators {
fn drop(&mut self) {
// SAFETY: close this thread's handles before balancing its successful COM init.
unsafe {
for handle in self.handles {
if !handle.is_null() {
ucol_close(handle);
}
}
if self.com >= 0 {
CoUninitialize();
}
}
}
}
#[cfg(test)]
pub unsafe fn ole_initialize() -> i32 {
unsafe { OleInitialize(std::ptr::null_mut()) }
}
#[cfg(test)]
pub unsafe fn ole_uninitialize() {
unsafe { OleUninitialize() }
}
thread_local! { static CACHE: RefCell<Collators> = RefCell::new(Collators::new()); }
pub fn compare(a: &str, b: &str, base: bool) -> Option<Ordering> {
CACHE.with(|cache| cache.borrow_mut().compare(a, b, base))
}
// Preserve the original per-comparison lifetime as an equivalence/benchmark oracle.
#[cfg(test)]
pub fn uncached(a: &str, b: &str, base: bool) -> Option<Ordering> {
let (a_len, b_len) = (i32::try_from(a.len()).ok()?, i32::try_from(b.len()).ok()?);
// SAFETY: same checked buffers and COM/ICU lifetime as the original implementation.
unsafe {
let com = CoInitializeEx(std::ptr::null_mut(), 0);
if com < 0 && com != -2147417850 {
return None;
}
let mut status = 0;
let collator = ucol_open(c"en".as_ptr(), &mut status);
let order = if !collator.is_null() && status <= 0 {
ucol_setStrength(collator, if base { 0 } else { 2 });
let result = ucol_strcollUTF8(
collator,
a.as_ptr().cast(),
a_len,
b.as_ptr().cast(),
b_len,
&mut status,
);
(status <= 0).then(|| result.cmp(&0))
} else {
None
};
if !collator.is_null() {
ucol_close(collator);
}
if com >= 0 {
CoUninitialize();
}
order
}
}
}
#[cfg(all(test, windows))]
mod tests {
use super::*;
fn titles(n: usize) -> Vec<String> {
let parts = [
"!skins",
"[Beta]",
"Rail-planner",
"rail Planner",
"Énergie",
"energy",
"物流",
"A.B",
"a_b",
"O'Reilly",
"🚂 Trains",
"Quality+",
"FOO",
"foo",
];
(0..n)
.map(|i| format!("{} {}", parts[(i * 97) % parts.len()], (i * 7919) % n))
.collect()
}
#[test]
fn cached_collation_matches_original_for_both_strengths_and_threads() {
let check = || {
for base in [true, false] {
let mut cached = titles(600);
let mut original = cached.clone();
cached.sort_by(|a, b| compare_english(a, b, base));
original.sort_by(|a, b| windows_icu::uncached(a, b, base).unwrap());
assert_eq!(cached, original);
}
};
check();
std::thread::spawn(check).join().unwrap();
}
#[test]
#[ignore = "release timing probe"]
fn time_collation_24k() {
for base in [true, false] {
// Separate threads keep the cached path's COM apartment from warming the oracle.
let (original, before) = std::thread::spawn(move || {
let mut original = titles(24000);
let start = std::time::Instant::now();
original.sort_by(|a, b| windows_icu::uncached(a, b, base).unwrap());
(original, start.elapsed())
})
.join()
.unwrap();
let (cached, after) = std::thread::spawn(move || {
let mut cached = titles(24000);
let start = std::time::Instant::now();
cached.sort_by(|a, b| compare_english(a, b, base));
(cached, start.elapsed())
})
.join()
.unwrap();
eprintln!("24k collation base={base}: uncached={before:?}, cached={after:?}",);
assert_eq!(cached, original);
}
}
}
#[cfg(all(test, windows))]
mod ole_tests {
#[test]
fn collation_leaves_the_thread_usable_for_ole() {
std::thread::spawn(|| {
super::compare_english("b", "a", true);
// SAFETY: null reserved pointer; balanced below on success.
let hr = unsafe { super::windows_icu::ole_initialize() };
assert!(hr >= 0, "OleInitialize after collation returned {hr:#x}");
unsafe { super::windows_icu::ole_uninitialize() };
})
.join()
.unwrap();
}
}