harper_core/
char_string.rs1use crate::char_ext::CharExt;
2use std::borrow::Cow;
3
4use smallvec::SmallVec;
5
6pub(crate) const CHAR_STRING_INLINE_SIZE: usize = 16;
8
9pub type CharString = SmallVec<[char; CHAR_STRING_INLINE_SIZE]>;
12
13mod private {
14 pub trait Sealed {}
15
16 impl Sealed for [char] {}
17}
18
19pub trait CharStringExt: private::Sealed {
21 fn to_lower(&'_ self) -> Cow<'_, [char]>;
23
24 fn normalized(&'_ self) -> Cow<'_, [char]>;
26
27 fn to_string(&self) -> String;
29
30 fn eq_ch(&self, other: &[char]) -> bool;
33
34 fn eq_str(&self, other: &str) -> bool;
37
38 fn eq_any_ignore_ascii_case_str(&self, others: &[&str]) -> bool;
41
42 fn eq_any_ignore_ascii_case_chars(&self, others: &[&[char]]) -> bool;
45
46 fn starts_with_ignore_ascii_case_str(&self, prefix: &str) -> bool;
49
50 fn starts_with_any_ignore_ascii_case_str(&self, prefixes: &[&str]) -> bool;
53
54 fn ends_with_ignore_ascii_case_chars(&self, suffix: &[char]) -> bool;
57
58 fn ends_with_ignore_ascii_case_str(&self, suffix: &str) -> bool;
61
62 fn ends_with_any_ignore_ascii_case_chars(&self, suffixes: &[&[char]]) -> bool;
65
66 fn contains_vowel(&self) -> bool;
68}
69
70impl CharStringExt for [char] {
71 fn to_lower(&'_ self) -> Cow<'_, [char]> {
72 if self.iter().all(|c| c.is_lowercase()) {
73 return Cow::Borrowed(self);
74 }
75
76 let mut out = CharString::with_capacity(self.len());
77
78 out.extend(self.iter().flat_map(|v| v.to_lowercase()));
79
80 Cow::Owned(out.to_vec())
81 }
82
83 fn to_string(&self) -> String {
84 self.iter().collect()
85 }
86
87 fn normalized(&'_ self) -> Cow<'_, [char]> {
90 if self.as_ref().iter().any(|c| c.normalized() != *c) {
91 Cow::Owned(
92 self.as_ref()
93 .iter()
94 .copied()
95 .map(|c| c.normalized())
96 .collect(),
97 )
98 } else {
99 Cow::Borrowed(self)
100 }
101 }
102
103 fn eq_str(&self, other: &str) -> bool {
104 let mut chit = self.iter();
105 let mut strit = other.chars();
106
107 loop {
108 let (c, s) = (chit.next(), strit.next());
109 match (c, s) {
110 (Some(c), Some(s)) => {
111 if c.to_ascii_lowercase() != s {
112 return false;
113 }
114 }
115 (None, None) => return true,
116 _ => return false,
117 }
118 }
119 }
120
121 fn eq_ch(&self, other: &[char]) -> bool {
122 self.len() == other.len()
123 && self
124 .iter()
125 .zip(other.iter())
126 .all(|(a, b)| a.to_ascii_lowercase() == *b)
127 }
128
129 fn eq_any_ignore_ascii_case_str(&self, others: &[&str]) -> bool {
130 others.iter().any(|str| self.eq_str(str))
131 }
132
133 fn eq_any_ignore_ascii_case_chars(&self, others: &[&[char]]) -> bool {
134 others.iter().any(|chars| self.eq_ch(chars))
135 }
136
137 fn starts_with_ignore_ascii_case_str(&self, prefix: &str) -> bool {
138 let prefix_len = prefix.chars().count();
139 if self.len() < prefix_len {
140 return false;
141 }
142 self.iter()
143 .take(prefix_len)
144 .zip(prefix.chars())
145 .all(|(a, b)| a.to_ascii_lowercase() == b)
146 }
147
148 fn starts_with_any_ignore_ascii_case_str(&self, prefixes: &[&str]) -> bool {
149 prefixes
150 .iter()
151 .any(|prefix| self.starts_with_ignore_ascii_case_str(prefix))
152 }
153
154 fn ends_with_ignore_ascii_case_str(&self, suffix: &str) -> bool {
155 let suffix_len = suffix.chars().count();
156 if self.len() < suffix_len {
157 return false;
158 }
159 self.iter()
160 .rev()
161 .take(suffix_len)
162 .rev()
163 .zip(suffix.chars())
164 .all(|(a, b)| a.to_ascii_lowercase() == b)
165 }
166
167 fn ends_with_ignore_ascii_case_chars(&self, suffix: &[char]) -> bool {
168 let suffix_len = suffix.len();
169 if self.len() < suffix_len {
170 return false;
171 }
172 self.iter()
173 .rev()
174 .take(suffix_len)
175 .rev()
176 .zip(suffix.iter())
177 .all(|(a, b)| a.to_ascii_lowercase() == *b)
178 }
179
180 fn ends_with_any_ignore_ascii_case_chars(&self, suffixes: &[&[char]]) -> bool {
181 suffixes
182 .iter()
183 .any(|suffix| self.ends_with_ignore_ascii_case_chars(suffix))
184 }
185
186 fn contains_vowel(&self) -> bool {
187 self.iter().any(|c| c.is_vowel())
188 }
189}
190
191macro_rules! char_string {
192 ($string:literal) => {{
193 use crate::char_string::CharString;
194
195 $string.chars().collect::<CharString>()
196 }};
197}
198
199pub(crate) use char_string;
200
201#[cfg(test)]
202mod tests {
203 use super::CharStringExt;
204
205 #[test]
206 fn eq_ignore_ascii_case_chars_matches_lowercase() {
207 assert!(['H', 'e', 'l', 'l', 'o'].eq_ch(&['h', 'e', 'l', 'l', 'o']));
208 }
209
210 #[test]
211 fn eq_ignore_ascii_case_chars_does_not_match_different_word() {
212 assert!(!['H', 'e', 'l', 'l', 'o'].eq_ch(&['w', 'o', 'r', 'l', 'd']));
213 }
214
215 #[test]
216 fn eq_ignore_ascii_case_str_matches_lowercase() {
217 assert!(['H', 'e', 'l', 'l', 'o'].eq_str("hello"));
218 }
219
220 #[test]
221 fn eq_ignore_ascii_case_str_does_not_match_different_word() {
222 assert!(!['H', 'e', 'l', 'l', 'o'].eq_str("world"));
223 }
224
225 #[test]
226 fn ends_with_ignore_ascii_case_chars_matches_suffix() {
227 assert!(['H', 'e', 'l', 'l', 'o'].ends_with_ignore_ascii_case_chars(&['l', 'o']));
228 }
229
230 #[test]
231 fn ends_with_ignore_ascii_case_chars_does_not_match_different_suffix() {
232 assert!(
233 !['H', 'e', 'l', 'l', 'o']
234 .ends_with_ignore_ascii_case_chars(&['w', 'o', 'r', 'l', 'd'])
235 );
236 }
237
238 #[test]
239 fn ends_with_ignore_ascii_case_str_matches_suffix() {
240 assert!(['H', 'e', 'l', 'l', 'o'].ends_with_ignore_ascii_case_str("lo"));
241 }
242
243 #[test]
244 fn ends_with_ignore_ascii_case_str_does_not_match_different_suffix() {
245 assert!(!['H', 'e', 'l', 'l', 'o'].ends_with_ignore_ascii_case_str("world"));
246 }
247
248 #[test]
249 fn differs_only_by_length_1() {
250 assert!(!['b', 'b'].eq_str("b"));
251 }
252
253 #[test]
254 fn differs_only_by_length_2() {
255 assert!(!['c'].eq_str("cc"));
256 }
257}