pub const YOURS_CAP: usize = 400;
pub const BOT_CAP: usize = 1_200;
pub const LINE_CAP: usize = 150;
pub fn estimate(text: &str) -> usize {
let (cjk, other) =
text.chars().fold((0, 0), |(cjk, other), ch| if is_cjk(ch) { (cjk + 1, other) } else { (cjk, other + 1) });
cjk + usize::div_ceil(other, 4)
}
fn is_cjk(ch: char) -> bool {
matches!(u32::from(ch),
0x1100..=0x11FF | 0x2E80..=0x2FDF | 0x3000..=0x31FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xAC00..=0xD7AF | 0xF900..=0xFAFF | 0xFF00..=0xFFEF | 0x2_0000..=0x3_FFFF) }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_cjk_per_character_and_latin_per_four() {
assert_eq!(estimate(""), 0);
assert_eq!(estimate("abcd"), 1);
assert_eq!(estimate("abcde"), 2);
assert_eq!(estimate("台北時間"), 4);
assert_eq!(estimate("住在台北 ok"), 5);
assert_eq!(estimate("한국어"), 3);
}
}