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
// License: see LICENSE file at root directory of `master` branch

//! # Case-insensitive string (borrowed)

use std::{
    cmp::Ordering,
    fmt,
    hash::{Hash, Hasher},
};

/// # Case-insensitive string (borrowed)
///
/// ## Notes
///
/// - Implementations of [`PartialEq`][rust::PartialEq], [`Ord`][rust::Ord], [`PartialOrd`][rust::PartialOrd], [`Hash`][rust::Hash] call
///   [`::to_lowercase()`][rust::to_lowercase()] on source string. So they are _slow_.
///
/// [rust::Eq]: https://doc.rust-lang.org/std/cmp/trait.Eq.html
/// [rust::PartialEq]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [rust::Ord]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
/// [rust::PartialOrd]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html
/// [rust::Hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
/// [rust::Display]: https://doc.rust-lang.org/std/fmt/trait.Display.html
/// [rust::to_lowercase()]: https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
#[derive(Debug, Eq)]
pub struct CainStr<'a> {

    /// # Source string
    src: &'a str,

}

impl PartialEq for CainStr<'_> {

    fn eq(&self, other: &Self) -> bool {
        self.src.to_lowercase().eq(&other.src.to_lowercase())
    }

}

impl Ord for CainStr<'_> {

    fn cmp(&self, other: &Self) -> Ordering {
        self.src.to_lowercase().cmp(&other.src.to_lowercase())
    }

}

impl PartialOrd for CainStr<'_> {

    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }

}

impl Hash for CainStr<'_> {

    fn hash<H>(&self, h: &mut H) where H: Hasher {
        self.src.to_lowercase().hash(h);
    }

}

impl fmt::Display for CainStr<'_> {

    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(self.src)
    }

}

impl<'a> From<&'a str> for CainStr<'a> {

    fn from(src: &'a str) -> Self {
        Self { src }
    }

}

impl<'a> From<CainStr<'a>> for &'a str {

    fn from(cain_str: CainStr<'a>) -> &'a str {
        cain_str.src
    }

}

impl AsRef<str> for CainStr<'_> {

    fn as_ref(&self) -> &str {
        self.src
    }

}