focaccia/folding/
turkic.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use core::cmp::Ordering;

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

/// Compare two strings with Full Unicode case folding for Turkic languages.
///
/// This function is implemented with a lookup table generated from Unicode case
/// folding tables.
///
/// # Examples
///
/// ```
/// # use core::cmp::Ordering;
/// # use focaccia::unicode_full_turkic_casecmp;
/// assert_eq!(unicode_full_turkic_casecmp("İstanbul", "istanbul"), Ordering::Equal);
/// assert_ne!(unicode_full_turkic_casecmp("İstanbul", "Istanbul"), Ordering::Equal);
/// ```
///
/// # Examples – Full
///
/// Turkic case folding is largely compatible with full Unicode case folding.
///
/// ```
/// # use core::cmp::Ordering;
/// # use focaccia::unicode_full_turkic_casecmp;
/// assert_eq!(unicode_full_turkic_casecmp("MASSE", "Maße"), Ordering::Equal);
/// assert_eq!(unicode_full_turkic_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::Turkic));
    let right = right.chars().flat_map(|c| lookup(c, Mode::Turkic));
    left.cmp(right)
}

/// Check two strings for equality with Full Unicode case folding for Turkic
/// languages.
///
/// This function is implemented with a lookup table generated from Unicode case
/// folding tables.
///
/// # Examples
///
/// ```
/// # use focaccia::unicode_full_turkic_case_eq;
/// assert!(unicode_full_turkic_case_eq("İstanbul", "istanbul"));
/// assert!(!unicode_full_turkic_case_eq("İstanbul", "Istanbul"));
/// ```
///
/// # Examples – Full
///
/// Turkic case folding is largely compatible with full Unicode case folding.
///
/// ```
/// # use focaccia::unicode_full_turkic_case_eq;
/// assert!(unicode_full_turkic_case_eq("MASSE", "Maße"));
/// assert!(!unicode_full_turkic_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::Turkic));
    let right = right.chars().flat_map(|c| lookup(c, Mode::Turkic));
    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);

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

        assert!(!case_eq("İstanbul", ""));
        assert_eq!(casecmp("İstanbul", ""), 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);
    }

    #[test]
    fn does_case_mapping_for_turkic_unicode_chars() {
        // -- UTF-8 --
        let upper_dotless_i = "I";
        let lower_dotless_i = "ı";
        let upper_dotted_i = "İ";
        let lower_dotted_i = "i";

        assert!(case_eq(upper_dotless_i, lower_dotless_i));
        assert!(case_eq(upper_dotted_i, lower_dotted_i));
        assert!(case_eq(lower_dotless_i, upper_dotless_i));
        assert!(case_eq(lower_dotted_i, upper_dotted_i));

        assert_eq!(casecmp(upper_dotless_i, lower_dotless_i), Ordering::Equal);
        assert_eq!(casecmp(upper_dotted_i, lower_dotted_i), Ordering::Equal);
        assert_eq!(casecmp(lower_dotless_i, upper_dotless_i), Ordering::Equal);
        assert_eq!(casecmp(lower_dotted_i, upper_dotted_i), Ordering::Equal);

        assert!(!case_eq(upper_dotless_i, upper_dotted_i));
        assert!(!case_eq(upper_dotless_i, lower_dotted_i));
        assert!(!case_eq(lower_dotless_i, upper_dotted_i));
        assert!(!case_eq(lower_dotless_i, lower_dotted_i));
        assert!(!case_eq(upper_dotted_i, upper_dotless_i));
        assert!(!case_eq(upper_dotted_i, lower_dotless_i));
        assert!(!case_eq(lower_dotted_i, upper_dotless_i));
        assert!(!case_eq(lower_dotted_i, lower_dotless_i));

        assert_ne!(casecmp(upper_dotless_i, upper_dotted_i), Ordering::Equal);
        assert_ne!(casecmp(upper_dotless_i, lower_dotted_i), Ordering::Equal);
        assert_ne!(casecmp(lower_dotless_i, upper_dotted_i), Ordering::Equal);
        assert_ne!(casecmp(lower_dotless_i, lower_dotted_i), Ordering::Equal);
        assert_ne!(casecmp(upper_dotted_i, upper_dotless_i), Ordering::Equal);
        assert_ne!(casecmp(upper_dotted_i, lower_dotless_i), Ordering::Equal);
        assert_ne!(casecmp(lower_dotted_i, upper_dotless_i), Ordering::Equal);
        assert_ne!(casecmp(lower_dotted_i, lower_dotless_i), Ordering::Equal);
    }
}