#[cfg(test)]
mod tests;
use crate::combinator::trace;
use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParserError};
use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::stream::{AsBytes, Stream, StreamIsPartial, ToUsize};
use crate::{unpeek, IResult, PResult, Parser};
const BYTE: usize = u8::BITS as usize;
pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl Parser<I, O, E2>
where
E1: ParserError<(I, usize)> + ErrorConvert<E2>,
E2: ParserError<I>,
I: Stream + Clone,
P: Parser<(I, usize), O, E1>,
{
trace(
"bits",
unpeek(move |input: I| {
match parser.parse_peek((input, 0)) {
Ok(((rest, offset), result)) => {
let remaining_bytes_index =
offset / BYTE + if offset % BYTE == 0 { 0 } else { 1 };
let (input, _) = rest.peek_slice(remaining_bytes_index);
Ok((input, result))
}
Err(ErrMode::Incomplete(n)) => {
Err(ErrMode::Incomplete(n.map(|u| u.get() / BYTE + 1)))
}
Err(e) => Err(e.convert()),
}
}),
)
}
pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl Parser<(I, usize), O, E2>
where
E1: ParserError<I> + ErrorConvert<E2>,
E2: ParserError<(I, usize)>,
I: Stream<Token = u8> + Clone,
P: Parser<I, O, E1>,
{
trace(
"bytes",
unpeek(move |(input, offset): (I, usize)| {
let (inner, _) = if offset % BYTE != 0 {
input.peek_slice(1 + offset / BYTE)
} else {
input.peek_slice(offset / BYTE)
};
let i = (input, offset);
match parser.parse_peek(inner) {
Ok((rest, res)) => Ok(((rest, 0), res)),
Err(ErrMode::Incomplete(Needed::Unknown)) => {
Err(ErrMode::Incomplete(Needed::Unknown))
}
Err(ErrMode::Incomplete(Needed::Size(sz))) => {
Err(match sz.get().checked_mul(BYTE) {
Some(v) => ErrMode::Incomplete(Needed::new(v)),
None => ErrMode::Cut(E2::assert(
&i,
"overflow in turning needed bytes into needed bits",
)),
})
}
Err(e) => Err(e.convert()),
}
}),
)
}
#[inline(always)]
pub fn take<I, O, C, E: ParserError<(I, usize)>>(count: C) -> impl Parser<(I, usize), O, E>
where
I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
let count = count.to_usize();
trace(
"take",
unpeek(move |input: (I, usize)| {
if <I as StreamIsPartial>::is_partial_supported() {
take_::<_, _, _, true>(input, count)
} else {
take_::<_, _, _, false>(input, count)
}
}),
)
}
fn take_<I, O, E: ParserError<(I, usize)>, const PARTIAL: bool>(
(input, bit_offset): (I, usize),
count: usize,
) -> IResult<(I, usize), O, E>
where
I: StreamIsPartial,
I: Stream<Token = u8> + AsBytes + Clone,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
if count == 0 {
Ok(((input, bit_offset), 0u8.into()))
} else {
if input.eof_offset() * BYTE < count + bit_offset {
if PARTIAL && input.is_partial() {
Err(ErrMode::Incomplete(Needed::new(count)))
} else {
Err(ErrMode::from_error_kind(
&(input, bit_offset),
ErrorKind::Eof,
))
}
} else {
let cnt = (count + bit_offset).div(BYTE);
let mut acc: O = 0_u8.into();
let mut offset: usize = bit_offset;
let mut remaining: usize = count;
let mut end_offset: usize = 0;
for byte in input.as_bytes().iter().copied().take(cnt + 1) {
if remaining == 0 {
break;
}
let val: O = if offset == 0 {
byte.into()
} else {
(byte << offset >> offset).into()
};
if remaining < BYTE - offset {
acc += val >> (BYTE - offset - remaining);
end_offset = remaining + offset;
break;
} else {
acc += val << (remaining - (BYTE - offset));
remaining -= BYTE - offset;
offset = 0;
}
}
let (input, _) = input.peek_slice(cnt);
Ok(((input, end_offset), acc))
}
}
}
#[inline(always)]
#[doc(alias = "literal")]
#[doc(alias = "just")]
#[doc(alias = "tag")]
pub fn pattern<I, O, C, E: ParserError<(I, usize)>>(
pattern: O,
count: C,
) -> impl Parser<(I, usize), O, E>
where
I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
let count = count.to_usize();
trace("pattern", move |input: &mut (I, usize)| {
let start = input.checkpoint();
take(count).parse_next(input).and_then(|o| {
if pattern == o {
Ok(o)
} else {
input.reset(start);
Err(ErrMode::Backtrack(E::from_error_kind(
input,
ErrorKind::Tag,
)))
}
})
})
}
#[deprecated(since = "0.5.38", note = "Replaced with `pattern`")]
pub fn tag<I, O, C, E: ParserError<(I, usize)>>(p: O, count: C) -> impl Parser<(I, usize), O, E>
where
I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
pattern(p, count)
}
#[doc(alias = "any")]
pub fn bool<I, E: ParserError<(I, usize)>>(input: &mut (I, usize)) -> PResult<bool, E>
where
I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone,
{
trace("bool", |input: &mut (I, usize)| {
let bit: u32 = take(1usize).parse_next(input)?;
Ok(bit != 0)
})
.parse_next(input)
}