1 //! parsers recognizing numbers, complete input version
2 
3 use crate::internal::*;
4 use crate::error::ParseError;
5 use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition};
6 use crate::lib::std::ops::{RangeFrom, RangeTo};
7 use crate::traits::{Offset, Slice};
8 use crate::error::{ErrorKind, make_error};
9 use crate::character::complete::{char, digit1};
10 use crate::combinator::{opt, cut, map, recognize};
11 use crate::branch::alt;
12 use crate::sequence::{tuple, pair};
13 
14 /// Recognizes an unsigned 1 byte integer
15 ///
16 /// *complete version*: returns an error if there is not enough input data
17 /// ```rust
18 /// # use nom::{Err, error::ErrorKind, Needed};
19 /// # use nom::Needed::Size;
20 /// use nom::number::complete::be_u8;
21 ///
22 /// let parser = |s| {
23 ///   be_u8(s)
24 /// };
25 ///
26 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00)));
27 /// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof))));
28 /// ```
29 #[inline]
be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E>30 pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
31   if i.len() < 1 {
32     Err(Err::Error(make_error(i, ErrorKind::Eof)))
33   } else {
34     Ok((&i[1..], i[0]))
35   }
36 }
37 
38 /// Recognizes a big endian unsigned 2 bytes integer
39 ///
40 /// *complete version*: returns an error if there is not enough input data
41 /// ```rust
42 /// # use nom::{Err, error::ErrorKind, Needed};
43 /// # use nom::Needed::Size;
44 /// use nom::number::complete::be_u16;
45 ///
46 /// let parser = |s| {
47 ///   be_u16(s)
48 /// };
49 ///
50 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003)));
51 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
52 /// ```
53 #[inline]
be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E>54 pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
55   if i.len() < 2 {
56     Err(Err::Error(make_error(i, ErrorKind::Eof)))
57   } else {
58     let res = ((i[0] as u16) << 8) + i[1] as u16;
59     Ok((&i[2..], res))
60   }
61 }
62 
63 /// Recognizes a big endian unsigned 3 byte integer
64 ///
65 /// *complete version*: returns an error if there is not enough input data
66 /// ```rust
67 /// # use nom::{Err, error::ErrorKind, Needed};
68 /// # use nom::Needed::Size;
69 /// use nom::number::complete::be_u24;
70 ///
71 /// let parser = |s| {
72 ///   be_u24(s)
73 /// };
74 ///
75 /// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305)));
76 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
77 /// ```
78 #[inline]
be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>79 pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
80   if i.len() < 3 {
81     Err(Err::Error(make_error(i, ErrorKind::Eof)))
82   } else {
83     let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32);
84     Ok((&i[3..], res))
85   }
86 }
87 
88 /// Recognizes a big endian unsigned 4 bytes integer
89 ///
90 /// *complete version*: returns an error if there is not enough input data
91 /// ```rust
92 /// # use nom::{Err, error::ErrorKind, Needed};
93 /// # use nom::Needed::Size;
94 /// use nom::number::complete::be_u32;
95 ///
96 /// let parser = |s| {
97 ///   be_u32(s)
98 /// };
99 ///
100 /// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507)));
101 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
102 /// ```
103 #[inline]
be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>104 pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
105   if i.len() < 4 {
106     Err(Err::Error(make_error(i, ErrorKind::Eof)))
107   } else {
108     let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32;
109     Ok((&i[4..], res))
110   }
111 }
112 
113 /// Recognizes a big endian unsigned 8 bytes integer
114 ///
115 /// *complete version*: returns an error if there is not enough input data
116 /// ```rust
117 /// # use nom::{Err, error::ErrorKind, Needed};
118 /// # use nom::Needed::Size;
119 /// use nom::number::complete::be_u64;
120 ///
121 /// let parser = |s| {
122 ///   be_u64(s)
123 /// };
124 ///
125 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607)));
126 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
127 /// ```
128 #[inline]
be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E>129 pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
130   if i.len() < 8 {
131     Err(Err::Error(make_error(i, ErrorKind::Eof)))
132   } else {
133     let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24)
134       + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64;
135     Ok((&i[8..], res))
136   }
137 }
138 
139 /// Recognizes a big endian unsigned 16 bytes integer
140 ///
141 /// *complete version*: returns an error if there is not enough input data
142 /// ```rust
143 /// # use nom::{Err, error::ErrorKind, Needed};
144 /// # use nom::Needed::Size;
145 /// use nom::number::complete::be_u128;
146 ///
147 /// let parser = |s| {
148 ///   be_u128(s)
149 /// };
150 ///
151 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
152 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
153 /// ```
154 #[inline]
155 #[cfg(stable_i128)]
be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E>156 pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
157   if i.len() < 16 {
158     Err(Err::Error(make_error(i, ErrorKind::Eof)))
159   } else {
160     let res = ((i[0] as u128) << 120)
161       + ((i[1] as u128) << 112)
162       + ((i[2] as u128) << 104)
163       + ((i[3] as u128) << 96)
164       + ((i[4] as u128) << 88)
165       + ((i[5] as u128) << 80)
166       + ((i[6] as u128) << 72)
167       + ((i[7] as u128) << 64)
168       + ((i[8] as u128) << 56)
169       + ((i[9] as u128) << 48)
170       + ((i[10] as u128) << 40)
171       + ((i[11] as u128) << 32)
172       + ((i[12] as u128) << 24)
173       + ((i[13] as u128) << 16)
174       + ((i[14] as u128) << 8)
175       + i[15] as u128;
176     Ok((&i[16..], res))
177   }
178 }
179 
180 /// Recognizes a signed 1 byte integer
181 ///
182 /// *complete version*: returns an error if there is not enough input data
183 /// ```rust
184 /// # use nom::{Err, error::ErrorKind, Needed};
185 /// # use nom::Needed::Size;
186 /// use nom::number::complete::be_i8;
187 ///
188 /// let parser = |s| {
189 ///   be_i8(s)
190 /// };
191 ///
192 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00)));
193 /// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof))));
194 /// ```
195 #[inline]
be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E>196 pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
197   map!(i, be_u8, |x| x as i8)
198 }
199 
200 /// Recognizes a big endian signed 2 bytes integer
201 ///
202 /// *complete version*: returns an error if there is not enough input data
203 /// ```rust
204 /// # use nom::{Err, error::ErrorKind, Needed};
205 /// # use nom::Needed::Size;
206 /// use nom::number::complete::be_i16;
207 ///
208 /// let parser = |s| {
209 ///   be_i16(s)
210 /// };
211 ///
212 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003)));
213 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
214 /// ```
215 #[inline]
be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E>216 pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
217   map!(i, be_u16, |x| x as i16)
218 }
219 
220 /// Recognizes a big endian signed 3 bytes integer
221 ///
222 /// *complete version*: returns an error if there is not enough input data
223 /// ```rust
224 /// # use nom::{Err, error::ErrorKind, Needed};
225 /// # use nom::Needed::Size;
226 /// use nom::number::complete::be_i24;
227 ///
228 /// let parser = |s| {
229 ///   be_i24(s)
230 /// };
231 ///
232 /// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305)));
233 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
234 /// ```
235 #[inline]
be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>236 pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
237   // Same as the unsigned version but we need to sign-extend manually here
238   map!(i, be_u24, |x| if x & 0x80_00_00 != 0 {
239     (x | 0xff_00_00_00) as i32
240   } else {
241     x as i32
242   })
243 }
244 
245 /// Recognizes a big endian signed 4 bytes integer
246 ///
247 /// *complete version*: returns an error if there is not enough input data
248 /// ```rust
249 /// # use nom::{Err, error::ErrorKind, Needed};
250 /// # use nom::Needed::Size;
251 /// use nom::number::complete::be_i32;
252 ///
253 /// let parser = |s| {
254 ///   be_i32(s)
255 /// };
256 ///
257 /// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507)));
258 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
259 /// ```
260 #[inline]
be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>261 pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
262   map!(i, be_u32, |x| x as i32)
263 }
264 
265 /// Recognizes a big endian signed 8 bytes integer
266 ///
267 /// *complete version*: returns an error if there is not enough input data
268 /// ```rust
269 /// # use nom::{Err, error::ErrorKind, Needed};
270 /// # use nom::Needed::Size;
271 /// use nom::number::complete::be_i64;
272 ///
273 /// let parser = |s| {
274 ///   be_i64(s)
275 /// };
276 ///
277 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607)));
278 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
279 /// ```
280 #[inline]
be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E>281 pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
282   map!(i, be_u64, |x| x as i64)
283 }
284 
285 /// Recognizes a big endian signed 16 bytes integer
286 ///
287 /// *complete version*: returns an error if there is not enough input data
288 /// ```rust
289 /// # use nom::{Err, error::ErrorKind, Needed};
290 /// # use nom::Needed::Size;
291 /// use nom::number::complete::be_i128;
292 ///
293 /// let parser = |s| {
294 ///   be_i128(s)
295 /// };
296 ///
297 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607)));
298 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
299 /// ```
300 #[inline]
301 #[cfg(stable_i128)]
be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E>302 pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
303   map!(i, be_u128, |x| x as i128)
304 }
305 
306 /// Recognizes an unsigned 1 byte integer
307 ///
308 /// *complete version*: returns an error if there is not enough input data
309 /// ```rust
310 /// # use nom::{Err, error::ErrorKind, Needed};
311 /// # use nom::Needed::Size;
312 /// use nom::number::complete::le_u8;
313 ///
314 /// let parser = |s| {
315 ///   le_u8(s)
316 /// };
317 ///
318 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00)));
319 /// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof))));
320 /// ```
321 #[inline]
le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E>322 pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
323   if i.len() < 1 {
324     Err(Err::Error(make_error(i, ErrorKind::Eof)))
325   } else {
326     Ok((&i[1..], i[0]))
327   }
328 }
329 
330 /// Recognizes a little endian unsigned 2 bytes integer
331 ///
332 /// *complete version*: returns an error if there is not enough input data
333 /// ```rust
334 /// # use nom::{Err, error::ErrorKind, Needed};
335 /// # use nom::Needed::Size;
336 /// use nom::number::complete::le_u16;
337 ///
338 /// let parser = |s| {
339 ///   le_u16(s)
340 /// };
341 ///
342 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300)));
343 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
344 /// ```
345 #[inline]
le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E>346 pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
347   if i.len() < 2 {
348     Err(Err::Error(make_error(i, ErrorKind::Eof)))
349   } else {
350     let res = ((i[1] as u16) << 8) + i[0] as u16;
351     Ok((&i[2..], res))
352   }
353 }
354 
355 /// Recognizes a little endian unsigned 3 byte integer
356 ///
357 /// *complete version*: returns an error if there is not enough input data
358 /// ```rust
359 /// # use nom::{Err, error::ErrorKind, Needed};
360 /// # use nom::Needed::Size;
361 /// use nom::number::complete::le_u24;
362 ///
363 /// let parser = |s| {
364 ///   le_u24(s)
365 /// };
366 ///
367 /// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300)));
368 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
369 /// ```
370 #[inline]
le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>371 pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
372   if i.len() < 3 {
373     Err(Err::Error(make_error(i, ErrorKind::Eof)))
374   } else {
375     let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16);
376     Ok((&i[3..], res))
377   }
378 }
379 
380 /// Recognizes a little endian unsigned 4 bytes integer
381 ///
382 /// *complete version*: returns an error if there is not enough input data
383 /// ```rust
384 /// # use nom::{Err, error::ErrorKind, Needed};
385 /// # use nom::Needed::Size;
386 /// use nom::number::complete::le_u32;
387 ///
388 /// let parser = |s| {
389 ///   le_u32(s)
390 /// };
391 ///
392 /// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300)));
393 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
394 /// ```
395 #[inline]
le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>396 pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
397   if i.len() < 4 {
398     Err(Err::Error(make_error(i, ErrorKind::Eof)))
399   } else {
400     let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32;
401     Ok((&i[4..], res))
402   }
403 }
404 
405 /// Recognizes a little endian unsigned 8 bytes integer
406 ///
407 /// *complete version*: returns an error if there is not enough input data
408 /// ```rust
409 /// # use nom::{Err, error::ErrorKind, Needed};
410 /// # use nom::Needed::Size;
411 /// use nom::number::complete::le_u64;
412 ///
413 /// let parser = |s| {
414 ///   le_u64(s)
415 /// };
416 ///
417 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100)));
418 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
419 /// ```
420 #[inline]
le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E>421 pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
422   if i.len() < 8 {
423     Err(Err::Error(make_error(i, ErrorKind::Eof)))
424   } else {
425     let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24)
426       + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64;
427     Ok((&i[8..], res))
428   }
429 }
430 
431 /// Recognizes a little endian unsigned 16 bytes integer
432 ///
433 /// *complete version*: returns an error if there is not enough input data
434 /// ```rust
435 /// # use nom::{Err, error::ErrorKind, Needed};
436 /// # use nom::Needed::Size;
437 /// use nom::number::complete::le_u128;
438 ///
439 /// let parser = |s| {
440 ///   le_u128(s)
441 /// };
442 ///
443 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
444 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
445 /// ```
446 #[inline]
447 #[cfg(stable_i128)]
le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E>448 pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
449   if i.len() < 16 {
450     Err(Err::Error(make_error(i, ErrorKind::Eof)))
451   } else {
452     let res = ((i[15] as u128) << 120)
453       + ((i[14] as u128) << 112)
454       + ((i[13] as u128) << 104)
455       + ((i[12] as u128) << 96)
456       + ((i[11] as u128) << 88)
457       + ((i[10] as u128) << 80)
458       + ((i[9] as u128) << 72)
459       + ((i[8] as u128) << 64)
460       + ((i[7] as u128) << 56)
461       + ((i[6] as u128) << 48)
462       + ((i[5] as u128) << 40)
463       + ((i[4] as u128) << 32)
464       + ((i[3] as u128) << 24)
465       + ((i[2] as u128) << 16)
466       + ((i[1] as u128) << 8)
467       + i[0] as u128;
468     Ok((&i[16..], res))
469   }
470 }
471 
472 /// Recognizes a signed 1 byte integer
473 ///
474 /// *complete version*: returns an error if there is not enough input data
475 /// ```rust
476 /// # use nom::{Err, error::ErrorKind, Needed};
477 /// # use nom::Needed::Size;
478 /// use nom::number::complete::le_i8;
479 ///
480 /// let parser = |s| {
481 ///   le_i8(s)
482 /// };
483 ///
484 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00)));
485 /// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof))));
486 /// ```
487 #[inline]
le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E>488 pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
489   map!(i, le_u8, |x| x as i8)
490 }
491 
492 /// Recognizes a little endian signed 2 bytes integer
493 ///
494 /// *complete version*: returns an error if there is not enough input data
495 /// ```rust
496 /// # use nom::{Err, error::ErrorKind, Needed};
497 /// # use nom::Needed::Size;
498 /// use nom::number::complete::le_i16;
499 ///
500 /// let parser = |s| {
501 ///   le_i16(s)
502 /// };
503 ///
504 /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300)));
505 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
506 /// ```
507 #[inline]
le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E>508 pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
509   map!(i, le_u16, |x| x as i16)
510 }
511 
512 /// Recognizes a little endian signed 3 bytes integer
513 ///
514 /// *complete version*: returns an error if there is not enough input data
515 /// ```rust
516 /// # use nom::{Err, error::ErrorKind, Needed};
517 /// # use nom::Needed::Size;
518 /// use nom::number::complete::le_i24;
519 ///
520 /// let parser = |s| {
521 ///   le_i24(s)
522 /// };
523 ///
524 /// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300)));
525 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
526 /// ```
527 #[inline]
le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>528 pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
529   // Same as the unsigned version but we need to sign-extend manually here
530   map!(i, le_u24, |x| if x & 0x80_00_00 != 0 {
531     (x | 0xff_00_00_00) as i32
532   } else {
533     x as i32
534   })
535 }
536 
537 /// Recognizes a little endian signed 4 bytes integer
538 ///
539 /// *complete version*: returns an error if there is not enough input data
540 /// ```rust
541 /// # use nom::{Err, error::ErrorKind, Needed};
542 /// # use nom::Needed::Size;
543 /// use nom::number::complete::le_i32;
544 ///
545 /// let parser = |s| {
546 ///   le_i32(s)
547 /// };
548 ///
549 /// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300)));
550 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
551 /// ```
552 #[inline]
le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>553 pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
554   map!(i, le_u32, |x| x as i32)
555 }
556 
557 /// Recognizes a little endian signed 8 bytes integer
558 ///
559 /// *complete version*: returns an error if there is not enough input data
560 /// ```rust
561 /// # use nom::{Err, error::ErrorKind, Needed};
562 /// # use nom::Needed::Size;
563 /// use nom::number::complete::le_i64;
564 ///
565 /// let parser = |s| {
566 ///   le_i64(s)
567 /// };
568 ///
569 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100)));
570 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
571 /// ```
572 #[inline]
le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E>573 pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
574   map!(i, le_u64, |x| x as i64)
575 }
576 
577 /// Recognizes a little endian signed 16 bytes integer
578 ///
579 /// *complete version*: returns an error if there is not enough input data
580 /// ```rust
581 /// # use nom::{Err, error::ErrorKind, Needed};
582 /// # use nom::Needed::Size;
583 /// use nom::number::complete::le_i128;
584 ///
585 /// let parser = |s| {
586 ///   le_i128(s)
587 /// };
588 ///
589 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100)));
590 /// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
591 /// ```
592 #[inline]
593 #[cfg(stable_i128)]
le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E>594 pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
595   map!(i, le_u128, |x| x as i128)
596 }
597 
598 /// Recognizes a big endian 4 bytes floating point number
599 ///
600 /// *complete version*: returns an error if there is not enough input data
601 /// ```rust
602 /// # use nom::{Err, error::ErrorKind, Needed};
603 /// # use nom::Needed::Size;
604 /// use nom::number::complete::be_f32;
605 ///
606 /// let parser = |s| {
607 ///   be_f32(s)
608 /// };
609 ///
610 /// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
611 /// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
612 /// ```
613 #[inline]
be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E>614 pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
615   match be_u32(input) {
616     Err(e) => Err(e),
617     Ok((i, o)) => Ok((i, f32::from_bits(o))),
618   }
619 }
620 
621 /// Recognizes a big endian 8 bytes floating point number
622 ///
623 /// *complete version*: returns an error if there is not enough input data
624 /// ```rust
625 /// # use nom::{Err, error::ErrorKind, Needed};
626 /// # use nom::Needed::Size;
627 /// use nom::number::complete::be_f64;
628 ///
629 /// let parser = |s| {
630 ///   be_f64(s)
631 /// };
632 ///
633 /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
634 /// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
635 /// ```
636 #[inline]
be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E>637 pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
638   match be_u64(input) {
639     Err(e) => Err(e),
640     Ok((i, o)) => Ok((i, f64::from_bits(o))),
641   }
642 }
643 
644 /// Recognizes a little endian 4 bytes floating point number
645 ///
646 /// *complete version*: returns an error if there is not enough input data
647 /// ```rust
648 /// # use nom::{Err, error::ErrorKind, Needed};
649 /// # use nom::Needed::Size;
650 /// use nom::number::complete::le_f32;
651 ///
652 /// let parser = |s| {
653 ///   le_f32(s)
654 /// };
655 ///
656 /// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
657 /// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
658 /// ```
659 #[inline]
le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E>660 pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
661   match le_u32(input) {
662     Err(e) => Err(e),
663     Ok((i, o)) => Ok((i, f32::from_bits(o))),
664   }
665 }
666 
667 /// Recognizes a little endian 8 bytes floating point number
668 ///
669 /// *complete version*: returns an error if there is not enough input data
670 /// ```rust
671 /// # use nom::{Err, error::ErrorKind, Needed};
672 /// # use nom::Needed::Size;
673 /// use nom::number::complete::le_f64;
674 ///
675 /// let parser = |s| {
676 ///   le_f64(s)
677 /// };
678 ///
679 /// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
680 /// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
681 /// ```
682 #[inline]
le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E>683 pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
684   match le_u64(input) {
685     Err(e) => Err(e),
686     Ok((i, o)) => Ok((i, f64::from_bits(o))),
687   }
688 }
689 
690 /// Recognizes a hex-encoded integer
691 ///
692 /// *complete version*: will parse until the end of input if it has less than 8 bytes
693 /// ```rust
694 /// # use nom::{Err, error::ErrorKind, Needed};
695 /// # use nom::Needed::Size;
696 /// use nom::number::complete::hex_u32;
697 ///
698 /// let parser = |s| {
699 ///   hex_u32(s)
700 /// };
701 ///
702 /// assert_eq!(parser(b"01AE"), Ok((&b""[..], 0x01AE)));
703 /// assert_eq!(parser(b"abc"), Ok((&b""[..], 0x0ABC)));
704 /// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
705 /// ```
706 #[inline]
hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E>707 pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> {
708   let (i, o) = crate::bytes::complete::is_a(&b"0123456789abcdefABCDEF"[..])(input)?;
709   // Do not parse more than 8 characters for a u32
710   let (parsed, remaining) = if o.len() <= 8 {
711     (o, i)
712   } else {
713     (&input[..8], &input[8..])
714   };
715 
716   let res = parsed
717     .iter()
718     .rev()
719     .enumerate()
720     .map(|(k, &v)| {
721       let digit = v as char;
722       digit.to_digit(16).unwrap_or(0) << (k * 4)
723     })
724     .sum();
725 
726   Ok((remaining, res))
727 }
728 
729 /// Recognizes floating point number in a byte string and returns the corresponding slice
730 ///
731 /// *complete version*: can parse until the end of input
732 ///
733 /// ```rust
734 /// # use nom::{Err, error::ErrorKind, Needed};
735 /// # use nom::Needed::Size;
736 /// use nom::number::complete::recognize_float;
737 ///
738 /// let parser = |s| {
739 ///   recognize_float(s)
740 /// };
741 ///
742 /// assert_eq!(parser("11e-1"), Ok(("", "11e-1")));
743 /// assert_eq!(parser("123E-02"), Ok(("", "123E-02")));
744 /// assert_eq!(parser("123K-01"), Ok(("K-01", "123")));
745 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
746 /// ```
747 #[allow(unused_imports)]
748 #[cfg_attr(rustfmt, rustfmt_skip)]
recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar,749 pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
750 where
751   T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
752   T: Clone + Offset,
753   T: InputIter,
754   <T as InputIter>::Item: AsChar,
755   T: InputTakeAtPosition,
756   <T as InputTakeAtPosition>::Item: AsChar,
757 {
758   recognize(
759     tuple((
760       opt(alt((char('+'), char('-')))),
761       alt((
762         map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
763         map(tuple((char('.'), digit1)), |_| ())
764       )),
765       opt(tuple((
766         alt((char('e'), char('E'))),
767         opt(alt((char('+'), char('-')))),
768         cut(digit1)
769       )))
770     ))
771   )(input)
772 }
773 
774 /// Recognizes floating point number in a byte string and returns a f32
775 ///
776 /// *complete version*: can parse until the end of input
777 /// ```rust
778 /// # use nom::{Err, error::ErrorKind, Needed};
779 /// # use nom::Needed::Size;
780 /// use nom::number::complete::float;
781 ///
782 /// let parser = |s| {
783 ///   float(s)
784 /// };
785 ///
786 /// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
787 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
788 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
789 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
790 /// ```
791 #[cfg(not(feature = "lexical"))]
float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + crate::traits::ParseTo<f32>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar792 pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
793 where
794   T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
795   T: Clone + Offset,
796   T: InputIter + InputLength + crate::traits::ParseTo<f32>,
797   <T as InputIter>::Item: AsChar,
798   T: InputTakeAtPosition,
799   <T as InputTakeAtPosition>::Item: AsChar
800 {
801   match recognize_float(input) {
802     Err(e) => Err(e),
803     Ok((i, s)) => match s.parse_to() {
804       Some(n) => Ok((i, n)),
805       None =>  Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
806     }
807   }
808 }
809 
810 /// Recognizes floating point number in a byte string and returns a f32
811 ///
812 /// *complete version*: can parse until the end of input
813 ///
814 /// this function uses the lexical-core crate for float parsing by default, you
815 /// can deactivate it by removing the "lexical" feature
816 /// ```rust
817 /// # use nom::{Err, error::ErrorKind, Needed};
818 /// # use nom::Needed::Size;
819 /// use nom::number::complete::float;
820 ///
821 /// let parser = |s| {
822 ///   float(s)
823 /// };
824 ///
825 /// assert_eq!(parser("1.1"), Ok(("", 1.1)));
826 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
827 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
828 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
829 /// ```
830 #[cfg(feature = "lexical")]
float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> where T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,831 pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
832 where
833   T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
834 {
835   match ::lexical_core::parse_partial(input.as_bytes()) {
836     Ok((value, processed)) => Ok((input.slice(processed..), value)),
837     Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
838   }
839 }
840 
841 /// Recognizes floating point number in a byte string and returns a f64
842 ///
843 /// *complete version*: can parse until the end of input
844 /// ```rust
845 /// # use nom::{Err, error::ErrorKind, Needed};
846 /// # use nom::Needed::Size;
847 /// use nom::number::complete::double;
848 ///
849 /// let parser = |s| {
850 ///   double(s)
851 /// };
852 ///
853 /// assert_eq!(parser("11e-1"), Ok(("", 1.1)));
854 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
855 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
856 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
857 /// ```
858 #[cfg(not(feature = "lexical"))]
double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + crate::traits::ParseTo<f64>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar859 pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
860 where
861   T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
862   T: Clone + Offset,
863   T: InputIter + InputLength + crate::traits::ParseTo<f64>,
864   <T as InputIter>::Item: AsChar,
865   T: InputTakeAtPosition,
866   <T as InputTakeAtPosition>::Item: AsChar
867 {
868   match recognize_float(input) {
869     Err(e) => Err(e),
870     Ok((i, s)) => match s.parse_to() {
871       Some(n) => Ok((i, n)),
872       None =>  Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
873     }
874   }
875 }
876 
877 /// Recognizes floating point number in a byte string and returns a f64
878 ///
879 /// *complete version*: can parse until the end of input
880 ///
881 /// this function uses the lexical-core crate for float parsing by default, you
882 /// can deactivate it by removing the "lexical" feature
883 /// ```rust
884 /// # use nom::{Err, error::ErrorKind, Needed};
885 /// # use nom::Needed::Size;
886 /// use nom::number::complete::double;
887 ///
888 /// let parser = |s| {
889 ///   double(s)
890 /// };
891 ///
892 /// assert_eq!(parser("1.1"), Ok(("", 1.1)));
893 /// assert_eq!(parser("123E-02"), Ok(("", 1.23)));
894 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
895 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
896 /// ```
897 #[cfg(feature = "lexical")]
double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> where T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,898 pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
899 where
900   T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
901 {
902   match ::lexical_core::parse_partial(input.as_bytes()) {
903     Ok((value, processed)) => Ok((input.slice(processed..), value)),
904     Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
905   }
906 }
907 
908 #[cfg(test)]
909 mod tests {
910   use super::*;
911   use crate::internal::Err;
912   use crate::error::ErrorKind;
913 
914   macro_rules! assert_parse(
915     ($left: expr, $right: expr) => {
916       let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
917       assert_eq!(res, $right);
918     };
919   );
920 
921   #[test]
i8_tests()922   fn i8_tests() {
923     assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0)));
924     assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127)));
925     assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1)));
926     assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128)));
927   }
928 
929   #[test]
i16_tests()930   fn i16_tests() {
931     assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0)));
932     assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16)));
933     assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1)));
934     assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16)));
935   }
936 
937   #[test]
u24_tests()938   fn u24_tests() {
939     assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
940     assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32)));
941     assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32)));
942   }
943 
944   #[test]
i24_tests()945   fn i24_tests() {
946     assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32)));
947     assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32)));
948     assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32)));
949   }
950 
951   #[test]
i32_tests()952   fn i32_tests() {
953     assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
954     assert_parse!(
955       be_i32(&[0x7f, 0xff, 0xff, 0xff]),
956       Ok((&b""[..], 2_147_483_647_i32))
957     );
958     assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1)));
959     assert_parse!(
960       be_i32(&[0x80, 0x00, 0x00, 0x00]),
961       Ok((&b""[..], -2_147_483_648_i32))
962     );
963   }
964 
965   #[test]
i64_tests()966   fn i64_tests() {
967     assert_parse!(
968       be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
969       Ok((&b""[..], 0))
970     );
971     assert_parse!(
972       be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
973       Ok((&b""[..], 9_223_372_036_854_775_807_i64))
974     );
975     assert_parse!(
976       be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
977       Ok((&b""[..], -1))
978     );
979     assert_parse!(
980       be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
981       Ok((&b""[..], -9_223_372_036_854_775_808_i64))
982     );
983   }
984 
985   #[test]
986   #[cfg(stable_i128)]
i128_tests()987   fn i128_tests() {
988     assert_parse!(
989       be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
990       Ok((&b""[..], 0))
991     );
992     assert_parse!(
993       be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
994       Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128))
995     );
996     assert_parse!(
997       be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
998       Ok((&b""[..], -1))
999     );
1000     assert_parse!(
1001       be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1002       Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128))
1003     );
1004   }
1005 
1006   #[test]
le_i8_tests()1007   fn le_i8_tests() {
1008     assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0)));
1009     assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127)));
1010     assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1)));
1011     assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128)));
1012   }
1013 
1014   #[test]
le_i16_tests()1015   fn le_i16_tests() {
1016     assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0)));
1017     assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16)));
1018     assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1)));
1019     assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
1020   }
1021 
1022   #[test]
le_u24_tests()1023   fn le_u24_tests() {
1024     assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
1025     assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32)));
1026     assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32)));
1027   }
1028 
1029   #[test]
le_i24_tests()1030   fn le_i24_tests() {
1031     assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32)));
1032     assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32)));
1033     assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32)));
1034   }
1035 
1036   #[test]
le_i32_tests()1037   fn le_i32_tests() {
1038     assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
1039     assert_parse!(
1040       le_i32(&[0xff, 0xff, 0xff, 0x7f]),
1041       Ok((&b""[..], 2_147_483_647_i32))
1042     );
1043     assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1)));
1044     assert_parse!(
1045       le_i32(&[0x00, 0x00, 0x00, 0x80]),
1046       Ok((&b""[..], -2_147_483_648_i32))
1047     );
1048   }
1049 
1050   #[test]
le_i64_tests()1051   fn le_i64_tests() {
1052     assert_parse!(
1053       le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1054       Ok((&b""[..], 0))
1055     );
1056     assert_parse!(
1057       le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
1058       Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1059     );
1060     assert_parse!(
1061       le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1062       Ok((&b""[..], -1))
1063     );
1064     assert_parse!(
1065       le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]),
1066       Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1067     );
1068   }
1069 
1070   #[test]
1071   #[cfg(stable_i128)]
le_i128_tests()1072   fn le_i128_tests() {
1073     assert_parse!(
1074       le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1075       Ok((&b""[..], 0))
1076     );
1077     assert_parse!(
1078       le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
1079       Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128))
1080     );
1081     assert_parse!(
1082       le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1083       Ok((&b""[..], -1))
1084     );
1085     assert_parse!(
1086       le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]),
1087       Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128))
1088     );
1089   }
1090 
1091   #[test]
be_f32_tests()1092   fn be_f32_tests() {
1093     assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32)));
1094     assert_parse!(
1095       be_f32(&[0x4d, 0x31, 0x1f, 0xd8]),
1096       Ok((&b""[..], 185_728_392_f32))
1097     );
1098   }
1099 
1100   #[test]
be_f64_tests()1101   fn be_f64_tests() {
1102     assert_parse!(
1103       be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1104       Ok((&b""[..], 0_f64))
1105     );
1106     assert_parse!(
1107       be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]),
1108       Ok((&b""[..], 185_728_392_f64))
1109     );
1110   }
1111 
1112   #[test]
le_f32_tests()1113   fn le_f32_tests() {
1114     assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32)));
1115     assert_parse!(
1116       le_f32(&[0xd8, 0x1f, 0x31, 0x4d]),
1117       Ok((&b""[..], 185_728_392_f32))
1118     );
1119   }
1120 
1121   #[test]
le_f64_tests()1122   fn le_f64_tests() {
1123     assert_parse!(
1124       le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1125       Ok((&b""[..], 0_f64))
1126     );
1127     assert_parse!(
1128       le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]),
1129       Ok((&b""[..], 185_728_392_f64))
1130     );
1131   }
1132 
1133   #[test]
hex_u32_tests()1134   fn hex_u32_tests() {
1135     assert_parse!(
1136       hex_u32(&b";"[..]),
1137       Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
1138     );
1139     assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
1140     assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
1141     assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
1142     assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
1143     assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
1144     assert_parse!(
1145       hex_u32(&b"c5a31be201;"[..]),
1146       Ok((&b"01;"[..], 3_315_801_058))
1147     );
1148     assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
1149     assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
1150     assert_parse!(hex_u32(&b"12af"[..]), Ok((&b""[..], 0x12af)));
1151   }
1152 
1153   #[test]
1154   #[cfg(feature = "std")]
float_test()1155   fn float_test() {
1156     let mut test_cases = vec![
1157       "+3.14",
1158       "3.14",
1159       "-3.14",
1160       "0",
1161       "0.0",
1162       "1.",
1163       ".789",
1164       "-.5",
1165       "1e7",
1166       "-1E-7",
1167       ".3e-2",
1168       "1.e4",
1169       "1.2e4",
1170       "12.34",
1171       "-1.234E-12",
1172       "-1.234e-12",
1173     ];
1174 
1175     for test in test_cases.drain(..) {
1176       let expected32 = str::parse::<f32>(test).unwrap();
1177       let expected64 = str::parse::<f64>(test).unwrap();
1178 
1179       println!("now parsing: {} -> {}", test, expected32);
1180 
1181       let larger = format!("{}", test);
1182       assert_parse!(recognize_float(&larger[..]), Ok(("", test)));
1183 
1184       assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32)));
1185       assert_parse!(float(&larger[..]), Ok(("", expected32)));
1186 
1187       assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64)));
1188       assert_parse!(double(&larger[..]), Ok(("", expected64)));
1189     }
1190 
1191     let remaining_exponent = "-1.234E-";
1192     assert_parse!(
1193       recognize_float(remaining_exponent),
1194       Err(Err::Failure(("", ErrorKind::Digit)))
1195     );
1196   }
1197 
1198 }
1199