/rust/registry/src/index.crates.io-6f17d22bba15001f/unicase-2.8.1/src/ascii.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use alloc::string::String; |
2 | | use core::cmp::Ordering; |
3 | | use core::fmt; |
4 | | use core::hash::{Hash, Hasher}; |
5 | | use core::ops::{Deref, DerefMut}; |
6 | | use core::str::FromStr; |
7 | | |
8 | | use super::{Ascii, Encoding, UniCase}; |
9 | | |
10 | | impl<S> Ascii<S> { |
11 | | /// Construct a new `Ascii`. |
12 | | /// |
13 | | /// For Rust versions >= 1.31, this is a `const fn`. |
14 | | #[inline] |
15 | 0 | pub const fn new(s: S) -> Ascii<S> { |
16 | 0 | Ascii(s) |
17 | 0 | } |
18 | | |
19 | | /// Convert this into a [`UniCase`]. |
20 | 0 | pub const fn into_unicase(self) -> UniCase<S> { |
21 | 0 | UniCase(Encoding::Ascii(self)) |
22 | 0 | } |
23 | | |
24 | | /// Consume this `Ascii` and get the inner value. |
25 | | #[inline] |
26 | 0 | pub fn into_inner(self) -> S { |
27 | 0 | self.0 |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl<S> Deref for Ascii<S> { |
32 | | type Target = S; |
33 | | #[inline] |
34 | 0 | fn deref<'a>(&'a self) -> &'a S { |
35 | 0 | &self.0 |
36 | 0 | } |
37 | | } |
38 | | |
39 | | impl<S> DerefMut for Ascii<S> { |
40 | | #[inline] |
41 | 0 | fn deref_mut<'a>(&'a mut self) -> &'a mut S { |
42 | 0 | &mut self.0 |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl<T: AsRef<str>> PartialOrd for Ascii<T> { |
47 | | #[inline] |
48 | 0 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
49 | 0 | Some(self.cmp(other)) |
50 | 0 | } |
51 | | } |
52 | | |
53 | | impl<T: AsRef<str>> Ord for Ascii<T> { |
54 | | #[inline] |
55 | 0 | fn cmp(&self, other: &Self) -> Ordering { |
56 | 0 | let self_chars = self.as_ref().chars().map(|c| c.to_ascii_lowercase()); |
57 | 0 | let other_chars = other.as_ref().chars().map(|c| c.to_ascii_lowercase()); |
58 | 0 | self_chars.cmp(other_chars) |
59 | 0 | } |
60 | | } |
61 | | |
62 | | impl<S: AsRef<str>> AsRef<str> for Ascii<S> { |
63 | | #[inline] |
64 | 3.86M | fn as_ref(&self) -> &str { |
65 | 3.86M | self.0.as_ref() |
66 | 3.86M | } <unicase::Ascii<pulldown_cmark::strings::CowStr> as core::convert::AsRef<str>>::as_ref Line | Count | Source | 64 | 3.86M | fn as_ref(&self) -> &str { | 65 | 3.86M | self.0.as_ref() | 66 | 3.86M | } |
Unexecuted instantiation: <unicase::Ascii<_> as core::convert::AsRef<str>>::as_ref |
67 | | } |
68 | | |
69 | | impl<S: fmt::Display> fmt::Display for Ascii<S> { |
70 | | #[inline] |
71 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
72 | 0 | fmt::Display::fmt(&self.0, fmt) |
73 | 0 | } |
74 | | } |
75 | | |
76 | | impl<S1: AsRef<str>> PartialEq<Ascii<S1>> for String { |
77 | | #[inline] |
78 | 0 | fn eq(&self, other: &Ascii<S1>) -> bool { |
79 | 0 | other == self |
80 | 0 | } |
81 | | } |
82 | | |
83 | | impl<'a, S1: AsRef<str>> PartialEq<Ascii<S1>> for &'a str { |
84 | | #[inline] |
85 | 0 | fn eq(&self, other: &Ascii<S1>) -> bool { |
86 | 0 | other == self |
87 | 0 | } |
88 | | } |
89 | | |
90 | | impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<S2> for Ascii<S1> { |
91 | | #[inline] |
92 | 623k | fn eq(&self, other: &S2) -> bool { |
93 | 623k | self.as_ref().eq_ignore_ascii_case(other.as_ref()) |
94 | 623k | } <unicase::Ascii<pulldown_cmark::strings::CowStr> as core::cmp::PartialEq>::eq Line | Count | Source | 92 | 623k | fn eq(&self, other: &S2) -> bool { | 93 | 623k | self.as_ref().eq_ignore_ascii_case(other.as_ref()) | 94 | 623k | } |
Unexecuted instantiation: <unicase::Ascii<_> as core::cmp::PartialEq<_>>::eq |
95 | | } |
96 | | |
97 | | impl<S: AsRef<str>> Eq for Ascii<S> {} |
98 | | |
99 | | impl<S: FromStr> FromStr for Ascii<S> { |
100 | | type Err = <S as FromStr>::Err; |
101 | 0 | fn from_str(s: &str) -> Result<Ascii<S>, <S as FromStr>::Err> { |
102 | 0 | s.parse().map(Ascii) |
103 | 0 | } |
104 | | } |
105 | | |
106 | | impl<S: AsRef<str>> Hash for Ascii<S> { |
107 | | #[inline] |
108 | 2.59M | fn hash<H: Hasher>(&self, hasher: &mut H) { |
109 | 19.5M | for byte in self.as_ref().bytes().map(|b| b.to_ascii_lowercase()) { <unicase::Ascii<pulldown_cmark::strings::CowStr> as core::hash::Hash>::hash::<std::hash::random::DefaultHasher>::{closure#0} Line | Count | Source | 109 | 19.5M | for byte in self.as_ref().bytes().map(|b| b.to_ascii_lowercase()) { |
Unexecuted instantiation: <unicase::Ascii<_> as core::hash::Hash>::hash::<_>::{closure#0} |
110 | 19.5M | hasher.write_u8(byte); |
111 | 19.5M | } |
112 | | // prefix-freedom |
113 | 2.59M | hasher.write_u8(0xFF); |
114 | 2.59M | } <unicase::Ascii<pulldown_cmark::strings::CowStr> as core::hash::Hash>::hash::<std::hash::random::DefaultHasher> Line | Count | Source | 108 | 2.59M | fn hash<H: Hasher>(&self, hasher: &mut H) { | 109 | 19.5M | for byte in self.as_ref().bytes().map(|b| b.to_ascii_lowercase()) { | 110 | 19.5M | hasher.write_u8(byte); | 111 | 19.5M | } | 112 | | // prefix-freedom | 113 | 2.59M | hasher.write_u8(0xFF); | 114 | 2.59M | } |
Unexecuted instantiation: <unicase::Ascii<_> as core::hash::Hash>::hash::<_> |
115 | | } |
116 | | |
117 | | #[cfg(test)] |
118 | | mod tests { |
119 | | use super::Ascii; |
120 | | use std::collections::hash_map::DefaultHasher; |
121 | | use std::hash::{Hash, Hasher}; |
122 | | use std::string::String; |
123 | | |
124 | | fn hash<T: Hash>(t: &T) -> u64 { |
125 | | let mut s = DefaultHasher::new(); |
126 | | t.hash(&mut s); |
127 | | s.finish() |
128 | | } |
129 | | |
130 | | #[test] |
131 | | fn test_case_insensitive() { |
132 | | let a = Ascii("foobar"); |
133 | | let b = Ascii("FOOBAR"); |
134 | | |
135 | | assert_eq!(a, b); |
136 | | assert_eq!(hash(&a), hash(&b)); |
137 | | |
138 | | assert_eq!(a, "fooBar"); |
139 | | assert_eq!("fooBar", a); |
140 | | assert_eq!(String::from("fooBar"), a); |
141 | | assert_eq!(a, String::from("fooBar")); |
142 | | } |
143 | | |
144 | | #[cfg(feature = "nightly")] |
145 | | #[bench] |
146 | | fn bench_ascii_eq(b: &mut ::test::Bencher) { |
147 | | b.bytes = b"foobar".len() as u64; |
148 | | b.iter(|| assert_eq!(Ascii("foobar"), Ascii("FOOBAR"))); |
149 | | } |
150 | | |
151 | | #[test] |
152 | | fn test_case_cmp() { |
153 | | assert!(Ascii("foobar") == Ascii("FOOBAR")); |
154 | | assert!(Ascii("a") < Ascii("B")); |
155 | | |
156 | | assert!(Ascii("A") < Ascii("b")); |
157 | | assert!(Ascii("aa") > Ascii("a")); |
158 | | |
159 | | assert!(Ascii("a") < Ascii("aa")); |
160 | | assert!(Ascii("a") < Ascii("AA")); |
161 | | } |
162 | | |
163 | | #[test] |
164 | | fn test_ascii_new_const() { |
165 | | const _ASCII: Ascii<&'static str> = Ascii::new(""); |
166 | | } |
167 | | } |