nom::count!
[−]
[src]
macro_rules! count( ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( { let mut begin = 0; let mut remaining = $i.len(); let mut res = Vec::new(); let mut cnt: usize = 0; let mut err = false; loop { match $submac!(&$i[begin..], $($args)*) { $crate::IResult::Done(i,o) => { res.push(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, $f:expr, $count: expr) => ( count!($i, call!($f), $count); ); );
count!(I -> IResult<I,O>, nb) => I -> IResult<I, Vec<O>>
Applies the child parser a specified number of times
named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); let a = b"abcdabcdabcdef"; let b = b"abcdefgh"; let res = vec![&b"abcd"[..], &b"abcd"[..]]; assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); assert_eq!(counter(&b[..]), Error(Position(ErrorCode::Count as u32, &b[..])));