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.
This commit is contained in:
Sylvestre Ledru
2026-03-20 15:35:34 +01:00
committed by Daniel Hofstetter
parent 483f13e918
commit 5d513d65bb
@@ -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<u8>) {
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