add hex integer literal parsing support
This commit is contained in:
parent
df869d487b
commit
40d27df446
@ -27,6 +27,7 @@ regex = { version = "1.5.5", optional = true}
|
|||||||
serde = { version = "1.0.133", optional = true}
|
serde = { version = "1.0.133", optional = true}
|
||||||
serde_derive = { version = "1.0.133", optional = true}
|
serde_derive = { version = "1.0.133", optional = true}
|
||||||
rand = { version = "0.8.5", optional = true}
|
rand = { version = "0.8.5", optional = true}
|
||||||
|
num-traits = "0.2.15"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
serde_support = ["serde", "serde_derive"]
|
serde_support = ["serde", "serde_derive"]
|
||||||
|
@ -348,7 +348,7 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
|
|||||||
},
|
},
|
||||||
PartialToken::Literal(literal) => {
|
PartialToken::Literal(literal) => {
|
||||||
cutoff = 1;
|
cutoff = 1;
|
||||||
if let Ok(number) = literal.parse::<IntType>() {
|
if let Ok(number) = literal.parse_dec_or_hex::<IntType>() {
|
||||||
Some(Token::Int(number))
|
Some(Token::Int(number))
|
||||||
} else if let Ok(number) = literal.parse::<FloatType>() {
|
} else if let Ok(number) = literal.parse::<FloatType>() {
|
||||||
Some(Token::Float(number))
|
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)?)
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::token::{char_to_partial_token, tokenize, Token};
|
use crate::token::{char_to_partial_token, tokenize, Token};
|
||||||
|
@ -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)));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user