Skip to main content

parse_offset

Function parse_offset 

Source
pub fn parse_offset(input: &str) -> IResult<&str, OffsetSpec>
Expand description

Parse an offset specification for absolute offsets

Supports decimal and hexadecimal formats, both positive and negative.

§Examples

use libmagic_rs::parser::grammar::parse_offset;
use libmagic_rs::parser::ast::OffsetSpec;

assert_eq!(parse_offset("0"), Ok(("", OffsetSpec::Absolute(0))));
assert_eq!(parse_offset("123"), Ok(("", OffsetSpec::Absolute(123))));
assert_eq!(parse_offset("0x10"), Ok(("", OffsetSpec::Absolute(16))));
assert_eq!(parse_offset("-4"), Ok(("", OffsetSpec::Absolute(-4))));
assert_eq!(parse_offset("-0xFF"), Ok(("", OffsetSpec::Absolute(-255))));

§Errors

Returns a nom parsing error if:

  • The input contains invalid number format (propagated from parse_number)
  • Input is empty or contains no parseable offset value
  • The offset value cannot be represented as a valid i64