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