#![no_std]
#[rustfmt::skip]
pub const LETTERS: [&[u8]; 26] =
[
& [0x1e, 0x9, 0x9, 0x9, 0x1e],
& [0x1f, 0x15, 0x15, 0x15, 0xa],
& [0xe, 0x11, 0x11, 0x11, 0x11],
& [0x1f, 0x11, 0x11, 0x11, 0xe],
& [0x1f, 0x15, 0x15, 0x15, 0x11],
& [0x1f, 0x5, 0x5, 0x5, 0x1],
& [0xe, 0x11, 0x15, 0x15, 0x9],
& [0x1f, 0x4, 0x4, 0x4, 0x1f],
& [0x1f],
& [0xc, 0x10, 0x10, 0x10, 0xf],
& [0x1f, 0x4, 0x4, 0xa, 0x11],
& [0x1f, 0x10, 0x10, 0x10, 0x8],
& [0x1f, 0x1, 0x6, 0x1, 0x1f],
& [0x1f, 0x1, 0xe, 0x10, 0x1f],
& [0xe, 0x11, 0x11, 0x11, 0xe],
& [0x1f, 0x5, 0x5, 0x5, 0x2],
& [0xe, 0x11, 0x15, 0x9, 0x16],
& [0x1f, 0x5, 0x5, 0xd, 0x16],
& [0x16, 0x15, 0x15, 0x15, 0xd],
& [0x1, 0x1, 0x1f, 0x1, 0x1],
& [0xf, 0x10, 0x10, 0x10, 0xf],
& [0x7, 0x8, 0x10, 0x8, 0x7],
& [0xf, 0x10, 0x1f, 0x10, 0xf],
& [0x11, 0xa, 0x4, 0xa, 0x11],
& [0x1, 0x2, 0x1c, 0x2, 0x1],
& [0x11, 0x19, 0x15, 0x13, 0x11],
];
#[rustfmt::skip]
pub const DIGITS: [[u8; 4]; 10] =
[
[0x1f, 0x11, 0x11, 0x1f],
[0x4, 0x2, 0x1, 0x1f],
[0x1d, 0x15, 0x15, 0x17],
[0x15, 0x15, 0x15, 0x1f],
[0x7, 0x4, 0x4, 0x1f],
[0x17, 0x15, 0x15, 0x1d],
[0x1f, 0x15, 0x15, 0x1d],
[0x3, 0x1, 0x1, 0x1f],
[0x1f, 0x15, 0x15, 0x1f],
[0x7, 0x5, 0x5, 0x1f],
];
#[rustfmt::skip]
pub const SYMBOLS: [&[u8]; 7] =
[
& [0x17],
& [0x10],
& [0x0],
& [0x4, 0x4, 0x4],
& [0xa, 0xa, 0xa],
& [0xa, 0x1f, 0xa, 0x1f, 0xa],
& [0x4, 0xe, 0x4],
];
pub const UNSUPPORTED: [u8; 5] = [0x11, 0x13, 0x15, 0x19, 0x11];
pub const SPACING: [u8; 1] = [0u8; 1];
pub fn col_defs(text: &str, final_sp: usize, out: &mut [&[u8]]) {
if text.len() == 0 {
return;
}
let mut ix = 0;
let mut chars = text.chars();
let mut c = chars.next();
loop {
out[ix] = col_def(c.unwrap());
ix += 1;
c = chars.next();
if c.is_none() {
break;
}
out[ix] = &SPACING;
ix += 1;
}
for _ in 0..final_sp {
out[ix] = &SPACING;
ix += 1;
}
}
pub fn col_def(mut c: char) -> &'static [u8] {
if c.is_ascii() {
if c.is_lowercase() {
c = c.to_ascii_uppercase()
}
}
let code = c as usize;
if code > 64 && code < 91 {
return &LETTERS[code - 65];
}
if code > 47 && code < 58 {
return &DIGITS[code - 48];
}
match c {
'!' => SYMBOLS[0],
'.' => SYMBOLS[1],
' ' => SYMBOLS[2],
'-' => SYMBOLS[3],
'=' => SYMBOLS[4],
'#' => SYMBOLS[5],
'+' => SYMBOLS[6],
_ => &UNSUPPORTED,
}
}
pub const fn buff_size(text: &str, extra: usize) -> usize {
let len = text.len();
len * 2 - 1 + extra
}
#[cfg(test)]
mod tests_of_units {
use super::{buff_size, col_defs, DIGITS, LETTERS, SPACING, SYMBOLS, UNSUPPORTED};
#[test]
fn letters_symbols() {
let alphabet1 = "abcdefghijklmnopqrstuvwxyz";
let alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let symbols = "!. -=#+";
test::<51>(alphabet1, &LETTERS);
test::<51>(alphabet2, &LETTERS);
test::<13>(symbols, &SYMBOLS);
fn test<const OUTSIZE: usize>(source: &str, sample: &[&[u8]]) {
let boundary = sample.len() - 1;
let mut out = [&[0u8; 0] as &[u8]; OUTSIZE];
col_defs(&source, 0, &mut out);
let mut out_iter = out.into_iter();
for i in 0..=boundary {
let letter = sample[i];
let mut next = out_iter.next().unwrap();
assert_eq!(letter, next);
if i < boundary {
next = out_iter.next().unwrap();
assert_eq!(SPACING, next);
}
}
assert_eq!(None, out_iter.next());
}
}
#[test]
fn numbers() {
let numbers = "0123456789";
let mut out = [&[0u8; 0] as &[u8]; 19];
col_defs(&numbers, 0, &mut out);
let mut out_iter = out.into_iter();
for i in 0..10usize {
let digit = DIGITS[i];
let mut next = out_iter.next().unwrap();
assert_eq!(digit, next);
if i < 9 {
next = out_iter.next().unwrap();
assert_eq!(SPACING, next);
}
}
assert_eq!(None, out_iter.next());
}
#[test]
fn unsupported() {
let mut out = [&[0u8; 0] as &[u8]; 1];
let text = ">";
col_defs(&text, 0, &mut out);
assert_eq!(UNSUPPORTED, out[0]);
}
#[test]
fn final_spacing() {
let mut out = [&[0u8; 0] as &[u8]; 6];
let text = " ";
col_defs(&text, 5, &mut out);
for i in 1..6 {
assert_eq!(SPACING, out[i]);
}
}
#[test]
fn buff_size_const() {
const TEXT: &str = "abc123";
let buffer = [&[0u8; 0]; buff_size(TEXT, 2)];
assert_eq!(13, buffer.len());
}
#[test]
fn buff_size_dynam() {
let text: &str = "abc123";
assert_eq!(11, buff_size(text, 0));
}
}