nom8/number/
streaming.rs

1//! Parsers recognizing numbers, streaming version
2
3#![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/// Recognizes an unsigned 1 byte integer.
19///
20/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
21/// ```rust
22/// # use nom8::{Err, error::ErrorKind, Needed};
23/// use nom8::number::streaming::be_u8;
24///
25/// let parser = |s| {
26///   be_u8::<_, (_, ErrorKind)>(s)
27/// };
28///
29/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
30/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
31/// ```
32#[inline]
33///
34/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u8`][crate::number::be_u8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
35#[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/// Recognizes a big endian unsigned 2 bytes integer.
54///
55/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
56///
57/// ```rust
58/// # use nom8::{Err, error::ErrorKind, Needed};
59/// use nom8::number::streaming::be_u16;
60///
61/// let parser = |s| {
62///   be_u16::<_, (_, ErrorKind)>(s)
63/// };
64///
65/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
66/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
67/// ```
68#[inline]
69///
70/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u16`][crate::number::be_u16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
71#[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/// Recognizes a big endian unsigned 3 byte integer.
93///
94/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
95///
96/// ```rust
97/// # use nom8::{Err, error::ErrorKind, Needed};
98/// use nom8::number::streaming::be_u24;
99///
100/// let parser = |s| {
101///   be_u24::<_, (_, ErrorKind)>(s)
102/// };
103///
104/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
105/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
106/// ```
107#[inline]
108///
109/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u24`][crate::number::be_u24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
110#[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/// Recognizes a big endian unsigned 4 bytes integer.
132///
133/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
134///
135/// ```rust
136/// # use nom8::{Err, error::ErrorKind, Needed};
137/// use nom8::number::streaming::be_u32;
138///
139/// let parser = |s| {
140///   be_u32::<_, (_, ErrorKind)>(s)
141/// };
142///
143/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
144/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
145/// ```
146#[inline]
147///
148/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u32`][crate::number::be_u32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
149#[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/// Recognizes a big endian unsigned 8 bytes integer.
171///
172/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
173///
174/// ```rust
175/// # use nom8::{Err, error::ErrorKind, Needed};
176/// use nom8::number::streaming::be_u64;
177///
178/// let parser = |s| {
179///   be_u64::<_, (_, ErrorKind)>(s)
180/// };
181///
182/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
183/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
184/// ```
185#[inline]
186///
187/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u64`][crate::number::be_u64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
188#[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/// Recognizes a big endian unsigned 16 bytes integer.
210///
211/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
212/// ```rust
213/// # use nom8::{Err, error::ErrorKind, Needed};
214/// use nom8::number::streaming::be_u128;
215///
216/// let parser = |s| {
217///   be_u128::<_, (_, ErrorKind)>(s)
218/// };
219///
220/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
221/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
222/// ```
223#[inline]
224///
225/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u128`][crate::number::be_u128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
226#[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/// Recognizes a signed 1 byte integer.
248///
249/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
250/// ```rust
251/// # use nom8::{Err, error::ErrorKind, Needed};
252/// use nom8::number::streaming::be_i8;
253///
254/// let parser = be_i8::<_, (_, ErrorKind)>;
255///
256/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
257/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
258/// ```
259#[inline]
260///
261/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i8`][crate::number::be_i8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
262#[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/// Recognizes a big endian signed 2 bytes integer.
274///
275/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
276/// ```rust
277/// # use nom8::{Err, error::ErrorKind, Needed};
278/// use nom8::number::streaming::be_i16;
279///
280/// let parser = be_i16::<_, (_, ErrorKind)>;
281///
282/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001)));
283/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(2))));
284/// ```
285#[inline]
286///
287/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i16`][crate::number::be_i16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
288#[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/// Recognizes a big endian signed 3 bytes integer.
300///
301/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
302/// ```rust
303/// # use nom8::{Err, error::ErrorKind, Needed};
304/// use nom8::number::streaming::be_i24;
305///
306/// let parser = be_i24::<_, (_, ErrorKind)>;
307///
308/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102)));
309/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(3))));
310/// ```
311#[inline]
312///
313/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i24`][crate::number::be_i24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
314#[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  // Same as the unsigned version but we need to sign-extend manually here
323  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/// Recognizes a big endian signed 4 bytes integer.
335///
336/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
337/// ```rust
338/// # use nom8::{Err, error::ErrorKind, Needed};
339/// use nom8::number::streaming::be_i32;
340///
341/// let parser = be_i32::<_, (_, ErrorKind)>;
342///
343/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203)));
344/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(4))));
345/// ```
346#[inline]
347///
348/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i32`][crate::number::be_i32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
349#[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/// Recognizes a big endian signed 8 bytes integer.
361///
362/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
363///
364/// ```rust
365/// # use nom8::{Err, error::ErrorKind, Needed};
366/// use nom8::number::streaming::be_i64;
367///
368/// let parser = be_i64::<_, (_, ErrorKind)>;
369///
370/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607)));
371/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
372/// ```
373#[inline]
374///
375/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i64`][crate::number::be_i64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
376#[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/// Recognizes a big endian signed 16 bytes integer.
388///
389/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
390/// ```rust
391/// # use nom8::{Err, error::ErrorKind, Needed};
392/// use nom8::number::streaming::be_i128;
393///
394/// let parser = be_i128::<_, (_, ErrorKind)>;
395///
396/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
397/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
398/// ```
399#[inline]
400///
401/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i128`][crate::number::be_i128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
402#[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/// Recognizes an unsigned 1 byte integer.
414///
415/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
416/// ```rust
417/// # use nom8::{Err, error::ErrorKind, Needed};
418/// use nom8::number::streaming::le_u8;
419///
420/// let parser = le_u8::<_, (_, ErrorKind)>;
421///
422/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
423/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
424/// ```
425#[inline]
426///
427/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u8`][crate::number::le_u8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
428#[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/// Recognizes a little endian unsigned 2 bytes integer.
447///
448/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
449///
450/// ```rust
451/// # use nom8::{Err, error::ErrorKind, Needed};
452/// use nom8::number::streaming::le_u16;
453///
454/// let parser = |s| {
455///   le_u16::<_, (_, ErrorKind)>(s)
456/// };
457///
458/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
459/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
460/// ```
461#[inline]
462///
463/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u16`][crate::number::le_u16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
464#[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/// Recognizes a little endian unsigned 3 bytes integer.
486///
487/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
488///
489/// ```rust
490/// # use nom8::{Err, error::ErrorKind, Needed};
491/// use nom8::number::streaming::le_u24;
492///
493/// let parser = |s| {
494///   le_u24::<_, (_, ErrorKind)>(s)
495/// };
496///
497/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
498/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
499/// ```
500#[inline]
501///
502/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u24`][crate::number::le_u24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
503#[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/// Recognizes a little endian unsigned 4 bytes integer.
525///
526/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
527///
528/// ```rust
529/// # use nom8::{Err, error::ErrorKind, Needed};
530/// use nom8::number::streaming::le_u32;
531///
532/// let parser = |s| {
533///   le_u32::<_, (_, ErrorKind)>(s)
534/// };
535///
536/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
537/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
538/// ```
539#[inline]
540///
541/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u32`][crate::number::le_u32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
542#[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/// Recognizes a little endian unsigned 8 bytes integer.
564///
565/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
566///
567/// ```rust
568/// # use nom8::{Err, error::ErrorKind, Needed};
569/// use nom8::number::streaming::le_u64;
570///
571/// let parser = |s| {
572///   le_u64::<_, (_, ErrorKind)>(s)
573/// };
574///
575/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
576/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
577/// ```
578#[inline]
579///
580/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u64`][crate::number::le_u64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
581#[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/// Recognizes a little endian unsigned 16 bytes integer.
603///
604/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
605///
606/// ```rust
607/// # use nom8::{Err, error::ErrorKind, Needed};
608/// use nom8::number::streaming::le_u128;
609///
610/// let parser = |s| {
611///   le_u128::<_, (_, ErrorKind)>(s)
612/// };
613///
614/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
615/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
616/// ```
617#[inline]
618///
619/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u128`][crate::number::le_u128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
620#[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/// Recognizes a signed 1 byte integer.
642///
643/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
644/// ```rust
645/// # use nom8::{Err, error::ErrorKind, Needed};
646/// use nom8::number::streaming::le_i8;
647///
648/// let parser = le_i8::<_, (_, ErrorKind)>;
649///
650/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00)));
651/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
652/// ```
653#[inline]
654///
655/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i8`][crate::number::le_i8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
656#[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/// Recognizes a little endian signed 2 bytes integer.
668///
669/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
670///
671/// ```rust
672/// # use nom8::{Err, error::ErrorKind, Needed};
673/// use nom8::number::streaming::le_i16;
674///
675/// let parser = |s| {
676///   le_i16::<_, (_, ErrorKind)>(s)
677/// };
678///
679/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100)));
680/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
681/// ```
682#[inline]
683///
684/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i16`][crate::number::le_i16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
685#[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/// Recognizes a little endian signed 3 bytes integer.
697///
698/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
699///
700/// ```rust
701/// # use nom8::{Err, error::ErrorKind, Needed};
702/// use nom8::number::streaming::le_i24;
703///
704/// let parser = |s| {
705///   le_i24::<_, (_, ErrorKind)>(s)
706/// };
707///
708/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100)));
709/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
710/// ```
711#[inline]
712///
713/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i24`][crate::number::le_i24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
714#[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  // Same as the unsigned version but we need to sign-extend manually here
723  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/// Recognizes a little endian signed 4 bytes integer.
735///
736/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
737///
738/// ```rust
739/// # use nom8::{Err, error::ErrorKind, Needed};
740/// use nom8::number::streaming::le_i32;
741///
742/// let parser = |s| {
743///   le_i32::<_, (_, ErrorKind)>(s)
744/// };
745///
746/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100)));
747/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
748/// ```
749#[inline]
750///
751/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i32`][crate::number::le_i32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
752#[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/// Recognizes a little endian signed 8 bytes integer.
764///
765/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
766///
767/// ```rust
768/// # use nom8::{Err, error::ErrorKind, Needed};
769/// use nom8::number::streaming::le_i64;
770///
771/// let parser = |s| {
772///   le_i64::<_, (_, ErrorKind)>(s)
773/// };
774///
775/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100)));
776/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
777/// ```
778#[inline]
779///
780/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i64`][crate::number::le_i64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
781#[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/// Recognizes a little endian signed 16 bytes integer.
793///
794/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
795///
796/// ```rust
797/// # use nom8::{Err, error::ErrorKind, Needed};
798/// use nom8::number::streaming::le_i128;
799///
800/// let parser = |s| {
801///   le_i128::<_, (_, ErrorKind)>(s)
802/// };
803///
804/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
805/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
806/// ```
807#[inline]
808///
809/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i128`][crate::number::le_i128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
810#[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/// Recognizes an unsigned 1 byte integer
822///
823/// Note that endianness does not apply to 1 byte numbers.
824/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
825/// ```rust
826/// # use nom8::{Err, error::ErrorKind, Needed};
827/// # use nom8::Needed::Size;
828/// use nom8::number::streaming::u8;
829///
830/// let parser = |s| {
831///   u8::<_, (_, ErrorKind)>(s)
832/// };
833///
834/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
835/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
836/// ```
837#[inline]
838///
839/// **WARNING:** Deprecated, replaced with [`nom8::number::u8`][crate::number::u8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
840#[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/// Recognizes an unsigned 2 bytes integer
859///
860/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u16 integer,
861/// otherwise if `nom8::number::Endianness::Little` parse a little endian u16 integer.
862/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
863///
864/// ```rust
865/// # use nom8::{Err, error::ErrorKind, Needed};
866/// # use nom8::Needed::Size;
867/// use nom8::number::streaming::u16;
868///
869/// let be_u16 = |s| {
870///   u16::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
871/// };
872///
873/// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
874/// assert_eq!(be_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
875///
876/// let le_u16 = |s| {
877///   u16::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
878/// };
879///
880/// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
881/// assert_eq!(le_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
882/// ```
883#[inline]
884///
885/// **WARNING:** Deprecated, replaced with [`nom8::number::u16`][crate::number::u16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
886#[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/// Recognizes an unsigned 3 byte integer
905///
906/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u24 integer,
907/// otherwise if `nom8::number::Endianness::Little` parse a little endian u24 integer.
908/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
909/// ```rust
910/// # use nom8::{Err, error::ErrorKind, Needed};
911/// # use nom8::Needed::Size;
912/// use nom8::number::streaming::u24;
913///
914/// let be_u24 = |s| {
915///   u24::<_,(_, ErrorKind)>(nom8::number::Endianness::Big)(s)
916/// };
917///
918/// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
919/// assert_eq!(be_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
920///
921/// let le_u24 = |s| {
922///   u24::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
923/// };
924///
925/// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
926/// assert_eq!(le_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
927/// ```
928#[inline]
929///
930/// **WARNING:** Deprecated, replaced with [`nom8::number::u24`][crate::number::u24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
931#[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/// Recognizes an unsigned 4 byte integer
950///
951/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u32 integer,
952/// otherwise if `nom8::number::Endianness::Little` parse a little endian u32 integer.
953/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
954/// ```rust
955/// # use nom8::{Err, error::ErrorKind, Needed};
956/// # use nom8::Needed::Size;
957/// use nom8::number::streaming::u32;
958///
959/// let be_u32 = |s| {
960///   u32::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
961/// };
962///
963/// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
964/// assert_eq!(be_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
965///
966/// let le_u32 = |s| {
967///   u32::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
968/// };
969///
970/// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
971/// assert_eq!(le_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
972/// ```
973#[inline]
974///
975/// **WARNING:** Deprecated, replaced with [`nom8::number::u32`][crate::number::u32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
976#[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/// Recognizes an unsigned 8 byte integer
995///
996/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u64 integer,
997/// otherwise if `nom8::number::Endianness::Little` parse a little endian u64 integer.
998/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
999/// ```rust
1000/// # use nom8::{Err, error::ErrorKind, Needed};
1001/// # use nom8::Needed::Size;
1002/// use nom8::number::streaming::u64;
1003///
1004/// let be_u64 = |s| {
1005///   u64::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1006/// };
1007///
1008/// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
1009/// assert_eq!(be_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1010///
1011/// let le_u64 = |s| {
1012///   u64::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1013/// };
1014///
1015/// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
1016/// assert_eq!(le_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1017/// ```
1018#[inline]
1019///
1020/// **WARNING:** Deprecated, replaced with [`nom8::number::u64`][crate::number::u64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1021#[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/// Recognizes an unsigned 16 byte integer
1040///
1041/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u128 integer,
1042/// otherwise if `nom8::number::Endianness::Little` parse a little endian u128 integer.
1043/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1044/// ```rust
1045/// # use nom8::{Err, error::ErrorKind, Needed};
1046/// # use nom8::Needed::Size;
1047/// use nom8::number::streaming::u128;
1048///
1049/// let be_u128 = |s| {
1050///   u128::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1051/// };
1052///
1053/// 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)));
1054/// assert_eq!(be_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1055///
1056/// let le_u128 = |s| {
1057///   u128::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1058/// };
1059///
1060/// 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)));
1061/// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1062/// ```
1063#[inline]
1064///
1065/// **WARNING:** Deprecated, replaced with [`nom8::number::u128`][crate::number::u128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1066#[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/// Recognizes a signed 1 byte integer
1085///
1086/// Note that endianness does not apply to 1 byte numbers.
1087/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1088/// ```rust
1089/// # use nom8::{Err, error::ErrorKind, Needed};
1090/// # use nom8::Needed::Size;
1091/// use nom8::number::streaming::i8;
1092///
1093/// let parser = |s| {
1094///   i8::<_, (_, ErrorKind)>(s)
1095/// };
1096///
1097/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
1098/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1))));
1099/// ```
1100#[inline]
1101///
1102/// **WARNING:** Deprecated, replaced with [`nom8::number::i8`][crate::number::i8] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1103#[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/// Recognizes a signed 2 byte integer
1115///
1116/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i16 integer,
1117/// otherwise if `nom8::number::Endianness::Little` parse a little endian i16 integer.
1118/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1119/// ```rust
1120/// # use nom8::{Err, error::ErrorKind, Needed};
1121/// # use nom8::Needed::Size;
1122/// use nom8::number::streaming::i16;
1123///
1124/// let be_i16 = |s| {
1125///   i16::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1126/// };
1127///
1128/// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
1129/// assert_eq!(be_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
1130///
1131/// let le_i16 = |s| {
1132///   i16::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1133/// };
1134///
1135/// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
1136/// assert_eq!(le_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1))));
1137/// ```
1138#[inline]
1139///
1140/// **WARNING:** Deprecated, replaced with [`nom8::number::i16`][crate::number::i16] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1141#[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/// Recognizes a signed 3 byte integer
1160///
1161/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i24 integer,
1162/// otherwise if `nom8::number::Endianness::Little` parse a little endian i24 integer.
1163/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1164/// ```rust
1165/// # use nom8::{Err, error::ErrorKind, Needed};
1166/// # use nom8::Needed::Size;
1167/// use nom8::number::streaming::i24;
1168///
1169/// let be_i24 = |s| {
1170///   i24::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1171/// };
1172///
1173/// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
1174/// assert_eq!(be_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
1175///
1176/// let le_i24 = |s| {
1177///   i24::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1178/// };
1179///
1180/// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
1181/// assert_eq!(le_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2))));
1182/// ```
1183#[inline]
1184///
1185/// **WARNING:** Deprecated, replaced with [`nom8::number::i24`][crate::number::i24] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1186#[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/// Recognizes a signed 4 byte integer
1205///
1206/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i32 integer,
1207/// otherwise if `nom8::number::Endianness::Little` parse a little endian i32 integer.
1208/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1209/// ```rust
1210/// # use nom8::{Err, error::ErrorKind, Needed};
1211/// # use nom8::Needed::Size;
1212/// use nom8::number::streaming::i32;
1213///
1214/// let be_i32 = |s| {
1215///   i32::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1216/// };
1217///
1218/// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
1219/// assert_eq!(be_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
1220///
1221/// let le_i32 = |s| {
1222///   i32::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1223/// };
1224///
1225/// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
1226/// assert_eq!(le_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3))));
1227/// ```
1228#[inline]
1229///
1230/// **WARNING:** Deprecated, replaced with [`nom8::number::i32`][crate::number::i32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1231#[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/// Recognizes a signed 8 byte integer
1250///
1251/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i64 integer,
1252/// otherwise if `nom8::number::Endianness::Little` parse a little endian i64 integer.
1253/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1254/// ```rust
1255/// # use nom8::{Err, error::ErrorKind, Needed};
1256/// # use nom8::Needed::Size;
1257/// use nom8::number::streaming::i64;
1258///
1259/// let be_i64 = |s| {
1260///   i64::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1261/// };
1262///
1263/// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
1264/// assert_eq!(be_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1265///
1266/// let le_i64 = |s| {
1267///   i64::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1268/// };
1269///
1270/// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
1271/// assert_eq!(le_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7))));
1272/// ```
1273#[inline]
1274///
1275/// **WARNING:** Deprecated, replaced with [`nom8::number::i64`][crate::number::i64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1276#[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/// Recognizes a signed 16 byte integer
1295///
1296/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i128 integer,
1297/// otherwise if `nom8::number::Endianness::Little` parse a little endian i128 integer.
1298/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1299/// ```rust
1300/// # use nom8::{Err, error::ErrorKind, Needed};
1301/// # use nom8::Needed::Size;
1302/// use nom8::number::streaming::i128;
1303///
1304/// let be_i128 = |s| {
1305///   i128::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1306/// };
1307///
1308/// 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)));
1309/// assert_eq!(be_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1310///
1311/// let le_i128 = |s| {
1312///   i128::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1313/// };
1314///
1315/// 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)));
1316/// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
1317/// ```
1318#[inline]
1319///
1320/// **WARNING:** Deprecated, replaced with [`nom8::number::i128`][crate::number::i128] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1321#[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/// Recognizes a big endian 4 bytes floating point number.
1340///
1341/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1342/// ```rust
1343/// # use nom8::{Err, error::ErrorKind, Needed};
1344/// use nom8::number::streaming::be_f32;
1345///
1346/// let parser = |s| {
1347///   be_f32::<_, (_, ErrorKind)>(s)
1348/// };
1349///
1350/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625)));
1351/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
1352/// ```
1353#[inline]
1354///
1355/// **WARNING:** Deprecated, replaced with [`nom8::number::be_f32`][crate::number::be_f32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1356#[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/// Recognizes a big endian 8 bytes floating point number.
1371///
1372/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1373/// ```rust
1374/// # use nom8::{Err, error::ErrorKind, Needed};
1375/// use nom8::number::streaming::be_f64;
1376///
1377/// let parser = |s| {
1378///   be_f64::<_, (_, ErrorKind)>(s)
1379/// };
1380///
1381/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1382/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
1383/// ```
1384#[inline]
1385///
1386/// **WARNING:** Deprecated, replaced with [`nom8::number::be_f64`][crate::number::be_f64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1387#[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/// Recognizes a little endian 4 bytes floating point number.
1402///
1403/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1404/// ```rust
1405/// # use nom8::{Err, error::ErrorKind, Needed};
1406/// use nom8::number::streaming::le_f32;
1407///
1408/// let parser = |s| {
1409///   le_f32::<_, (_, ErrorKind)>(s)
1410/// };
1411///
1412/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1413/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
1414/// ```
1415#[inline]
1416///
1417/// **WARNING:** Deprecated, replaced with [`nom8::number::le_f32`][crate::number::le_f32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1418#[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/// Recognizes a little endian 8 bytes floating point number.
1433///
1434/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1435/// ```rust
1436/// # use nom8::{Err, error::ErrorKind, Needed};
1437/// use nom8::number::streaming::le_f64;
1438///
1439/// let parser = |s| {
1440///   le_f64::<_, (_, ErrorKind)>(s)
1441/// };
1442///
1443/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0)));
1444/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7))));
1445/// ```
1446#[inline]
1447///
1448/// **WARNING:** Deprecated, replaced with [`nom8::number::le_f64`][crate::number::le_f64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1449#[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/// Recognizes a 4 byte floating point number
1464///
1465/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian f32 float,
1466/// otherwise if `nom8::number::Endianness::Little` parse a little endian f32 float.
1467/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1468/// ```rust
1469/// # use nom8::{Err, error::ErrorKind, Needed};
1470/// # use nom8::Needed::Size;
1471/// use nom8::number::streaming::f32;
1472///
1473/// let be_f32 = |s| {
1474///   f32::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1475/// };
1476///
1477/// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1478/// assert_eq!(be_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
1479///
1480/// let le_f32 = |s| {
1481///   f32::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1482/// };
1483///
1484/// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1485/// assert_eq!(le_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
1486/// ```
1487#[inline]
1488///
1489/// **WARNING:** Deprecated, replaced with [`nom8::number::f32`][crate::number::f32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1490#[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/// Recognizes an 8 byte floating point number
1509///
1510/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian f64 float,
1511/// otherwise if `nom8::number::Endianness::Little` parse a little endian f64 float.
1512/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1513/// ```rust
1514/// # use nom8::{Err, error::ErrorKind, Needed};
1515/// # use nom8::Needed::Size;
1516/// use nom8::number::streaming::f64;
1517///
1518/// let be_f64 = |s| {
1519///   f64::<_, (_, ErrorKind)>(nom8::number::Endianness::Big)(s)
1520/// };
1521///
1522/// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1523/// assert_eq!(be_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
1524///
1525/// let le_f64 = |s| {
1526///   f64::<_, (_, ErrorKind)>(nom8::number::Endianness::Little)(s)
1527/// };
1528///
1529/// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
1530/// assert_eq!(le_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5))));
1531/// ```
1532#[inline]
1533///
1534/// **WARNING:** Deprecated, replaced with [`nom8::number::f64`][crate::number::f64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1535#[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/// Recognizes a hex-encoded integer.
1554///
1555/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1556/// ```rust
1557/// # use nom8::{Err, error::ErrorKind, Needed};
1558/// use nom8::number::streaming::hex_u32;
1559///
1560/// let parser = |s| {
1561///   hex_u32(s)
1562/// };
1563///
1564/// assert_eq!(parser(&b"01AE;"[..]), Ok((&b";"[..], 0x01AE)));
1565/// assert_eq!(parser(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));
1566/// assert_eq!(parser(&b"ggg"[..]), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
1567/// ```
1568#[inline]
1569///
1570/// **WARNING:** Deprecated, replaced with [`nom8::number::hex_u32`][crate::number::hex_u32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1571#[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  // Do not parse more than 8 characters for a u32
1593  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/// Recognizes a floating point number in text format and returns the corresponding part of the input.
1614///
1615/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if it reaches the end of input.
1616///
1617/// ```rust
1618/// # use nom8::{Err, error::ErrorKind, Needed};
1619/// use nom8::number::streaming::recognize_float;
1620///
1621/// let parser = |s| {
1622///   recognize_float(s)
1623/// };
1624///
1625/// assert_eq!(parser("11e-1;"), Ok((";", "11e-1")));
1626/// assert_eq!(parser("123E-02;"), Ok((";", "123E-02")));
1627/// assert_eq!(parser("123K-01"), Ok(("K-01", "123")));
1628/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
1629/// ```
1630#[rustfmt::skip]
1631///
1632/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float`][crate::character::recognize_float] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1633#[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// workaround until issues with minimal-lexical are fixed
1661#[doc(hidden)]
1662///
1663/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float_or_exceptions`][character::number::recognize_float_or_exceptions] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1664#[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/// Recognizes a floating point number in text format
1704///
1705/// It returns a tuple of (`sign`, `integer part`, `fraction part` and `exponent`) of the input
1706/// data.
1707///
1708/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1709///
1710///
1711/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float_parts`][crate::character::recognize_float_parts] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1712#[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) = take_while(|c: <T as InputTakeAtPosition>::Item| c.as_char() == '0')(i)?;
1742  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) = digit0(i)?;
1748  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    // keep the last zero if integer is empty
1759    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    // match number, trim right zeroes
1768    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/// Recognizes floating point number in text format and returns a f32.
1823///
1824/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1825///
1826/// ```rust
1827/// # use nom8::{Err, error::ErrorKind, Needed};
1828/// # use nom8::Needed::Size;
1829/// use nom8::number::complete::float;
1830///
1831/// let parser = |s| {
1832///   float(s)
1833/// };
1834///
1835/// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1836/// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1837/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1838/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1839/// ```
1840///
1841/// **WARNING:** Deprecated, replaced with [`nom8::character::f32`][crate::character::f32] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1842#[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/// Recognizes floating point number in text format and returns a f64.
1873///
1874/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
1875///
1876/// ```rust
1877/// # use nom8::{Err, error::ErrorKind, Needed};
1878/// # use nom8::Needed::Size;
1879/// use nom8::number::complete::double;
1880///
1881/// let parser = |s| {
1882///   double(s)
1883/// };
1884///
1885/// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1886/// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1887/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1888/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1889/// ```
1890///
1891/// **WARNING:** Deprecated, replaced with [`nom8::character::f64`][crate::character::f64] with input wrapped in [`nom8::input::Streaming`][crate::input::Streaming]
1892#[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}