unicode_math_class/
lib.rs

1//! Determine the Unicode class of a mathematical character.
2//!
3//! # Example
4//! ```
5//! use unicode_math_class::{class, MathClass};
6//!
7//! assert_eq!(class('0'), Some(MathClass::Normal));
8//! assert_eq!(class('a'), Some(MathClass::Alphabetic));
9//! assert_eq!(class('𝔸'), Some(MathClass::Alphabetic));
10//! assert_eq!(class('+'), Some(MathClass::Vary));
11//! assert_eq!(class('×'), Some(MathClass::Binary));
12//! assert_eq!(class('('), Some(MathClass::Opening));
13//! assert_eq!(class(','), Some(MathClass::Punctuation));
14//! assert_eq!(class('|'), Some(MathClass::Fence));
15//! assert_eq!(class('😃'), None);
16//! ```
17//!
18//! For more details, see [Section 5.1 of Unicode Technical Report #25][report]
19//! and [this data file][data].
20//!
21//! [report]: https://www.unicode.org/reports/tr25/tr25-15.pdf
22//! [data]: https://www.unicode.org/Public/math/revision-15/MathClass-15.txt
23
24use MathClass::*;
25
26/// The revision of the used data file.
27///
28/// This crate does not specify a Unicode version because the math classes are
29/// not formally part of the Unicode character database.
30pub const REVISION: u8 = 15;
31
32/// Determine the class of a mathematical character.
33///
34/// Returns `None` if the character isn't part of any class.
35pub fn class(c: char) -> Option<MathClass> {
36    let i = CLASSES.binary_search_by_key(&c, |pair| pair.0).ok()?;
37    Some(CLASSES[i].1)
38}
39
40/// Classification of a mathematical character.
41#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
42pub enum MathClass {
43    Normal,
44    Alphabetic,
45    Binary,
46    Closing,
47    Diacritic,
48    Fence,
49    GlyphPart,
50    Large,
51    Opening,
52    Punctuation,
53    Relation,
54    Space,
55    Unary,
56    Vary,
57    Special,
58}
59
60include!("classes.rs");