use crate::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ByteSet(pub [bool; 256]);
impl ByteSet {
pub const fn new() -> Self {
Self([false; 256])
}
pub const fn contains(self, b: u8) -> bool {
self.0[b as usize]
}
pub const fn add(mut self, b: u8) -> Self {
self.0[b as usize] = true;
self
}
pub const fn add_many(mut self, bs: &[u8]) -> Self {
let mut i = 0;
while i < bs.len() {
self = self.add(bs[i]);
i += 1;
}
self
}
pub const fn remove(mut self, b: u8) -> Self {
self.0[b as usize] = false;
self
}
pub const fn remove_many(mut self, bs: &[u8]) -> Self {
let mut i = 0;
while i < bs.len() {
self = self.remove(bs[i]);
i += 1;
}
self
}
pub const fn merge(mut self, other: Self) -> Self {
let mut i = 0;
while i < 256 {
self.0[i] |= other.0[i];
i += 1;
}
self
}
pub const fn invert(mut self) -> Self {
let mut i = 0;
while i < 256 {
self.0[i] = !self.0[i];
i += 1;
}
self
}
pub const fn add_non_ascii(mut self) -> Self {
let mut i = 128;
while i < 256 {
self.0[i] = true;
i += 1;
}
self
}
pub const fn remove_non_ascii(mut self) -> Self {
let mut i = 128;
while i < 256 {
self.0[i] = false;
i += 1;
}
self
}
}
impl Default for ByteSet {
fn default() -> Self {
Self::new()
}
}
pub const FORBIDDEN_HOST_INPUT : ByteSet = ByteSet::new().add_many(b"\x00\t\n\r #/:<>?@[\\]^|");
pub const FORBIDDEN_DOMAIN_SEGMENT_INPUT : ByteSet = FORBIDDEN_DOMAIN_SEGMENTS_INPUT.add(b'.');
pub const FORBIDDEN_DOMAIN_SEGMENTS_INPUT: ByteSet = FORBIDDEN_HOST_INPUT.merge(C0.into_inner().remove_non_ascii()).add(b'%');
pub const FORBIDDEN_DOMAIN_HOST_INPUT : ByteSet = FORBIDDEN_DOMAIN_SEGMENTS_INPUT;
pub const FORBIDDEN_HOST_LITERAL : ByteSet = FORBIDDEN_HOST_INPUT .add_non_ascii().add_many(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
pub const FORBIDDEN_DOMAIN_SEGMENT_LITERAL : ByteSet = FORBIDDEN_DOMAIN_SEGMENT_INPUT .add_non_ascii().add_many(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
pub const FORBIDDEN_DOMAIN_SEGMENTS_LITERAL: ByteSet = FORBIDDEN_DOMAIN_SEGMENTS_INPUT.add_non_ascii().add_many(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
pub const FORBIDDEN_DOMAIN_HOST_LITERAL : ByteSet = FORBIDDEN_DOMAIN_HOST_INPUT .add_non_ascii().add_many(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
pub const FORBIDDEN_IPV4_DEC_INPUT: ByteSet = ByteSet::new().add_many(b"0123456789." ).invert();
pub const FORBIDDEN_IPV4_INPUT : ByteSet = ByteSet::new().add_many(b"0123456789.abcdefABCDEFxX").invert();