1use crate::branch::alt;
4use crate::bytes::streaming::tag;
5use crate::character::streaming::{char, digit1, sign};
6use crate::combinator::{cut, map, opt, recognize};
7use crate::error::{ErrorKind, ParseError};
8use crate::internal::*;
9use crate::lib::std::ops::{RangeFrom, RangeTo};
10use crate::sequence::{pair, tuple};
11use crate::traits::{
12 AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, Offset, Slice,
13};
14
15#[inline]
30pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
31where
32 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
33{
34 let bound: usize = 1;
35 if input.input_len() < bound {
36 Err(Err::Incomplete(Needed::new(1)))
37 } else {
38 let res = input.iter_elements().next().unwrap();
39
40 Ok((input.slice(bound..), res))
41 }
42}
43
44#[inline]
60pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
61where
62 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
63{
64 let bound: usize = 2;
65 if input.input_len() < bound {
66 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
67 } else {
68 let mut res = 0u16;
69 for byte in input.iter_elements().take(bound) {
70 res = (res << 8) + byte as u16;
71 }
72
73 Ok((input.slice(bound..), res))
74 }
75}
76
77#[inline]
93pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
94where
95 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
96{
97 let bound: usize = 3;
98 if input.input_len() < bound {
99 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
100 } else {
101 let mut res = 0u32;
102 for byte in input.iter_elements().take(bound) {
103 res = (res << 8) + byte as u32;
104 }
105
106 Ok((input.slice(bound..), res))
107 }
108}
109
110#[inline]
126pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
127where
128 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
129{
130 let bound: usize = 4;
131 if input.input_len() < bound {
132 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
133 } else {
134 let mut res = 0u32;
135 for byte in input.iter_elements().take(bound) {
136 res = (res << 8) + byte as u32;
137 }
138
139 Ok((input.slice(bound..), res))
140 }
141}
142
143#[inline]
159pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
160where
161 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
162{
163 let bound: usize = 8;
164 if input.input_len() < bound {
165 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
166 } else {
167 let mut res = 0u64;
168 for byte in input.iter_elements().take(bound) {
169 res = (res << 8) + byte as u64;
170 }
171
172 Ok((input.slice(bound..), res))
173 }
174}
175
176#[inline]
191pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
192where
193 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
194{
195 let bound: usize = 16;
196 if input.input_len() < bound {
197 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
198 } else {
199 let mut res = 0u128;
200 for byte in input.iter_elements().take(bound) {
201 res = (res << 8) + byte as u128;
202 }
203
204 Ok((input.slice(bound..), res))
205 }
206}
207
208#[inline]
221pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
222where
223 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
224{
225 be_u8.map(|x| x as i8).parse(input)
226}
227
228#[inline]
241pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
242where
243 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
244{
245 be_u16.map(|x| x as i16).parse(input)
246}
247
248#[inline]
261pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
262where
263 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
264{
265 be_u24
267 .map(|x| {
268 if x & 0x80_00_00 != 0 {
269 (x | 0xff_00_00_00) as i32
270 } else {
271 x as i32
272 }
273 })
274 .parse(input)
275}
276
277#[inline]
290pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
291where
292 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
293{
294 be_u32.map(|x| x as i32).parse(input)
295}
296
297#[inline]
311pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
312where
313 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
314{
315 be_u64.map(|x| x as i64).parse(input)
316}
317
318#[inline]
331pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
332where
333 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
334{
335 be_u128.map(|x| x as i128).parse(input)
336}
337
338#[inline]
351pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
352where
353 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
354{
355 let bound: usize = 1;
356 if input.input_len() < bound {
357 Err(Err::Incomplete(Needed::new(1)))
358 } else {
359 let res = input.iter_elements().next().unwrap();
360
361 Ok((input.slice(bound..), res))
362 }
363}
364
365#[inline]
381pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
382where
383 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
384{
385 let bound: usize = 2;
386 if input.input_len() < bound {
387 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
388 } else {
389 let mut res = 0u16;
390 for (index, byte) in input.iter_indices().take(bound) {
391 res += (byte as u16) << (8 * index);
392 }
393
394 Ok((input.slice(bound..), res))
395 }
396}
397
398#[inline]
414pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
415where
416 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
417{
418 let bound: usize = 3;
419 if input.input_len() < bound {
420 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
421 } else {
422 let mut res = 0u32;
423 for (index, byte) in input.iter_indices().take(bound) {
424 res += (byte as u32) << (8 * index);
425 }
426
427 Ok((input.slice(bound..), res))
428 }
429}
430
431#[inline]
447pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
448where
449 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
450{
451 let bound: usize = 4;
452 if input.input_len() < bound {
453 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
454 } else {
455 let mut res = 0u32;
456 for (index, byte) in input.iter_indices().take(bound) {
457 res += (byte as u32) << (8 * index);
458 }
459
460 Ok((input.slice(bound..), res))
461 }
462}
463
464#[inline]
480pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
481where
482 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
483{
484 let bound: usize = 8;
485 if input.input_len() < bound {
486 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
487 } else {
488 let mut res = 0u64;
489 for (index, byte) in input.iter_indices().take(bound) {
490 res += (byte as u64) << (8 * index);
491 }
492
493 Ok((input.slice(bound..), res))
494 }
495}
496
497#[inline]
513pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
514where
515 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
516{
517 let bound: usize = 16;
518 if input.input_len() < bound {
519 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
520 } else {
521 let mut res = 0u128;
522 for (index, byte) in input.iter_indices().take(bound) {
523 res += (byte as u128) << (8 * index);
524 }
525
526 Ok((input.slice(bound..), res))
527 }
528}
529
530#[inline]
543pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
544where
545 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
546{
547 le_u8.map(|x| x as i8).parse(input)
548}
549
550#[inline]
566pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
567where
568 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
569{
570 le_u16.map(|x| x as i16).parse(input)
571}
572
573#[inline]
589pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
590where
591 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
592{
593 le_u24
595 .map(|x| {
596 if x & 0x80_00_00 != 0 {
597 (x | 0xff_00_00_00) as i32
598 } else {
599 x as i32
600 }
601 })
602 .parse(input)
603}
604
605#[inline]
621pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
622where
623 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
624{
625 le_u32.map(|x| x as i32).parse(input)
626}
627
628#[inline]
644pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
645where
646 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
647{
648 le_u64.map(|x| x as i64).parse(input)
649}
650
651#[inline]
667pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
668where
669 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
670{
671 le_u128.map(|x| x as i128).parse(input)
672}
673
674#[inline]
691pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
692where
693 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
694{
695 let bound: usize = 1;
696 if input.input_len() < bound {
697 Err(Err::Incomplete(Needed::new(1)))
698 } else {
699 let res = input.iter_elements().next().unwrap();
700
701 Ok((input.slice(bound..), res))
702 }
703}
704
705#[inline]
731pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
732where
733 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
734{
735 match endian {
736 crate::number::Endianness::Big => be_u16,
737 crate::number::Endianness::Little => le_u16,
738 #[cfg(target_endian = "big")]
739 crate::number::Endianness::Native => be_u16,
740 #[cfg(target_endian = "little")]
741 crate::number::Endianness::Native => le_u16,
742 }
743}
744
745#[inline]
770pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
771where
772 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
773{
774 match endian {
775 crate::number::Endianness::Big => be_u24,
776 crate::number::Endianness::Little => le_u24,
777 #[cfg(target_endian = "big")]
778 crate::number::Endianness::Native => be_u24,
779 #[cfg(target_endian = "little")]
780 crate::number::Endianness::Native => le_u24,
781 }
782}
783
784#[inline]
809pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
810where
811 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
812{
813 match endian {
814 crate::number::Endianness::Big => be_u32,
815 crate::number::Endianness::Little => le_u32,
816 #[cfg(target_endian = "big")]
817 crate::number::Endianness::Native => be_u32,
818 #[cfg(target_endian = "little")]
819 crate::number::Endianness::Native => le_u32,
820 }
821}
822
823#[inline]
848pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
849where
850 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
851{
852 match endian {
853 crate::number::Endianness::Big => be_u64,
854 crate::number::Endianness::Little => le_u64,
855 #[cfg(target_endian = "big")]
856 crate::number::Endianness::Native => be_u64,
857 #[cfg(target_endian = "little")]
858 crate::number::Endianness::Native => le_u64,
859 }
860}
861
862#[inline]
887pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
888where
889 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
890{
891 match endian {
892 crate::number::Endianness::Big => be_u128,
893 crate::number::Endianness::Little => le_u128,
894 #[cfg(target_endian = "big")]
895 crate::number::Endianness::Native => be_u128,
896 #[cfg(target_endian = "little")]
897 crate::number::Endianness::Native => le_u128,
898 }
899}
900
901#[inline]
918pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
919where
920 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
921{
922 u8.map(|x| x as i8).parse(i)
923}
924
925#[inline]
950pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
951where
952 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
953{
954 match endian {
955 crate::number::Endianness::Big => be_i16,
956 crate::number::Endianness::Little => le_i16,
957 #[cfg(target_endian = "big")]
958 crate::number::Endianness::Native => be_i16,
959 #[cfg(target_endian = "little")]
960 crate::number::Endianness::Native => le_i16,
961 }
962}
963
964#[inline]
989pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
990where
991 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
992{
993 match endian {
994 crate::number::Endianness::Big => be_i24,
995 crate::number::Endianness::Little => le_i24,
996 #[cfg(target_endian = "big")]
997 crate::number::Endianness::Native => be_i24,
998 #[cfg(target_endian = "little")]
999 crate::number::Endianness::Native => le_i24,
1000 }
1001}
1002
1003#[inline]
1028pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1029where
1030 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1031{
1032 match endian {
1033 crate::number::Endianness::Big => be_i32,
1034 crate::number::Endianness::Little => le_i32,
1035 #[cfg(target_endian = "big")]
1036 crate::number::Endianness::Native => be_i32,
1037 #[cfg(target_endian = "little")]
1038 crate::number::Endianness::Native => le_i32,
1039 }
1040}
1041
1042#[inline]
1067pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
1068where
1069 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1070{
1071 match endian {
1072 crate::number::Endianness::Big => be_i64,
1073 crate::number::Endianness::Little => le_i64,
1074 #[cfg(target_endian = "big")]
1075 crate::number::Endianness::Native => be_i64,
1076 #[cfg(target_endian = "little")]
1077 crate::number::Endianness::Native => le_i64,
1078 }
1079}
1080
1081#[inline]
1106pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
1107where
1108 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1109{
1110 match endian {
1111 crate::number::Endianness::Big => be_i128,
1112 crate::number::Endianness::Little => le_i128,
1113 #[cfg(target_endian = "big")]
1114 crate::number::Endianness::Native => be_i128,
1115 #[cfg(target_endian = "little")]
1116 crate::number::Endianness::Native => le_i128,
1117 }
1118}
1119
1120#[inline]
1135pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1136where
1137 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1138{
1139 match be_u32(input) {
1140 Err(e) => Err(e),
1141 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1142 }
1143}
1144
1145#[inline]
1160pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1161where
1162 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1163{
1164 match be_u64(input) {
1165 Err(e) => Err(e),
1166 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1167 }
1168}
1169
1170#[inline]
1185pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1186where
1187 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1188{
1189 match le_u32(input) {
1190 Err(e) => Err(e),
1191 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1192 }
1193}
1194
1195#[inline]
1210pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1211where
1212 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1213{
1214 match le_u64(input) {
1215 Err(e) => Err(e),
1216 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1217 }
1218}
1219
1220#[inline]
1245pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1246where
1247 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1248{
1249 match endian {
1250 crate::number::Endianness::Big => be_f32,
1251 crate::number::Endianness::Little => le_f32,
1252 #[cfg(target_endian = "big")]
1253 crate::number::Endianness::Native => be_f32,
1254 #[cfg(target_endian = "little")]
1255 crate::number::Endianness::Native => le_f32,
1256 }
1257}
1258
1259#[inline]
1284pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1285where
1286 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1287{
1288 match endian {
1289 crate::number::Endianness::Big => be_f64,
1290 crate::number::Endianness::Little => le_f64,
1291 #[cfg(target_endian = "big")]
1292 crate::number::Endianness::Native => be_f64,
1293 #[cfg(target_endian = "little")]
1294 crate::number::Endianness::Native => le_f64,
1295 }
1296}
1297
1298#[inline]
1314pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u32, E> {
1315 let (i, o) = crate::bytes::streaming::is_a(&b"0123456789abcdefABCDEF"[..])(input)?;
1316
1317 let (parsed, remaining) = if o.len() <= 8 {
1319 (o, i)
1320 } else {
1321 (&input[..8], &input[8..])
1322 };
1323
1324 let res = parsed
1325 .iter()
1326 .rev()
1327 .enumerate()
1328 .map(|(k, &v)| {
1329 let digit = v as char;
1330 digit.to_digit(16).unwrap_or(0) << (k * 4)
1331 })
1332 .sum();
1333
1334 Ok((remaining, res))
1335}
1336
1337#[rustfmt::skip]
1355pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
1356where
1357 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1358 T: Clone + Offset,
1359 T: InputIter,
1360 <T as InputIter>::Item: AsChar,
1361 T: InputTakeAtPosition + InputLength,
1362 <T as InputTakeAtPosition>::Item: AsChar
1363{
1364 recognize(
1365 tuple((
1366 opt(alt((char('+'), char('-')))),
1367 alt((
1368 map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
1369 map(tuple((char('.'), digit1)), |_| ())
1370 )),
1371 opt(tuple((
1372 alt((char('e'), char('E'))),
1373 opt(alt((char('+'), char('-')))),
1374 cut(digit1)
1375 )))
1376 ))
1377 )(input)
1378}
1379
1380#[doc(hidden)]
1382pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
1383where
1384 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1385 T: Clone + Offset,
1386 T: InputIter + InputTake + InputLength + Compare<&'static str>,
1387 <T as InputIter>::Item: AsChar,
1388 T: InputTakeAtPosition,
1389 <T as InputTakeAtPosition>::Item: AsChar,
1390{
1391 alt((
1392 |i: T| {
1393 recognize_float::<_, E>(i.clone()).map_err(|e| match e {
1394 crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
1395 crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
1396 crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
1397 })
1398 },
1399 |i: T| {
1400 crate::bytes::streaming::tag_no_case::<_, _, E>("nan")(i.clone())
1401 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1402 },
1403 |i: T| {
1404 crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
1405 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1406 },
1407 |i: T| {
1408 crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
1409 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1410 },
1411 ))(input)
1412}
1413
1414pub fn recognize_float_parts<T, E: ParseError<T>>(input: T) -> IResult<T, (bool, T, T, i32), E>
1422where
1423 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1424 T: Clone + Offset,
1425 T: InputIter + crate::traits::ParseTo<i32>,
1426 <T as InputIter>::Item: AsChar,
1427 T: InputTakeAtPosition + InputTake + InputLength,
1428 <T as InputTakeAtPosition>::Item: AsChar,
1429 T: for<'a> Compare<&'a [u8]>,
1430 T: AsBytes,
1431{
1432 let (i, sign) = sign(input.clone())?;
1433
1434 let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
1436 Some(index) => i.take_split(index),
1437 None => i.take_split(i.input_len()),
1438 };
1439
1440 let (i, mut integer) = match i
1442 .as_bytes()
1443 .iter()
1444 .position(|c| !(*c >= b'0' && *c <= b'9'))
1445 {
1446 Some(index) => i.take_split(index),
1447 None => i.take_split(i.input_len()),
1448 };
1449
1450 if integer.input_len() == 0 && zeroes.input_len() > 0 {
1451 integer = zeroes.slice(zeroes.input_len() - 1..);
1453 }
1454
1455 let (i, opt_dot) = opt(tag(&b"."[..]))(i)?;
1456 let (i, fraction) = if opt_dot.is_none() {
1457 let i2 = i.clone();
1458 (i2, i.slice(..0))
1459 } else {
1460 let mut zero_count = 0usize;
1462 let mut position = None;
1463 for (pos, c) in i.as_bytes().iter().enumerate() {
1464 if *c >= b'0' && *c <= b'9' {
1465 if *c == b'0' {
1466 zero_count += 1;
1467 } else {
1468 zero_count = 0;
1469 }
1470 } else {
1471 position = Some(pos);
1472 break;
1473 }
1474 }
1475
1476 let position = match position {
1477 Some(p) => p,
1478 None => return Err(Err::Incomplete(Needed::new(1))),
1479 };
1480
1481 let index = if zero_count == 0 {
1482 position
1483 } else if zero_count == position {
1484 position - zero_count + 1
1485 } else {
1486 position - zero_count
1487 };
1488
1489 (i.slice(position..), i.slice(..index))
1490 };
1491
1492 if integer.input_len() == 0 && fraction.input_len() == 0 {
1493 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
1494 }
1495
1496 let i2 = i.clone();
1497 let (i, e) = match i.as_bytes().iter().next() {
1498 Some(b'e') => (i.slice(1..), true),
1499 Some(b'E') => (i.slice(1..), true),
1500 _ => (i, false),
1501 };
1502
1503 let (i, exp) = if e {
1504 cut(crate::character::streaming::i32)(i)?
1505 } else {
1506 (i2, 0)
1507 };
1508
1509 Ok((i, (sign, integer, fraction, exp)))
1510}
1511
1512pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1531where
1532 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1533 T: Clone + Offset,
1534 T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f32> + Compare<&'static str>,
1535 <T as InputIter>::Item: AsChar,
1536 <T as InputIter>::IterElem: Clone,
1537 T: InputTakeAtPosition,
1538 <T as InputTakeAtPosition>::Item: AsChar,
1539 T: AsBytes,
1540 T: for<'a> Compare<&'a [u8]>,
1541{
1542 let (i, s) = recognize_float_or_exceptions(input)?;
1557 match s.parse_to() {
1558 Some(f) => Ok((i, f)),
1559 None => Err(crate::Err::Error(E::from_error_kind(
1560 i,
1561 crate::error::ErrorKind::Float,
1562 ))),
1563 }
1564}
1565
1566pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1585where
1586 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1587 T: Clone + Offset,
1588 T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f64> + Compare<&'static str>,
1589 <T as InputIter>::Item: AsChar,
1590 <T as InputIter>::IterElem: Clone,
1591 T: InputTakeAtPosition,
1592 <T as InputTakeAtPosition>::Item: AsChar,
1593 T: AsBytes,
1594 T: for<'a> Compare<&'a [u8]>,
1595{
1596 let (i, s) = recognize_float_or_exceptions(input)?;
1611 match s.parse_to() {
1612 Some(f) => Ok((i, f)),
1613 None => Err(crate::Err::Error(E::from_error_kind(
1614 i,
1615 crate::error::ErrorKind::Float,
1616 ))),
1617 }
1618}
1619
1620#[cfg(test)]
1621mod tests {
1622 use super::*;
1623 use crate::error::ErrorKind;
1624 use crate::internal::{Err, Needed};
1625 use proptest::prelude::*;
1626
1627 macro_rules! assert_parse(
1628 ($left: expr, $right: expr) => {
1629 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
1630 assert_eq!(res, $right);
1631 };
1632 );
1633
1634 #[test]
1635 fn i8_tests() {
1636 assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0)));
1637 assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1638 assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1)));
1639 assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128)));
1640 assert_parse!(be_i8(&[][..]), Err(Err::Incomplete(Needed::new(1))));
1641 }
1642
1643 #[test]
1644 fn i16_tests() {
1645 assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1646 assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16)));
1647 assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1648 assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16)));
1649 assert_parse!(be_i16(&[][..]), Err(Err::Incomplete(Needed::new(2))));
1650 assert_parse!(be_i16(&[0x00][..]), Err(Err::Incomplete(Needed::new(1))));
1651 }
1652
1653 #[test]
1654 fn u24_tests() {
1655 assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1656 assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32)));
1657 assert_parse!(
1658 be_u24(&[0x12, 0x34, 0x56][..]),
1659 Ok((&b""[..], 1_193_046_u32))
1660 );
1661 assert_parse!(be_u24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
1662 assert_parse!(be_u24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
1663 assert_parse!(
1664 be_u24(&[0x00, 0x00][..]),
1665 Err(Err::Incomplete(Needed::new(1)))
1666 );
1667 }
1668
1669 #[test]
1670 fn i24_tests() {
1671 assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1672 assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32)));
1673 assert_parse!(
1674 be_i24(&[0xED, 0xCB, 0xAA][..]),
1675 Ok((&b""[..], -1_193_046_i32))
1676 );
1677 assert_parse!(be_i24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
1678 assert_parse!(be_i24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
1679 assert_parse!(
1680 be_i24(&[0x00, 0x00][..]),
1681 Err(Err::Incomplete(Needed::new(1)))
1682 );
1683 }
1684
1685 #[test]
1686 fn i32_tests() {
1687 assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1688 assert_parse!(
1689 be_i32(&[0x7f, 0xff, 0xff, 0xff][..]),
1690 Ok((&b""[..], 2_147_483_647_i32))
1691 );
1692 assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1693 assert_parse!(
1694 be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
1695 Ok((&b""[..], -2_147_483_648_i32))
1696 );
1697 assert_parse!(be_i32(&[][..]), Err(Err::Incomplete(Needed::new(4))));
1698 assert_parse!(be_i32(&[0x00][..]), Err(Err::Incomplete(Needed::new(3))));
1699 assert_parse!(
1700 be_i32(&[0x00, 0x00][..]),
1701 Err(Err::Incomplete(Needed::new(2)))
1702 );
1703 assert_parse!(
1704 be_i32(&[0x00, 0x00, 0x00][..]),
1705 Err(Err::Incomplete(Needed::new(1)))
1706 );
1707 }
1708
1709 #[test]
1710 fn i64_tests() {
1711 assert_parse!(
1712 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1713 Ok((&b""[..], 0))
1714 );
1715 assert_parse!(
1716 be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1717 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1718 );
1719 assert_parse!(
1720 be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1721 Ok((&b""[..], -1))
1722 );
1723 assert_parse!(
1724 be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1725 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1726 );
1727 assert_parse!(be_i64(&[][..]), Err(Err::Incomplete(Needed::new(8))));
1728 assert_parse!(be_i64(&[0x00][..]), Err(Err::Incomplete(Needed::new(7))));
1729 assert_parse!(
1730 be_i64(&[0x00, 0x00][..]),
1731 Err(Err::Incomplete(Needed::new(6)))
1732 );
1733 assert_parse!(
1734 be_i64(&[0x00, 0x00, 0x00][..]),
1735 Err(Err::Incomplete(Needed::new(5)))
1736 );
1737 assert_parse!(
1738 be_i64(&[0x00, 0x00, 0x00, 0x00][..]),
1739 Err(Err::Incomplete(Needed::new(4)))
1740 );
1741 assert_parse!(
1742 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
1743 Err(Err::Incomplete(Needed::new(3)))
1744 );
1745 assert_parse!(
1746 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1747 Err(Err::Incomplete(Needed::new(2)))
1748 );
1749 assert_parse!(
1750 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1751 Err(Err::Incomplete(Needed::new(1)))
1752 );
1753 }
1754
1755 #[test]
1756 fn i128_tests() {
1757 assert_parse!(
1758 be_i128(
1759 &[
1760 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1761 0x00
1762 ][..]
1763 ),
1764 Ok((&b""[..], 0))
1765 );
1766 assert_parse!(
1767 be_i128(
1768 &[
1769 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1770 0xff
1771 ][..]
1772 ),
1773 Ok((
1774 &b""[..],
1775 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
1776 ))
1777 );
1778 assert_parse!(
1779 be_i128(
1780 &[
1781 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1782 0xff
1783 ][..]
1784 ),
1785 Ok((&b""[..], -1))
1786 );
1787 assert_parse!(
1788 be_i128(
1789 &[
1790 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1791 0x00
1792 ][..]
1793 ),
1794 Ok((
1795 &b""[..],
1796 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
1797 ))
1798 );
1799 assert_parse!(be_i128(&[][..]), Err(Err::Incomplete(Needed::new(16))));
1800 assert_parse!(be_i128(&[0x00][..]), Err(Err::Incomplete(Needed::new(15))));
1801 assert_parse!(
1802 be_i128(&[0x00, 0x00][..]),
1803 Err(Err::Incomplete(Needed::new(14)))
1804 );
1805 assert_parse!(
1806 be_i128(&[0x00, 0x00, 0x00][..]),
1807 Err(Err::Incomplete(Needed::new(13)))
1808 );
1809 assert_parse!(
1810 be_i128(&[0x00, 0x00, 0x00, 0x00][..]),
1811 Err(Err::Incomplete(Needed::new(12)))
1812 );
1813 assert_parse!(
1814 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
1815 Err(Err::Incomplete(Needed::new(11)))
1816 );
1817 assert_parse!(
1818 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1819 Err(Err::Incomplete(Needed::new(10)))
1820 );
1821 assert_parse!(
1822 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1823 Err(Err::Incomplete(Needed::new(9)))
1824 );
1825 assert_parse!(
1826 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1827 Err(Err::Incomplete(Needed::new(8)))
1828 );
1829 assert_parse!(
1830 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1831 Err(Err::Incomplete(Needed::new(7)))
1832 );
1833 assert_parse!(
1834 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1835 Err(Err::Incomplete(Needed::new(6)))
1836 );
1837 assert_parse!(
1838 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1839 Err(Err::Incomplete(Needed::new(5)))
1840 );
1841 assert_parse!(
1842 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1843 Err(Err::Incomplete(Needed::new(4)))
1844 );
1845 assert_parse!(
1846 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1847 Err(Err::Incomplete(Needed::new(3)))
1848 );
1849 assert_parse!(
1850 be_i128(
1851 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
1852 ),
1853 Err(Err::Incomplete(Needed::new(2)))
1854 );
1855 assert_parse!(
1856 be_i128(
1857 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
1858 [..]
1859 ),
1860 Err(Err::Incomplete(Needed::new(1)))
1861 );
1862 }
1863
1864 #[test]
1865 fn le_i8_tests() {
1866 assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0)));
1867 assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1868 assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1)));
1869 assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128)));
1870 }
1871
1872 #[test]
1873 fn le_i16_tests() {
1874 assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1875 assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16)));
1876 assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1877 assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
1878 }
1879
1880 #[test]
1881 fn le_u24_tests() {
1882 assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1883 assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32)));
1884 assert_parse!(
1885 le_u24(&[0x56, 0x34, 0x12][..]),
1886 Ok((&b""[..], 1_193_046_u32))
1887 );
1888 }
1889
1890 #[test]
1891 fn le_i24_tests() {
1892 assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1893 assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32)));
1894 assert_parse!(
1895 le_i24(&[0xAA, 0xCB, 0xED][..]),
1896 Ok((&b""[..], -1_193_046_i32))
1897 );
1898 }
1899
1900 #[test]
1901 fn le_i32_tests() {
1902 assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1903 assert_parse!(
1904 le_i32(&[0xff, 0xff, 0xff, 0x7f][..]),
1905 Ok((&b""[..], 2_147_483_647_i32))
1906 );
1907 assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1908 assert_parse!(
1909 le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
1910 Ok((&b""[..], -2_147_483_648_i32))
1911 );
1912 }
1913
1914 #[test]
1915 fn le_i64_tests() {
1916 assert_parse!(
1917 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1918 Ok((&b""[..], 0))
1919 );
1920 assert_parse!(
1921 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
1922 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1923 );
1924 assert_parse!(
1925 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1926 Ok((&b""[..], -1))
1927 );
1928 assert_parse!(
1929 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
1930 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1931 );
1932 }
1933
1934 #[test]
1935 fn le_i128_tests() {
1936 assert_parse!(
1937 le_i128(
1938 &[
1939 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1940 0x00
1941 ][..]
1942 ),
1943 Ok((&b""[..], 0))
1944 );
1945 assert_parse!(
1946 le_i128(
1947 &[
1948 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1949 0x7f
1950 ][..]
1951 ),
1952 Ok((
1953 &b""[..],
1954 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
1955 ))
1956 );
1957 assert_parse!(
1958 le_i128(
1959 &[
1960 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1961 0xff
1962 ][..]
1963 ),
1964 Ok((&b""[..], -1))
1965 );
1966 assert_parse!(
1967 le_i128(
1968 &[
1969 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1970 0x80
1971 ][..]
1972 ),
1973 Ok((
1974 &b""[..],
1975 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
1976 ))
1977 );
1978 }
1979
1980 #[test]
1981 fn be_f32_tests() {
1982 assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
1983 assert_parse!(
1984 be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
1985 Ok((&b""[..], 185_728_392_f32))
1986 );
1987 }
1988
1989 #[test]
1990 fn be_f64_tests() {
1991 assert_parse!(
1992 be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1993 Ok((&b""[..], 0_f64))
1994 );
1995 assert_parse!(
1996 be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
1997 Ok((&b""[..], 185_728_392_f64))
1998 );
1999 }
2000
2001 #[test]
2002 fn le_f32_tests() {
2003 assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2004 assert_parse!(
2005 le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
2006 Ok((&b""[..], 185_728_392_f32))
2007 );
2008 }
2009
2010 #[test]
2011 fn le_f64_tests() {
2012 assert_parse!(
2013 le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2014 Ok((&b""[..], 0_f64))
2015 );
2016 assert_parse!(
2017 le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
2018 Ok((&b""[..], 185_728_392_f64))
2019 );
2020 }
2021
2022 #[test]
2023 fn hex_u32_tests() {
2024 assert_parse!(
2025 hex_u32(&b";"[..]),
2026 Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
2027 );
2028 assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
2029 assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
2030 assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2031 assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2032 assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
2033 assert_parse!(
2034 hex_u32(&b"c5a31be201;"[..]),
2035 Ok((&b"01;"[..], 3_315_801_058))
2036 );
2037 assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
2038 assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
2039 assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::new(1))));
2040 }
2041
2042 #[test]
2043 #[cfg(feature = "std")]
2044 fn float_test() {
2045 let mut test_cases = vec![
2046 "+3.14",
2047 "3.14",
2048 "-3.14",
2049 "0",
2050 "0.0",
2051 "1.",
2052 ".789",
2053 "-.5",
2054 "1e7",
2055 "-1E-7",
2056 ".3e-2",
2057 "1.e4",
2058 "1.2e4",
2059 "12.34",
2060 "-1.234E-12",
2061 "-1.234e-12",
2062 "0.00000000000000000087",
2063 ];
2064
2065 for test in test_cases.drain(..) {
2066 let expected32 = str::parse::<f32>(test).unwrap();
2067 let expected64 = str::parse::<f64>(test).unwrap();
2068
2069 println!("now parsing: {} -> {}", test, expected32);
2070
2071 let larger = format!("{};", test);
2072 assert_parse!(recognize_float(&larger[..]), Ok((";", test)));
2073
2074 assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32)));
2075 assert_parse!(float(&larger[..]), Ok((";", expected32)));
2076
2077 assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64)));
2078 assert_parse!(double(&larger[..]), Ok((";", expected64)));
2079 }
2080
2081 let remaining_exponent = "-1.234E-";
2082 assert_parse!(
2083 recognize_float(remaining_exponent),
2084 Err(Err::Incomplete(Needed::new(1)))
2085 );
2086
2087 let (_i, nan) = float::<_, ()>("NaN").unwrap();
2088 assert!(nan.is_nan());
2089
2090 let (_i, inf) = float::<_, ()>("inf").unwrap();
2091 assert!(inf.is_infinite());
2092 let (_i, inf) = float::<_, ()>("infinite").unwrap();
2093 assert!(inf.is_infinite());
2094 }
2095
2096 #[test]
2097 fn configurable_endianness() {
2098 use crate::number::Endianness;
2099
2100 fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2101 u16(Endianness::Big)(i)
2102 }
2103 fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2104 u16(Endianness::Little)(i)
2105 }
2106 assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
2107 assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
2108
2109 fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2110 u32(Endianness::Big)(i)
2111 }
2112 fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2113 u32(Endianness::Little)(i)
2114 }
2115 assert_eq!(
2116 be_tst32(&[0x12, 0x00, 0x60, 0x00]),
2117 Ok((&b""[..], 302_014_464_u32))
2118 );
2119 assert_eq!(
2120 le_tst32(&[0x12, 0x00, 0x60, 0x00]),
2121 Ok((&b""[..], 6_291_474_u32))
2122 );
2123
2124 fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2125 u64(Endianness::Big)(i)
2126 }
2127 fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2128 u64(Endianness::Little)(i)
2129 }
2130 assert_eq!(
2131 be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2132 Ok((&b""[..], 1_297_142_246_100_992_000_u64))
2133 );
2134 assert_eq!(
2135 le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2136 Ok((&b""[..], 36_028_874_334_666_770_u64))
2137 );
2138
2139 fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2140 i16(Endianness::Big)(i)
2141 }
2142 fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2143 i16(Endianness::Little)(i)
2144 }
2145 assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
2146 assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
2147
2148 fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2149 i32(Endianness::Big)(i)
2150 }
2151 fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2152 i32(Endianness::Little)(i)
2153 }
2154 assert_eq!(
2155 be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2156 Ok((&b""[..], 1_204_224_i32))
2157 );
2158 assert_eq!(
2159 le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2160 Ok((&b""[..], 6_296_064_i32))
2161 );
2162
2163 fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2164 i64(Endianness::Big)(i)
2165 }
2166 fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2167 i64(Endianness::Little)(i)
2168 }
2169 assert_eq!(
2170 be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2171 Ok((&b""[..], 71_881_672_479_506_432_i64))
2172 );
2173 assert_eq!(
2174 le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2175 Ok((&b""[..], 36_028_874_334_732_032_i64))
2176 );
2177 }
2178
2179 #[cfg(feature = "std")]
2180 fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
2181 use crate::traits::ParseTo;
2182 match recognize_float_or_exceptions(i) {
2183 Err(e) => Err(e),
2184 Ok((i, s)) => {
2185 if s.is_empty() {
2186 return Err(Err::Error(()));
2187 }
2188 match s.parse_to() {
2189 Some(n) => Ok((i, n)),
2190 None => Err(Err::Error(())),
2191 }
2192 }
2193 }
2194 }
2195
2196 proptest! {
2197 #[test]
2198 #[cfg(feature = "std")]
2199 fn floats(s in "\\PC*") {
2200 println!("testing {}", s);
2201 let res1 = parse_f64(&s);
2202 let res2 = double::<_, ()>(s.as_str());
2203 assert_eq!(res1, res2);
2204 }
2205 }
2206}