aboutsummaryrefslogtreecommitdiff
path: root/src/number/streaming.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/number/streaming.rs')
-rw-r--r--src/number/streaming.rs1561
1 files changed, 1170 insertions, 391 deletions
diff --git a/src/number/streaming.rs b/src/number/streaming.rs
index 15a89a4..f502519 100644
--- a/src/number/streaming.rs
+++ b/src/number/streaming.rs
@@ -1,694 +1,1301 @@
-//! parsers recognizing numbers, streaming version
+//! Parsers recognizing numbers, streaming version
-use crate::internal::*;
+use crate::branch::alt;
+use crate::character::streaming::{char, digit1};
+use crate::combinator::{cut, map, opt, recognize};
use crate::error::{ErrorKind, ParseError};
-use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition};
+use crate::internal::*;
use crate::lib::std::ops::{RangeFrom, RangeTo};
-use crate::traits::{Offset, Slice};
-use crate::character::streaming::{char, digit1};
use crate::sequence::{pair, tuple};
-use crate::combinator::{cut, map, opt, recognize};
-use crate::branch::alt;
+use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition};
+use crate::traits::{Offset, Slice};
-/// Recognizes an unsigned 1 byte integer
+/// Recognizes an unsigned 1 byte integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u8;
///
-/// let parser = be_u8::<(_, ErrorKind)>;
+/// let parser = |s| {
+/// be_u8::<_, (_, ErrorKind)>(s)
+/// };
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
- if i.len() < 1 {
- Err(Err::Incomplete(Needed::Size(1)))
+pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 1;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(1)))
} else {
- Ok((&i[1..], i[0]))
+ let res = input.iter_elements().next().unwrap();
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a big endian unsigned 2 bytes integer
+/// Recognizes a big endian unsigned 2 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u16;
///
/// let parser = |s| {
-/// be_u16::<(_, ErrorKind)>(s)
+/// be_u16::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
- if i.len() < 2 {
- Err(Err::Incomplete(Needed::Size(2)))
+pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 2;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[0] as u16) << 8) + i[1] as u16;
- Ok((&i[2..], res))
+ let mut res = 0u16;
+ for byte in input.iter_elements().take(bound) {
+ res = (res << 8) + byte as u16;
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a big endian unsigned 3 byte integer
+/// Recognizes a big endian unsigned 3 byte integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u24;
///
/// let parser = |s| {
-/// be_u24::<(_, ErrorKind)>(s)
+/// be_u24::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
+/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
/// ```
#[inline]
-pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
- if i.len() < 3 {
- Err(Err::Incomplete(Needed::Size(3)))
+pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 3;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32);
- Ok((&i[3..], res))
+ let mut res = 0u32;
+ for byte in input.iter_elements().take(bound) {
+ res = (res << 8) + byte as u32;
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a big endian unsigned 4 bytes integer
+/// Recognizes a big endian unsigned 4 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u32;
///
/// let parser = |s| {
-/// be_u32::<(_, ErrorKind)>(s)
+/// be_u32::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
- if i.len() < 4 {
- Err(Err::Incomplete(Needed::Size(4)))
+pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 4;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32;
- Ok((&i[4..], res))
+ let mut res = 0u32;
+ for byte in input.iter_elements().take(bound) {
+ res = (res << 8) + byte as u32;
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a big endian unsigned 8 bytes integer
+/// Recognizes a big endian unsigned 8 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u64;
///
/// let parser = |s| {
-/// be_u64::<(_, ErrorKind)>(s)
+/// be_u64::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
- if i.len() < 8 {
- Err(Err::Incomplete(Needed::Size(8)))
+pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 8;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24)
- + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64;
- Ok((&i[8..], res))
+ let mut res = 0u64;
+ for byte in input.iter_elements().take(bound) {
+ res = (res << 8) + byte as u64;
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a big endian unsigned 16 bytes integer
+/// Recognizes a big endian unsigned 16 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_u128;
///
/// let parser = |s| {
-/// be_u128::<(_, ErrorKind)>(s)
+/// be_u128::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
#[cfg(stable_i128)]
-pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
- if i.len() < 16 {
- Err(Err::Incomplete(Needed::Size(16)))
+pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 16;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[0] as u128) << 120)
- + ((i[1] as u128) << 112)
- + ((i[2] as u128) << 104)
- + ((i[3] as u128) << 96)
- + ((i[4] as u128) << 88)
- + ((i[5] as u128) << 80)
- + ((i[6] as u128) << 72)
- + ((i[7] as u128) << 64)
- + ((i[8] as u128) << 56)
- + ((i[9] as u128) << 48)
- + ((i[10] as u128) << 40)
- + ((i[11] as u128) << 32)
- + ((i[12] as u128) << 24)
- + ((i[13] as u128) << 16)
- + ((i[14] as u128) << 8)
- + i[15] as u128;
- Ok((&i[16..], res))
+ let mut res = 0u128;
+ for byte in input.iter_elements().take(bound) {
+ res = (res << 8) + byte as u128;
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a signed 1 byte integer
+/// Recognizes a signed 1 byte integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i8;
///
-/// let parser = be_i8::<(_, ErrorKind)>;
+/// let parser = be_i8::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
- map!(i, be_u8, |x| x as i8)
+pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, be_u8, |x| x as i8)
}
-/// Recognizes a big endian signed 2 bytes integer
+/// Recognizes a big endian signed 2 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i16;
///
-/// let parser = be_i16::<(_, ErrorKind)>;
+/// let parser = be_i16::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(2))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(2))));
/// ```
#[inline]
-pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
- map!(i, be_u16, |x| x as i16)
+pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, be_u16, |x| x as i16)
}
-/// Recognizes a big endian signed 3 bytes integer
+/// Recognizes a big endian signed 3 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i24;
///
-/// let parser = be_i24::<(_, ErrorKind)>;
+/// let parser = be_i24::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(3))));
+/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
+pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
// Same as the unsigned version but we need to sign-extend manually here
- map!(i, be_u24, |x| if x & 0x80_00_00 != 0 {
+ map!(input, be_u24, |x| if x & 0x80_00_00 != 0 {
(x | 0xff_00_00_00) as i32
} else {
x as i32
})
}
-/// Recognizes a big endian signed 4 bytes integer
+/// Recognizes a big endian signed 4 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i32;
///
-/// let parser = be_i32::<(_, ErrorKind)>;
+/// let parser = be_i32::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(4))));
/// ```
#[inline]
-pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
- map!(i, be_u32, |x| x as i32)
+pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, be_u32, |x| x as i32)
}
-/// Recognizes a big endian signed 8 bytes integer
+/// Recognizes a big endian signed 8 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i64;
///
-/// let parser = be_i64::<(_, ErrorKind)>;
+/// let parser = be_i64::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
- map!(i, be_u64, |x| x as i64)
+pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, be_u64, |x| x as i64)
}
-/// Recognizes a big endian signed 16 bytes integer
+/// Recognizes a big endian signed 16 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_i128;
///
-/// let parser = be_i128::<(_, ErrorKind)>;
+/// let parser = be_i128::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
#[cfg(stable_i128)]
-pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
- map!(i, be_u128, |x| x as i128)
+pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, be_u128, |x| x as i128)
}
-/// Recognizes an unsigned 1 byte integer
+/// Recognizes an unsigned 1 byte integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u8;
///
-/// let parser = le_u8::<(_, ErrorKind)>;
+/// let parser = le_u8::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
- if i.len() < 1 {
- Err(Err::Incomplete(Needed::Size(1)))
+pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 1;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(1)))
} else {
- Ok((&i[1..], i[0]))
+ let res = input.iter_elements().next().unwrap();
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a little endian unsigned 2 bytes integer
+/// Recognizes a little endian unsigned 2 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u16;
///
/// let parser = |s| {
-/// le_u16::<(_, ErrorKind)>(s)
+/// le_u16::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
- if i.len() < 2 {
- Err(Err::Incomplete(Needed::Size(2)))
+pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 2;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[1] as u16) << 8) + i[0] as u16;
- Ok((&i[2..], res))
+ let mut res = 0u16;
+ for (index, byte) in input.iter_indices().take(bound) {
+ res += (byte as u16) << (8 * index);
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a little endian unsigned 3 bytes integer
+/// Recognizes a little endian unsigned 3 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u24;
///
/// let parser = |s| {
-/// le_u24::<(_, ErrorKind)>(s)
+/// le_u24::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
+/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
/// ```
#[inline]
-pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
- if i.len() < 3 {
- Err(Err::Incomplete(Needed::Size(3)))
+pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 3;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16);
- Ok((&i[3..], res))
+ let mut res = 0u32;
+ for (index, byte) in input.iter_indices().take(bound) {
+ res += (byte as u32) << (8 * index);
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a little endian unsigned 4 bytes integer
+/// Recognizes a little endian unsigned 4 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u32;
///
/// let parser = |s| {
-/// le_u32::<(_, ErrorKind)>(s)
+/// le_u32::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
- if i.len() < 4 {
- Err(Err::Incomplete(Needed::Size(4)))
+pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 4;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32;
- Ok((&i[4..], res))
+ let mut res = 0u32;
+ for (index, byte) in input.iter_indices().take(bound) {
+ res += (byte as u32) << (8 * index);
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a little endian unsigned 8 bytes integer
+/// Recognizes a little endian unsigned 8 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u64;
///
/// let parser = |s| {
-/// le_u64::<(_, ErrorKind)>(s)
+/// le_u64::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
- if i.len() < 8 {
- Err(Err::Incomplete(Needed::Size(8)))
+pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 8;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24)
- + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64;
- Ok((&i[8..], res))
+ let mut res = 0u64;
+ for (index, byte) in input.iter_indices().take(bound) {
+ res += (byte as u64) << (8 * index);
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a little endian unsigned 16 bytes integer
+/// Recognizes a little endian unsigned 16 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_u128;
///
/// let parser = |s| {
-/// le_u128::<(_, ErrorKind)>(s)
+/// le_u128::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
#[cfg(stable_i128)]
-pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
- if i.len() < 16 {
- Err(Err::Incomplete(Needed::Size(16)))
+pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 16;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
- let res = ((i[15] as u128) << 120)
- + ((i[14] as u128) << 112)
- + ((i[13] as u128) << 104)
- + ((i[12] as u128) << 96)
- + ((i[11] as u128) << 88)
- + ((i[10] as u128) << 80)
- + ((i[9] as u128) << 72)
- + ((i[8] as u128) << 64)
- + ((i[7] as u128) << 56)
- + ((i[6] as u128) << 48)
- + ((i[5] as u128) << 40)
- + ((i[4] as u128) << 32)
- + ((i[3] as u128) << 24)
- + ((i[2] as u128) << 16)
- + ((i[1] as u128) << 8)
- + i[0] as u128;
- Ok((&i[16..], res))
+ let mut res = 0u128;
+ for (index, byte) in input.iter_indices().take(bound) {
+ res += (byte as u128) << (8 * index);
+ }
+
+ Ok((input.slice(bound..), res))
}
}
-/// Recognizes a signed 1 byte integer
+/// Recognizes a signed 1 byte integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i8;
///
-/// let parser = le_i8::<(_, ErrorKind)>;
+/// let parser = le_i8::<_, (_, ErrorKind)>;
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
-/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
- map!(i, le_u8, |x| x as i8)
+pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, le_u8, |x| x as i8)
}
-/// Recognizes a little endian signed 2 bytes integer
+/// Recognizes a little endian signed 2 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i16;
///
/// let parser = |s| {
-/// le_i16::<(_, ErrorKind)>(s)
+/// le_i16::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
+/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
/// ```
#[inline]
-pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
- map!(i, le_u16, |x| x as i16)
+pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, le_u16, |x| x as i16)
}
-/// Recognizes a little endian signed 3 bytes integer
+/// Recognizes a little endian signed 3 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i24;
///
/// let parser = |s| {
-/// le_i24::<(_, ErrorKind)>(s)
+/// le_i24::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
+/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
/// ```
#[inline]
-pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
+pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
// Same as the unsigned version but we need to sign-extend manually here
- map!(i, le_u24, |x| if x & 0x80_00_00 != 0 {
+ map!(input, le_u24, |x| if x & 0x80_00_00 != 0 {
(x | 0xff_00_00_00) as i32
} else {
x as i32
})
}
-/// Recognizes a little endian signed 4 bytes integer
+/// Recognizes a little endian signed 4 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i32;
///
/// let parser = |s| {
-/// le_i32::<(_, ErrorKind)>(s)
+/// le_i32::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
- map!(i, le_u32, |x| x as i32)
+pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, le_u32, |x| x as i32)
}
-/// Recognizes a little endian signed 8 bytes integer
+/// Recognizes a little endian signed 8 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i64;
///
/// let parser = |s| {
-/// le_i64::<(_, ErrorKind)>(s)
+/// le_i64::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
- map!(i, le_u64, |x| x as i64)
+pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, le_u64, |x| x as i64)
}
-/// Recognizes a little endian signed 16 bytes integer
+/// Recognizes a little endian signed 16 bytes integer.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_i128;
///
/// let parser = |s| {
-/// le_i128::<(_, ErrorKind)>(s)
+/// le_i128::<_, (_, ErrorKind)>(s)
/// };
///
-/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
-/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
+/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
+/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
#[cfg(stable_i128)]
-pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
- map!(i, le_u128, |x| x as i128)
+pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(input, le_u128, |x| x as i128)
+}
+
+/// Recognizes an unsigned 1 byte integer
+///
+/// Note that endianness does not apply to 1 byte numbers.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u8;
+///
+/// let parser = |s| {
+/// u8::<_, (_, ErrorKind)>(s)
+/// };
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
+/// ```
+#[inline]
+pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ let bound: usize = 1;
+ if input.input_len() < bound {
+ Err(Err::Incomplete(Needed::new(1)))
+ } else {
+ let res = input.iter_elements().next().unwrap();
+
+ Ok((input.slice(bound..), res))
+ }
+}
+
+/// Recognizes an unsigned 2 bytes integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u16 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian u16 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+///
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u16;
+///
+/// let be_u16 = |s| {
+/// u16::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(be_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
+///
+/// let le_u16 = |s| {
+/// u16::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(le_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
+/// ```
+#[inline]
+pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_u16,
+ crate::number::Endianness::Little => le_u16,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_u16,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_u16,
+ }
+}
+
+/// Recognizes an unsigned 3 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u24 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian u24 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u24;
+///
+/// let be_u24 = |s| {
+/// u24::<_,(_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(be_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
+///
+/// let le_u24 = |s| {
+/// u24::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(le_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
+/// ```
+#[inline]
+pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_u24,
+ crate::number::Endianness::Little => le_u24,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_u24,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_u24,
+ }
+}
+
+/// Recognizes an unsigned 4 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u32 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian u32 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u32;
+///
+/// let be_u32 = |s| {
+/// u32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(be_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
+///
+/// let le_u32 = |s| {
+/// u32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(le_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
+/// ```
+#[inline]
+pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_u32,
+ crate::number::Endianness::Little => le_u32,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_u32,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_u32,
+ }
+}
+
+/// Recognizes an unsigned 8 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u64 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian u64 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u64;
+///
+/// let be_u64 = |s| {
+/// u64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(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(Err::Incomplete(Needed::new(7))));
+///
+/// let le_u64 = |s| {
+/// u64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(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(Err::Incomplete(Needed::new(7))));
+/// ```
+#[inline]
+pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_u64,
+ crate::number::Endianness::Little => le_u64,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_u64,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_u64,
+ }
+}
+
+/// Recognizes an unsigned 16 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u128 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian u128 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::u128;
+///
+/// let be_u128 = |s| {
+/// u128::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(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(Err::Incomplete(Needed::new(15))));
+///
+/// let le_u128 = |s| {
+/// u128::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(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(Err::Incomplete(Needed::new(15))));
+/// ```
+#[inline]
+#[cfg(stable_i128)]
+pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_u128,
+ crate::number::Endianness::Little => le_u128,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_u128,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_u128,
+ }
+}
+
+/// Recognizes a signed 1 byte integer
+///
+/// Note that endianness does not apply to 1 byte numbers.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::i8;
+///
+/// let parser = |s| {
+/// i8::<_, (_, ErrorKind)>(s)
+/// };
+///
+/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
+/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
+/// ```
+#[inline]
+pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ map!(i, u8, |x| x as i8)
}
-/// Recognizes a big endian 4 bytes floating point number
+/// Recognizes a signed 2 byte integer
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i16 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian i16 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
/// # use nom::Needed::Size;
+/// use nom::number::streaming::i16;
+///
+/// let be_i16 = |s| {
+/// i16::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
+/// assert_eq!(be_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
+///
+/// let le_i16 = |s| {
+/// i16::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
+/// assert_eq!(le_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
+/// ```
+#[inline]
+pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_i16,
+ crate::number::Endianness::Little => le_i16,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_i16,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_i16,
+ }
+}
+
+/// Recognizes a signed 3 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i24 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian i24 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::i24;
+///
+/// let be_i24 = |s| {
+/// i24::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
+/// assert_eq!(be_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
+///
+/// let le_i24 = |s| {
+/// i24::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
+/// assert_eq!(le_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
+/// ```
+#[inline]
+pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_i24,
+ crate::number::Endianness::Little => le_i24,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_i24,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_i24,
+ }
+}
+
+/// Recognizes a signed 4 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i32 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian i32 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::i32;
+///
+/// let be_i32 = |s| {
+/// i32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
+/// assert_eq!(be_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
+///
+/// let le_i32 = |s| {
+/// i32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
+/// assert_eq!(le_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
+/// ```
+#[inline]
+pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_i32,
+ crate::number::Endianness::Little => le_i32,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_i32,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_i32,
+ }
+}
+
+/// Recognizes a signed 8 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i64 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian i64 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::i64;
+///
+/// let be_i64 = |s| {
+/// i64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(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(Err::Incomplete(Needed::new(7))));
+///
+/// let le_i64 = |s| {
+/// i64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(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(Err::Incomplete(Needed::new(7))));
+/// ```
+#[inline]
+pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_i64,
+ crate::number::Endianness::Little => le_i64,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_i64,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_i64,
+ }
+}
+
+/// Recognizes a signed 16 byte integer
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i128 integer,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian i128 integer.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::i128;
+///
+/// let be_i128 = |s| {
+/// i128::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(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(Err::Incomplete(Needed::new(15))));
+///
+/// let le_i128 = |s| {
+/// i128::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(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(Err::Incomplete(Needed::new(15))));
+/// ```
+#[inline]
+#[cfg(stable_i128)]
+pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_i128,
+ crate::number::Endianness::Little => le_i128,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_i128,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_i128,
+ }
+}
+
+/// Recognizes a big endian 4 bytes floating point number.
+///
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
/// use nom::number::streaming::be_f32;
///
/// let parser = |s| {
-/// be_f32::<(_, ErrorKind)>(s)
+/// be_f32::<_, (_, ErrorKind)>(s)
/// };
///
/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625)));
-/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
+pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
match be_u32(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f32::from_bits(o))),
}
}
-/// Recognizes a big endian 8 bytes floating point number
+/// Recognizes a big endian 8 bytes floating point number.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::be_f64;
///
/// let parser = |s| {
-/// be_f64::<(_, ErrorKind)>(s)
+/// be_f64::<_, (_, ErrorKind)>(s)
/// };
///
/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
-/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
+pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
match be_u64(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f64::from_bits(o))),
}
}
-/// Recognizes a little endian 4 bytes floating point number
+/// Recognizes a little endian 4 bytes floating point number.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_f32;
///
/// let parser = |s| {
-/// le_f32::<(_, ErrorKind)>(s)
+/// le_f32::<_, (_, ErrorKind)>(s)
/// };
///
/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
-/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4))));
+/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
/// ```
#[inline]
-pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
+pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
match le_u32(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f32::from_bits(o))),
}
}
-/// Recognizes a little endian 8 bytes floating point number
+/// Recognizes a little endian 8 bytes floating point number.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::le_f64;
///
/// let parser = |s| {
-/// le_f64::<(_, ErrorKind)>(s)
+/// le_f64::<_, (_, ErrorKind)>(s)
/// };
///
/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0)));
-/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8))));
+/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
/// ```
#[inline]
-pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
+pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
match le_u64(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f64::from_bits(o))),
}
}
-/// Recognizes a hex-encoded integer
+/// Recognizes a 4 byte floating point number
+///
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian f32 float,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian f32 float.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::number::streaming::f32;
+///
+/// let be_f32 = |s| {
+/// f32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(be_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
+///
+/// let le_f32 = |s| {
+/// f32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(le_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
+/// ```
+#[inline]
+pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_f32,
+ crate::number::Endianness::Little => le_f32,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_f32,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_f32,
+ }
+}
+
+/// Recognizes an 8 byte floating point number
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
+/// If the parameter is `nom::number::Endianness::Big`, parse a big endian f64 float,
+/// otherwise if `nom::number::Endianness::Little` parse a little endian f64 float.
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
/// # use nom::Needed::Size;
+/// use nom::number::streaming::f64;
+///
+/// let be_f64 = |s| {
+/// f64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
+/// };
+///
+/// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(be_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
+///
+/// let le_f64 = |s| {
+/// f64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
+/// };
+///
+/// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
+/// assert_eq!(le_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
+/// ```
+#[inline]
+pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
+where
+ I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
+{
+ match endian {
+ crate::number::Endianness::Big => be_f64,
+ crate::number::Endianness::Little => le_f64,
+ #[cfg(target_endian = "big")]
+ crate::number::Endianness::Native => be_f64,
+ #[cfg(target_endian = "little")]
+ crate::number::Endianness::Native => le_f64,
+ }
+}
+
+/// Recognizes a hex-encoded integer.
+///
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
+/// ```rust
+/// # use nom::{Err, error::ErrorKind, Needed};
/// use nom::number::streaming::hex_u32;
///
/// let parser = |s| {
@@ -696,11 +1303,11 @@ pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f
/// };
///
/// assert_eq!(parser(b"01AE;"), Ok((&b";"[..], 0x01AE)));
-/// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::Size(1))));
+/// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::new(1))));
/// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
/// ```
#[inline]
-pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> {
+pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u32, E> {
let (i, o) = crate::bytes::streaming::is_a(&b"0123456789abcdefABCDEF"[..])(input)?;
// Do not parse more than 8 characters for a u32
@@ -723,13 +1330,12 @@ pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8],
Ok((remaining, res))
}
-/// Recognizes a floating point number in text format and returns the corresponding part of the input
+/// Recognizes a floating point number in text format and returns the corresponding part of the input.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
///
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::recognize_float;
///
/// let parser = |s| {
@@ -742,14 +1348,14 @@ pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8],
/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
/// ```
#[allow(unused_imports)]
-#[cfg_attr(rustfmt, rustfmt_skip)]
+#[rustfmt::skip]
pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
T: Clone + Offset,
T: InputIter,
<T as InputIter>::Item: AsChar,
- T: InputTakeAtPosition,
+ T: InputTakeAtPosition + InputLength,
<T as InputTakeAtPosition>::Item: AsChar
{
recognize(
@@ -768,12 +1374,11 @@ where
)(input)
}
-/// Recognizes floating point number in a byte string and returns a f32
+/// Recognizes floating point number in a byte string and returns a `f32`.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::float;
///
/// let parser = |s| {
@@ -786,30 +1391,29 @@ where
/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
/// ```
#[cfg(not(feature = "lexical"))]
-pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
+pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
where
T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
T: Clone + Offset,
T: InputIter + InputLength + crate::traits::ParseTo<f32>,
<T as InputIter>::Item: AsChar,
T: InputTakeAtPosition,
- <T as InputTakeAtPosition>::Item: AsChar
+ <T as InputTakeAtPosition>::Item: AsChar,
{
match recognize_float(input) {
Err(e) => Err(e),
Ok((i, s)) => match s.parse_to() {
Some(n) => Ok((i, n)),
- None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
- }
+ None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))),
+ },
}
}
-/// Recognizes floating point number in a byte string and returns a f32
+/// Recognizes floating point number in a byte string and returns a `f32`.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::float;
///
/// let parser = |s| {
@@ -825,7 +1429,7 @@ where
/// this function uses the lexical-core crate for float parsing by default, you
/// can deactivate it by removing the "lexical" feature
#[cfg(feature = "lexical")]
-pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
+pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
where
T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
{
@@ -836,17 +1440,16 @@ where
} else {
Ok((input.slice(processed..), value))
}
- },
- Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
+ }
+ Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))),
}
}
-/// Recognizes floating point number in a byte string and returns a f64
+/// Recognizes floating point number in a byte string and returns a `f64`.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::double;
///
/// let parser = |s| {
@@ -859,30 +1462,29 @@ where
/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
/// ```
#[cfg(not(feature = "lexical"))]
-pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
+pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
where
T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
T: Clone + Offset,
T: InputIter + InputLength + crate::traits::ParseTo<f64>,
<T as InputIter>::Item: AsChar,
T: InputTakeAtPosition,
- <T as InputTakeAtPosition>::Item: AsChar
+ <T as InputTakeAtPosition>::Item: AsChar,
{
match recognize_float(input) {
Err(e) => Err(e),
Ok((i, s)) => match s.parse_to() {
Some(n) => Ok((i, n)),
- None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
- }
+ None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))),
+ },
}
}
-/// Recognizes floating point number in a byte string and returns a f64
+/// Recognizes floating point number in a byte string and returns a `f64`.
///
-/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
+/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed};
-/// # use nom::Needed::Size;
/// use nom::number::streaming::double;
///
/// let parser = |s| {
@@ -898,7 +1500,7 @@ where
/// this function uses the lexical-core crate for float parsing by default, you
/// can deactivate it by removing the "lexical" feature
#[cfg(feature = "lexical")]
-pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
+pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
where
T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
{
@@ -909,16 +1511,16 @@ where
} else {
Ok((input.slice(processed..), value))
}
- },
- Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
+ }
+ Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))),
}
}
#[cfg(test)]
mod tests {
use super::*;
- use crate::internal::{Err, Needed};
use crate::error::ErrorKind;
+ use crate::internal::{Err, Needed};
macro_rules! assert_parse(
($left: expr, $right: expr) => {
@@ -929,129 +1531,281 @@ mod tests {
#[test]
fn 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)));
+ 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)));
+ assert_parse!(be_i8(&[][..]), Err(Err::Incomplete(Needed::new(1))));
}
#[test]
fn 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)));
+ 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)));
+ assert_parse!(be_i16(&[][..]), Err(Err::Incomplete(Needed::new(2))));
+ assert_parse!(be_i16(&[0x00][..]), Err(Err::Incomplete(Needed::new(1))));
}
#[test]
fn 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)));
+ 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))
+ );
+ assert_parse!(be_u24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
+ assert_parse!(be_u24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
+ assert_parse!(
+ be_u24(&[0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(1)))
+ );
}
#[test]
fn 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)));
+ 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))
+ );
+ assert_parse!(be_i24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
+ assert_parse!(be_i24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
+ assert_parse!(
+ be_i24(&[0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(1)))
+ );
}
#[test]
fn i32_tests() {
- assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
+ assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
- be_i32(&[0x7f, 0xff, 0xff, 0xff]),
+ 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(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
assert_parse!(
- be_i32(&[0x80, 0x00, 0x00, 0x00]),
+ be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
+ assert_parse!(be_i32(&[][..]), Err(Err::Incomplete(Needed::new(4))));
+ assert_parse!(be_i32(&[0x00][..]), Err(Err::Incomplete(Needed::new(3))));
+ assert_parse!(
+ be_i32(&[0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i32(&[0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(1)))
+ );
}
#[test]
fn i64_tests() {
assert_parse!(
- be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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]),
+ 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]),
+ 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]),
+ be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
+ assert_parse!(be_i64(&[][..]), Err(Err::Incomplete(Needed::new(8))));
+ assert_parse!(be_i64(&[0x00][..]), Err(Err::Incomplete(Needed::new(7))));
+ assert_parse!(
+ be_i64(&[0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(6)))
+ );
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(5)))
+ );
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(4)))
+ );
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(1)))
+ );
}
#[test]
#[cfg(stable_i128)]
fn i128_tests() {
assert_parse!(
- be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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))
+ 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]),
+ 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))
+ 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
+ ))
+ );
+ assert_parse!(be_i128(&[][..]), Err(Err::Incomplete(Needed::new(16))));
+ assert_parse!(be_i128(&[0x00][..]), Err(Err::Incomplete(Needed::new(15))));
+ assert_parse!(
+ be_i128(&[0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(14)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(13)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(12)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(11)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(10)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(9)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(8)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(7)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(6)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(5)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(4)))
+ );
+ assert_parse!(
+ be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
+ Err(Err::Incomplete(Needed::new(3)))
+ );
+ assert_parse!(
+ be_i128(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
+ ),
+ Err(Err::Incomplete(Needed::new(2)))
+ );
+ assert_parse!(
+ be_i128(
+ &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
+ [..]
+ ),
+ Err(Err::Incomplete(Needed::new(1)))
);
}
#[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)));
+ 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)));
+ 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)));
+ 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)));
+ 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(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
- le_i32(&[0xff, 0xff, 0xff, 0x7f]),
+ 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(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
assert_parse!(
- le_i32(&[0x00, 0x00, 0x00, 0x80]),
+ le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
}
@@ -1059,19 +1813,19 @@ mod tests {
#[test]
fn le_i64_tests() {
assert_parse!(
- le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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]),
+ 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]),
+ 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]),
+ le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
}
@@ -1080,28 +1834,54 @@ mod tests {
#[cfg(stable_i128)]
fn le_i128_tests() {
assert_parse!(
- le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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))
+ 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]),
+ 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))
+ 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(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
assert_parse!(
- be_f32(&[0x4d, 0x31, 0x1f, 0xd8]),
+ be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
@@ -1109,20 +1889,20 @@ mod tests {
#[test]
fn be_f64_tests() {
assert_parse!(
- be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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]),
+ 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(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
assert_parse!(
- le_f32(&[0xd8, 0x1f, 0x31, 0x4d]),
+ le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
@@ -1130,11 +1910,11 @@ mod tests {
#[test]
fn le_f64_tests() {
assert_parse!(
- le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
+ 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]),
+ le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
Ok((&b""[..], 185_728_392_f64))
);
}
@@ -1156,7 +1936,7 @@ mod tests {
);
assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
- assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::Size(1))));
+ assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::new(1))));
}
#[test]
@@ -1200,8 +1980,7 @@ mod tests {
let remaining_exponent = "-1.234E-";
assert_parse!(
recognize_float(remaining_exponent),
- Err(Err::Incomplete(Needed::Size(1)))
+ Err(Err::Incomplete(Needed::new(1)))
);
}
-
}