use crate::util::unicode::PUNCTUATION;
#[derive(Debug, PartialEq, Eq)]
pub enum Kind {
Whitespace,
Punctuation,
Other,
}
pub fn classify(char: char) -> Kind {
if char.is_whitespace() {
Kind::Whitespace
}
else if char.is_ascii_punctuation()
|| (!char.is_ascii() && PUNCTUATION.binary_search(&char).is_ok())
{
Kind::Punctuation
}
else {
Kind::Other
}
}
pub fn classify_opt(char_opt: Option<char>) -> Kind {
char_opt.map_or(Kind::Whitespace, classify)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_classify() {
assert_eq!(
classify(' '),
Kind::Whitespace,
"should classify whitespace"
);
assert_eq!(
classify('.'),
Kind::Punctuation,
"should classify punctuation"
);
assert_eq!(classify('a'), Kind::Other, "should classify other");
}
#[test]
fn classifies_unicode_punctuation_with_sorted_table() {
assert!(PUNCTUATION.windows(2).all(|pair| pair[0] < pair[1]));
for &character in &PUNCTUATION {
assert_eq!(classify(character), Kind::Punctuation);
}
assert_eq!(classify('界'), Kind::Other);
assert_eq!(classify('\u{2003}'), Kind::Whitespace);
assert_eq!(classify('\0'), Kind::Other);
}
}