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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use alloc::borrow::{Cow, Borrow};
use alloc::{string::String, boxed::Box};

use core::ops::Deref;
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use core::fmt;

use crate::UncasedStr;

/// An uncased (case-insensitive, case-preserving), owned _or_ borrowed ASCII
/// string.
#[cfg_attr(nightly, doc(cfg(feature = "alloc")))]
#[derive(Clone, Debug)]
pub struct Uncased<'s> {
    #[doc(hidden)]
    pub string: Cow<'s, str>
}

impl<'s> Uncased<'s> {
    /// Creates a new `Uncased` string from `string` without allocating.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// let uncased = Uncased::new("Content-Type");
    /// assert_eq!(uncased, "content-type");
    /// assert_eq!(uncased, "CONTENT-Type");
    /// ```
    #[inline(always)]
    pub fn new<S: Into<Cow<'s, str>>>(string: S) -> Uncased<'s> {
        Uncased { string: string.into() }
    }

    /// Creates a new `Uncased` string from a borrowed `string`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// const UNCASED: Uncased = Uncased::from_borrowed("Content-Type");
    /// assert_eq!(UNCASED, "content-type");
    /// assert_eq!(UNCASED, "CONTENT-Type");
    /// ```
    #[inline(always)]
    pub const fn from_borrowed(string: &'s str) -> Uncased<'s> {
        Uncased { string: Cow::Borrowed(string) }
    }

    /// Creates a new `Uncased` string from `string` without allocating.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// const UNCASED: Uncased = Uncased::from_owned(String::new());
    ///
    /// let uncased = Uncased::from_owned("Content-Type".to_string());
    /// assert_eq!(uncased, "content-type");
    /// assert_eq!(uncased, "CONTENT-Type");
    /// ```
    #[inline(always)]
    pub const fn from_owned(string: String) -> Uncased<'s> {
        Uncased { string: Cow::Owned(string) }
    }

    /// Converts `self` into an owned `Uncased<'static>`, allocating if
    /// necessary.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// let foo = "foo".to_string();
    /// let uncased = Uncased::from(foo.as_str());
    /// let owned: Uncased<'static> = uncased.into_owned();
    /// assert_eq!(owned, "foo");
    /// ```
    #[inline(always)]
    pub fn as_uncased_str(&self) -> &UncasedStr {
        self.as_ref()
    }

    /// Converts `self` into an owned `String`, allocating if necessary.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// let uncased = Uncased::new("Content-Type");
    /// let string = uncased.into_string();
    /// assert_eq!(string, "Content-Type".to_string());
    /// ```
    #[inline(always)]
    pub fn into_string(self) -> String {
        self.string.into_owned()
    }

    /// Converts `self` into an owned `Uncased<'static>`, allocating if
    /// necessary.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// let foo = "foo".to_string();
    /// let uncased = Uncased::from(foo.as_str());
    /// let owned: Uncased<'static> = uncased.into_owned();
    /// assert_eq!(owned, "foo");
    /// ```
    #[inline(always)]
    pub fn into_owned(self) -> Uncased<'static> {
        match self.string {
            Cow::Owned(string) => Uncased::new(string),
            Cow::Borrowed(str) => Uncased::new(String::from(str)),
        }
    }

    /// Converts `self` into a `Box<UncasedStr>`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uncased::Uncased;
    ///
    /// let boxed = Uncased::new("Content-Type").into_boxed_uncased();
    /// assert_eq!(&*boxed, "content-type");
    /// ```
    #[inline(always)]
    pub fn into_boxed_uncased(self) -> Box<UncasedStr> {
        // This is simply a `newtype`-like transformation. The
        // `repr(transparent)` ensures that this is safe and correct. Note this
        // exact pattern appears often in the standard library.
        unsafe {
            let raw_str = Box::into_raw(self.string.into_owned().into_boxed_str());
            Box::from_raw(raw_str as *mut UncasedStr)
        }
    }

    /// Returns the inner `Cow`.
    #[doc(hidden)]
    #[inline(always)]
    pub fn into_cow(self) -> Cow<'s, str> {
        self.string
    }
}

impl Deref for Uncased<'_> {
    type Target = UncasedStr;

    #[inline(always)]
    fn deref(&self) -> &UncasedStr {
        UncasedStr::new(self.string.borrow())
    }
}

impl AsRef<UncasedStr> for Uncased<'_> {
    #[inline(always)]
    fn as_ref(&self) -> &UncasedStr {
        UncasedStr::new(self.string.borrow())
    }
}

impl AsRef<str> for Uncased<'_> {
    #[inline(always)]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<[u8]> for Uncased<'_> {
    #[inline(always)]
    fn as_ref(&self) -> &[u8] {
        self.as_str().as_bytes()
    }
}

impl Borrow<UncasedStr> for Uncased<'_> {
    #[inline(always)]
    fn borrow(&self) -> &UncasedStr {
        self.as_str().into()
    }
}

impl<'s, 'c: 's> From<&'c UncasedStr> for Uncased<'s> {
    #[inline(always)]
    fn from(string: &'c UncasedStr) -> Self {
        Uncased::new(string.as_str())
    }
}

impl<'s, 'c: 's> From<&'c str> for Uncased<'s> {
    #[inline(always)]
    fn from(string: &'c str) -> Self {
        Uncased::new(string)
    }
}

impl From<String> for Uncased<'static> {
    #[inline(always)]
    fn from(string: String) -> Self {
        Uncased::new(string)
    }
}

impl<'s, 'c: 's> From<Cow<'c, str>> for Uncased<'s> {
    #[inline(always)]
    fn from(string: Cow<'c, str>) -> Self {
        Uncased::new(string)
    }
}

impl fmt::Display for Uncased<'_> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.string.fmt(f)
    }
}

macro_rules! impl_partial_eq {
    ($other:ty $([$o_i:ident])? = $this:ty $([$t_i:ident])?) => (
        impl PartialEq<$other> for $this {
            #[inline(always)]
            fn eq(&self, other: &$other) -> bool {
                self $(.$t_i())? .eq(other $(.$o_i())?)
            }
        }
    )
}

impl_partial_eq!(Uncased<'_> [as_uncased_str] = Uncased<'_> [as_uncased_str]);
impl_partial_eq!(str = Uncased<'_> [as_uncased_str]);
impl_partial_eq!(Uncased<'_> [as_uncased_str] = str);
impl_partial_eq!(&str = Uncased<'_> [as_uncased_str]);
impl_partial_eq!(Uncased<'_> [as_uncased_str] = &str);
impl_partial_eq!(String [as_str] = Uncased<'_> [as_uncased_str]);
impl_partial_eq!(Uncased<'_> [as_uncased_str] = String [as_str]);
impl_partial_eq!(String [as_str] = &Uncased<'_> [as_uncased_str]);
impl_partial_eq!(&Uncased<'_> [as_uncased_str] = String [as_str]);


macro_rules! impl_partial_ord {
    ($other:ty $([$o_i:ident])? >< $this:ty $([$t_i:ident])?) => (
        impl PartialOrd<$other> for $this {
            #[inline(always)]
            fn partial_cmp(&self, other: &$other) -> Option<Ordering> {
                let this: &UncasedStr = self$(.$t_i())?.into();
                let other: &UncasedStr = other$(.$o_i())?.into();
                this.partial_cmp(other)
            }
        }
    )
}

impl Ord for Uncased<'_> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_uncased_str().cmp(other.as_uncased_str())
    }
}

impl_partial_ord!(Uncased<'_> [as_uncased_str] >< Uncased<'_> [as_uncased_str]);
impl_partial_ord!(str >< Uncased<'_> [as_uncased_str]);
impl_partial_ord!(Uncased<'_> [as_uncased_str] >< str);
impl_partial_ord!(String [as_str] >< Uncased<'_> [as_uncased_str]);
impl_partial_ord!(Uncased<'_> [as_uncased_str] >< String [as_str]);
impl_partial_ord!(String [as_str] >< &Uncased<'_> [as_uncased_str]);
impl_partial_ord!(&Uncased<'_> [as_uncased_str] >< String [as_str]);

impl Eq for Uncased<'_> {  }

impl Hash for Uncased<'_> {
    #[inline(always)]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.as_uncased_str().hash(hasher)
    }
}