|
| 1 | +use crate::digits::iter_digits; |
| 2 | +use crate::integer::{Base, IntegerImpl}; |
| 3 | +use crate::UnsignedInteger; |
| 4 | +use core::fmt; |
| 5 | + |
| 6 | +/// Formats an unsigned integer using outlined digits |
| 7 | +/// from the [Legacy Computing Supplement] block. |
| 8 | +/// |
| 9 | +/// You may need to install an extra font such as [Kreative Square] to display these digits. |
| 10 | +/// |
| 11 | +/// [Legacy Computing Supplement]: https://www.unicode.org/charts/PDF/U1CC00.pdf |
| 12 | +/// [Kreative Square]: http://www.kreativekorp.com/software/fonts/ksquare/ |
| 13 | +/// |
| 14 | +/// ``` |
| 15 | +/// use fmtastic::Outlined; |
| 16 | +/// |
| 17 | +/// assert_eq!("", Outlined(628_u32).to_string()); |
| 18 | +/// |
| 19 | +/// assert_eq!("", Outlined(0_u32).to_string()); |
| 20 | +/// assert_eq!("", Outlined(1_u32).to_string()); |
| 21 | +/// assert_eq!("", Outlined(2_u32).to_string()); |
| 22 | +/// assert_eq!("", Outlined(3_u32).to_string()); |
| 23 | +/// assert_eq!("", Outlined(4_u32).to_string()); |
| 24 | +/// assert_eq!("", Outlined(5_u32).to_string()); |
| 25 | +/// assert_eq!("", Outlined(6_u32).to_string()); |
| 26 | +/// assert_eq!("", Outlined(7_u32).to_string()); |
| 27 | +/// assert_eq!("", Outlined(8_u32).to_string()); |
| 28 | +/// assert_eq!("", Outlined(9_u32).to_string()); |
| 29 | +/// |
| 30 | +/// // Binary |
| 31 | +/// assert_eq!("", format!("{:b}", Outlined(0_u8))); |
| 32 | +/// assert_eq!("", format!("{:+b}", Outlined(0b101010_u8))); |
| 33 | +/// |
| 34 | +/// // Hexadecimal |
| 35 | +/// assert_eq!("", format!("{:X}", Outlined(0x1CCF0_u32))); |
| 36 | +/// ``` |
| 37 | +#[derive(Debug, Clone, Copy, Eq, PartialEq)] |
| 38 | +pub struct Outlined<T>(pub T); |
| 39 | + |
| 40 | +impl<T> From<T> for Outlined<T> |
| 41 | +where |
| 42 | + T: UnsignedInteger, |
| 43 | +{ |
| 44 | + fn from(value: T) -> Self { |
| 45 | + Outlined(value) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T> fmt::Binary for Outlined<T> |
| 50 | +where |
| 51 | + T: UnsignedInteger, |
| 52 | +{ |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 54 | + fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseTwo>(self.0.into_impl(), f) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<T> fmt::Display for Outlined<T> |
| 59 | +where |
| 60 | + T: UnsignedInteger, |
| 61 | +{ |
| 62 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 63 | + fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseTen>(self.0.into_impl(), f) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<T> fmt::UpperHex for Outlined<T> |
| 68 | +where |
| 69 | + T: UnsignedInteger, |
| 70 | +{ |
| 71 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 72 | + fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseSixteen>(self.0.into_impl(), f) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn fmt_outlined<T: IntegerImpl, B: Base<T>>(n: T, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 | + iter_digits::<_, B>(n).try_for_each(|digit| write!(f, "{}", DIGITS[digit])) |
| 78 | +} |
| 79 | + |
| 80 | +const DIGITS: [&str; 16] = [ |
| 81 | + // Outlined digits 0-9 |
| 82 | + "\u{1CCF0}", |
| 83 | + "\u{1CCF1}", |
| 84 | + "\u{1CCF2}", |
| 85 | + "\u{1CCF3}", |
| 86 | + "\u{1CCF4}", |
| 87 | + "\u{1CCF5}", |
| 88 | + "\u{1CCF6}", |
| 89 | + "\u{1CCF7}", |
| 90 | + "\u{1CCF8}", |
| 91 | + "\u{1CCF9}", |
| 92 | + // Outlined uppercase Latin alphabet A-F |
| 93 | + "\u{1CCD6}", |
| 94 | + "\u{1CCD7}", |
| 95 | + "\u{1CCD8}", |
| 96 | + "\u{1CCD9}", |
| 97 | + "\u{1CCDA}", |
| 98 | + "\u{1CCDB}", |
| 99 | +]; |
0 commit comments