Skip to main content

bitmask

Function bitmask 

Source
pub fn bitmask<U: UnsignedInteger>(bytes: &'static [u8; 2]) -> BitmaskParser<U>
Expand description

Leaf parser for strings made of two different bytes, packed into a bitmask.

Bytes in the input matching the first provided byte are mapped to 0 bits, and bytes matching the second provided byte are mapped to 1 bits.

The first input byte is put in the least significant bit. Returns the bitmask and its length, or an error if the string is empty or too long for the integer type.

ยงExamples

let parser = parser::bitmask::<u8>(b".#");
assert_eq!(
    parser.parse(b"#.###. abc"),
    Ok(((0b011101, 6), &b" abc"[..]))
);
assert_eq!(
    parser.parse(b".##...##"),
    Ok(((0b11000110, 8), &b""[..]))
);
assert!(parser.parse(b"abc").is_err());
assert!(parser.parse(b"#########").is_err());

let parser = parser::bitmask::<u64>(b".#");
assert_eq!(
    parser.parse(b".##...####.....######.......######## 123"),
    Ok(((0b111111110000000111111000001111000110, 36), &b" 123"[..]))
);