use unicode_normalization::UnicodeNormalization;
#[must_use]
pub fn fold_text(value: &str) -> String {
value
.nfkc()
.collect::<String>()
.to_lowercase()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
#[must_use]
pub fn mixed_script(value: &str) -> bool {
let mut latin = false;
let mut other = false;
for ch in value.chars() {
if ch.is_ascii_alphabetic() {
latin = true;
} else if ch.is_alphabetic() {
other = true;
}
if latin && other {
return true;
}
}
false
}
#[must_use]
pub fn fold_words(value: &str) -> String {
fold_text(value)
.chars()
.map(|character| {
if character.is_alphanumeric() {
character
} else {
' '
}
})
.collect::<String>()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cyrillic_case_folds() {
assert_eq!(fold_text(" ДОБАВЬ Обработку "), "добавь обработку");
}
#[test]
fn nfkc_composes_compatibility_forms() {
assert_eq!(fold_text("fix"), "fix");
}
}