[][src]Macro nom::length_value

macro_rules! length_value {
    ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { ... };
    ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { ... };
    ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { ... };
    ($i:expr, $f:expr, $g:expr) => { ... };
}

length_value!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, O>

Gets a number from the first parser, takes a subslice of the input of that size, then applies the second parser on that subslice. If the second parser returns Incomplete, length_value will return an error.

use nom::number::complete::be_u8;
use nom::character::complete::alpha0;
use nom::bytes::complete::tag;
named!(parser, length_value!(be_u8, alpha0));

assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..])));
assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::new(3))));