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
#![cfg_attr(test, deny(missing_docs))]
#![cfg_attr(test, deny(warnings))]
use std::ascii::AsciiExt;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
#[derive(Clone, Debug)]
pub struct UniCase<S>(pub S);
impl<S> Deref for UniCase<S> {
type Target = S;
#[inline]
fn deref<'a>(&'a self) -> &'a S {
&self.0
}
}
impl<S> DerefMut for UniCase<S> {
#[inline]
fn deref_mut<'a>(&'a mut self) -> &'a mut S {
&mut self.0
}
}
impl<S: AsRef<str>> AsRef<str> for UniCase<S> {
#[inline]
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl<S: fmt::Display> fmt::Display for UniCase<S> {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
}
}
impl<S: AsRef<str>> PartialEq for UniCase<S> {
#[inline]
fn eq(&self, other: &UniCase<S>) -> bool {
self.as_ref().eq_ignore_ascii_case(other.as_ref())
}
}
impl<S: AsRef<str>> PartialEq<S> for UniCase<S> {
#[inline]
fn eq(&self, other: &S) -> bool {
self.as_ref().eq_ignore_ascii_case(other.as_ref())
}
}
impl<S: AsRef<str>> Eq for UniCase<S> {}
impl<S: FromStr> FromStr for UniCase<S> {
type Err = <S as FromStr>::Err;
fn from_str(s: &str) -> Result<UniCase<S>, <S as FromStr>::Err> {
s.parse().map(UniCase)
}
}
impl<S: AsRef<str>> Hash for UniCase<S> {
#[inline]
fn hash<H: Hasher>(&self, hasher: &mut H) {
for byte in self.as_ref().bytes().map(|b| b.to_ascii_lowercase()) {
hasher.write(&[byte]);
}
}
}
#[cfg(test)]
mod test {
use super::UniCase;
use std::hash::{Hash, Hasher, SipHasher};
fn hash<T: Hash>(t: &T) -> u64 {
let mut s = SipHasher::new();
t.hash(&mut s);
s.finish()
}
#[test]
fn test_case_insensitive() {
let a = UniCase("foobar");
let b = UniCase("FOOBAR");
assert_eq!(a, b);
assert_eq!(hash(&a), hash(&b));
}
}