str_utils/
eq_ignore_case.rs

1use unicase::UniCase;
2
3/**
4To extend types which implement `AsRef<str>` to have a `eq_ignore_case` method.
5
6However, it is not recommended to use this trait. Try to wrap your type inside `UniCase` by yourself instead.
7
8```rust
9use unicase::UniCase;
10
11let a = UniCase::new("Maße");
12let b = UniCase::new("MASSE");
13
14assert_eq!(a, b);
15```
16*/
17pub trait EqIgnoreCase {
18    /// Returns `true` if the given string slice case-insensitively (using case-folding) matches this string slice.
19    fn eq_ignore_case<S: AsRef<str>>(&self, b: S) -> bool;
20}
21
22impl<T: AsRef<str>> EqIgnoreCase for T {
23    #[inline]
24    fn eq_ignore_case<S: AsRef<str>>(&self, b: S) -> bool {
25        let a = self.as_ref();
26        let b = b.as_ref();
27
28        if a.is_ascii() {
29            if b.is_ascii() {
30                a.eq_ignore_ascii_case(b)
31            } else {
32                let a = UniCase::ascii(a);
33                let b = UniCase::unicode(b);
34
35                a == b
36            }
37        } else if b.is_ascii() {
38            let a = UniCase::unicode(a);
39            let b = UniCase::ascii(b);
40
41            a == b
42        } else {
43            let a = UniCase::unicode(a);
44            let b = UniCase::unicode(b);
45
46            a == b
47        }
48    }
49}