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
217
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

sub-strs

Copyright (C) 2019-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2019-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Case-insensitive string

use {
    alloc::{
        borrow::Cow,
        string::{String, ToString},
    },
    core::{
        cmp::Ordering,
        fmt,
        hash::{Hash, Hasher},
        str::FromStr,
    },
    crate::Error,
};

/// # Case-insensitive string
///
/// ## Notes
///
/// - This struct is intended for sorting in lists, or to be used in sets, so it _generates_ a lower-case form of source string and stores it
///   inside. That might look like a waste of memory, but it helps with performance. For example when you call [`Vec::sort()`][r://Vec/sort()]
///   or when you insert it into a [`HashSet`][r://HashSet], it doesn't have to generate any string again and again...
/// - Implementations of `From<&'a str>`, `From<&'a String>` and `From<String>` either borrow or take the source string and store it inside.
/// - Implementation of `FromStr` will _clone_ the source strings.
///
/// ## Examples
///
/// ```
/// use std::collections::HashSet;
/// use sub_strs::CainStr;
///
/// let wild_data: HashSet<_> = vec!["swift", "C++", "rust", "SWIFT"].into_iter().collect();
/// assert_eq!(wild_data.len(), 4);
///
/// let data: HashSet<_> = wild_data.into_iter().map(|s| CainStr::from(s)).collect();
/// assert_eq!(data.len(), 3);
///
/// let mut data: Vec<_> = data.into_iter().collect();
/// data.sort();
/// let data: Vec<_> = data.iter().map(|cs| cs.as_ref()).collect();
/// assert_eq!(&data[..2], &["C++", "rust"]);
/// assert!(data[2].eq_ignore_ascii_case("swift"));
/// ```
///
/// [r://HashSet]: https://doc.rust-lang.org/std/collections/struct.HashSet.html
/// [r://Vec/sort()]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort
#[derive(Debug, Eq)]
pub struct CainStr<'a> {

    /// # Source string
    src: Cow<'a, str>,

    /// # Lower-case form of source string
    lowercase: String,

}

impl CainStr<'_> {

    /// # Gets the lowercase form of source string
    pub fn lowercase(&self) -> &str {
        &self.lowercase
    }

    /// # Converts self into the lowercase form of source string
    ///
    /// This function simply takes the inner field out. There is no allocation.
    pub fn into_lowercase(self) -> String {
        self.lowercase
    }

}

impl Clone for CainStr<'_> {

    fn clone(&self) -> Self {
        Self {
            src: self.src.clone().into_owned().into(),
            lowercase: self.lowercase.to_string(),
        }
    }

}

impl PartialEq for CainStr<'_> {

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

}

impl Ord for CainStr<'_> {

    fn cmp(&self, other: &Self) -> Ordering {
        self.lowercase.cmp(&other.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.lowercase.hash(h);
    }

}

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

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

}

impl<'a> From<Cow<'a, str>> for CainStr<'a> {

    fn from(src: Cow<'a, str>) -> Self {
        let lowercase = src.to_lowercase();
        Self {
            src,
            lowercase,
        }
    }

}

impl<'a> From<CainStr<'a>> for Cow<'a, str> {

    fn from(src: CainStr<'a>) -> Self {
        src.src
    }

}

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

    fn from(src: &'a str) -> Self {
        Self {
            src: src.into(),
            lowercase: src.to_lowercase(),
        }
    }

}

impl From<String> for CainStr<'_> {

    fn from(src: String) -> Self {
        let lowercase = src.to_lowercase();
        Self {
            src: src.into(),
            lowercase,
        }
    }

}

impl FromStr for CainStr<'_> {

    type Err = Error;

    fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
        Ok(Self::from(String::from(s)))
    }

}

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

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

}

#[test]
fn test_cain_str() {
    let s = "UPPER-CASE";
    assert_eq!(s.to_lowercase(), CainStr::from(s).lowercase);
}