Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support 0x format in float #35

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@

// 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
Loading