const SUPERSCRIPTS: [(char, char); 59] = [
('0', '\u{2070}'), ('1', '\u{00b9}'), ('2', '\u{00b2}'), ('3', '\u{00b3}'), ('4', '\u{2074}'),
('5', '\u{2075}'), ('6', '\u{2076}'), ('7', '\u{2077}'), ('8', '\u{2078}'), ('9', '\u{2079}'),
('+', '\u{207a}'), ('-', '\u{207b}'), ('=', '\u{207c}'), ('(', '\u{207d}'), (')', '\u{207e}'),
('a', '\u{1d43}'), ('b', '\u{1d47}'), ('c', '\u{1d9c}'), ('d', '\u{1d48}'), ('e', '\u{1d49}'),
('f', '\u{1da0}'), ('g', '\u{1d4d}'), ('h', '\u{02b0}'), ('i', '\u{2071}'), ('j', '\u{02b2}'),
('k', '\u{1d4f}'), ('l', '\u{02e1}'), ('m', '\u{1d50}'), ('n', '\u{207f}'), ('o', '\u{1d52}'),
('p', '\u{1d56}'), ('r', '\u{02b3}'), ('s', '\u{02e2}'), ('t', '\u{1d57}'), ('u', '\u{1d58}'),
('v', '\u{1d5b}'), ('w', '\u{02b7}'), ('x', '\u{02e3}'), ('y', '\u{02b8}'), ('z', '\u{1dbb}'),
('A', '\u{1d2c}'), ('B', '\u{1d2e}'), ('D', '\u{1d30}'), ('E', '\u{1d31}'), ('G', '\u{1d33}'),
('H', '\u{1d34}'), ('I', '\u{1d35}'), ('J', '\u{1d36}'), ('K', '\u{1d37}'), ('L', '\u{1d38}'),
('M', '\u{1d39}'), ('N', '\u{1d3a}'), ('O', '\u{1d3c}'), ('P', '\u{1d3e}'), ('R', '\u{1d3f}'),
('T', '\u{1d40}'), ('U', '\u{1d41}'), ('V', '\u{2c7d}'), ('W', '\u{1d42}'),
];
const SUBSCRIPTS: [(char, char); 32] = [
('0', '\u{2080}'), ('1', '\u{2081}'), ('2', '\u{2082}'), ('3', '\u{2083}'), ('4', '\u{2084}'),
('5', '\u{2085}'), ('6', '\u{2086}'), ('7', '\u{2087}'), ('8', '\u{2088}'), ('9', '\u{2089}'),
('+', '\u{208a}'), ('-', '\u{208b}'), ('=', '\u{208c}'), ('(', '\u{208d}'), (')', '\u{208e}'),
('a', '\u{2090}'), ('e', '\u{2091}'), ('h', '\u{2095}'), ('i', '\u{1d62}'), ('j', '\u{2c7c}'),
('k', '\u{2096}'), ('l', '\u{2097}'), ('m', '\u{2098}'), ('n', '\u{2099}'), ('o', '\u{2092}'),
('p', '\u{209a}'), ('r', '\u{1d63}'), ('s', '\u{209b}'), ('t', '\u{209c}'), ('u', '\u{1d64}'),
('v', '\u{1d65}'), ('x', '\u{2093}'),
];
pub fn superscript_form(c: char) -> Option<char> {
form_in(&SUPERSCRIPTS, c)
}
pub fn subscript_form(c: char) -> Option<char> {
form_in(&SUBSCRIPTS, c)
}
fn form_in(table: &[(char, char)], c: char) -> Option<char> {
table.iter().find(|(from, _)| *from == c).map(|(_, form)| *form)
}
#[cfg(test)]
mod tests {
use super::*;
fn printed(heading: &str) -> Vec<(char, char)> {
let source = include_str!("superscript.rs");
let section = source.split(heading).nth(1).expect("the heading is in the documentation");
let mut pairs = Vec::new();
for line in section.lines().skip_while(|line| !line.starts_with("//! |")) {
let Some(row) = line.strip_prefix("//! |") else { break };
let cells: Vec<&str> = row.split('`').collect();
if cells.len() < 4 {
continue;
}
pairs.extend(cells[1].chars().zip(cells[3].chars()));
}
pairs
}
#[test]
fn the_printed_tables_are_the_tables() {
assert_eq!(printed("# The superscript forms"), SUPERSCRIPTS.to_vec());
assert_eq!(printed("# The subscript forms"), SUBSCRIPTS.to_vec());
}
#[test]
fn every_character_has_at_most_one_form_and_no_form_is_shared() {
for table in [&SUPERSCRIPTS[..], &SUBSCRIPTS[..]] {
let mut from: Vec<char> = table.iter().map(|(c, _)| *c).collect();
let mut to: Vec<char> = table.iter().map(|(_, f)| *f).collect();
let count = table.len();
from.sort_unstable();
from.dedup();
to.sort_unstable();
to.dedup();
assert_eq!((from.len(), to.len()), (count, count));
}
}
#[test]
fn the_digits_are_the_numbering_formats_digits() {
let raised: String = ('0'..='9').filter_map(superscript_form).collect();
assert_eq!(raised, "⁰¹²³⁴⁵⁶⁷⁸⁹");
let lowered: String = ('0'..='9').filter_map(subscript_form).collect();
assert_eq!(lowered, "₀₁₂₃₄₅₆₇₈₉");
}
#[test]
fn a_character_with_no_form_answers_none() {
for c in ['q', 'S', 'Q', ' ', '.', 'é', '\u{b9}'] {
assert_eq!(superscript_form(c), None, "{c:?}");
}
for c in ['b', 'A', 'y', ' '] {
assert_eq!(subscript_form(c), None, "{c:?}");
}
}
}