json-extractor 0.1.0

High-performance two-stage JSON fragment scanner with SIMD acceleration
Documentation
//! Character Classification
//!
//! Compile-time lookup table for fast character classification.
//! Replaces pattern matching with a single array lookup.

/// Character class bit flags
pub const CLASS_STRUCTURAL: u8 = 0b0001; // { } [ ] : , "

/// Compile-time character classification lookup table
///
/// Each byte maps to a bitfield indicating its character classes.
/// Multiple classes can be set (e.g., " is both STRUCTURAL and QUOTE).
pub const CHAR_CLASS: [u8; 256] = {
    let mut table = [0u8; 256];

    // Structural characters
    table[b'{' as usize] = CLASS_STRUCTURAL;
    table[b'}' as usize] = CLASS_STRUCTURAL;
    table[b'[' as usize] = CLASS_STRUCTURAL;
    table[b']' as usize] = CLASS_STRUCTURAL;
    table[b':' as usize] = CLASS_STRUCTURAL;
    table[b',' as usize] = CLASS_STRUCTURAL;
    table[b'"' as usize] = CLASS_STRUCTURAL;

    table
};

/// Check if a byte is a structural character
#[inline(always)]
pub fn is_structural(byte: u8) -> bool {
    CHAR_CLASS[usize::from(byte)] & CLASS_STRUCTURAL != 0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_structural_chars() {
        assert!(is_structural(b'{'));
        assert!(is_structural(b'}'));
        assert!(is_structural(b'['));
        assert!(is_structural(b']'));
        assert!(is_structural(b':'));
        assert!(is_structural(b','));
        assert!(is_structural(b'"'));

        assert!(!is_structural(b'a'));
        assert!(!is_structural(b'0'));
        assert!(!is_structural(b' '));
    }
}