• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Parsers recognizing numbers, streaming version
2 
3 use crate::branch::alt;
4 use crate::bytes::streaming::tag;
5 use crate::character::streaming::{char, digit1, sign};
6 use crate::combinator::{cut, map, opt, recognize};
7 use crate::error::{ErrorKind, ParseError};
8 use crate::internal::*;
9 use crate::lib::std::ops::{RangeFrom, RangeTo};
10 use crate::sequence::{pair, tuple};
11 use crate::traits::{
12   AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, Offset, Slice,
13 };
14 
15 /// Recognizes an unsigned 1 byte integer.
16 ///
17 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
18 /// ```rust
19 /// # use nom::{Err, error::ErrorKind, Needed};
20 /// use nom::number::streaming::be_u8;
21 ///
22 /// let parser = |s| {
23 ///   be_u8::<_, (_, ErrorKind)>(s)
24 /// };
25 ///
26 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
27 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
28 /// ```
29 #[inline]
be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,30 pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
31 where
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 /// Recognizes a big endian unsigned 2 bytes integer.
45 ///
46 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
47 ///
48 /// ```rust
49 /// # use nom::{Err, error::ErrorKind, Needed};
50 /// use nom::number::streaming::be_u16;
51 ///
52 /// let parser = |s| {
53 ///   be_u16::<_, (_, ErrorKind)>(s)
54 /// };
55 ///
56 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
57 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
58 /// ```
59 #[inline]
be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,60 pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
61 where
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 /// Recognizes a big endian unsigned 3 byte integer.
78 ///
79 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
80 ///
81 /// ```rust
82 /// # use nom::{Err, error::ErrorKind, Needed};
83 /// use nom::number::streaming::be_u24;
84 ///
85 /// let parser = |s| {
86 ///   be_u24::<_, (_, ErrorKind)>(s)
87 /// };
88 ///
89 /// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
90 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
91 /// ```
92 #[inline]
be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,93 pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
94 where
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 /// Recognizes a big endian unsigned 4 bytes integer.
111 ///
112 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
113 ///
114 /// ```rust
115 /// # use nom::{Err, error::ErrorKind, Needed};
116 /// use nom::number::streaming::be_u32;
117 ///
118 /// let parser = |s| {
119 ///   be_u32::<_, (_, ErrorKind)>(s)
120 /// };
121 ///
122 /// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
123 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
124 /// ```
125 #[inline]
be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,126 pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
127 where
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 /// Recognizes a big endian unsigned 8 bytes integer.
144 ///
145 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
146 ///
147 /// ```rust
148 /// # use nom::{Err, error::ErrorKind, Needed};
149 /// use nom::number::streaming::be_u64;
150 ///
151 /// let parser = |s| {
152 ///   be_u64::<_, (_, ErrorKind)>(s)
153 /// };
154 ///
155 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
156 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
157 /// ```
158 #[inline]
be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,159 pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
160 where
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 /// Recognizes a big endian unsigned 16 bytes integer.
177 ///
178 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
179 /// ```rust
180 /// # use nom::{Err, error::ErrorKind, Needed};
181 /// use nom::number::streaming::be_u128;
182 ///
183 /// let parser = |s| {
184 ///   be_u128::<_, (_, ErrorKind)>(s)
185 /// };
186 ///
187 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
188 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
189 /// ```
190 #[inline]
be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,191 pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
192 where
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 /// Recognizes a signed 1 byte integer.
209 ///
210 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
211 /// ```rust
212 /// # use nom::{Err, error::ErrorKind, Needed};
213 /// use nom::number::streaming::be_i8;
214 ///
215 /// let parser = be_i8::<_, (_, ErrorKind)>;
216 ///
217 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
218 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
219 /// ```
220 #[inline]
be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,221 pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
222 where
223   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
224 {
225   be_u8.map(|x| x as i8).parse(input)
226 }
227 
228 /// Recognizes a big endian signed 2 bytes integer.
229 ///
230 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
231 /// ```rust
232 /// # use nom::{Err, error::ErrorKind, Needed};
233 /// use nom::number::streaming::be_i16;
234 ///
235 /// let parser = be_i16::<_, (_, ErrorKind)>;
236 ///
237 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
238 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(2))));
239 /// ```
240 #[inline]
be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,241 pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
242 where
243   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
244 {
245   be_u16.map(|x| x as i16).parse(input)
246 }
247 
248 /// Recognizes a big endian signed 3 bytes integer.
249 ///
250 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
251 /// ```rust
252 /// # use nom::{Err, error::ErrorKind, Needed};
253 /// use nom::number::streaming::be_i24;
254 ///
255 /// let parser = be_i24::<_, (_, ErrorKind)>;
256 ///
257 /// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
258 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(3))));
259 /// ```
260 #[inline]
be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,261 pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
262 where
263   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
264 {
265   // Same as the unsigned version but we need to sign-extend manually here
266   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 /// Recognizes a big endian signed 4 bytes integer.
278 ///
279 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
280 /// ```rust
281 /// # use nom::{Err, error::ErrorKind, Needed};
282 /// use nom::number::streaming::be_i32;
283 ///
284 /// let parser = be_i32::<_, (_, ErrorKind)>;
285 ///
286 /// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
287 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(4))));
288 /// ```
289 #[inline]
be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,290 pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
291 where
292   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
293 {
294   be_u32.map(|x| x as i32).parse(input)
295 }
296 
297 /// Recognizes a big endian signed 8 bytes integer.
298 ///
299 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
300 ///
301 /// ```rust
302 /// # use nom::{Err, error::ErrorKind, Needed};
303 /// use nom::number::streaming::be_i64;
304 ///
305 /// let parser = be_i64::<_, (_, ErrorKind)>;
306 ///
307 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
308 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
309 /// ```
310 #[inline]
be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,311 pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
312 where
313   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
314 {
315   be_u64.map(|x| x as i64).parse(input)
316 }
317 
318 /// Recognizes a big endian signed 16 bytes integer.
319 ///
320 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
321 /// ```rust
322 /// # use nom::{Err, error::ErrorKind, Needed};
323 /// use nom::number::streaming::be_i128;
324 ///
325 /// let parser = be_i128::<_, (_, ErrorKind)>;
326 ///
327 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
328 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
329 /// ```
330 #[inline]
be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,331 pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
332 where
333   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
334 {
335   be_u128.map(|x| x as i128).parse(input)
336 }
337 
338 /// Recognizes an unsigned 1 byte integer.
339 ///
340 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
341 /// ```rust
342 /// # use nom::{Err, error::ErrorKind, Needed};
343 /// use nom::number::streaming::le_u8;
344 ///
345 /// let parser = le_u8::<_, (_, ErrorKind)>;
346 ///
347 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
348 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
349 /// ```
350 #[inline]
le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,351 pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
352 where
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 /// Recognizes a little endian unsigned 2 bytes integer.
366 ///
367 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
368 ///
369 /// ```rust
370 /// # use nom::{Err, error::ErrorKind, Needed};
371 /// use nom::number::streaming::le_u16;
372 ///
373 /// let parser = |s| {
374 ///   le_u16::<_, (_, ErrorKind)>(s)
375 /// };
376 ///
377 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
378 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
379 /// ```
380 #[inline]
le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,381 pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
382 where
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 /// Recognizes a little endian unsigned 3 bytes integer.
399 ///
400 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
401 ///
402 /// ```rust
403 /// # use nom::{Err, error::ErrorKind, Needed};
404 /// use nom::number::streaming::le_u24;
405 ///
406 /// let parser = |s| {
407 ///   le_u24::<_, (_, ErrorKind)>(s)
408 /// };
409 ///
410 /// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
411 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
412 /// ```
413 #[inline]
le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,414 pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
415 where
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 /// Recognizes a little endian unsigned 4 bytes integer.
432 ///
433 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
434 ///
435 /// ```rust
436 /// # use nom::{Err, error::ErrorKind, Needed};
437 /// use nom::number::streaming::le_u32;
438 ///
439 /// let parser = |s| {
440 ///   le_u32::<_, (_, ErrorKind)>(s)
441 /// };
442 ///
443 /// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
444 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
445 /// ```
446 #[inline]
le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,447 pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
448 where
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 /// Recognizes a little endian unsigned 8 bytes integer.
465 ///
466 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
467 ///
468 /// ```rust
469 /// # use nom::{Err, error::ErrorKind, Needed};
470 /// use nom::number::streaming::le_u64;
471 ///
472 /// let parser = |s| {
473 ///   le_u64::<_, (_, ErrorKind)>(s)
474 /// };
475 ///
476 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
477 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
478 /// ```
479 #[inline]
le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,480 pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
481 where
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 /// Recognizes a little endian unsigned 16 bytes integer.
498 ///
499 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
500 ///
501 /// ```rust
502 /// # use nom::{Err, error::ErrorKind, Needed};
503 /// use nom::number::streaming::le_u128;
504 ///
505 /// let parser = |s| {
506 ///   le_u128::<_, (_, ErrorKind)>(s)
507 /// };
508 ///
509 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
510 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
511 /// ```
512 #[inline]
le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,513 pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
514 where
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 /// Recognizes a signed 1 byte integer.
531 ///
532 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
533 /// ```rust
534 /// # use nom::{Err, error::ErrorKind, Needed};
535 /// use nom::number::streaming::le_i8;
536 ///
537 /// let parser = le_i8::<_, (_, ErrorKind)>;
538 ///
539 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
540 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
541 /// ```
542 #[inline]
le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,543 pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
544 where
545   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
546 {
547   le_u8.map(|x| x as i8).parse(input)
548 }
549 
550 /// Recognizes a little endian signed 2 bytes integer.
551 ///
552 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
553 ///
554 /// ```rust
555 /// # use nom::{Err, error::ErrorKind, Needed};
556 /// use nom::number::streaming::le_i16;
557 ///
558 /// let parser = |s| {
559 ///   le_i16::<_, (_, ErrorKind)>(s)
560 /// };
561 ///
562 /// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
563 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
564 /// ```
565 #[inline]
le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,566 pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
567 where
568   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
569 {
570   le_u16.map(|x| x as i16).parse(input)
571 }
572 
573 /// Recognizes a little endian signed 3 bytes integer.
574 ///
575 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
576 ///
577 /// ```rust
578 /// # use nom::{Err, error::ErrorKind, Needed};
579 /// use nom::number::streaming::le_i24;
580 ///
581 /// let parser = |s| {
582 ///   le_i24::<_, (_, ErrorKind)>(s)
583 /// };
584 ///
585 /// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
586 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
587 /// ```
588 #[inline]
le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,589 pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
590 where
591   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
592 {
593   // Same as the unsigned version but we need to sign-extend manually here
594   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 /// Recognizes a little endian signed 4 bytes integer.
606 ///
607 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
608 ///
609 /// ```rust
610 /// # use nom::{Err, error::ErrorKind, Needed};
611 /// use nom::number::streaming::le_i32;
612 ///
613 /// let parser = |s| {
614 ///   le_i32::<_, (_, ErrorKind)>(s)
615 /// };
616 ///
617 /// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
618 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
619 /// ```
620 #[inline]
le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,621 pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
622 where
623   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
624 {
625   le_u32.map(|x| x as i32).parse(input)
626 }
627 
628 /// Recognizes a little endian signed 8 bytes integer.
629 ///
630 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
631 ///
632 /// ```rust
633 /// # use nom::{Err, error::ErrorKind, Needed};
634 /// use nom::number::streaming::le_i64;
635 ///
636 /// let parser = |s| {
637 ///   le_i64::<_, (_, ErrorKind)>(s)
638 /// };
639 ///
640 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
641 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
642 /// ```
643 #[inline]
le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,644 pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
645 where
646   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
647 {
648   le_u64.map(|x| x as i64).parse(input)
649 }
650 
651 /// Recognizes a little endian signed 16 bytes integer.
652 ///
653 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
654 ///
655 /// ```rust
656 /// # use nom::{Err, error::ErrorKind, Needed};
657 /// use nom::number::streaming::le_i128;
658 ///
659 /// let parser = |s| {
660 ///   le_i128::<_, (_, ErrorKind)>(s)
661 /// };
662 ///
663 /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
664 /// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
665 /// ```
666 #[inline]
le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,667 pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
668 where
669   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
670 {
671   le_u128.map(|x| x as i128).parse(input)
672 }
673 
674 /// Recognizes an unsigned 1 byte integer
675 ///
676 /// Note that endianness does not apply to 1 byte numbers.
677 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
678 /// ```rust
679 /// # use nom::{Err, error::ErrorKind, Needed};
680 /// # use nom::Needed::Size;
681 /// use nom::number::streaming::u8;
682 ///
683 /// let parser = |s| {
684 ///   u8::<_, (_, ErrorKind)>(s)
685 /// };
686 ///
687 /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
688 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
689 /// ```
690 #[inline]
u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,691 pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
692 where
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 /// Recognizes an unsigned 2 bytes integer
706 ///
707 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian u16 integer,
708 /// otherwise if `nom::number::Endianness::Little` parse a little endian u16 integer.
709 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
710 ///
711 /// ```rust
712 /// # use nom::{Err, error::ErrorKind, Needed};
713 /// # use nom::Needed::Size;
714 /// use nom::number::streaming::u16;
715 ///
716 /// let be_u16 = |s| {
717 ///   u16::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
718 /// };
719 ///
720 /// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
721 /// assert_eq!(be_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
722 ///
723 /// let le_u16 = |s| {
724 ///   u16::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
725 /// };
726 ///
727 /// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
728 /// assert_eq!(le_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
729 /// ```
730 #[inline]
u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,731 pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
732 where
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 /// Recognizes an unsigned 3 byte integer
746 ///
747 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian u24 integer,
748 /// otherwise if `nom::number::Endianness::Little` parse a little endian u24 integer.
749 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
750 /// ```rust
751 /// # use nom::{Err, error::ErrorKind, Needed};
752 /// # use nom::Needed::Size;
753 /// use nom::number::streaming::u24;
754 ///
755 /// let be_u24 = |s| {
756 ///   u24::<_,(_, ErrorKind)>(nom::number::Endianness::Big)(s)
757 /// };
758 ///
759 /// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
760 /// assert_eq!(be_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
761 ///
762 /// let le_u24 = |s| {
763 ///   u24::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
764 /// };
765 ///
766 /// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
767 /// assert_eq!(le_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
768 /// ```
769 #[inline]
u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,770 pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
771 where
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 /// Recognizes an unsigned 4 byte integer
785 ///
786 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian u32 integer,
787 /// otherwise if `nom::number::Endianness::Little` parse a little endian u32 integer.
788 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
789 /// ```rust
790 /// # use nom::{Err, error::ErrorKind, Needed};
791 /// # use nom::Needed::Size;
792 /// use nom::number::streaming::u32;
793 ///
794 /// let be_u32 = |s| {
795 ///   u32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
796 /// };
797 ///
798 /// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
799 /// assert_eq!(be_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
800 ///
801 /// let le_u32 = |s| {
802 ///   u32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
803 /// };
804 ///
805 /// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
806 /// assert_eq!(le_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
807 /// ```
808 #[inline]
u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,809 pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
810 where
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 /// Recognizes an unsigned 8 byte integer
824 ///
825 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian u64 integer,
826 /// otherwise if `nom::number::Endianness::Little` parse a little endian u64 integer.
827 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
828 /// ```rust
829 /// # use nom::{Err, error::ErrorKind, Needed};
830 /// # use nom::Needed::Size;
831 /// use nom::number::streaming::u64;
832 ///
833 /// let be_u64 = |s| {
834 ///   u64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
835 /// };
836 ///
837 /// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
838 /// assert_eq!(be_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
839 ///
840 /// let le_u64 = |s| {
841 ///   u64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
842 /// };
843 ///
844 /// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
845 /// assert_eq!(le_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
846 /// ```
847 #[inline]
u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,848 pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
849 where
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 /// Recognizes an unsigned 16 byte integer
863 ///
864 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian u128 integer,
865 /// otherwise if `nom::number::Endianness::Little` parse a little endian u128 integer.
866 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
867 /// ```rust
868 /// # use nom::{Err, error::ErrorKind, Needed};
869 /// # use nom::Needed::Size;
870 /// use nom::number::streaming::u128;
871 ///
872 /// let be_u128 = |s| {
873 ///   u128::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
874 /// };
875 ///
876 /// assert_eq!(be_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
877 /// assert_eq!(be_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
878 ///
879 /// let le_u128 = |s| {
880 ///   u128::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
881 /// };
882 ///
883 /// assert_eq!(le_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
884 /// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
885 /// ```
886 #[inline]
u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,887 pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
888 where
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 /// Recognizes a signed 1 byte integer
902 ///
903 /// Note that endianness does not apply to 1 byte numbers.
904 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
905 /// ```rust
906 /// # use nom::{Err, error::ErrorKind, Needed};
907 /// # use nom::Needed::Size;
908 /// use nom::number::streaming::i8;
909 ///
910 /// let parser = |s| {
911 ///   i8::<_, (_, ErrorKind)>(s)
912 /// };
913 ///
914 /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
915 /// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
916 /// ```
917 #[inline]
i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,918 pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
919 where
920   I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
921 {
922   u8.map(|x| x as i8).parse(i)
923 }
924 
925 /// Recognizes a signed 2 byte integer
926 ///
927 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian i16 integer,
928 /// otherwise if `nom::number::Endianness::Little` parse a little endian i16 integer.
929 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
930 /// ```rust
931 /// # use nom::{Err, error::ErrorKind, Needed};
932 /// # use nom::Needed::Size;
933 /// use nom::number::streaming::i16;
934 ///
935 /// let be_i16 = |s| {
936 ///   i16::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
937 /// };
938 ///
939 /// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
940 /// assert_eq!(be_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
941 ///
942 /// let le_i16 = |s| {
943 ///   i16::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
944 /// };
945 ///
946 /// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
947 /// assert_eq!(le_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
948 /// ```
949 #[inline]
i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,950 pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
951 where
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 /// Recognizes a signed 3 byte integer
965 ///
966 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian i24 integer,
967 /// otherwise if `nom::number::Endianness::Little` parse a little endian i24 integer.
968 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
969 /// ```rust
970 /// # use nom::{Err, error::ErrorKind, Needed};
971 /// # use nom::Needed::Size;
972 /// use nom::number::streaming::i24;
973 ///
974 /// let be_i24 = |s| {
975 ///   i24::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
976 /// };
977 ///
978 /// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
979 /// assert_eq!(be_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
980 ///
981 /// let le_i24 = |s| {
982 ///   i24::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
983 /// };
984 ///
985 /// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
986 /// assert_eq!(le_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
987 /// ```
988 #[inline]
i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,989 pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
990 where
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 /// Recognizes a signed 4 byte integer
1004 ///
1005 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian i32 integer,
1006 /// otherwise if `nom::number::Endianness::Little` parse a little endian i32 integer.
1007 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1008 /// ```rust
1009 /// # use nom::{Err, error::ErrorKind, Needed};
1010 /// # use nom::Needed::Size;
1011 /// use nom::number::streaming::i32;
1012 ///
1013 /// let be_i32 = |s| {
1014 ///   i32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
1015 /// };
1016 ///
1017 /// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
1018 /// assert_eq!(be_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
1019 ///
1020 /// let le_i32 = |s| {
1021 ///   i32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
1022 /// };
1023 ///
1024 /// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
1025 /// assert_eq!(le_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
1026 /// ```
1027 #[inline]
i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1028 pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1029 where
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 /// Recognizes a signed 8 byte integer
1043 ///
1044 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian i64 integer,
1045 /// otherwise if `nom::number::Endianness::Little` parse a little endian i64 integer.
1046 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1047 /// ```rust
1048 /// # use nom::{Err, error::ErrorKind, Needed};
1049 /// # use nom::Needed::Size;
1050 /// use nom::number::streaming::i64;
1051 ///
1052 /// let be_i64 = |s| {
1053 ///   i64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
1054 /// };
1055 ///
1056 /// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
1057 /// assert_eq!(be_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1058 ///
1059 /// let le_i64 = |s| {
1060 ///   i64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
1061 /// };
1062 ///
1063 /// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
1064 /// assert_eq!(le_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1065 /// ```
1066 #[inline]
i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1067 pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
1068 where
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 /// Recognizes a signed 16 byte integer
1082 ///
1083 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian i128 integer,
1084 /// otherwise if `nom::number::Endianness::Little` parse a little endian i128 integer.
1085 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1086 /// ```rust
1087 /// # use nom::{Err, error::ErrorKind, Needed};
1088 /// # use nom::Needed::Size;
1089 /// use nom::number::streaming::i128;
1090 ///
1091 /// let be_i128 = |s| {
1092 ///   i128::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
1093 /// };
1094 ///
1095 /// assert_eq!(be_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
1096 /// assert_eq!(be_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1097 ///
1098 /// let le_i128 = |s| {
1099 ///   i128::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
1100 /// };
1101 ///
1102 /// assert_eq!(le_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
1103 /// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1104 /// ```
1105 #[inline]
i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1106 pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
1107 where
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 /// Recognizes a big endian 4 bytes floating point number.
1121 ///
1122 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1123 /// ```rust
1124 /// # use nom::{Err, error::ErrorKind, Needed};
1125 /// use nom::number::streaming::be_f32;
1126 ///
1127 /// let parser = |s| {
1128 ///   be_f32::<_, (_, ErrorKind)>(s)
1129 /// };
1130 ///
1131 /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625)));
1132 /// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
1133 /// ```
1134 #[inline]
be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1135 pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1136 where
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 /// Recognizes a big endian 8 bytes floating point number.
1146 ///
1147 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1148 /// ```rust
1149 /// # use nom::{Err, error::ErrorKind, Needed};
1150 /// use nom::number::streaming::be_f64;
1151 ///
1152 /// let parser = |s| {
1153 ///   be_f64::<_, (_, ErrorKind)>(s)
1154 /// };
1155 ///
1156 /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1157 /// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
1158 /// ```
1159 #[inline]
be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1160 pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1161 where
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 /// Recognizes a little endian 4 bytes floating point number.
1171 ///
1172 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1173 /// ```rust
1174 /// # use nom::{Err, error::ErrorKind, Needed};
1175 /// use nom::number::streaming::le_f32;
1176 ///
1177 /// let parser = |s| {
1178 ///   le_f32::<_, (_, ErrorKind)>(s)
1179 /// };
1180 ///
1181 /// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1182 /// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
1183 /// ```
1184 #[inline]
le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1185 pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1186 where
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 /// Recognizes a little endian 8 bytes floating point number.
1196 ///
1197 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1198 /// ```rust
1199 /// # use nom::{Err, error::ErrorKind, Needed};
1200 /// use nom::number::streaming::le_f64;
1201 ///
1202 /// let parser = |s| {
1203 ///   le_f64::<_, (_, ErrorKind)>(s)
1204 /// };
1205 ///
1206 /// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0)));
1207 /// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
1208 /// ```
1209 #[inline]
le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1210 pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1211 where
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 /// Recognizes a 4 byte floating point number
1221 ///
1222 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian f32 float,
1223 /// otherwise if `nom::number::Endianness::Little` parse a little endian f32 float.
1224 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1225 /// ```rust
1226 /// # use nom::{Err, error::ErrorKind, Needed};
1227 /// # use nom::Needed::Size;
1228 /// use nom::number::streaming::f32;
1229 ///
1230 /// let be_f32 = |s| {
1231 ///   f32::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
1232 /// };
1233 ///
1234 /// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1235 /// assert_eq!(be_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
1236 ///
1237 /// let le_f32 = |s| {
1238 ///   f32::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
1239 /// };
1240 ///
1241 /// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1242 /// assert_eq!(le_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
1243 /// ```
1244 #[inline]
f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1245 pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1246 where
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 /// Recognizes an 8 byte floating point number
1260 ///
1261 /// If the parameter is `nom::number::Endianness::Big`, parse a big endian f64 float,
1262 /// otherwise if `nom::number::Endianness::Little` parse a little endian f64 float.
1263 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1264 /// ```rust
1265 /// # use nom::{Err, error::ErrorKind, Needed};
1266 /// # use nom::Needed::Size;
1267 /// use nom::number::streaming::f64;
1268 ///
1269 /// let be_f64 = |s| {
1270 ///   f64::<_, (_, ErrorKind)>(nom::number::Endianness::Big)(s)
1271 /// };
1272 ///
1273 /// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1274 /// assert_eq!(be_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
1275 ///
1276 /// let le_f64 = |s| {
1277 ///   f64::<_, (_, ErrorKind)>(nom::number::Endianness::Little)(s)
1278 /// };
1279 ///
1280 /// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
1281 /// assert_eq!(le_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
1282 /// ```
1283 #[inline]
f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E> where I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,1284 pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1285 where
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 /// Recognizes a hex-encoded integer.
1299 ///
1300 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1301 /// ```rust
1302 /// # use nom::{Err, error::ErrorKind, Needed};
1303 /// use nom::number::streaming::hex_u32;
1304 ///
1305 /// let parser = |s| {
1306 ///   hex_u32(s)
1307 /// };
1308 ///
1309 /// assert_eq!(parser(b"01AE;"), Ok((&b";"[..], 0x01AE)));
1310 /// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::new(1))));
1311 /// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
1312 /// ```
1313 #[inline]
hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u32, E>1314 pub 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   // Do not parse more than 8 characters for a u32
1318   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 /// Recognizes a floating point number in text format and returns the corresponding part of the input.
1338 ///
1339 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input.
1340 ///
1341 /// ```rust
1342 /// # use nom::{Err, error::ErrorKind, Needed};
1343 /// use nom::number::streaming::recognize_float;
1344 ///
1345 /// let parser = |s| {
1346 ///   recognize_float(s)
1347 /// };
1348 ///
1349 /// assert_eq!(parser("11e-1;"), Ok((";", "11e-1")));
1350 /// assert_eq!(parser("123E-02;"), Ok((";", "123E-02")));
1351 /// assert_eq!(parser("123K-01"), Ok(("K-01", "123")));
1352 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
1353 /// ```
1354 #[rustfmt::skip]
recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition + InputLength, <T as InputTakeAtPosition>::Item: AsChar1355 pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
1356 where
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 // workaround until issues with minimal-lexical are fixed
1381 #[doc(hidden)]
recognize_float_or_exceptions<T, E: ParseError<T>>(input: T) -> IResult<T, T, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputTake + InputLength + Compare<&'static str>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar,1382 pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
1383 where
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 
1414 /// Recognizes a floating point number in text format
1415 ///
1416 /// It returns a tuple of (`sign`, `integer part`, `fraction part` and `exponent`) of the input
1417 /// data.
1418 ///
1419 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1420 ///
recognize_float_parts<T, E: ParseError<T>>(input: T) -> IResult<T, (bool, T, T, i32), E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + crate::traits::ParseTo<i32>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition + InputTake + InputLength, <T as InputTakeAtPosition>::Item: AsChar, T: for<'a> Compare<&'a [u8]>, T: AsBytes,1421 pub fn recognize_float_parts<T, E: ParseError<T>>(input: T) -> IResult<T, (bool, T, T, i32), E>
1422 where
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) = take_while(|c: <T as InputTakeAtPosition>::Item| c.as_char() == '0')(i)?;
1435   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) = digit0(i)?;
1441   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     // keep the last zero if integer is empty
1452     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     // match number, trim right zeroes
1461     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 
1512 /// Recognizes floating point number in text format and returns a f32.
1513 ///
1514 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1515 ///
1516 /// ```rust
1517 /// # use nom::{Err, error::ErrorKind, Needed};
1518 /// # use nom::Needed::Size;
1519 /// use nom::number::complete::float;
1520 ///
1521 /// let parser = |s| {
1522 ///   float(s)
1523 /// };
1524 ///
1525 /// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1526 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1527 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1528 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1529 /// ```
float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f32> + Compare<&'static str>, <T as InputIter>::Item: AsChar, <T as InputIter>::IterElem: Clone, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar, T: AsBytes, T: for<'a> Compare<&'a [u8]>,1530 pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1531 where
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   /*
1543   let (i, (sign, integer, fraction, exponent)) = recognize_float_parts(input)?;
1544 
1545   let mut float: f32 = minimal_lexical::parse_float(
1546     integer.as_bytes().iter(),
1547     fraction.as_bytes().iter(),
1548     exponent,
1549   );
1550   if !sign {
1551     float = -float;
1552   }
1553 
1554   Ok((i, float))
1555   */
1556   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 
1566 /// Recognizes floating point number in text format and returns a f64.
1567 ///
1568 /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
1569 ///
1570 /// ```rust
1571 /// # use nom::{Err, error::ErrorKind, Needed};
1572 /// # use nom::Needed::Size;
1573 /// use nom::number::complete::double;
1574 ///
1575 /// let parser = |s| {
1576 ///   double(s)
1577 /// };
1578 ///
1579 /// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1580 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1581 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1582 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1583 /// ```
double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f64> + Compare<&'static str>, <T as InputIter>::Item: AsChar, <T as InputIter>::IterElem: Clone, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar, T: AsBytes, T: for<'a> Compare<&'a [u8]>,1584 pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1585 where
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   /*
1597   let (i, (sign, integer, fraction, exponent)) = recognize_float_parts(input)?;
1598 
1599   let mut float: f64 = minimal_lexical::parse_float(
1600     integer.as_bytes().iter(),
1601     fraction.as_bytes().iter(),
1602     exponent,
1603   );
1604   if !sign {
1605     float = -float;
1606   }
1607 
1608   Ok((i, float))
1609   */
1610   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)]
1621 mod 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]
i8_tests()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]
i16_tests()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]
u24_tests()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]
i24_tests()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]
i32_tests()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]
i64_tests()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]
i128_tests()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]
le_i8_tests()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]
le_i16_tests()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]
le_u24_tests()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]
le_i24_tests()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]
le_i32_tests()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]
le_i64_tests()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]
le_i128_tests()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]
be_f32_tests()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]
be_f64_tests()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]
le_f32_tests()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]
le_f64_tests()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]
hex_u32_tests()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")]
float_test()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]
configurable_endianness()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")]
parse_f64(i: &str) -> IResult<&str, f64, ()>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 }
2207