use crate::branch::alt;
use crate::bytes::streaming::tag;
use crate::character::streaming::{char, digit1, sign};
use crate::combinator::{cut, map, opt, recognize};
use crate::error::{ErrorKind, ParseError};
use crate::lib::std::ops::{Add, Shl};
use crate::sequence::pair;
use crate::traits::{AsBytes, AsChar, Compare, Offset};
use crate::{internal::*, Input};
#[inline]
pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
where
I: Input<Item = u8>,
{
be_uint(input, 1)
}
#[inline]
pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
where
I: Input<Item = u8>,
{
be_uint(input, 2)
}
#[inline]
pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
be_uint(input, 3)
}
#[inline]
pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
be_uint(input, 4)
}
#[inline]
pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
where
I: Input<Item = u8>,
{
be_uint(input, 8)
}
#[inline]
pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Input<Item = u8>,
{
be_uint(input, 16)
}
#[inline]
fn be_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
where
I: Input<Item = u8>,
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
{
super::be_uint(bound).parse(input)
}
#[inline]
pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
where
I: Input<Item = u8>,
{
be_u8.map(|x| x as i8).parse(input)
}
#[inline]
pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
where
I: Input<Item = u8>,
{
be_u16.map(|x| x as i16).parse(input)
}
#[inline]
pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
be_u24
.map(|x| {
if x & 0x80_00_00 != 0 {
(x | 0xff_00_00_00) as i32
} else {
x as i32
}
})
.parse(input)
}
#[inline]
pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
be_u32.map(|x| x as i32).parse(input)
}
#[inline]
pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
where
I: Input<Item = u8>,
{
be_u64.map(|x| x as i64).parse(input)
}
#[inline]
pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Input<Item = u8>,
{
be_u128.map(|x| x as i128).parse(input)
}
#[inline]
pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
where
I: Input<Item = u8>,
{
le_uint(input, 1)
}
#[inline]
pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
where
I: Input<Item = u8>,
{
le_uint(input, 2)
}
#[inline]
pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
le_uint(input, 3)
}
#[inline]
pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
le_uint(input, 4)
}
#[inline]
pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
where
I: Input<Item = u8>,
{
le_uint(input, 8)
}
#[inline]
pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Input<Item = u8>,
{
le_uint(input, 16)
}
#[inline]
fn le_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
where
I: Input<Item = u8>,
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
{
super::le_uint(bound).parse(input)
}
#[inline]
pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
where
I: Input<Item = u8>,
{
le_u8.map(|x| x as i8).parse(input)
}
#[inline]
pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
where
I: Input<Item = u8>,
{
le_u16.map(|x| x as i16).parse(input)
}
#[inline]
pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
le_u24
.map(|x| {
if x & 0x80_00_00 != 0 {
(x | 0xff_00_00_00) as i32
} else {
x as i32
}
})
.parse(input)
}
#[inline]
pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
le_u32.map(|x| x as i32).parse(input)
}
#[inline]
pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
where
I: Input<Item = u8>,
{
le_u64.map(|x| x as i64).parse(input)
}
#[inline]
pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Input<Item = u8>,
{
le_u128.map(|x| x as i128).parse(input)
}
#[inline]
pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
where
I: Input<Item = u8>,
{
super::u8().parse(input)
}
#[inline]
pub fn u16<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, u16, E>
where
I: Input<Item = u8>,
{
move |input| super::u16(endian).parse(input)
}
#[inline]
pub fn u24<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
move |input| super::u24(endian).parse(input)
}
#[inline]
pub fn u32<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, u32, E>
where
I: Input<Item = u8>,
{
move |input| super::u32(endian).parse(input)
}
#[inline]
pub fn u64<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, u64, E>
where
I: Input<Item = u8>,
{
move |input| super::u64(endian).parse(input)
}
#[inline]
pub fn u128<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, u128, E>
where
I: Input<Item = u8>,
{
move |input| super::u128(endian).parse(input)
}
#[inline]
pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
where
I: Input<Item = u8>,
{
super::u8().map(|x| x as i8).parse(i)
}
#[inline]
pub fn i16<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, i16, E>
where
I: Input<Item = u8>,
{
move |input| super::i16(endian).parse(input)
}
#[inline]
pub fn i24<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
move |input| super::i24(endian).parse(input)
}
#[inline]
pub fn i32<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, i32, E>
where
I: Input<Item = u8>,
{
move |input| super::i32(endian).parse(input)
}
#[inline]
pub fn i64<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, i64, E>
where
I: Input<Item = u8>,
{
move |input| super::i64(endian).parse(input)
}
#[inline]
pub fn i128<I, E: ParseError<I>>(
endian: crate::number::Endianness,
) -> impl Fn(I) -> IResult<I, i128, E>
where
I: Input<Item = u8>,
{
move |input| super::i128(endian).parse(input)
}
#[inline]
pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
where
I: Input<Item = u8>,
{
match be_u32(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f32::from_bits(o))),
}
}
#[inline]
pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
where
I: Input<Item = u8>,
{
match be_u64(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f64::from_bits(o))),
}
}
#[inline]
pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
where
I: Input<Item = u8>,
{
match le_u32(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f32::from_bits(o))),
}
}
#[inline]
pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
where
I: Input<Item = u8>,
{
match le_u64(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((i, f64::from_bits(o))),
}
}
#[inline]
pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
where
I: Input<Item = u8>,
{
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,
}
}
#[inline]
pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
where
I: Input<Item = u8>,
{
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,
}
}
#[inline]
pub fn hex_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
where
I: Input + AsBytes,
<I as Input>::Item: AsChar,
{
let e: ErrorKind = ErrorKind::IsA;
let (i, o) = input.split_at_position1(
|c| {
let c = c.as_char();
!"0123456789abcdefABCDEF".contains(c)
},
e,
)?;
let (remaining, parsed) = if o.input_len() <= 8 {
(i, o)
} else {
input.take_split(8)
};
let res = parsed
.as_bytes()
.iter()
.rev()
.enumerate()
.map(|(k, &v)| {
let digit = v as char;
digit.to_digit(16).unwrap_or(0) << (k * 4)
})
.sum();
Ok((remaining, res))
}
#[rustfmt::skip]
pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: Clone + Offset,
T: Input,
<T as Input>::Item: AsChar,
{
recognize((
opt(alt((char('+'), char('-')))),
alt((
map((digit1, opt(pair(char('.'), opt(digit1)))), |_| ()),
map((char('.'), digit1), |_| ())
)),
opt((
alt((char('e'), char('E'))),
opt(alt((char('+'), char('-')))),
cut(digit1)
))
)).parse(input)
}
#[doc(hidden)]
pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: Clone + Offset,
T: Input + Compare<&'static str>,
<T as Input>::Item: AsChar,
{
alt((
|i: T| {
recognize_float::<_, E>(i.clone()).map_err(|e| match e {
crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
})
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("nan")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
))
.parse(input)
}
pub fn recognize_float_parts<T, E: ParseError<T>>(input: T) -> IResult<T, (bool, T, T, i32), E>
where
T: Clone + Offset,
T: Input,
<T as Input>::Item: AsChar,
T: for<'a> Compare<&'a [u8]>,
T: AsBytes,
{
let (i, sign) = sign(input.clone())?;
let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
Some(index) => i.take_split(index),
None => i.take_split(i.input_len()),
};
let (i, mut integer) = match i
.as_bytes()
.iter()
.position(|c| !(*c >= b'0' && *c <= b'9'))
{
Some(index) => i.take_split(index),
None => i.take_split(i.input_len()),
};
if integer.input_len() == 0 && zeroes.input_len() > 0 {
integer = zeroes.take_from(zeroes.input_len() - 1);
}
let (i, opt_dot) = opt(tag(&b"."[..])).parse(i)?;
let (i, fraction) = if opt_dot.is_none() {
let i2 = i.clone();
(i2, i.take(0))
} else {
let mut zero_count = 0usize;
let mut position = None;
for (pos, c) in i.as_bytes().iter().enumerate() {
if *c >= b'0' && *c <= b'9' {
if *c == b'0' {
zero_count += 1;
} else {
zero_count = 0;
}
} else {
position = Some(pos);
break;
}
}
let position = match position {
Some(p) => p,
None => return Err(Err::Incomplete(Needed::new(1))),
};
let index = if zero_count == 0 {
position
} else if zero_count == position {
position - zero_count + 1
} else {
position - zero_count
};
(i.take_from(position), i.take(index))
};
if integer.input_len() == 0 && fraction.input_len() == 0 {
return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
}
let i2 = i.clone();
let (i, e) = match i.as_bytes().iter().next() {
Some(b'e') => (i.take_from(1), true),
Some(b'E') => (i.take_from(1), true),
_ => (i, false),
};
let (i, exp) = if e {
cut(crate::character::streaming::i32).parse(i)?
} else {
(i2, 0)
};
Ok((i, (sign, integer, fraction, exp)))
}
pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f32> + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
{
let (i, s) = recognize_float_or_exceptions(input)?;
match s.parse_to() {
Some(f) => Ok((i, f)),
None => Err(crate::Err::Error(E::from_error_kind(
i,
crate::error::ErrorKind::Float,
))),
}
}
pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f64> + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
{
let (i, s) = recognize_float_or_exceptions(input)?;
match s.parse_to() {
Some(f) => Ok((i, f)),
None => Err(crate::Err::Error(E::from_error_kind(
i,
crate::error::ErrorKind::Float,
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::ErrorKind;
use crate::internal::{Err, Needed};
use proptest::prelude::*;
macro_rules! assert_parse(
($left: expr, $right: expr) => {
let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
assert_eq!(res, $right);
};
);
#[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(&[][..]), 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(&[][..]), 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(&[][..]), 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(&[][..]), 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(&[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))
);
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][..]),
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))
);
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]
fn 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
))
);
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)));
}
#[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_u16_test() {
assert_parse!(le_u16(&[0x00, 0x03][..]), Ok((&b""[..], 0x0300)));
assert_parse!(le_u16(&[b'a', b'b'][..]), Ok((&b""[..], 0x6261)));
assert_parse!(le_u16(&[0x01][..]), Err(Err::Incomplete(Needed::new(1))));
}
#[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_u32_test() {
assert_parse!(
le_u32(&[0x00, 0x03, 0x05, 0x07][..]),
Ok((&b""[..], 0x07050300))
);
assert_parse!(
le_u32(&[b'a', b'b', b'c', b'd'][..]),
Ok((&b""[..], 0x64636261))
);
assert_parse!(le_u32(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
}
#[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_392_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_392_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 hex_u32_tests() {
assert_parse!(
hex_u32(&b";"[..]),
Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
);
assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
assert_parse!(
hex_u32(&b"c5a31be201;"[..]),
Ok((&b"01;"[..], 3_315_801_058))
);
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::new(1))));
}
#[test]
#[cfg(feature = "std")]
fn float_test() {
let mut test_cases = vec![
"+3.14",
"3.14",
"-3.14",
"0",
"0.0",
"1.",
".789",
"-.5",
"1e7",
"-1E-7",
".3e-2",
"1.e4",
"1.2e4",
"12.34",
"-1.234E-12",
"-1.234e-12",
"0.00000000000000000087",
];
for test in test_cases.drain(..) {
let expected32 = str::parse::<f32>(test).unwrap();
let expected64 = str::parse::<f64>(test).unwrap();
println!("now parsing: {} -> {}", test, expected32);
let larger = format!("{};", test);
assert_parse!(recognize_float(&larger[..]), Ok((";", test)));
assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32)));
assert_parse!(float(&larger[..]), Ok((";", expected32)));
assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64)));
assert_parse!(double(&larger[..]), Ok((";", expected64)));
}
let remaining_exponent = "-1.234E-";
assert_parse!(
recognize_float(remaining_exponent),
Err(Err::Incomplete(Needed::new(1)))
);
let (_i, nan) = float::<_, ()>("NaN").unwrap();
assert!(nan.is_nan());
let (_i, inf) = float::<_, ()>("inf").unwrap();
assert!(inf.is_infinite());
let (i, inf) = float::<_, ()>("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());
}
#[test]
fn configurable_endianness() {
use crate::number::Endianness;
fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Big)(i)
}
fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Little)(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)(i)
}
fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
u32(Endianness::Little)(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)(i)
}
fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
u64(Endianness::Little)(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)(i)
}
fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
i16(Endianness::Little)(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)(i)
}
fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
i32(Endianness::Little)(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)(i)
}
fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
i64(Endianness::Little)(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))
);
}
#[cfg(feature = "std")]
fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
use crate::traits::ParseTo;
match recognize_float_or_exceptions(i) {
Err(e) => Err(e),
Ok((i, s)) => {
if s.is_empty() {
return Err(Err::Error(()));
}
match s.parse_to() {
Some(n) => Ok((i, n)),
None => Err(Err::Error(())),
}
}
}
}
proptest! {
#[test]
#[cfg(feature = "std")]
fn floats(s in "\\PC*") {
println!("testing {}", s);
let res1 = parse_f64(&s);
let res2 = double::<_, ()>(s.as_str());
assert_eq!(res1, res2);
}
}
}