| 1 |
//! Parsers recognizing numbers |
| 2 |
|
| 3 |
use core::{ |
| 4 |
marker::PhantomData, |
| 5 |
ops::{Add, Shl}, |
| 6 |
}; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
branch::alt, |
| 10 |
character::{char, digit1}, |
| 11 |
combinator::{cut, map, opt, recognize}, |
| 12 |
error::{make_error, ErrorKind, ParseError}, |
| 13 |
sequence::pair, |
| 14 |
AsBytes, AsChar, Compare, Either, Emit, Err, Input, IsStreaming, Mode, Needed, Offset, OutputM, |
| 15 |
Parser, |
| 16 |
}; |
| 17 |
|
| 18 |
pub mod complete; |
| 19 |
pub mod streaming; |
| 20 |
|
| 21 |
/// Configurable endianness |
| 22 |
#[derive(Debug, PartialEq, Eq, Clone, Copy)] |
| 23 |
pub enum Endianness { |
| 24 |
/// Big endian |
| 25 |
Big, |
| 26 |
/// Little endian |
| 27 |
Little, |
| 28 |
/// Will match the host's endianness |
| 29 |
Native, |
| 30 |
} |
| 31 |
|
| 32 |
/// creates a big endian unsigned integer parser |
| 33 |
/// |
| 34 |
/// * `bound`: the number of bytes that will be read |
| 35 |
/// * `Uint`: the output type |
| 36 |
#[inline] |
| 37 |
fn be_uint<I, Uint, E: ParseError<I>>(bound: usize) -> impl Parser<I, Output = Uint, Error = E> |
| 38 |
where |
| 39 |
I: Input<Item = u8>, |
| 40 |
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>, |
| 41 |
{ |
| 42 |
BeUint { |
| 43 |
bound, |
| 44 |
e: PhantomData, |
| 45 |
u: PhantomData, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// Big endian unsigned integer parser |
| 50 |
struct BeUint<Uint, E> { |
| 51 |
bound: usize, |
| 52 |
e: PhantomData<E>, |
| 53 |
u: PhantomData<Uint>, |
| 54 |
} |
| 55 |
|
| 56 |
impl<I, Uint, E: ParseError<I>> Parser<I> for BeUint<Uint, E> |
| 57 |
where |
| 58 |
I: Input<Item = u8>, |
| 59 |
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>, |
| 60 |
{ |
| 61 |
type Output = Uint; |
| 62 |
type Error = E; |
| 63 |
|
| 64 |
#[inline(always)] |
| 65 |
fn process<OM: crate::OutputMode>( |
| 66 |
&mut self, |
| 67 |
input: I, |
| 68 |
) -> crate::PResult<OM, I, Self::Output, Self::Error> { |
| 69 |
if input.input_len() < self.bound { |
| 70 |
if OM::Incomplete::is_streaming() { |
| 71 |
Err(Err::Incomplete(Needed::new(self.bound - input.input_len()))) |
| 72 |
} else { |
| 73 |
Err(Err::Error(OM::Error::bind(|| { |
| 74 |
make_error(input, ErrorKind::Eof) |
| 75 |
}))) |
| 76 |
} |
| 77 |
} else { |
| 78 |
let res = OM::Output::bind(|| { |
| 79 |
let mut res = Uint::default(); |
| 80 |
|
| 81 |
// special case to avoid shift a byte with overflow |
| 82 |
if self.bound > 1 { |
| 83 |
for byte in input.iter_elements().take(self.bound) { |
| 84 |
res = (res << 8) + byte.into(); |
| 85 |
} |
| 86 |
} else { |
| 87 |
for byte in input.iter_elements().take(self.bound) { |
| 88 |
res = byte.into(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
res |
| 93 |
}); |
| 94 |
|
| 95 |
Ok((input.take_from(self.bound), res)) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// Recognizes an unsigned 1 byte integer. |
| 101 |
/// |
| 102 |
/// ```rust |
| 103 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 104 |
/// use nom::number::be_u8; |
| 105 |
/// |
| 106 |
/// let parser = |s| { |
| 107 |
/// be_u8::<_, (_, ErrorKind)>().parse(s) |
| 108 |
/// }; |
| 109 |
/// |
| 110 |
/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00))); |
| 111 |
/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 112 |
/// ``` |
| 113 |
#[inline] |
| 114 |
pub fn be_u8<I, E: ParseError<I>>() -> impl Parser<I, Output = u8, Error = E> |
| 115 |
where |
| 116 |
I: Input<Item = u8>, |
| 117 |
{ |
| 118 |
be_uint(1) |
| 119 |
} |
| 120 |
|
| 121 |
/// Recognizes a big endian unsigned 2 bytes integer. |
| 122 |
/// |
| 123 |
/// ```rust |
| 124 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 125 |
/// use nom::number::be_u16; |
| 126 |
/// |
| 127 |
/// let parser = |s| { |
| 128 |
/// be_u16::<_, (_, ErrorKind)>().parse(s) |
| 129 |
/// }; |
| 130 |
/// |
| 131 |
/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001))); |
| 132 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 133 |
/// ``` |
| 134 |
#[inline] |
| 135 |
pub fn be_u16<I, E: ParseError<I>>() -> impl Parser<I, Output = u16, Error = E> |
| 136 |
where |
| 137 |
I: Input<Item = u8>, |
| 138 |
{ |
| 139 |
be_uint(2) |
| 140 |
} |
| 141 |
|
| 142 |
/// Recognizes a big endian unsigned 3 byte integer. |
| 143 |
/// |
| 144 |
/// ```rust |
| 145 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 146 |
/// use nom::number::be_u24; |
| 147 |
/// |
| 148 |
/// let parser = |s| { |
| 149 |
/// be_u24::<_, (_, ErrorKind)>().parse(s) |
| 150 |
/// }; |
| 151 |
/// |
| 152 |
/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102))); |
| 153 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 154 |
/// ``` |
| 155 |
#[inline] |
| 156 |
pub fn be_u24<I, E: ParseError<I>>() -> impl Parser<I, Output = u32, Error = E> |
| 157 |
where |
| 158 |
I: Input<Item = u8>, |
| 159 |
{ |
| 160 |
be_uint(3) |
| 161 |
} |
| 162 |
|
| 163 |
/// Recognizes a big endian unsigned 4 bytes integer. |
| 164 |
/// |
| 165 |
/// ```rust |
| 166 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 167 |
/// use nom::number::be_u32; |
| 168 |
/// |
| 169 |
/// let parser = |s| { |
| 170 |
/// be_u32::<_, (_, ErrorKind)>().parse(s) |
| 171 |
/// }; |
| 172 |
/// |
| 173 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203))); |
| 174 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 175 |
/// ``` |
| 176 |
#[inline] |
| 177 |
pub fn be_u32<I, E: ParseError<I>>() -> impl Parser<I, Output = u32, Error = E> |
| 178 |
where |
| 179 |
I: Input<Item = u8>, |
| 180 |
{ |
| 181 |
be_uint(4) |
| 182 |
} |
| 183 |
|
| 184 |
/// Recognizes a big endian unsigned 8 bytes integer. |
| 185 |
/// |
| 186 |
/// ```rust |
| 187 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 188 |
/// use nom::number::be_u64; |
| 189 |
/// |
| 190 |
/// let parser = |s| { |
| 191 |
/// be_u64::<_, (_, ErrorKind)>().parse(s) |
| 192 |
/// }; |
| 193 |
/// |
| 194 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607))); |
| 195 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 196 |
/// ``` |
| 197 |
#[inline] |
| 198 |
pub fn be_u64<I, E: ParseError<I>>() -> impl Parser<I, Output = u64, Error = E> |
| 199 |
where |
| 200 |
I: Input<Item = u8>, |
| 201 |
{ |
| 202 |
be_uint(8) |
| 203 |
} |
| 204 |
|
| 205 |
/// Recognizes a big endian unsigned 16 bytes integer. |
| 206 |
/// |
| 207 |
/// ```rust |
| 208 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 209 |
/// use nom::number::be_u128; |
| 210 |
/// |
| 211 |
/// let parser = |s| { |
| 212 |
/// be_u128::<_, (_, ErrorKind)>().parse(s) |
| 213 |
/// }; |
| 214 |
/// |
| 215 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); |
| 216 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 217 |
/// ``` |
| 218 |
#[inline] |
| 219 |
pub fn be_u128<I, E: ParseError<I>>() -> impl Parser<I, Output = u128, Error = E> |
| 220 |
where |
| 221 |
I: Input<Item = u8>, |
| 222 |
{ |
| 223 |
be_uint(16) |
| 224 |
} |
| 225 |
|
| 226 |
/// Recognizes a signed 1 byte integer. |
| 227 |
/// |
| 228 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 229 |
/// ```rust |
| 230 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 231 |
/// use nom::number::be_i8; |
| 232 |
/// |
| 233 |
/// let mut parser = be_i8::<_, (_, ErrorKind)>(); |
| 234 |
/// |
| 235 |
/// assert_eq!(parser.parse(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00))); |
| 236 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 237 |
/// ``` |
| 238 |
#[inline] |
| 239 |
pub fn be_i8<I, E: ParseError<I>>() -> impl Parser<I, Output = i8, Error = E> |
| 240 |
where |
| 241 |
I: Input<Item = u8>, |
| 242 |
{ |
| 243 |
be_u8().map(|x| x as i8) |
| 244 |
} |
| 245 |
|
| 246 |
/// Recognizes a big endian signed 2 bytes integer. |
| 247 |
/// |
| 248 |
/// ```rust |
| 249 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 250 |
/// use nom::number::be_i16; |
| 251 |
/// |
| 252 |
/// let mut parser = be_i16::<_, (_, ErrorKind)>(); |
| 253 |
/// |
| 254 |
/// assert_eq!(parser.parse(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0001))); |
| 255 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 256 |
/// ``` |
| 257 |
#[inline] |
| 258 |
pub fn be_i16<I, E: ParseError<I>>() -> impl Parser<I, Output = i16, Error = E> |
| 259 |
where |
| 260 |
I: Input<Item = u8>, |
| 261 |
{ |
| 262 |
be_u16().map(|x| x as i16) |
| 263 |
} |
| 264 |
|
| 265 |
/// Recognizes a big endian signed 3 bytes integer. |
| 266 |
/// |
| 267 |
/// ```rust |
| 268 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 269 |
/// use nom::number::be_i24; |
| 270 |
/// |
| 271 |
/// let mut parser = be_i24::<_, (_, ErrorKind)>(); |
| 272 |
/// |
| 273 |
/// assert_eq!(parser.parse(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x000102))); |
| 274 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 275 |
/// ``` |
| 276 |
#[inline] |
| 277 |
pub fn be_i24<I, E: ParseError<I>>() -> impl Parser<I, Output = i32, Error = E> |
| 278 |
where |
| 279 |
I: Input<Item = u8>, |
| 280 |
{ |
| 281 |
// Same as the unsigned version but we need to sign-extend manually here |
| 282 |
be_u24().map(|x| { |
| 283 |
if x & 0x80_00_00 != 0 { |
| 284 |
(x | 0xff_00_00_00) as i32 |
| 285 |
} else { |
| 286 |
x as i32 |
| 287 |
} |
| 288 |
}) |
| 289 |
} |
| 290 |
|
| 291 |
/// Recognizes a big endian signed 4 bytes integer. |
| 292 |
/// |
| 293 |
/// ```rust |
| 294 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 295 |
/// use nom::number::be_i32; |
| 296 |
/// |
| 297 |
/// let mut parser = be_i32::<_, (_, ErrorKind)>(); |
| 298 |
/// |
| 299 |
/// assert_eq!(parser.parse(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x00010203))); |
| 300 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(4)))); |
| 301 |
/// ``` |
| 302 |
#[inline] |
| 303 |
pub fn be_i32<I, E: ParseError<I>>() -> impl Parser<I, Output = i32, Error = E> |
| 304 |
where |
| 305 |
I: Input<Item = u8>, |
| 306 |
{ |
| 307 |
be_u32().map(|x| x as i32) |
| 308 |
} |
| 309 |
|
| 310 |
/// Recognizes a big endian signed 8 bytes integer. |
| 311 |
/// |
| 312 |
/// ```rust |
| 313 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 314 |
/// use nom::number::be_i64; |
| 315 |
/// |
| 316 |
/// let mut parser = be_i64::<_, (_, ErrorKind)>(); |
| 317 |
/// |
| 318 |
/// assert_eq!(parser.parse(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0001020304050607))); |
| 319 |
/// assert_eq!(parser.parse(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 320 |
/// ``` |
| 321 |
#[inline] |
| 322 |
pub fn be_i64<I, E: ParseError<I>>() -> impl Parser<I, Output = i64, Error = E> |
| 323 |
where |
| 324 |
I: Input<Item = u8>, |
| 325 |
{ |
| 326 |
be_u64().map(|x| x as i64) |
| 327 |
} |
| 328 |
|
| 329 |
/// Recognizes a big endian signed 16 bytes integer. |
| 330 |
/// |
| 331 |
/// ```rust |
| 332 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 333 |
/// use nom::number::be_i128; |
| 334 |
/// |
| 335 |
/// let mut parser = be_i128::<_, (_, ErrorKind)>(); |
| 336 |
/// |
| 337 |
/// assert_eq!(parser.parse(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); |
| 338 |
/// assert_eq!(parser.parse(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 339 |
/// ``` |
| 340 |
#[inline] |
| 341 |
pub fn be_i128<I, E: ParseError<I>>() -> impl Parser<I, Output = i128, Error = E> |
| 342 |
where |
| 343 |
I: Input<Item = u8>, |
| 344 |
{ |
| 345 |
be_u128().map(|x| x as i128) |
| 346 |
} |
| 347 |
|
| 348 |
/// creates a little endian unsigned integer parser |
| 349 |
/// |
| 350 |
/// * `bound`: the number of bytes that will be read |
| 351 |
/// * `Uint`: the output type |
| 352 |
#[inline] |
| 353 |
fn le_uint<I, Uint, E: ParseError<I>>(bound: usize) -> impl Parser<I, Output = Uint, Error = E> |
| 354 |
where |
| 355 |
I: Input<Item = u8>, |
| 356 |
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>, |
| 357 |
{ |
| 358 |
LeUint { |
| 359 |
bound, |
| 360 |
e: PhantomData, |
| 361 |
u: PhantomData, |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/// Little endian unsigned integer parser |
| 366 |
struct LeUint<Uint, E> { |
| 367 |
bound: usize, |
| 368 |
e: PhantomData<E>, |
| 369 |
u: PhantomData<Uint>, |
| 370 |
} |
| 371 |
|
| 372 |
impl<I, Uint, E: ParseError<I>> Parser<I> for LeUint<Uint, E> |
| 373 |
where |
| 374 |
I: Input<Item = u8>, |
| 375 |
Uint: Default + Shl<u8, Output = Uint> + Add<Uint, Output = Uint> + From<u8>, |
| 376 |
{ |
| 377 |
type Output = Uint; |
| 378 |
type Error = E; |
| 379 |
|
| 380 |
#[inline(always)] |
| 381 |
fn process<OM: crate::OutputMode>( |
| 382 |
&mut self, |
| 383 |
input: I, |
| 384 |
) -> crate::PResult<OM, I, Self::Output, Self::Error> { |
| 385 |
if input.input_len() < self.bound { |
| 386 |
if OM::Incomplete::is_streaming() { |
| 387 |
Err(Err::Incomplete(Needed::new(self.bound - input.input_len()))) |
| 388 |
} else { |
| 389 |
Err(Err::Error(OM::Error::bind(|| { |
| 390 |
make_error(input, ErrorKind::Eof) |
| 391 |
}))) |
| 392 |
} |
| 393 |
} else { |
| 394 |
let res = OM::Output::bind(|| { |
| 395 |
let mut res = Uint::default(); |
| 396 |
for (index, byte) in input.iter_elements().take(self.bound).enumerate() { |
| 397 |
res = res + (Uint::from(byte) << (8 * index as u8)); |
| 398 |
} |
| 399 |
|
| 400 |
res |
| 401 |
}); |
| 402 |
|
| 403 |
Ok((input.take_from(self.bound), res)) |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/// Recognizes an unsigned 1 byte integer. |
| 409 |
/// |
| 410 |
/// ```rust |
| 411 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 412 |
/// use nom::number::le_u8; |
| 413 |
/// |
| 414 |
/// let mut parser = le_u8::<_, (_, ErrorKind)>(); |
| 415 |
/// |
| 416 |
/// assert_eq!(parser.parse(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00))); |
| 417 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 418 |
/// ``` |
| 419 |
#[inline] |
| 420 |
pub fn le_u8<I, E: ParseError<I>>() -> impl Parser<I, Output = u8, Error = E> |
| 421 |
where |
| 422 |
I: Input<Item = u8>, |
| 423 |
{ |
| 424 |
le_uint(1) |
| 425 |
} |
| 426 |
|
| 427 |
/// Recognizes a little endian unsigned 2 bytes integer. |
| 428 |
/// |
| 429 |
/// |
| 430 |
/// ```rust |
| 431 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 432 |
/// use nom::number::le_u16; |
| 433 |
/// |
| 434 |
/// let parser = |s| { |
| 435 |
/// le_u16::<_, (_, ErrorKind)>().parse(s) |
| 436 |
/// }; |
| 437 |
/// |
| 438 |
/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100))); |
| 439 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 440 |
/// ``` |
| 441 |
#[inline] |
| 442 |
pub fn le_u16<I, E: ParseError<I>>() -> impl Parser<I, Output = u16, Error = E> |
| 443 |
where |
| 444 |
I: Input<Item = u8>, |
| 445 |
{ |
| 446 |
le_uint(2) |
| 447 |
} |
| 448 |
|
| 449 |
/// Recognizes a little endian unsigned 3 bytes integer. |
| 450 |
/// |
| 451 |
/// ```rust |
| 452 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 453 |
/// use nom::number::le_u24; |
| 454 |
/// |
| 455 |
/// let parser = |s| { |
| 456 |
/// le_u24::<_, (_, ErrorKind)>().parse(s) |
| 457 |
/// }; |
| 458 |
/// |
| 459 |
/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100))); |
| 460 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 461 |
/// ``` |
| 462 |
#[inline] |
| 463 |
pub fn le_u24<I, E: ParseError<I>>() -> impl Parser<I, Output = u32, Error = E> |
| 464 |
where |
| 465 |
I: Input<Item = u8>, |
| 466 |
{ |
| 467 |
le_uint(3) |
| 468 |
} |
| 469 |
|
| 470 |
/// Recognizes a little endian unsigned 4 bytes integer. |
| 471 |
/// |
| 472 |
/// ```rust |
| 473 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 474 |
/// use nom::number::le_u32; |
| 475 |
/// |
| 476 |
/// let parser = |s| { |
| 477 |
/// le_u32::<_, (_, ErrorKind)>().parse(s) |
| 478 |
/// }; |
| 479 |
/// |
| 480 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100))); |
| 481 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 482 |
/// ``` |
| 483 |
#[inline] |
| 484 |
pub fn le_u32<I, E: ParseError<I>>() -> impl Parser<I, Output = u32, Error = E> |
| 485 |
where |
| 486 |
I: Input<Item = u8>, |
| 487 |
{ |
| 488 |
le_uint(4) |
| 489 |
} |
| 490 |
|
| 491 |
/// Recognizes a little endian unsigned 8 bytes integer. |
| 492 |
/// |
| 493 |
/// ```rust |
| 494 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 495 |
/// use nom::number::le_u64; |
| 496 |
/// |
| 497 |
/// let parser = |s| { |
| 498 |
/// le_u64::<_, (_, ErrorKind)>().parse(s) |
| 499 |
/// }; |
| 500 |
/// |
| 501 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100))); |
| 502 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 503 |
/// ``` |
| 504 |
#[inline] |
| 505 |
pub fn le_u64<I, E: ParseError<I>>() -> impl Parser<I, Output = u64, Error = E> |
| 506 |
where |
| 507 |
I: Input<Item = u8>, |
| 508 |
{ |
| 509 |
le_uint(8) |
| 510 |
} |
| 511 |
|
| 512 |
/// Recognizes a little endian unsigned 16 bytes integer. |
| 513 |
/// |
| 514 |
/// ```rust |
| 515 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 516 |
/// use nom::number::le_u128; |
| 517 |
/// |
| 518 |
/// let mut parser = |s| { |
| 519 |
/// le_u128::<_, (_, ErrorKind)>().parse(s) |
| 520 |
/// }; |
| 521 |
/// |
| 522 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); |
| 523 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 524 |
/// ``` |
| 525 |
#[inline] |
| 526 |
pub fn le_u128<I, E: ParseError<I>>() -> impl Parser<I, Output = u128, Error = E> |
| 527 |
where |
| 528 |
I: Input<Item = u8>, |
| 529 |
{ |
| 530 |
le_uint(16) |
| 531 |
} |
| 532 |
|
| 533 |
/// Recognizes a signed 1 byte integer. |
| 534 |
/// |
| 535 |
/// ```rust |
| 536 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 537 |
/// use nom::number::le_i8; |
| 538 |
/// |
| 539 |
/// let mut parser = le_i8::<_, (_, ErrorKind)>(); |
| 540 |
/// |
| 541 |
/// assert_eq!(parser.parse(&b"\x00\x01abcd"[..]), Ok((&b"\x01abcd"[..], 0x00))); |
| 542 |
/// assert_eq!(parser.parse(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 543 |
/// ``` |
| 544 |
#[inline] |
| 545 |
pub fn le_i8<I, E: ParseError<I>>() -> impl Parser<I, Output = i8, Error = E> |
| 546 |
where |
| 547 |
I: Input<Item = u8>, |
| 548 |
{ |
| 549 |
le_u8().map(|x| x as i8) |
| 550 |
} |
| 551 |
|
| 552 |
/// Recognizes a little endian signed 2 bytes integer. |
| 553 |
/// |
| 554 |
/// ```rust |
| 555 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 556 |
/// use nom::number::le_i16; |
| 557 |
/// |
| 558 |
/// let parser = |s| { |
| 559 |
/// le_i16::<_, (_, ErrorKind)>().parse(s) |
| 560 |
/// }; |
| 561 |
/// |
| 562 |
/// assert_eq!(parser(&b"\x00\x01abcd"[..]), Ok((&b"abcd"[..], 0x0100))); |
| 563 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 564 |
/// ``` |
| 565 |
#[inline] |
| 566 |
pub fn le_i16<I, E: ParseError<I>>() -> impl Parser<I, Output = i16, Error = E> |
| 567 |
where |
| 568 |
I: Input<Item = u8>, |
| 569 |
{ |
| 570 |
le_u16().map(|x| x as i16) |
| 571 |
} |
| 572 |
|
| 573 |
/// Recognizes a little endian signed 3 bytes integer. |
| 574 |
/// |
| 575 |
/// ```rust |
| 576 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 577 |
/// use nom::number::le_i24; |
| 578 |
/// |
| 579 |
/// let parser = |s| { |
| 580 |
/// le_i24::<_, (_, ErrorKind)>().parse(s) |
| 581 |
/// }; |
| 582 |
/// |
| 583 |
/// assert_eq!(parser(&b"\x00\x01\x02abcd"[..]), Ok((&b"abcd"[..], 0x020100))); |
| 584 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 585 |
/// ``` |
| 586 |
#[inline] |
| 587 |
pub fn le_i24<I, E: ParseError<I>>() -> impl Parser<I, Output = i32, Error = E> |
| 588 |
where |
| 589 |
I: Input<Item = u8>, |
| 590 |
{ |
| 591 |
// Same as the unsigned version but we need to sign-extend manually here |
| 592 |
le_u24().map(|x| { |
| 593 |
if x & 0x80_00_00 != 0 { |
| 594 |
(x | 0xff_00_00_00) as i32 |
| 595 |
} else { |
| 596 |
x as i32 |
| 597 |
} |
| 598 |
}) |
| 599 |
} |
| 600 |
|
| 601 |
/// Recognizes a little endian signed 4 bytes integer. |
| 602 |
/// |
| 603 |
/// ```rust |
| 604 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 605 |
/// use nom::number::le_i32; |
| 606 |
/// |
| 607 |
/// let parser = |s| { |
| 608 |
/// le_i32::<_, (_, ErrorKind)>().parse(s) |
| 609 |
/// }; |
| 610 |
/// |
| 611 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03abcd"[..]), Ok((&b"abcd"[..], 0x03020100))); |
| 612 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 613 |
/// ``` |
| 614 |
#[inline] |
| 615 |
pub fn le_i32<I, E: ParseError<I>>() -> impl Parser<I, Output = i32, Error = E> |
| 616 |
where |
| 617 |
I: Input<Item = u8>, |
| 618 |
{ |
| 619 |
le_u32().map(|x| x as i32) |
| 620 |
} |
| 621 |
|
| 622 |
/// Recognizes a little endian signed 8 bytes integer. |
| 623 |
/// |
| 624 |
/// ```rust |
| 625 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 626 |
/// use nom::number::le_i64; |
| 627 |
/// |
| 628 |
/// let parser = |s| { |
| 629 |
/// le_i64::<_, (_, ErrorKind)>().parse(s) |
| 630 |
/// }; |
| 631 |
/// |
| 632 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..]), Ok((&b"abcd"[..], 0x0706050403020100))); |
| 633 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 634 |
/// ``` |
| 635 |
#[inline] |
| 636 |
pub fn le_i64<I, E: ParseError<I>>() -> impl Parser<I, Output = i64, Error = E> |
| 637 |
where |
| 638 |
I: Input<Item = u8>, |
| 639 |
{ |
| 640 |
le_u64().map(|x| x as i64) |
| 641 |
} |
| 642 |
|
| 643 |
/// Recognizes a little endian signed 16 bytes integer. |
| 644 |
/// |
| 645 |
/// ```rust |
| 646 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 647 |
/// use nom::number::le_i128; |
| 648 |
/// |
| 649 |
/// let parser = |s| { |
| 650 |
/// le_i128::<_, (_, ErrorKind)>().parse(s) |
| 651 |
/// }; |
| 652 |
/// |
| 653 |
/// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..]), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); |
| 654 |
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 655 |
/// ``` |
| 656 |
#[inline] |
| 657 |
pub fn le_i128<I, E: ParseError<I>>() -> impl Parser<I, Output = i128, Error = E> |
| 658 |
where |
| 659 |
I: Input<Item = u8>, |
| 660 |
{ |
| 661 |
le_u128().map(|x| x as i128) |
| 662 |
} |
| 663 |
|
| 664 |
/// Recognizes an unsigned 1 byte integer |
| 665 |
/// |
| 666 |
/// Note that endianness does not apply to 1 byte numbers. |
| 667 |
/// ```rust |
| 668 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 669 |
/// # use nom::Needed::Size; |
| 670 |
/// use nom::number::u8; |
| 671 |
/// |
| 672 |
/// let parser = |s| { |
| 673 |
/// u8::<_, (_, ErrorKind)>().parse(s) |
| 674 |
/// }; |
| 675 |
/// |
| 676 |
/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); |
| 677 |
/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 678 |
/// ``` |
| 679 |
#[inline] |
| 680 |
pub fn u8<I, E: ParseError<I>>() -> impl Parser<I, Output = u8, Error = E> |
| 681 |
where |
| 682 |
I: Input<Item = u8>, |
| 683 |
{ |
| 684 |
be_u8() |
| 685 |
} |
| 686 |
|
| 687 |
/// Recognizes an unsigned 2 bytes integer |
| 688 |
/// |
| 689 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u16 integer, |
| 690 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian u16 integer. |
| 691 |
/// |
| 692 |
/// ```rust |
| 693 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 694 |
/// # use nom::Needed::Size; |
| 695 |
/// use nom::number::u16; |
| 696 |
/// |
| 697 |
/// let be_u16 = |s| { |
| 698 |
/// u16::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 699 |
/// }; |
| 700 |
/// |
| 701 |
/// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); |
| 702 |
/// assert_eq!(be_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 703 |
/// |
| 704 |
/// let le_u16 = |s| { |
| 705 |
/// u16::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 706 |
/// }; |
| 707 |
/// |
| 708 |
/// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); |
| 709 |
/// assert_eq!(le_u16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 710 |
/// ``` |
| 711 |
#[inline] |
| 712 |
pub fn u16<I, E: ParseError<I>>( |
| 713 |
endian: crate::number::Endianness, |
| 714 |
) -> impl Parser<I, Output = u16, Error = E> |
| 715 |
where |
| 716 |
I: Input<Item = u8>, |
| 717 |
{ |
| 718 |
match endian { |
| 719 |
crate::number::Endianness::Big => Either::Left(be_u16()), |
| 720 |
crate::number::Endianness::Little => Either::Right(le_u16()), |
| 721 |
#[cfg(target_endian = "big")] |
| 722 |
crate::number::Endianness::Native => Either::Left(be_u16()), |
| 723 |
#[cfg(target_endian = "little")] |
| 724 |
crate::number::Endianness::Native => Either::Right(le_u16()), |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/// Recognizes an unsigned 3 byte integer |
| 729 |
/// |
| 730 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u24 integer, |
| 731 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian u24 integer. |
| 732 |
/// ```rust |
| 733 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 734 |
/// # use nom::Needed::Size; |
| 735 |
/// use nom::number::u24; |
| 736 |
/// |
| 737 |
/// let be_u24 = |s| { |
| 738 |
/// u24::<_,(_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 739 |
/// }; |
| 740 |
/// |
| 741 |
/// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); |
| 742 |
/// assert_eq!(be_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 743 |
/// |
| 744 |
/// let le_u24 = |s| { |
| 745 |
/// u24::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 746 |
/// }; |
| 747 |
/// |
| 748 |
/// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); |
| 749 |
/// assert_eq!(le_u24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 750 |
/// ``` |
| 751 |
#[inline] |
| 752 |
pub fn u24<I, E: ParseError<I>>( |
| 753 |
endian: crate::number::Endianness, |
| 754 |
) -> impl Parser<I, Output = u32, Error = E> |
| 755 |
where |
| 756 |
I: Input<Item = u8>, |
| 757 |
{ |
| 758 |
match endian { |
| 759 |
crate::number::Endianness::Big => Either::Left(be_u24()), |
| 760 |
crate::number::Endianness::Little => Either::Right(le_u24()), |
| 761 |
#[cfg(target_endian = "big")] |
| 762 |
crate::number::Endianness::Native => Either::Left(be_u24()), |
| 763 |
#[cfg(target_endian = "little")] |
| 764 |
crate::number::Endianness::Native => Either::Right(le_u24()), |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
/// Recognizes an unsigned 4 byte integer |
| 769 |
/// |
| 770 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u32 integer, |
| 771 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian u32 integer. |
| 772 |
/// ```rust |
| 773 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 774 |
/// # use nom::Needed::Size; |
| 775 |
/// use nom::number::u32; |
| 776 |
/// |
| 777 |
/// let be_u32 = |s| { |
| 778 |
/// u32::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 779 |
/// }; |
| 780 |
/// |
| 781 |
/// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); |
| 782 |
/// assert_eq!(be_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 783 |
/// |
| 784 |
/// let le_u32 = |s| { |
| 785 |
/// u32::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 786 |
/// }; |
| 787 |
/// |
| 788 |
/// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); |
| 789 |
/// assert_eq!(le_u32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 790 |
/// ``` |
| 791 |
#[inline] |
| 792 |
pub fn u32<I, E: ParseError<I>>( |
| 793 |
endian: crate::number::Endianness, |
| 794 |
) -> impl Parser<I, Output = u32, Error = E> |
| 795 |
where |
| 796 |
I: Input<Item = u8>, |
| 797 |
{ |
| 798 |
match endian { |
| 799 |
crate::number::Endianness::Big => Either::Left(be_u32()), |
| 800 |
crate::number::Endianness::Little => Either::Right(le_u32()), |
| 801 |
#[cfg(target_endian = "big")] |
| 802 |
crate::number::Endianness::Native => Either::Left(be_u32()), |
| 803 |
#[cfg(target_endian = "little")] |
| 804 |
crate::number::Endianness::Native => Either::Right(le_u32()), |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
/// Recognizes an unsigned 8 byte integer |
| 809 |
/// |
| 810 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u64 integer, |
| 811 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian u64 integer. |
| 812 |
/// ```rust |
| 813 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 814 |
/// # use nom::Needed::Size; |
| 815 |
/// use nom::number::u64; |
| 816 |
/// |
| 817 |
/// let be_u64 = |s| { |
| 818 |
/// u64::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 819 |
/// }; |
| 820 |
/// |
| 821 |
/// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); |
| 822 |
/// assert_eq!(be_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 823 |
/// |
| 824 |
/// let le_u64 = |s| { |
| 825 |
/// u64::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 826 |
/// }; |
| 827 |
/// |
| 828 |
/// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); |
| 829 |
/// assert_eq!(le_u64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 830 |
/// ``` |
| 831 |
#[inline] |
| 832 |
pub fn u64<I, E: ParseError<I>>( |
| 833 |
endian: crate::number::Endianness, |
| 834 |
) -> impl Parser<I, Output = u64, Error = E> |
| 835 |
where |
| 836 |
I: Input<Item = u8>, |
| 837 |
{ |
| 838 |
match endian { |
| 839 |
crate::number::Endianness::Big => Either::Left(be_u64()), |
| 840 |
crate::number::Endianness::Little => Either::Right(le_u64()), |
| 841 |
#[cfg(target_endian = "big")] |
| 842 |
crate::number::Endianness::Native => Either::Left(be_u64()), |
| 843 |
#[cfg(target_endian = "little")] |
| 844 |
crate::number::Endianness::Native => Either::Right(le_u64()), |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
/// Recognizes an unsigned 16 byte integer |
| 849 |
/// |
| 850 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian u128 integer, |
| 851 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian u128 integer. |
| 852 |
/// ```rust |
| 853 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 854 |
/// # use nom::Needed::Size; |
| 855 |
/// use nom::number::u128; |
| 856 |
/// |
| 857 |
/// let be_u128 = |s| { |
| 858 |
/// u128::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 859 |
/// }; |
| 860 |
/// |
| 861 |
/// 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))); |
| 862 |
/// assert_eq!(be_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 863 |
/// |
| 864 |
/// let le_u128 = |s| { |
| 865 |
/// u128::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 866 |
/// }; |
| 867 |
/// |
| 868 |
/// 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))); |
| 869 |
/// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 870 |
/// ``` |
| 871 |
#[inline] |
| 872 |
pub fn u128<I, E: ParseError<I>>( |
| 873 |
endian: crate::number::Endianness, |
| 874 |
) -> impl Parser<I, Output = u128, Error = E> |
| 875 |
where |
| 876 |
I: Input<Item = u8>, |
| 877 |
{ |
| 878 |
match endian { |
| 879 |
crate::number::Endianness::Big => Either::Left(be_u128()), |
| 880 |
crate::number::Endianness::Little => Either::Right(le_u128()), |
| 881 |
#[cfg(target_endian = "big")] |
| 882 |
crate::number::Endianness::Native => Either::Left(be_u128()), |
| 883 |
#[cfg(target_endian = "little")] |
| 884 |
crate::number::Endianness::Native => Either::Right(le_u128()), |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/// Recognizes a signed 1 byte integer |
| 889 |
/// |
| 890 |
/// Note that endianness does not apply to 1 byte numbers. |
| 891 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 892 |
/// ```rust |
| 893 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 894 |
/// # use nom::Needed::Size; |
| 895 |
/// use nom::number::i8; |
| 896 |
/// |
| 897 |
/// let parser = |s| { |
| 898 |
/// i8::<_, (_, ErrorKind)>().parse(s) |
| 899 |
/// }; |
| 900 |
/// |
| 901 |
/// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); |
| 902 |
/// assert_eq!(parser(&b""[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 903 |
/// ``` |
| 904 |
#[inline] |
| 905 |
pub fn i8<I, E: ParseError<I>>() -> impl Parser<I, Output = i8, Error = E> |
| 906 |
where |
| 907 |
I: Input<Item = u8>, |
| 908 |
{ |
| 909 |
u8().map(|x| x as i8) |
| 910 |
} |
| 911 |
|
| 912 |
/// Recognizes a signed 2 byte integer |
| 913 |
/// |
| 914 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i16 integer, |
| 915 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian i16 integer. |
| 916 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 917 |
/// ```rust |
| 918 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 919 |
/// # use nom::Needed::Size; |
| 920 |
/// use nom::number::i16; |
| 921 |
/// |
| 922 |
/// let be_i16 = |s| { |
| 923 |
/// i16::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 924 |
/// }; |
| 925 |
/// |
| 926 |
/// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); |
| 927 |
/// assert_eq!(be_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 928 |
/// |
| 929 |
/// let le_i16 = |s| { |
| 930 |
/// i16::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 931 |
/// }; |
| 932 |
/// |
| 933 |
/// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); |
| 934 |
/// assert_eq!(le_i16(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 935 |
/// ``` |
| 936 |
#[inline] |
| 937 |
pub fn i16<I, E: ParseError<I>>( |
| 938 |
endian: crate::number::Endianness, |
| 939 |
) -> impl Parser<I, Output = i16, Error = E> |
| 940 |
where |
| 941 |
I: Input<Item = u8>, |
| 942 |
{ |
| 943 |
match endian { |
| 944 |
crate::number::Endianness::Big => Either::Left(be_i16()), |
| 945 |
crate::number::Endianness::Little => Either::Right(le_i16()), |
| 946 |
#[cfg(target_endian = "big")] |
| 947 |
crate::number::Endianness::Native => Either::Left(be_i16()), |
| 948 |
#[cfg(target_endian = "little")] |
| 949 |
crate::number::Endianness::Native => Either::Right(le_i16()), |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
/// Recognizes a signed 3 byte integer |
| 954 |
/// |
| 955 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i24 integer, |
| 956 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian i24 integer. |
| 957 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 958 |
/// ```rust |
| 959 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 960 |
/// # use nom::Needed::Size; |
| 961 |
/// use nom::number::i24; |
| 962 |
/// |
| 963 |
/// let be_i24 = |s| { |
| 964 |
/// i24::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 965 |
/// }; |
| 966 |
/// |
| 967 |
/// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); |
| 968 |
/// assert_eq!(be_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 969 |
/// |
| 970 |
/// let le_i24 = |s| { |
| 971 |
/// i24::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 972 |
/// }; |
| 973 |
/// |
| 974 |
/// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); |
| 975 |
/// assert_eq!(le_i24(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(2)))); |
| 976 |
/// ``` |
| 977 |
#[inline] |
| 978 |
pub fn i24<I, E: ParseError<I>>( |
| 979 |
endian: crate::number::Endianness, |
| 980 |
) -> impl Parser<I, Output = i32, Error = E> |
| 981 |
where |
| 982 |
I: Input<Item = u8>, |
| 983 |
{ |
| 984 |
match endian { |
| 985 |
crate::number::Endianness::Big => Either::Left(be_i24()), |
| 986 |
crate::number::Endianness::Little => Either::Right(le_i24()), |
| 987 |
#[cfg(target_endian = "big")] |
| 988 |
crate::number::Endianness::Native => Either::Left(be_i24()), |
| 989 |
#[cfg(target_endian = "little")] |
| 990 |
crate::number::Endianness::Native => Either::Right(le_i24()), |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
/// Recognizes a signed 4 byte integer |
| 995 |
/// |
| 996 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i32 integer, |
| 997 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian i32 integer. |
| 998 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 999 |
/// ```rust |
| 1000 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1001 |
/// # use nom::Needed::Size; |
| 1002 |
/// use nom::number::i32; |
| 1003 |
/// |
| 1004 |
/// let be_i32 = |s| { |
| 1005 |
/// i32::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 1006 |
/// }; |
| 1007 |
/// |
| 1008 |
/// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); |
| 1009 |
/// assert_eq!(be_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 1010 |
/// |
| 1011 |
/// let le_i32 = |s| { |
| 1012 |
/// i32::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 1013 |
/// }; |
| 1014 |
/// |
| 1015 |
/// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); |
| 1016 |
/// assert_eq!(le_i32(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(3)))); |
| 1017 |
/// ``` |
| 1018 |
#[inline] |
| 1019 |
pub fn i32<I, E: ParseError<I>>( |
| 1020 |
endian: crate::number::Endianness, |
| 1021 |
) -> impl Parser<I, Output = i32, Error = E> |
| 1022 |
where |
| 1023 |
I: Input<Item = u8>, |
| 1024 |
{ |
| 1025 |
match endian { |
| 1026 |
crate::number::Endianness::Big => Either::Left(be_i32()), |
| 1027 |
crate::number::Endianness::Little => Either::Right(le_i32()), |
| 1028 |
#[cfg(target_endian = "big")] |
| 1029 |
crate::number::Endianness::Native => Either::Left(be_i32()), |
| 1030 |
#[cfg(target_endian = "little")] |
| 1031 |
crate::number::Endianness::Native => Either::Right(le_i32()), |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
/// Recognizes a signed 8 byte integer |
| 1036 |
/// |
| 1037 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i64 integer, |
| 1038 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian i64 integer. |
| 1039 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1040 |
/// ```rust |
| 1041 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1042 |
/// # use nom::Needed::Size; |
| 1043 |
/// use nom::number::i64; |
| 1044 |
/// |
| 1045 |
/// let be_i64 = |s| { |
| 1046 |
/// i64::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 1047 |
/// }; |
| 1048 |
/// |
| 1049 |
/// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); |
| 1050 |
/// assert_eq!(be_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 1051 |
/// |
| 1052 |
/// let le_i64 = |s| { |
| 1053 |
/// i64::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 1054 |
/// }; |
| 1055 |
/// |
| 1056 |
/// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); |
| 1057 |
/// assert_eq!(le_i64(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(7)))); |
| 1058 |
/// ``` |
| 1059 |
#[inline] |
| 1060 |
pub fn i64<I, E: ParseError<I>>( |
| 1061 |
endian: crate::number::Endianness, |
| 1062 |
) -> impl Parser<I, Output = i64, Error = E> |
| 1063 |
where |
| 1064 |
I: Input<Item = u8>, |
| 1065 |
{ |
| 1066 |
match endian { |
| 1067 |
crate::number::Endianness::Big => Either::Left(be_i64()), |
| 1068 |
crate::number::Endianness::Little => Either::Right(le_i64()), |
| 1069 |
#[cfg(target_endian = "big")] |
| 1070 |
crate::number::Endianness::Native => Either::Left(be_i64()), |
| 1071 |
#[cfg(target_endian = "little")] |
| 1072 |
crate::number::Endianness::Native => Either::Right(le_i64()), |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
/// Recognizes a signed 16 byte integer |
| 1077 |
/// |
| 1078 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian i128 integer, |
| 1079 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian i128 integer. |
| 1080 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1081 |
/// ```rust |
| 1082 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1083 |
/// # use nom::Needed::Size; |
| 1084 |
/// use nom::number::i128; |
| 1085 |
/// |
| 1086 |
/// let be_i128 = |s| { |
| 1087 |
/// i128::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 1088 |
/// }; |
| 1089 |
/// |
| 1090 |
/// 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))); |
| 1091 |
/// assert_eq!(be_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 1092 |
/// |
| 1093 |
/// let le_i128 = |s| { |
| 1094 |
/// i128::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 1095 |
/// }; |
| 1096 |
/// |
| 1097 |
/// 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))); |
| 1098 |
/// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15)))); |
| 1099 |
/// ``` |
| 1100 |
#[inline] |
| 1101 |
pub fn i128<I, E: ParseError<I>>( |
| 1102 |
endian: crate::number::Endianness, |
| 1103 |
) -> impl Parser<I, Output = i128, Error = E> |
| 1104 |
where |
| 1105 |
I: Input<Item = u8>, |
| 1106 |
{ |
| 1107 |
match endian { |
| 1108 |
crate::number::Endianness::Big => Either::Left(be_i128()), |
| 1109 |
crate::number::Endianness::Little => Either::Right(le_i128()), |
| 1110 |
#[cfg(target_endian = "big")] |
| 1111 |
crate::number::Endianness::Native => Either::Left(be_i128()), |
| 1112 |
#[cfg(target_endian = "little")] |
| 1113 |
crate::number::Endianness::Native => Either::Right(le_i128()), |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
/// Recognizes a big endian 4 bytes floating point number. |
| 1118 |
/// |
| 1119 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1120 |
/// ```rust |
| 1121 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1122 |
/// use nom::number::be_f32; |
| 1123 |
/// |
| 1124 |
/// let parser = |s| { |
| 1125 |
/// be_f32::<_, (_, ErrorKind)>().parse(s) |
| 1126 |
/// }; |
| 1127 |
/// |
| 1128 |
/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625))); |
| 1129 |
/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3)))); |
| 1130 |
/// ``` |
| 1131 |
#[inline] |
| 1132 |
pub fn be_f32<I, E: ParseError<I>>() -> impl Parser<I, Output = f32, Error = E> |
| 1133 |
where |
| 1134 |
I: Input<Item = u8>, |
| 1135 |
{ |
| 1136 |
be_u32().map(f32::from_bits) |
| 1137 |
} |
| 1138 |
|
| 1139 |
/// Recognizes a big endian 8 bytes floating point number. |
| 1140 |
/// |
| 1141 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1142 |
/// ```rust |
| 1143 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1144 |
/// use nom::number::be_f64; |
| 1145 |
/// |
| 1146 |
/// let parser = |s| { |
| 1147 |
/// be_f64::<_, (_, ErrorKind)>().parse(s) |
| 1148 |
/// }; |
| 1149 |
/// |
| 1150 |
/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); |
| 1151 |
/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7)))); |
| 1152 |
/// ``` |
| 1153 |
#[inline] |
| 1154 |
pub fn be_f64<I, E: ParseError<I>>() -> impl Parser<I, Output = f64, Error = E> |
| 1155 |
where |
| 1156 |
I: Input<Item = u8>, |
| 1157 |
{ |
| 1158 |
be_u64().map(f64::from_bits) |
| 1159 |
} |
| 1160 |
|
| 1161 |
/// Recognizes a little endian 4 bytes floating point number. |
| 1162 |
/// |
| 1163 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1164 |
/// ```rust |
| 1165 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1166 |
/// use nom::number::le_f32; |
| 1167 |
/// |
| 1168 |
/// let parser = |s| { |
| 1169 |
/// le_f32::<_, (_, ErrorKind)>().parse(s) |
| 1170 |
/// }; |
| 1171 |
/// |
| 1172 |
/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); |
| 1173 |
/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(3)))); |
| 1174 |
/// ``` |
| 1175 |
#[inline] |
| 1176 |
pub fn le_f32<I, E: ParseError<I>>() -> impl Parser<I, Output = f32, Error = E> |
| 1177 |
where |
| 1178 |
I: Input<Item = u8>, |
| 1179 |
{ |
| 1180 |
le_u32().map(f32::from_bits) |
| 1181 |
} |
| 1182 |
|
| 1183 |
/// Recognizes a little endian 8 bytes floating point number. |
| 1184 |
/// |
| 1185 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1186 |
/// ```rust |
| 1187 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1188 |
/// use nom::number::le_f64; |
| 1189 |
/// |
| 1190 |
/// let parser = |s| { |
| 1191 |
/// le_f64::<_, (_, ErrorKind)>().parse(s) |
| 1192 |
/// }; |
| 1193 |
/// |
| 1194 |
/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0))); |
| 1195 |
/// assert_eq!(parser(&[0x01][..]), Err(Err::Incomplete(Needed::new(7)))); |
| 1196 |
/// ``` |
| 1197 |
#[inline] |
| 1198 |
pub fn le_f64<I, E: ParseError<I>>() -> impl Parser<I, Output = f64, Error = E> |
| 1199 |
where |
| 1200 |
I: Input<Item = u8>, |
| 1201 |
{ |
| 1202 |
le_u64().map(f64::from_bits) |
| 1203 |
} |
| 1204 |
|
| 1205 |
/// Recognizes a 4 byte floating point number |
| 1206 |
/// |
| 1207 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian f32 float, |
| 1208 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian f32 float. |
| 1209 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1210 |
/// ```rust |
| 1211 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1212 |
/// # use nom::Needed::Size; |
| 1213 |
/// use nom::number::f32; |
| 1214 |
/// |
| 1215 |
/// let be_f32 = |s| { |
| 1216 |
/// f32::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 1217 |
/// }; |
| 1218 |
/// |
| 1219 |
/// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); |
| 1220 |
/// assert_eq!(be_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 1221 |
/// |
| 1222 |
/// let le_f32 = |s| { |
| 1223 |
/// f32::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 1224 |
/// }; |
| 1225 |
/// |
| 1226 |
/// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); |
| 1227 |
/// assert_eq!(le_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1)))); |
| 1228 |
/// ``` |
| 1229 |
#[inline] |
| 1230 |
pub fn f32<I, E: ParseError<I>>( |
| 1231 |
endian: crate::number::Endianness, |
| 1232 |
) -> impl Parser<I, Output = f32, Error = E> |
| 1233 |
where |
| 1234 |
I: Input<Item = u8>, |
| 1235 |
{ |
| 1236 |
match endian { |
| 1237 |
crate::number::Endianness::Big => Either::Left(be_f32()), |
| 1238 |
crate::number::Endianness::Little => Either::Right(le_f32()), |
| 1239 |
#[cfg(target_endian = "big")] |
| 1240 |
crate::number::Endianness::Native => Either::Left(be_f32()), |
| 1241 |
#[cfg(target_endian = "little")] |
| 1242 |
crate::number::Endianness::Native => Either::Right(le_f32()), |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
/// Recognizes an 8 byte floating point number |
| 1247 |
/// |
| 1248 |
/// If the parameter is `nom::number::Endianness::Big`, parse a big endian f64 float, |
| 1249 |
/// otherwise if `nom::number::Endianness::Little` parse a little endian f64 float. |
| 1250 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. |
| 1251 |
/// ```rust |
| 1252 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1253 |
/// # use nom::Needed::Size; |
| 1254 |
/// use nom::number::f64; |
| 1255 |
/// |
| 1256 |
/// let be_f64 = |s| { |
| 1257 |
/// f64::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s) |
| 1258 |
/// }; |
| 1259 |
/// |
| 1260 |
/// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); |
| 1261 |
/// assert_eq!(be_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5)))); |
| 1262 |
/// |
| 1263 |
/// let le_f64 = |s| { |
| 1264 |
/// f64::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s) |
| 1265 |
/// }; |
| 1266 |
/// |
| 1267 |
/// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5))); |
| 1268 |
/// assert_eq!(le_f64(&b"abc"[..]), Err(Err::Incomplete(Needed::new(5)))); |
| 1269 |
/// ``` |
| 1270 |
#[inline] |
| 1271 |
pub fn f64<I, E: ParseError<I>>( |
| 1272 |
endian: crate::number::Endianness, |
| 1273 |
) -> impl Parser<I, Output = f64, Error = E> |
| 1274 |
where |
| 1275 |
I: Input<Item = u8>, |
| 1276 |
{ |
| 1277 |
match endian { |
| 1278 |
crate::number::Endianness::Big => Either::Left(be_f64()), |
| 1279 |
crate::number::Endianness::Little => Either::Right(le_f64()), |
| 1280 |
#[cfg(target_endian = "big")] |
| 1281 |
crate::number::Endianness::Native => Either::Left(be_f64()), |
| 1282 |
#[cfg(target_endian = "little")] |
| 1283 |
crate::number::Endianness::Native => Either::Right(le_f64()), |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
/// Recognizes a floating point number in text format and returns the corresponding part of the input. |
| 1288 |
/// |
| 1289 |
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input. |
| 1290 |
/// |
| 1291 |
/// ```rust |
| 1292 |
/// # use nom::{Err, error::ErrorKind, Needed, Parser}; |
| 1293 |
/// use nom::number::recognize_float; |
| 1294 |
/// |
| 1295 |
/// let parser = |s| { |
| 1296 |
/// recognize_float().parse(s) |
| 1297 |
/// }; |
| 1298 |
/// |
| 1299 |
/// assert_eq!(parser("11e-1;"), Ok((";", "11e-1"))); |
| 1300 |
/// assert_eq!(parser("123E-02;"), Ok((";", "123E-02"))); |
| 1301 |
/// assert_eq!(parser("123K-01"), Ok(("K-01", "123"))); |
| 1302 |
/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); |
| 1303 |
/// ``` |
| 1304 |
#[rustfmt::skip] |
| 1305 |
pub fn recognize_float<T, E:ParseError<T>>() -> impl Parser<T, Output=T,Error= E> |
| 1306 |
where |
| 1307 |
T: Clone + Offset, |
| 1308 |
T: Input, |
| 1309 |
<T as Input>::Item: AsChar, |
| 1310 |
{ |
| 1311 |
recognize(( |
| 1312 |
opt(alt((char('+'), char('-')))), |
| 1313 |
alt(( |
| 1314 |
map((digit1(), opt(pair(char('.'), opt(digit1())))), |_| ()), |
| 1315 |
map((char('.'), digit1()), |_| ()) |
| 1316 |
)), |
| 1317 |
opt(( |
| 1318 |
alt((char('e'), char('E'))), |
| 1319 |
opt(alt((char('+'), char('-')))), |
| 1320 |
cut(digit1()) |
| 1321 |
)) |
| 1322 |
)) |
| 1323 |
} |
| 1324 |
|
| 1325 |
/// float number text parser that also recognizes "nan", "infinity" and "inf" (case insensitive) |
| 1326 |
pub fn recognize_float_or_exceptions<T, E: ParseError<T>>() -> impl Parser<T, Output = T, Error = E> |
| 1327 |
where |
| 1328 |
T: Clone + Offset, |
| 1329 |
T: Input + Compare<&'static str>, |
| 1330 |
<T as Input>::Item: AsChar, |
| 1331 |
{ |
| 1332 |
alt(( |
| 1333 |
recognize_float::<_, E>(), |
| 1334 |
|i: T| { |
| 1335 |
crate::bytes::streaming::tag_no_case::<_, _, E>("nan")(i.clone()) |
| 1336 |
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float))) |
| 1337 |
}, |
| 1338 |
|i: T| { |
| 1339 |
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone()) |
| 1340 |
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float))) |
| 1341 |
}, |
| 1342 |
|i: T| { |
| 1343 |
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone()) |
| 1344 |
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float))) |
| 1345 |
}, |
| 1346 |
)) |
| 1347 |
} |
| 1348 |
|
| 1349 |
/// single precision floating point number parser from text |
| 1350 |
pub fn float<T, E: ParseError<T>>() -> impl Parser<T, Output = f32, Error = E> |
| 1351 |
where |
| 1352 |
T: Clone + Offset, |
| 1353 |
T: Input + crate::traits::ParseTo<f32> + Compare<&'static str>, |
| 1354 |
<T as Input>::Item: AsChar + Clone, |
| 1355 |
T: AsBytes, |
| 1356 |
T: for<'a> Compare<&'a [u8]>, |
| 1357 |
{ |
| 1358 |
Float { |
| 1359 |
o: PhantomData, |
| 1360 |
e: PhantomData, |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
/// double precision floating point number parser from text |
| 1365 |
pub fn double<T, E: ParseError<T>>() -> impl Parser<T, Output = f64, Error = E> |
| 1366 |
where |
| 1367 |
T: Clone + Offset, |
| 1368 |
T: Input + crate::traits::ParseTo<f64> + Compare<&'static str>, |
| 1369 |
<T as Input>::Item: AsChar + Clone, |
| 1370 |
T: AsBytes, |
| 1371 |
T: for<'a> Compare<&'a [u8]>, |
| 1372 |
{ |
| 1373 |
Float { |
| 1374 |
o: PhantomData, |
| 1375 |
e: PhantomData, |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
/// f64 parser from text |
| 1380 |
struct Float<O, E> { |
| 1381 |
o: PhantomData<O>, |
| 1382 |
e: PhantomData<E>, |
| 1383 |
} |
| 1384 |
|
| 1385 |
impl<I, O, E: ParseError<I>> Parser<I> for Float<O, E> |
| 1386 |
where |
| 1387 |
I: Clone + Offset, |
| 1388 |
I: Input + crate::traits::ParseTo<O> + Compare<&'static str>, |
| 1389 |
<I as Input>::Item: AsChar + Clone, |
| 1390 |
I: AsBytes, |
| 1391 |
I: for<'a> Compare<&'a [u8]>, |
| 1392 |
{ |
| 1393 |
type Output = O; |
| 1394 |
type Error = E; |
| 1395 |
|
| 1396 |
fn process<OM: crate::OutputMode>( |
| 1397 |
&mut self, |
| 1398 |
input: I, |
| 1399 |
) -> crate::PResult<OM, I, Self::Output, Self::Error> { |
| 1400 |
let (i, s) = |
| 1401 |
recognize_float_or_exceptions().process::<OutputM<Emit, OM::Error, OM::Incomplete>>(input)?; |
| 1402 |
|
| 1403 |
match s.parse_to() { |
| 1404 |
Some(f) => Ok((i, OM::Output::bind(|| f))), |
| 1405 |
None => Err(crate::Err::Error(OM::Error::bind(|| { |
| 1406 |
E::from_error_kind(i, crate::error::ErrorKind::Float) |
| 1407 |
}))), |
| 1408 |
} |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[cfg(test)] |
| 1413 |
#[cfg(feature = "std")] |
| 1414 |
mod tests { |
| 1415 |
use super::*; |
| 1416 |
use crate::error::ErrorKind; |
| 1417 |
use crate::internal::Err; |
| 1418 |
|
| 1419 |
macro_rules! assert_parse( |
| 1420 |
($left: expr, $right: expr) => { |
| 1421 |
let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; |
| 1422 |
assert_eq!(res, $right); |
| 1423 |
}; |
| 1424 |
); |
| 1425 |
|
| 1426 |
#[test] |
| 1427 |
fn float_test() { |
| 1428 |
let mut test_cases = vec![ |
| 1429 |
"+3.14", |
| 1430 |
"3.14", |
| 1431 |
"-3.14", |
| 1432 |
"0", |
| 1433 |
"0.0", |
| 1434 |
"1.", |
| 1435 |
".789", |
| 1436 |
"-.5", |
| 1437 |
"1e7", |
| 1438 |
"-1E-7", |
| 1439 |
".3e-2", |
| 1440 |
"1.e4", |
| 1441 |
"1.2e4", |
| 1442 |
"12.34", |
| 1443 |
"-1.234E-12", |
| 1444 |
"-1.234e-12", |
| 1445 |
"0.00000000000000000087", |
| 1446 |
]; |
| 1447 |
|
| 1448 |
for test in test_cases.drain(..) { |
| 1449 |
let expected32 = str::parse::<f32>(test).unwrap(); |
| 1450 |
let expected64 = str::parse::<f64>(test).unwrap(); |
| 1451 |
|
| 1452 |
println!("now parsing: {} -> {}", test, expected32); |
| 1453 |
|
| 1454 |
assert_parse!(recognize_float().parse_complete(test), Ok(("", test))); |
| 1455 |
|
| 1456 |
/*assert_parse!(float(test.as_bytes()), Ok((&b""[..], expected32))); |
| 1457 |
assert_parse!(float(test), Ok(("", expected32))); |
| 1458 |
*/ |
| 1459 |
|
| 1460 |
assert_parse!( |
| 1461 |
double().parse_complete(test.as_bytes()), |
| 1462 |
Ok((&b""[..], expected64)) |
| 1463 |
); |
| 1464 |
assert_parse!(double().parse_complete(test), Ok(("", expected64))); |
| 1465 |
} |
| 1466 |
|
| 1467 |
let remaining_exponent = "-1.234E-"; |
| 1468 |
assert_parse!( |
| 1469 |
recognize_float().parse_complete(remaining_exponent), |
| 1470 |
Err(Err::Failure(("", ErrorKind::Digit))) |
| 1471 |
); |
| 1472 |
|
| 1473 |
/*let (_i, nan) = float::<_, ()>("NaN").unwrap(); |
| 1474 |
assert!(nan.is_nan()); |
| 1475 |
|
| 1476 |
let (_i, inf) = float::<_, ()>("inf").unwrap(); |
| 1477 |
assert!(inf.is_infinite()); |
| 1478 |
let (i, inf) = float::<_, ()>("infinity").unwrap(); |
| 1479 |
assert!(inf.is_infinite()); |
| 1480 |
assert!(i.is_empty());*/ |
| 1481 |
} |
| 1482 |
} |