nom::bits

Function bytes

Source
pub fn bytes<I, O, E1, E2, P>(
    parser: P,
) -> impl FnMut((I, usize)) -> IResult<(I, usize), O, E2>
where E1: ParseError<I> + ErrorConvert<E2>, E2: ParseError<(I, usize)>, I: Slice<RangeFrom<usize>> + Clone, P: Parser<I, O, E1>,
Expand description

Counterpart to bits, bytes transforms its bit stream input into a byte slice for the underlying parser, allowing byte-slice parsers to work on bit streams.

A partial byte remaining in the input will be ignored and the given parser will start parsing at the next full byte.

use nom::bits::{bits, bytes, streaming::take};
use nom::combinator::rest;
use nom::error::Error;
use nom::sequence::tuple;
use nom::IResult;

fn parse(input: &[u8]) -> IResult<&[u8], (u8, u8, &[u8])> {
  bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((
    take(4usize),
    take(8usize),
    bytes::<_, _, Error<&[u8]>, _, _>(rest)
  )))(input)
}

let input = &[0x12, 0x34, 0xff, 0xff];

assert_eq!(parse( input ), Ok(( &[][..], (0x01, 0x23, &[0xff, 0xff][..]) )));