Coverage Report

Created: 2026-11-12 08:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/nom-8.0.0/src/number/streaming.rs
Line
Count
Source
1
//! Parsers recognizing numbers, streaming version
2
3
use crate::branch::alt;
4
use crate::bytes::streaming::tag;
5
use crate::character::streaming::{char, digit1, sign};
6
use crate::combinator::{cut, map, opt, recognize};
7
use crate::error::{ErrorKind, ParseError};
8
use crate::lib::std::ops::{Add, Shl};
9
use crate::sequence::pair;
10
use crate::traits::{AsBytes, AsChar, Compare, Offset};
11
use 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]
28
0
pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
29
0
where
30
0
  I: Input<Item = u8>,
31
{
32
0
  be_uint(input, 1)
33
0
}
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]
51
0
pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
52
0
where
53
0
  I: Input<Item = u8>,
54
{
55
0
  be_uint(input, 2)
56
0
}
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]
74
0
pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
75
0
where
76
0
  I: Input<Item = u8>,
77
{
78
0
  be_uint(input, 3)
79
0
}
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]
97
0
pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
98
0
where
99
0
  I: Input<Item = u8>,
100
{
101
0
  be_uint(input, 4)
102
0
}
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]
120
0
pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
121
0
where
122
0
  I: Input<Item = u8>,
123
{
124
0
  be_uint(input, 8)
125
0
}
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]
142
0
pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
143
0
where
144
0
  I: Input<Item = u8>,
145
{
146
0
  be_uint(input, 16)
147
0
}
148
149
#[inline]
150
0
fn be_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
151
0
where
152
0
  I: Input<Item = u8>,
153
0
  Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
154
{
155
0
  super::be_uint(bound).parse(input)
156
0
}
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]
171
0
pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
172
0
where
173
0
  I: Input<Item = u8>,
174
{
175
0
  be_u8.map(|x| x as i8).parse(input)
176
0
}
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]
191
0
pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
192
0
where
193
0
  I: Input<Item = u8>,
194
{
195
0
  be_u16.map(|x| x as i16).parse(input)
196
0
}
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]
211
0
pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
212
0
where
213
0
  I: Input<Item = u8>,
214
{
215
  // Same as the unsigned version but we need to sign-extend manually here
216
  be_u24
217
0
    .map(|x| {
218
0
      if x & 0x80_00_00 != 0 {
219
0
        (x | 0xff_00_00_00) as i32
220
      } else {
221
0
        x as i32
222
      }
223
0
    })
224
0
    .parse(input)
225
0
}
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]
240
0
pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
241
0
where
242
0
  I: Input<Item = u8>,
243
{
244
0
  be_u32.map(|x| x as i32).parse(input)
245
0
}
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]
261
0
pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
262
0
where
263
0
  I: Input<Item = u8>,
264
{
265
0
  be_u64.map(|x| x as i64).parse(input)
266
0
}
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]
281
0
pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
282
0
where
283
0
  I: Input<Item = u8>,
284
{
285
0
  be_u128.map(|x| x as i128).parse(input)
286
0
}
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]
301
0
pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
302
0
where
303
0
  I: Input<Item = u8>,
304
{
305
0
  le_uint(input, 1)
306
0
}
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]
324
0
pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E>
325
0
where
326
0
  I: Input<Item = u8>,
327
{
328
0
  le_uint(input, 2)
329
0
}
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]
347
0
pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
348
0
where
349
0
  I: Input<Item = u8>,
350
{
351
0
  le_uint(input, 3)
352
0
}
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]
370
0
pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
371
0
where
372
0
  I: Input<Item = u8>,
373
{
374
0
  le_uint(input, 4)
375
0
}
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]
393
0
pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E>
394
0
where
395
0
  I: Input<Item = u8>,
396
{
397
0
  le_uint(input, 8)
398
0
}
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]
416
0
pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
417
0
where
418
0
  I: Input<Item = u8>,
419
{
420
0
  le_uint(input, 16)
421
0
}
422
423
#[inline]
424
0
fn le_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E>
425
0
where
426
0
  I: Input<Item = u8>,
427
0
  Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>,
428
{
429
0
  super::le_uint(bound).parse(input)
430
0
}
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]
445
0
pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E>
446
0
where
447
0
  I: Input<Item = u8>,
448
{
449
0
  le_u8.map(|x| x as i8).parse(input)
450
0
}
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]
468
0
pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E>
469
0
where
470
0
  I: Input<Item = u8>,
471
{
472
0
  le_u16.map(|x| x as i16).parse(input)
473
0
}
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]
491
0
pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
492
0
where
493
0
  I: Input<Item = u8>,
494
{
495
  // Same as the unsigned version but we need to sign-extend manually here
496
  le_u24
497
0
    .map(|x| {
498
0
      if x & 0x80_00_00 != 0 {
499
0
        (x | 0xff_00_00_00) as i32
500
      } else {
501
0
        x as i32
502
      }
503
0
    })
504
0
    .parse(input)
505
0
}
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]
523
0
pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E>
524
0
where
525
0
  I: Input<Item = u8>,
526
{
527
0
  le_u32.map(|x| x as i32).parse(input)
528
0
}
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]
546
0
pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E>
547
0
where
548
0
  I: Input<Item = u8>,
549
{
550
0
  le_u64.map(|x| x as i64).parse(input)
551
0
}
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]
569
0
pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
570
0
where
571
0
  I: Input<Item = u8>,
572
{
573
0
  le_u128.map(|x| x as i128).parse(input)
574
0
}
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]
593
0
pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E>
594
0
where
595
0
  I: Input<Item = u8>,
596
{
597
0
  super::u8().parse(input)
598
0
}
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]
626
0
pub fn u16<I, E: ParseError<I>>(
627
0
  endian: crate::number::Endianness,
628
0
) -> impl Fn(I) -> IResult<I, u16, E>
629
0
where
630
0
  I: Input<Item = u8>,
631
{
632
0
  move |input| super::u16(endian).parse(input)
633
0
}
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]
660
0
pub fn u24<I, E: ParseError<I>>(
661
0
  endian: crate::number::Endianness,
662
0
) -> impl Fn(I) -> IResult<I, u32, E>
663
0
where
664
0
  I: Input<Item = u8>,
665
{
666
0
  move |input| super::u24(endian).parse(input)
667
0
}
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]
694
0
pub fn u32<I, E: ParseError<I>>(
695
0
  endian: crate::number::Endianness,
696
0
) -> impl Fn(I) -> IResult<I, u32, E>
697
0
where
698
0
  I: Input<Item = u8>,
699
{
700
0
  move |input| super::u32(endian).parse(input)
701
0
}
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]
728
0
pub fn u64<I, E: ParseError<I>>(
729
0
  endian: crate::number::Endianness,
730
0
) -> impl Fn(I) -> IResult<I, u64, E>
731
0
where
732
0
  I: Input<Item = u8>,
733
{
734
0
  move |input| super::u64(endian).parse(input)
735
0
}
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]
762
0
pub fn u128<I, E: ParseError<I>>(
763
0
  endian: crate::number::Endianness,
764
0
) -> impl Fn(I) -> IResult<I, u128, E>
765
0
where
766
0
  I: Input<Item = u8>,
767
{
768
0
  move |input| super::u128(endian).parse(input)
769
0
}
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]
788
0
pub fn i8<I, E: ParseError<I>>(i: I) -> IResult<I, i8, E>
789
0
where
790
0
  I: Input<Item = u8>,
791
{
792
0
  super::u8().map(|x| x as i8).parse(i)
793
0
}
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]
820
0
pub fn i16<I, E: ParseError<I>>(
821
0
  endian: crate::number::Endianness,
822
0
) -> impl Fn(I) -> IResult<I, i16, E>
823
0
where
824
0
  I: Input<Item = u8>,
825
{
826
0
  move |input| super::i16(endian).parse(input)
827
0
}
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]
854
0
pub fn i24<I, E: ParseError<I>>(
855
0
  endian: crate::number::Endianness,
856
0
) -> impl Fn(I) -> IResult<I, i32, E>
857
0
where
858
0
  I: Input<Item = u8>,
859
{
860
0
  move |input| super::i24(endian).parse(input)
861
0
}
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]
888
0
pub fn i32<I, E: ParseError<I>>(
889
0
  endian: crate::number::Endianness,
890
0
) -> impl Fn(I) -> IResult<I, i32, E>
891
0
where
892
0
  I: Input<Item = u8>,
893
{
894
0
  move |input| super::i32(endian).parse(input)
895
0
}
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]
922
0
pub fn i64<I, E: ParseError<I>>(
923
0
  endian: crate::number::Endianness,
924
0
) -> impl Fn(I) -> IResult<I, i64, E>
925
0
where
926
0
  I: Input<Item = u8>,
927
{
928
0
  move |input| super::i64(endian).parse(input)
929
0
}
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]
956
0
pub fn i128<I, E: ParseError<I>>(
957
0
  endian: crate::number::Endianness,
958
0
) -> impl Fn(I) -> IResult<I, i128, E>
959
0
where
960
0
  I: Input<Item = u8>,
961
{
962
0
  move |input| super::i128(endian).parse(input)
963
0
}
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]
980
0
pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
981
0
where
982
0
  I: Input<Item = u8>,
983
{
984
0
  match be_u32(input) {
985
0
    Err(e) => Err(e),
986
0
    Ok((i, o)) => Ok((i, f32::from_bits(o))),
987
  }
988
0
}
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]
1005
0
pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1006
0
where
1007
0
  I: Input<Item = u8>,
1008
{
1009
0
  match be_u64(input) {
1010
0
    Err(e) => Err(e),
1011
0
    Ok((i, o)) => Ok((i, f64::from_bits(o))),
1012
  }
1013
0
}
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]
1030
0
pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E>
1031
0
where
1032
0
  I: Input<Item = u8>,
1033
{
1034
0
  match le_u32(input) {
1035
0
    Err(e) => Err(e),
1036
0
    Ok((i, o)) => Ok((i, f32::from_bits(o))),
1037
  }
1038
0
}
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]
1055
0
pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E>
1056
0
where
1057
0
  I: Input<Item = u8>,
1058
{
1059
0
  match le_u64(input) {
1060
0
    Err(e) => Err(e),
1061
0
    Ok((i, o)) => Ok((i, f64::from_bits(o))),
1062
  }
1063
0
}
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]
1090
0
pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f32, E>
1091
0
where
1092
0
  I: Input<Item = u8>,
1093
{
1094
0
  match endian {
1095
0
    crate::number::Endianness::Big => be_f32,
1096
0
    crate::number::Endianness::Little => le_f32,
1097
    #[cfg(target_endian = "big")]
1098
    crate::number::Endianness::Native => be_f32,
1099
    #[cfg(target_endian = "little")]
1100
0
    crate::number::Endianness::Native => le_f32,
1101
  }
1102
0
}
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]
1129
0
pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, f64, E>
1130
0
where
1131
0
  I: Input<Item = u8>,
1132
{
1133
0
  match endian {
1134
0
    crate::number::Endianness::Big => be_f64,
1135
0
    crate::number::Endianness::Little => le_f64,
1136
    #[cfg(target_endian = "big")]
1137
    crate::number::Endianness::Native => be_f64,
1138
    #[cfg(target_endian = "little")]
1139
0
    crate::number::Endianness::Native => le_f64,
1140
  }
1141
0
}
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]
1159
0
pub fn hex_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E>
1160
0
where
1161
0
  I: Input + AsBytes,
1162
0
  <I as Input>::Item: AsChar,
1163
{
1164
0
  let e: ErrorKind = ErrorKind::IsA;
1165
0
  let (i, o) = input.split_at_position1(
1166
0
    |c| {
1167
0
      let c = c.as_char();
1168
0
      !"0123456789abcdefABCDEF".contains(c)
1169
0
    },
1170
0
    e,
1171
0
  )?;
1172
1173
  // Do not parse more than 8 characters for a u32
1174
0
  let (remaining, parsed) = if o.input_len() <= 8 {
1175
0
    (i, o)
1176
  } else {
1177
0
    input.take_split(8)
1178
  };
1179
1180
0
  let res = parsed
1181
0
    .as_bytes()
1182
0
    .iter()
1183
0
    .rev()
1184
0
    .enumerate()
1185
0
    .map(|(k, &v)| {
1186
0
      let digit = v as char;
1187
0
      digit.to_digit(16).unwrap_or(0) << (k * 4)
1188
0
    })
1189
0
    .sum();
1190
1191
0
  Ok((remaining, res))
1192
0
}
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]
1212
0
pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
1213
0
where
1214
0
  T: Clone + Offset,
1215
0
  T: Input,
1216
0
  <T as Input>::Item: AsChar,
1217
{
1218
0
  recognize((
1219
0
      opt(alt((char('+'), char('-')))),
1220
0
      alt((
1221
0
        map((digit1, opt(pair(char('.'), opt(digit1)))), |_| ()),
1222
0
        map((char('.'), digit1), |_| ())
1223
      )),
1224
0
      opt((
1225
0
        alt((char('e'), char('E'))),
1226
0
        opt(alt((char('+'), char('-')))),
1227
0
        cut(digit1)
1228
0
      ))
1229
0
  )).parse(input)
1230
0
}
1231
1232
// workaround until issues with minimal-lexical are fixed
1233
#[doc(hidden)]
1234
0
pub fn recognize_float_or_exceptions<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
1235
0
where
1236
0
  T: Clone + Offset,
1237
0
  T: Input + Compare<&'static str>,
1238
0
  <T as Input>::Item: AsChar,
1239
{
1240
0
  alt((
1241
0
    |i: T| {
1242
0
      recognize_float::<_, E>(i.clone()).map_err(|e| match e {
1243
0
        crate::Err::Error(_) => crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)),
1244
0
        crate::Err::Failure(_) => crate::Err::Failure(E::from_error_kind(i, ErrorKind::Float)),
1245
0
        crate::Err::Incomplete(needed) => crate::Err::Incomplete(needed),
1246
0
      })
1247
0
    },
1248
0
    |i: T| {
1249
0
      crate::bytes::streaming::tag_no_case::<_, _, E>("nan")(i.clone())
1250
0
        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1251
0
    },
1252
0
    |i: T| {
1253
0
      crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
1254
0
        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1255
0
    },
1256
0
    |i: T| {
1257
0
      crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
1258
0
        .map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
1259
0
    },
1260
  ))
1261
0
  .parse(input)
1262
0
}
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
///
1271
0
pub fn recognize_float_parts<T, E: ParseError<T>>(input: T) -> IResult<T, (bool, T, T, i32), E>
1272
0
where
1273
0
  T: Clone + Offset,
1274
0
  T: Input,
1275
0
  <T as Input>::Item: AsChar,
1276
0
  T: for<'a> Compare<&'a [u8]>,
1277
0
  T: AsBytes,
1278
{
1279
0
  let (i, sign) = sign(input.clone())?;
1280
1281
  //let (i, zeroes) = take_while(|c: <T as InputTakeAtPosition>::Item| c.as_char() == '0')(i)?;
1282
0
  let (i, zeroes) = match i.as_bytes().iter().position(|c| *c != b'0') {
1283
0
    Some(index) => i.take_split(index),
1284
0
    None => i.take_split(i.input_len()),
1285
  };
1286
1287
  //let (i, mut integer) = digit0(i)?;
1288
0
  let (i, mut integer) = match i
1289
0
    .as_bytes()
1290
0
    .iter()
1291
0
    .position(|c| !(*c >= b'0' && *c <= b'9'))
1292
  {
1293
0
    Some(index) => i.take_split(index),
1294
0
    None => i.take_split(i.input_len()),
1295
  };
1296
1297
0
  if integer.input_len() == 0 && zeroes.input_len() > 0 {
1298
0
    // keep the last zero if integer is empty
1299
0
    integer = zeroes.take_from(zeroes.input_len() - 1);
1300
0
  }
1301
1302
0
  let (i, opt_dot) = opt(tag(&b"."[..])).parse(i)?;
1303
0
  let (i, fraction) = if opt_dot.is_none() {
1304
0
    let i2 = i.clone();
1305
0
    (i2, i.take(0))
1306
  } else {
1307
    // match number, trim right zeroes
1308
0
    let mut zero_count = 0usize;
1309
0
    let mut position = None;
1310
0
    for (pos, c) in i.as_bytes().iter().enumerate() {
1311
0
      if *c >= b'0' && *c <= b'9' {
1312
0
        if *c == b'0' {
1313
0
          zero_count += 1;
1314
0
        } else {
1315
0
          zero_count = 0;
1316
0
        }
1317
      } else {
1318
0
        position = Some(pos);
1319
0
        break;
1320
      }
1321
    }
1322
1323
0
    let position = match position {
1324
0
      Some(p) => p,
1325
0
      None => return Err(Err::Incomplete(Needed::new(1))),
1326
    };
1327
1328
0
    let index = if zero_count == 0 {
1329
0
      position
1330
0
    } else if zero_count == position {
1331
0
      position - zero_count + 1
1332
    } else {
1333
0
      position - zero_count
1334
    };
1335
1336
0
    (i.take_from(position), i.take(index))
1337
  };
1338
1339
0
  if integer.input_len() == 0 && fraction.input_len() == 0 {
1340
0
    return Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)));
1341
0
  }
1342
1343
0
  let i2 = i.clone();
1344
0
  let (i, e) = match i.as_bytes().iter().next() {
1345
0
    Some(b'e') => (i.take_from(1), true),
1346
0
    Some(b'E') => (i.take_from(1), true),
1347
0
    _ => (i, false),
1348
  };
1349
1350
0
  let (i, exp) = if e {
1351
0
    cut(crate::character::streaming::i32).parse(i)?
1352
  } else {
1353
0
    (i2, 0)
1354
  };
1355
1356
0
  Ok((i, (sign, integer, fraction, exp)))
1357
0
}
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
/// ```
1377
0
pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
1378
0
where
1379
0
  T: Clone + Offset,
1380
0
  T: Input + crate::traits::ParseTo<f32> + Compare<&'static str>,
1381
0
  <T as Input>::Item: AsChar + Clone,
1382
0
  T: AsBytes,
1383
0
  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
0
  let (i, s) = recognize_float_or_exceptions(input)?;
1400
0
  match s.parse_to() {
1401
0
    Some(f) => Ok((i, f)),
1402
0
    None => Err(crate::Err::Error(E::from_error_kind(
1403
0
      i,
1404
0
      crate::error::ErrorKind::Float,
1405
0
    ))),
1406
  }
1407
0
}
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
/// ```
1427
0
pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
1428
0
where
1429
0
  T: Clone + Offset,
1430
0
  T: Input + crate::traits::ParseTo<f64> + Compare<&'static str>,
1431
0
  <T as Input>::Item: AsChar + Clone,
1432
0
  T: AsBytes,
1433
0
  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
0
  let (i, s) = recognize_float_or_exceptions(input)?;
1450
0
  match s.parse_to() {
1451
0
    Some(f) => Ok((i, f)),
1452
0
    None => Err(crate::Err::Error(E::from_error_kind(
1453
0
      i,
1454
0
      crate::error::ErrorKind::Float,
1455
0
    ))),
1456
  }
1457
0
}
1458
1459
#[cfg(test)]
1460
mod 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
}