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