1#![allow(deprecated)]
4
5use crate::branch::alt;
6use crate::bytes::complete::tag;
7use crate::character::complete::{char, digit1, sign};
8use crate::combinator::{cut, map, opt, recognize};
9use crate::error::ParseError;
10use crate::error::{make_error, ErrorKind};
11use crate::input::{
12 AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, IntoOutput,
13 Offset, Slice,
14};
15use crate::lib::std::ops::{Range, RangeFrom, RangeTo};
16use crate::sequence::{pair, tuple};
17use crate::*;
18
19#[inline]
35#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u8`")]
38pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
39where
40 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
41{
42 let bound: usize = 1;
43 if input.input_len() < bound {
44 Err(Err::Error(make_error(input, ErrorKind::Eof)))
45 } else {
46 let res = input.iter_elements().next().unwrap();
47
48 Ok((input.slice(bound..), res))
49 }
50}
51
52#[inline]
68#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u16`")]
71pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
72where
73 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
74{
75 let bound: usize = 2;
76 if input.input_len() < bound {
77 Err(Err::Error(make_error(input, ErrorKind::Eof)))
78 } else {
79 let mut res = 0u16;
80 for byte in input.iter_elements().take(bound) {
81 res = (res << 8) + byte as u16;
82 }
83
84 Ok((input.slice(bound..), res))
85 }
86}
87
88#[inline]
104#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u24`")]
107pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
108where
109 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
110{
111 let bound: usize = 3;
112 if input.input_len() < bound {
113 Err(Err::Error(make_error(input, ErrorKind::Eof)))
114 } else {
115 let mut res = 0u32;
116 for byte in input.iter_elements().take(bound) {
117 res = (res << 8) + byte as u32;
118 }
119
120 Ok((input.slice(bound..), res))
121 }
122}
123
124#[inline]
140#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u32`")]
143pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
144where
145 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
146{
147 let bound: usize = 4;
148 if input.input_len() < bound {
149 Err(Err::Error(make_error(input, ErrorKind::Eof)))
150 } else {
151 let mut res = 0u32;
152 for byte in input.iter_elements().take(bound) {
153 res = (res << 8) + byte as u32;
154 }
155
156 Ok((input.slice(bound..), res))
157 }
158}
159
160#[inline]
176#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u64`")]
179pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
180where
181 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
182{
183 let bound: usize = 8;
184 if input.input_len() < bound {
185 Err(Err::Error(make_error(input, ErrorKind::Eof)))
186 } else {
187 let mut res = 0u64;
188 for byte in input.iter_elements().take(bound) {
189 res = (res << 8) + byte as u64;
190 }
191
192 Ok((input.slice(bound..), res))
193 }
194}
195
196#[inline]
212#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u128`")]
215pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
216where
217 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
218{
219 let bound: usize = 16;
220 if input.input_len() < bound {
221 Err(Err::Error(make_error(input, ErrorKind::Eof)))
222 } else {
223 let mut res = 0u128;
224 for byte in input.iter_elements().take(bound) {
225 res = (res << 8) + byte as u128;
226 }
227
228 Ok((input.slice(bound..), res))
229 }
230}
231
232#[inline]
248#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i8`")]
251pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
252where
253 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
254{
255 be_u8.map(|x| x as i8).parse(input)
256}
257
258#[inline]
274#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i16`")]
277pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
278where
279 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
280{
281 be_u16.map(|x| x as i16).parse(input)
282}
283
284#[inline]
300#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i24`")]
303pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
304where
305 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
306{
307 be_u24
309 .map(|x| {
310 if x & 0x80_00_00 != 0 {
311 (x | 0xff_00_00_00) as i32
312 } else {
313 x as i32
314 }
315 })
316 .parse(input)
317}
318
319#[inline]
335#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i32`")]
338pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
339where
340 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
341{
342 be_u32.map(|x| x as i32).parse(input)
343}
344
345#[inline]
361#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i64`")]
364pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
365where
366 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
367{
368 be_u64.map(|x| x as i64).parse(input)
369}
370
371#[inline]
387#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i128`")]
390pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
391where
392 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
393{
394 be_u128.map(|x| x as i128).parse(input)
395}
396
397#[inline]
413#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u8`")]
416pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
417where
418 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
419{
420 let bound: usize = 1;
421 if input.input_len() < bound {
422 Err(Err::Error(make_error(input, ErrorKind::Eof)))
423 } else {
424 let res = input.iter_elements().next().unwrap();
425
426 Ok((input.slice(bound..), res))
427 }
428}
429
430#[inline]
446#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u16`")]
449pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
450where
451 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
452{
453 let bound: usize = 2;
454 if input.input_len() < bound {
455 Err(Err::Error(make_error(input, ErrorKind::Eof)))
456 } else {
457 let mut res = 0u16;
458 for (index, byte) in input.iter_indices().take(bound) {
459 res += (byte as u16) << (8 * index);
460 }
461
462 Ok((input.slice(bound..), res))
463 }
464}
465
466#[inline]
482#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u24`")]
485pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
486where
487 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
488{
489 let bound: usize = 3;
490 if input.input_len() < bound {
491 Err(Err::Error(make_error(input, ErrorKind::Eof)))
492 } else {
493 let mut res = 0u32;
494 for (index, byte) in input.iter_indices().take(bound) {
495 res += (byte as u32) << (8 * index);
496 }
497
498 Ok((input.slice(bound..), res))
499 }
500}
501
502#[inline]
518#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u32`")]
521pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
522where
523 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
524{
525 let bound: usize = 4;
526 if input.input_len() < bound {
527 Err(Err::Error(make_error(input, ErrorKind::Eof)))
528 } else {
529 let mut res = 0u32;
530 for (index, byte) in input.iter_indices().take(bound) {
531 res += (byte as u32) << (8 * index);
532 }
533
534 Ok((input.slice(bound..), res))
535 }
536}
537
538#[inline]
554#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u64`")]
557pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
558where
559 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
560{
561 let bound: usize = 8;
562 if input.input_len() < bound {
563 Err(Err::Error(make_error(input, ErrorKind::Eof)))
564 } else {
565 let mut res = 0u64;
566 for (index, byte) in input.iter_indices().take(bound) {
567 res += (byte as u64) << (8 * index);
568 }
569
570 Ok((input.slice(bound..), res))
571 }
572}
573
574#[inline]
590#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u128`")]
593pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
594where
595 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
596{
597 let bound: usize = 16;
598 if input.input_len() < bound {
599 Err(Err::Error(make_error(input, ErrorKind::Eof)))
600 } else {
601 let mut res = 0u128;
602 for (index, byte) in input.iter_indices().take(bound) {
603 res += (byte as u128) << (8 * index);
604 }
605
606 Ok((input.slice(bound..), res))
607 }
608}
609
610#[inline]
626#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i8`")]
629pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
630where
631 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
632{
633 be_u8.map(|x| x as i8).parse(input)
634}
635
636#[inline]
652#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i16`")]
655pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
656where
657 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
658{
659 le_u16.map(|x| x as i16).parse(input)
660}
661
662#[inline]
678#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i24`")]
681pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
682where
683 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
684{
685 le_u24
687 .map(|x| {
688 if x & 0x80_00_00 != 0 {
689 (x | 0xff_00_00_00) as i32
690 } else {
691 x as i32
692 }
693 })
694 .parse(input)
695}
696
697#[inline]
713#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i32`")]
716pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
717where
718 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
719{
720 le_u32.map(|x| x as i32).parse(input)
721}
722
723#[inline]
739#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i64`")]
742pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
743where
744 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
745{
746 le_u64.map(|x| x as i64).parse(input)
747}
748
749#[inline]
765#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i128`")]
768pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
769where
770 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
771{
772 le_u128.map(|x| x as i128).parse(input)
773}
774
775#[inline]
792#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u8`")]
795pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
796where
797 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
798{
799 let bound: usize = 1;
800 if input.input_len() < bound {
801 Err(Err::Error(make_error(input, ErrorKind::Eof)))
802 } else {
803 let res = input.iter_elements().next().unwrap();
804
805 Ok((input.slice(bound..), res))
806 }
807}
808
809#[inline]
835#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u16`")]
838pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
839where
840 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
841{
842 match endian {
843 crate::number::Endianness::Big => be_u16,
844 crate::number::Endianness::Little => le_u16,
845 #[cfg(target_endian = "big")]
846 crate::number::Endianness::Native => be_u16,
847 #[cfg(target_endian = "little")]
848 crate::number::Endianness::Native => le_u16,
849 }
850}
851
852#[inline]
877#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u24`")]
880pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
881where
882 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
883{
884 match endian {
885 crate::number::Endianness::Big => be_u24,
886 crate::number::Endianness::Little => le_u24,
887 #[cfg(target_endian = "big")]
888 crate::number::Endianness::Native => be_u24,
889 #[cfg(target_endian = "little")]
890 crate::number::Endianness::Native => le_u24,
891 }
892}
893
894#[inline]
919#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u32`")]
922pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
923where
924 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
925{
926 match endian {
927 crate::number::Endianness::Big => be_u32,
928 crate::number::Endianness::Little => le_u32,
929 #[cfg(target_endian = "big")]
930 crate::number::Endianness::Native => be_u32,
931 #[cfg(target_endian = "little")]
932 crate::number::Endianness::Native => le_u32,
933 }
934}
935
936#[inline]
961#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u64`")]
964pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
965where
966 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
967{
968 match endian {
969 crate::number::Endianness::Big => be_u64,
970 crate::number::Endianness::Little => le_u64,
971 #[cfg(target_endian = "big")]
972 crate::number::Endianness::Native => be_u64,
973 #[cfg(target_endian = "little")]
974 crate::number::Endianness::Native => le_u64,
975 }
976}
977
978#[inline]
1003#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u128`")]
1006pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
1007where
1008 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1009{
1010 match endian {
1011 crate::number::Endianness::Big => be_u128,
1012 crate::number::Endianness::Little => le_u128,
1013 #[cfg(target_endian = "big")]
1014 crate::number::Endianness::Native => be_u128,
1015 #[cfg(target_endian = "little")]
1016 crate::number::Endianness::Native => le_u128,
1017 }
1018}
1019
1020#[inline]
1037#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i8`")]
1040pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
1041where
1042 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1043{
1044 u8.map(|x| x as i8).parse(i)
1045}
1046
1047#[inline]
1072#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i16`")]
1075pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
1076where
1077 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1078{
1079 match endian {
1080 crate::number::Endianness::Big => be_i16,
1081 crate::number::Endianness::Little => le_i16,
1082 #[cfg(target_endian = "big")]
1083 crate::number::Endianness::Native => be_i16,
1084 #[cfg(target_endian = "little")]
1085 crate::number::Endianness::Native => le_i16,
1086 }
1087}
1088
1089#[inline]
1114#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i24`")]
1117pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1118where
1119 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1120{
1121 match endian {
1122 crate::number::Endianness::Big => be_i24,
1123 crate::number::Endianness::Little => le_i24,
1124 #[cfg(target_endian = "big")]
1125 crate::number::Endianness::Native => be_i24,
1126 #[cfg(target_endian = "little")]
1127 crate::number::Endianness::Native => le_i24,
1128 }
1129}
1130
1131#[inline]
1156#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i32`")]
1159pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1160where
1161 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1162{
1163 match endian {
1164 crate::number::Endianness::Big => be_i32,
1165 crate::number::Endianness::Little => le_i32,
1166 #[cfg(target_endian = "big")]
1167 crate::number::Endianness::Native => be_i32,
1168 #[cfg(target_endian = "little")]
1169 crate::number::Endianness::Native => le_i32,
1170 }
1171}
1172
1173#[inline]
1198#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i64`")]
1201pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
1202where
1203 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1204{
1205 match endian {
1206 crate::number::Endianness::Big => be_i64,
1207 crate::number::Endianness::Little => le_i64,
1208 #[cfg(target_endian = "big")]
1209 crate::number::Endianness::Native => be_i64,
1210 #[cfg(target_endian = "little")]
1211 crate::number::Endianness::Native => le_i64,
1212 }
1213}
1214
1215#[inline]
1240#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i128`")]
1243pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
1244where
1245 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1246{
1247 match endian {
1248 crate::number::Endianness::Big => be_i128,
1249 crate::number::Endianness::Little => le_i128,
1250 #[cfg(target_endian = "big")]
1251 crate::number::Endianness::Native => be_i128,
1252 #[cfg(target_endian = "little")]
1253 crate::number::Endianness::Native => le_i128,
1254 }
1255}
1256
1257#[inline]
1273#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_f32`")]
1276pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1277where
1278 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1279{
1280 match be_u32(input) {
1281 Err(e) => Err(e),
1282 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1283 }
1284}
1285
1286#[inline]
1302#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_f64`")]
1305pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1306where
1307 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1308{
1309 match be_u64(input) {
1310 Err(e) => Err(e),
1311 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1312 }
1313}
1314
1315#[inline]
1331#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_f32`")]
1334pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1335where
1336 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1337{
1338 match le_u32(input) {
1339 Err(e) => Err(e),
1340 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1341 }
1342}
1343
1344#[inline]
1360#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_f64`")]
1363pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1364where
1365 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1366{
1367 match le_u64(input) {
1368 Err(e) => Err(e),
1369 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1370 }
1371}
1372
1373#[inline]
1398#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::f32`")]
1401pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1402where
1403 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1404{
1405 match endian {
1406 crate::number::Endianness::Big => be_f32,
1407 crate::number::Endianness::Little => le_f32,
1408 #[cfg(target_endian = "big")]
1409 crate::number::Endianness::Native => be_f32,
1410 #[cfg(target_endian = "little")]
1411 crate::number::Endianness::Native => le_f32,
1412 }
1413}
1414
1415#[inline]
1440#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::f64`")]
1443pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1444where
1445 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1446{
1447 match endian {
1448 crate::number::Endianness::Big => be_f64,
1449 crate::number::Endianness::Little => le_f64,
1450 #[cfg(target_endian = "big")]
1451 crate::number::Endianness::Native => be_f64,
1452 #[cfg(target_endian = "little")]
1453 crate::number::Endianness::Native => le_f64,
1454 }
1455}
1456
1457#[inline]
1474#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::hex_u32`")]
1477pub fn hex_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
1478where
1479 I: InputTakeAtPosition,
1480 I: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1481 <I as InputTakeAtPosition>::Item: AsChar,
1482 I: AsBytes,
1483 I: InputLength,
1484{
1485 let e: ErrorKind = ErrorKind::IsA;
1486 let (i, o) = input.split_at_position1_complete(
1487 |c| {
1488 let c = c.as_char();
1489 !"0123456789abcdefABCDEF".contains(c)
1490 },
1491 e,
1492 )?;
1493
1494 let (parsed, remaining) = if o.input_len() <= 8 {
1496 (o, i)
1497 } else {
1498 (input.slice(..8), input.slice(8..))
1499 };
1500
1501 let res = parsed
1502 .as_bytes()
1503 .iter()
1504 .rev()
1505 .enumerate()
1506 .map(|(k, &v)| {
1507 let digit = v as char;
1508 digit.to_digit(16).unwrap_or(0) << (k * 4)
1509 })
1510 .sum();
1511
1512 Ok((remaining, res))
1513}
1514
1515#[rustfmt::skip]
1534#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::recognize_float`")]
1537pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, <T as IntoOutput>::Output, E>
1538where
1539 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1540 T: Clone + Offset,
1541 T: InputIter,
1542 T: IntoOutput,
1543 <T as InputIter>::Item: AsChar,
1544 T: InputTakeAtPosition,
1545 <T as InputTakeAtPosition>::Item: AsChar,
1546{
1547 recognize(
1548 tuple((
1549 opt(alt((char('+'), char('-')))),
1550 alt((
1551 map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
1552 map(tuple((char('.'), digit1)), |_| ())
1553 )),
1554 opt(tuple((
1555 alt((char('e'), char('E'))),
1556 opt(alt((char('+'), char('-')))),
1557 cut(digit1)
1558 )))
1559 ))
1560 )(input)
1561}
1562
1563#[doc(hidden)]
1565#[deprecated(
1568 since = "8.0.0",
1569 note = "Replaced with `nom8::character::recognize_float_or_exceptions`"
1570)]
1571pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(
1572 input: T,
1573) -> IResult<T, <T as IntoOutput>::Output, E>
1574where
1575 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1576 T: Clone + Offset,
1577 T: InputIter + InputTake + Compare<&'static str>,
1578 <T as InputIter>::Item: AsChar,
1579 T: InputTakeAtPosition,
1580 T: IntoOutput,
1581 <T as InputTakeAtPosition>::Item: AsChar,
1582{
1583 alt((
1584 |i: T| {
1585 recognize_float::<_, E>(i.clone()).map_err(|e| match e {
1586 crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
1587 crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
1588 crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
1589 })
1590 },
1591 |i: T| {
1592 crate::bytes::complete::tag_no_case::<_, _, E>("nan")(i.clone())
1593 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1594 },
1595 |i: T| {
1596 crate::bytes::complete::tag_no_case::<_, _, E>("inf")(i.clone())
1597 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1598 },
1599 |i: T| {
1600 crate::bytes::complete::tag_no_case::<_, _, E>("infinity")(i.clone())
1601 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1602 },
1603 ))(input)
1604}
1605
1606#[deprecated(
1616 since = "8.0.0",
1617 note = "Replaced with `nom8::character::recognize_float_parts`"
1618)]
1619pub fn recognize_float_parts<T, E: ParseError<T>>(
1620 input: T,
1621) -> IResult<
1622 T,
1623 (
1624 bool,
1625 <T as IntoOutput>::Output,
1626 <T as IntoOutput>::Output,
1627 i32,
1628 ),
1629 E,
1630>
1631where
1632 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1633 T: Clone + Offset,
1634 T: InputIter + InputTake,
1635 T: IntoOutput,
1636 <T as InputIter>::Item: AsChar + Copy,
1637 T: InputTakeAtPosition + InputLength,
1638 <T as InputTakeAtPosition>::Item: AsChar,
1639 T: for<'a> Compare<&'a [u8]>,
1640 T: AsBytes,
1641{
1642 let (i, sign) = sign(input.clone())?;
1643
1644 let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
1645 Some(index) => i.take_split(index),
1646 None => i.take_split(i.input_len()),
1647 };
1648 let (i, mut integer) = match i
1650 .as_bytes()
1651 .iter()
1652 .position(|c| !(*c >= b'0' && *c <= b'9'))
1653 {
1654 Some(index) => i.take_split(index),
1655 None => i.take_split(i.input_len()),
1656 };
1657
1658 if integer.input_len() == 0 && zeroes.input_len() > 0 {
1659 integer = zeroes.slice(zeroes.input_len() - 1..);
1661 }
1662
1663 let (i, opt_dot) = opt(tag(&b"."[..]))(i)?;
1664 let (i, fraction) = if opt_dot.is_none() {
1665 let i2 = i.clone();
1666 (i2, i.slice(..0))
1667 } else {
1668 let mut zero_count = 0usize;
1670 let mut position = None;
1671 for (pos, c) in i.as_bytes().iter().enumerate() {
1672 if *c >= b'0' && *c <= b'9' {
1673 if *c == b'0' {
1674 zero_count += 1;
1675 } else {
1676 zero_count = 0;
1677 }
1678 } else {
1679 position = Some(pos);
1680 break;
1681 }
1682 }
1683
1684 let position = position.unwrap_or(i.input_len());
1685
1686 let index = if zero_count == 0 {
1687 position
1688 } else if zero_count == position {
1689 position - zero_count + 1
1690 } else {
1691 position - zero_count
1692 };
1693
1694 (i.slice(position..), i.slice(..index))
1695 };
1696
1697 if integer.input_len() == 0 && fraction.input_len() == 0 {
1698 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
1699 }
1700
1701 let i2 = i.clone();
1702 let (i, e) = match i.as_bytes().iter().next() {
1703 Some(b'e') => (i.slice(1..), true),
1704 Some(b'E') => (i.slice(1..), true),
1705 _ => (i, false),
1706 };
1707
1708 let (i, exp) = if e {
1709 cut(crate::character::complete::i32)(i)?
1710 } else {
1711 (i2, 0)
1712 };
1713
1714 Ok((
1715 i,
1716 (sign, integer.into_output(), fraction.into_output(), exp),
1717 ))
1718}
1719
1720use crate::input::ParseTo;
1721
1722#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::f32`")]
1742pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1743where
1744 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1745 T: Clone + Offset + Compare<&'static str>,
1746 T: InputIter + InputLength + InputTake,
1747 T: IntoOutput,
1748 <T as IntoOutput>::Output: ParseTo<f32>,
1749 <T as InputIter>::Item: AsChar + Copy,
1750 <T as InputIter>::IterElem: Clone,
1751 T: InputTakeAtPosition,
1752 <T as InputTakeAtPosition>::Item: AsChar,
1753 T: AsBytes,
1754 T: for<'a> Compare<&'a [u8]>,
1755{
1756 let (i, s) = recognize_float_or_exceptions(input)?;
1757 match s.parse_to() {
1758 Some(f) => Ok((i, f)),
1759 None => Err(crate::Err::Error(E::from_error_kind(
1760 i,
1761 crate::error::ErrorKind::Float,
1762 ))),
1763 }
1764}
1765
1766#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::f64`")]
1786pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1787where
1788 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1789 T: Clone + Offset + Compare<&'static str>,
1790 T: InputIter + InputLength + InputTake,
1791 T: IntoOutput,
1792 <T as IntoOutput>::Output: ParseTo<f64>,
1793 <T as InputIter>::Item: AsChar + Copy,
1794 <T as InputIter>::IterElem: Clone,
1795 T: InputTakeAtPosition,
1796 <T as InputTakeAtPosition>::Item: AsChar,
1797 T: AsBytes,
1798 T: for<'a> Compare<&'a [u8]>,
1799{
1800 let (i, s) = recognize_float_or_exceptions(input)?;
1801 match s.parse_to() {
1802 Some(f) => Ok((i, f)),
1803 None => Err(crate::Err::Error(E::from_error_kind(
1804 i,
1805 crate::error::ErrorKind::Float,
1806 ))),
1807 }
1808}
1809
1810#[cfg(test)]
1811mod tests {
1812 use super::*;
1813 use crate::error::ErrorKind;
1814 use crate::Err;
1815 use proptest::prelude::*;
1816
1817 macro_rules! assert_parse(
1818 ($left: expr, $right: expr) => {
1819 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
1820 assert_eq!(res, $right);
1821 };
1822 );
1823
1824 #[test]
1825 fn i8_tests() {
1826 assert_parse!(i8(&[0x00][..]), Ok((&b""[..], 0)));
1827 assert_parse!(i8(&[0x7f][..]), Ok((&b""[..], 127)));
1828 assert_parse!(i8(&[0xff][..]), Ok((&b""[..], -1)));
1829 assert_parse!(i8(&[0x80][..]), Ok((&b""[..], -128)));
1830 }
1831
1832 #[test]
1833 fn be_i8_tests() {
1834 assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0)));
1835 assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1836 assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1)));
1837 assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128)));
1838 }
1839
1840 #[test]
1841 fn be_i16_tests() {
1842 assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1843 assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16)));
1844 assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1845 assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16)));
1846 }
1847
1848 #[test]
1849 fn be_u24_tests() {
1850 assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1851 assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32)));
1852 assert_parse!(
1853 be_u24(&[0x12, 0x34, 0x56][..]),
1854 Ok((&b""[..], 1_193_046_u32))
1855 );
1856 }
1857
1858 #[test]
1859 fn be_i24_tests() {
1860 assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1861 assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32)));
1862 assert_parse!(
1863 be_i24(&[0xED, 0xCB, 0xAA][..]),
1864 Ok((&b""[..], -1_193_046_i32))
1865 );
1866 }
1867
1868 #[test]
1869 fn be_i32_tests() {
1870 assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1871 assert_parse!(
1872 be_i32(&[0x7f, 0xff, 0xff, 0xff][..]),
1873 Ok((&b""[..], 2_147_483_647_i32))
1874 );
1875 assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1876 assert_parse!(
1877 be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
1878 Ok((&b""[..], -2_147_483_648_i32))
1879 );
1880 }
1881
1882 #[test]
1883 fn be_i64_tests() {
1884 assert_parse!(
1885 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1886 Ok((&b""[..], 0))
1887 );
1888 assert_parse!(
1889 be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1890 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1891 );
1892 assert_parse!(
1893 be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1894 Ok((&b""[..], -1))
1895 );
1896 assert_parse!(
1897 be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1898 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1899 );
1900 }
1901
1902 #[test]
1903 fn be_i128_tests() {
1904 assert_parse!(
1905 be_i128(
1906 &[
1907 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x00
1909 ][..]
1910 ),
1911 Ok((&b""[..], 0))
1912 );
1913 assert_parse!(
1914 be_i128(
1915 &[
1916 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1917 0xff
1918 ][..]
1919 ),
1920 Ok((
1921 &b""[..],
1922 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
1923 ))
1924 );
1925 assert_parse!(
1926 be_i128(
1927 &[
1928 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1929 0xff
1930 ][..]
1931 ),
1932 Ok((&b""[..], -1))
1933 );
1934 assert_parse!(
1935 be_i128(
1936 &[
1937 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1938 0x00
1939 ][..]
1940 ),
1941 Ok((
1942 &b""[..],
1943 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
1944 ))
1945 );
1946 }
1947
1948 #[test]
1949 fn le_i8_tests() {
1950 assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0)));
1951 assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1952 assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1)));
1953 assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128)));
1954 }
1955
1956 #[test]
1957 fn le_i16_tests() {
1958 assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1959 assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16)));
1960 assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1961 assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
1962 }
1963
1964 #[test]
1965 fn le_u24_tests() {
1966 assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1967 assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32)));
1968 assert_parse!(
1969 le_u24(&[0x56, 0x34, 0x12][..]),
1970 Ok((&b""[..], 1_193_046_u32))
1971 );
1972 }
1973
1974 #[test]
1975 fn le_i24_tests() {
1976 assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1977 assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32)));
1978 assert_parse!(
1979 le_i24(&[0xAA, 0xCB, 0xED][..]),
1980 Ok((&b""[..], -1_193_046_i32))
1981 );
1982 }
1983
1984 #[test]
1985 fn le_i32_tests() {
1986 assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1987 assert_parse!(
1988 le_i32(&[0xff, 0xff, 0xff, 0x7f][..]),
1989 Ok((&b""[..], 2_147_483_647_i32))
1990 );
1991 assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1992 assert_parse!(
1993 le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
1994 Ok((&b""[..], -2_147_483_648_i32))
1995 );
1996 }
1997
1998 #[test]
1999 fn le_i64_tests() {
2000 assert_parse!(
2001 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2002 Ok((&b""[..], 0))
2003 );
2004 assert_parse!(
2005 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
2006 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
2007 );
2008 assert_parse!(
2009 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
2010 Ok((&b""[..], -1))
2011 );
2012 assert_parse!(
2013 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
2014 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
2015 );
2016 }
2017
2018 #[test]
2019 fn le_i128_tests() {
2020 assert_parse!(
2021 le_i128(
2022 &[
2023 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2024 0x00
2025 ][..]
2026 ),
2027 Ok((&b""[..], 0))
2028 );
2029 assert_parse!(
2030 le_i128(
2031 &[
2032 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2033 0x7f
2034 ][..]
2035 ),
2036 Ok((
2037 &b""[..],
2038 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
2039 ))
2040 );
2041 assert_parse!(
2042 le_i128(
2043 &[
2044 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2045 0xff
2046 ][..]
2047 ),
2048 Ok((&b""[..], -1))
2049 );
2050 assert_parse!(
2051 le_i128(
2052 &[
2053 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2054 0x80
2055 ][..]
2056 ),
2057 Ok((
2058 &b""[..],
2059 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
2060 ))
2061 );
2062 }
2063
2064 #[test]
2065 fn be_f32_tests() {
2066 assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2067 assert_parse!(
2068 be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
2069 Ok((&b""[..], 185_728_392_f32))
2070 );
2071 }
2072
2073 #[test]
2074 fn be_f64_tests() {
2075 assert_parse!(
2076 be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2077 Ok((&b""[..], 0_f64))
2078 );
2079 assert_parse!(
2080 be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
2081 Ok((&b""[..], 185_728_392_f64))
2082 );
2083 }
2084
2085 #[test]
2086 fn le_f32_tests() {
2087 assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2088 assert_parse!(
2089 le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
2090 Ok((&b""[..], 185_728_392_f32))
2091 );
2092 }
2093
2094 #[test]
2095 fn le_f64_tests() {
2096 assert_parse!(
2097 le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2098 Ok((&b""[..], 0_f64))
2099 );
2100 assert_parse!(
2101 le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
2102 Ok((&b""[..], 185_728_392_f64))
2103 );
2104 }
2105
2106 #[test]
2107 fn hex_u32_tests() {
2108 assert_parse!(
2109 hex_u32(&b";"[..]),
2110 Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
2111 );
2112 assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
2113 assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
2114 assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2115 assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2116 assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
2117 assert_parse!(
2118 hex_u32(&b"c5a31be201;"[..]),
2119 Ok((&b"01;"[..], 3_315_801_058))
2120 );
2121 assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
2122 assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
2123 assert_parse!(hex_u32(&b"12af"[..]), Ok((&b""[..], 0x12af)));
2124 }
2125
2126 #[test]
2127 #[cfg(feature = "std")]
2128 fn float_test() {
2129 let mut test_cases = vec![
2130 "+3.14",
2131 "3.14",
2132 "-3.14",
2133 "0",
2134 "0.0",
2135 "1.",
2136 ".789",
2137 "-.5",
2138 "1e7",
2139 "-1E-7",
2140 ".3e-2",
2141 "1.e4",
2142 "1.2e4",
2143 "12.34",
2144 "-1.234E-12",
2145 "-1.234e-12",
2146 "0.00000000000000000087",
2147 ];
2148
2149 for test in test_cases.drain(..) {
2150 let expected32 = str::parse::<f32>(test).unwrap();
2151 let expected64 = str::parse::<f64>(test).unwrap();
2152
2153 println!("now parsing: {} -> {}", test, expected32);
2154
2155 let larger = format!("{}", test);
2156 assert_parse!(recognize_float(&larger[..]), Ok(("", test)));
2157
2158 assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32)));
2159 assert_parse!(float(&larger[..]), Ok(("", expected32)));
2160
2161 assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64)));
2162 assert_parse!(double(&larger[..]), Ok(("", expected64)));
2163 }
2164
2165 let remaining_exponent = "-1.234E-";
2166 assert_parse!(
2167 recognize_float(remaining_exponent),
2168 Err(Err::Failure(("", ErrorKind::Digit)))
2169 );
2170
2171 let (_i, nan) = float::<_, ()>("NaN").unwrap();
2172 assert!(nan.is_nan());
2173
2174 let (_i, inf) = float::<_, ()>("inf").unwrap();
2175 assert!(inf.is_infinite());
2176 let (_i, inf) = float::<_, ()>("infinite").unwrap();
2177 assert!(inf.is_infinite());
2178 }
2179
2180 #[test]
2181 fn configurable_endianness() {
2182 use crate::number::Endianness;
2183
2184 fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2185 u16(Endianness::Big)(i)
2186 }
2187 fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2188 u16(Endianness::Little)(i)
2189 }
2190 assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
2191 assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
2192
2193 fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2194 u32(Endianness::Big)(i)
2195 }
2196 fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2197 u32(Endianness::Little)(i)
2198 }
2199 assert_eq!(
2200 be_tst32(&[0x12, 0x00, 0x60, 0x00]),
2201 Ok((&b""[..], 302_014_464_u32))
2202 );
2203 assert_eq!(
2204 le_tst32(&[0x12, 0x00, 0x60, 0x00]),
2205 Ok((&b""[..], 6_291_474_u32))
2206 );
2207
2208 fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2209 u64(Endianness::Big)(i)
2210 }
2211 fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2212 u64(Endianness::Little)(i)
2213 }
2214 assert_eq!(
2215 be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2216 Ok((&b""[..], 1_297_142_246_100_992_000_u64))
2217 );
2218 assert_eq!(
2219 le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2220 Ok((&b""[..], 36_028_874_334_666_770_u64))
2221 );
2222
2223 fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2224 i16(Endianness::Big)(i)
2225 }
2226 fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2227 i16(Endianness::Little)(i)
2228 }
2229 assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
2230 assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
2231
2232 fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2233 i32(Endianness::Big)(i)
2234 }
2235 fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2236 i32(Endianness::Little)(i)
2237 }
2238 assert_eq!(
2239 be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2240 Ok((&b""[..], 1_204_224_i32))
2241 );
2242 assert_eq!(
2243 le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2244 Ok((&b""[..], 6_296_064_i32))
2245 );
2246
2247 fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2248 i64(Endianness::Big)(i)
2249 }
2250 fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2251 i64(Endianness::Little)(i)
2252 }
2253 assert_eq!(
2254 be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2255 Ok((&b""[..], 71_881_672_479_506_432_i64))
2256 );
2257 assert_eq!(
2258 le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2259 Ok((&b""[..], 36_028_874_334_732_032_i64))
2260 );
2261 }
2262
2263 #[cfg(feature = "std")]
2264 fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
2265 match recognize_float_or_exceptions(i) {
2266 Err(e) => Err(e),
2267 Ok((i, s)) => {
2268 if s.is_empty() {
2269 return Err(Err::Error(()));
2270 }
2271 match s.parse_to() {
2272 Some(n) => Ok((i, n)),
2273 None => Err(Err::Error(())),
2274 }
2275 }
2276 }
2277 }
2278
2279 proptest! {
2280 #[test]
2281 #[cfg(feature = "std")]
2282 fn floats(s in "\\PC*") {
2283 println!("testing {}", s);
2284 let res1 = parse_f64(&s);
2285 let res2 = double::<_, ()>(s.as_str());
2286 assert_eq!(res1, res2);
2287 }
2288 }
2289}