Coverage Report

Created: 2025-10-14 06:57

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