summaryrefslogtreecommitdiffstats
path: root/vendor/winnow/src/binary
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/winnow/src/binary
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/winnow/src/binary')
-rw-r--r--vendor/winnow/src/binary/bits/mod.rs342
-rw-r--r--vendor/winnow/src/binary/bits/tests.rs190
-rw-r--r--vendor/winnow/src/binary/mod.rs2550
-rw-r--r--vendor/winnow/src/binary/tests.rs1214
4 files changed, 4296 insertions, 0 deletions
diff --git a/vendor/winnow/src/binary/bits/mod.rs b/vendor/winnow/src/binary/bits/mod.rs
new file mode 100644
index 000000000..5400e3308
--- /dev/null
+++ b/vendor/winnow/src/binary/bits/mod.rs
@@ -0,0 +1,342 @@
+//! Bit level parsers
+//!
+
+#[cfg(test)]
+mod tests;
+
+use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParseError};
+use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
+use crate::stream::{AsBytes, Stream, StreamIsPartial, ToUsize};
+use crate::trace::trace;
+use crate::{IResult, Parser};
+
+/// Converts a byte-level input to a bit-level input
+///
+/// See [`bytes`] to convert it back.
+///
+/// # Example
+/// ```
+/// use winnow::prelude::*;
+/// use winnow::Bytes;
+/// use winnow::binary::bits::{bits, take};
+/// use winnow::error::Error;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8)> {
+/// bits::<_, _, Error<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_next(input)
+/// }
+///
+/// let input = stream(&[0x12, 0x34, 0xff, 0xff]);
+///
+/// let output = parse(input).expect("We take 1.5 bytes and the input is longer than 2 bytes");
+///
+/// // The first byte is consumed, the second byte is partially consumed and dropped.
+/// let remaining = output.0;
+/// assert_eq!(remaining, stream(&[0xff, 0xff]));
+///
+/// let parsed = output.1;
+/// assert_eq!(parsed.0, 0x01);
+/// assert_eq!(parsed.1, 0x23);
+/// ```
+pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl Parser<I, O, E2>
+where
+ E1: ParseError<(I, usize)> + ErrorConvert<E2>,
+ E2: ParseError<I>,
+ I: Stream,
+ P: Parser<(I, usize), O, E1>,
+{
+ trace("bits", move |input: I| {
+ match parser.parse_next((input, 0)) {
+ Ok(((rest, offset), result)) => {
+ // If the next byte has been partially read, it will be sliced away as well.
+ // The parser functions might already slice away all fully read bytes.
+ // That's why `offset / 8` isn't necessarily needed at all times.
+ let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
+ let (input, _) = rest.next_slice(remaining_bytes_index);
+ Ok((input, result))
+ }
+ Err(ErrMode::Incomplete(n)) => Err(ErrMode::Incomplete(n.map(|u| u.get() / 8 + 1))),
+ Err(e) => Err(e.convert()),
+ }
+ })
+}
+
+/// Convert a [`bits`] stream back into a byte stream
+///
+/// **Warning:** A partial byte remaining in the input will be ignored and the given parser will
+/// start parsing at the next full byte.
+///
+/// ```
+/// use winnow::prelude::*;
+/// use winnow::Bytes;
+/// use winnow::binary::bits::{bits, bytes, take};
+/// use winnow::combinator::rest;
+/// use winnow::error::Error;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8, &[u8])> {
+/// bits::<_, _, Error<(_, usize)>, _, _>((
+/// take(4usize),
+/// take(8usize),
+/// bytes::<_, _, Error<_>, _, _>(rest)
+/// )).parse_next(input)
+/// }
+///
+/// let input = stream(&[0x12, 0x34, 0xff, 0xff]);
+///
+/// assert_eq!(parse(input), Ok(( stream(&[]), (0x01, 0x23, &[0xff, 0xff][..]) )));
+/// ```
+pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl Parser<(I, usize), O, E2>
+where
+ E1: ParseError<I> + ErrorConvert<E2>,
+ E2: ParseError<(I, usize)>,
+ I: Stream<Token = u8>,
+ P: Parser<I, O, E1>,
+{
+ trace("bytes", move |(input, offset): (I, usize)| {
+ let (inner, _) = if offset % 8 != 0 {
+ input.next_slice(1 + offset / 8)
+ } else {
+ input.next_slice(offset / 8)
+ };
+ let i = (input, offset);
+ match parser.parse_next(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(8) {
+ 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()),
+ }
+ })
+}
+
+/// Parse taking `count` bits
+///
+/// # Example
+/// ```rust
+/// # use winnow::prelude::*;
+/// # use winnow::Bytes;
+/// # use winnow::error::{Error, ErrorKind};
+/// use winnow::binary::bits::take;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// fn parser(input: (Stream<'_>, usize), count: usize)-> IResult<(Stream<'_>, usize), u8> {
+/// take(count).parse_next(input)
+/// }
+///
+/// // Consumes 0 bits, returns 0
+/// assert_eq!(parser((stream(&[0b00010010]), 0), 0), Ok(((stream(&[0b00010010]), 0), 0)));
+///
+/// // Consumes 4 bits, returns their values and increase offset to 4
+/// assert_eq!(parser((stream(&[0b00010010]), 0), 4), Ok(((stream(&[0b00010010]), 4), 0b00000001)));
+///
+/// // Consumes 4 bits, offset is 4, returns their values and increase offset to 0 of next byte
+/// assert_eq!(parser((stream(&[0b00010010]), 4), 4), Ok(((stream(&[]), 0), 0b00000010)));
+///
+/// // Tries to consume 12 bits but only 8 are available
+/// assert_eq!(parser((stream(&[0b00010010]), 0), 12), Err(winnow::error::ErrMode::Backtrack(Error{input: (stream(&[0b00010010]), 0), kind: ErrorKind::Eof })));
+/// ```
+#[inline(always)]
+pub fn take<I, O, C, E: ParseError<(I, usize)>>(count: C) -> impl Parser<(I, usize), O, E>
+where
+ I: Stream<Token = u8> + AsBytes + StreamIsPartial,
+ C: ToUsize,
+ O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
+{
+ let count = count.to_usize();
+ trace("take", 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: ParseError<(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,
+ O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
+{
+ if count == 0 {
+ Ok(((input, bit_offset), 0u8.into()))
+ } else {
+ let cnt = (count + bit_offset).div(8);
+ if input.eof_offset() * 8 < 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 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 < 8 - offset {
+ acc += val >> (8 - offset - remaining);
+ end_offset = remaining + offset;
+ break;
+ } else {
+ acc += val << (remaining - (8 - offset));
+ remaining -= 8 - offset;
+ offset = 0;
+ }
+ }
+ let (input, _) = input.next_slice(cnt);
+ Ok(((input, end_offset), acc))
+ }
+ }
+}
+
+/// Parse taking `count` bits and comparing them to `pattern`
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::prelude::*;
+/// # use winnow::Bytes;
+/// # use winnow::error::{Error, ErrorKind};
+/// use winnow::binary::bits::tag;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// /// Compare the lowest `count` bits of `input` against the lowest `count` bits of `pattern`.
+/// /// Return Ok and the matching section of `input` if there's a match.
+/// /// Return Err if there's no match.
+/// fn parser(pattern: u8, count: u8, input: (Stream<'_>, usize)) -> IResult<(Stream<'_>, usize), u8> {
+/// tag(pattern, count).parse_next(input)
+/// }
+///
+/// // The lowest 4 bits of 0b00001111 match the lowest 4 bits of 0b11111111.
+/// assert_eq!(
+/// parser(0b0000_1111, 4, (stream(&[0b1111_1111]), 0)),
+/// Ok(((stream(&[0b1111_1111]), 4), 0b0000_1111))
+/// );
+///
+/// // The lowest bit of 0b00001111 matches the lowest bit of 0b11111111 (both are 1).
+/// assert_eq!(
+/// parser(0b00000001, 1, (stream(&[0b11111111]), 0)),
+/// Ok(((stream(&[0b11111111]), 1), 0b00000001))
+/// );
+///
+/// // The lowest 2 bits of 0b11111111 and 0b00000001 are different.
+/// assert_eq!(
+/// parser(0b000000_01, 2, (stream(&[0b111111_11]), 0)),
+/// Err(winnow::error::ErrMode::Backtrack(Error {
+/// input: (stream(&[0b11111111]), 0),
+/// kind: ErrorKind::Tag
+/// }))
+/// );
+///
+/// // The lowest 8 bits of 0b11111111 and 0b11111110 are different.
+/// assert_eq!(
+/// parser(0b11111110, 8, (stream(&[0b11111111]), 0)),
+/// Err(winnow::error::ErrMode::Backtrack(Error {
+/// input: (stream(&[0b11111111]), 0),
+/// kind: ErrorKind::Tag
+/// }))
+/// );
+/// ```
+#[inline(always)]
+#[doc(alias = "literal")]
+#[doc(alias = "just")]
+pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
+ pattern: O,
+ count: C,
+) -> impl Parser<(I, usize), O, E>
+where
+ I: Stream<Token = u8> + AsBytes + StreamIsPartial,
+ C: ToUsize,
+ O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
+{
+ let count = count.to_usize();
+ trace("tag", move |input: (I, usize)| {
+ let inp = input.clone();
+
+ take(count).parse_next(input).and_then(|(i, o)| {
+ if pattern == o {
+ Ok((i, o))
+ } else {
+ Err(ErrMode::Backtrack(E::from_error_kind(inp, ErrorKind::Tag)))
+ }
+ })
+ })
+}
+
+/// Parses one specific bit as a bool.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::prelude::*;
+/// # use winnow::Bytes;
+/// # use winnow::error::{Error, ErrorKind};
+/// use winnow::binary::bits::bool;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// fn parse(input: (Stream<'_>, usize)) -> IResult<(Stream<'_>, usize), bool> {
+/// bool.parse_next(input)
+/// }
+///
+/// assert_eq!(parse((stream(&[0b10000000]), 0)), Ok(((stream(&[0b10000000]), 1), true)));
+/// assert_eq!(parse((stream(&[0b10000000]), 1)), Ok(((stream(&[0b10000000]), 2), false)));
+/// ```
+#[doc(alias = "any")]
+pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
+where
+ I: Stream<Token = u8> + AsBytes + StreamIsPartial,
+{
+ trace("bool", |input: (I, usize)| {
+ let (res, bit): (_, u32) = take(1usize).parse_next(input)?;
+ Ok((res, bit != 0))
+ })
+ .parse_next(input)
+}
diff --git a/vendor/winnow/src/binary/bits/tests.rs b/vendor/winnow/src/binary/bits/tests.rs
new file mode 100644
index 000000000..61dba2c31
--- /dev/null
+++ b/vendor/winnow/src/binary/bits/tests.rs
@@ -0,0 +1,190 @@
+use super::*;
+use crate::error::Error;
+use crate::Partial;
+
+#[test]
+/// Take the `bits` function and assert that remaining bytes are correctly returned, if the
+/// previous bytes are fully consumed
+fn test_complete_byte_consumption_bits() {
+ let input = &[0x12, 0x34, 0x56, 0x78][..];
+
+ // Take 3 bit slices with sizes [4, 8, 4].
+ let result: IResult<&[u8], (u8, u8, u8)> =
+ bits::<_, _, Error<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize)))
+ .parse_next(input);
+
+ let output = result.expect("We take 2 bytes and the input is longer than 2 bytes");
+
+ let remaining = output.0;
+ assert_eq!(remaining, [0x56, 0x78]);
+
+ let parsed = output.1;
+ assert_eq!(parsed.0, 0x01);
+ assert_eq!(parsed.1, 0x23);
+ assert_eq!(parsed.2, 0x04);
+}
+
+#[test]
+/// Take the `bits` function and assert that remaining bytes are correctly returned, if the
+/// previous bytes are NOT fully consumed. Partially consumed bytes are supposed to be dropped.
+/// I.e. if we consume 1.5 bytes of 4 bytes, 2 bytes will be returned, bits 13-16 will be
+/// dropped.
+fn test_partial_byte_consumption_bits() {
+ let input = &[0x12, 0x34, 0x56, 0x78][..];
+
+ // Take bit slices with sizes [4, 8].
+ let result: IResult<&[u8], (u8, u8)> =
+ bits::<_, _, Error<(&[u8], usize)>, _, _>((take(4usize), take(8usize))).parse_next(input);
+
+ let output = result.expect("We take 1.5 bytes and the input is longer than 2 bytes");
+
+ let remaining = output.0;
+ assert_eq!(remaining, [0x56, 0x78]);
+
+ let parsed = output.1;
+ assert_eq!(parsed.0, 0x01);
+ assert_eq!(parsed.1, 0x23);
+}
+
+#[test]
+#[cfg(feature = "std")]
+/// Ensure that in Incomplete error is thrown, if too few bytes are passed for a given parser.
+fn test_incomplete_bits() {
+ let input = Partial::new(&[0x12][..]);
+
+ // Take bit slices with sizes [4, 8].
+ let result: IResult<_, (u8, u8)> =
+ bits::<_, _, Error<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_next(input);
+
+ assert!(result.is_err());
+ let error = result.err().unwrap();
+ assert_eq!("Parsing requires 2 bytes/chars", error.to_string());
+}
+
+#[test]
+fn test_take_complete_0() {
+ let input = &[0b00010010][..];
+ let count = 0usize;
+ assert_eq!(count, 0usize);
+ let offset = 0usize;
+
+ let result: crate::IResult<(&[u8], usize), usize> = take(count).parse_next((input, offset));
+
+ assert_eq!(result, Ok(((input, offset), 0)));
+}
+
+#[test]
+fn test_take_complete_eof() {
+ let input = &[0b00010010][..];
+
+ let result: crate::IResult<(&[u8], usize), usize> = take(1usize).parse_next((input, 8));
+
+ assert_eq!(
+ result,
+ Err(crate::error::ErrMode::Backtrack(crate::error::Error {
+ input: (input, 8),
+ kind: ErrorKind::Eof
+ }))
+ );
+}
+
+#[test]
+fn test_take_complete_span_over_multiple_bytes() {
+ let input = &[0b00010010, 0b00110100, 0b11111111, 0b11111111][..];
+
+ let result: crate::IResult<(&[u8], usize), usize> = take(24usize).parse_next((input, 4));
+
+ assert_eq!(
+ result,
+ Ok((([0b11111111].as_ref(), 4), 0b1000110100111111111111))
+ );
+}
+
+#[test]
+fn test_take_partial_0() {
+ let input = Partial::new(&[][..]);
+ let count = 0usize;
+ assert_eq!(count, 0usize);
+ let offset = 0usize;
+
+ let result: crate::IResult<(_, usize), usize> = take(count).parse_next((input, offset));
+
+ assert_eq!(result, Ok(((input, offset), 0)));
+}
+
+#[test]
+fn test_tag_partial_ok() {
+ let input = Partial::new(&[0b00011111][..]);
+ let offset = 0usize;
+ let bits_to_take = 4usize;
+ let value_to_tag = 0b0001;
+
+ let result: crate::IResult<(_, usize), usize> =
+ tag(value_to_tag, bits_to_take).parse_next((input, offset));
+
+ assert_eq!(result, Ok(((input, bits_to_take), value_to_tag)));
+}
+
+#[test]
+fn test_tag_partial_err() {
+ let input = Partial::new(&[0b00011111][..]);
+ let offset = 0usize;
+ let bits_to_take = 4usize;
+ let value_to_tag = 0b1111;
+
+ let result: crate::IResult<(_, usize), usize> =
+ tag(value_to_tag, bits_to_take).parse_next((input, offset));
+
+ assert_eq!(
+ result,
+ Err(crate::error::ErrMode::Backtrack(crate::error::Error {
+ input: (input, offset),
+ kind: ErrorKind::Tag
+ }))
+ );
+}
+
+#[test]
+fn test_bool_0_complete() {
+ let input = [0b10000000].as_ref();
+
+ let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0));
+
+ assert_eq!(result, Ok(((input, 1), true)));
+}
+
+#[test]
+fn test_bool_eof_complete() {
+ let input = [0b10000000].as_ref();
+
+ let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8));
+
+ assert_eq!(
+ result,
+ Err(crate::error::ErrMode::Backtrack(crate::error::Error {
+ input: (input, 8),
+ kind: ErrorKind::Eof
+ }))
+ );
+}
+
+#[test]
+fn test_bool_0_partial() {
+ let input = Partial::new([0b10000000].as_ref());
+
+ let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool((input, 0));
+
+ assert_eq!(result, Ok(((input, 1), true)));
+}
+
+#[test]
+fn test_bool_eof_partial() {
+ let input = Partial::new([0b10000000].as_ref());
+
+ let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_next((input, 8));
+
+ assert_eq!(
+ result,
+ Err(crate::error::ErrMode::Incomplete(Needed::new(1)))
+ );
+}
diff --git a/vendor/winnow/src/binary/mod.rs b/vendor/winnow/src/binary/mod.rs
new file mode 100644
index 000000000..80435e359
--- /dev/null
+++ b/vendor/winnow/src/binary/mod.rs
@@ -0,0 +1,2550 @@
+//! Parsers recognizing numbers
+
+#![allow(clippy::match_same_arms)]
+
+pub mod bits;
+
+#[cfg(test)]
+mod tests;
+
+use crate::combinator::repeat;
+use crate::error::ErrMode;
+use crate::error::ErrorKind;
+use crate::error::Needed;
+use crate::error::ParseError;
+use crate::lib::std::ops::{Add, Shl};
+use crate::stream::Accumulate;
+use crate::stream::{AsBytes, Stream, StreamIsPartial};
+use crate::stream::{ToUsize, UpdateSlice};
+use crate::token::take;
+use crate::trace::trace;
+use crate::IResult;
+use crate::Parser;
+
+/// Configurable endianness
+#[derive(Debug, PartialEq, Eq, Clone, Copy)]
+pub enum Endianness {
+ /// Big endian
+ Big,
+ /// Little endian
+ Little,
+ /// Will match the host's endianness
+ Native,
+}
+
+/// Recognizes an unsigned 1 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u8> {
+/// be_u8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> {
+/// be_u8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ u8(input)
+}
+
+/// Recognizes a big endian unsigned 2 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u16;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u16> {
+/// be_u16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u16;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
+/// be_u16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_u16", move |input: I| be_uint(input, 2)).parse_next(input)
+}
+
+/// Recognizes a big endian unsigned 3 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u24;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u32> {
+/// be_u24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u24;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+/// be_u24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x000102)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_u23", move |input: I| be_uint(input, 3)).parse_next(input)
+}
+
+/// Recognizes a big endian unsigned 4 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u32> {
+/// be_u32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+/// be_u32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_u32", move |input: I| be_uint(input, 4)).parse_next(input)
+}
+
+/// Recognizes a big endian unsigned 8 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u64> {
+/// be_u64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> {
+/// be_u64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001020304050607)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_u64", move |input: I| be_uint(input, 8)).parse_next(input)
+}
+
+/// Recognizes a big endian unsigned 16 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_u128;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u128> {
+/// be_u128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_u128;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u128> {
+/// be_u128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203040506070809101112131415)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_u128", move |input: I| be_uint(input, 16)).parse_next(input)
+}
+
+#[inline]
+fn be_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+ Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
+{
+ debug_assert_ne!(bound, 1, "to_be_uint needs extra work to avoid overflow");
+ take(bound)
+ .map(|n: <I as Stream>::Slice| to_be_uint(n.as_bytes()))
+ .parse_next(input)
+}
+
+#[inline]
+fn to_be_uint<Uint>(number: &[u8]) -> Uint
+where
+ Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
+{
+ let mut res = Uint::default();
+ for byte in number.iter().copied() {
+ res = (res << 8) + byte.into();
+ }
+
+ res
+}
+
+/// Recognizes a signed 1 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i8> {
+/// be_i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> {
+/// be_i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ i8(input)
+}
+
+/// Recognizes a big endian signed 2 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i16;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i16> {
+/// be_i16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i16;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> {
+/// be_i16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_i16", move |input: I| {
+ be_uint::<_, u16, _>(input, 2).map(|(i, n)| (i, n as i16))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a big endian signed 3 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i24;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i32> {
+/// be_i24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i24;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+/// be_i24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x000102)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_i24", move |input: I| {
+ be_uint::<_, u32, _>(input, 3).map(|(i, n)| {
+ // Same as the unsigned version but we need to sign-extend manually here
+ let n = if n & 0x80_00_00 != 0 {
+ (n | 0xff_00_00_00) as i32
+ } else {
+ n as i32
+ };
+ (i, n)
+ })
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a big endian signed 4 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i32> {
+/// be_i32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+/// be_i32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(4))));
+/// ```
+#[inline(always)]
+pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_i32", move |input: I| {
+ be_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, n as i32))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a big endian signed 8 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i64> {
+/// be_i64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> {
+/// be_i64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001020304050607)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_i64", move |input: I| {
+ be_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, n as i64))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a big endian signed 16 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_i128;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i128> {
+/// be_i128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_i128;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i128> {
+/// be_i128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203040506070809101112131415)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_i128", move |input: I| {
+ be_uint::<_, u128, _>(input, 16).map(|(i, n)| (i, n as i128))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes an unsigned 1 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u8> {
+/// le_u8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> {
+/// le_u8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ u8(input)
+}
+
+/// Recognizes a little endian unsigned 2 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u16;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u16> {
+/// le_u16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u16;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
+/// le_u16::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_u16", move |input: I| le_uint(input, 2)).parse_next(input)
+}
+
+/// Recognizes a little endian unsigned 3 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u24;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u32> {
+/// le_u24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u24;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+/// le_u24::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_u24", move |input: I| le_uint(input, 3)).parse_next(input)
+}
+
+/// Recognizes a little endian unsigned 4 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u32> {
+/// le_u32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+/// le_u32::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x03020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_u32", move |input: I| le_uint(input, 4)).parse_next(input)
+}
+
+/// Recognizes a little endian unsigned 8 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u64> {
+/// le_u64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> {
+/// le_u64::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0706050403020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_u64", move |input: I| le_uint(input, 8)).parse_next(input)
+}
+
+/// Recognizes a little endian unsigned 16 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_u128;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u128> {
+/// le_u128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_u128;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u128> {
+/// le_u128::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x15141312111009080706050403020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_u128", move |input: I| le_uint(input, 16)).parse_next(input)
+}
+
+#[inline]
+fn le_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+ Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
+{
+ take(bound)
+ .map(|n: <I as Stream>::Slice| to_le_uint(n.as_bytes()))
+ .parse_next(input)
+}
+
+#[inline]
+fn to_le_uint<Uint>(number: &[u8]) -> Uint
+where
+ Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
+{
+ let mut res = Uint::default();
+ for (index, byte) in number.iter_offsets() {
+ res = res + (Uint::from(byte) << (8 * index as u8));
+ }
+
+ res
+}
+
+/// Recognizes a signed 1 byte integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i8> {
+/// le_i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> {
+/// le_i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ i8(input)
+}
+
+/// Recognizes a little endian signed 2 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i16;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i16> {
+/// le_i16.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i16;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> {
+/// le_i16::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_i16", move |input: I| {
+ le_uint::<_, u16, _>(input, 2).map(|(i, n)| (i, n as i16))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian signed 3 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i24;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i32> {
+/// le_i24.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i24;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+/// le_i24::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_i24", move |input: I| {
+ le_uint::<_, u32, _>(input, 3).map(|(i, n)| {
+ // Same as the unsigned version but we need to sign-extend manually here
+ let n = if n & 0x80_00_00 != 0 {
+ (n | 0xff_00_00_00) as i32
+ } else {
+ n as i32
+ };
+ (i, n)
+ })
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian signed 4 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i32> {
+/// le_i32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+/// le_i32::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x03020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_i32", move |input: I| {
+ le_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, n as i32))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian signed 8 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i64> {
+/// le_i64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> {
+/// le_i64::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0706050403020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_i64", move |input: I| {
+ le_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, n as i64))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian signed 16 bytes integer.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_i128;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i128> {
+/// le_i128.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_i128;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i128> {
+/// le_i128::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x15141312111009080706050403020100)));
+/// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_i128", move |input: I| {
+ le_uint::<_, u128, _>(input, 16).map(|(i, n)| (i, n as i128))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes an unsigned 1 byte integer
+///
+/// **Note:** that endianness does not apply to 1 byte numbers.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], u8> {
+/// u8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> {
+/// u8::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"\x03abcefg"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ trace("u8", move |input: I| {
+ if <I as StreamIsPartial>::is_partial_supported() {
+ u8_::<_, _, true>(input)
+ } else {
+ u8_::<_, _, false>(input)
+ }
+ })
+ .parse_next(input)
+}
+
+fn u8_<I, E: ParseError<I>, const PARTIAL: bool>(input: I) -> IResult<I, u8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ input.next_token().ok_or_else(|| {
+ if PARTIAL && input.is_partial() {
+ ErrMode::Incomplete(Needed::new(1))
+ } else {
+ ErrMode::Backtrack(E::from_error_kind(input, ErrorKind::Token))
+ }
+ })
+}
+
+/// Recognizes an unsigned 2 bytes integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian u16 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian u16 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u16;
+///
+/// let be_u16 = |s| {
+/// u16(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(be_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_u16 = |s| {
+/// u16(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(le_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u16;
+///
+/// let be_u16 = |s| {
+/// u16::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003)));
+/// assert_eq!(be_u16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+///
+/// let le_u16 = |s| {
+/// u16::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0300)));
+/// assert_eq!(le_u16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn u16<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_u16,
+ Endianness::Little => le_u16,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_u16,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_u16,
+ }
+ }(input)
+}
+
+/// Recognizes an unsigned 3 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian u24 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian u24 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u24;
+///
+/// let be_u24 = |s| {
+/// u24(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(be_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_u24 = |s| {
+/// u24(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(le_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u24;
+///
+/// let be_u24 = |s| {
+/// u24::<_,Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305)));
+/// assert_eq!(be_u24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+///
+/// let le_u24 = |s| {
+/// u24::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x050300)));
+/// assert_eq!(le_u24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn u24<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_u24,
+ Endianness::Little => le_u24,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_u24,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_u24,
+ }
+ }(input)
+}
+
+/// Recognizes an unsigned 4 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian u32 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian u32 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u32;
+///
+/// let be_u32 = |s| {
+/// u32(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(be_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_u32 = |s| {
+/// u32(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(le_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u32;
+///
+/// let be_u32 = |s| {
+/// u32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507)));
+/// assert_eq!(be_u32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+///
+/// let le_u32 = |s| {
+/// u32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07050300)));
+/// assert_eq!(le_u32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn u32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_u32,
+ Endianness::Little => le_u32,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_u32,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_u32,
+ }
+ }(input)
+}
+
+/// Recognizes an unsigned 8 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian u64 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian u64 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u64;
+///
+/// let be_u64 = |s| {
+/// u64(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
+/// assert_eq!(be_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_u64 = |s| {
+/// u64(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
+/// assert_eq!(le_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u64;
+///
+/// let be_u64 = |s| {
+/// u64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607)));
+/// assert_eq!(be_u64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+///
+/// let le_u64 = |s| {
+/// u64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0706050403020100)));
+/// assert_eq!(le_u64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn u64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_u64,
+ Endianness::Little => le_u64,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_u64,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_u64,
+ }
+ }(input)
+}
+
+/// Recognizes an unsigned 16 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian u128 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian u128 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::u128;
+///
+/// let be_u128 = |s| {
+/// u128(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
+/// assert_eq!(be_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_u128 = |s| {
+/// u128(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
+/// assert_eq!(le_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::u128;
+///
+/// let be_u128 = |s| {
+/// u128::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_u128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607)));
+/// assert_eq!(be_u128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+///
+/// let le_u128 = |s| {
+/// u128::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_u128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07060504030201000706050403020100)));
+/// assert_eq!(le_u128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn u128<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_u128,
+ Endianness::Little => le_u128,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_u128,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_u128,
+ }
+ }(input)
+}
+
+/// Recognizes a signed 1 byte integer
+///
+/// **Note:** that endianness does not apply to 1 byte numbers.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i8;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], i8> {
+/// i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i8;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> {
+/// i8.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"\x03abcefg"[..]), 0x00)));
+/// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+{
+ trace("i8", move |input: I| {
+ if <I as StreamIsPartial>::is_partial_supported() {
+ u8_::<_, _, true>(input)
+ } else {
+ u8_::<_, _, false>(input)
+ }
+ .map(|(i, n)| (i, n as i8))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a signed 2 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian i16 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian i16 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i16;
+///
+/// let be_i16 = |s| {
+/// i16(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(be_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_i16 = |s| {
+/// i16(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(le_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i16;
+///
+/// let be_i16 = |s| {
+/// i16::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003)));
+/// assert_eq!(be_i16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+///
+/// let le_i16 = |s| {
+/// i16::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0300)));
+/// assert_eq!(le_i16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn i16<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i16, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_i16,
+ Endianness::Little => le_i16,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_i16,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_i16,
+ }
+ }(input)
+}
+
+/// Recognizes a signed 3 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian i24 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian i24 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i24;
+///
+/// let be_i24 = |s| {
+/// i24(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(be_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_i24 = |s| {
+/// i24(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(le_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i24;
+///
+/// let be_i24 = |s| {
+/// i24::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305)));
+/// assert_eq!(be_i24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+///
+/// let le_i24 = |s| {
+/// i24::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x050300)));
+/// assert_eq!(le_i24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+#[inline(always)]
+pub fn i24<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_i24,
+ Endianness::Little => le_i24,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_i24,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_i24,
+ }
+ }(input)
+}
+
+/// Recognizes a signed 4 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian i32 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian i32 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i32;
+///
+/// let be_i32 = |s| {
+/// i32(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(be_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_i32 = |s| {
+/// i32(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(le_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i32;
+///
+/// let be_i32 = |s| {
+/// i32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507)));
+/// assert_eq!(be_i32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+///
+/// let le_i32 = |s| {
+/// i32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07050300)));
+/// assert_eq!(le_i32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn i32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_i32,
+ Endianness::Little => le_i32,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_i32,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_i32,
+ }
+ }(input)
+}
+
+/// Recognizes a signed 8 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian i64 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian i64 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i64;
+///
+/// let be_i64 = |s| {
+/// i64(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
+/// assert_eq!(be_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_i64 = |s| {
+/// i64(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
+/// assert_eq!(le_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i64;
+///
+/// let be_i64 = |s| {
+/// i64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607)));
+/// assert_eq!(be_i64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+///
+/// let le_i64 = |s| {
+/// i64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0706050403020100)));
+/// assert_eq!(le_i64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn i64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_i64,
+ Endianness::Little => le_i64,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_i64,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_i64,
+ }
+ }(input)
+}
+
+/// Recognizes a signed 16 byte integer
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian i128 integer,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian i128 integer.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::i128;
+///
+/// let be_i128 = |s| {
+/// i128(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
+/// assert_eq!(be_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+///
+/// let le_i128 = |s| {
+/// i128(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
+/// assert_eq!(le_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::i128;
+///
+/// let be_i128 = |s| {
+/// i128::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_i128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607)));
+/// assert_eq!(be_i128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+///
+/// let le_i128 = |s| {
+/// i128::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_i128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07060504030201000706050403020100)));
+/// assert_eq!(le_i128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15))));
+/// ```
+#[inline(always)]
+pub fn i128<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i128, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_i128,
+ Endianness::Little => le_i128,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_i128,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_i128,
+ }
+ }(input)
+}
+
+/// Recognizes a big endian 4 bytes floating point number.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_f32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], f32> {
+/// be_f32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_f32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f32> {
+/// be_f32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&[0x40, 0x29, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 2.640625)));
+/// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_f32", move |input: I| {
+ be_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, f32::from_bits(n)))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a big endian 8 bytes floating point number.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::be_f64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], f64> {
+/// be_f64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::be_f64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f64> {
+/// be_f64::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_f64", move |input: I| {
+ be_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, f64::from_bits(n)))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian 4 bytes floating point number.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_f32;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], f32> {
+/// le_f32.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_f32;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f32> {
+/// le_f32::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3))));
+/// ```
+#[inline(always)]
+pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("le_f32", move |input: I| {
+ le_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, f32::from_bits(n)))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a little endian 8 bytes floating point number.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::le_f64;
+///
+/// fn parser(s: &[u8]) -> IResult<&[u8], f64> {
+/// le_f64.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::Partial;
+/// use winnow::binary::le_f64;
+///
+/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f64> {
+/// le_f64::<_, Error<_>>.parse_next(s)
+/// }
+///
+/// assert_eq!(parser(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 3145728.0)));
+/// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7))));
+/// ```
+#[inline(always)]
+pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ trace("be_f64", move |input: I| {
+ le_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, f64::from_bits(n)))
+ })
+ .parse_next(input)
+}
+
+/// Recognizes a 4 byte floating point number
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian f32 float,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian f32 float.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::f32;
+///
+/// let be_f32 = |s| {
+/// f32(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(be_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+///
+/// let le_f32 = |s| {
+/// f32(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(le_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::f32;
+///
+/// let be_f32 = |s| {
+/// f32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_f32(Partial::new(&[0x41, 0x48, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(be_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+///
+/// let le_f32 = |s| {
+/// f32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_f32(Partial::new(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(le_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
+/// ```
+#[inline(always)]
+pub fn f32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f32, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_f32,
+ Endianness::Little => le_f32,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_f32,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_f32,
+ }
+ }(input)
+}
+
+/// Recognizes an 8 byte floating point number
+///
+/// If the parameter is `winnow::binary::Endianness::Big`, parse a big endian f64 float,
+/// otherwise if `winnow::binary::Endianness::Little` parse a little endian f64 float.
+///
+/// *Complete version*: returns an error if there is not enough input data
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// use winnow::binary::f64;
+///
+/// let be_f64 = |s| {
+/// f64(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(be_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+///
+/// let le_f64 = |s| {
+/// f64(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(le_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
+/// ```
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed};
+/// # use winnow::prelude::*;
+/// # use winnow::error::Needed::Size;
+/// # use winnow::Partial;
+/// use winnow::binary::f64;
+///
+/// let be_f64 = |s| {
+/// f64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s)
+/// };
+///
+/// assert_eq!(be_f64(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(be_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));
+///
+/// let le_f64 = |s| {
+/// f64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s)
+/// };
+///
+/// assert_eq!(le_f64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..])), Ok((Partial::new(&b""[..]), 12.5)));
+/// assert_eq!(le_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));
+/// ```
+#[inline(always)]
+pub fn f64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f64, E>
+where
+ I: StreamIsPartial,
+ I: Stream<Token = u8>,
+ <I as Stream>::Slice: AsBytes,
+{
+ move |input: I| {
+ match endian {
+ Endianness::Big => be_f64,
+ Endianness::Little => le_f64,
+ #[cfg(target_endian = "big")]
+ Endianness::Native => be_f64,
+ #[cfg(target_endian = "little")]
+ Endianness::Native => le_f64,
+ }
+ }(input)
+}
+
+/// Gets a number from the parser and returns a
+/// subslice of the input of that size.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Arguments
+/// * `f` The parser to apply.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed, stream::Partial};
+/// # use winnow::prelude::*;
+/// use winnow::Bytes;
+/// use winnow::binary::be_u16;
+/// use winnow::binary::length_data;
+/// use winnow::token::tag;
+///
+/// type Stream<'i> = Partial<&'i Bytes>;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Partial::new(Bytes::new(b))
+/// }
+///
+/// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
+/// length_data(be_u16).parse_next(s)
+/// }
+///
+/// assert_eq!(parser(stream(b"\x00\x03abcefg")), Ok((stream(&b"efg"[..]), &b"abc"[..])));
+/// assert_eq!(parser(stream(b"\x00\x03a")), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+pub fn length_data<I, N, E, F>(mut f: F) -> impl Parser<I, <I as Stream>::Slice, E>
+where
+ I: StreamIsPartial,
+ I: Stream,
+ N: ToUsize,
+ F: Parser<I, N, E>,
+ E: ParseError<I>,
+{
+ trace("length_data", move |i: I| {
+ let (i, length) = f.parse_next(i)?;
+
+ crate::token::take(length).parse_next(i)
+ })
+}
+
+/// 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.
+///
+/// *Complete version*: Returns an error if there is not enough input data.
+///
+/// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there is not enough data.
+///
+/// # Arguments
+/// * `f` The parser to apply.
+/// * `g` The parser to apply on the subslice.
+///
+/// # Example
+///
+/// ```rust
+/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed, stream::{Partial, StreamIsPartial}};
+/// # use winnow::prelude::*;
+/// use winnow::Bytes;
+/// use winnow::binary::be_u16;
+/// use winnow::binary::length_value;
+/// use winnow::token::tag;
+///
+/// type Stream<'i> = Partial<&'i Bytes>;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Partial::new(Bytes::new(b))
+/// }
+///
+/// fn complete_stream(b: &[u8]) -> Stream<'_> {
+/// let mut p = Partial::new(Bytes::new(b));
+/// let _ = p.complete();
+/// p
+/// }
+///
+/// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
+/// length_value(be_u16, "abc").parse_next(s)
+/// }
+///
+/// assert_eq!(parser(stream(b"\x00\x03abcefg")), Ok((stream(&b"efg"[..]), &b"abc"[..])));
+/// assert_eq!(parser(stream(b"\x00\x03123123")), Err(ErrMode::Backtrack(Error::new(complete_stream(&b"123"[..]), ErrorKind::Tag))));
+/// assert_eq!(parser(stream(b"\x00\x03a")), Err(ErrMode::Incomplete(Needed::new(2))));
+/// ```
+pub fn length_value<I, O, N, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, O, E>
+where
+ I: StreamIsPartial,
+ I: Stream + UpdateSlice,
+ N: ToUsize,
+ F: Parser<I, N, E>,
+ G: Parser<I, O, E>,
+ E: ParseError<I>,
+{
+ trace("length_value", move |i: I| {
+ let (i, data) = length_data(f.by_ref()).parse_next(i)?;
+ let mut data = I::update_slice(i.clone(), data);
+ let _ = data.complete();
+ let (_, o) = g.by_ref().complete_err().parse_next(data)?;
+ Ok((i, o))
+ })
+}
+
+/// Gets a number from the first parser,
+/// then applies the second parser that many times.
+///
+/// # Arguments
+/// * `f` The parser to apply to obtain the count.
+/// * `g` The parser to apply repeatedly.
+///
+/// # Example
+///
+/// ```rust
+/// # #[cfg(feature = "std")] {
+/// # use winnow::prelude::*;
+/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed};
+/// # use winnow::prelude::*;
+/// use winnow::Bytes;
+/// use winnow::binary::u8;
+/// use winnow::binary::length_count;
+/// use winnow::token::tag;
+///
+/// type Stream<'i> = &'i Bytes;
+///
+/// fn stream(b: &[u8]) -> Stream<'_> {
+/// Bytes::new(b)
+/// }
+///
+/// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, Vec<&[u8]>> {
+/// length_count(u8.map(|i| {
+/// println!("got number: {}", i);
+/// i
+/// }), "abc").parse_next(s)
+/// }
+///
+/// assert_eq!(parser(stream(b"\x02abcabcabc")), Ok((stream(b"abc"), vec![&b"abc"[..], &b"abc"[..]])));
+/// assert_eq!(parser(stream(b"\x03123123123")), Err(ErrMode::Backtrack(Error::new(stream(b"123123123"), ErrorKind::Tag))));
+/// # }
+/// ```
+pub fn length_count<I, O, C, N, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, C, E>
+where
+ I: Stream,
+ N: ToUsize,
+ C: Accumulate<O>,
+ F: Parser<I, N, E>,
+ G: Parser<I, O, E>,
+ E: ParseError<I>,
+{
+ trace("length_count", move |i: I| {
+ let (i, n) = f.parse_next(i)?;
+ let n = n.to_usize();
+ repeat(n, g.by_ref()).parse_next(i)
+ })
+}
diff --git a/vendor/winnow/src/binary/tests.rs b/vendor/winnow/src/binary/tests.rs
new file mode 100644
index 000000000..4307d88fe
--- /dev/null
+++ b/vendor/winnow/src/binary/tests.rs
@@ -0,0 +1,1214 @@
+use super::*;
+
+mod complete {
+ use super::*;
+ use crate::error::Error;
+
+ macro_rules! assert_parse(
+ ($left: expr, $right: expr) => {
+ let res: $crate::IResult<_, _, Error<_>> = $left;
+ assert_eq!(res, $right);
+ };
+ );
+
+ #[test]
+ fn i8_tests() {
+ assert_parse!(i8(&[0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(i8(&[0x7f][..]), Ok((&b""[..], 127)));
+ assert_parse!(i8(&[0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(i8(&[0x80][..]), Ok((&b""[..], -128)));
+ }
+
+ #[test]
+ fn be_i8_tests() {
+ assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127)));
+ assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128)));
+ }
+
+ #[test]
+ fn be_i16_tests() {
+ assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16)));
+ assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16)));
+ }
+
+ #[test]
+ fn be_u24_tests() {
+ assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32)));
+ assert_parse!(
+ be_u24(&[0x12, 0x34, 0x56][..]),
+ Ok((&b""[..], 1_193_046_u32))
+ );
+ }
+
+ #[test]
+ fn be_i24_tests() {
+ assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
+ assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32)));
+ assert_parse!(
+ be_i24(&[0xED, 0xCB, 0xAA][..]),
+ Ok((&b""[..], -1_193_046_i32))
+ );
+ }
+
+ #[test]
+ fn be_i32_tests() {
+ assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(
+ be_i32(&[0x7f, 0xff, 0xff, 0xff][..]),
+ Ok((&b""[..], 2_147_483_647_i32))
+ );
+ assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(
+ be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], -2_147_483_648_i32))
+ );
+ }
+
+ #[test]
+ fn be_i64_tests() {
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], 0))
+ );
+ assert_parse!(
+ be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
+ Ok((&b""[..], 9_223_372_036_854_775_807_i64))
+ );
+ assert_parse!(
+ be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
+ Ok((&b""[..], -1))
+ );
+ assert_parse!(
+ be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], -9_223_372_036_854_775_808_i64))
+ );
+ }
+
+ #[test]
+ fn be_i128_tests() {
+ assert_parse!(
+ be_i128(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ ),
+ Ok((&b""[..], 0))
+ );
+ assert_parse!(
+ be_i128(
+ &[
+ 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ ),
+ Ok((
+ &b""[..],
+ 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
+ ))
+ );
+ assert_parse!(
+ be_i128(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ ),
+ Ok((&b""[..], -1))
+ );
+ assert_parse!(
+ be_i128(
+ &[
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ ),
+ Ok((
+ &b""[..],
+ -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
+ ))
+ );
+ }
+
+ #[test]
+ fn le_i8_tests() {
+ assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127)));
+ assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128)));
+ }
+
+ #[test]
+ fn le_i16_tests() {
+ assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16)));
+ assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
+ }
+
+ #[test]
+ fn le_u24_tests() {
+ assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32)));
+ assert_parse!(
+ le_u24(&[0x56, 0x34, 0x12][..]),
+ Ok((&b""[..], 1_193_046_u32))
+ );
+ }
+
+ #[test]
+ fn le_i24_tests() {
+ assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
+ assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32)));
+ assert_parse!(
+ le_i24(&[0xAA, 0xCB, 0xED][..]),
+ Ok((&b""[..], -1_193_046_i32))
+ );
+ }
+
+ #[test]
+ fn le_i32_tests() {
+ assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
+ assert_parse!(
+ le_i32(&[0xff, 0xff, 0xff, 0x7f][..]),
+ Ok((&b""[..], 2_147_483_647_i32))
+ );
+ assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
+ assert_parse!(
+ le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
+ Ok((&b""[..], -2_147_483_648_i32))
+ );
+ }
+
+ #[test]
+ fn le_i64_tests() {
+ assert_parse!(
+ le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], 0))
+ );
+ assert_parse!(
+ le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
+ Ok((&b""[..], 9_223_372_036_854_775_807_i64))
+ );
+ assert_parse!(
+ le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
+ Ok((&b""[..], -1))
+ );
+ assert_parse!(
+ le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
+ Ok((&b""[..], -9_223_372_036_854_775_808_i64))
+ );
+ }
+
+ #[test]
+ fn le_i128_tests() {
+ assert_parse!(
+ le_i128(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ ),
+ Ok((&b""[..], 0))
+ );
+ assert_parse!(
+ le_i128(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0x7f
+ ][..]
+ ),
+ Ok((
+ &b""[..],
+ 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
+ ))
+ );
+ assert_parse!(
+ le_i128(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ ),
+ Ok((&b""[..], -1))
+ );
+ assert_parse!(
+ le_i128(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x80
+ ][..]
+ ),
+ Ok((
+ &b""[..],
+ -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
+ ))
+ );
+ }
+
+ #[test]
+ fn be_f32_tests() {
+ assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
+ assert_parse!(
+ be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
+ Ok((&b""[..], 185_728_380_f32))
+ );
+ }
+
+ #[test]
+ fn be_f64_tests() {
+ assert_parse!(
+ be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], 0_f64))
+ );
+ assert_parse!(
+ be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], 185_728_392_f64))
+ );
+ }
+
+ #[test]
+ fn le_f32_tests() {
+ assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
+ assert_parse!(
+ le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
+ Ok((&b""[..], 185_728_380_f32))
+ );
+ }
+
+ #[test]
+ fn le_f64_tests() {
+ assert_parse!(
+ le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Ok((&b""[..], 0_f64))
+ );
+ assert_parse!(
+ le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
+ Ok((&b""[..], 185_728_392_f64))
+ );
+ }
+
+ #[test]
+ fn configurable_endianness() {
+ use crate::binary::Endianness;
+
+ fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
+ u16(Endianness::Big).parse_next(i)
+ }
+ fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
+ u16(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
+ assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
+
+ fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
+ u32(Endianness::Big).parse_next(i)
+ }
+ fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
+ u32(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tst32(&[0x12, 0x00, 0x60, 0x00]),
+ Ok((&b""[..], 302_014_464_u32))
+ );
+ assert_eq!(
+ le_tst32(&[0x12, 0x00, 0x60, 0x00]),
+ Ok((&b""[..], 6_291_474_u32))
+ );
+
+ fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
+ u64(Endianness::Big).parse_next(i)
+ }
+ fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
+ u64(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
+ Ok((&b""[..], 1_297_142_246_100_992_000_u64))
+ );
+ assert_eq!(
+ le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
+ Ok((&b""[..], 36_028_874_334_666_770_u64))
+ );
+
+ fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
+ i16(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
+ i16(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
+ assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
+
+ fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
+ i32(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
+ i32(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
+ Ok((&b""[..], 1_204_224_i32))
+ );
+ assert_eq!(
+ le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
+ Ok((&b""[..], 6_296_064_i32))
+ );
+
+ fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
+ i64(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
+ i64(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
+ Ok((&b""[..], 71_881_672_479_506_432_i64))
+ );
+ assert_eq!(
+ le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
+ Ok((&b""[..], 36_028_874_334_732_032_i64))
+ );
+ }
+}
+
+mod partial {
+ use super::*;
+ use crate::error::ErrMode;
+ use crate::error::Error;
+ use crate::error::Needed;
+ #[cfg(feature = "alloc")]
+ use crate::lib::std::vec::Vec;
+ use crate::Partial;
+ use crate::{
+ ascii::digit1 as digit,
+ binary::{be_u16, be_u8},
+ error::ErrorKind,
+ lib::std::str::{self, FromStr},
+ IResult,
+ };
+
+ macro_rules! assert_parse(
+ ($left: expr, $right: expr) => {
+ let res: $crate::IResult<_, _, Error<_>> = $left;
+ assert_eq!(res, $right);
+ };
+ );
+
+ #[test]
+ fn i8_tests() {
+ assert_parse!(
+ be_i8(Partial::new(&[0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_i8(Partial::new(&[0x7f][..])),
+ Ok((Partial::new(&b""[..]), 127))
+ );
+ assert_parse!(
+ be_i8(Partial::new(&[0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ be_i8(Partial::new(&[0x80][..])),
+ Ok((Partial::new(&b""[..]), -128))
+ );
+ assert_parse!(
+ be_i8(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn i16_tests() {
+ assert_parse!(
+ be_i16(Partial::new(&[0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_i16(Partial::new(&[0x7f, 0xff][..])),
+ Ok((Partial::new(&b""[..]), 32_767_i16))
+ );
+ assert_parse!(
+ be_i16(Partial::new(&[0xff, 0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ be_i16(Partial::new(&[0x80, 0x00][..])),
+ Ok((Partial::new(&b""[..]), -32_768_i16))
+ );
+ assert_parse!(
+ be_i16(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i16(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn u24_tests() {
+ assert_parse!(
+ be_u24(Partial::new(&[0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_u24(Partial::new(&[0x00, 0xFF, 0xFF][..])),
+ Ok((Partial::new(&b""[..]), 65_535_u32))
+ );
+ assert_parse!(
+ be_u24(Partial::new(&[0x12, 0x34, 0x56][..])),
+ Ok((Partial::new(&b""[..]), 1_193_046_u32))
+ );
+ assert_parse!(
+ be_u24(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_u24(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_u24(Partial::new(&[0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn i24_tests() {
+ assert_parse!(
+ be_i24(Partial::new(&[0xFF, 0xFF, 0xFF][..])),
+ Ok((Partial::new(&b""[..]), -1_i32))
+ );
+ assert_parse!(
+ be_i24(Partial::new(&[0xFF, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), -65_536_i32))
+ );
+ assert_parse!(
+ be_i24(Partial::new(&[0xED, 0xCB, 0xAA][..])),
+ Ok((Partial::new(&b""[..]), -1_193_046_i32))
+ );
+ assert_parse!(
+ be_i24(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i24(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i24(Partial::new(&[0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn i32_tests() {
+ assert_parse!(
+ be_i32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0x7f, 0xff, 0xff, 0xff][..])),
+ Ok((Partial::new(&b""[..]), 2_147_483_647_i32))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0x80, 0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), -2_147_483_648_i32))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(4)))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i32(Partial::new(&[0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn i64_tests() {
+ assert_parse!(
+ be_i64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_i64(Partial::new(
+ &[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]
+ )),
+ Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64))
+ );
+ assert_parse!(
+ be_i64(Partial::new(
+ &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]
+ )),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ be_i64(Partial::new(
+ &[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(8)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(7)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(6)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(5)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(4)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn i128_tests() {
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ )),
+ Ok((
+ Partial::new(&b""[..]),
+ 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
+ ))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ )),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ )),
+ Ok((
+ Partial::new(&b""[..]),
+ -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
+ ))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[][..])),
+ Err(ErrMode::Incomplete(Needed::new(16)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(15)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(14)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(13)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(12)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(11)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])),
+ Err(ErrMode::Incomplete(Needed::new(10)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(9)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(8)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(7)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(6)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(5)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(4)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00
+ ][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i128(Partial::new(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00
+ ][..]
+ )),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ }
+
+ #[test]
+ fn le_i8_tests() {
+ assert_parse!(
+ le_i8(Partial::new(&[0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_i8(Partial::new(&[0x7f][..])),
+ Ok((Partial::new(&b""[..]), 127))
+ );
+ assert_parse!(
+ le_i8(Partial::new(&[0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ le_i8(Partial::new(&[0x80][..])),
+ Ok((Partial::new(&b""[..]), -128))
+ );
+ }
+
+ #[test]
+ fn le_i16_tests() {
+ assert_parse!(
+ le_i16(Partial::new(&[0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_i16(Partial::new(&[0xff, 0x7f][..])),
+ Ok((Partial::new(&b""[..]), 32_767_i16))
+ );
+ assert_parse!(
+ le_i16(Partial::new(&[0xff, 0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ le_i16(Partial::new(&[0x00, 0x80][..])),
+ Ok((Partial::new(&b""[..]), -32_768_i16))
+ );
+ }
+
+ #[test]
+ fn le_u24_tests() {
+ assert_parse!(
+ le_u24(Partial::new(&[0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_u24(Partial::new(&[0xFF, 0xFF, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 65_535_u32))
+ );
+ assert_parse!(
+ le_u24(Partial::new(&[0x56, 0x34, 0x12][..])),
+ Ok((Partial::new(&b""[..]), 1_193_046_u32))
+ );
+ }
+
+ #[test]
+ fn le_i24_tests() {
+ assert_parse!(
+ le_i24(Partial::new(&[0xFF, 0xFF, 0xFF][..])),
+ Ok((Partial::new(&b""[..]), -1_i32))
+ );
+ assert_parse!(
+ le_i24(Partial::new(&[0x00, 0x00, 0xFF][..])),
+ Ok((Partial::new(&b""[..]), -65_536_i32))
+ );
+ assert_parse!(
+ le_i24(Partial::new(&[0xAA, 0xCB, 0xED][..])),
+ Ok((Partial::new(&b""[..]), -1_193_046_i32))
+ );
+ }
+
+ #[test]
+ fn le_i32_tests() {
+ assert_parse!(
+ le_i32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_i32(Partial::new(&[0xff, 0xff, 0xff, 0x7f][..])),
+ Ok((Partial::new(&b""[..]), 2_147_483_647_i32))
+ );
+ assert_parse!(
+ le_i32(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ le_i32(Partial::new(&[0x00, 0x00, 0x00, 0x80][..])),
+ Ok((Partial::new(&b""[..]), -2_147_483_648_i32))
+ );
+ }
+
+ #[test]
+ fn le_i64_tests() {
+ assert_parse!(
+ le_i64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_i64(Partial::new(
+ &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]
+ )),
+ Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64))
+ );
+ assert_parse!(
+ le_i64(Partial::new(
+ &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]
+ )),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ le_i64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]
+ )),
+ Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64))
+ );
+ }
+
+ #[test]
+ fn le_i128_tests() {
+ assert_parse!(
+ le_i128(Partial::new(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00
+ ][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0))
+ );
+ assert_parse!(
+ le_i128(Partial::new(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0x7f
+ ][..]
+ )),
+ Ok((
+ Partial::new(&b""[..]),
+ 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
+ ))
+ );
+ assert_parse!(
+ le_i128(Partial::new(
+ &[
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff
+ ][..]
+ )),
+ Ok((Partial::new(&b""[..]), -1))
+ );
+ assert_parse!(
+ le_i128(Partial::new(
+ &[
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x80
+ ][..]
+ )),
+ Ok((
+ Partial::new(&b""[..]),
+ -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
+ ))
+ );
+ }
+
+ #[test]
+ fn be_f32_tests() {
+ assert_parse!(
+ be_f32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0_f32))
+ );
+ assert_parse!(
+ be_f32(Partial::new(&[0x4d, 0x31, 0x1f, 0xd8][..])),
+ Ok((Partial::new(&b""[..]), 185_728_380_f32))
+ );
+ }
+
+ #[test]
+ fn be_f64_tests() {
+ assert_parse!(
+ be_f64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0_f64))
+ );
+ assert_parse!(
+ be_f64(Partial::new(
+ &[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), 185_728_392_f64))
+ );
+ }
+
+ #[test]
+ fn le_f32_tests() {
+ assert_parse!(
+ le_f32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])),
+ Ok((Partial::new(&b""[..]), 0_f32))
+ );
+ assert_parse!(
+ le_f32(Partial::new(&[0xd8, 0x1f, 0x31, 0x4d][..])),
+ Ok((Partial::new(&b""[..]), 185_728_380_f32))
+ );
+ }
+
+ #[test]
+ fn le_f64_tests() {
+ assert_parse!(
+ le_f64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ )),
+ Ok((Partial::new(&b""[..]), 0_f64))
+ );
+ assert_parse!(
+ le_f64(Partial::new(
+ &[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]
+ )),
+ Ok((Partial::new(&b""[..]), 185_728_392_f64))
+ );
+ }
+
+ #[test]
+ fn configurable_endianness() {
+ use crate::binary::Endianness;
+
+ fn be_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
+ u16(Endianness::Big).parse_next(i)
+ }
+ fn le_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
+ u16(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tst16(Partial::new(&[0x80, 0x00])),
+ Ok((Partial::new(&b""[..]), 32_768_u16))
+ );
+ assert_eq!(
+ le_tst16(Partial::new(&[0x80, 0x00])),
+ Ok((Partial::new(&b""[..]), 128_u16))
+ );
+
+ fn be_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+ u32(Endianness::Big).parse_next(i)
+ }
+ fn le_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+ u32(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])),
+ Ok((Partial::new(&b""[..]), 302_014_464_u32))
+ );
+ assert_eq!(
+ le_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])),
+ Ok((Partial::new(&b""[..]), 6_291_474_u32))
+ );
+
+ fn be_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> {
+ u64(Endianness::Big).parse_next(i)
+ }
+ fn le_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> {
+ u64(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tst64(Partial::new(&[
+ 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00
+ ])),
+ Ok((Partial::new(&b""[..]), 1_297_142_246_100_992_000_u64))
+ );
+ assert_eq!(
+ le_tst64(Partial::new(&[
+ 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00
+ ])),
+ Ok((Partial::new(&b""[..]), 36_028_874_334_666_770_u64))
+ );
+
+ fn be_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> {
+ i16(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> {
+ i16(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tsti16(Partial::new(&[0x00, 0x80])),
+ Ok((Partial::new(&b""[..]), 128_i16))
+ );
+ assert_eq!(
+ le_tsti16(Partial::new(&[0x00, 0x80])),
+ Ok((Partial::new(&b""[..]), -32_768_i16))
+ );
+
+ fn be_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+ i32(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> {
+ i32(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])),
+ Ok((Partial::new(&b""[..]), 1_204_224_i32))
+ );
+ assert_eq!(
+ le_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])),
+ Ok((Partial::new(&b""[..]), 6_296_064_i32))
+ );
+
+ fn be_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> {
+ i64(Endianness::Big).parse_next(i)
+ }
+ fn le_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> {
+ i64(Endianness::Little).parse_next(i)
+ }
+ assert_eq!(
+ be_tsti64(Partial::new(&[
+ 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00
+ ])),
+ Ok((Partial::new(&b""[..]), 71_881_672_479_506_432_i64))
+ );
+ assert_eq!(
+ le_tsti64(Partial::new(&[
+ 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00
+ ])),
+ Ok((Partial::new(&b""[..]), 36_028_874_334_732_032_i64))
+ );
+ }
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn length_count_test() {
+ fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+ digit
+ .try_map(str::from_utf8)
+ .try_map(FromStr::from_str)
+ .parse_next(i)
+ }
+
+ fn cnt(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> {
+ length_count(number, "abc").parse_next(i)
+ }
+
+ assert_eq!(
+ cnt(Partial::new(&b"2abcabcabcdef"[..])),
+ Ok((Partial::new(&b"abcdef"[..]), vec![&b"abc"[..], &b"abc"[..]]))
+ );
+ assert_eq!(
+ cnt(Partial::new(&b"2ab"[..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ assert_eq!(
+ cnt(Partial::new(&b"3abcab"[..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ assert_eq!(
+ cnt(Partial::new(&b"xxx"[..])),
+ Err(ErrMode::Backtrack(error_position!(
+ Partial::new(&b"xxx"[..]),
+ ErrorKind::Slice
+ )))
+ );
+ assert_eq!(
+ cnt(Partial::new(&b"2abcxxx"[..])),
+ Err(ErrMode::Backtrack(error_position!(
+ Partial::new(&b"xxx"[..]),
+ ErrorKind::Tag
+ )))
+ );
+ }
+
+ #[test]
+ fn length_data_test() {
+ fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> {
+ digit
+ .try_map(str::from_utf8)
+ .try_map(FromStr::from_str)
+ .parse_next(i)
+ }
+
+ fn take(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> {
+ length_data(number).parse_next(i)
+ }
+
+ assert_eq!(
+ take(Partial::new(&b"6abcabcabcdef"[..])),
+ Ok((Partial::new(&b"abcdef"[..]), &b"abcabc"[..]))
+ );
+ assert_eq!(
+ take(Partial::new(&b"3ab"[..])),
+ Err(ErrMode::Incomplete(Needed::new(1)))
+ );
+ assert_eq!(
+ take(Partial::new(&b"xxx"[..])),
+ Err(ErrMode::Backtrack(error_position!(
+ Partial::new(&b"xxx"[..]),
+ ErrorKind::Slice
+ )))
+ );
+ assert_eq!(
+ take(Partial::new(&b"2abcxxx"[..])),
+ Ok((Partial::new(&b"cxxx"[..]), &b"ab"[..]))
+ );
+ }
+
+ #[test]
+ fn length_value_test() {
+ use crate::stream::StreamIsPartial;
+
+ fn length_value_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
+ length_value(be_u8, be_u16).parse_next(i)
+ }
+ fn length_value_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u8, u8)> {
+ length_value(be_u8, (be_u8, be_u8)).parse_next(i)
+ }
+
+ let mut empty_complete = Partial::new(&b""[..]);
+ let _ = empty_complete.complete();
+
+ let i1 = [0, 5, 6];
+ assert_eq!(
+ length_value_1(Partial::new(&i1)),
+ Err(ErrMode::Backtrack(error_position!(
+ empty_complete,
+ ErrorKind::Slice
+ )))
+ );
+ assert_eq!(
+ length_value_2(Partial::new(&i1)),
+ Err(ErrMode::Backtrack(error_position!(
+ empty_complete,
+ ErrorKind::Token
+ )))
+ );
+
+ let i2 = [1, 5, 6, 3];
+ {
+ let mut middle_complete = Partial::new(&i2[1..2]);
+ let _ = middle_complete.complete();
+ assert_eq!(
+ length_value_1(Partial::new(&i2)),
+ Err(ErrMode::Backtrack(error_position!(
+ middle_complete,
+ ErrorKind::Slice
+ )))
+ );
+ assert_eq!(
+ length_value_2(Partial::new(&i2)),
+ Err(ErrMode::Backtrack(error_position!(
+ empty_complete,
+ ErrorKind::Token
+ )))
+ );
+ }
+
+ let i3 = [2, 5, 6, 3, 4, 5, 7];
+ assert_eq!(
+ length_value_1(Partial::new(&i3)),
+ Ok((Partial::new(&i3[3..]), 1286))
+ );
+ assert_eq!(
+ length_value_2(Partial::new(&i3)),
+ Ok((Partial::new(&i3[3..]), (5, 6)))
+ );
+
+ let i4 = [3, 5, 6, 3, 4, 5];
+ assert_eq!(
+ length_value_1(Partial::new(&i4)),
+ Ok((Partial::new(&i4[4..]), 1286))
+ );
+ assert_eq!(
+ length_value_2(Partial::new(&i4)),
+ Ok((Partial::new(&i4[4..]), (5, 6)))
+ );
+ }
+}