1#![allow(deprecated)]
4
5use crate::branch::alt;
6use crate::bytes::streaming::tag;
7use crate::character::streaming::{char, digit1, sign};
8use crate::combinator::{cut, map, opt, recognize};
9use crate::error::{ErrorKind, ParseError};
10use crate::input::{
11 AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, IntoOutput,
12 Offset, Slice,
13};
14use crate::lib::std::ops::{RangeFrom, RangeTo};
15use crate::sequence::{pair, tuple};
16use crate::*;
17
18#[inline]
33#[deprecated(
36 since = "8.0.0",
37 note = "Replaced with `nom8::number::be_u8` with input wrapped in `nom8::input::Streaming`"
38)]
39pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
40where
41 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
42{
43 let bound: usize = 1;
44 if input.input_len() < bound {
45 Err(Err::Incomplete(Needed::new(1)))
46 } else {
47 let res = input.iter_elements().next().unwrap();
48
49 Ok((input.slice(bound..), res))
50 }
51}
52
53#[inline]
69#[deprecated(
72 since = "8.0.0",
73 note = "Replaced with `nom8::number::be_u16` with input wrapped in `nom8::input::Streaming`"
74)]
75pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
76where
77 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
78{
79 let bound: usize = 2;
80 if input.input_len() < bound {
81 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
82 } else {
83 let mut res = 0u16;
84 for byte in input.iter_elements().take(bound) {
85 res = (res << 8) + byte as u16;
86 }
87
88 Ok((input.slice(bound..), res))
89 }
90}
91
92#[inline]
108#[deprecated(
111 since = "8.0.0",
112 note = "Replaced with `nom8::number::be_u24` with input wrapped in `nom8::input::Streaming`"
113)]
114pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
115where
116 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
117{
118 let bound: usize = 3;
119 if input.input_len() < bound {
120 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
121 } else {
122 let mut res = 0u32;
123 for byte in input.iter_elements().take(bound) {
124 res = (res << 8) + byte as u32;
125 }
126
127 Ok((input.slice(bound..), res))
128 }
129}
130
131#[inline]
147#[deprecated(
150 since = "8.0.0",
151 note = "Replaced with `nom8::number::be_u32` with input wrapped in `nom8::input::Streaming`"
152)]
153pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
154where
155 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
156{
157 let bound: usize = 4;
158 if input.input_len() < bound {
159 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
160 } else {
161 let mut res = 0u32;
162 for byte in input.iter_elements().take(bound) {
163 res = (res << 8) + byte as u32;
164 }
165
166 Ok((input.slice(bound..), res))
167 }
168}
169
170#[inline]
186#[deprecated(
189 since = "8.0.0",
190 note = "Replaced with `nom8::number::be_u64` with input wrapped in `nom8::input::Streaming`"
191)]
192pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
193where
194 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
195{
196 let bound: usize = 8;
197 if input.input_len() < bound {
198 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
199 } else {
200 let mut res = 0u64;
201 for byte in input.iter_elements().take(bound) {
202 res = (res << 8) + byte as u64;
203 }
204
205 Ok((input.slice(bound..), res))
206 }
207}
208
209#[inline]
224#[deprecated(
227 since = "8.0.0",
228 note = "Replaced with `nom8::number::be_u128` with input wrapped in `nom8::input::Streaming`"
229)]
230pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
231where
232 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
233{
234 let bound: usize = 16;
235 if input.input_len() < bound {
236 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
237 } else {
238 let mut res = 0u128;
239 for byte in input.iter_elements().take(bound) {
240 res = (res << 8) + byte as u128;
241 }
242
243 Ok((input.slice(bound..), res))
244 }
245}
246
247#[inline]
260#[deprecated(
263 since = "8.0.0",
264 note = "Replaced with `nom8::number::be_i8` with input wrapped in `nom8::input::Streaming`"
265)]
266pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
267where
268 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
269{
270 be_u8.map(|x| x as i8).parse(input)
271}
272
273#[inline]
286#[deprecated(
289 since = "8.0.0",
290 note = "Replaced with `nom8::number::be_i16` with input wrapped in `nom8::input::Streaming`"
291)]
292pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
293where
294 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
295{
296 be_u16.map(|x| x as i16).parse(input)
297}
298
299#[inline]
312#[deprecated(
315 since = "8.0.0",
316 note = "Replaced with `nom8::number::be_i24` with input wrapped in `nom8::input::Streaming`"
317)]
318pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
319where
320 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
321{
322 be_u24
324 .map(|x| {
325 if x & 0x80_00_00 != 0 {
326 (x | 0xff_00_00_00) as i32
327 } else {
328 x as i32
329 }
330 })
331 .parse(input)
332}
333
334#[inline]
347#[deprecated(
350 since = "8.0.0",
351 note = "Replaced with `nom8::number::be_i32` with input wrapped in `nom8::input::Streaming`"
352)]
353pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
354where
355 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
356{
357 be_u32.map(|x| x as i32).parse(input)
358}
359
360#[inline]
374#[deprecated(
377 since = "8.0.0",
378 note = "Replaced with `nom8::number::be_i64` with input wrapped in `nom8::input::Streaming`"
379)]
380pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
381where
382 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
383{
384 be_u64.map(|x| x as i64).parse(input)
385}
386
387#[inline]
400#[deprecated(
403 since = "8.0.0",
404 note = "Replaced with `nom8::number::be_i128` with input wrapped in `nom8::input::Streaming`"
405)]
406pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
407where
408 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
409{
410 be_u128.map(|x| x as i128).parse(input)
411}
412
413#[inline]
426#[deprecated(
429 since = "8.0.0",
430 note = "Replaced with `nom8::number::le_u8` with input wrapped in `nom8::input::Streaming`"
431)]
432pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
433where
434 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
435{
436 let bound: usize = 1;
437 if input.input_len() < bound {
438 Err(Err::Incomplete(Needed::new(1)))
439 } else {
440 let res = input.iter_elements().next().unwrap();
441
442 Ok((input.slice(bound..), res))
443 }
444}
445
446#[inline]
462#[deprecated(
465 since = "8.0.0",
466 note = "Replaced with `nom8::number::le_u16` with input wrapped in `nom8::input::Streaming`"
467)]
468pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
469where
470 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
471{
472 let bound: usize = 2;
473 if input.input_len() < bound {
474 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
475 } else {
476 let mut res = 0u16;
477 for (index, byte) in input.iter_indices().take(bound) {
478 res += (byte as u16) << (8 * index);
479 }
480
481 Ok((input.slice(bound..), res))
482 }
483}
484
485#[inline]
501#[deprecated(
504 since = "8.0.0",
505 note = "Replaced with `nom8::number::le_u24` with input wrapped in `nom8::input::Streaming`"
506)]
507pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
508where
509 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
510{
511 let bound: usize = 3;
512 if input.input_len() < bound {
513 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
514 } else {
515 let mut res = 0u32;
516 for (index, byte) in input.iter_indices().take(bound) {
517 res += (byte as u32) << (8 * index);
518 }
519
520 Ok((input.slice(bound..), res))
521 }
522}
523
524#[inline]
540#[deprecated(
543 since = "8.0.0",
544 note = "Replaced with `nom8::number::le_u32` with input wrapped in `nom8::input::Streaming`"
545)]
546pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
547where
548 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
549{
550 let bound: usize = 4;
551 if input.input_len() < bound {
552 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
553 } else {
554 let mut res = 0u32;
555 for (index, byte) in input.iter_indices().take(bound) {
556 res += (byte as u32) << (8 * index);
557 }
558
559 Ok((input.slice(bound..), res))
560 }
561}
562
563#[inline]
579#[deprecated(
582 since = "8.0.0",
583 note = "Replaced with `nom8::number::le_u64` with input wrapped in `nom8::input::Streaming`"
584)]
585pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
586where
587 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
588{
589 let bound: usize = 8;
590 if input.input_len() < bound {
591 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
592 } else {
593 let mut res = 0u64;
594 for (index, byte) in input.iter_indices().take(bound) {
595 res += (byte as u64) << (8 * index);
596 }
597
598 Ok((input.slice(bound..), res))
599 }
600}
601
602#[inline]
618#[deprecated(
621 since = "8.0.0",
622 note = "Replaced with `nom8::number::le_u128` with input wrapped in `nom8::input::Streaming`"
623)]
624pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
625where
626 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
627{
628 let bound: usize = 16;
629 if input.input_len() < bound {
630 Err(Err::Incomplete(Needed::new(bound - input.input_len())))
631 } else {
632 let mut res = 0u128;
633 for (index, byte) in input.iter_indices().take(bound) {
634 res += (byte as u128) << (8 * index);
635 }
636
637 Ok((input.slice(bound..), res))
638 }
639}
640
641#[inline]
654#[deprecated(
657 since = "8.0.0",
658 note = "Replaced with `nom8::number::le_i8` with input wrapped in `nom8::input::Streaming`"
659)]
660pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
661where
662 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
663{
664 le_u8.map(|x| x as i8).parse(input)
665}
666
667#[inline]
683#[deprecated(
686 since = "8.0.0",
687 note = "Replaced with `nom8::number::le_i16` with input wrapped in `nom8::input::Streaming`"
688)]
689pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
690where
691 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
692{
693 le_u16.map(|x| x as i16).parse(input)
694}
695
696#[inline]
712#[deprecated(
715 since = "8.0.0",
716 note = "Replaced with `nom8::number::le_i24` with input wrapped in `nom8::input::Streaming`"
717)]
718pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
719where
720 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
721{
722 le_u24
724 .map(|x| {
725 if x & 0x80_00_00 != 0 {
726 (x | 0xff_00_00_00) as i32
727 } else {
728 x as i32
729 }
730 })
731 .parse(input)
732}
733
734#[inline]
750#[deprecated(
753 since = "8.0.0",
754 note = "Replaced with `nom8::number::le_i32` with input wrapped in `nom8::input::Streaming`"
755)]
756pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
757where
758 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
759{
760 le_u32.map(|x| x as i32).parse(input)
761}
762
763#[inline]
779#[deprecated(
782 since = "8.0.0",
783 note = "Replaced with `nom8::number::le_i64` with input wrapped in `nom8::input::Streaming`"
784)]
785pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
786where
787 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
788{
789 le_u64.map(|x| x as i64).parse(input)
790}
791
792#[inline]
808#[deprecated(
811 since = "8.0.0",
812 note = "Replaced with `nom8::number::le_i128` with input wrapped in `nom8::input::Streaming`"
813)]
814pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
815where
816 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
817{
818 le_u128.map(|x| x as i128).parse(input)
819}
820
821#[inline]
838#[deprecated(
841 since = "8.0.0",
842 note = "Replaced with `nom8::number::u8` with input wrapped in `nom8::input::Streaming`"
843)]
844pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
845where
846 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
847{
848 let bound: usize = 1;
849 if input.input_len() < bound {
850 Err(Err::Incomplete(Needed::new(1)))
851 } else {
852 let res = input.iter_elements().next().unwrap();
853
854 Ok((input.slice(bound..), res))
855 }
856}
857
858#[inline]
884#[deprecated(
887 since = "8.0.0",
888 note = "Replaced with `nom8::number::u16` with input wrapped in `nom8::input::Streaming`"
889)]
890pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
891where
892 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
893{
894 match endian {
895 crate::number::Endianness::Big => be_u16,
896 crate::number::Endianness::Little => le_u16,
897 #[cfg(target_endian = "big")]
898 crate::number::Endianness::Native => be_u16,
899 #[cfg(target_endian = "little")]
900 crate::number::Endianness::Native => le_u16,
901 }
902}
903
904#[inline]
929#[deprecated(
932 since = "8.0.0",
933 note = "Replaced with `nom8::number::u24` with input wrapped in `nom8::input::Streaming`"
934)]
935pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
936where
937 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
938{
939 match endian {
940 crate::number::Endianness::Big => be_u24,
941 crate::number::Endianness::Little => le_u24,
942 #[cfg(target_endian = "big")]
943 crate::number::Endianness::Native => be_u24,
944 #[cfg(target_endian = "little")]
945 crate::number::Endianness::Native => le_u24,
946 }
947}
948
949#[inline]
974#[deprecated(
977 since = "8.0.0",
978 note = "Replaced with `nom8::number::u32` with input wrapped in `nom8::input::Streaming`"
979)]
980pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
981where
982 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
983{
984 match endian {
985 crate::number::Endianness::Big => be_u32,
986 crate::number::Endianness::Little => le_u32,
987 #[cfg(target_endian = "big")]
988 crate::number::Endianness::Native => be_u32,
989 #[cfg(target_endian = "little")]
990 crate::number::Endianness::Native => le_u32,
991 }
992}
993
994#[inline]
1019#[deprecated(
1022 since = "8.0.0",
1023 note = "Replaced with `nom8::number::u64` with input wrapped in `nom8::input::Streaming`"
1024)]
1025pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
1026where
1027 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1028{
1029 match endian {
1030 crate::number::Endianness::Big => be_u64,
1031 crate::number::Endianness::Little => le_u64,
1032 #[cfg(target_endian = "big")]
1033 crate::number::Endianness::Native => be_u64,
1034 #[cfg(target_endian = "little")]
1035 crate::number::Endianness::Native => le_u64,
1036 }
1037}
1038
1039#[inline]
1064#[deprecated(
1067 since = "8.0.0",
1068 note = "Replaced with `nom8::number::u128` with input wrapped in `nom8::input::Streaming`"
1069)]
1070pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
1071where
1072 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1073{
1074 match endian {
1075 crate::number::Endianness::Big => be_u128,
1076 crate::number::Endianness::Little => le_u128,
1077 #[cfg(target_endian = "big")]
1078 crate::number::Endianness::Native => be_u128,
1079 #[cfg(target_endian = "little")]
1080 crate::number::Endianness::Native => le_u128,
1081 }
1082}
1083
1084#[inline]
1101#[deprecated(
1104 since = "8.0.0",
1105 note = "Replaced with `nom8::number::i8` with input wrapped in `nom8::input::Streaming`"
1106)]
1107pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
1108where
1109 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1110{
1111 u8.map(|x| x as i8).parse(i)
1112}
1113
1114#[inline]
1139#[deprecated(
1142 since = "8.0.0",
1143 note = "Replaced with `nom8::number::i16` with input wrapped in `nom8::input::Streaming`"
1144)]
1145pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
1146where
1147 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1148{
1149 match endian {
1150 crate::number::Endianness::Big => be_i16,
1151 crate::number::Endianness::Little => le_i16,
1152 #[cfg(target_endian = "big")]
1153 crate::number::Endianness::Native => be_i16,
1154 #[cfg(target_endian = "little")]
1155 crate::number::Endianness::Native => le_i16,
1156 }
1157}
1158
1159#[inline]
1184#[deprecated(
1187 since = "8.0.0",
1188 note = "Replaced with `nom8::number::i24` with input wrapped in `nom8::input::Streaming`"
1189)]
1190pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1191where
1192 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1193{
1194 match endian {
1195 crate::number::Endianness::Big => be_i24,
1196 crate::number::Endianness::Little => le_i24,
1197 #[cfg(target_endian = "big")]
1198 crate::number::Endianness::Native => be_i24,
1199 #[cfg(target_endian = "little")]
1200 crate::number::Endianness::Native => le_i24,
1201 }
1202}
1203
1204#[inline]
1229#[deprecated(
1232 since = "8.0.0",
1233 note = "Replaced with `nom8::number::i32` with input wrapped in `nom8::input::Streaming`"
1234)]
1235pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1236where
1237 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1238{
1239 match endian {
1240 crate::number::Endianness::Big => be_i32,
1241 crate::number::Endianness::Little => le_i32,
1242 #[cfg(target_endian = "big")]
1243 crate::number::Endianness::Native => be_i32,
1244 #[cfg(target_endian = "little")]
1245 crate::number::Endianness::Native => le_i32,
1246 }
1247}
1248
1249#[inline]
1274#[deprecated(
1277 since = "8.0.0",
1278 note = "Replaced with `nom8::number::i64` with input wrapped in `nom8::input::Streaming`"
1279)]
1280pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
1281where
1282 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1283{
1284 match endian {
1285 crate::number::Endianness::Big => be_i64,
1286 crate::number::Endianness::Little => le_i64,
1287 #[cfg(target_endian = "big")]
1288 crate::number::Endianness::Native => be_i64,
1289 #[cfg(target_endian = "little")]
1290 crate::number::Endianness::Native => le_i64,
1291 }
1292}
1293
1294#[inline]
1319#[deprecated(
1322 since = "8.0.0",
1323 note = "Replaced with `nom8::number::i128` with input wrapped in `nom8::input::Streaming`"
1324)]
1325pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
1326where
1327 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1328{
1329 match endian {
1330 crate::number::Endianness::Big => be_i128,
1331 crate::number::Endianness::Little => le_i128,
1332 #[cfg(target_endian = "big")]
1333 crate::number::Endianness::Native => be_i128,
1334 #[cfg(target_endian = "little")]
1335 crate::number::Endianness::Native => le_i128,
1336 }
1337}
1338
1339#[inline]
1354#[deprecated(
1357 since = "8.0.0",
1358 note = "Replaced with `nom8::number::be_f32` with input wrapped in `nom8::input::Streaming`"
1359)]
1360pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1361where
1362 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1363{
1364 match be_u32(input) {
1365 Err(e) => Err(e),
1366 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1367 }
1368}
1369
1370#[inline]
1385#[deprecated(
1388 since = "8.0.0",
1389 note = "Replaced with `nom8::number::be_f64` with input wrapped in `nom8::input::Streaming`"
1390)]
1391pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1392where
1393 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1394{
1395 match be_u64(input) {
1396 Err(e) => Err(e),
1397 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1398 }
1399}
1400
1401#[inline]
1416#[deprecated(
1419 since = "8.0.0",
1420 note = "Replaced with `nom8::number::le_f32` with input wrapped in `nom8::input::Streaming`"
1421)]
1422pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1423where
1424 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1425{
1426 match le_u32(input) {
1427 Err(e) => Err(e),
1428 Ok((i, o)) => Ok((i, f32::from_bits(o))),
1429 }
1430}
1431
1432#[inline]
1447#[deprecated(
1450 since = "8.0.0",
1451 note = "Replaced with `nom8::number::le_f64` with input wrapped in `nom8::input::Streaming`"
1452)]
1453pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1454where
1455 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1456{
1457 match le_u64(input) {
1458 Err(e) => Err(e),
1459 Ok((i, o)) => Ok((i, f64::from_bits(o))),
1460 }
1461}
1462
1463#[inline]
1488#[deprecated(
1491 since = "8.0.0",
1492 note = "Replaced with `nom8::number::f32` with input wrapped in `nom8::input::Streaming`"
1493)]
1494pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1495where
1496 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1497{
1498 match endian {
1499 crate::number::Endianness::Big => be_f32,
1500 crate::number::Endianness::Little => le_f32,
1501 #[cfg(target_endian = "big")]
1502 crate::number::Endianness::Native => be_f32,
1503 #[cfg(target_endian = "little")]
1504 crate::number::Endianness::Native => le_f32,
1505 }
1506}
1507
1508#[inline]
1533#[deprecated(
1536 since = "8.0.0",
1537 note = "Replaced with `nom8::number::f64` with input wrapped in `nom8::input::Streaming`"
1538)]
1539pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1540where
1541 I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1542{
1543 match endian {
1544 crate::number::Endianness::Big => be_f64,
1545 crate::number::Endianness::Little => le_f64,
1546 #[cfg(target_endian = "big")]
1547 crate::number::Endianness::Native => be_f64,
1548 #[cfg(target_endian = "little")]
1549 crate::number::Endianness::Native => le_f64,
1550 }
1551}
1552
1553#[inline]
1569#[deprecated(
1572 since = "8.0.0",
1573 note = "Replaced with `nom8::number::hex_u32` with input wrapped in `nom8::input::Streaming`"
1574)]
1575pub fn hex_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
1576where
1577 I: InputTakeAtPosition,
1578 I: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1579 <I as InputTakeAtPosition>::Item: AsChar,
1580 I: AsBytes,
1581 I: InputLength,
1582{
1583 let e: ErrorKind = ErrorKind::IsA;
1584 let (i, o) = input.split_at_position1_streaming(
1585 |c| {
1586 let c = c.as_char();
1587 !"0123456789abcdefABCDEF".contains(c)
1588 },
1589 e,
1590 )?;
1591
1592 let (parsed, remaining) = if o.input_len() <= 8 {
1594 (o, i)
1595 } else {
1596 (input.slice(..8), input.slice(8..))
1597 };
1598
1599 let res = parsed
1600 .as_bytes()
1601 .iter()
1602 .rev()
1603 .enumerate()
1604 .map(|(k, &v)| {
1605 let digit = v as char;
1606 digit.to_digit(16).unwrap_or(0) << (k * 4)
1607 })
1608 .sum();
1609
1610 Ok((remaining, res))
1611}
1612
1613#[rustfmt::skip]
1631#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::recognize_float` with input wrapped in `nom8::input::Streaming`")]
1634pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, <T as IntoOutput>::Output, E>
1635where
1636 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1637 T: Clone + Offset,
1638 T: InputIter,
1639 T: IntoOutput,
1640 <T as InputIter>::Item: AsChar,
1641 T: InputTakeAtPosition + InputLength,
1642 <T as InputTakeAtPosition>::Item: AsChar
1643{
1644 recognize(
1645 tuple((
1646 opt(alt((char('+'), char('-')))),
1647 alt((
1648 map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
1649 map(tuple((char('.'), digit1)), |_| ())
1650 )),
1651 opt(tuple((
1652 alt((char('e'), char('E'))),
1653 opt(alt((char('+'), char('-')))),
1654 cut(digit1)
1655 )))
1656 ))
1657 )(input)
1658}
1659
1660#[doc(hidden)]
1662#[deprecated(
1665 since = "8.0.0",
1666 note = "Replaced with `nom8::character::recognize_float_or_exceptions` with input wrapped in `nom8::input::Streaming`"
1667)]
1668pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(
1669 input: T,
1670) -> IResult<T, <T as IntoOutput>::Output, E>
1671where
1672 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1673 T: Clone + Offset,
1674 T: InputIter + InputTake + InputLength + Compare<&'static str>,
1675 T: IntoOutput,
1676 <T as InputIter>::Item: AsChar,
1677 T: InputTakeAtPosition,
1678 <T as InputTakeAtPosition>::Item: AsChar,
1679{
1680 alt((
1681 |i: T| {
1682 recognize_float::<_, E>(i.clone()).map_err(|e| match e {
1683 crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
1684 crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
1685 crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
1686 })
1687 },
1688 |i: T| {
1689 crate::bytes::streaming::tag_no_case::<_, _, E>("nan")(i.clone())
1690 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1691 },
1692 |i: T| {
1693 crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
1694 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1695 },
1696 |i: T| {
1697 crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
1698 .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1699 },
1700 ))(input)
1701}
1702
1703#[deprecated(
1713 since = "8.0.0",
1714 note = "Replaced with `nom8::character::recognize_float_parts` with input wrapped in `nom8::input::Streaming`"
1715)]
1716pub fn recognize_float_parts<T, E: ParseError<T>>(
1717 input: T,
1718) -> IResult<
1719 T,
1720 (
1721 bool,
1722 <T as IntoOutput>::Output,
1723 <T as IntoOutput>::Output,
1724 i32,
1725 ),
1726 E,
1727>
1728where
1729 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1730 T: Clone + Offset,
1731 T: InputIter,
1732 T: IntoOutput,
1733 <T as InputIter>::Item: AsChar,
1734 T: InputTakeAtPosition + InputTake + InputLength,
1735 <T as InputTakeAtPosition>::Item: AsChar,
1736 T: for<'a> Compare<&'a [u8]>,
1737 T: AsBytes,
1738{
1739 let (i, sign) = sign(input.clone())?;
1740
1741 let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
1743 Some(index) => i.take_split(index),
1744 None => i.take_split(i.input_len()),
1745 };
1746
1747 let (i, mut integer) = match i
1749 .as_bytes()
1750 .iter()
1751 .position(|c| !(*c >= b'0' && *c <= b'9'))
1752 {
1753 Some(index) => i.take_split(index),
1754 None => i.take_split(i.input_len()),
1755 };
1756
1757 if integer.input_len() == 0 && zeroes.input_len() > 0 {
1758 integer = zeroes.slice(zeroes.input_len() - 1..);
1760 }
1761
1762 let (i, opt_dot) = opt(tag(&b"."[..]))(i)?;
1763 let (i, fraction) = if opt_dot.is_none() {
1764 let i2 = i.clone();
1765 (i2, i.slice(..0))
1766 } else {
1767 let mut zero_count = 0usize;
1769 let mut position = None;
1770 for (pos, c) in i.as_bytes().iter().enumerate() {
1771 if *c >= b'0' && *c <= b'9' {
1772 if *c == b'0' {
1773 zero_count += 1;
1774 } else {
1775 zero_count = 0;
1776 }
1777 } else {
1778 position = Some(pos);
1779 break;
1780 }
1781 }
1782
1783 let position = match position {
1784 Some(p) => p,
1785 None => return Err(Err::Incomplete(Needed::new(1))),
1786 };
1787
1788 let index = if zero_count == 0 {
1789 position
1790 } else if zero_count == position {
1791 position - zero_count + 1
1792 } else {
1793 position - zero_count
1794 };
1795
1796 (i.slice(position..), i.slice(..index))
1797 };
1798
1799 if integer.input_len() == 0 && fraction.input_len() == 0 {
1800 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
1801 }
1802
1803 let i2 = i.clone();
1804 let (i, e) = match i.as_bytes().iter().next() {
1805 Some(b'e') => (i.slice(1..), true),
1806 Some(b'E') => (i.slice(1..), true),
1807 _ => (i, false),
1808 };
1809
1810 let (i, exp) = if e {
1811 cut(crate::character::streaming::i32)(i)?
1812 } else {
1813 (i2, 0)
1814 };
1815
1816 Ok((
1817 i,
1818 (sign, integer.into_output(), fraction.into_output(), exp),
1819 ))
1820}
1821
1822#[deprecated(
1843 since = "8.0.0",
1844 note = "Replaced with `nom8::character::f32` with input wrapped in `nom8::input::Streaming`"
1845)]
1846pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1847where
1848 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1849 T: Clone + Offset,
1850 T: InputIter + InputLength + InputTake + Compare<&'static str>,
1851 T: IntoOutput,
1852 <T as IntoOutput>::Output: crate::input::ParseTo<f32>,
1853 <T as InputIter>::Item: AsChar,
1854 <T as InputIter>::IterElem: Clone,
1855 T: InputTakeAtPosition,
1856 <T as InputTakeAtPosition>::Item: AsChar,
1857 T: AsBytes,
1858 T: for<'a> Compare<&'a [u8]>,
1859{
1860 use crate::input::ParseTo;
1861
1862 let (i, s) = recognize_float_or_exceptions(input)?;
1863 match s.parse_to() {
1864 Some(f) => Ok((i, f)),
1865 None => Err(crate::Err::Error(E::from_error_kind(
1866 i,
1867 crate::error::ErrorKind::Float,
1868 ))),
1869 }
1870}
1871
1872#[deprecated(
1893 since = "8.0.0",
1894 note = "Replaced with `nom8::character::f64` with input wrapped in `nom8::input::Streaming`"
1895)]
1896pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1897where
1898 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1899 T: Clone + Offset,
1900 T: InputIter + InputLength + InputTake + Compare<&'static str>,
1901 T: IntoOutput,
1902 <T as IntoOutput>::Output: crate::input::ParseTo<f64>,
1903 <T as InputIter>::Item: AsChar,
1904 <T as InputIter>::IterElem: Clone,
1905 T: InputTakeAtPosition,
1906 <T as InputTakeAtPosition>::Item: AsChar,
1907 T: AsBytes,
1908 T: for<'a> Compare<&'a [u8]>,
1909{
1910 use crate::input::ParseTo;
1911
1912 let (i, s) = recognize_float_or_exceptions(input)?;
1913 match s.parse_to() {
1914 Some(f) => Ok((i, f)),
1915 None => Err(crate::Err::Error(E::from_error_kind(
1916 i,
1917 crate::error::ErrorKind::Float,
1918 ))),
1919 }
1920}
1921
1922#[cfg(test)]
1923mod tests {
1924 use super::*;
1925 use crate::error::ErrorKind;
1926 use crate::{Err, Needed};
1927 use proptest::prelude::*;
1928
1929 macro_rules! assert_parse(
1930 ($left: expr, $right: expr) => {
1931 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
1932 assert_eq!(res, $right);
1933 };
1934 );
1935
1936 #[test]
1937 fn i8_tests() {
1938 assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0)));
1939 assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1940 assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1)));
1941 assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128)));
1942 assert_parse!(be_i8(&[][..]), Err(Err::Incomplete(Needed::new(1))));
1943 }
1944
1945 #[test]
1946 fn i16_tests() {
1947 assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1948 assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16)));
1949 assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1950 assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16)));
1951 assert_parse!(be_i16(&[][..]), Err(Err::Incomplete(Needed::new(2))));
1952 assert_parse!(be_i16(&[0x00][..]), Err(Err::Incomplete(Needed::new(1))));
1953 }
1954
1955 #[test]
1956 fn u24_tests() {
1957 assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1958 assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32)));
1959 assert_parse!(
1960 be_u24(&[0x12, 0x34, 0x56][..]),
1961 Ok((&b""[..], 1_193_046_u32))
1962 );
1963 assert_parse!(be_u24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
1964 assert_parse!(be_u24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
1965 assert_parse!(
1966 be_u24(&[0x00, 0x00][..]),
1967 Err(Err::Incomplete(Needed::new(1)))
1968 );
1969 }
1970
1971 #[test]
1972 fn i24_tests() {
1973 assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1974 assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32)));
1975 assert_parse!(
1976 be_i24(&[0xED, 0xCB, 0xAA][..]),
1977 Ok((&b""[..], -1_193_046_i32))
1978 );
1979 assert_parse!(be_i24(&[][..]), Err(Err::Incomplete(Needed::new(3))));
1980 assert_parse!(be_i24(&[0x00][..]), Err(Err::Incomplete(Needed::new(2))));
1981 assert_parse!(
1982 be_i24(&[0x00, 0x00][..]),
1983 Err(Err::Incomplete(Needed::new(1)))
1984 );
1985 }
1986
1987 #[test]
1988 fn i32_tests() {
1989 assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1990 assert_parse!(
1991 be_i32(&[0x7f, 0xff, 0xff, 0xff][..]),
1992 Ok((&b""[..], 2_147_483_647_i32))
1993 );
1994 assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1995 assert_parse!(
1996 be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
1997 Ok((&b""[..], -2_147_483_648_i32))
1998 );
1999 assert_parse!(be_i32(&[][..]), Err(Err::Incomplete(Needed::new(4))));
2000 assert_parse!(be_i32(&[0x00][..]), Err(Err::Incomplete(Needed::new(3))));
2001 assert_parse!(
2002 be_i32(&[0x00, 0x00][..]),
2003 Err(Err::Incomplete(Needed::new(2)))
2004 );
2005 assert_parse!(
2006 be_i32(&[0x00, 0x00, 0x00][..]),
2007 Err(Err::Incomplete(Needed::new(1)))
2008 );
2009 }
2010
2011 #[test]
2012 fn i64_tests() {
2013 assert_parse!(
2014 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2015 Ok((&b""[..], 0))
2016 );
2017 assert_parse!(
2018 be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
2019 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
2020 );
2021 assert_parse!(
2022 be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
2023 Ok((&b""[..], -1))
2024 );
2025 assert_parse!(
2026 be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2027 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
2028 );
2029 assert_parse!(be_i64(&[][..]), Err(Err::Incomplete(Needed::new(8))));
2030 assert_parse!(be_i64(&[0x00][..]), Err(Err::Incomplete(Needed::new(7))));
2031 assert_parse!(
2032 be_i64(&[0x00, 0x00][..]),
2033 Err(Err::Incomplete(Needed::new(6)))
2034 );
2035 assert_parse!(
2036 be_i64(&[0x00, 0x00, 0x00][..]),
2037 Err(Err::Incomplete(Needed::new(5)))
2038 );
2039 assert_parse!(
2040 be_i64(&[0x00, 0x00, 0x00, 0x00][..]),
2041 Err(Err::Incomplete(Needed::new(4)))
2042 );
2043 assert_parse!(
2044 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
2045 Err(Err::Incomplete(Needed::new(3)))
2046 );
2047 assert_parse!(
2048 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2049 Err(Err::Incomplete(Needed::new(2)))
2050 );
2051 assert_parse!(
2052 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2053 Err(Err::Incomplete(Needed::new(1)))
2054 );
2055 }
2056
2057 #[test]
2058 fn i128_tests() {
2059 assert_parse!(
2060 be_i128(
2061 &[
2062 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2063 0x00
2064 ][..]
2065 ),
2066 Ok((&b""[..], 0))
2067 );
2068 assert_parse!(
2069 be_i128(
2070 &[
2071 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2072 0xff
2073 ][..]
2074 ),
2075 Ok((
2076 &b""[..],
2077 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
2078 ))
2079 );
2080 assert_parse!(
2081 be_i128(
2082 &[
2083 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2084 0xff
2085 ][..]
2086 ),
2087 Ok((&b""[..], -1))
2088 );
2089 assert_parse!(
2090 be_i128(
2091 &[
2092 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2093 0x00
2094 ][..]
2095 ),
2096 Ok((
2097 &b""[..],
2098 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
2099 ))
2100 );
2101 assert_parse!(be_i128(&[][..]), Err(Err::Incomplete(Needed::new(16))));
2102 assert_parse!(be_i128(&[0x00][..]), Err(Err::Incomplete(Needed::new(15))));
2103 assert_parse!(
2104 be_i128(&[0x00, 0x00][..]),
2105 Err(Err::Incomplete(Needed::new(14)))
2106 );
2107 assert_parse!(
2108 be_i128(&[0x00, 0x00, 0x00][..]),
2109 Err(Err::Incomplete(Needed::new(13)))
2110 );
2111 assert_parse!(
2112 be_i128(&[0x00, 0x00, 0x00, 0x00][..]),
2113 Err(Err::Incomplete(Needed::new(12)))
2114 );
2115 assert_parse!(
2116 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
2117 Err(Err::Incomplete(Needed::new(11)))
2118 );
2119 assert_parse!(
2120 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2121 Err(Err::Incomplete(Needed::new(10)))
2122 );
2123 assert_parse!(
2124 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2125 Err(Err::Incomplete(Needed::new(9)))
2126 );
2127 assert_parse!(
2128 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2129 Err(Err::Incomplete(Needed::new(8)))
2130 );
2131 assert_parse!(
2132 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2133 Err(Err::Incomplete(Needed::new(7)))
2134 );
2135 assert_parse!(
2136 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2137 Err(Err::Incomplete(Needed::new(6)))
2138 );
2139 assert_parse!(
2140 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2141 Err(Err::Incomplete(Needed::new(5)))
2142 );
2143 assert_parse!(
2144 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2145 Err(Err::Incomplete(Needed::new(4)))
2146 );
2147 assert_parse!(
2148 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2149 Err(Err::Incomplete(Needed::new(3)))
2150 );
2151 assert_parse!(
2152 be_i128(
2153 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]
2154 ),
2155 Err(Err::Incomplete(Needed::new(2)))
2156 );
2157 assert_parse!(
2158 be_i128(
2159 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
2160 [..]
2161 ),
2162 Err(Err::Incomplete(Needed::new(1)))
2163 );
2164 }
2165
2166 #[test]
2167 fn le_i8_tests() {
2168 assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0)));
2169 assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127)));
2170 assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1)));
2171 assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128)));
2172 }
2173
2174 #[test]
2175 fn le_i16_tests() {
2176 assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
2177 assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16)));
2178 assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
2179 assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
2180 }
2181
2182 #[test]
2183 fn le_u24_tests() {
2184 assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
2185 assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32)));
2186 assert_parse!(
2187 le_u24(&[0x56, 0x34, 0x12][..]),
2188 Ok((&b""[..], 1_193_046_u32))
2189 );
2190 }
2191
2192 #[test]
2193 fn le_i24_tests() {
2194 assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
2195 assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32)));
2196 assert_parse!(
2197 le_i24(&[0xAA, 0xCB, 0xED][..]),
2198 Ok((&b""[..], -1_193_046_i32))
2199 );
2200 }
2201
2202 #[test]
2203 fn le_i32_tests() {
2204 assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
2205 assert_parse!(
2206 le_i32(&[0xff, 0xff, 0xff, 0x7f][..]),
2207 Ok((&b""[..], 2_147_483_647_i32))
2208 );
2209 assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
2210 assert_parse!(
2211 le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
2212 Ok((&b""[..], -2_147_483_648_i32))
2213 );
2214 }
2215
2216 #[test]
2217 fn le_i64_tests() {
2218 assert_parse!(
2219 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2220 Ok((&b""[..], 0))
2221 );
2222 assert_parse!(
2223 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
2224 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
2225 );
2226 assert_parse!(
2227 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
2228 Ok((&b""[..], -1))
2229 );
2230 assert_parse!(
2231 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
2232 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
2233 );
2234 }
2235
2236 #[test]
2237 fn le_i128_tests() {
2238 assert_parse!(
2239 le_i128(
2240 &[
2241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2242 0x00
2243 ][..]
2244 ),
2245 Ok((&b""[..], 0))
2246 );
2247 assert_parse!(
2248 le_i128(
2249 &[
2250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2251 0x7f
2252 ][..]
2253 ),
2254 Ok((
2255 &b""[..],
2256 170_141_183_460_469_231_731_687_303_715_884_105_727_i128
2257 ))
2258 );
2259 assert_parse!(
2260 le_i128(
2261 &[
2262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2263 0xff
2264 ][..]
2265 ),
2266 Ok((&b""[..], -1))
2267 );
2268 assert_parse!(
2269 le_i128(
2270 &[
2271 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2272 0x80
2273 ][..]
2274 ),
2275 Ok((
2276 &b""[..],
2277 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
2278 ))
2279 );
2280 }
2281
2282 #[test]
2283 fn be_f32_tests() {
2284 assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2285 assert_parse!(
2286 be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
2287 Ok((&b""[..], 185_728_392_f32))
2288 );
2289 }
2290
2291 #[test]
2292 fn be_f64_tests() {
2293 assert_parse!(
2294 be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2295 Ok((&b""[..], 0_f64))
2296 );
2297 assert_parse!(
2298 be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
2299 Ok((&b""[..], 185_728_392_f64))
2300 );
2301 }
2302
2303 #[test]
2304 fn le_f32_tests() {
2305 assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2306 assert_parse!(
2307 le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
2308 Ok((&b""[..], 185_728_392_f32))
2309 );
2310 }
2311
2312 #[test]
2313 fn le_f64_tests() {
2314 assert_parse!(
2315 le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2316 Ok((&b""[..], 0_f64))
2317 );
2318 assert_parse!(
2319 le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
2320 Ok((&b""[..], 185_728_392_f64))
2321 );
2322 }
2323
2324 #[test]
2325 fn hex_u32_tests() {
2326 assert_parse!(
2327 hex_u32(&b";"[..]),
2328 Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
2329 );
2330 assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
2331 assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
2332 assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2333 assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2334 assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
2335 assert_parse!(
2336 hex_u32(&b"c5a31be201;"[..]),
2337 Ok((&b"01;"[..], 3_315_801_058))
2338 );
2339 assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
2340 assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
2341 assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::new(1))));
2342 }
2343
2344 #[test]
2345 #[cfg(feature = "std")]
2346 fn float_test() {
2347 let mut test_cases = vec![
2348 "+3.14",
2349 "3.14",
2350 "-3.14",
2351 "0",
2352 "0.0",
2353 "1.",
2354 ".789",
2355 "-.5",
2356 "1e7",
2357 "-1E-7",
2358 ".3e-2",
2359 "1.e4",
2360 "1.2e4",
2361 "12.34",
2362 "-1.234E-12",
2363 "-1.234e-12",
2364 "0.00000000000000000087",
2365 ];
2366
2367 for test in test_cases.drain(..) {
2368 let expected32 = str::parse::<f32>(test).unwrap();
2369 let expected64 = str::parse::<f64>(test).unwrap();
2370
2371 println!("now parsing: {} -> {}", test, expected32);
2372
2373 let larger = format!("{};", test);
2374 assert_parse!(recognize_float(&larger[..]), Ok((";", test)));
2375
2376 assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32)));
2377 assert_parse!(float(&larger[..]), Ok((";", expected32)));
2378
2379 assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64)));
2380 assert_parse!(double(&larger[..]), Ok((";", expected64)));
2381 }
2382
2383 let remaining_exponent = "-1.234E-";
2384 assert_parse!(
2385 recognize_float(remaining_exponent),
2386 Err(Err::Incomplete(Needed::new(1)))
2387 );
2388
2389 let (_i, nan) = float::<_, ()>("NaN").unwrap();
2390 assert!(nan.is_nan());
2391
2392 let (_i, inf) = float::<_, ()>("inf").unwrap();
2393 assert!(inf.is_infinite());
2394 let (_i, inf) = float::<_, ()>("infinite").unwrap();
2395 assert!(inf.is_infinite());
2396 }
2397
2398 #[test]
2399 fn configurable_endianness() {
2400 use crate::number::Endianness;
2401
2402 fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2403 u16(Endianness::Big)(i)
2404 }
2405 fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2406 u16(Endianness::Little)(i)
2407 }
2408 assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
2409 assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
2410
2411 fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2412 u32(Endianness::Big)(i)
2413 }
2414 fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2415 u32(Endianness::Little)(i)
2416 }
2417 assert_eq!(
2418 be_tst32(&[0x12, 0x00, 0x60, 0x00]),
2419 Ok((&b""[..], 302_014_464_u32))
2420 );
2421 assert_eq!(
2422 le_tst32(&[0x12, 0x00, 0x60, 0x00]),
2423 Ok((&b""[..], 6_291_474_u32))
2424 );
2425
2426 fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2427 u64(Endianness::Big)(i)
2428 }
2429 fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2430 u64(Endianness::Little)(i)
2431 }
2432 assert_eq!(
2433 be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2434 Ok((&b""[..], 1_297_142_246_100_992_000_u64))
2435 );
2436 assert_eq!(
2437 le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2438 Ok((&b""[..], 36_028_874_334_666_770_u64))
2439 );
2440
2441 fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2442 i16(Endianness::Big)(i)
2443 }
2444 fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2445 i16(Endianness::Little)(i)
2446 }
2447 assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
2448 assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
2449
2450 fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2451 i32(Endianness::Big)(i)
2452 }
2453 fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2454 i32(Endianness::Little)(i)
2455 }
2456 assert_eq!(
2457 be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2458 Ok((&b""[..], 1_204_224_i32))
2459 );
2460 assert_eq!(
2461 le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2462 Ok((&b""[..], 6_296_064_i32))
2463 );
2464
2465 fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2466 i64(Endianness::Big)(i)
2467 }
2468 fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2469 i64(Endianness::Little)(i)
2470 }
2471 assert_eq!(
2472 be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2473 Ok((&b""[..], 71_881_672_479_506_432_i64))
2474 );
2475 assert_eq!(
2476 le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2477 Ok((&b""[..], 36_028_874_334_732_032_i64))
2478 );
2479 }
2480
2481 #[cfg(feature = "std")]
2482 fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
2483 use crate::input::ParseTo;
2484 match recognize_float_or_exceptions(i) {
2485 Err(e) => Err(e),
2486 Ok((i, s)) => {
2487 if s.is_empty() {
2488 return Err(Err::Error(()));
2489 }
2490 match s.parse_to() {
2491 Some(n) => Ok((i, n)),
2492 None => Err(Err::Error(())),
2493 }
2494 }
2495 }
2496 }
2497
2498 proptest! {
2499 #[test]
2500 #[cfg(feature = "std")]
2501 fn floats(s in "\\PC*") {
2502 println!("testing {}", s);
2503 let res1 = parse_f64(&s);
2504 let res2 = double::<_, ()>(s.as_str());
2505 assert_eq!(res1, res2);
2506 }
2507 }
2508}