nom8/number/
complete.rs

1//! Parsers recognizing numbers, complete input version
2
3#![allow(deprecated)]
4
5use crate::branch::alt;
6use crate::bytes::complete::tag;
7use crate::character::complete::{char, digit1, sign};
8use crate::combinator::{cut, map, opt, recognize};
9use crate::error::ParseError;
10use crate::error::{make_error, ErrorKind};
11use crate::input::{
12  AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, IntoOutput,
13  Offset, Slice,
14};
15use crate::lib::std::ops::{Range, RangeFrom, RangeTo};
16use crate::sequence::{pair, tuple};
17use crate::*;
18
19/// Recognizes an unsigned 1 byte integer.
20///
21/// *Complete version*: Returns an error if there is not enough input data.
22/// ```rust
23/// # use nom8::{Err, error::ErrorKind, Needed};
24/// # use nom8::Needed::Size;
25/// use nom8::number::complete::be_u8;
26///
27/// let parser = |s| {
28///   be_u8(s)
29/// };
30///
31/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
32/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
33/// ```
34#[inline]
35///
36/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u8`][crate::number::be_u8]
37#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u8`")]
38pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
39where
40  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
41{
42  let bound: usize = 1;
43  if input.input_len() < bound {
44    Err(Err::Error(make_error(input, ErrorKind::Eof)))
45  } else {
46    let res = input.iter_elements().next().unwrap();
47
48    Ok((input.slice(bound..), res))
49  }
50}
51
52/// Recognizes a big endian unsigned 2 bytes integer.
53///
54/// *Complete version*: Returns an error if there is not enough input data.
55/// ```rust
56/// # use nom8::{Err, error::ErrorKind, Needed};
57/// # use nom8::Needed::Size;
58/// use nom8::number::complete::be_u16;
59///
60/// let parser = |s| {
61///   be_u16(s)
62/// };
63///
64/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
65/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
66/// ```
67#[inline]
68///
69/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u16`][crate::number::be_u16]
70#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u16`")]
71pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
72where
73  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
74{
75  let bound: usize = 2;
76  if input.input_len() < bound {
77    Err(Err::Error(make_error(input, ErrorKind::Eof)))
78  } else {
79    let mut res = 0u16;
80    for byte in input.iter_elements().take(bound) {
81      res = (res << 8) + byte as u16;
82    }
83
84    Ok((input.slice(bound..), res))
85  }
86}
87
88/// Recognizes a big endian unsigned 3 byte integer.
89///
90/// *Complete version*: Returns an error if there is not enough input data.
91/// ```rust
92/// # use nom8::{Err, error::ErrorKind, Needed};
93/// # use nom8::Needed::Size;
94/// use nom8::number::complete::be_u24;
95///
96/// let parser = |s| {
97///   be_u24(s)
98/// };
99///
100/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
101/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
102/// ```
103#[inline]
104///
105/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u24`][crate::number::be_u24]
106#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u24`")]
107pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
108where
109  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
110{
111  let bound: usize = 3;
112  if input.input_len() < bound {
113    Err(Err::Error(make_error(input, ErrorKind::Eof)))
114  } else {
115    let mut res = 0u32;
116    for byte in input.iter_elements().take(bound) {
117      res = (res << 8) + byte as u32;
118    }
119
120    Ok((input.slice(bound..), res))
121  }
122}
123
124/// Recognizes a big endian unsigned 4 bytes integer.
125///
126/// *Complete version*: Returns an error if there is not enough input data.
127/// ```rust
128/// # use nom8::{Err, error::ErrorKind, Needed};
129/// # use nom8::Needed::Size;
130/// use nom8::number::complete::be_u32;
131///
132/// let parser = |s| {
133///   be_u32(s)
134/// };
135///
136/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
137/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
138/// ```
139#[inline]
140///
141/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u32`][crate::number::be_u32]
142#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u32`")]
143pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
144where
145  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
146{
147  let bound: usize = 4;
148  if input.input_len() < bound {
149    Err(Err::Error(make_error(input, ErrorKind::Eof)))
150  } else {
151    let mut res = 0u32;
152    for byte in input.iter_elements().take(bound) {
153      res = (res << 8) + byte as u32;
154    }
155
156    Ok((input.slice(bound..), res))
157  }
158}
159
160/// Recognizes a big endian unsigned 8 bytes integer.
161///
162/// *Complete version*: Returns an error if there is not enough input data.
163/// ```rust
164/// # use nom8::{Err, error::ErrorKind, Needed};
165/// # use nom8::Needed::Size;
166/// use nom8::number::complete::be_u64;
167///
168/// let parser = |s| {
169///   be_u64(s)
170/// };
171///
172/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
173/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
174/// ```
175#[inline]
176///
177/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u64`][crate::number::be_u64]
178#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u64`")]
179pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
180where
181  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
182{
183  let bound: usize = 8;
184  if input.input_len() < bound {
185    Err(Err::Error(make_error(input, ErrorKind::Eof)))
186  } else {
187    let mut res = 0u64;
188    for byte in input.iter_elements().take(bound) {
189      res = (res << 8) + byte as u64;
190    }
191
192    Ok((input.slice(bound..), res))
193  }
194}
195
196/// Recognizes a big endian unsigned 16 bytes integer.
197///
198/// *Complete version*: Returns an error if there is not enough input data.
199/// ```rust
200/// # use nom8::{Err, error::ErrorKind, Needed};
201/// # use nom8::Needed::Size;
202/// use nom8::number::complete::be_u128;
203///
204/// let parser = |s| {
205///   be_u128(s)
206/// };
207///
208/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
209/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
210/// ```
211#[inline]
212///
213/// **WARNING:** Deprecated, replaced with [`nom8::number::be_u128`][crate::number::be_u128]
214#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_u128`")]
215pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
216where
217  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
218{
219  let bound: usize = 16;
220  if input.input_len() < bound {
221    Err(Err::Error(make_error(input, ErrorKind::Eof)))
222  } else {
223    let mut res = 0u128;
224    for byte in input.iter_elements().take(bound) {
225      res = (res << 8) + byte as u128;
226    }
227
228    Ok((input.slice(bound..), res))
229  }
230}
231
232/// Recognizes a signed 1 byte integer.
233///
234/// *Complete version*: Returns an error if there is not enough input data.
235/// ```rust
236/// # use nom8::{Err, error::ErrorKind, Needed};
237/// # use nom8::Needed::Size;
238/// use nom8::number::complete::be_i8;
239///
240/// let parser = |s| {
241///   be_i8(s)
242/// };
243///
244/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
245/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
246/// ```
247#[inline]
248///
249/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i8`][crate::number::be_i8]
250#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i8`")]
251pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
252where
253  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
254{
255  be_u8.map(|x| x as i8).parse(input)
256}
257
258/// Recognizes a big endian signed 2 bytes integer.
259///
260/// *Complete version*: Returns an error if there is not enough input data.
261/// ```rust
262/// # use nom8::{Err, error::ErrorKind, Needed};
263/// # use nom8::Needed::Size;
264/// use nom8::number::complete::be_i16;
265///
266/// let parser = |s| {
267///   be_i16(s)
268/// };
269///
270/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
271/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
272/// ```
273#[inline]
274///
275/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i16`][crate::number::be_i16]
276#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i16`")]
277pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
278where
279  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
280{
281  be_u16.map(|x| x as i16).parse(input)
282}
283
284/// Recognizes a big endian signed 3 bytes integer.
285///
286/// *Complete version*: Returns an error if there is not enough input data.
287/// ```rust
288/// # use nom8::{Err, error::ErrorKind, Needed};
289/// # use nom8::Needed::Size;
290/// use nom8::number::complete::be_i24;
291///
292/// let parser = |s| {
293///   be_i24(s)
294/// };
295///
296/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
297/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
298/// ```
299#[inline]
300///
301/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i24`][crate::number::be_i24]
302#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i24`")]
303pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
304where
305  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
306{
307  // Same as the unsigned version but we need to sign-extend manually here
308  be_u24
309    .map(|x| {
310      if x & 0x80_00_00 != 0 {
311        (x | 0xff_00_00_00) as i32
312      } else {
313        x as i32
314      }
315    })
316    .parse(input)
317}
318
319/// Recognizes a big endian signed 4 bytes integer.
320///
321/// *Complete version*: Teturns an error if there is not enough input data.
322/// ```rust
323/// # use nom8::{Err, error::ErrorKind, Needed};
324/// # use nom8::Needed::Size;
325/// use nom8::number::complete::be_i32;
326///
327/// let parser = |s| {
328///   be_i32(s)
329/// };
330///
331/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
332/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
333/// ```
334#[inline]
335///
336/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i32`][crate::number::be_i32]
337#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i32`")]
338pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
339where
340  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
341{
342  be_u32.map(|x| x as i32).parse(input)
343}
344
345/// Recognizes a big endian signed 8 bytes integer.
346///
347/// *Complete version*: Returns an error if there is not enough input data.
348/// ```rust
349/// # use nom8::{Err, error::ErrorKind, Needed};
350/// # use nom8::Needed::Size;
351/// use nom8::number::complete::be_i64;
352///
353/// let parser = |s| {
354///   be_i64(s)
355/// };
356///
357/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
358/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
359/// ```
360#[inline]
361///
362/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i64`][crate::number::be_i64]
363#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i64`")]
364pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
365where
366  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
367{
368  be_u64.map(|x| x as i64).parse(input)
369}
370
371/// Recognizes a big endian signed 16 bytes integer.
372///
373/// *Complete version*: Returns an error if there is not enough input data.
374/// ```rust
375/// # use nom8::{Err, error::ErrorKind, Needed};
376/// # use nom8::Needed::Size;
377/// use nom8::number::complete::be_i128;
378///
379/// let parser = |s| {
380///   be_i128(s)
381/// };
382///
383/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
384/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
385/// ```
386#[inline]
387///
388/// **WARNING:** Deprecated, replaced with [`nom8::number::be_i128`][crate::number::be_i128]
389#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_i128`")]
390pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
391where
392  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
393{
394  be_u128.map(|x| x as i128).parse(input)
395}
396
397/// Recognizes an unsigned 1 byte integer.
398///
399/// *Complete version*: Returns an error if there is not enough input data.
400/// ```rust
401/// # use nom8::{Err, error::ErrorKind, Needed};
402/// # use nom8::Needed::Size;
403/// use nom8::number::complete::le_u8;
404///
405/// let parser = |s| {
406///   le_u8(s)
407/// };
408///
409/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
410/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
411/// ```
412#[inline]
413///
414/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u8`][crate::number::le_u8]
415#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u8`")]
416pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
417where
418  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
419{
420  let bound: usize = 1;
421  if input.input_len() < bound {
422    Err(Err::Error(make_error(input, ErrorKind::Eof)))
423  } else {
424    let res = input.iter_elements().next().unwrap();
425
426    Ok((input.slice(bound..), res))
427  }
428}
429
430/// Recognizes a little endian unsigned 2 bytes integer.
431///
432/// *Complete version*: Returns an error if there is not enough input data.
433/// ```rust
434/// # use nom8::{Err, error::ErrorKind, Needed};
435/// # use nom8::Needed::Size;
436/// use nom8::number::complete::le_u16;
437///
438/// let parser = |s| {
439///   le_u16(s)
440/// };
441///
442/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
443/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
444/// ```
445#[inline]
446///
447/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u16`][crate::number::le_u16]
448#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u16`")]
449pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
450where
451  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
452{
453  let bound: usize = 2;
454  if input.input_len() < bound {
455    Err(Err::Error(make_error(input, ErrorKind::Eof)))
456  } else {
457    let mut res = 0u16;
458    for (index, byte) in input.iter_indices().take(bound) {
459      res += (byte as u16) << (8 * index);
460    }
461
462    Ok((input.slice(bound..), res))
463  }
464}
465
466/// Recognizes a little endian unsigned 3 byte integer.
467///
468/// *Complete version*: Returns an error if there is not enough input data.
469/// ```rust
470/// # use nom8::{Err, error::ErrorKind, Needed};
471/// # use nom8::Needed::Size;
472/// use nom8::number::complete::le_u24;
473///
474/// let parser = |s| {
475///   le_u24(s)
476/// };
477///
478/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
479/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
480/// ```
481#[inline]
482///
483/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u24`][crate::number::le_u24]
484#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u24`")]
485pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
486where
487  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
488{
489  let bound: usize = 3;
490  if input.input_len() < bound {
491    Err(Err::Error(make_error(input, ErrorKind::Eof)))
492  } else {
493    let mut res = 0u32;
494    for (index, byte) in input.iter_indices().take(bound) {
495      res += (byte as u32) << (8 * index);
496    }
497
498    Ok((input.slice(bound..), res))
499  }
500}
501
502/// Recognizes a little endian unsigned 4 bytes integer.
503///
504/// *Complete version*: Returns an error if there is not enough input data.
505/// ```rust
506/// # use nom8::{Err, error::ErrorKind, Needed};
507/// # use nom8::Needed::Size;
508/// use nom8::number::complete::le_u32;
509///
510/// let parser = |s| {
511///   le_u32(s)
512/// };
513///
514/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
515/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
516/// ```
517#[inline]
518///
519/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u32`][crate::number::le_u32]
520#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u32`")]
521pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
522where
523  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
524{
525  let bound: usize = 4;
526  if input.input_len() < bound {
527    Err(Err::Error(make_error(input, ErrorKind::Eof)))
528  } else {
529    let mut res = 0u32;
530    for (index, byte) in input.iter_indices().take(bound) {
531      res += (byte as u32) << (8 * index);
532    }
533
534    Ok((input.slice(bound..), res))
535  }
536}
537
538/// Recognizes a little endian unsigned 8 bytes integer.
539///
540/// *Complete version*: Returns an error if there is not enough input data.
541/// ```rust
542/// # use nom8::{Err, error::ErrorKind, Needed};
543/// # use nom8::Needed::Size;
544/// use nom8::number::complete::le_u64;
545///
546/// let parser = |s| {
547///   le_u64(s)
548/// };
549///
550/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
551/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
552/// ```
553#[inline]
554///
555/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u64`][crate::number::le_u64]
556#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u64`")]
557pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
558where
559  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
560{
561  let bound: usize = 8;
562  if input.input_len() < bound {
563    Err(Err::Error(make_error(input, ErrorKind::Eof)))
564  } else {
565    let mut res = 0u64;
566    for (index, byte) in input.iter_indices().take(bound) {
567      res += (byte as u64) << (8 * index);
568    }
569
570    Ok((input.slice(bound..), res))
571  }
572}
573
574/// Recognizes a little endian unsigned 16 bytes integer.
575///
576/// *Complete version*: Returns an error if there is not enough input data.
577/// ```rust
578/// # use nom8::{Err, error::ErrorKind, Needed};
579/// # use nom8::Needed::Size;
580/// use nom8::number::complete::le_u128;
581///
582/// let parser = |s| {
583///   le_u128(s)
584/// };
585///
586/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
587/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
588/// ```
589#[inline]
590///
591/// **WARNING:** Deprecated, replaced with [`nom8::number::le_u128`][crate::number::le_u128]
592#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_u128`")]
593pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
594where
595  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
596{
597  let bound: usize = 16;
598  if input.input_len() < bound {
599    Err(Err::Error(make_error(input, ErrorKind::Eof)))
600  } else {
601    let mut res = 0u128;
602    for (index, byte) in input.iter_indices().take(bound) {
603      res += (byte as u128) << (8 * index);
604    }
605
606    Ok((input.slice(bound..), res))
607  }
608}
609
610/// Recognizes a signed 1 byte integer.
611///
612/// *Complete version*: Returns an error if there is not enough input data.
613/// ```rust
614/// # use nom8::{Err, error::ErrorKind, Needed};
615/// # use nom8::Needed::Size;
616/// use nom8::number::complete::le_i8;
617///
618/// let parser = |s| {
619///   le_i8(s)
620/// };
621///
622/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
623/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
624/// ```
625#[inline]
626///
627/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i8`][crate::number::le_i8]
628#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i8`")]
629pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
630where
631  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
632{
633  be_u8.map(|x| x as i8).parse(input)
634}
635
636/// Recognizes a little endian signed 2 bytes integer.
637///
638/// *Complete version*: Returns an error if there is not enough input data.
639/// ```rust
640/// # use nom8::{Err, error::ErrorKind, Needed};
641/// # use nom8::Needed::Size;
642/// use nom8::number::complete::le_i16;
643///
644/// let parser = |s| {
645///   le_i16(s)
646/// };
647///
648/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
649/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
650/// ```
651#[inline]
652///
653/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i16`][crate::number::le_i16]
654#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i16`")]
655pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
656where
657  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
658{
659  le_u16.map(|x| x as i16).parse(input)
660}
661
662/// Recognizes a little endian signed 3 bytes integer.
663///
664/// *Complete version*: Returns an error if there is not enough input data.
665/// ```rust
666/// # use nom8::{Err, error::ErrorKind, Needed};
667/// # use nom8::Needed::Size;
668/// use nom8::number::complete::le_i24;
669///
670/// let parser = |s| {
671///   le_i24(s)
672/// };
673///
674/// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
675/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
676/// ```
677#[inline]
678///
679/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i24`][crate::number::le_i24]
680#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i24`")]
681pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
682where
683  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
684{
685  // Same as the unsigned version but we need to sign-extend manually here
686  le_u24
687    .map(|x| {
688      if x & 0x80_00_00 != 0 {
689        (x | 0xff_00_00_00) as i32
690      } else {
691        x as i32
692      }
693    })
694    .parse(input)
695}
696
697/// Recognizes a little endian signed 4 bytes integer.
698///
699/// *Complete version*: Returns an error if there is not enough input data.
700/// ```rust
701/// # use nom8::{Err, error::ErrorKind, Needed};
702/// # use nom8::Needed::Size;
703/// use nom8::number::complete::le_i32;
704///
705/// let parser = |s| {
706///   le_i32(s)
707/// };
708///
709/// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
710/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
711/// ```
712#[inline]
713///
714/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i32`][crate::number::le_i32]
715#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i32`")]
716pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
717where
718  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
719{
720  le_u32.map(|x| x as i32).parse(input)
721}
722
723/// Recognizes a little endian signed 8 bytes integer.
724///
725/// *Complete version*: Returns an error if there is not enough input data.
726/// ```rust
727/// # use nom8::{Err, error::ErrorKind, Needed};
728/// # use nom8::Needed::Size;
729/// use nom8::number::complete::le_i64;
730///
731/// let parser = |s| {
732///   le_i64(s)
733/// };
734///
735/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
736/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
737/// ```
738#[inline]
739///
740/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i64`][crate::number::le_i64]
741#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i64`")]
742pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
743where
744  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
745{
746  le_u64.map(|x| x as i64).parse(input)
747}
748
749/// Recognizes a little endian signed 16 bytes integer.
750///
751/// *Complete version*: Returns an error if there is not enough input data.
752/// ```rust
753/// # use nom8::{Err, error::ErrorKind, Needed};
754/// # use nom8::Needed::Size;
755/// use nom8::number::complete::le_i128;
756///
757/// let parser = |s| {
758///   le_i128(s)
759/// };
760///
761/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
762/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
763/// ```
764#[inline]
765///
766/// **WARNING:** Deprecated, replaced with [`nom8::number::le_i128`][crate::number::le_i128]
767#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_i128`")]
768pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
769where
770  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
771{
772  le_u128.map(|x| x as i128).parse(input)
773}
774
775/// Recognizes an unsigned 1 byte integer
776///
777/// Note that endianness does not apply to 1 byte numbers.
778/// *complete version*: returns an error if there is not enough input data
779/// ```rust
780/// # use nom8::{Err, error::ErrorKind, Needed};
781/// # use nom8::Needed::Size;
782/// use nom8::number::complete::u8;
783///
784/// let parser = |s| {
785///   u8(s)
786/// };
787///
788/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
789/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
790/// ```
791#[inline]
792///
793/// **WARNING:** Deprecated, replaced with [`nom8::number::u8`][crate::number::u8]
794#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u8`")]
795pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
796where
797  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
798{
799  let bound: usize = 1;
800  if input.input_len() < bound {
801    Err(Err::Error(make_error(input, ErrorKind::Eof)))
802  } else {
803    let res = input.iter_elements().next().unwrap();
804
805    Ok((input.slice(bound..), res))
806  }
807}
808
809/// Recognizes an unsigned 2 bytes integer
810///
811/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u16 integer,
812/// otherwise if `nom8::number::Endianness::Little` parse a little endian u16 integer.
813/// *complete version*: returns an error if there is not enough input data
814///
815/// ```rust
816/// # use nom8::{Err, error::ErrorKind, Needed};
817/// # use nom8::Needed::Size;
818/// use nom8::number::complete::u16;
819///
820/// let be_u16 = |s| {
821///   u16(nom8::number::Endianness::Big)(s)
822/// };
823///
824/// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
825/// assert_eq!(be_u16(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
826///
827/// let le_u16 = |s| {
828///   u16(nom8::number::Endianness::Little)(s)
829/// };
830///
831/// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
832/// assert_eq!(le_u16(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
833/// ```
834#[inline]
835///
836/// **WARNING:** Deprecated, replaced with [`nom8::number::u16`][crate::number::u16]
837#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u16`")]
838pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u16, E>
839where
840  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
841{
842  match endian {
843    crate::number::Endianness::Big => be_u16,
844    crate::number::Endianness::Little => le_u16,
845    #[cfg(target_endian = "big")]
846    crate::number::Endianness::Native => be_u16,
847    #[cfg(target_endian = "little")]
848    crate::number::Endianness::Native => le_u16,
849  }
850}
851
852/// Recognizes an unsigned 3 byte integer
853///
854/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u24 integer,
855/// otherwise if `nom8::number::Endianness::Little` parse a little endian u24 integer.
856/// *complete version*: returns an error if there is not enough input data
857/// ```rust
858/// # use nom8::{Err, error::ErrorKind, Needed};
859/// # use nom8::Needed::Size;
860/// use nom8::number::complete::u24;
861///
862/// let be_u24 = |s| {
863///   u24(nom8::number::Endianness::Big)(s)
864/// };
865///
866/// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
867/// assert_eq!(be_u24(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
868///
869/// let le_u24 = |s| {
870///   u24(nom8::number::Endianness::Little)(s)
871/// };
872///
873/// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
874/// assert_eq!(le_u24(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
875/// ```
876#[inline]
877///
878/// **WARNING:** Deprecated, replaced with [`nom8::number::u24`][crate::number::u24]
879#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u24`")]
880pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
881where
882  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
883{
884  match endian {
885    crate::number::Endianness::Big => be_u24,
886    crate::number::Endianness::Little => le_u24,
887    #[cfg(target_endian = "big")]
888    crate::number::Endianness::Native => be_u24,
889    #[cfg(target_endian = "little")]
890    crate::number::Endianness::Native => le_u24,
891  }
892}
893
894/// Recognizes an unsigned 4 byte integer
895///
896/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u32 integer,
897/// otherwise if `nom8::number::Endianness::Little` parse a little endian u32 integer.
898/// *complete version*: returns an error if there is not enough input data
899/// ```rust
900/// # use nom8::{Err, error::ErrorKind, Needed};
901/// # use nom8::Needed::Size;
902/// use nom8::number::complete::u32;
903///
904/// let be_u32 = |s| {
905///   u32(nom8::number::Endianness::Big)(s)
906/// };
907///
908/// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
909/// assert_eq!(be_u32(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
910///
911/// let le_u32 = |s| {
912///   u32(nom8::number::Endianness::Little)(s)
913/// };
914///
915/// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
916/// assert_eq!(le_u32(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
917/// ```
918#[inline]
919///
920/// **WARNING:** Deprecated, replaced with [`nom8::number::u32`][crate::number::u32]
921#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u32`")]
922pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u32, E>
923where
924  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
925{
926  match endian {
927    crate::number::Endianness::Big => be_u32,
928    crate::number::Endianness::Little => le_u32,
929    #[cfg(target_endian = "big")]
930    crate::number::Endianness::Native => be_u32,
931    #[cfg(target_endian = "little")]
932    crate::number::Endianness::Native => le_u32,
933  }
934}
935
936/// Recognizes an unsigned 8 byte integer
937///
938/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u64 integer,
939/// otherwise if `nom8::number::Endianness::Little` parse a little endian u64 integer.
940/// *complete version*: returns an error if there is not enough input data
941/// ```rust
942/// # use nom8::{Err, error::ErrorKind, Needed};
943/// # use nom8::Needed::Size;
944/// use nom8::number::complete::u64;
945///
946/// let be_u64 = |s| {
947///   u64(nom8::number::Endianness::Big)(s)
948/// };
949///
950/// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
951/// assert_eq!(be_u64(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
952///
953/// let le_u64 = |s| {
954///   u64(nom8::number::Endianness::Little)(s)
955/// };
956///
957/// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
958/// assert_eq!(le_u64(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
959/// ```
960#[inline]
961///
962/// **WARNING:** Deprecated, replaced with [`nom8::number::u64`][crate::number::u64]
963#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u64`")]
964pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u64, E>
965where
966  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
967{
968  match endian {
969    crate::number::Endianness::Big => be_u64,
970    crate::number::Endianness::Little => le_u64,
971    #[cfg(target_endian = "big")]
972    crate::number::Endianness::Native => be_u64,
973    #[cfg(target_endian = "little")]
974    crate::number::Endianness::Native => le_u64,
975  }
976}
977
978/// Recognizes an unsigned 16 byte integer
979///
980/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian u128 integer,
981/// otherwise if `nom8::number::Endianness::Little` parse a little endian u128 integer.
982/// *complete version*: returns an error if there is not enough input data
983/// ```rust
984/// # use nom8::{Err, error::ErrorKind, Needed};
985/// # use nom8::Needed::Size;
986/// use nom8::number::complete::u128;
987///
988/// let be_u128 = |s| {
989///   u128(nom8::number::Endianness::Big)(s)
990/// };
991///
992/// 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)));
993/// assert_eq!(be_u128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
994///
995/// let le_u128 = |s| {
996///   u128(nom8::number::Endianness::Little)(s)
997/// };
998///
999/// 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)));
1000/// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1001/// ```
1002#[inline]
1003///
1004/// **WARNING:** Deprecated, replaced with [`nom8::number::u128`][crate::number::u128]
1005#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::u128`")]
1006pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
1007where
1008  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1009{
1010  match endian {
1011    crate::number::Endianness::Big => be_u128,
1012    crate::number::Endianness::Little => le_u128,
1013    #[cfg(target_endian = "big")]
1014    crate::number::Endianness::Native => be_u128,
1015    #[cfg(target_endian = "little")]
1016    crate::number::Endianness::Native => le_u128,
1017  }
1018}
1019
1020/// Recognizes a signed 1 byte integer
1021///
1022/// Note that endianness does not apply to 1 byte numbers.
1023/// *complete version*: returns an error if there is not enough input data
1024/// ```rust
1025/// # use nom8::{Err, error::ErrorKind, Needed};
1026/// # use nom8::Needed::Size;
1027/// use nom8::number::complete::i8;
1028///
1029/// let parser = |s| {
1030///   i8(s)
1031/// };
1032///
1033/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00)));
1034/// assert_eq!(parser(&b""[..]), Err(Err::Error((&[][..], ErrorKind::Eof))));
1035/// ```
1036#[inline]
1037///
1038/// **WARNING:** Deprecated, replaced with [`nom8::number::i8`][crate::number::i8]
1039#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i8`")]
1040pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
1041where
1042  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1043{
1044  u8.map(|x| x as i8).parse(i)
1045}
1046
1047/// Recognizes a signed 2 byte integer
1048///
1049/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i16 integer,
1050/// otherwise if `nom8::number::Endianness::Little` parse a little endian i16 integer.
1051/// *complete version*: returns an error if there is not enough input data
1052/// ```rust
1053/// # use nom8::{Err, error::ErrorKind, Needed};
1054/// # use nom8::Needed::Size;
1055/// use nom8::number::complete::i16;
1056///
1057/// let be_i16 = |s| {
1058///   i16(nom8::number::Endianness::Big)(s)
1059/// };
1060///
1061/// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
1062/// assert_eq!(be_i16(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1063///
1064/// let le_i16 = |s| {
1065///   i16(nom8::number::Endianness::Little)(s)
1066/// };
1067///
1068/// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
1069/// assert_eq!(le_i16(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1070/// ```
1071#[inline]
1072///
1073/// **WARNING:** Deprecated, replaced with [`nom8::number::i16`][crate::number::i16]
1074#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i16`")]
1075pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i16, E>
1076where
1077  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1078{
1079  match endian {
1080    crate::number::Endianness::Big => be_i16,
1081    crate::number::Endianness::Little => le_i16,
1082    #[cfg(target_endian = "big")]
1083    crate::number::Endianness::Native => be_i16,
1084    #[cfg(target_endian = "little")]
1085    crate::number::Endianness::Native => le_i16,
1086  }
1087}
1088
1089/// Recognizes a signed 3 byte integer
1090///
1091/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i24 integer,
1092/// otherwise if `nom8::number::Endianness::Little` parse a little endian i24 integer.
1093/// *complete version*: returns an error if there is not enough input data
1094/// ```rust
1095/// # use nom8::{Err, error::ErrorKind, Needed};
1096/// # use nom8::Needed::Size;
1097/// use nom8::number::complete::i24;
1098///
1099/// let be_i24 = |s| {
1100///   i24(nom8::number::Endianness::Big)(s)
1101/// };
1102///
1103/// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305)));
1104/// assert_eq!(be_i24(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1105///
1106/// let le_i24 = |s| {
1107///   i24(nom8::number::Endianness::Little)(s)
1108/// };
1109///
1110/// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300)));
1111/// assert_eq!(le_i24(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1112/// ```
1113#[inline]
1114///
1115/// **WARNING:** Deprecated, replaced with [`nom8::number::i24`][crate::number::i24]
1116#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i24`")]
1117pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1118where
1119  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1120{
1121  match endian {
1122    crate::number::Endianness::Big => be_i24,
1123    crate::number::Endianness::Little => le_i24,
1124    #[cfg(target_endian = "big")]
1125    crate::number::Endianness::Native => be_i24,
1126    #[cfg(target_endian = "little")]
1127    crate::number::Endianness::Native => le_i24,
1128  }
1129}
1130
1131/// Recognizes a signed 4 byte integer
1132///
1133/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i32 integer,
1134/// otherwise if `nom8::number::Endianness::Little` parse a little endian i32 integer.
1135/// *complete version*: returns an error if there is not enough input data
1136/// ```rust
1137/// # use nom8::{Err, error::ErrorKind, Needed};
1138/// # use nom8::Needed::Size;
1139/// use nom8::number::complete::i32;
1140///
1141/// let be_i32 = |s| {
1142///   i32(nom8::number::Endianness::Big)(s)
1143/// };
1144///
1145/// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507)));
1146/// assert_eq!(be_i32(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1147///
1148/// let le_i32 = |s| {
1149///   i32(nom8::number::Endianness::Little)(s)
1150/// };
1151///
1152/// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300)));
1153/// assert_eq!(le_i32(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1154/// ```
1155#[inline]
1156///
1157/// **WARNING:** Deprecated, replaced with [`nom8::number::i32`][crate::number::i32]
1158#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i32`")]
1159pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i32, E>
1160where
1161  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1162{
1163  match endian {
1164    crate::number::Endianness::Big => be_i32,
1165    crate::number::Endianness::Little => le_i32,
1166    #[cfg(target_endian = "big")]
1167    crate::number::Endianness::Native => be_i32,
1168    #[cfg(target_endian = "little")]
1169    crate::number::Endianness::Native => le_i32,
1170  }
1171}
1172
1173/// Recognizes a signed 8 byte integer
1174///
1175/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i64 integer,
1176/// otherwise if `nom8::number::Endianness::Little` parse a little endian i64 integer.
1177/// *complete version*: returns an error if there is not enough input data
1178/// ```rust
1179/// # use nom8::{Err, error::ErrorKind, Needed};
1180/// # use nom8::Needed::Size;
1181/// use nom8::number::complete::i64;
1182///
1183/// let be_i64 = |s| {
1184///   i64(nom8::number::Endianness::Big)(s)
1185/// };
1186///
1187/// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607)));
1188/// assert_eq!(be_i64(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1189///
1190/// let le_i64 = |s| {
1191///   i64(nom8::number::Endianness::Little)(s)
1192/// };
1193///
1194/// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100)));
1195/// assert_eq!(le_i64(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1196/// ```
1197#[inline]
1198///
1199/// **WARNING:** Deprecated, replaced with [`nom8::number::i64`][crate::number::i64]
1200#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i64`")]
1201pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i64, E>
1202where
1203  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1204{
1205  match endian {
1206    crate::number::Endianness::Big => be_i64,
1207    crate::number::Endianness::Little => le_i64,
1208    #[cfg(target_endian = "big")]
1209    crate::number::Endianness::Native => be_i64,
1210    #[cfg(target_endian = "little")]
1211    crate::number::Endianness::Native => le_i64,
1212  }
1213}
1214
1215/// Recognizes a signed 16 byte integer
1216///
1217/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian i128 integer,
1218/// otherwise if `nom8::number::Endianness::Little` parse a little endian i128 integer.
1219/// *complete version*: returns an error if there is not enough input data
1220/// ```rust
1221/// # use nom8::{Err, error::ErrorKind, Needed};
1222/// # use nom8::Needed::Size;
1223/// use nom8::number::complete::i128;
1224///
1225/// let be_i128 = |s| {
1226///   i128(nom8::number::Endianness::Big)(s)
1227/// };
1228///
1229/// 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)));
1230/// assert_eq!(be_i128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1231///
1232/// let le_i128 = |s| {
1233///   i128(nom8::number::Endianness::Little)(s)
1234/// };
1235///
1236/// 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)));
1237/// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
1238/// ```
1239#[inline]
1240///
1241/// **WARNING:** Deprecated, replaced with [`nom8::number::i128`][crate::number::i128]
1242#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::i128`")]
1243pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
1244where
1245  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1246{
1247  match endian {
1248    crate::number::Endianness::Big => be_i128,
1249    crate::number::Endianness::Little => le_i128,
1250    #[cfg(target_endian = "big")]
1251    crate::number::Endianness::Native => be_i128,
1252    #[cfg(target_endian = "little")]
1253    crate::number::Endianness::Native => le_i128,
1254  }
1255}
1256
1257/// Recognizes a big endian 4 bytes floating point number.
1258///
1259/// *Complete version*: Returns an error if there is not enough input data.
1260/// ```rust
1261/// # use nom8::{Err, error::ErrorKind, Needed};
1262/// # use nom8::Needed::Size;
1263/// use nom8::number::complete::be_f32;
1264///
1265/// let parser = |s| {
1266///   be_f32(s)
1267/// };
1268///
1269/// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1270/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1271/// ```
1272#[inline]
1273///
1274/// **WARNING:** Deprecated, replaced with [`nom8::number::be_f32`][crate::number::be_f32]
1275#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_f32`")]
1276pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1277where
1278  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1279{
1280  match be_u32(input) {
1281    Err(e) => Err(e),
1282    Ok((i, o)) => Ok((i, f32::from_bits(o))),
1283  }
1284}
1285
1286/// Recognizes a big endian 8 bytes floating point number.
1287///
1288/// *Complete version*: Returns an error if there is not enough input data.
1289/// ```rust
1290/// # use nom8::{Err, error::ErrorKind, Needed};
1291/// # use nom8::Needed::Size;
1292/// use nom8::number::complete::be_f64;
1293///
1294/// let parser = |s| {
1295///   be_f64(s)
1296/// };
1297///
1298/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1299/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1300/// ```
1301#[inline]
1302///
1303/// **WARNING:** Deprecated, replaced with [`nom8::number::be_f64`][crate::number::be_f64]
1304#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::be_f64`")]
1305pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1306where
1307  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1308{
1309  match be_u64(input) {
1310    Err(e) => Err(e),
1311    Ok((i, o)) => Ok((i, f64::from_bits(o))),
1312  }
1313}
1314
1315/// Recognizes a little endian 4 bytes floating point number.
1316///
1317/// *Complete version*: Returns an error if there is not enough input data.
1318/// ```rust
1319/// # use nom8::{Err, error::ErrorKind, Needed};
1320/// # use nom8::Needed::Size;
1321/// use nom8::number::complete::le_f32;
1322///
1323/// let parser = |s| {
1324///   le_f32(s)
1325/// };
1326///
1327/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1328/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1329/// ```
1330#[inline]
1331///
1332/// **WARNING:** Deprecated, replaced with [`nom8::number::le_f32`][crate::number::le_f32]
1333#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_f32`")]
1334pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1335where
1336  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1337{
1338  match le_u32(input) {
1339    Err(e) => Err(e),
1340    Ok((i, o)) => Ok((i, f32::from_bits(o))),
1341  }
1342}
1343
1344/// Recognizes a little endian 8 bytes floating point number.
1345///
1346/// *Complete version*: Returns an error if there is not enough input data.
1347/// ```rust
1348/// # use nom8::{Err, error::ErrorKind, Needed};
1349/// # use nom8::Needed::Size;
1350/// use nom8::number::complete::le_f64;
1351///
1352/// let parser = |s| {
1353///   le_f64(s)
1354/// };
1355///
1356/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
1357/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1358/// ```
1359#[inline]
1360///
1361/// **WARNING:** Deprecated, replaced with [`nom8::number::le_f64`][crate::number::le_f64]
1362#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::le_f64`")]
1363pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1364where
1365  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1366{
1367  match le_u64(input) {
1368    Err(e) => Err(e),
1369    Ok((i, o)) => Ok((i, f64::from_bits(o))),
1370  }
1371}
1372
1373/// Recognizes a 4 byte floating point number
1374///
1375/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian f32 float,
1376/// otherwise if `nom8::number::Endianness::Little` parse a little endian f32 float.
1377/// *complete version*: returns an error if there is not enough input data
1378/// ```rust
1379/// # use nom8::{Err, error::ErrorKind, Needed};
1380/// # use nom8::Needed::Size;
1381/// use nom8::number::complete::f32;
1382///
1383/// let be_f32 = |s| {
1384///   f32(nom8::number::Endianness::Big)(s)
1385/// };
1386///
1387/// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1388/// assert_eq!(be_f32(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1389///
1390/// let le_f32 = |s| {
1391///   f32(nom8::number::Endianness::Little)(s)
1392/// };
1393///
1394/// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
1395/// assert_eq!(le_f32(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1396/// ```
1397#[inline]
1398///
1399/// **WARNING:** Deprecated, replaced with [`nom8::number::f32`][crate::number::f32]
1400#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::f32`")]
1401pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1402where
1403  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1404{
1405  match endian {
1406    crate::number::Endianness::Big => be_f32,
1407    crate::number::Endianness::Little => le_f32,
1408    #[cfg(target_endian = "big")]
1409    crate::number::Endianness::Native => be_f32,
1410    #[cfg(target_endian = "little")]
1411    crate::number::Endianness::Native => le_f32,
1412  }
1413}
1414
1415/// Recognizes an 8 byte floating point number
1416///
1417/// If the parameter is `nom8::number::Endianness::Big`, parse a big endian f64 float,
1418/// otherwise if `nom8::number::Endianness::Little` parse a little endian f64 float.
1419/// *complete version*: returns an error if there is not enough input data
1420/// ```rust
1421/// # use nom8::{Err, error::ErrorKind, Needed};
1422/// # use nom8::Needed::Size;
1423/// use nom8::number::complete::f64;
1424///
1425/// let be_f64 = |s| {
1426///   f64(nom8::number::Endianness::Big)(s)
1427/// };
1428///
1429/// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
1430/// assert_eq!(be_f64(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1431///
1432/// let le_f64 = |s| {
1433///   f64(nom8::number::Endianness::Little)(s)
1434/// };
1435///
1436/// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
1437/// assert_eq!(le_f64(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
1438/// ```
1439#[inline]
1440///
1441/// **WARNING:** Deprecated, replaced with [`nom8::number::f64`][crate::number::f64]
1442#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::f64`")]
1443pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1444where
1445  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
1446{
1447  match endian {
1448    crate::number::Endianness::Big => be_f64,
1449    crate::number::Endianness::Little => le_f64,
1450    #[cfg(target_endian = "big")]
1451    crate::number::Endianness::Native => be_f64,
1452    #[cfg(target_endian = "little")]
1453    crate::number::Endianness::Native => le_f64,
1454  }
1455}
1456
1457/// Recognizes a hex-encoded integer.
1458///
1459/// *Complete version*: Will parse until the end of input if it has less than 8 bytes.
1460/// ```rust
1461/// # use nom8::{Err, error::ErrorKind, Needed};
1462/// # use nom8::Needed::Size;
1463/// use nom8::number::complete::hex_u32;
1464///
1465/// let parser = |s| {
1466///   hex_u32(s)
1467/// };
1468///
1469/// assert_eq!(parser(&b"01AE"[..]), Ok((&b""[..], 0x01AE)));
1470/// assert_eq!(parser(&b"abc"[..]), Ok((&b""[..], 0x0ABC)));
1471/// assert_eq!(parser(&b"ggg"[..]), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
1472/// ```
1473#[inline]
1474///
1475/// **WARNING:** Deprecated, replaced with [`nom8::number::hex_u32`][crate::number::hex_u32]
1476#[deprecated(since = "8.0.0", note = "Replaced with `nom8::number::hex_u32`")]
1477pub fn hex_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
1478where
1479  I: InputTakeAtPosition,
1480  I: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1481  <I as InputTakeAtPosition>::Item: AsChar,
1482  I: AsBytes,
1483  I: InputLength,
1484{
1485  let e: ErrorKind = ErrorKind::IsA;
1486  let (i, o) = input.split_at_position1_complete(
1487    |c| {
1488      let c = c.as_char();
1489      !"0123456789abcdefABCDEF".contains(c)
1490    },
1491    e,
1492  )?;
1493
1494  // Do not parse more than 8 characters for a u32
1495  let (parsed, remaining) = if o.input_len() <= 8 {
1496    (o, i)
1497  } else {
1498    (input.slice(..8), input.slice(8..))
1499  };
1500
1501  let res = parsed
1502    .as_bytes()
1503    .iter()
1504    .rev()
1505    .enumerate()
1506    .map(|(k, &v)| {
1507      let digit = v as char;
1508      digit.to_digit(16).unwrap_or(0) << (k * 4)
1509    })
1510    .sum();
1511
1512  Ok((remaining, res))
1513}
1514
1515/// Recognizes floating point number in a byte string and returns the corresponding slice.
1516///
1517/// *Complete version*: Can parse until the end of input.
1518///
1519/// ```rust
1520/// # use nom8::{Err, error::ErrorKind, Needed};
1521/// # use nom8::Needed::Size;
1522/// use nom8::number::complete::recognize_float;
1523///
1524/// let parser = |s| {
1525///   recognize_float(s)
1526/// };
1527///
1528/// assert_eq!(parser("11e-1"), Ok(("", "11e-1")));
1529/// assert_eq!(parser("123E-02"), Ok(("", "123E-02")));
1530/// assert_eq!(parser("123K-01"), Ok(("K-01", "123")));
1531/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
1532/// ```
1533#[rustfmt::skip]
1534///
1535/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float`][crate::character::recognize_float]
1536#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::recognize_float`")]
1537pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, <T as IntoOutput>::Output, E>
1538where
1539  T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1540  T: Clone + Offset,
1541  T: InputIter,
1542  T: IntoOutput,
1543  <T as InputIter>::Item: AsChar,
1544  T: InputTakeAtPosition,
1545  <T as InputTakeAtPosition>::Item: AsChar,
1546{
1547  recognize(
1548    tuple((
1549      opt(alt((char('+'), char('-')))),
1550      alt((
1551        map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
1552        map(tuple((char('.'), digit1)), |_| ())
1553      )),
1554      opt(tuple((
1555        alt((char('e'), char('E'))),
1556        opt(alt((char('+'), char('-')))),
1557        cut(digit1)
1558      )))
1559    ))
1560  )(input)
1561}
1562
1563// workaround until issues with minimal-lexical are fixed
1564#[doc(hidden)]
1565///
1566/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float_or_exceptions`][crate::character::recognize_float_or_exceptions]
1567#[deprecated(
1568  since = "8.0.0",
1569  note = "Replaced with `nom8::character::recognize_float_or_exceptions`"
1570)]
1571pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(
1572  input: T,
1573) -> IResult<T, <T as IntoOutput>::Output, E>
1574where
1575  T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
1576  T: Clone + Offset,
1577  T: InputIter + InputTake + Compare<&'static str>,
1578  <T as InputIter>::Item: AsChar,
1579  T: InputTakeAtPosition,
1580  T: IntoOutput,
1581  <T as InputTakeAtPosition>::Item: AsChar,
1582{
1583  alt((
1584    |i: T| {
1585      recognize_float::<_, E>(i.clone()).map_err(|e| match e {
1586        crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
1587        crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
1588        crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
1589      })
1590    },
1591    |i: T| {
1592      crate::bytes::complete::tag_no_case::<_, _, E>("nan")(i.clone())
1593        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1594    },
1595    |i: T| {
1596      crate::bytes::complete::tag_no_case::<_, _, E>("inf")(i.clone())
1597        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1598    },
1599    |i: T| {
1600      crate::bytes::complete::tag_no_case::<_, _, E>("infinity")(i.clone())
1601        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1602    },
1603  ))(input)
1604}
1605
1606/// Recognizes a floating point number in text format
1607///
1608/// It returns a tuple of (`sign`, `integer part`, `fraction part` and `exponent`) of the input
1609/// data.
1610///
1611/// *Complete version*: Can parse until the end of input.
1612///
1613///
1614/// **WARNING:** Deprecated, replaced with [`nom8::character::recognize_float_parts`][crate::character::recognize_float_parts]
1615#[deprecated(
1616  since = "8.0.0",
1617  note = "Replaced with `nom8::character::recognize_float_parts`"
1618)]
1619pub fn recognize_float_parts<T, E: ParseError<T>>(
1620  input: T,
1621) -> IResult<
1622  T,
1623  (
1624    bool,
1625    <T as IntoOutput>::Output,
1626    <T as IntoOutput>::Output,
1627    i32,
1628  ),
1629  E,
1630>
1631where
1632  T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1633  T: Clone + Offset,
1634  T: InputIter + InputTake,
1635  T: IntoOutput,
1636  <T as InputIter>::Item: AsChar + Copy,
1637  T: InputTakeAtPosition + InputLength,
1638  <T as InputTakeAtPosition>::Item: AsChar,
1639  T: for<'a> Compare<&'a [u8]>,
1640  T: AsBytes,
1641{
1642  let (i, sign) = sign(input.clone())?;
1643
1644  let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
1645    Some(index) => i.take_split(index),
1646    None => i.take_split(i.input_len()),
1647  };
1648  //let (i, mut integer) = digit0(i)?;
1649  let (i, mut integer) = match i
1650    .as_bytes()
1651    .iter()
1652    .position(|c| !(*c >= b'0' && *c <= b'9'))
1653  {
1654    Some(index) => i.take_split(index),
1655    None => i.take_split(i.input_len()),
1656  };
1657
1658  if integer.input_len() == 0 && zeroes.input_len() > 0 {
1659    // keep the last zero if integer is empty
1660    integer = zeroes.slice(zeroes.input_len() - 1..);
1661  }
1662
1663  let (i, opt_dot) = opt(tag(&b"."[..]))(i)?;
1664  let (i, fraction) = if opt_dot.is_none() {
1665    let i2 = i.clone();
1666    (i2, i.slice(..0))
1667  } else {
1668    // match number, trim right zeroes
1669    let mut zero_count = 0usize;
1670    let mut position = None;
1671    for (pos, c) in i.as_bytes().iter().enumerate() {
1672      if *c >= b'0' && *c <= b'9' {
1673        if *c == b'0' {
1674          zero_count += 1;
1675        } else {
1676          zero_count = 0;
1677        }
1678      } else {
1679        position = Some(pos);
1680        break;
1681      }
1682    }
1683
1684    let position = position.unwrap_or(i.input_len());
1685
1686    let index = if zero_count == 0 {
1687      position
1688    } else if zero_count == position {
1689      position - zero_count + 1
1690    } else {
1691      position - zero_count
1692    };
1693
1694    (i.slice(position..), i.slice(..index))
1695  };
1696
1697  if integer.input_len() == 0 && fraction.input_len() == 0 {
1698    return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
1699  }
1700
1701  let i2 = i.clone();
1702  let (i, e) = match i.as_bytes().iter().next() {
1703    Some(b'e') => (i.slice(1..), true),
1704    Some(b'E') => (i.slice(1..), true),
1705    _ => (i, false),
1706  };
1707
1708  let (i, exp) = if e {
1709    cut(crate::character::complete::i32)(i)?
1710  } else {
1711    (i2, 0)
1712  };
1713
1714  Ok((
1715    i,
1716    (sign, integer.into_output(), fraction.into_output(), exp),
1717  ))
1718}
1719
1720use crate::input::ParseTo;
1721
1722/// Recognizes floating point number in text format and returns a f32.
1723///
1724/// *Complete version*: Can parse until the end of input.
1725/// ```rust
1726/// # use nom8::{Err, error::ErrorKind, Needed};
1727/// # use nom8::Needed::Size;
1728/// use nom8::number::complete::float;
1729///
1730/// let parser = |s| {
1731///   float(s)
1732/// };
1733///
1734/// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1735/// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1736/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1737/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1738/// ```
1739///
1740/// **WARNING:** Deprecated, replaced with [`nom8::character::f32`][crate::character::f32]
1741#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::f32`")]
1742pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1743where
1744  T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1745  T: Clone + Offset + Compare<&'static str>,
1746  T: InputIter + InputLength + InputTake,
1747  T: IntoOutput,
1748  <T as IntoOutput>::Output: ParseTo<f32>,
1749  <T as InputIter>::Item: AsChar + Copy,
1750  <T as InputIter>::IterElem: Clone,
1751  T: InputTakeAtPosition,
1752  <T as InputTakeAtPosition>::Item: AsChar,
1753  T: AsBytes,
1754  T: for<'a> Compare<&'a [u8]>,
1755{
1756  let (i, s) = recognize_float_or_exceptions(input)?;
1757  match s.parse_to() {
1758    Some(f) => Ok((i, f)),
1759    None => Err(crate::Err::Error(E::from_error_kind(
1760      i,
1761      crate::error::ErrorKind::Float,
1762    ))),
1763  }
1764}
1765
1766/// Recognizes floating point number in text format and returns a f64.
1767///
1768/// *Complete version*: Can parse until the end of input.
1769/// ```rust
1770/// # use nom8::{Err, error::ErrorKind, Needed};
1771/// # use nom8::Needed::Size;
1772/// use nom8::number::complete::double;
1773///
1774/// let parser = |s| {
1775///   double(s)
1776/// };
1777///
1778/// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
1779/// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
1780/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
1781/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
1782/// ```
1783///
1784/// **WARNING:** Deprecated, replaced with [`nom8::character::f64`][crate::character::f64]
1785#[deprecated(since = "8.0.0", note = "Replaced with `nom8::character::f64`")]
1786pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1787where
1788  T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Slice<Range<usize>>,
1789  T: Clone + Offset + Compare<&'static str>,
1790  T: InputIter + InputLength + InputTake,
1791  T: IntoOutput,
1792  <T as IntoOutput>::Output: ParseTo<f64>,
1793  <T as InputIter>::Item: AsChar + Copy,
1794  <T as InputIter>::IterElem: Clone,
1795  T: InputTakeAtPosition,
1796  <T as InputTakeAtPosition>::Item: AsChar,
1797  T: AsBytes,
1798  T: for<'a> Compare<&'a [u8]>,
1799{
1800  let (i, s) = recognize_float_or_exceptions(input)?;
1801  match s.parse_to() {
1802    Some(f) => Ok((i, f)),
1803    None => Err(crate::Err::Error(E::from_error_kind(
1804      i,
1805      crate::error::ErrorKind::Float,
1806    ))),
1807  }
1808}
1809
1810#[cfg(test)]
1811mod tests {
1812  use super::*;
1813  use crate::error::ErrorKind;
1814  use crate::Err;
1815  use proptest::prelude::*;
1816
1817  macro_rules! assert_parse(
1818    ($left: expr, $right: expr) => {
1819      let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
1820      assert_eq!(res, $right);
1821    };
1822  );
1823
1824  #[test]
1825  fn i8_tests() {
1826    assert_parse!(i8(&[0x00][..]), Ok((&b""[..], 0)));
1827    assert_parse!(i8(&[0x7f][..]), Ok((&b""[..], 127)));
1828    assert_parse!(i8(&[0xff][..]), Ok((&b""[..], -1)));
1829    assert_parse!(i8(&[0x80][..]), Ok((&b""[..], -128)));
1830  }
1831
1832  #[test]
1833  fn be_i8_tests() {
1834    assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0)));
1835    assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1836    assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1)));
1837    assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128)));
1838  }
1839
1840  #[test]
1841  fn be_i16_tests() {
1842    assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1843    assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16)));
1844    assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1845    assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16)));
1846  }
1847
1848  #[test]
1849  fn be_u24_tests() {
1850    assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1851    assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32)));
1852    assert_parse!(
1853      be_u24(&[0x12, 0x34, 0x56][..]),
1854      Ok((&b""[..], 1_193_046_u32))
1855    );
1856  }
1857
1858  #[test]
1859  fn be_i24_tests() {
1860    assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1861    assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32)));
1862    assert_parse!(
1863      be_i24(&[0xED, 0xCB, 0xAA][..]),
1864      Ok((&b""[..], -1_193_046_i32))
1865    );
1866  }
1867
1868  #[test]
1869  fn be_i32_tests() {
1870    assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1871    assert_parse!(
1872      be_i32(&[0x7f, 0xff, 0xff, 0xff][..]),
1873      Ok((&b""[..], 2_147_483_647_i32))
1874    );
1875    assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1876    assert_parse!(
1877      be_i32(&[0x80, 0x00, 0x00, 0x00][..]),
1878      Ok((&b""[..], -2_147_483_648_i32))
1879    );
1880  }
1881
1882  #[test]
1883  fn be_i64_tests() {
1884    assert_parse!(
1885      be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1886      Ok((&b""[..], 0))
1887    );
1888    assert_parse!(
1889      be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1890      Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1891    );
1892    assert_parse!(
1893      be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
1894      Ok((&b""[..], -1))
1895    );
1896    assert_parse!(
1897      be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
1898      Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1899    );
1900  }
1901
1902  #[test]
1903  fn be_i128_tests() {
1904    assert_parse!(
1905      be_i128(
1906        &[
1907          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908          0x00
1909        ][..]
1910      ),
1911      Ok((&b""[..], 0))
1912    );
1913    assert_parse!(
1914      be_i128(
1915        &[
1916          0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1917          0xff
1918        ][..]
1919      ),
1920      Ok((
1921        &b""[..],
1922        170_141_183_460_469_231_731_687_303_715_884_105_727_i128
1923      ))
1924    );
1925    assert_parse!(
1926      be_i128(
1927        &[
1928          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1929          0xff
1930        ][..]
1931      ),
1932      Ok((&b""[..], -1))
1933    );
1934    assert_parse!(
1935      be_i128(
1936        &[
1937          0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1938          0x00
1939        ][..]
1940      ),
1941      Ok((
1942        &b""[..],
1943        -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
1944      ))
1945    );
1946  }
1947
1948  #[test]
1949  fn le_i8_tests() {
1950    assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0)));
1951    assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127)));
1952    assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1)));
1953    assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128)));
1954  }
1955
1956  #[test]
1957  fn le_i16_tests() {
1958    assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
1959    assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16)));
1960    assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
1961    assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
1962  }
1963
1964  #[test]
1965  fn le_u24_tests() {
1966    assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1967    assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32)));
1968    assert_parse!(
1969      le_u24(&[0x56, 0x34, 0x12][..]),
1970      Ok((&b""[..], 1_193_046_u32))
1971    );
1972  }
1973
1974  #[test]
1975  fn le_i24_tests() {
1976    assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32)));
1977    assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32)));
1978    assert_parse!(
1979      le_i24(&[0xAA, 0xCB, 0xED][..]),
1980      Ok((&b""[..], -1_193_046_i32))
1981    );
1982  }
1983
1984  #[test]
1985  fn le_i32_tests() {
1986    assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
1987    assert_parse!(
1988      le_i32(&[0xff, 0xff, 0xff, 0x7f][..]),
1989      Ok((&b""[..], 2_147_483_647_i32))
1990    );
1991    assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)));
1992    assert_parse!(
1993      le_i32(&[0x00, 0x00, 0x00, 0x80][..]),
1994      Ok((&b""[..], -2_147_483_648_i32))
1995    );
1996  }
1997
1998  #[test]
1999  fn le_i64_tests() {
2000    assert_parse!(
2001      le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2002      Ok((&b""[..], 0))
2003    );
2004    assert_parse!(
2005      le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
2006      Ok((&b""[..], 9_223_372_036_854_775_807_i64))
2007    );
2008    assert_parse!(
2009      le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
2010      Ok((&b""[..], -1))
2011    );
2012    assert_parse!(
2013      le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
2014      Ok((&b""[..], -9_223_372_036_854_775_808_i64))
2015    );
2016  }
2017
2018  #[test]
2019  fn le_i128_tests() {
2020    assert_parse!(
2021      le_i128(
2022        &[
2023          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2024          0x00
2025        ][..]
2026      ),
2027      Ok((&b""[..], 0))
2028    );
2029    assert_parse!(
2030      le_i128(
2031        &[
2032          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2033          0x7f
2034        ][..]
2035      ),
2036      Ok((
2037        &b""[..],
2038        170_141_183_460_469_231_731_687_303_715_884_105_727_i128
2039      ))
2040    );
2041    assert_parse!(
2042      le_i128(
2043        &[
2044          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2045          0xff
2046        ][..]
2047      ),
2048      Ok((&b""[..], -1))
2049    );
2050    assert_parse!(
2051      le_i128(
2052        &[
2053          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2054          0x80
2055        ][..]
2056      ),
2057      Ok((
2058        &b""[..],
2059        -170_141_183_460_469_231_731_687_303_715_884_105_728_i128
2060      ))
2061    );
2062  }
2063
2064  #[test]
2065  fn be_f32_tests() {
2066    assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2067    assert_parse!(
2068      be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]),
2069      Ok((&b""[..], 185_728_392_f32))
2070    );
2071  }
2072
2073  #[test]
2074  fn be_f64_tests() {
2075    assert_parse!(
2076      be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2077      Ok((&b""[..], 0_f64))
2078    );
2079    assert_parse!(
2080      be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
2081      Ok((&b""[..], 185_728_392_f64))
2082    );
2083  }
2084
2085  #[test]
2086  fn le_f32_tests() {
2087    assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32)));
2088    assert_parse!(
2089      le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]),
2090      Ok((&b""[..], 185_728_392_f32))
2091    );
2092  }
2093
2094  #[test]
2095  fn le_f64_tests() {
2096    assert_parse!(
2097      le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
2098      Ok((&b""[..], 0_f64))
2099    );
2100    assert_parse!(
2101      le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
2102      Ok((&b""[..], 185_728_392_f64))
2103    );
2104  }
2105
2106  #[test]
2107  fn hex_u32_tests() {
2108    assert_parse!(
2109      hex_u32(&b";"[..]),
2110      Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
2111    );
2112    assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
2113    assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
2114    assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2115    assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
2116    assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
2117    assert_parse!(
2118      hex_u32(&b"c5a31be201;"[..]),
2119      Ok((&b"01;"[..], 3_315_801_058))
2120    );
2121    assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
2122    assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
2123    assert_parse!(hex_u32(&b"12af"[..]), Ok((&b""[..], 0x12af)));
2124  }
2125
2126  #[test]
2127  #[cfg(feature = "std")]
2128  fn float_test() {
2129    let mut test_cases = vec![
2130      "+3.14",
2131      "3.14",
2132      "-3.14",
2133      "0",
2134      "0.0",
2135      "1.",
2136      ".789",
2137      "-.5",
2138      "1e7",
2139      "-1E-7",
2140      ".3e-2",
2141      "1.e4",
2142      "1.2e4",
2143      "12.34",
2144      "-1.234E-12",
2145      "-1.234e-12",
2146      "0.00000000000000000087",
2147    ];
2148
2149    for test in test_cases.drain(..) {
2150      let expected32 = str::parse::<f32>(test).unwrap();
2151      let expected64 = str::parse::<f64>(test).unwrap();
2152
2153      println!("now parsing: {} -> {}", test, expected32);
2154
2155      let larger = format!("{}", test);
2156      assert_parse!(recognize_float(&larger[..]), Ok(("", test)));
2157
2158      assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32)));
2159      assert_parse!(float(&larger[..]), Ok(("", expected32)));
2160
2161      assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64)));
2162      assert_parse!(double(&larger[..]), Ok(("", expected64)));
2163    }
2164
2165    let remaining_exponent = "-1.234E-";
2166    assert_parse!(
2167      recognize_float(remaining_exponent),
2168      Err(Err::Failure(("", ErrorKind::Digit)))
2169    );
2170
2171    let (_i, nan) = float::<_, ()>("NaN").unwrap();
2172    assert!(nan.is_nan());
2173
2174    let (_i, inf) = float::<_, ()>("inf").unwrap();
2175    assert!(inf.is_infinite());
2176    let (_i, inf) = float::<_, ()>("infinite").unwrap();
2177    assert!(inf.is_infinite());
2178  }
2179
2180  #[test]
2181  fn configurable_endianness() {
2182    use crate::number::Endianness;
2183
2184    fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2185      u16(Endianness::Big)(i)
2186    }
2187    fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
2188      u16(Endianness::Little)(i)
2189    }
2190    assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
2191    assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
2192
2193    fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2194      u32(Endianness::Big)(i)
2195    }
2196    fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
2197      u32(Endianness::Little)(i)
2198    }
2199    assert_eq!(
2200      be_tst32(&[0x12, 0x00, 0x60, 0x00]),
2201      Ok((&b""[..], 302_014_464_u32))
2202    );
2203    assert_eq!(
2204      le_tst32(&[0x12, 0x00, 0x60, 0x00]),
2205      Ok((&b""[..], 6_291_474_u32))
2206    );
2207
2208    fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2209      u64(Endianness::Big)(i)
2210    }
2211    fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
2212      u64(Endianness::Little)(i)
2213    }
2214    assert_eq!(
2215      be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2216      Ok((&b""[..], 1_297_142_246_100_992_000_u64))
2217    );
2218    assert_eq!(
2219      le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2220      Ok((&b""[..], 36_028_874_334_666_770_u64))
2221    );
2222
2223    fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2224      i16(Endianness::Big)(i)
2225    }
2226    fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
2227      i16(Endianness::Little)(i)
2228    }
2229    assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
2230    assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
2231
2232    fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2233      i32(Endianness::Big)(i)
2234    }
2235    fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
2236      i32(Endianness::Little)(i)
2237    }
2238    assert_eq!(
2239      be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2240      Ok((&b""[..], 1_204_224_i32))
2241    );
2242    assert_eq!(
2243      le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
2244      Ok((&b""[..], 6_296_064_i32))
2245    );
2246
2247    fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2248      i64(Endianness::Big)(i)
2249    }
2250    fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
2251      i64(Endianness::Little)(i)
2252    }
2253    assert_eq!(
2254      be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2255      Ok((&b""[..], 71_881_672_479_506_432_i64))
2256    );
2257    assert_eq!(
2258      le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
2259      Ok((&b""[..], 36_028_874_334_732_032_i64))
2260    );
2261  }
2262
2263  #[cfg(feature = "std")]
2264  fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
2265    match recognize_float_or_exceptions(i) {
2266      Err(e) => Err(e),
2267      Ok((i, s)) => {
2268        if s.is_empty() {
2269          return Err(Err::Error(()));
2270        }
2271        match s.parse_to() {
2272          Some(n) => Ok((i, n)),
2273          None => Err(Err::Error(())),
2274        }
2275      }
2276    }
2277  }
2278
2279  proptest! {
2280    #[test]
2281    #[cfg(feature = "std")]
2282    fn floats(s in "\\PC*") {
2283        println!("testing {}", s);
2284        let res1 = parse_f64(&s);
2285        let res2 = double::<_, ()>(s.as_str());
2286        assert_eq!(res1, res2);
2287    }
2288  }
2289}