1use std::cmp::Ordering;
11
12pub fn utf8(a: &str, b: &str) -> Ordering {
14 a.as_bytes().cmp(b.as_bytes())
15}
16
17pub fn utf16(a: &str, b: &str) -> Ordering {
19 let common = a.bytes().zip(b.bytes()).take_while(|(x, y)| x == y).count();
22 let start = (0..=common)
23 .rev()
24 .find(|&i| a.is_char_boundary(i))
25 .unwrap_or(0);
26 a[start..].encode_utf16().cmp(b[start..].encode_utf16())
27}
28
29#[cfg(test)]
30mod tests {
31 use std::cmp::Ordering::*;
32
33 #[test]
34 fn utf16_puts_supplementary_planes_before_the_end_of_the_bmp() {
35 assert_eq!(super::utf16("\u{ffff}", "\u{10000}"), Greater);
36 assert_eq!(super::utf8("\u{ffff}", "\u{10000}"), Less);
37 assert_eq!(super::utf16("ab\u{e000}", "ab\u{1f600}x"), Greater);
38 assert_eq!(super::utf16("ab", "abc"), Less);
39 }
40}