static WC: [(u32, u8); 1002] = include!("../wcwidth_table.in");
static LEAD: [(u32, u8); 961] = include!("../displaywidth_table.in");
fn lookup(table: &[(u32, u8)], c: char) -> usize {
let r = u32::from(c);
let after = table.partition_point(|&(first, _)| first <= r);
usize::from(table[after - 1].1)
}
pub(super) fn wc_char(c: char) -> usize {
if c.is_ascii_graphic() || c == ' ' {
return 1;
}
lookup(&WC, c)
}
pub(super) fn wc_width(cluster: &str) -> usize {
cluster.chars().map(wc_char).sum()
}
pub(super) fn cluster_width(cluster: &str) -> usize {
let mut chars = cluster.chars();
let Some(first) = chars.next() else {
return 0;
};
if first.is_ascii_control() {
return 0;
}
if first.is_ascii() && cluster.len() == 1 {
return 1;
}
if chars.next() == Some('\u{FE0F}') {
return 2;
}
lookup(&LEAD, first)
}