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
use std::borrow::Cow;
pub trait StrExtensions {
fn to_ascii_lowercase_cow(&self) -> Cow<'_, str>;
fn is_ascii_lowercase(&self) -> bool;
}
impl StrExtensions for str {
fn to_ascii_lowercase_cow(&self) -> Cow<'_, str> {
if self.is_ascii_lowercase() {
Cow::Borrowed(self)
} else {
Cow::Owned(self.to_ascii_lowercase())
}
}
fn is_ascii_lowercase(&self) -> bool {
let bytes = self.as_bytes();
bytes.is_ascii()
&& bytes
.iter()
.all(|b| !b.is_ascii_alphabetic() || b.is_ascii_lowercase())
}
}