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 e58891e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 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.len() > 2 && (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)),
}
} else {
match text.parse::<f32>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),
}
}
}

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.len() > 2 && (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)),
}
} else {
match text.parse::<f64>() {
Ok(num) => Ok(num),
Err(_) => Err(ParserError::malformed_number(self, context, text)),
}
}
}

Expand Down

0 comments on commit e58891e

Please sign in to comment.