Skip to content

Commit

Permalink
feat: support "0x" format in float values
Browse files Browse the repository at this point in the history
  • Loading branch information
louiscaron committed Aug 10, 2024
1 parent fb99932 commit e026b13
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions a2lfile/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,16 @@ impl<'a> ParserState<'a> {
pub(crate) fn get_float(&mut self, context: &ParseContext) -> Result<f32, ParserError> {
let token = self.expect_token(context, A2lTokenType::Number)?;
let text = self.get_token_text(token);
match text.parse::<f32>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),
if text.starts_with("0x") || text.starts_with("0X") {
match u64::from_str_radix(&text[2..], 16) {
Ok(num) => Ok(num as f32),
Err(_) => Err(ParserError::malformed_number(self, context, text)),

Check warning on line 604 in a2lfile/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

a2lfile/src/parser.rs#L604

Added line #L604 was not covered by tests
}
} else {
match text.parse::<f32>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),

Check warning on line 609 in a2lfile/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

a2lfile/src/parser.rs#L609

Added line #L609 was not covered by tests
}
}
}

Expand All @@ -610,9 +617,16 @@ impl<'a> ParserState<'a> {
pub(crate) fn get_double(&mut self, context: &ParseContext) -> Result<f64, ParserError> {
let token = self.expect_token(context, A2lTokenType::Number)?;
let text = self.get_token_text(token);
match text.parse::<f64>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),
if text.starts_with("0x") || text.starts_with("0X") {
match u64::from_str_radix(&text[2..], 16) {
Ok(num) => Ok(num as f64),
Err(_) => Err(ParserError::malformed_number(self, context, text)),

Check warning on line 623 in a2lfile/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

a2lfile/src/parser.rs#L621-L623

Added lines #L621 - L623 were not covered by tests
}
} else {
match text.parse::<f64>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),

Check warning on line 628 in a2lfile/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

a2lfile/src/parser.rs#L628

Added line #L628 was not covered by tests
}
}
}

Expand Down Expand Up @@ -1154,7 +1168,9 @@ mod tests {

// float: 0x11
let res = parser.get_float(&context);
assert!(res.is_err());
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val, 17f32);

// float: 1.0e+2
let res = parser.get_float(&context);
Expand Down

0 comments on commit e026b13

Please sign in to comment.