From 5d513d65bb2427644b6f68bcb88feba28466a855 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 18 Mar 2026 23:39:25 +0100 Subject: [PATCH] uucore: add compute_sort_key_utf8 for pre-computing ICU collation keys Expose ICU4X's write_sort_key_utf8_to via a public helper that appends a collation sort key to a caller-supplied buffer. This enables computing sort keys once per line (O(n)) and then comparing cheap byte arrays during sorting, instead of calling compare_utf8 on every comparison. --- src/uucore/src/lib/features/i18n/collator.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/uucore/src/lib/features/i18n/collator.rs b/src/uucore/src/lib/features/i18n/collator.rs index 007e17c90..9a8ee1fc6 100644 --- a/src/uucore/src/lib/features/i18n/collator.rs +++ b/src/uucore/src/lib/features/i18n/collator.rs @@ -74,6 +74,17 @@ pub fn init_locale_collation() -> bool { try_init_collator(opts) } +/// Compute the ICU collation sort key for the given input bytes and append it to `buf`. +/// This allows pre-computing sort keys once per line, then comparing them with simple +/// byte comparison during sorting (much faster than calling `compare_utf8` per comparison). +pub fn compute_sort_key_utf8(input: &[u8], buf: &mut Vec) { + let c = COLLATOR + .get() + .expect("compute_sort_key_utf8 called before collator initialization"); + c.write_sort_key_utf8_to(input, buf) + .expect("ICU write_sort_key_utf8_to failed"); +} + /// Compare both strings with regard to the current locale. pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering { // If the detected locale is 'C', just do byte-wise comparison