use super::confusables;
fn has_non_ascii(input: &str) -> bool {
!input.is_ascii()
}
#[must_use]
pub fn replace_confusables(input: &str) -> (String, bool) {
if !has_non_ascii(input) {
return (input.to_string(), false);
}
if confusables::contains_confusable(input) {
(confusables::replace_confusable(input), true)
} else {
(input.to_string(), false)
}
}
#[must_use]
#[allow(dead_code)] pub fn contains_confusables(input: &str) -> bool {
has_non_ascii(input) && confusables::contains_confusable(input)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cyrillic_a() {
let input = "\u{0430}uthentication"; let (normalized, changed) = replace_confusables(input);
assert!(changed);
assert_eq!(normalized, "authentication");
}
#[test]
fn test_no_confusables() {
let input = "authentication";
let (normalized, changed) = replace_confusables(input);
assert!(!changed);
assert_eq!(normalized, input);
}
#[test]
fn test_contains_confusables() {
let input_with = "\u{0430}uthentication"; let input_without = "authentication";
assert!(contains_confusables(input_with));
assert!(!contains_confusables(input_without));
}
#[test]
fn test_mixed_scripts() {
let input = "h\u{0435}llo"; let (normalized, changed) = replace_confusables(input);
assert!(changed);
assert_eq!(normalized, "hello");
}
}