oxedyne/fe2o3/fe2o3_text/src/unicode/math.rs
1.6 KiB, 1 run
created by r1870400018:65251, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download ยท who wrote it ยท its history
| 1 | //! The mathematical character classes of Unicode Technical Report #25, revision 15, which decide |
| 2 | //! how a character spaces and stretches in a formula: `+` is Vary (binary between operands, unary |
| 3 | //! before one), `(` is Opening, `โ` is Large. |
| 4 | //! |
| 5 | //! The classes are not part of the UCD proper, so the table in [`tables::math`](super::tables::math) |
| 6 | //! comes from `MathClass-15.txt`, fetched by the same generator as the rest. |
| 7 | //! |
| 8 | //! ``` |
| 9 | //! use oxedyne_fe2o3_text::unicode::math::{class, MathClass}; |
| 10 | //! |
| 11 | //! assert_eq!(class('+'), Some(MathClass::Vary)); |
| 12 | //! assert_eq!(class('('), Some(MathClass::Opening)); |
| 13 | //! assert_eq!(class('๐'), None); |
| 14 | //! ``` |
| 15 | |
| 16 | use crate::unicode::lookup; |
| 17 | use crate::unicode::tables::math::{ |
| 18 | MATH_STARTS, |
| 19 | MATH_VALS, |
| 20 | }; |
| 21 | |
| 22 | #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] |
| 23 | pub enum MathClass { |
| 24 | Normal, |
| 25 | Alphabetic, |
| 26 | Binary, |
| 27 | Closing, |
| 28 | Diacritic, |
| 29 | Fence, |
| 30 | GlyphPart, |
| 31 | Large, |
| 32 | Opening, |
| 33 | Punctuation, |
| 34 | Relation, |
| 35 | Space, |
| 36 | Unary, |
| 37 | Vary, |
| 38 | Special, |
| 39 | } |
| 40 | |
| 41 | // In the order of the generator's class letters, N A B C D F G L O P R S U V X. |
| 42 | const CLASSES: [MathClass; 15] = [ |
| 43 | MathClass::Normal, |
| 44 | MathClass::Alphabetic, |
| 45 | MathClass::Binary, |
| 46 | MathClass::Closing, |
| 47 | MathClass::Diacritic, |
| 48 | MathClass::Fence, |
| 49 | MathClass::GlyphPart, |
| 50 | MathClass::Large, |
| 51 | MathClass::Opening, |
| 52 | MathClass::Punctuation, |
| 53 | MathClass::Relation, |
| 54 | MathClass::Space, |
| 55 | MathClass::Unary, |
| 56 | MathClass::Vary, |
| 57 | MathClass::Special, |
| 58 | ]; |
| 59 | |
| 60 | /// The class of `c`, or `None` for a character the report does not classify. |
| 61 | pub fn class(c: char) -> Option<MathClass> { |
| 62 | match lookup::flags(&MATH_STARTS, &MATH_VALS, c) { |
| 63 | 0 => None, |
| 64 | n => CLASSES.get(n as usize - 1).copied(), |
| 65 | } |
| 66 | } |