Skip to main content

zero_client/
strings.rs

1//! The two string orders Zero uses.
2//!
3//! - UTF-8 byte order (code point order) is what `compareUTF8` and SQLite
4//!   use. Row values, primary keys, and AST normalization sort this way. It
5//!   is Rust's own `str` order.
6//! - UTF-16 code unit order is what JS `<` and `Array#sort` use. Object keys
7//!   are sorted this way before hashing. It differs from code point order
8//!   only between U+E000..=U+FFFF and the supplementary planes.
9
10use std::cmp::Ordering;
11
12/// `compareUTF8(a, b)`.
13pub fn utf8(a: &str, b: &str) -> Ordering {
14    a.as_bytes().cmp(b.as_bytes())
15}
16
17/// JS `a < b ? -1 : a > b ? 1 : 0` for strings.
18pub fn utf16(a: &str, b: &str) -> Ordering {
19    // The strings agree up to their first differing byte, which starts or is
20    // inside the first differing character.
21    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}