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
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 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 super::{casecmp, eq};
    use core::cmp::Ordering;

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

        assert!(matches!(casecmp("abcdef", "abcde"), Ordering::Greater));
        assert!(matches!(casecmp("aBcDeF", "abcdef"), Ordering::Equal));
        assert!(matches!(casecmp("abcdef", "abcdefg"), Ordering::Less));
        assert!(matches!(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 doesent_consider_non_ascii_chars_equal_that_arent() {
        // -- 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!(!eq(lower_a_tilde, lower_a_umlaut));
        assert!(!eq(lower_a_umlaut, lower_a_tilde));
        assert!(!eq(upper_a_tilde, upper_a_umlaut));
        assert!(!eq(upper_a_umlaut, upper_a_tilde));

        assert!(!matches!(
            casecmp(lower_a_tilde, lower_a_umlaut),
            Ordering::Equal
        ));
        assert!(!matches!(
            casecmp(lower_a_umlaut, lower_a_tilde),
            Ordering::Equal
        ));
        assert!(!matches!(
            casecmp(upper_a_tilde, upper_a_umlaut),
            Ordering::Equal
        ));
        assert!(!matches!(
            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!(eq(upper_a_tilde, lower_a_tilde));
        assert!(eq(upper_a_umlaut, lower_a_umlaut));
        assert!(eq(lower_a_tilde, upper_a_tilde));
        assert!(eq(lower_a_umlaut, upper_a_umlaut));

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