harper_core/
char_string.rs

1use std::borrow::Cow;
2
3use smallvec::SmallVec;
4
5/// A char sequence that improves cache locality.
6/// Most English words are fewer than 12 characters.
7pub type CharString = SmallVec<[char; 16]>;
8
9/// Extensions to character sequences that make them easier to wrangle.
10pub trait CharStringExt {
11    fn to_lower(&self) -> Cow<[char]>;
12    fn to_string(&self) -> String;
13}
14
15impl CharStringExt for [char] {
16    fn to_lower(&self) -> Cow<[char]> {
17        if self.iter().all(|c| c.is_lowercase()) {
18            return Cow::Borrowed(self);
19        }
20
21        let mut out = CharString::with_capacity(self.len());
22
23        out.extend(self.iter().flat_map(|v| v.to_lowercase()));
24
25        Cow::Owned(out.to_vec())
26    }
27
28    fn to_string(&self) -> String {
29        self.iter().collect()
30    }
31}
32
33macro_rules! char_string {
34    ($string:literal) => {{
35        use crate::char_string::CharString;
36
37        $string.chars().collect::<CharString>()
38    }};
39}
40
41pub(crate) use char_string;