focaccia/folding/
full.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use core::cmp::Ordering;

use crate::folding::mapping::{lookup, Mode};

/// Compare two strings with Full Unicode case folding.
///
/// This function is implemented with a lookup table generated from Unicode case
/// folding tables.
///
/// # Examples
///
/// ```
/// # use core::cmp::Ordering;
/// # use focaccia::unicode_full_casecmp;
/// assert_eq!(unicode_full_casecmp("MASSE", "Maße"), Ordering::Equal);
/// assert_eq!(unicode_full_casecmp("São Paulo", "Sao Paulo"), Ordering::Greater);
/// ```
#[inline]
#[must_use]
pub fn casecmp(left: &str, right: &str) -> Ordering {
    let left = left.chars().flat_map(|c| lookup(c, Mode::Full));
    let right = right.chars().flat_map(|c| lookup(c, Mode::Full));
    left.cmp(right)
}

/// Check two strings for equality with Full Unicode case folding.
///
/// This function is implemented with a lookup table generated from Unicode case
/// folding tables.
///
/// # Examples
///
/// ```
/// # use focaccia::unicode_full_case_eq;
/// assert!(unicode_full_case_eq("MASSE", "Maße"));
/// assert!(!unicode_full_case_eq("São Paulo", "Sao Paulo"));
/// ```
#[inline]
#[must_use]
pub fn case_eq(left: &str, right: &str) -> bool {
    let left = left.chars().flat_map(|c| lookup(c, Mode::Full));
    let right = right.chars().flat_map(|c| lookup(c, Mode::Full));
    left.eq(right)
}

#[cfg(test)]
mod tests {
    use core::cmp::Ordering;

    use super::{case_eq, casecmp};

    #[test]
    fn empty_string() {
        assert!(case_eq("", ""));
        assert_eq!(casecmp("", ""), Ordering::Equal);

        assert!(!case_eq("", "rake"));
        assert_eq!(casecmp("", "rake"), Ordering::Less);

        assert!(!case_eq("rake", ""));
        assert_eq!(casecmp("rake", ""), Ordering::Greater);

        assert!(!case_eq("", "São Paulo"));
        assert_eq!(casecmp("", "São Paulo"), Ordering::Less);

        assert!(!case_eq("São Paulo", ""));
        assert_eq!(casecmp("São Paulo", ""), Ordering::Greater);
    }

    #[test]
    fn unicode_replacement_character() {
        assert!(case_eq("\u{FFFD}", "\u{FFFD}"));
        assert_eq!(casecmp("\u{FFFD}", "\u{FFFD}"), Ordering::Equal);

        assert_eq!(casecmp("\u{FFFD}", "\u{FFFD}yam"), Ordering::Less);
        assert_eq!(casecmp("\u{FFFD}yam", "\u{FFFD}"), Ordering::Greater);
    }

    #[test]
    fn compares_symbols_without_regard_to_case() {
        assert!(!case_eq("abcdef", "abcde"));
        assert!(case_eq("aBcDeF", "abcdef"));
        assert!(!case_eq("abcdef", "abcdefg"));
        assert!(case_eq("abcdef", "ABCDEF"));

        assert_eq!(casecmp("abcdef", "abcde"), Ordering::Greater);
        assert_eq!(casecmp("aBcDeF", "abcdef"), Ordering::Equal);
        assert_eq!(casecmp("abcdef", "abcdefg"), Ordering::Less);
        assert_eq!(casecmp("abcdef", "ABCDEF"), Ordering::Equal);

        assert_eq!(casecmp("abcdef", "abcde") as i32, 1);
        assert_eq!(casecmp("aBcDeF", "abcdef") as i32, 0);
        assert_eq!(casecmp("abcdef", "abcdefg") as i32, -1);
        assert_eq!(casecmp("abcdef", "ABCDEF") as i32, 0);
        assert_eq!(casecmp("abcdef", "abcde") as i32, 1);
    }

    #[test]
    fn non_ascii_chars_that_are_not_fold_eq_are_not_eq() {
        // -- UTF-8 --
        let upper_a_tilde = "Ã";
        let lower_a_tilde = "ã";
        let upper_a_umlaut = "Ä";
        let lower_a_umlaut = "ä";

        // From `spec/core/symbol/casecmp_spec.rb`:
        //
        // ```ruby
        // lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true
        // lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true
        // upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true
        // upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true
        // ```
        assert!(!case_eq(lower_a_tilde, lower_a_umlaut));
        assert!(!case_eq(lower_a_umlaut, lower_a_tilde));
        assert!(!case_eq(upper_a_tilde, upper_a_umlaut));
        assert!(!case_eq(upper_a_umlaut, upper_a_tilde));

        assert_ne!(casecmp(lower_a_tilde, lower_a_umlaut), Ordering::Equal);
        assert_ne!(casecmp(lower_a_umlaut, lower_a_tilde), Ordering::Equal);
        assert_ne!(casecmp(upper_a_tilde, upper_a_umlaut), Ordering::Equal);
        assert_ne!(casecmp(upper_a_umlaut, upper_a_tilde), Ordering::Equal);
    }

    #[test]
    fn does_case_mapping_for_unicode_chars() {
        // -- UTF-8 --
        let upper_a_tilde = "Ã";
        let lower_a_tilde = "ã";
        let upper_a_umlaut = "Ä";
        let lower_a_umlaut = "ä";

        // From `spec/core/symbol/casecmp_spec.rb`:
        //
        // ```ruby
        // upper_a_tilde.casecmp?(lower_a_tilde).should == true
        // upper_a_umlaut.casecmp?(lower_a_umlaut).should == true
        // lower_a_tilde.casecmp?(upper_a_tilde).should == true
        // lower_a_umlaut.casecmp?(upper_a_umlaut).should == true
        // ```
        assert!(case_eq(upper_a_tilde, lower_a_tilde));
        assert!(case_eq(upper_a_umlaut, lower_a_umlaut));
        assert!(case_eq(lower_a_tilde, upper_a_tilde));
        assert!(case_eq(lower_a_umlaut, upper_a_umlaut));

        assert_eq!(casecmp(upper_a_tilde, lower_a_tilde), Ordering::Equal);
        assert_eq!(casecmp(upper_a_umlaut, lower_a_umlaut), Ordering::Equal);
        assert_eq!(casecmp(lower_a_tilde, upper_a_tilde), Ordering::Equal);
        assert_eq!(casecmp(lower_a_umlaut, upper_a_umlaut), Ordering::Equal);
    }
}