use std::collections::BTreeSet;
pub fn script_of(c: char) -> &'static str {
let short = crate::text::unicode_properties::script_short_of(c);
match short {
"Zzzz" => "Other",
"Zyyy" => "Common",
"Zinh" => "Inherited",
_ => script_long_for_short_cached(short),
}
}
fn script_long_for_short_cached(short: &'static str) -> &'static str {
let table = crate::text::unicode_properties::SCRIPT_SHORT_TO_LONG;
let mut lo = 0usize;
let mut hi = table.len();
while lo < hi {
let mid = lo + (hi - lo) / 2;
let (s, l) = table[mid];
if short < s {
hi = mid;
} else if short > s {
lo = mid + 1;
} else {
return l;
}
}
short
}
pub fn policy_script_of(cp: u32) -> &'static str {
match char::from_u32(cp).map(script_of) {
Some("Other") | None => "Unknown",
Some(s) => s,
}
}
pub fn is_legitimate_mixture(scripts: &BTreeSet<&str>) -> bool {
if scripts.len() < 2 {
return false;
}
let japanese = ["Han", "Hiragana", "Katakana"];
if scripts.iter().all(|s| japanese.contains(s)) {
return true;
}
if scripts.contains("Hangul")
&& scripts
.iter()
.all(|s| matches!(*s, "Hangul" | "Han" | "Latin"))
{
return true;
}
false
}
pub fn resolved_script_set(text: &str) -> BTreeSet<&'static str> {
crate::text::unicode_properties::resolved_script_set(text)
}
pub fn is_mixed_script(text: &str) -> bool {
crate::text::unicode_properties::is_mixed_script(text)
}
pub fn is_script_bearing(script: &str) -> bool {
!matches!(script, "Common" | "Inherited" | "Unknown" | "Other")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn single_authoritative_source_spot_checks() {
assert_eq!(script_of('A'), "Latin");
assert_eq!(script_of('é'), "Latin");
assert_eq!(script_of('Α'), "Greek");
assert_eq!(script_of('Ἀ'), "Greek");
assert_eq!(script_of('А'), "Cyrillic");
assert_eq!(script_of('а'), "Cyrillic");
assert_eq!(script_of('あ'), "Hiragana");
assert_eq!(script_of('ア'), "Katakana");
assert_eq!(script_of('漢'), "Han");
assert_eq!(script_of('한'), "Hangul");
assert_eq!(script_of('0'), "Common");
assert_eq!(script_of('\u{0301}'), "Inherited");
}
#[test]
fn japanese_and_korean_mixtures_are_legitimate_legacy() {
let ja: BTreeSet<&str> = ["Han", "Hiragana", "Katakana"].into_iter().collect();
assert!(is_legitimate_mixture(&ja));
let ja2: BTreeSet<&str> = ["Hiragana", "Katakana"].into_iter().collect();
assert!(is_legitimate_mixture(&ja2));
let ko: BTreeSet<&str> = ["Hangul", "Han"].into_iter().collect();
assert!(is_legitimate_mixture(&ko));
}
#[test]
fn latin_cyrillic_mixture_is_spoof() {
let spoof: BTreeSet<&str> = ["Latin", "Cyrillic"].into_iter().collect();
assert!(!is_legitimate_mixture(&spoof));
}
#[test]
fn resolved_sets_are_authoritative() {
assert!(!is_mixed_script("Circle"));
assert!(is_mixed_script("hello привеt"));
assert!(!is_mixed_script("日本語テスト漢字"));
assert!(is_mixed_script("한한자test"));
}
}