add hex integer literal parsing support

This commit is contained in:
Kaspar Schleiser 2023-05-25 10:29:11 +02:00 committed by ISibboI
parent df869d487b
commit 40d27df446
3 changed files with 23 additions and 1 deletions

View File

@ -27,6 +27,7 @@ regex = { version = "1.5.5", optional = true}
serde = { version = "1.0.133", optional = true}
serde_derive = { version = "1.0.133", optional = true}
rand = { version = "0.8.5", optional = true}
num-traits = "0.2.15"
[features]
serde_support = ["serde", "serde_derive"]

View File

@ -348,7 +348,7 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
},
PartialToken::Literal(literal) => {
cutoff = 1;
if let Ok(number) = literal.parse::<IntType>() {
if let Ok(number) = literal.parse_dec_or_hex::<IntType>() {
Some(Token::Int(number))
} else if let Ok(number) = literal.parse::<FloatType>() {
Some(Token::Float(number))
@ -442,6 +442,21 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult<Vec<Token>> {
partial_tokens_to_tokens(&str_to_partial_tokens(string)?)
}
/// Helper trait to parse strings to integer from both decimal or hex
trait ParseDecOrHex {
fn parse_dec_or_hex<T: num_traits::Num>(&self) -> Result<T, T::FromStrRadixErr>;
}
impl ParseDecOrHex for str {
fn parse_dec_or_hex<T: num_traits::Num>(&self) -> Result<T, T::FromStrRadixErr> {
if self.starts_with("0x") {
T::from_str_radix(&self[2..], 16)
} else {
T::from_str_radix(&self, 10)
}
}
}
#[cfg(test)]
mod tests {
use crate::token::{char_to_partial_token, tokenize, Token};

View File

@ -2291,3 +2291,9 @@ fn test_builtin_functions_context() {
)))
);
}
#[test]
fn test_hex() {
assert_eq!(eval("0x3"), Ok(Value::Int(3)));
assert_eq!(eval("0xFF"), Ok(Value::Int(255)));
}