Export some escaping utilities, refactor a little bit
This commit is contained in:
parent
a5856fb7fc
commit
28a1e3a3f0
|
@ -4,8 +4,22 @@ use array_vec::ArrayStr;
|
||||||
|
|
||||||
use super::Buffer;
|
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 {
|
pub const fn are_all_chars_identity<E: Escape + ~const EscapeMeta>(set: &[char]) -> bool {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < set.len() {
|
while i < set.len() {
|
||||||
|
@ -17,19 +31,21 @@ pub const fn are_all_chars_identity<E: Escape + ~const EscapeMeta>(set: &[char])
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BOOL_CHARS: &[char] = &ascii_chars(b"truefals");
|
||||||
|
const UINT_CHARS: &[char] = &ascii_chars(b"0123456789");
|
||||||
|
|
||||||
impl<E: Escape> CommonIdents<E> {
|
impl<E: Escape> CommonIdents<E> {
|
||||||
/// True if `true` and `false` will never need escaping.
|
/// True if `true` and `false` will never need escaping.
|
||||||
pub const BOOLS: bool =
|
pub const BOOLS: bool = are_all_chars_identity::<E>(BOOL_CHARS);
|
||||||
are_all_chars_identity::<E>(&['t', 'r', 'u', 'e', 'f', 'a', 'l', 's']);
|
|
||||||
/// True if unsigned integers will never need escaping.
|
/// True if unsigned integers will never need escaping.
|
||||||
pub const UINTS: bool =
|
pub const UINTS: bool = are_all_chars_identity::<E>(UINT_CHARS);
|
||||||
are_all_chars_identity::<E>(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
|
|
||||||
/// True if signed integers will never need escaping.
|
/// True if signed integers will never need escaping.
|
||||||
pub const INTS: bool = Self::UINTS && are_all_chars_identity::<E>(&['-']);
|
pub const INTS: bool = Self::UINTS && are_all_chars_identity::<E>(&['-']);
|
||||||
/// True if floats (using [`ryu`]'s formatting) will never need escaping.
|
/// 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::INTS && are_all_chars_identity::<E>(&['.', 'e']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constant metadata about an impl of [`Escape`].
|
||||||
#[const_trait]
|
#[const_trait]
|
||||||
pub trait EscapeMeta {
|
pub trait EscapeMeta {
|
||||||
/// Returns `true` if the escaping scheme will never map the given character, regardless of its
|
/// Returns `true` if the escaping scheme will never map the given character, regardless of its
|
||||||
|
|
Loading…
Reference in New Issue