Compare commits

...

2 Commits

1 changed files with 33 additions and 7 deletions

View File

@ -4,8 +4,22 @@ use array_vec::ArrayStr;
use super::Buffer;
pub(crate) struct CommonIdents<E: Escape>(PhantomData<E>);
/// Holds computed constants for common escaping scheme identity checks.
pub struct CommonIdents<E: Escape>(PhantomData<E>);
/// Converts an array of ASCII bytes to an array of characters.
pub const fn ascii_chars<const N: usize>(s: &[u8; N]) -> [char; N] {
assert!(s.is_ascii());
let mut chars = ['\0'; N];
let mut i = 0;
while i < s.len() {
chars[i] = s[i] as char;
i += 1;
}
chars
}
/// Returns `true` if, for the given escaping scheme `E`, every character in `set` requires no escaping.
pub const fn are_all_chars_identity<E: Escape + ~const EscapeMeta>(set: &[char]) -> bool {
let mut i = 0;
while i < set.len() {
@ -17,19 +31,31 @@ pub const fn are_all_chars_identity<E: Escape + ~const EscapeMeta>(set: &[char])
true
}
const ALPHA_LOWERCASE_CHARS: &[char] = &ascii_chars(b"abcdefghijklmnopqrstuvwxyz");
const ALPHA_UPPERCASE_CHARS: &[char] = &ascii_chars(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
const DIGIT_CHARS: &[char] = &ascii_chars(b"0123456789");
const BOOL_CHARS: &[char] = &ascii_chars(b"truefals");
impl<E: Escape> CommonIdents<E> {
/// True if lowercase ASCII alphabetic characters will never need escaping.
pub const ALPHA_LOWERCASE: bool = are_all_chars_identity::<E>(ALPHA_LOWERCASE_CHARS);
/// True if uppercase ASCII alphabetic characters will never need escaping.
pub const ALPHA_UPPERCASE: bool = are_all_chars_identity::<E>(ALPHA_UPPERCASE_CHARS);
/// True if base10 digit characters will never need escaping.
pub const DIGITS: bool = are_all_chars_identity::<E>(DIGIT_CHARS);
/// True if `true` and `false` will never need escaping.
pub const BOOLS: bool =
are_all_chars_identity::<E>(&['t', 'r', 'u', 'e', 'f', 'a', 'l', 's']);
pub const BOOLS: bool = are_all_chars_identity::<E>(BOOL_CHARS);
/// True if unsigned integers will never need escaping.
pub const UINTS: bool =
are_all_chars_identity::<E>(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
pub const UINTS: bool = Self::DIGITS;
/// True if signed integers will never need escaping.
pub const INTS: bool = Self::UINTS && are_all_chars_identity::<E>(&['-']);
pub const INTS: bool = Self::DIGITS && are_all_chars_identity::<E>(&['-']);
/// True if floats (using [`ryu`]'s formatting) will never need escaping.
pub const FLOATS: bool = Self::INTS && are_all_chars_identity::<E>(&['.', 'e']);
pub const FLOATS: bool =
Self::DIGITS && are_all_chars_identity::<E>(&['-', '.', 'e']);
}
/// Constant metadata about an impl of [`Escape`].
#[const_trait]
pub trait EscapeMeta {
/// Returns `true` if the escaping scheme will never map the given character, regardless of its