nom::count! [] [src]

macro_rules! count(
  ($i:expr, $submac:ident!( $($args:tt)* ), $typ:ty, $count: expr) => (
    {
      let mut begin = 0;
      let mut remaining = $i.len();
      let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count]};
      let mut cnt = 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, $f:expr, $typ:ty, $count: expr) => (
    count!($i, call!($f), $typ, $count);
  );
);

Applies the child parser a specified number of times

 named!(counter< [&[u8]; 2] >, count!( tag!( "abcd" ), &[u8], 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[..])));