|
| 1 | +//! ASCII helpers. |
| 2 | +
|
| 3 | +use crate::bit::BitIterator; |
| 4 | +use std::fmt::{Debug, Display, Formatter}; |
| 5 | + |
| 6 | +/// A set of ASCII characters. |
| 7 | +/// |
| 8 | +/// # Examples |
| 9 | +/// ``` |
| 10 | +/// # use utils::ascii::AsciiSet; |
| 11 | +/// let set1 = AsciiSet::new((1 << b'A') | (1 << b'B') | (1 << b'C')); |
| 12 | +/// assert_eq!(set1.len(), 3); |
| 13 | +/// assert_eq!(set1.to_string(), "'A', 'B', 'C'"); |
| 14 | +/// assert_eq!(format!("{set1:?}"), "{'A', 'B', 'C'}"); |
| 15 | +/// |
| 16 | +/// let mut array = [false; 128]; |
| 17 | +/// array[b'A' as usize] = true; |
| 18 | +/// array[b'B' as usize] = true; |
| 19 | +/// array[b'C' as usize] = true; |
| 20 | +/// assert_eq!(AsciiSet::from(array), set1); |
| 21 | +/// |
| 22 | +/// assert_eq!(AsciiSet::from(|b| (b'A'..=b'C').contains(&b)), set1); |
| 23 | +/// ``` |
| 24 | +#[derive(Copy, Clone, Eq, PartialEq, Default)] |
| 25 | +#[repr(transparent)] |
| 26 | +#[must_use] |
| 27 | +pub struct AsciiSet { |
| 28 | + set: u128, |
| 29 | +} |
| 30 | + |
| 31 | +impl AsciiSet { |
| 32 | + /// Creates a new `AsciiSet` from the specified bitset. |
| 33 | + pub const fn new(set: u128) -> Self { |
| 34 | + Self { set } |
| 35 | + } |
| 36 | + |
| 37 | + #[must_use] |
| 38 | + pub const fn is_empty(&self) -> bool { |
| 39 | + self.set == 0 |
| 40 | + } |
| 41 | + |
| 42 | + #[must_use] |
| 43 | + pub const fn len(&self) -> usize { |
| 44 | + self.set.count_ones() as usize |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Display for AsciiSet { |
| 49 | + #[expect(clippy::cast_possible_truncation)] |
| 50 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 51 | + if self.set == 0 { |
| 52 | + return write!(f, "(empty)"); |
| 53 | + } |
| 54 | + |
| 55 | + for (i, (c, _)) in BitIterator::ones(self.set).enumerate() { |
| 56 | + let c = c as u8 as char; |
| 57 | + if i == 0 { |
| 58 | + write!(f, "{c:?}")?; |
| 59 | + } else { |
| 60 | + write!(f, ", {c:?}")?; |
| 61 | + } |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Debug for AsciiSet { |
| 68 | + #[expect(clippy::cast_possible_truncation)] |
| 69 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 70 | + f.debug_set() |
| 71 | + .entries(BitIterator::ones(self.set).map(|(c, _)| c as u8 as char)) |
| 72 | + .finish() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl From<u128> for AsciiSet { |
| 77 | + fn from(set: u128) -> Self { |
| 78 | + Self { set } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl From<[bool; 128]> for AsciiSet { |
| 83 | + fn from(value: [bool; 128]) -> Self { |
| 84 | + Self { |
| 85 | + set: value |
| 86 | + .iter() |
| 87 | + .enumerate() |
| 88 | + .fold(0, |s, (i, &b)| s | u128::from(b) << i), |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<F: Fn(u8) -> bool> From<F> for AsciiSet { |
| 94 | + fn from(value: F) -> Self { |
| 95 | + Self { |
| 96 | + set: (0u8..=127).fold(0, |s, i| s | u128::from(value(i)) << i), |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments