nom::count_fixed!
[−]
[src]
macro_rules! count_fixed( ($i:expr, $typ:ty, $submac:ident!( $($args:tt)* ), $count: expr) => ( { let mut begin = 0; let mut remaining = $i.len(); let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]}; let mut cnt: usize = 0; let mut err = false; loop { match $submac!(&$i[begin..], $($args)*) { $crate::IResult::Done(i,o) => { res[cnt] = o; begin += remaining - i.len(); remaining = i.len(); cnt = cnt + 1; if cnt == $count { break } }, $crate::IResult::Error(_) => { err = true; break; }, $crate::IResult::Incomplete(_) => { break; } } } if err { $crate::IResult::Error($crate::Err::Position($crate::ErrorCode::Count as u32,$i)) } else if cnt == $count { $crate::IResult::Done(&$i[begin..], res) } else { $crate::IResult::Incomplete($crate::Needed::Unknown) } } ); ($i:expr, $typ: ty, $f:ident, $count: expr) => ( count_fixed!($i, $typ, call!($f), $count); ); ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( count_fixed!($i, &[u8], $submac!($($args)*), $count); ); ($i:expr, $f:ident, $count: expr) => ( count_fixed!($i, &[u8], call!($f), $count); ); );
count_fixed!(I -> IResult<I,O>, nb) => I -> IResult<I, [O; nb]>
Applies the child parser a fixed number of times and returns a fixed size array
named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], tag!( "abcd" ), 2 ) ); // can omit the type specifier if returning slices // named!(counter< [&[u8]; 2] >, count_fixed!( tag!( "abcd" ), 2 ) ); let a = b"abcdabcdabcdef"; let b = b"abcdefgh"; let res = [&b"abcd"[..], &b"abcd"[..]]; assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); assert_eq!(counter(&b[..]), Error(Position(ErrorCode::Count as u32, &b[..])));