winnow::ascii

Function hex_uint

Source
pub fn hex_uint<Input, Output, Error>(
    input: &mut Input,
) -> PResult<Output, Error>
where Input: StreamIsPartial + Stream, <Input as Stream>::Token: AsChar, <Input as Stream>::Slice: AsBStr, Output: HexUint, Error: ParserError<Input>,
Expand description

Decode a variable-width hexadecimal integer (e.g. u32)

Complete version: Will parse until the end of input if it has fewer characters than the type supports.

[Partial version][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_)) if end-of-input is hit before a hard boundary (non-hex character, more characters than supported).

§Effective Signature

Assuming you are parsing a &str Stream into a u32:

pub fn hex_uint(input: &mut &str) -> PResult<u32>

§Example

use winnow::ascii::hex_uint;

fn parser<'s>(s: &mut &'s [u8]) -> PResult<u32, InputError<&'s [u8]>> {
  hex_uint(s)
}

assert_eq!(parser.parse_peek(&b"01AE"[..]), Ok((&b""[..], 0x01AE)));
assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b""[..], 0x0ABC)));
assert_eq!(parser.parse_peek(&b"ggg"[..]), Err(ErrMode::Backtrack(InputError::new(&b"ggg"[..], ErrorKind::Slice))));
use winnow::ascii::hex_uint;

fn parser<'s>(s: &mut Partial<&'s [u8]>) -> PResult<u32, InputError<Partial<&'s [u8]>>> {
  hex_uint(s)
}

assert_eq!(parser.parse_peek(Partial::new(&b"01AE;"[..])), Ok((Partial::new(&b";"[..]), 0x01AE)));
assert_eq!(parser.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(parser.parse_peek(Partial::new(&b"ggg"[..])), Err(ErrMode::Backtrack(InputError::new(Partial::new(&b"ggg"[..]), ErrorKind::Slice))));