1 //! parsers recognizing numbers, streaming version
2
3 use crate::internal::*;
4 use crate::error::{ErrorKind, ParseError};
5 use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition};
6 use crate::lib::std::ops::{RangeFrom, RangeTo};
7 use crate::traits::{Offset, Slice};
8 use crate::character::streaming::{char, digit1};
9 use crate::sequence::{pair, tuple};
10 use crate::combinator::{cut, map, opt, recognize};
11 use crate::branch::alt;
12
13 /// Recognizes an unsigned 1 byte integer
14 ///
15 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
16 /// ```rust
17 /// # use nom::{Err, error::ErrorKind, Needed};
18 /// # use nom::Needed::Size;
19 /// use nom::number::streaming::be_u8;
20 ///
21 /// let parser = be_u8::<(_, ErrorKind)>;
22 ///
23 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
24 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
25 /// ```
26 #[inline]
be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E>27 pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
28 if i.len() < 1 {
29 Err(Err::Incomplete(Needed::Size(1)))
30 } else {
31 Ok((&i[1..], i[0]))
32 }
33 }
34
35 /// Recognizes a big endian unsigned 2 bytes integer
36 ///
37 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
38 ///
39 /// ```rust
40 /// # use nom::{Err, error::ErrorKind, Needed};
41 /// # use nom::Needed::Size;
42 /// use nom::number::streaming::be_u16;
43 ///
44 /// let parser = |s| {
45 /// be_u16::<(_, ErrorKind)>(s)
46 /// };
47 ///
48 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001)));
49 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
50 /// ```
51 #[inline]
be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E>52 pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
53 if i.len() < 2 {
54 Err(Err::Incomplete(Needed::Size(2)))
55 } else {
56 let res = ((i[0] as u16) << 8) + i[1] as u16;
57 Ok((&i[2..], res))
58 }
59 }
60
61 /// Recognizes a big endian unsigned 3 byte integer
62 ///
63 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
64 ///
65 /// ```rust
66 /// # use nom::{Err, error::ErrorKind, Needed};
67 /// # use nom::Needed::Size;
68 /// use nom::number::streaming::be_u24;
69 ///
70 /// let parser = |s| {
71 /// be_u24::<(_, ErrorKind)>(s)
72 /// };
73 ///
74 /// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102)));
75 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
76 /// ```
77 #[inline]
be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>78 pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
79 if i.len() < 3 {
80 Err(Err::Incomplete(Needed::Size(3)))
81 } else {
82 let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32);
83 Ok((&i[3..], res))
84 }
85 }
86
87 /// Recognizes a big endian unsigned 4 bytes integer
88 ///
89 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
90 ///
91 /// ```rust
92 /// # use nom::{Err, error::ErrorKind, Needed};
93 /// # use nom::Needed::Size;
94 /// use nom::number::streaming::be_u32;
95 ///
96 /// let parser = |s| {
97 /// be_u32::<(_, ErrorKind)>(s)
98 /// };
99 ///
100 /// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203)));
101 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
102 /// ```
103 #[inline]
be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>104 pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
105 if i.len() < 4 {
106 Err(Err::Incomplete(Needed::Size(4)))
107 } else {
108 let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32;
109 Ok((&i[4..], res))
110 }
111 }
112
113 /// Recognizes a big endian unsigned 8 bytes integer
114 ///
115 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
116 ///
117 /// ```rust
118 /// # use nom::{Err, error::ErrorKind, Needed};
119 /// # use nom::Needed::Size;
120 /// use nom::number::streaming::be_u64;
121 ///
122 /// let parser = |s| {
123 /// be_u64::<(_, ErrorKind)>(s)
124 /// };
125 ///
126 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607)));
127 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
128 /// ```
129 #[inline]
be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E>130 pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
131 if i.len() < 8 {
132 Err(Err::Incomplete(Needed::Size(8)))
133 } else {
134 let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24)
135 + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64;
136 Ok((&i[8..], res))
137 }
138 }
139
140 /// Recognizes a big endian unsigned 16 bytes integer
141 ///
142 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
143 /// ```rust
144 /// # use nom::{Err, error::ErrorKind, Needed};
145 /// # use nom::Needed::Size;
146 /// use nom::number::streaming::be_u128;
147 ///
148 /// let parser = |s| {
149 /// be_u128::<(_, ErrorKind)>(s)
150 /// };
151 ///
152 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
153 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
154 /// ```
155 #[inline]
156 #[cfg(stable_i128)]
be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E>157 pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
158 if i.len() < 16 {
159 Err(Err::Incomplete(Needed::Size(16)))
160 } else {
161 let res = ((i[0] as u128) << 120)
162 + ((i[1] as u128) << 112)
163 + ((i[2] as u128) << 104)
164 + ((i[3] as u128) << 96)
165 + ((i[4] as u128) << 88)
166 + ((i[5] as u128) << 80)
167 + ((i[6] as u128) << 72)
168 + ((i[7] as u128) << 64)
169 + ((i[8] as u128) << 56)
170 + ((i[9] as u128) << 48)
171 + ((i[10] as u128) << 40)
172 + ((i[11] as u128) << 32)
173 + ((i[12] as u128) << 24)
174 + ((i[13] as u128) << 16)
175 + ((i[14] as u128) << 8)
176 + i[15] as u128;
177 Ok((&i[16..], res))
178 }
179 }
180
181 /// Recognizes a signed 1 byte integer
182 ///
183 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
184 /// ```rust
185 /// # use nom::{Err, error::ErrorKind, Needed};
186 /// # use nom::Needed::Size;
187 /// use nom::number::streaming::be_i8;
188 ///
189 /// let parser = be_i8::<(_, ErrorKind)>;
190 ///
191 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
192 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
193 /// ```
194 #[inline]
be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E>195 pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
196 map!(i, be_u8, |x| x as i8)
197 }
198
199 /// Recognizes a big endian signed 2 bytes integer
200 ///
201 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
202 /// ```rust
203 /// # use nom::{Err, error::ErrorKind, Needed};
204 /// # use nom::Needed::Size;
205 /// use nom::number::streaming::be_i16;
206 ///
207 /// let parser = be_i16::<(_, ErrorKind)>;
208 ///
209 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001)));
210 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(2))));
211 /// ```
212 #[inline]
be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E>213 pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
214 map!(i, be_u16, |x| x as i16)
215 }
216
217 /// Recognizes a big endian signed 3 bytes integer
218 ///
219 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
220 /// ```rust
221 /// # use nom::{Err, error::ErrorKind, Needed};
222 /// # use nom::Needed::Size;
223 /// use nom::number::streaming::be_i24;
224 ///
225 /// let parser = be_i24::<(_, ErrorKind)>;
226 ///
227 /// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102)));
228 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(3))));
229 /// ```
230 #[inline]
be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>231 pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
232 // Same as the unsigned version but we need to sign-extend manually here
233 map!(i, be_u24, |x| if x & 0x80_00_00 != 0 {
234 (x | 0xff_00_00_00) as i32
235 } else {
236 x as i32
237 })
238 }
239
240 /// Recognizes a big endian signed 4 bytes integer
241 ///
242 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
243 /// ```rust
244 /// # use nom::{Err, error::ErrorKind, Needed};
245 /// # use nom::Needed::Size;
246 /// use nom::number::streaming::be_i32;
247 ///
248 /// let parser = be_i32::<(_, ErrorKind)>;
249 ///
250 /// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203)));
251 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(4))));
252 /// ```
253 #[inline]
be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>254 pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
255 map!(i, be_u32, |x| x as i32)
256 }
257
258 /// Recognizes a big endian signed 8 bytes integer
259 ///
260 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
261 ///
262 /// ```rust
263 /// # use nom::{Err, error::ErrorKind, Needed};
264 /// # use nom::Needed::Size;
265 /// use nom::number::streaming::be_i64;
266 ///
267 /// let parser = be_i64::<(_, ErrorKind)>;
268 ///
269 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607)));
270 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
271 /// ```
272 #[inline]
be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E>273 pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
274 map!(i, be_u64, |x| x as i64)
275 }
276
277 /// Recognizes a big endian signed 16 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::Needed::Size;
283 /// use nom::number::streaming::be_i128;
284 ///
285 /// let parser = be_i128::<(_, ErrorKind)>;
286 ///
287 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415)));
288 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
289 /// ```
290 #[inline]
291 #[cfg(stable_i128)]
be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E>292 pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
293 map!(i, be_u128, |x| x as i128)
294 }
295
296 /// Recognizes an unsigned 1 byte integer
297 ///
298 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
299 /// ```rust
300 /// # use nom::{Err, error::ErrorKind, Needed};
301 /// # use nom::Needed::Size;
302 /// use nom::number::streaming::le_u8;
303 ///
304 /// let parser = le_u8::<(_, ErrorKind)>;
305 ///
306 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
307 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
308 /// ```
309 #[inline]
le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E>310 pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> {
311 if i.len() < 1 {
312 Err(Err::Incomplete(Needed::Size(1)))
313 } else {
314 Ok((&i[1..], i[0]))
315 }
316 }
317
318 /// Recognizes a little endian unsigned 2 bytes integer
319 ///
320 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
321 ///
322 /// ```rust
323 /// # use nom::{Err, error::ErrorKind, Needed};
324 /// # use nom::Needed::Size;
325 /// use nom::number::streaming::le_u16;
326 ///
327 /// let parser = |s| {
328 /// le_u16::<(_, ErrorKind)>(s)
329 /// };
330 ///
331 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100)));
332 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
333 /// ```
334 #[inline]
le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E>335 pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> {
336 if i.len() < 2 {
337 Err(Err::Incomplete(Needed::Size(2)))
338 } else {
339 let res = ((i[1] as u16) << 8) + i[0] as u16;
340 Ok((&i[2..], res))
341 }
342 }
343
344 /// Recognizes a little endian unsigned 3 bytes integer
345 ///
346 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
347 ///
348 /// ```rust
349 /// # use nom::{Err, error::ErrorKind, Needed};
350 /// # use nom::Needed::Size;
351 /// use nom::number::streaming::le_u24;
352 ///
353 /// let parser = |s| {
354 /// le_u24::<(_, ErrorKind)>(s)
355 /// };
356 ///
357 /// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100)));
358 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
359 /// ```
360 #[inline]
le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>361 pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
362 if i.len() < 3 {
363 Err(Err::Incomplete(Needed::Size(3)))
364 } else {
365 let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16);
366 Ok((&i[3..], res))
367 }
368 }
369
370 /// Recognizes a little endian unsigned 4 bytes integer
371 ///
372 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
373 ///
374 /// ```rust
375 /// # use nom::{Err, error::ErrorKind, Needed};
376 /// # use nom::Needed::Size;
377 /// use nom::number::streaming::le_u32;
378 ///
379 /// let parser = |s| {
380 /// le_u32::<(_, ErrorKind)>(s)
381 /// };
382 ///
383 /// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100)));
384 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
385 /// ```
386 #[inline]
le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E>387 pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> {
388 if i.len() < 4 {
389 Err(Err::Incomplete(Needed::Size(4)))
390 } else {
391 let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32;
392 Ok((&i[4..], res))
393 }
394 }
395
396 /// Recognizes a little endian unsigned 8 bytes integer
397 ///
398 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
399 ///
400 /// ```rust
401 /// # use nom::{Err, error::ErrorKind, Needed};
402 /// # use nom::Needed::Size;
403 /// use nom::number::streaming::le_u64;
404 ///
405 /// let parser = |s| {
406 /// le_u64::<(_, ErrorKind)>(s)
407 /// };
408 ///
409 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100)));
410 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
411 /// ```
412 #[inline]
le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E>413 pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> {
414 if i.len() < 8 {
415 Err(Err::Incomplete(Needed::Size(8)))
416 } else {
417 let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24)
418 + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64;
419 Ok((&i[8..], res))
420 }
421 }
422
423 /// Recognizes a little endian unsigned 16 bytes integer
424 ///
425 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
426 ///
427 /// ```rust
428 /// # use nom::{Err, error::ErrorKind, Needed};
429 /// # use nom::Needed::Size;
430 /// use nom::number::streaming::le_u128;
431 ///
432 /// let parser = |s| {
433 /// le_u128::<(_, ErrorKind)>(s)
434 /// };
435 ///
436 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
437 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
438 /// ```
439 #[inline]
440 #[cfg(stable_i128)]
le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E>441 pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> {
442 if i.len() < 16 {
443 Err(Err::Incomplete(Needed::Size(16)))
444 } else {
445 let res = ((i[15] as u128) << 120)
446 + ((i[14] as u128) << 112)
447 + ((i[13] as u128) << 104)
448 + ((i[12] as u128) << 96)
449 + ((i[11] as u128) << 88)
450 + ((i[10] as u128) << 80)
451 + ((i[9] as u128) << 72)
452 + ((i[8] as u128) << 64)
453 + ((i[7] as u128) << 56)
454 + ((i[6] as u128) << 48)
455 + ((i[5] as u128) << 40)
456 + ((i[4] as u128) << 32)
457 + ((i[3] as u128) << 24)
458 + ((i[2] as u128) << 16)
459 + ((i[1] as u128) << 8)
460 + i[0] as u128;
461 Ok((&i[16..], res))
462 }
463 }
464
465 /// Recognizes a signed 1 byte integer
466 ///
467 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
468 /// ```rust
469 /// # use nom::{Err, error::ErrorKind, Needed};
470 /// # use nom::Needed::Size;
471 /// use nom::number::streaming::le_i8;
472 ///
473 /// let parser = le_i8::<(_, ErrorKind)>;
474 ///
475 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00)));
476 /// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1))));
477 /// ```
478 #[inline]
le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E>479 pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> {
480 map!(i, le_u8, |x| x as i8)
481 }
482
483 /// Recognizes a little endian signed 2 bytes integer
484 ///
485 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
486 ///
487 /// ```rust
488 /// # use nom::{Err, error::ErrorKind, Needed};
489 /// # use nom::Needed::Size;
490 /// use nom::number::streaming::le_i16;
491 ///
492 /// let parser = |s| {
493 /// le_i16::<(_, ErrorKind)>(s)
494 /// };
495 ///
496 /// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100)));
497 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2))));
498 /// ```
499 #[inline]
le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E>500 pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> {
501 map!(i, le_u16, |x| x as i16)
502 }
503
504 /// Recognizes a little endian signed 3 bytes integer
505 ///
506 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
507 ///
508 /// ```rust
509 /// # use nom::{Err, error::ErrorKind, Needed};
510 /// # use nom::Needed::Size;
511 /// use nom::number::streaming::le_i24;
512 ///
513 /// let parser = |s| {
514 /// le_i24::<(_, ErrorKind)>(s)
515 /// };
516 ///
517 /// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100)));
518 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3))));
519 /// ```
520 #[inline]
le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>521 pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
522 // Same as the unsigned version but we need to sign-extend manually here
523 map!(i, le_u24, |x| if x & 0x80_00_00 != 0 {
524 (x | 0xff_00_00_00) as i32
525 } else {
526 x as i32
527 })
528 }
529
530 /// Recognizes a little endian signed 4 bytes integer
531 ///
532 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
533 ///
534 /// ```rust
535 /// # use nom::{Err, error::ErrorKind, Needed};
536 /// # use nom::Needed::Size;
537 /// use nom::number::streaming::le_i32;
538 ///
539 /// let parser = |s| {
540 /// le_i32::<(_, ErrorKind)>(s)
541 /// };
542 ///
543 /// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100)));
544 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4))));
545 /// ```
546 #[inline]
le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E>547 pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> {
548 map!(i, le_u32, |x| x as i32)
549 }
550
551 /// Recognizes a little endian signed 8 bytes integer
552 ///
553 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
554 ///
555 /// ```rust
556 /// # use nom::{Err, error::ErrorKind, Needed};
557 /// # use nom::Needed::Size;
558 /// use nom::number::streaming::le_i64;
559 ///
560 /// let parser = |s| {
561 /// le_i64::<(_, ErrorKind)>(s)
562 /// };
563 ///
564 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100)));
565 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8))));
566 /// ```
567 #[inline]
le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E>568 pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> {
569 map!(i, le_u64, |x| x as i64)
570 }
571
572 /// Recognizes a little endian signed 16 bytes integer
573 ///
574 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
575 ///
576 /// ```rust
577 /// # use nom::{Err, error::ErrorKind, Needed};
578 /// # use nom::Needed::Size;
579 /// use nom::number::streaming::le_i128;
580 ///
581 /// let parser = |s| {
582 /// le_i128::<(_, ErrorKind)>(s)
583 /// };
584 ///
585 /// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100)));
586 /// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16))));
587 /// ```
588 #[inline]
589 #[cfg(stable_i128)]
le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E>590 pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> {
591 map!(i, le_u128, |x| x as i128)
592 }
593
594 /// Recognizes a big endian 4 bytes floating point number
595 ///
596 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
597 /// ```rust
598 /// # use nom::{Err, error::ErrorKind, Needed};
599 /// # use nom::Needed::Size;
600 /// use nom::number::streaming::be_f32;
601 ///
602 /// let parser = |s| {
603 /// be_f32::<(_, ErrorKind)>(s)
604 /// };
605 ///
606 /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625)));
607 /// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4))));
608 /// ```
609 #[inline]
be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E>610 pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
611 match be_u32(input) {
612 Err(e) => Err(e),
613 Ok((i, o)) => Ok((i, f32::from_bits(o))),
614 }
615 }
616
617 /// Recognizes a big endian 8 bytes floating point number
618 ///
619 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
620 /// ```rust
621 /// # use nom::{Err, error::ErrorKind, Needed};
622 /// # use nom::Needed::Size;
623 /// use nom::number::streaming::be_f64;
624 ///
625 /// let parser = |s| {
626 /// be_f64::<(_, ErrorKind)>(s)
627 /// };
628 ///
629 /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
630 /// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8))));
631 /// ```
632 #[inline]
be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E>633 pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
634 match be_u64(input) {
635 Err(e) => Err(e),
636 Ok((i, o)) => Ok((i, f64::from_bits(o))),
637 }
638 }
639
640 /// Recognizes a little endian 4 bytes floating point number
641 ///
642 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
643 /// ```rust
644 /// # use nom::{Err, error::ErrorKind, Needed};
645 /// # use nom::Needed::Size;
646 /// use nom::number::streaming::le_f32;
647 ///
648 /// let parser = |s| {
649 /// le_f32::<(_, ErrorKind)>(s)
650 /// };
651 ///
652 /// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
653 /// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4))));
654 /// ```
655 #[inline]
le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E>656 pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> {
657 match le_u32(input) {
658 Err(e) => Err(e),
659 Ok((i, o)) => Ok((i, f32::from_bits(o))),
660 }
661 }
662
663 /// Recognizes a little endian 8 bytes floating point number
664 ///
665 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
666 /// ```rust
667 /// # use nom::{Err, error::ErrorKind, Needed};
668 /// # use nom::Needed::Size;
669 /// use nom::number::streaming::le_f64;
670 ///
671 /// let parser = |s| {
672 /// le_f64::<(_, ErrorKind)>(s)
673 /// };
674 ///
675 /// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0)));
676 /// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8))));
677 /// ```
678 #[inline]
le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E>679 pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> {
680 match le_u64(input) {
681 Err(e) => Err(e),
682 Ok((i, o)) => Ok((i, f64::from_bits(o))),
683 }
684 }
685
686 /// Recognizes a hex-encoded integer
687 ///
688 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data
689 /// ```rust
690 /// # use nom::{Err, error::ErrorKind, Needed};
691 /// # use nom::Needed::Size;
692 /// use nom::number::streaming::hex_u32;
693 ///
694 /// let parser = |s| {
695 /// hex_u32(s)
696 /// };
697 ///
698 /// assert_eq!(parser(b"01AE;"), Ok((&b";"[..], 0x01AE)));
699 /// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::Size(1))));
700 /// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA))));
701 /// ```
702 #[inline]
hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E>703 pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> {
704 let (i, o) = crate::bytes::streaming::is_a(&b"0123456789abcdefABCDEF"[..])(input)?;
705
706 // Do not parse more than 8 characters for a u32
707 let (parsed, remaining) = if o.len() <= 8 {
708 (o, i)
709 } else {
710 (&input[..8], &input[8..])
711 };
712
713 let res = parsed
714 .iter()
715 .rev()
716 .enumerate()
717 .map(|(k, &v)| {
718 let digit = v as char;
719 digit.to_digit(16).unwrap_or(0) << (k * 4)
720 })
721 .sum();
722
723 Ok((remaining, res))
724 }
725
726 /// Recognizes a floating point number in text format and returns the corresponding part of the input
727 ///
728 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
729 ///
730 /// ```rust
731 /// # use nom::{Err, error::ErrorKind, Needed};
732 /// # use nom::Needed::Size;
733 /// use nom::number::streaming::recognize_float;
734 ///
735 /// let parser = |s| {
736 /// recognize_float(s)
737 /// };
738 ///
739 /// assert_eq!(parser("11e-1;"), Ok((";", "11e-1")));
740 /// assert_eq!(parser("123E-02;"), Ok((";", "123E-02")));
741 /// assert_eq!(parser("123K-01"), Ok(("K-01", "123")));
742 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
743 /// ```
744 #[allow(unused_imports)]
745 #[cfg_attr(rustfmt, rustfmt_skip)]
recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar746 pub fn recognize_float<T, E:ParseError<T>>(input: T) -> IResult<T, T, E>
747 where
748 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
749 T: Clone + Offset,
750 T: InputIter,
751 <T as InputIter>::Item: AsChar,
752 T: InputTakeAtPosition,
753 <T as InputTakeAtPosition>::Item: AsChar
754 {
755 recognize(
756 tuple((
757 opt(alt((char('+'), char('-')))),
758 alt((
759 map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()),
760 map(tuple((char('.'), digit1)), |_| ())
761 )),
762 opt(tuple((
763 alt((char('e'), char('E'))),
764 opt(alt((char('+'), char('-')))),
765 cut(digit1)
766 )))
767 ))
768 )(input)
769 }
770
771 /// Recognizes floating point number in a byte string and returns a f32
772 ///
773 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
774 /// ```rust
775 /// # use nom::{Err, error::ErrorKind, Needed};
776 /// # use nom::Needed::Size;
777 /// use nom::number::streaming::float;
778 ///
779 /// let parser = |s| {
780 /// float(s)
781 /// };
782 ///
783 /// assert_eq!(parser("11e-1;"), Ok((";", 1.1)));
784 /// assert_eq!(parser("123E-02;"), Ok((";", 1.23)));
785 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
786 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
787 /// ```
788 #[cfg(not(feature = "lexical"))]
float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + crate::traits::ParseTo<f32>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar789 pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
790 where
791 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
792 T: Clone + Offset,
793 T: InputIter + InputLength + crate::traits::ParseTo<f32>,
794 <T as InputIter>::Item: AsChar,
795 T: InputTakeAtPosition,
796 <T as InputTakeAtPosition>::Item: AsChar
797 {
798 match recognize_float(input) {
799 Err(e) => Err(e),
800 Ok((i, s)) => match s.parse_to() {
801 Some(n) => Ok((i, n)),
802 None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
803 }
804 }
805 }
806
807 /// Recognizes floating point number in a byte string and returns a f32
808 ///
809 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
810 /// ```rust
811 /// # use nom::{Err, error::ErrorKind, Needed};
812 /// # use nom::Needed::Size;
813 /// use nom::number::streaming::float;
814 ///
815 /// let parser = |s| {
816 /// float(s)
817 /// };
818 ///
819 /// assert_eq!(parser("11e-1;"), Ok((";", 1.1)));
820 /// assert_eq!(parser("123E-02;"), Ok((";", 1.23)));
821 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
822 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
823 /// ```
824 ///
825 /// this function uses the lexical-core crate for float parsing by default, you
826 /// can deactivate it by removing the "lexical" feature
827 #[cfg(feature = "lexical")]
float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E> where T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,828 pub fn float<T, E:ParseError<T>>(input: T) -> IResult<T, f32, E>
829 where
830 T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
831 {
832 match ::lexical_core::parse_partial(input.as_bytes()) {
833 Ok((value, processed)) => {
834 if processed == input.input_len() {
835 Err(Err::Incomplete(Needed::Unknown))
836 } else {
837 Ok((input.slice(processed..), value))
838 }
839 },
840 Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
841 }
842 }
843
844 /// Recognizes floating point number in a byte string and returns a f64
845 ///
846 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
847 /// ```rust
848 /// # use nom::{Err, error::ErrorKind, Needed};
849 /// # use nom::Needed::Size;
850 /// use nom::number::streaming::double;
851 ///
852 /// let parser = |s| {
853 /// double(s)
854 /// };
855 ///
856 /// assert_eq!(parser("11e-1;"), Ok((";", 1.1)));
857 /// assert_eq!(parser("123E-02;"), Ok((";", 1.23)));
858 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
859 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char))));
860 /// ```
861 #[cfg(not(feature = "lexical"))]
double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> where T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, T: Clone + Offset, T: InputIter + InputLength + crate::traits::ParseTo<f64>, <T as InputIter>::Item: AsChar, T: InputTakeAtPosition, <T as InputTakeAtPosition>::Item: AsChar862 pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
863 where
864 T: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
865 T: Clone + Offset,
866 T: InputIter + InputLength + crate::traits::ParseTo<f64>,
867 <T as InputIter>::Item: AsChar,
868 T: InputTakeAtPosition,
869 <T as InputTakeAtPosition>::Item: AsChar
870 {
871 match recognize_float(input) {
872 Err(e) => Err(e),
873 Ok((i, s)) => match s.parse_to() {
874 Some(n) => Ok((i, n)),
875 None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float)))
876 }
877 }
878 }
879
880 /// Recognizes floating point number in a byte string and returns a f64
881 ///
882 /// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input
883 /// ```rust
884 /// # use nom::{Err, error::ErrorKind, Needed};
885 /// # use nom::Needed::Size;
886 /// use nom::number::streaming::double;
887 ///
888 /// let parser = |s| {
889 /// double(s)
890 /// };
891 ///
892 /// assert_eq!(parser("11e-1;"), Ok((";", 1.1)));
893 /// assert_eq!(parser("123E-02;"), Ok((";", 1.23)));
894 /// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0)));
895 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float))));
896 /// ```
897 ///
898 /// this function uses the lexical-core crate for float parsing by default, you
899 /// can deactivate it by removing the "lexical" feature
900 #[cfg(feature = "lexical")]
double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E> where T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,901 pub fn double<T, E:ParseError<T>>(input: T) -> IResult<T, f64, E>
902 where
903 T: crate::traits::AsBytes + InputLength + Slice<RangeFrom<usize>>,
904 {
905 match ::lexical_core::parse_partial(input.as_bytes()) {
906 Ok((value, processed)) => {
907 if processed == input.input_len() {
908 Err(Err::Incomplete(Needed::Unknown))
909 } else {
910 Ok((input.slice(processed..), value))
911 }
912 },
913 Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float)))
914 }
915 }
916
917 #[cfg(test)]
918 mod tests {
919 use super::*;
920 use crate::internal::{Err, Needed};
921 use crate::error::ErrorKind;
922
923 macro_rules! assert_parse(
924 ($left: expr, $right: expr) => {
925 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
926 assert_eq!(res, $right);
927 };
928 );
929
930 #[test]
i8_tests()931 fn i8_tests() {
932 assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0)));
933 assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127)));
934 assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1)));
935 assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128)));
936 }
937
938 #[test]
i16_tests()939 fn i16_tests() {
940 assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0)));
941 assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16)));
942 assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1)));
943 assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16)));
944 }
945
946 #[test]
u24_tests()947 fn u24_tests() {
948 assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
949 assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32)));
950 assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32)));
951 }
952
953 #[test]
i24_tests()954 fn i24_tests() {
955 assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32)));
956 assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32)));
957 assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32)));
958 }
959
960 #[test]
i32_tests()961 fn i32_tests() {
962 assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
963 assert_parse!(
964 be_i32(&[0x7f, 0xff, 0xff, 0xff]),
965 Ok((&b""[..], 2_147_483_647_i32))
966 );
967 assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1)));
968 assert_parse!(
969 be_i32(&[0x80, 0x00, 0x00, 0x00]),
970 Ok((&b""[..], -2_147_483_648_i32))
971 );
972 }
973
974 #[test]
i64_tests()975 fn i64_tests() {
976 assert_parse!(
977 be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
978 Ok((&b""[..], 0))
979 );
980 assert_parse!(
981 be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
982 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
983 );
984 assert_parse!(
985 be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
986 Ok((&b""[..], -1))
987 );
988 assert_parse!(
989 be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
990 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
991 );
992 }
993
994 #[test]
995 #[cfg(stable_i128)]
i128_tests()996 fn i128_tests() {
997 assert_parse!(
998 be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
999 Ok((&b""[..], 0))
1000 );
1001 assert_parse!(
1002 be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1003 Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128))
1004 );
1005 assert_parse!(
1006 be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1007 Ok((&b""[..], -1))
1008 );
1009 assert_parse!(
1010 be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1011 Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128))
1012 );
1013 }
1014
1015 #[test]
le_i8_tests()1016 fn le_i8_tests() {
1017 assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0)));
1018 assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127)));
1019 assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1)));
1020 assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128)));
1021 }
1022
1023 #[test]
le_i16_tests()1024 fn le_i16_tests() {
1025 assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0)));
1026 assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16)));
1027 assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1)));
1028 assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
1029 }
1030
1031 #[test]
le_u24_tests()1032 fn le_u24_tests() {
1033 assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
1034 assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32)));
1035 assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32)));
1036 }
1037
1038 #[test]
le_i24_tests()1039 fn le_i24_tests() {
1040 assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32)));
1041 assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32)));
1042 assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32)));
1043 }
1044
1045 #[test]
le_i32_tests()1046 fn le_i32_tests() {
1047 assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0)));
1048 assert_parse!(
1049 le_i32(&[0xff, 0xff, 0xff, 0x7f]),
1050 Ok((&b""[..], 2_147_483_647_i32))
1051 );
1052 assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1)));
1053 assert_parse!(
1054 le_i32(&[0x00, 0x00, 0x00, 0x80]),
1055 Ok((&b""[..], -2_147_483_648_i32))
1056 );
1057 }
1058
1059 #[test]
le_i64_tests()1060 fn le_i64_tests() {
1061 assert_parse!(
1062 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1063 Ok((&b""[..], 0))
1064 );
1065 assert_parse!(
1066 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
1067 Ok((&b""[..], 9_223_372_036_854_775_807_i64))
1068 );
1069 assert_parse!(
1070 le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1071 Ok((&b""[..], -1))
1072 );
1073 assert_parse!(
1074 le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]),
1075 Ok((&b""[..], -9_223_372_036_854_775_808_i64))
1076 );
1077 }
1078
1079 #[test]
1080 #[cfg(stable_i128)]
le_i128_tests()1081 fn le_i128_tests() {
1082 assert_parse!(
1083 le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1084 Ok((&b""[..], 0))
1085 );
1086 assert_parse!(
1087 le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]),
1088 Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128))
1089 );
1090 assert_parse!(
1091 le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
1092 Ok((&b""[..], -1))
1093 );
1094 assert_parse!(
1095 le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]),
1096 Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128))
1097 );
1098 }
1099
1100 #[test]
be_f32_tests()1101 fn be_f32_tests() {
1102 assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32)));
1103 assert_parse!(
1104 be_f32(&[0x4d, 0x31, 0x1f, 0xd8]),
1105 Ok((&b""[..], 185_728_392_f32))
1106 );
1107 }
1108
1109 #[test]
be_f64_tests()1110 fn be_f64_tests() {
1111 assert_parse!(
1112 be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1113 Ok((&b""[..], 0_f64))
1114 );
1115 assert_parse!(
1116 be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]),
1117 Ok((&b""[..], 185_728_392_f64))
1118 );
1119 }
1120
1121 #[test]
le_f32_tests()1122 fn le_f32_tests() {
1123 assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32)));
1124 assert_parse!(
1125 le_f32(&[0xd8, 0x1f, 0x31, 0x4d]),
1126 Ok((&b""[..], 185_728_392_f32))
1127 );
1128 }
1129
1130 #[test]
le_f64_tests()1131 fn le_f64_tests() {
1132 assert_parse!(
1133 le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
1134 Ok((&b""[..], 0_f64))
1135 );
1136 assert_parse!(
1137 le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]),
1138 Ok((&b""[..], 185_728_392_f64))
1139 );
1140 }
1141
1142 #[test]
hex_u32_tests()1143 fn hex_u32_tests() {
1144 assert_parse!(
1145 hex_u32(&b";"[..]),
1146 Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA)))
1147 );
1148 assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255)));
1149 assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138)));
1150 assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
1151 assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058)));
1152 assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347)));
1153 assert_parse!(
1154 hex_u32(&b"c5a31be201;"[..]),
1155 Ok((&b"01;"[..], 3_315_801_058))
1156 );
1157 assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295)));
1158 assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0)));
1159 assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::Size(1))));
1160 }
1161
1162 #[test]
1163 #[cfg(feature = "std")]
float_test()1164 fn float_test() {
1165 let mut test_cases = vec![
1166 "+3.14",
1167 "3.14",
1168 "-3.14",
1169 "0",
1170 "0.0",
1171 "1.",
1172 ".789",
1173 "-.5",
1174 "1e7",
1175 "-1E-7",
1176 ".3e-2",
1177 "1.e4",
1178 "1.2e4",
1179 "12.34",
1180 "-1.234E-12",
1181 "-1.234e-12",
1182 ];
1183
1184 for test in test_cases.drain(..) {
1185 let expected32 = str::parse::<f32>(test).unwrap();
1186 let expected64 = str::parse::<f64>(test).unwrap();
1187
1188 println!("now parsing: {} -> {}", test, expected32);
1189
1190 let larger = format!("{};", test);
1191 assert_parse!(recognize_float(&larger[..]), Ok((";", test)));
1192
1193 assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32)));
1194 assert_parse!(float(&larger[..]), Ok((";", expected32)));
1195
1196 assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64)));
1197 assert_parse!(double(&larger[..]), Ok((";", expected64)));
1198 }
1199
1200 let remaining_exponent = "-1.234E-";
1201 assert_parse!(
1202 recognize_float(remaining_exponent),
1203 Err(Err::Incomplete(Needed::Size(1)))
1204 );
1205 }
1206
1207 }
1208