Coverage Report

Created: 2026-05-16 06:08

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